• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright (c) 2017, Alliance for Open Media. All rights reserved
3#
4# This source code is subject to the terms of the BSD 2 Clause License and the
5# Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License was
6# not distributed with this source code in the LICENSE file, you can obtain it
7# at www.aomedia.org/license/software. If the Alliance for Open Media Patent
8# License 1.0 was not distributed with this source code in the PATENTS file, you
9# can obtain it at www.aomedia.org/license/patent.
10#
11if(AOM_BUILD_CMAKE_UTIL_CMAKE_)
12  return()
13endif() # AOM_BUILD_CMAKE_UTIL_CMAKE_
14set(AOM_BUILD_CMAKE_UTIL_CMAKE_ 1)
15
16# Directory where generated sources will be written.
17set(AOM_GEN_SRC_DIR "${AOM_CONFIG_DIR}/gen_src")
18
19# Creates dummy source file in $AOM_GEN_SRC_DIR named $basename.$extension and
20# returns the full path to the dummy source file via appending it to the list
21# variable referred to by $out_file_list_var parameter.
22macro(create_dummy_source_file basename extension out_file_list_var)
23  set(dummy_source_file "${AOM_GEN_SRC_DIR}/${basename}_dummy.${extension}")
24  file(WRITE "${dummy_source_file}"
25       "// Generated file. DO NOT EDIT!\n"
26       "// ${target_name} needs a ${extension} file to force link language, \n"
27       "// or to silence a harmless CMake warning: Ignore me.\n"
28       "void aom_${target_name}_dummy_function(void) {}\n")
29  list(APPEND "${out_file_list_var}" "${dummy_source_file}")
30endmacro()
31
32# Convenience function for adding a dummy source file to $target_name using
33# $extension as the file extension. Wraps create_dummy_source_file().
34function(add_dummy_source_file_to_target target_name extension)
35  create_dummy_source_file("${target_name}" "${extension}"
36                           "dummy_source_file_list")
37  target_sources(${target_name} PRIVATE ${dummy_source_file_list})
38endfunction()
39
40# Sets the value of the variable referenced by $feature to $value, and reports
41# the change to the user via call to message(WARNING ...). $cause is expected to
42# be a configuration variable that conflicts with $feature in some way. This
43# function is a noop if $feature is already set to $value.
44function(change_config_and_warn feature value cause)
45  if(${feature} EQUAL ${value})
46    return()
47  endif()
48  set(${feature} ${value} PARENT_SCOPE)
49  if(${value} EQUAL 1)
50    set(verb "Enabled")
51    set(reason "required for")
52  else()
53    set(verb "Disabled")
54    set(reason "incompatible with")
55  endif()
56  set(warning_message "${verb} ${feature}, ${reason} ${cause}.")
57  message(WARNING "--- ${warning_message}")
58endfunction()
59
60# Extracts the version string from $version_file and returns it to the user via
61# $version_string_out_var. To achieve this VERSION_STRING_NOSP is located in
62# $version_file and then everything but the string literal assigned to the
63# variable is removed. Quotes and the leading 'v' are stripped from the returned
64# string.
65function(extract_version_string version_file version_string_out_var)
66  file(STRINGS "${version_file}" aom_version REGEX "VERSION_STRING_NOSP")
67  string(REPLACE "#define VERSION_STRING_NOSP " "" aom_version "${aom_version}")
68  string(REPLACE "\"" "" aom_version "${aom_version}")
69  string(REPLACE " " "" aom_version "${aom_version}")
70  string(FIND "${aom_version}" "v" v_pos)
71  if(${v_pos} EQUAL 0)
72    string(SUBSTRING "${aom_version}" 1 -1 aom_version)
73  endif()
74  set("${version_string_out_var}" "${aom_version}" PARENT_SCOPE)
75endfunction()
76
77# Sets CMake compiler launcher to $launcher_name when $launcher_name is found in
78# $PATH. Warns user about ignoring build flag $launcher_flag when $launcher_name
79# is not found in $PATH.
80function(set_compiler_launcher launcher_flag launcher_name)
81  find_program(launcher_path "${launcher_name}")
82  if(launcher_path)
83    set(CMAKE_C_COMPILER_LAUNCHER "${launcher_path}" PARENT_SCOPE)
84    set(CMAKE_CXX_COMPILER_LAUNCHER "${launcher_path}" PARENT_SCOPE)
85    message("--- Using ${launcher_name} as compiler launcher.")
86  else()
87    message(
88      WARNING "--- Cannot find ${launcher_name}, ${launcher_flag} ignored.")
89  endif()
90endfunction()
91
92# Sentinel value used to detect when a variable has been set via the -D argument
93# passed to CMake on the command line.
94set(cmake_cmdline_helpstring "No help, variable specified on the command line.")
95
96# Wrapper macro for set() that does some book keeping to help with storage of
97# build configuration information.
98#
99# Sets the default value for variable $name when the value of $name has not
100# already been set via the CMake command line.
101#
102# The names of variables defaulted through this macro are added to
103# $AOM_CONFIG_VARS to facilitate build logging and diagnostics.
104macro(set_aom_detect_var name value helpstring)
105  unset(list_index)
106  list(FIND AOM_DETECT_VARS ${name} list_index)
107  if(${list_index} EQUAL -1)
108    list(APPEND AOM_DETECT_VARS ${name})
109  endif()
110
111  # Update the variable only when it does not carry the CMake assigned help
112  # string for variables specified via the command line.
113  unset(cache_helpstring)
114  get_property(cache_helpstring CACHE ${name} PROPERTY HELPSTRING)
115  if(NOT "${cache_helpstring}" STREQUAL "${cmake_cmdline_helpstring}")
116    set(${name} ${value} CACHE STRING "${helpstring}")
117    mark_as_advanced(${name})
118  else()
119    message(
120      WARNING
121        "${name} has been set by CMake, but it may be overridden by the build "
122        "system during environment detection")
123  endif()
124endmacro()
125
126# Wrapper macro for set() that does some book keeping to help with storage of
127# build configuration information.
128#
129# Sets the default value for variable $name when the value of $name has not
130# already been set via the CMake command line.
131#
132# The names of variables defaulted through this macro are added to
133# $AOM_CONFIG_VARS to facilitate build logging and diagnostics.
134macro(set_aom_config_var name value helpstring)
135  unset(list_index)
136  list(FIND AOM_CONFIG_VARS ${name} list_index)
137  if(${list_index} EQUAL -1)
138    list(APPEND AOM_CONFIG_VARS ${name})
139  endif()
140
141  # Update the variable only when it does not carry the CMake assigned help
142  # string for variables specified via the command line.
143  unset(cache_helpstring)
144  get_property(cache_helpstring CACHE ${name} PROPERTY HELPSTRING)
145  if(NOT "${cache_helpstring}" STREQUAL "${cmake_cmdline_helpstring}")
146    set(${name} ${value} CACHE STRING "${helpstring}")
147  endif()
148endmacro()
149
150# Wrapper macro for option() that does some book keeping to help with storage of
151# build configuration information.
152#
153# Sets the default value for variable $name when the value of $name has not
154# already been set via the CMake command line.
155#
156# The names of variables defaulted through this macro are added to
157# $AOM_OPTION_VARS to facilitate build logging and diagnostics.
158macro(set_aom_option_var name helpstring value)
159  unset(list_index)
160  list(FIND AOM_OPTION_VARS ${name} list_index)
161  if(${list_index} EQUAL -1)
162    list(APPEND AOM_OPTION_VARS ${name})
163  endif()
164
165  # Update the variable only when it does not carry the CMake assigned help
166  # string for variables specified via the command line.
167  unset(cache_helpstring)
168  get_property(cache_helpstring CACHE ${name} PROPERTY HELPSTRING)
169  if(NOT "${cache_helpstring}" STREQUAL "${cmake_cmdline_helpstring}")
170    option(${name} "${helpstring}" ${value})
171  endif()
172endmacro()
173