• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright (c) 2016, 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_COMPILER_FLAGS_CMAKE_)
12  return()
13endif() # AOM_BUILD_CMAKE_COMPILER_FLAGS_CMAKE_
14set(AOM_BUILD_CMAKE_COMPILER_FLAGS_CMAKE_ 1)
15
16include(CheckCCompilerFlag)
17include(CheckCXXCompilerFlag)
18include("${AOM_ROOT}/build/cmake/compiler_tests.cmake")
19
20# Strings used to cache flags.
21set(AOM_C_FLAGS)
22set(AOM_CXX_FLAGS)
23set(AOM_EXE_LINKER_FLAGS)
24set(AOM_FAILED_C_FLAGS)
25set(AOM_FAILED_CXX_FLAGS)
26
27# Sets variable named by $out_is_present to YES in the caller's scope when $flag
28# is found in the string variable named by $flag_cache. Sets the var to NO
29# otherwise.
30function(is_flag_present flag_cache flag out_is_present)
31  string(FIND "${${flag_cache}}" "${flag}" flag_pos)
32  if(${flag_pos} EQUAL -1)
33    set(${out_is_present} NO PARENT_SCOPE)
34  else()
35    set(${out_is_present} YES PARENT_SCOPE)
36  endif()
37endfunction()
38
39# Appends $flag to $flags. Ignores scope via use of FORCE with set() call.
40function(append_flag flags flag)
41  string(FIND "${${flags}}" "${flag}" found)
42  if(${found} EQUAL -1)
43    set(${flags} "${${flags}} ${flag}" CACHE STRING "" FORCE)
44  endif()
45endfunction()
46
47# Checks C compiler for support of $c_flag. Adds $c_flag to all
48# $CMAKE_C_FLAGS_<CONFIG>s stored in AOM_C_CONFIGS when the compile test passes.
49# Caches $c_flag in $AOM_C_FLAGS or $AOM_FAILED_C_FLAGS depending on test
50# outcome.
51function(add_c_flag_if_supported c_flag)
52  if(DEBUG_CMAKE_DISABLE_COMPILER_TESTS)
53    return()
54  endif()
55
56  is_flag_present(AOM_C_FLAGS "${c_flag}" flag_ok)
57  is_flag_present(AOM_FAILED_C_FLAGS "${c_flag}" flag_failed)
58  if(${flag_ok} OR ${flag_failed})
59    return()
60  endif()
61
62  # Between 3.17.0 and 3.18.2 check_c_compiler_flag() sets a normal variable at
63  # parent scope while check_cxx_source_compiles() continues to set an internal
64  # cache variable, so we unset both to avoid the failure / success state
65  # persisting between checks. See
66  # https://gitlab.kitware.com/cmake/cmake/-/issues/21207.
67  unset(C_FLAG_SUPPORTED)
68  unset(C_FLAG_SUPPORTED CACHE)
69  message("Checking C compiler flag support for: " ${c_flag})
70  check_c_compiler_flag("${c_flag}" C_FLAG_SUPPORTED)
71
72  if(${C_FLAG_SUPPORTED})
73    append_flag(AOM_C_FLAGS "${c_flag}")
74    foreach(config ${AOM_C_CONFIGS})
75      unset(C_FLAG_FOUND)
76      append_flag("${config}" "${c_flag}")
77    endforeach()
78  else()
79    append_flag(AOM_FAILED_C_FLAGS "${c_flag}")
80  endif()
81endfunction()
82
83# Checks C++ compiler for support of $cxx_flag. Adds $cxx_flag to all
84# $CMAKE_CXX_FLAGS_<CONFIG>s stored in AOM_CXX_CONFIGS when the compile test
85# passes. Caches $cxx_flag in $AOM_CXX_FLAGS or $AOM_FAILED_CXX_FLAGS depending
86# on test outcome.
87function(add_cxx_flag_if_supported cxx_flag)
88  if(DEBUG_CMAKE_DISABLE_COMPILER_TESTS)
89    return()
90  endif()
91
92  is_flag_present(AOM_CXX_FLAGS "${cxx_flag}" flag_ok)
93  is_flag_present(AOM_FAILED_CXX_FLAGS "${cxx_flag}" flag_failed)
94  if(${flag_ok} OR ${flag_failed})
95    return()
96  endif()
97
98  # Between 3.17.0 and 3.18.2 check_cxx_compiler_flag() sets a normal variable
99  # at parent scope while check_cxx_source_compiles() continues to set an
100  # internal cache variable, so we unset both to avoid the failure / success
101  # state persisting between checks. See
102  # https://gitlab.kitware.com/cmake/cmake/-/issues/21207.
103  unset(CXX_FLAG_SUPPORTED)
104  unset(CXX_FLAG_SUPPORTED CACHE)
105  message("Checking C++ compiler flag support for: " ${cxx_flag})
106  check_cxx_compiler_flag("${cxx_flag}" CXX_FLAG_SUPPORTED)
107
108  if(${CXX_FLAG_SUPPORTED})
109    append_flag(AOM_CXX_FLAGS "${cxx_flag}")
110    foreach(config ${AOM_CXX_CONFIGS})
111      unset(CXX_FLAG_FOUND)
112      append_flag("${config}" "${cxx_flag}")
113    endforeach()
114  else()
115    append_flag(AOM_FAILED_CXX_FLAGS "${cxx_flag}")
116  endif()
117endfunction()
118
119# Convenience method for adding a flag to both the C and C++ compiler command
120# lines.
121function(add_compiler_flag_if_supported flag)
122  add_c_flag_if_supported(${flag})
123  add_cxx_flag_if_supported(${flag})
124endfunction()
125
126# Checks C compiler for support of $c_flag and terminates generation when
127# support is not present.
128function(require_c_flag c_flag update_c_flags)
129  if(DEBUG_CMAKE_DISABLE_COMPILER_TESTS)
130    return()
131  endif()
132
133  is_flag_present(AOM_C_FLAGS "${c_flag}" flag_ok)
134  if(${flag_ok})
135    return()
136  endif()
137
138  if(NOT "${AOM_EXE_LINKER_FLAGS}" STREQUAL "")
139    aom_push_var(CMAKE_EXE_LINKER_FLAGS "${AOM_EXE_LINKER_FLAGS}")
140  endif()
141
142  unset(HAVE_C_FLAG CACHE)
143  message("Checking C compiler flag support for: " ${c_flag})
144  check_c_compiler_flag("${c_flag}" HAVE_C_FLAG)
145  if(NOT HAVE_C_FLAG)
146    message(
147      FATAL_ERROR "${PROJECT_NAME} requires support for C flag: ${c_flag}.")
148  endif()
149
150  if(NOT "${AOM_EXE_LINKER_FLAGS}" STREQUAL "")
151    aom_pop_var(CMAKE_EXE_LINKER_FLAGS)
152  endif()
153
154  append_flag(AOM_C_FLAGS "${c_flag}")
155  if(update_c_flags)
156    foreach(config ${AOM_C_CONFIGS})
157      set(${config} "${${config}} ${c_flag}" CACHE STRING "" FORCE)
158    endforeach()
159  endif()
160endfunction()
161
162# Checks CXX compiler for support of $cxx_flag and terminates generation when
163# support is not present.
164function(require_cxx_flag cxx_flag update_cxx_flags)
165  if(DEBUG_CMAKE_DISABLE_COMPILER_TESTS)
166    return()
167  endif()
168
169  is_flag_present(AOM_CXX_FLAGS "${cxx_flag}" flag_ok)
170  if(${flag_ok})
171    return()
172  endif()
173
174  if(NOT "${AOM_EXE_LINKER_FLAGS}" STREQUAL "")
175    aom_push_var(CMAKE_EXE_LINKER_FLAGS "${AOM_EXE_LINKER_FLAGS}")
176  endif()
177
178  unset(HAVE_CXX_FLAG CACHE)
179  message("Checking C compiler flag support for: " ${cxx_flag})
180  check_cxx_compiler_flag("${cxx_flag}" HAVE_CXX_FLAG)
181  if(NOT HAVE_CXX_FLAG)
182    message(
183      FATAL_ERROR "${PROJECT_NAME} requires support for C flag: ${cxx_flag}.")
184  endif()
185
186  if(NOT "${AOM_EXE_LINKER_FLAGS}" STREQUAL "")
187    aom_pop_var(CMAKE_EXE_LINKER_FLAGS)
188  endif()
189
190  append_flag(AOM_CXX_FLAGS "${cxx_flag}")
191  if(update_cxx_flags)
192    foreach(config ${AOM_CXX_CONFIGS})
193      set(${config} "${${config}} ${cxx_flag}" CACHE STRING "" FORCE)
194    endforeach()
195  endif()
196endfunction()
197
198# Checks for support of $flag by both the C and CXX compilers. Terminates
199# generation when support is not present in both compilers.
200function(require_compiler_flag flag update_cmake_flags)
201  require_c_flag(${flag} ${update_cmake_flags})
202  require_cxx_flag(${flag} ${update_cmake_flags})
203endfunction()
204
205# Checks only non-MSVC targets for support of $c_flag and terminates generation
206# when support is not present.
207function(require_c_flag_nomsvc c_flag update_c_flags)
208  if(NOT MSVC)
209    require_c_flag(${c_flag} ${update_c_flags})
210  endif()
211endfunction()
212
213# Checks only non-MSVC targets for support of $cxx_flag and terminates
214# generation when support is not present.
215function(require_cxx_flag_nomsvc cxx_flag update_cxx_flags)
216  if(NOT MSVC)
217    require_cxx_flag(${cxx_flag} ${update_cxx_flags})
218  endif()
219endfunction()
220
221# Checks only non-MSVC targets for support of $flag by both the C and CXX
222# compilers. Terminates generation when support is not present in both
223# compilers.
224function(require_compiler_flag_nomsvc flag update_cmake_flags)
225  require_c_flag_nomsvc(${flag} ${update_cmake_flags})
226  require_cxx_flag_nomsvc(${flag} ${update_cmake_flags})
227endfunction()
228
229# Adds $preproc_def to C compiler command line (as -D$preproc_def) if not
230# already present.
231function(add_c_preproc_definition preproc_def)
232  set(preproc_def "-D${preproc_def}")
233  is_flag_present(AOM_C_FLAGS "${preproc_def}" flag_cached)
234  if(${flag_cached})
235    return()
236  endif()
237
238  foreach(config ${AOM_C_CONFIGS})
239    set(${config} "${${config}} ${preproc_def}" CACHE STRING "" FORCE)
240  endforeach()
241endfunction()
242
243# Adds $preproc_def to CXX compiler command line (as -D$preproc_def) if not
244# already present.
245function(add_cxx_preproc_definition preproc_def)
246  set(preproc_def "-D${preproc_def}")
247  is_flag_present(AOM_CXX_FLAGS "${preproc_def}" flag_cached)
248  if(${flag_cached})
249    return()
250  endif()
251
252  foreach(config ${AOM_CXX_CONFIGS})
253    set(${config} "${${config}} ${preproc_def}" CACHE STRING "" FORCE)
254  endforeach()
255endfunction()
256
257# Adds $preproc_def to C and CXX compiler command line (as -D$preproc_def) if
258# not already present.
259function(add_preproc_definition preproc_def)
260  add_c_preproc_definition(${preproc_def})
261  add_cxx_preproc_definition(${preproc_def})
262endfunction()
263
264# Adds $flag to assembler command line.
265function(append_as_flag flag)
266  is_flag_present(AOM_AS_FLAGS "${flag}" flag_cached)
267  if(${flag_cached})
268    return()
269  endif()
270  append_flag(AOM_AS_FLAGS "${flag}")
271endfunction()
272
273# Adds $flag to the C compiler command line.
274function(append_c_flag flag)
275  is_flag_present(AOM_C_FLAGS "${flag}" flag_cached)
276  if(${flag_cached})
277    return()
278  endif()
279
280  foreach(config ${AOM_C_CONFIGS})
281    append_flag(${config} "${flag}")
282  endforeach()
283endfunction()
284
285# Adds $flag to the CXX compiler command line.
286function(append_cxx_flag flag)
287  is_flag_present(AOM_CXX_FLAGS "${flag}" flag_cached)
288  if(${flag_cached})
289    return()
290  endif()
291
292  foreach(config ${AOM_CXX_CONFIGS})
293    append_flag(${config} "${flag}")
294  endforeach()
295endfunction()
296
297# Adds $flag to the C and CXX compiler command lines.
298function(append_compiler_flag flag)
299  append_c_flag(${flag})
300  append_cxx_flag(${flag})
301endfunction()
302
303# Adds $flag to the executable linker command line when not present.
304function(append_exe_linker_flag flag)
305  is_flag_present(AOM_EXE_LINKER_FLAGS "${flag}" flag_cached)
306  if(${flag_cached})
307    return()
308  endif()
309
310  append_flag(AOM_EXE_LINKER_FLAGS "${flag}")
311  foreach(config ${AOM_EXE_LINKER_CONFIGS})
312    append_flag(${config} "${flag}")
313  endforeach()
314endfunction()
315
316# Adds $flag to the link flags for $target.
317function(append_link_flag_to_target target flag)
318  unset(target_link_flags)
319  get_target_property(target_link_flags ${target} LINK_FLAGS)
320
321  if(target_link_flags)
322    is_flag_present(target_link_flags "${flag}" flag_found)
323    if(${flag_found})
324      return()
325    endif()
326    set(target_link_flags "${target_link_flags} ${flag}")
327  else()
328    set(target_link_flags "${flag}")
329  endif()
330
331  set_target_properties(${target} PROPERTIES LINK_FLAGS ${target_link_flags})
332endfunction()
333
334# Adds $flag to executable linker flags, and makes sure C/CXX builds still work.
335function(require_linker_flag flag)
336  if(DEBUG_CMAKE_DISABLE_COMPILER_TESTS)
337    return()
338  endif()
339
340  append_exe_linker_flag(${flag})
341
342  unset(c_passed)
343  aom_check_c_compiles("LINKER_FLAG_C_TEST(${flag})" "" c_passed)
344  unset(cxx_passed)
345  aom_check_cxx_compiles("LINKER_FLAG_CXX_TEST(${flag})" "" cxx_passed)
346
347  if(NOT c_passed OR NOT cxx_passed)
348    message(FATAL_ERROR "Linker flag test for ${flag} failed.")
349  endif()
350endfunction()
351
352# Appends flags in $AOM_EXTRA_<TYPE>_FLAGS variables to the flags used at build
353# time.
354function(set_user_flags)
355
356  # Linker flags are handled first because some C/CXX flags require that a
357  # linker flag is present at link time.
358  if(AOM_EXTRA_EXE_LINKER_FLAGS)
359    is_flag_present(AOM_EXE_LINKER_FLAGS "${AOM_EXTRA_EXE_LINKER_FLAGS}"
360                    extra_present)
361    if(NOT ${extra_present})
362      require_linker_flag("${AOM_EXTRA_EXE_LINKER_FLAGS}")
363    endif()
364  endif()
365  if(AOM_EXTRA_AS_FLAGS)
366
367    # TODO(tomfinegan): assembler flag testing would be a good thing to have.
368    is_flag_present(AOM_AS_FLAGS "${AOM_EXTRA_AS_FLAGS}" extra_present)
369    if(NOT ${extra_present})
370      append_flag(AOM_AS_FLAGS "${AOM_EXTRA_AS_FLAGS}")
371    endif()
372  endif()
373  if(AOM_EXTRA_C_FLAGS)
374    is_flag_present(AOM_C_FLAGS "${AOM_EXTRA_C_FLAGS}" extra_present)
375    if(NOT ${extra_present})
376      require_c_flag("${AOM_EXTRA_C_FLAGS}" YES)
377    endif()
378  endif()
379  if(AOM_EXTRA_CXX_FLAGS)
380    is_flag_present(AOM_CXX_FLAGS "${AOM_EXTRA_CXX_FLAGS}" extra_present)
381    if(NOT ${extra_present})
382      require_cxx_flag("${AOM_EXTRA_CXX_FLAGS}" YES)
383    endif()
384  endif()
385endfunction()
386