• 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  unset(C_FLAG_SUPPORTED CACHE)
63  message("Checking C compiler flag support for: " ${c_flag})
64  check_c_compiler_flag("${c_flag}" C_FLAG_SUPPORTED)
65
66  if(${C_FLAG_SUPPORTED})
67    append_flag(AOM_C_FLAGS "${c_flag}")
68    foreach(config ${AOM_C_CONFIGS})
69      unset(C_FLAG_FOUND)
70      append_flag("${config}" "${c_flag}")
71    endforeach()
72  else()
73    append_flag(AOM_FAILED_C_FLAGS "${c_flag}")
74  endif()
75endfunction()
76
77# Checks C++ compiler for support of $cxx_flag. Adds $cxx_flag to all
78# $CMAKE_CXX_FLAGS_<CONFIG>s stored in AOM_CXX_CONFIGS when the compile test
79# passes. Caches $cxx_flag in $AOM_CXX_FLAGS or $AOM_FAILED_CXX_FLAGS depending
80# on test outcome.
81function(add_cxx_flag_if_supported cxx_flag)
82  if(DEBUG_CMAKE_DISABLE_COMPILER_TESTS)
83    return()
84  endif()
85
86  is_flag_present(AOM_CXX_FLAGS "${cxx_flag}" flag_ok)
87  is_flag_present(AOM_FAILED_CXX_FLAGS "${cxx_flag}" flag_failed)
88  if(${flag_ok} OR ${flag_failed})
89    return()
90  endif()
91
92  unset(CXX_FLAG_SUPPORTED CACHE)
93  message("Checking C++ compiler flag support for: " ${cxx_flag})
94  check_cxx_compiler_flag("${cxx_flag}" CXX_FLAG_SUPPORTED)
95
96  if(${CXX_FLAG_SUPPORTED})
97    append_flag(AOM_CXX_FLAGS "${cxx_flag}")
98    foreach(config ${AOM_CXX_CONFIGS})
99      unset(CXX_FLAG_FOUND)
100      append_flag("${config}" "${cxx_flag}")
101    endforeach()
102  else()
103    append_flag(AOM_FAILED_CXX_FLAGS "${cxx_flag}")
104  endif()
105endfunction()
106
107# Convenience method for adding a flag to both the C and C++ compiler command
108# lines.
109function(add_compiler_flag_if_supported flag)
110  add_c_flag_if_supported(${flag})
111  add_cxx_flag_if_supported(${flag})
112endfunction()
113
114# Checks C compiler for support of $c_flag and terminates generation when
115# support is not present.
116function(require_c_flag c_flag update_c_flags)
117  if(DEBUG_CMAKE_DISABLE_COMPILER_TESTS)
118    return()
119  endif()
120
121  is_flag_present(AOM_C_FLAGS "${c_flag}" flag_ok)
122  if(${flag_ok})
123    return()
124  endif()
125
126  if(NOT "${AOM_EXE_LINKER_FLAGS}" STREQUAL "")
127    aom_push_var(CMAKE_EXE_LINKER_FLAGS "${AOM_EXE_LINKER_FLAGS}")
128  endif()
129
130  unset(HAVE_C_FLAG CACHE)
131  message("Checking C compiler flag support for: " ${c_flag})
132  check_c_compiler_flag("${c_flag}" HAVE_C_FLAG)
133  if(NOT HAVE_C_FLAG)
134    message(
135      FATAL_ERROR "${PROJECT_NAME} requires support for C flag: ${c_flag}.")
136  endif()
137
138  if(NOT "${AOM_EXE_LINKER_FLAGS}" STREQUAL "")
139    aom_pop_var(CMAKE_EXE_LINKER_FLAGS)
140  endif()
141
142  append_flag(AOM_C_FLAGS "${c_flag}")
143  if(update_c_flags)
144    foreach(config ${AOM_C_CONFIGS})
145      set(${config} "${${config}} ${c_flag}" CACHE STRING "" FORCE)
146    endforeach()
147  endif()
148endfunction()
149
150# Checks CXX compiler for support of $cxx_flag and terminates generation when
151# support is not present.
152function(require_cxx_flag cxx_flag update_cxx_flags)
153  if(DEBUG_CMAKE_DISABLE_COMPILER_TESTS)
154    return()
155  endif()
156
157  is_flag_present(AOM_CXX_FLAGS "${cxx_flag}" flag_ok)
158  if(${flag_ok})
159    return()
160  endif()
161
162  if(NOT "${AOM_EXE_LINKER_FLAGS}" STREQUAL "")
163    aom_push_var(CMAKE_EXE_LINKER_FLAGS "${AOM_EXE_LINKER_FLAGS}")
164  endif()
165
166  unset(HAVE_CXX_FLAG CACHE)
167  message("Checking C compiler flag support for: " ${cxx_flag})
168  check_cxx_compiler_flag("${cxx_flag}" HAVE_CXX_FLAG)
169  if(NOT HAVE_CXX_FLAG)
170    message(
171      FATAL_ERROR "${PROJECT_NAME} requires support for C flag: ${cxx_flag}.")
172  endif()
173
174  if(NOT "${AOM_EXE_LINKER_FLAGS}" STREQUAL "")
175    aom_pop_var(CMAKE_EXE_LINKER_FLAGS)
176  endif()
177
178  append_flag(AOM_CXX_FLAGS "${cxx_flag}")
179  if(update_cxx_flags)
180    foreach(config ${AOM_CXX_CONFIGS})
181      set(${config} "${${config}} ${cxx_flag}" CACHE STRING "" FORCE)
182    endforeach()
183  endif()
184endfunction()
185
186# Checks for support of $flag by both the C and CXX compilers. Terminates
187# generation when support is not present in both compilers.
188function(require_compiler_flag flag update_cmake_flags)
189  require_c_flag(${flag} ${update_cmake_flags})
190  require_cxx_flag(${flag} ${update_cmake_flags})
191endfunction()
192
193# Checks only non-MSVC targets for support of $c_flag and terminates generation
194# when support is not present.
195function(require_c_flag_nomsvc c_flag update_c_flags)
196  if(NOT MSVC)
197    require_c_flag(${c_flag} ${update_c_flags})
198  endif()
199endfunction()
200
201# Checks only non-MSVC targets for support of $cxx_flag and terminates
202# generation when support is not present.
203function(require_cxx_flag_nomsvc cxx_flag update_cxx_flags)
204  if(NOT MSVC)
205    require_cxx_flag(${cxx_flag} ${update_cxx_flags})
206  endif()
207endfunction()
208
209# Checks only non-MSVC targets for support of $flag by both the C and CXX
210# compilers. Terminates generation when support is not present in both
211# compilers.
212function(require_compiler_flag_nomsvc flag update_cmake_flags)
213  require_c_flag_nomsvc(${flag} ${update_cmake_flags})
214  require_cxx_flag_nomsvc(${flag} ${update_cmake_flags})
215endfunction()
216
217# Adds $preproc_def to C compiler command line (as -D$preproc_def) if not
218# already present.
219function(add_c_preproc_definition preproc_def)
220  set(preproc_def "-D${preproc_def}")
221  is_flag_present(AOM_C_FLAGS "${preproc_def}" flag_cached)
222  if(${flag_cached})
223    return()
224  endif()
225
226  foreach(config ${AOM_C_CONFIGS})
227    set(${config} "${${config}} ${preproc_def}" CACHE STRING "" FORCE)
228  endforeach()
229endfunction()
230
231# Adds $preproc_def to CXX compiler command line (as -D$preproc_def) if not
232# already present.
233function(add_cxx_preproc_definition preproc_def)
234  set(preproc_def "-D${preproc_def}")
235  is_flag_present(AOM_CXX_FLAGS "${preproc_def}" flag_cached)
236  if(${flag_cached})
237    return()
238  endif()
239
240  foreach(config ${AOM_CXX_CONFIGS})
241    set(${config} "${${config}} ${preproc_def}" CACHE STRING "" FORCE)
242  endforeach()
243endfunction()
244
245# Adds $preproc_def to C and CXX compiler command line (as -D$preproc_def) if
246# not already present.
247function(add_preproc_definition preproc_def)
248  add_c_preproc_definition(${preproc_def})
249  add_cxx_preproc_definition(${preproc_def})
250endfunction()
251
252# Adds $flag to assembler command line.
253function(append_as_flag flag)
254  is_flag_present(AOM_AS_FLAGS "${flag}" flag_cached)
255  if(${flag_cached})
256    return()
257  endif()
258  append_flag(AOM_AS_FLAGS "${flag}")
259endfunction()
260
261# Adds $flag to the C compiler command line.
262function(append_c_flag flag)
263  is_flag_present(AOM_C_FLAGS "${flag}" flag_cached)
264  if(${flag_cached})
265    return()
266  endif()
267
268  foreach(config ${AOM_C_CONFIGS})
269    append_flag(${config} "${flag}")
270  endforeach()
271endfunction()
272
273# Adds $flag to the CXX compiler command line.
274function(append_cxx_flag flag)
275  is_flag_present(AOM_CXX_FLAGS "${flag}" flag_cached)
276  if(${flag_cached})
277    return()
278  endif()
279
280  foreach(config ${AOM_CXX_CONFIGS})
281    append_flag(${config} "${flag}")
282  endforeach()
283endfunction()
284
285# Adds $flag to the C and CXX compiler command lines.
286function(append_compiler_flag flag)
287  append_c_flag(${flag})
288  append_cxx_flag(${flag})
289endfunction()
290
291# Adds $flag to the executable linker command line when not present.
292function(append_exe_linker_flag flag)
293  is_flag_present(AOM_EXE_LINKER_FLAGS "${flag}" flag_cached)
294  if(${flag_cached})
295    return()
296  endif()
297
298  append_flag(AOM_EXE_LINKER_FLAGS "${flag}")
299  foreach(config ${AOM_EXE_LINKER_CONFIGS})
300    append_flag(${config} "${flag}")
301  endforeach()
302endfunction()
303
304# Adds $flag to the link flags for $target.
305function(append_link_flag_to_target target flag)
306  unset(target_link_flags)
307  get_target_property(target_link_flags ${target} LINK_FLAGS)
308
309  if(target_link_flags)
310    is_flag_present(target_link_flags "${flag}" flag_found)
311    if(${flag_found})
312      return()
313    endif()
314    set(target_link_flags "${target_link_flags} ${flag}")
315  else()
316    set(target_link_flags "${flag}")
317  endif()
318
319  set_target_properties(${target} PROPERTIES LINK_FLAGS ${target_link_flags})
320endfunction()
321
322# Adds $flag to executable linker flags, and makes sure C/CXX builds still work.
323function(require_linker_flag flag)
324  if(DEBUG_CMAKE_DISABLE_COMPILER_TESTS)
325    return()
326  endif()
327
328  append_exe_linker_flag(${flag})
329
330  unset(c_passed)
331  aom_check_c_compiles("LINKER_FLAG_C_TEST(${flag})" "" c_passed)
332  unset(cxx_passed)
333  aom_check_cxx_compiles("LINKER_FLAG_CXX_TEST(${flag})" "" cxx_passed)
334
335  if(NOT c_passed OR NOT cxx_passed)
336    message(FATAL_ERROR "Linker flag test for ${flag} failed.")
337  endif()
338endfunction()
339
340# Appends flags in $AOM_EXTRA_<TYPE>_FLAGS variables to the flags used at build
341# time.
342function(set_user_flags)
343
344  # Linker flags are handled first because some C/CXX flags require that a
345  # linker flag is present at link time.
346  if(AOM_EXTRA_EXE_LINKER_FLAGS)
347    is_flag_present(AOM_EXE_LINKER_FLAGS "${AOM_EXTRA_EXE_LINKER_FLAGS}"
348                    extra_present)
349    if(NOT ${extra_present})
350      require_linker_flag("${AOM_EXTRA_EXE_LINKER_FLAGS}")
351    endif()
352  endif()
353  if(AOM_EXTRA_AS_FLAGS)
354
355    # TODO(tomfinegan): assembler flag testing would be a good thing to have.
356    is_flag_present(AOM_AS_FLAGS "${AOM_EXTRA_AS_FLAGS}" extra_present)
357    if(NOT ${extra_present})
358      append_flag(AOM_AS_FLAGS "${AOM_EXTRA_AS_FLAGS}")
359    endif()
360  endif()
361  if(AOM_EXTRA_C_FLAGS)
362    is_flag_present(AOM_C_FLAGS "${AOM_EXTRA_C_FLAGS}" extra_present)
363    if(NOT ${extra_present})
364      require_c_flag("${AOM_EXTRA_C_FLAGS}" YES)
365    endif()
366  endif()
367  if(AOM_EXTRA_CXX_FLAGS)
368    is_flag_present(AOM_CXX_FLAGS "${AOM_EXTRA_CXX_FLAGS}" extra_present)
369    if(NOT ${extra_present})
370      require_cxx_flag("${AOM_EXTRA_CXX_FLAGS}" YES)
371    endif()
372  endif()
373endfunction()
374