xref: /openbsd-src/gnu/llvm/libcxx/cmake/Modules/HandleLibcxxFlags.cmake (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
146035553Spatrick# HandleLibcxxFlags - A set of macros used to setup the flags used to compile
246035553Spatrick# and link libc++. These macros add flags to the following CMake variables.
346035553Spatrick# - LIBCXX_COMPILE_FLAGS: flags used to compile libc++
446035553Spatrick# - LIBCXX_LINK_FLAGS: flags used to link libc++
546035553Spatrick# - LIBCXX_LIBRARIES: libraries to link libc++ to.
646035553Spatrick
746035553Spatrickinclude(CheckCXXCompilerFlag)
846035553Spatrick
946035553Spatrickunset(add_flag_if_supported)
1046035553Spatrick
1146035553Spatrick# Mangle the name of a compiler flag into a valid CMake identifier.
1246035553Spatrick# Ex: --std=c++11 -> STD_EQ_CXX11
1346035553Spatrickmacro(mangle_name str output)
1446035553Spatrick  string(STRIP "${str}" strippedStr)
1546035553Spatrick  string(REGEX REPLACE "^/" "" strippedStr "${strippedStr}")
1646035553Spatrick  string(REGEX REPLACE "^-+" "" strippedStr "${strippedStr}")
1746035553Spatrick  string(REGEX REPLACE "-+$" "" strippedStr "${strippedStr}")
1846035553Spatrick  string(REPLACE "-" "_" strippedStr "${strippedStr}")
1946035553Spatrick  string(REPLACE ":" "_COLON_" strippedStr "${strippedStr}")
2046035553Spatrick  string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}")
2146035553Spatrick  string(REPLACE "+" "X" strippedStr "${strippedStr}")
2246035553Spatrick  string(TOUPPER "${strippedStr}" ${output})
2346035553Spatrickendmacro()
2446035553Spatrick
2546035553Spatrick# Remove a list of flags from all CMake variables that affect compile flags.
2646035553Spatrick# This can be used to remove unwanted flags specified on the command line
2746035553Spatrick# or added in other parts of LLVM's cmake configuration.
2846035553Spatrickmacro(remove_flags)
2946035553Spatrick  foreach(var ${ARGN})
3046035553Spatrick    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
3146035553Spatrick    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
3246035553Spatrick    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
3346035553Spatrick    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
3446035553Spatrick    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
3546035553Spatrick    string(REPLACE "${var}" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
3646035553Spatrick    string(REPLACE "${var}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
3746035553Spatrick    string(REPLACE "${var}" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
3846035553Spatrick    string(REPLACE "${var}" "" CMAKE_SHARED_MODULE_FLAGS "${CMAKE_SHARED_MODULE_FLAGS}")
3946035553Spatrick    remove_definitions(${var})
4046035553Spatrick  endforeach()
4146035553Spatrickendmacro(remove_flags)
4246035553Spatrick
4346035553Spatrickmacro(check_flag_supported flag)
4446035553Spatrick    mangle_name("${flag}" flagname)
45*4bdff4beSrobert    check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
4646035553Spatrickendmacro()
4746035553Spatrick
4846035553Spatrickmacro(append_flags DEST)
4946035553Spatrick  foreach(value ${ARGN})
5046035553Spatrick    list(APPEND ${DEST} ${value})
5146035553Spatrick    list(APPEND ${DEST} ${value})
5246035553Spatrick  endforeach()
5346035553Spatrickendmacro()
5446035553Spatrick
5546035553Spatrick# If the specified 'condition' is true then append the specified list of flags to DEST
5646035553Spatrickmacro(append_flags_if condition DEST)
5746035553Spatrick  if (${condition})
5846035553Spatrick    list(APPEND ${DEST} ${ARGN})
5946035553Spatrick  endif()
6046035553Spatrickendmacro()
6146035553Spatrick
6246035553Spatrick# Add each flag in the list specified by DEST if that flag is supported by the current compiler.
6346035553Spatrickmacro(append_flags_if_supported DEST)
6446035553Spatrick  foreach(flag ${ARGN})
6546035553Spatrick    mangle_name("${flag}" flagname)
66*4bdff4beSrobert    check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
67*4bdff4beSrobert    append_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${DEST} ${flag})
6846035553Spatrick  endforeach()
6946035553Spatrickendmacro()
7046035553Spatrick
7146035553Spatrick# Add a macro definition if condition is true.
7246035553Spatrickmacro(define_if condition def)
7346035553Spatrick  if (${condition})
7446035553Spatrick    add_definitions(${def})
7546035553Spatrick  endif()
7646035553Spatrickendmacro()
7746035553Spatrick
7846035553Spatrick# Add a macro definition if condition is not true.
7946035553Spatrickmacro(define_if_not condition def)
8046035553Spatrick  if (NOT ${condition})
8146035553Spatrick    add_definitions(${def})
8246035553Spatrick  endif()
8346035553Spatrickendmacro()
8446035553Spatrick
8546035553Spatrick# Add a macro definition to the __config_site file if the specified condition
8646035553Spatrick# is 'true'. Note that '-D${def}' is not added. Instead it is expected that
8746035553Spatrick# the build include the '__config_site' header.
8846035553Spatrickmacro(config_define_if condition def)
8946035553Spatrick  if (${condition})
9046035553Spatrick    set(${def} ON)
9146035553Spatrick  endif()
9246035553Spatrickendmacro()
9346035553Spatrick
9446035553Spatrickmacro(config_define_if_not condition def)
9546035553Spatrick  if (NOT ${condition})
9646035553Spatrick    set(${def} ON)
9746035553Spatrick  endif()
9846035553Spatrickendmacro()
9946035553Spatrick
10046035553Spatrickmacro(config_define value def)
10146035553Spatrick  set(${def} ${value})
10246035553Spatrickendmacro()
10346035553Spatrick
10446035553Spatrick# Add a list of flags to all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS',
10546035553Spatrick# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'.
10646035553Spatrickmacro(add_target_flags)
10746035553Spatrick  foreach(value ${ARGN})
10846035553Spatrick    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}")
10946035553Spatrick    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${value}")
11046035553Spatrick    list(APPEND LIBCXX_COMPILE_FLAGS ${value})
11146035553Spatrick    list(APPEND LIBCXX_LINK_FLAGS ${value})
11246035553Spatrick  endforeach()
11346035553Spatrickendmacro()
11446035553Spatrick
11546035553Spatrick# If the specified 'condition' is true then add a list of flags to
11646035553Spatrick# all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBCXX_COMPILE_FLAGS'
11746035553Spatrick# and 'LIBCXX_LINK_FLAGS'.
11846035553Spatrickmacro(add_target_flags_if condition)
11946035553Spatrick  if (${condition})
12046035553Spatrick    add_target_flags(${ARGN})
12146035553Spatrick  endif()
12246035553Spatrickendmacro()
12346035553Spatrick
12476d0caaeSpatrick# Add all the flags supported by the compiler to all of
12576d0caaeSpatrick# 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBCXX_COMPILE_FLAGS'
12676d0caaeSpatrick# and 'LIBCXX_LINK_FLAGS'.
12776d0caaeSpatrickmacro(add_target_flags_if_supported)
12876d0caaeSpatrick  foreach(flag ${ARGN})
12976d0caaeSpatrick    mangle_name("${flag}" flagname)
130*4bdff4beSrobert    check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
131*4bdff4beSrobert    add_target_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${flag})
13276d0caaeSpatrick  endforeach()
13376d0caaeSpatrickendmacro()
13476d0caaeSpatrick
13546035553Spatrick# Add a specified list of flags to both 'LIBCXX_COMPILE_FLAGS' and
13646035553Spatrick# 'LIBCXX_LINK_FLAGS'.
13746035553Spatrickmacro(add_flags)
13846035553Spatrick  foreach(value ${ARGN})
13946035553Spatrick    list(APPEND LIBCXX_COMPILE_FLAGS ${value})
14046035553Spatrick    list(APPEND LIBCXX_LINK_FLAGS ${value})
14146035553Spatrick  endforeach()
14246035553Spatrickendmacro()
14346035553Spatrick
14446035553Spatrick# If the specified 'condition' is true then add a list of flags to both
14546035553Spatrick# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'.
14646035553Spatrickmacro(add_flags_if condition)
14746035553Spatrick  if (${condition})
14846035553Spatrick    add_flags(${ARGN})
14946035553Spatrick  endif()
15046035553Spatrickendmacro()
15146035553Spatrick
15246035553Spatrick# Add each flag in the list to LIBCXX_COMPILE_FLAGS and LIBCXX_LINK_FLAGS
15346035553Spatrick# if that flag is supported by the current compiler.
15446035553Spatrickmacro(add_flags_if_supported)
15546035553Spatrick  foreach(flag ${ARGN})
15646035553Spatrick      mangle_name("${flag}" flagname)
157*4bdff4beSrobert      check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
158*4bdff4beSrobert      add_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${flag})
15946035553Spatrick  endforeach()
16046035553Spatrickendmacro()
16146035553Spatrick
16246035553Spatrick# Add a list of flags to 'LIBCXX_COMPILE_FLAGS'.
16346035553Spatrickmacro(add_compile_flags)
16446035553Spatrick  foreach(f ${ARGN})
16546035553Spatrick    list(APPEND LIBCXX_COMPILE_FLAGS ${f})
16646035553Spatrick  endforeach()
16746035553Spatrickendmacro()
16846035553Spatrick
16946035553Spatrick# If 'condition' is true then add the specified list of flags to
17046035553Spatrick# 'LIBCXX_COMPILE_FLAGS'
17146035553Spatrickmacro(add_compile_flags_if condition)
17246035553Spatrick  if (${condition})
17346035553Spatrick    add_compile_flags(${ARGN})
17446035553Spatrick  endif()
17546035553Spatrickendmacro()
17646035553Spatrick
17746035553Spatrick# For each specified flag, add that flag to 'LIBCXX_COMPILE_FLAGS' if the
17846035553Spatrick# flag is supported by the C++ compiler.
17946035553Spatrickmacro(add_compile_flags_if_supported)
18046035553Spatrick  foreach(flag ${ARGN})
18146035553Spatrick      mangle_name("${flag}" flagname)
182*4bdff4beSrobert      check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
183*4bdff4beSrobert      add_compile_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${flag})
18446035553Spatrick  endforeach()
18546035553Spatrickendmacro()
18646035553Spatrick
18746035553Spatrick# Add a list of flags to 'LIBCXX_LINK_FLAGS'.
18846035553Spatrickmacro(add_link_flags)
18946035553Spatrick  foreach(f ${ARGN})
19046035553Spatrick    list(APPEND LIBCXX_LINK_FLAGS ${f})
19146035553Spatrick  endforeach()
19246035553Spatrickendmacro()
19346035553Spatrick
19446035553Spatrick# If 'condition' is true then add the specified list of flags to
19546035553Spatrick# 'LIBCXX_LINK_FLAGS'
19646035553Spatrickmacro(add_link_flags_if condition)
19746035553Spatrick  if (${condition})
19846035553Spatrick    add_link_flags(${ARGN})
19946035553Spatrick  endif()
20046035553Spatrickendmacro()
20146035553Spatrick
20246035553Spatrick# For each specified flag, add that flag to 'LIBCXX_LINK_FLAGS' if the
20346035553Spatrick# flag is supported by the C++ compiler.
20446035553Spatrickmacro(add_link_flags_if_supported)
20546035553Spatrick  foreach(flag ${ARGN})
20646035553Spatrick    mangle_name("${flag}" flagname)
207*4bdff4beSrobert    check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
208*4bdff4beSrobert    add_link_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${flag})
20946035553Spatrick  endforeach()
21046035553Spatrickendmacro()
21146035553Spatrick
21246035553Spatrick# Add a list of libraries or link flags to 'LIBCXX_LIBRARIES'.
21346035553Spatrickmacro(add_library_flags)
21446035553Spatrick  foreach(lib ${ARGN})
21546035553Spatrick    list(APPEND LIBCXX_LIBRARIES ${lib})
21646035553Spatrick  endforeach()
21746035553Spatrickendmacro()
21846035553Spatrick
21946035553Spatrick# if 'condition' is true then add the specified list of libraries and flags
22046035553Spatrick# to 'LIBCXX_LIBRARIES'.
22146035553Spatrickmacro(add_library_flags_if condition)
22246035553Spatrick  if(${condition})
22346035553Spatrick    add_library_flags(${ARGN})
22446035553Spatrick  endif()
22546035553Spatrickendmacro()
22646035553Spatrick
22746035553Spatrick# Turn a comma separated CMake list into a space separated string.
22846035553Spatrickmacro(split_list listname)
22946035553Spatrick  string(REPLACE ";" " " ${listname} "${${listname}}")
23046035553Spatrickendmacro()
23146035553Spatrick
23246035553Spatrick# For each specified flag, add that link flag to the provided target.
23346035553Spatrick# The flags are added with the given visibility, i.e. PUBLIC|PRIVATE|INTERFACE.
23446035553Spatrickfunction(target_add_link_flags_if_supported target visibility)
23546035553Spatrick  foreach(flag ${ARGN})
23646035553Spatrick    mangle_name("${flag}" flagname)
237*4bdff4beSrobert    check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
238*4bdff4beSrobert    if (CXX_SUPPORTS_${flagname}_FLAG)
23946035553Spatrick      target_link_libraries(${target} ${visibility} ${flag})
24046035553Spatrick    endif()
24146035553Spatrick  endforeach()
24246035553Spatrickendfunction()
24346035553Spatrick
24446035553Spatrick# For each specified flag, add that compile flag to the provided target.
24546035553Spatrick# The flags are added with the given visibility, i.e. PUBLIC|PRIVATE|INTERFACE.
24646035553Spatrickfunction(target_add_compile_flags_if_supported target visibility)
24746035553Spatrick  foreach(flag ${ARGN})
24846035553Spatrick    mangle_name("${flag}" flagname)
249*4bdff4beSrobert    check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")
250*4bdff4beSrobert    if (CXX_SUPPORTS_${flagname}_FLAG)
25146035553Spatrick      target_compile_options(${target} ${visibility} ${flag})
25246035553Spatrick    endif()
25346035553Spatrick  endforeach()
25446035553Spatrickendfunction()
255