brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.8 KiB · 1c744a0 Raw
108 lines · plain
1 2include(CheckCXXCompilerFlag)3 4unset(add_flag_if_supported)5 6# Mangle the name of a compiler flag into a valid CMake identifier.7# Ex: --std=c++11 -> STD_EQ_CXX118macro(mangle_name str output)9  string(STRIP "${str}" strippedStr)10  string(REGEX REPLACE "^/" "" strippedStr "${strippedStr}")11  string(REGEX REPLACE "^-+" "" strippedStr "${strippedStr}")12  string(REGEX REPLACE "-+$" "" strippedStr "${strippedStr}")13  string(REPLACE "-" "_" strippedStr "${strippedStr}")14  string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}")15  string(REPLACE "+" "X" strippedStr "${strippedStr}")16  string(TOUPPER "${strippedStr}" ${output})17endmacro()18 19# Remove a list of flags from all CMake variables that affect compile flags.20# This can be used to remove unwanted flags specified on the command line21# or added in other parts of LLVM's cmake configuration.22macro(remove_flags)23  foreach(var ${ARGN})24    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")25    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")26    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")27    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")28    string(REPLACE "${var}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")29    string(REPLACE "${var}" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")30    string(REPLACE "${var}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")31    string(REPLACE "${var}" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")32    string(REPLACE "${var}" "" CMAKE_SHARED_MODULE_FLAGS "${CMAKE_SHARED_MODULE_FLAGS}")33    remove_definitions(${var})34  endforeach()35endmacro(remove_flags)36 37macro(check_flag_supported flag)38    mangle_name("${flag}" flagname)39    check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")40endmacro()41 42macro(append_flags DEST)43  foreach(value ${ARGN})44    list(APPEND ${DEST} ${value})45    list(APPEND ${DEST} ${value})46  endforeach()47endmacro()48 49# If the specified 'condition' is true then append the specified list of flags to DEST50macro(append_flags_if condition DEST)51  if (${condition})52    list(APPEND ${DEST} ${ARGN})53  endif()54endmacro()55 56# Add each flag in the list specified by DEST if that flag is supported by the current compiler.57macro(append_flags_if_supported DEST)58  foreach(flag ${ARGN})59    mangle_name("${flag}" flagname)60    check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")61    append_flags_if(CXX_SUPPORTS_${flagname}_FLAG ${DEST} ${flag})62  endforeach()63endmacro()64 65# Add a macro definition if condition is true.66macro(define_if condition def)67  if (${condition})68    add_definitions(${def})69  endif()70endmacro()71 72# Add a macro definition if condition is not true.73macro(define_if_not condition def)74  if (NOT ${condition})75    add_definitions(${def})76  endif()77endmacro()78 79# Add a macro definition to the __config_site file if the specified condition80# is 'true'. Note that '-D${def}' is not added. Instead it is expected that81# the build include the '__config_site' header.82macro(config_define_if condition def)83  if (${condition})84    set(${def} ON)85  endif()86endmacro()87 88macro(config_define value def)89  set(${def} ${value})90endmacro()91 92# Turn a comma separated CMake list into a space separated string.93macro(split_list listname)94  string(REPLACE ";" " " ${listname} "${${listname}}")95endmacro()96 97# For each specified flag, add that compile flag to the provided target.98# The flags are added with the given visibility, i.e. PUBLIC|PRIVATE|INTERFACE.99function(target_add_compile_flags_if_supported target visibility)100  foreach(flag ${ARGN})101    mangle_name("${flag}" flagname)102    check_cxx_compiler_flag("${flag}" "CXX_SUPPORTS_${flagname}_FLAG")103    if (CXX_SUPPORTS_${flagname}_FLAG)104      target_compile_options(${target} ${visibility} ${flag})105    endif()106  endforeach()107endfunction()108