xref: /llvm-project/libcxx/cmake/Modules/HandleLibcxxFlags.cmake (revision 141471a0cbf2d029899b29baf1a54a238f6067f9)
1# HandleLibcxxFlags - A set of macros used to setup the flags used to compile
2# and link libc++. These macros add flags to the following CMake variables.
3# - LIBCXX_COMPILE_FLAGS: flags used to compile libc++
4# - LIBCXX_LINK_FLAGS: flags used to link libc++
5# - LIBCXX_LIBRARIES: libraries to link libc++ to.
6
7include(CheckCXXCompilerFlag)
8include(HandleFlags)
9
10unset(add_flag_if_supported)
11
12# Add a specified list of flags to both 'LIBCXX_COMPILE_FLAGS' and
13# 'LIBCXX_LINK_FLAGS'.
14macro(add_flags)
15  foreach(value ${ARGN})
16    list(APPEND LIBCXX_COMPILE_FLAGS ${value})
17    list(APPEND LIBCXX_LINK_FLAGS ${value})
18  endforeach()
19endmacro()
20
21# If the specified 'condition' is true then add a list of flags to both
22# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'.
23macro(add_flags_if condition)
24  if (${condition})
25    add_flags(${ARGN})
26  endif()
27endmacro()
28
29# Add each flag in the list to LIBCXX_COMPILE_FLAGS and LIBCXX_LINK_FLAGS
30# if that flag is supported by the current compiler.
31macro(add_flags_if_supported)
32  foreach(flag ${ARGN})
33      mangle_name("${flag}" flagname)
34      check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
35      add_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${flag})
36  endforeach()
37endmacro()
38
39# Add a list of flags to 'LIBCXX_LINK_FLAGS'.
40macro(add_link_flags)
41  foreach(f ${ARGN})
42    list(APPEND LIBCXX_LINK_FLAGS ${f})
43  endforeach()
44endmacro()
45
46# Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'.
47macro(add_library_flags)
48  foreach(lib ${ARGN})
49    list(APPEND LIBCXX_LIBRARIES ${lib})
50  endforeach()
51endmacro()
52
53# if 'condition' is true then add the specified list of libraries and flags
54# to 'LIBCXX_LIBRARIES'.
55macro(add_library_flags_if condition)
56  if(${condition})
57    add_library_flags(${ARGN})
58  endif()
59endmacro()
60
61# For each specified flag, add that link flag to the provided target.
62# The flags are added with the given visibility, i.e. PUBLIC|PRIVATE|INTERFACE.
63function(target_add_link_flags_if_supported target visibility)
64  foreach(flag ${ARGN})
65    mangle_name("${flag}" flagname)
66    check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
67    if (CXX_SUPPORTS_${flagname}_FLAG)
68      target_link_libraries(${target} ${visibility} ${flag})
69    endif()
70  endforeach()
71endfunction()
72