xref: /aosp_15_r20/external/libaom/build/cmake/util.cmake (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 a no-op source file in $AOM_GEN_SRC_DIR named $basename.$extension and
20# returns the full path to the source file via appending it to the list variable
21# referred to by $out_file_list_var parameter.
22macro(create_no_op_source_file basename extension out_file_list_var)
23  set(no_op_source_file "${AOM_GEN_SRC_DIR}/${basename}_no_op.${extension}")
24  file(WRITE "${no_op_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}_no_op_function(void);\n"
29       "void aom_${target_name}_no_op_function(void) {}\n")
30  list(APPEND "${out_file_list_var}" "${no_op_source_file}")
31endmacro()
32
33# Convenience function for adding a no-op source file to $target_name using
34# $extension as the file extension. Wraps create_no_op_source_file().
35function(add_no_op_source_file_to_target target_name extension)
36  create_no_op_source_file("${target_name}" "${extension}"
37                           "no_op_source_file_list")
38  target_sources(${target_name} PRIVATE ${no_op_source_file_list})
39endfunction()
40
41# Sets the value of the variable referenced by $feature to $value, and reports
42# the change to the user via call to message(WARNING ...). $cause is expected to
43# be a configuration variable that conflicts with $feature in some way. This
44# function is a no-op if $feature is already set to $value.
45function(change_config_and_warn feature value cause)
46  if(${feature} EQUAL ${value})
47    return()
48  endif()
49  set(${feature} ${value} PARENT_SCOPE)
50  if(${value} EQUAL 1)
51    set(verb "Enabled")
52    set(reason "required for")
53  else()
54    set(verb "Disabled")
55    set(reason "incompatible with")
56  endif()
57  set(warning_message "${verb} ${feature}, ${reason} ${cause}.")
58  message(WARNING "--- ${warning_message}")
59endfunction()
60
61# Extracts the version string from $version_file and returns it to the user via
62# $version_string_out_var. To achieve this VERSION_STRING_NOSP is located in
63# $version_file and then everything but the string literal assigned to the
64# variable is removed. Quotes and the leading 'v' are stripped from the returned
65# string.
66function(extract_version_string version_file version_string_out_var)
67  file(STRINGS "${version_file}" aom_version REGEX "VERSION_STRING_NOSP")
68  string(REPLACE "#define VERSION_STRING_NOSP " "" aom_version "${aom_version}")
69  string(REPLACE "\"" "" aom_version "${aom_version}")
70  string(REPLACE " " "" aom_version "${aom_version}")
71  string(FIND "${aom_version}" "v" v_pos)
72  if(${v_pos} EQUAL 0)
73    string(SUBSTRING "${aom_version}" 1 -1 aom_version)
74  endif()
75  set("${version_string_out_var}" "${aom_version}" PARENT_SCOPE)
76endfunction()
77
78# Sets CMake compiler launcher to $launcher_name when $launcher_name is found in
79# $PATH. Warns user about ignoring build flag $launcher_flag when $launcher_name
80# is not found in $PATH.
81function(set_compiler_launcher launcher_flag launcher_name)
82  find_program(launcher_path "${launcher_name}")
83  if(launcher_path)
84    set(CMAKE_C_COMPILER_LAUNCHER "${launcher_path}" PARENT_SCOPE)
85    set(CMAKE_CXX_COMPILER_LAUNCHER "${launcher_path}" PARENT_SCOPE)
86    message("--- Using ${launcher_name} as compiler launcher.")
87  else()
88    message(
89      WARNING "--- Cannot find ${launcher_name}, ${launcher_flag} ignored.")
90  endif()
91endfunction()
92
93# Sentinel value used to detect when a variable has been set via the -D argument
94# passed to CMake on the command line.
95set(cmake_cmdline_helpstring "No help, variable specified on the command line.")
96
97# Wrapper macro for set() that does some book keeping to help with storage of
98# build configuration information.
99#
100# Sets the default value for variable $name when the value of $name has not
101# already been set via the CMake command line.
102#
103# The names of variables defaulted through this macro are added to
104# $AOM_DETECT_VARS to facilitate build logging and diagnostics.
105macro(set_aom_detect_var name value helpstring)
106  unset(list_index)
107  list(FIND AOM_DETECT_VARS ${name} list_index)
108  if(${list_index} EQUAL -1)
109    list(APPEND AOM_DETECT_VARS ${name})
110  endif()
111
112  # Update the variable only when it does not carry the CMake assigned help
113  # string for variables specified via the command line.
114  unset(cache_helpstring)
115  get_property(cache_helpstring CACHE ${name} PROPERTY HELPSTRING)
116  if(NOT "${cache_helpstring}" STREQUAL "${cmake_cmdline_helpstring}")
117    set(${name} ${value} CACHE STRING "${helpstring}")
118    mark_as_advanced(${name})
119  else()
120    message(
121      WARNING
122        "${name} has been set by CMake, but it may be overridden by the build "
123        "system during environment detection")
124  endif()
125endmacro()
126
127# Wrapper macro for set() that does some book keeping to help with storage of
128# build configuration information.
129#
130# Sets the default value for variable $name when the value of $name has not
131# already been set via the CMake command line.
132#
133# The names of variables defaulted through this macro are added to
134# $AOM_CONFIG_VARS to facilitate build logging and diagnostics.
135macro(set_aom_config_var name value helpstring)
136  unset(list_index)
137  list(FIND AOM_CONFIG_VARS ${name} list_index)
138  if(${list_index} EQUAL -1)
139    list(APPEND AOM_CONFIG_VARS ${name})
140  endif()
141
142  # Update the variable only when it does not carry the CMake assigned help
143  # string for variables specified via the command line.
144  unset(cache_helpstring)
145  get_property(cache_helpstring CACHE ${name} PROPERTY HELPSTRING)
146  if(NOT "${cache_helpstring}" STREQUAL "${cmake_cmdline_helpstring}")
147    set(${name} ${value} CACHE STRING "${helpstring}")
148  endif()
149endmacro()
150
151# Wrapper macro for option() that does some book keeping to help with storage of
152# build configuration information.
153#
154# Sets the default value for variable $name when the value of $name has not
155# already been set via the CMake command line.
156#
157# The names of variables defaulted through this macro are added to
158# $AOM_OPTION_VARS to facilitate build logging and diagnostics.
159macro(set_aom_option_var name helpstring value)
160  unset(list_index)
161  list(FIND AOM_OPTION_VARS ${name} list_index)
162  if(${list_index} EQUAL -1)
163    list(APPEND AOM_OPTION_VARS ${name})
164  endif()
165
166  # Update the variable only when it does not carry the CMake assigned help
167  # string for variables specified via the command line.
168  unset(cache_helpstring)
169  get_property(cache_helpstring CACHE ${name} PROPERTY HELPSTRING)
170  if(NOT "${cache_helpstring}" STREQUAL "${cmake_cmdline_helpstring}")
171    option(${name} "${helpstring}" ${value})
172  endif()
173endmacro()
174