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  # macro parameters are not variables so a temporary is needed to work with
24  # list().
25  set(msg ${ARGN})
26  # message(${ARGN}) will merge all list elements with no separator while
27  # "${ARGN}" will output the list as a ';' delimited string.
28  list(JOIN msg " " msg)
29  message(FATAL_ERROR "${msg}")
30endmacro()
31
32# Converts semi-colon delimited list variable(s) to string. Output is written to
33# variable supplied via the DEST parameter. Input is from an expanded variable
34# referenced by SOURCE and/or variable(s) referenced by SOURCE_VARS.
35macro(libgav1_set_and_stringify)
36  set(optional_args)
37  set(single_value_args DEST SOURCE_VAR)
38  set(multi_value_args SOURCE SOURCE_VARS)
39  cmake_parse_arguments(sas "${optional_args}" "${single_value_args}"
40                        "${multi_value_args}" ${ARGN})
41
42  if(NOT sas_DEST OR NOT (sas_SOURCE OR sas_SOURCE_VARS))
43    libgav1_die("libgav1_set_and_stringify: DEST and at least one of SOURCE "
44                "SOURCE_VARS required.")
45  endif()
46
47  unset(${sas_DEST})
48
49  if(sas_SOURCE)
50    # $sas_SOURCE is one or more expanded variables, just copy the values to
51    # $sas_DEST.
52    set(${sas_DEST} "${sas_SOURCE}")
53  endif()
54
55  if(sas_SOURCE_VARS)
56    # $sas_SOURCE_VARS is one or more variable names. Each iteration expands a
57    # variable and appends it to $sas_DEST.
58    foreach(source_var ${sas_SOURCE_VARS})
59      set(${sas_DEST} "${${sas_DEST}} ${${source_var}}")
60    endforeach()
61
62    # Because $sas_DEST can be empty when entering this scope leading whitespace
63    # can be introduced to $sas_DEST on the first iteration of the above loop.
64    # Remove it:
65    string(STRIP "${${sas_DEST}}" ${sas_DEST})
66  endif()
67
68  # Lists in CMake are simply semicolon delimited strings, so stringification is
69  # just a find and replace of the semicolon.
70  string(REPLACE ";" " " ${sas_DEST} "${${sas_DEST}}")
71
72  if(LIBGAV1_VERBOSE GREATER 1)
73    message("libgav1_set_and_stringify: ${sas_DEST}=${${sas_DEST}}")
74  endif()
75endmacro()
76
77# Creates a dummy source file in $LIBGAV1_GENERATED_SOURCES_DIRECTORY and adds
78# it to the specified target. Optionally adds its path to a list variable.
79#
80# libgav1_create_dummy_source_file(<TARGET <target> BASENAME <basename of file>>
81# [LISTVAR <list variable>])
82macro(libgav1_create_dummy_source_file)
83  set(optional_args)
84  set(single_value_args TARGET BASENAME LISTVAR)
85  set(multi_value_args)
86  cmake_parse_arguments(cdsf "${optional_args}" "${single_value_args}"
87                        "${multi_value_args}" ${ARGN})
88
89  if(NOT cdsf_TARGET OR NOT cdsf_BASENAME)
90    libgav1_die(
91      "libgav1_create_dummy_source_file: TARGET and BASENAME required.")
92  endif()
93
94  if(NOT LIBGAV1_GENERATED_SOURCES_DIRECTORY)
95    set(LIBGAV1_GENERATED_SOURCES_DIRECTORY "${libgav1_build}/gen_src")
96  endif()
97
98  set(dummy_source_dir "${LIBGAV1_GENERATED_SOURCES_DIRECTORY}")
99  set(dummy_source_file
100      "${dummy_source_dir}/libgav1_${cdsf_TARGET}_${cdsf_BASENAME}.cc")
101  set(dummy_source_code
102      "// Generated file. DO NOT EDIT!\n"
103      "// C++ source file created for target ${cdsf_TARGET}.\n"
104      "void libgav1_${cdsf_TARGET}_${cdsf_BASENAME}_dummy_function(void)\;\n"
105      "void libgav1_${cdsf_TARGET}_${cdsf_BASENAME}_dummy_function(void) {}\n")
106  file(WRITE "${dummy_source_file}" ${dummy_source_code})
107
108  target_sources(${cdsf_TARGET} PRIVATE ${dummy_source_file})
109
110  if(cdsf_LISTVAR)
111    list(APPEND ${cdsf_LISTVAR} "${dummy_source_file}")
112  endif()
113endmacro()
114
115# Loads the version components from $libgav1_source/gav1/version.h and sets the
116# corresponding CMake variables:
117# - LIBGAV1_MAJOR_VERSION
118# - LIBGAV1_MINOR_VERSION
119# - LIBGAV1_PATCH_VERSION
120# - LIBGAV1_VERSION, which is:
121#   - $LIBGAV1_MAJOR_VERSION.$LIBGAV1_MINOR_VERSION.$LIBGAV1_PATCH_VERSION
122macro(libgav1_load_version_info)
123  file(STRINGS "${libgav1_source}/gav1/version.h" version_file_strings)
124  foreach(str ${version_file_strings})
125    if(str MATCHES "#define LIBGAV1_")
126      if(str MATCHES "#define LIBGAV1_MAJOR_VERSION ")
127        string(REPLACE "#define LIBGAV1_MAJOR_VERSION " "" LIBGAV1_MAJOR_VERSION
128                       "${str}")
129      elseif(str MATCHES "#define LIBGAV1_MINOR_VERSION ")
130        string(REPLACE "#define LIBGAV1_MINOR_VERSION " "" LIBGAV1_MINOR_VERSION
131                       "${str}")
132      elseif(str MATCHES "#define LIBGAV1_PATCH_VERSION ")
133        string(REPLACE "#define LIBGAV1_PATCH_VERSION " "" LIBGAV1_PATCH_VERSION
134                       "${str}")
135      endif()
136    endif()
137  endforeach()
138  set(LIBGAV1_VERSION "${LIBGAV1_MAJOR_VERSION}.${LIBGAV1_MINOR_VERSION}")
139  set(LIBGAV1_VERSION "${LIBGAV1_VERSION}.${LIBGAV1_PATCH_VERSION}")
140endmacro()
141