1# Copyright 2019 The libgav1 Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15if(LIBGAV1_CMAKE_LIBGAV1_HELPERS_CMAKE_)
16  return()
17endif() # LIBGAV1_CMAKE_LIBGAV1_HELPERS_CMAKE_
18set(LIBGAV1_CMAKE_LIBGAV1_HELPERS_CMAKE_ 1)
19
20# Kills build generation using message(FATAL_ERROR) and outputs all data passed
21# to the console via use of $ARGN.
22macro(libgav1_die)
23  message(FATAL_ERROR ${ARGN})
24endmacro()
25
26# Converts semi-colon delimited list variable(s) to string. Output is written to
27# variable supplied via the DEST parameter. Input is from an expanded variable
28# referenced by SOURCE and/or variable(s) referenced by SOURCE_VARS.
29macro(libgav1_set_and_stringify)
30  set(optional_args)
31  set(single_value_args DEST SOURCE_VAR)
32  set(multi_value_args SOURCE SOURCE_VARS)
33  cmake_parse_arguments(sas "${optional_args}" "${single_value_args}"
34                        "${multi_value_args}" ${ARGN})
35
36  if(NOT sas_DEST OR NOT (sas_SOURCE OR sas_SOURCE_VARS))
37    libgav1_die("libgav1_set_and_stringify: DEST and at least one of SOURCE "
38                "SOURCE_VARS required.")
39  endif()
40
41  unset(${sas_DEST})
42
43  if(sas_SOURCE)
44    # $sas_SOURCE is one or more expanded variables, just copy the values to
45    # $sas_DEST.
46    set(${sas_DEST} "${sas_SOURCE}")
47  endif()
48
49  if(sas_SOURCE_VARS)
50    # $sas_SOURCE_VARS is one or more variable names. Each iteration expands a
51    # variable and appends it to $sas_DEST.
52    foreach(source_var ${sas_SOURCE_VARS})
53      set(${sas_DEST} "${${sas_DEST}} ${${source_var}}")
54    endforeach()
55
56    # Because $sas_DEST can be empty when entering this scope leading whitespace
57    # can be introduced to $sas_DEST on the first iteration of the above loop.
58    # Remove it:
59    string(STRIP "${${sas_DEST}}" ${sas_DEST})
60  endif()
61
62  # Lists in CMake are simply semicolon delimited strings, so stringification is
63  # just a find and replace of the semicolon.
64  string(REPLACE ";" " " ${sas_DEST} "${${sas_DEST}}")
65
66  if(LIBGAV1_VERBOSE GREATER 1)
67    message("libgav1_set_and_stringify: ${sas_DEST}=${${sas_DEST}}")
68  endif()
69endmacro()
70
71# Creates a dummy source file in $LIBGAV1_GENERATED_SOURCES_DIRECTORY and adds
72# it to the specified target. Optionally adds its path to a list variable.
73#
74# libgav1_create_dummy_source_file(<TARGET <target> BASENAME <basename of file>>
75# [LISTVAR <list variable>])
76macro(libgav1_create_dummy_source_file)
77  set(optional_args)
78  set(single_value_args TARGET BASENAME LISTVAR)
79  set(multi_value_args)
80  cmake_parse_arguments(cdsf "${optional_args}" "${single_value_args}"
81                        "${multi_value_args}" ${ARGN})
82
83  if(NOT cdsf_TARGET OR NOT cdsf_BASENAME)
84    libgav1_die(
85      "libgav1_create_dummy_source_file: TARGET and BASENAME required.")
86  endif()
87
88  if(NOT LIBGAV1_GENERATED_SOURCES_DIRECTORY)
89    set(LIBGAV1_GENERATED_SOURCES_DIRECTORY "${libgav1_build}/gen_src")
90  endif()
91
92  set(dummy_source_dir "${LIBGAV1_GENERATED_SOURCES_DIRECTORY}")
93  set(dummy_source_file
94      "${dummy_source_dir}/libgav1_${cdsf_TARGET}_${cdsf_BASENAME}.cc")
95  set(dummy_source_code
96      "// Generated file. DO NOT EDIT!\n"
97      "// C++ source file created for target ${cdsf_TARGET}. \n"
98      "void libgav1_${cdsf_TARGET}_${cdsf_BASENAME}_dummy_function(void);\n"
99      "void libgav1_${cdsf_TARGET}_${cdsf_BASENAME}_dummy_function(void) {}\n")
100  file(WRITE "${dummy_source_file}" "${dummy_source_code}")
101
102  target_sources(${cdsf_TARGET} PRIVATE ${dummy_source_file})
103
104  if(cdsf_LISTVAR)
105    list(APPEND ${cdsf_LISTVAR} "${dummy_source_file}")
106  endif()
107endmacro()
108
109# Loads the version components from $libgav1_source/gav1/version.h and sets the
110# corresponding CMake variables:
111# - LIBGAV1_MAJOR_VERSION
112# - LIBGAV1_MINOR_VERSION
113# - LIBGAV1_PATCH_VERSION
114# - LIBGAV1_VERSION, which is:
115#   - $LIBGAV1_MAJOR_VERSION.$LIBGAV1_MINOR_VERSION.$LIBGAV1_PATCH_VERSION
116macro(libgav1_load_version_info)
117  file(STRINGS "${libgav1_source}/gav1/version.h" version_file_strings)
118  foreach(str ${version_file_strings})
119    if(str MATCHES "#define LIBGAV1_")
120      if(str MATCHES "#define LIBGAV1_MAJOR_VERSION ")
121        string(REPLACE "#define LIBGAV1_MAJOR_VERSION " "" LIBGAV1_MAJOR_VERSION
122                       "${str}")
123      elseif(str MATCHES "#define LIBGAV1_MINOR_VERSION ")
124        string(REPLACE "#define LIBGAV1_MINOR_VERSION " "" LIBGAV1_MINOR_VERSION
125                       "${str}")
126      elseif(str MATCHES "#define LIBGAV1_PATCH_VERSION ")
127        string(REPLACE "#define LIBGAV1_PATCH_VERSION " "" LIBGAV1_PATCH_VERSION
128                       "${str}")
129      endif()
130    endif()
131  endforeach()
132  set(LIBGAV1_VERSION "${LIBGAV1_MAJOR_VERSION}.${LIBGAV1_MINOR_VERSION}")
133  set(LIBGAV1_VERSION "${LIBGAV1_VERSION}.${LIBGAV1_PATCH_VERSION}")
134endmacro()
135