reactos/sdk/cmake/compilerflags.cmake
Jérôme Gardou ed61512236 [CMAKE] Get rid of add_compile_flags_language macro
in favor of add_compile_options and the like with generator expressions
Also take this as an opportunity to remove the C++11 standard hack, GCC 8 now defaults to C++14
2020-10-20 21:44:54 +02:00

88 lines
3.3 KiB
CMake

# add_target_property
# Adds one or more values to the specified property of the specified target.
# Note that there are properties which require (semicolon-separated) lists,
# while others require space-separated strings. The function has a list of
# properties of the former variety and handles the values accordingly
function(add_target_property _module _propname)
list(APPEND _list_properties COMPILE_DEFINITIONS INCLUDE_DIRECTORIES LINK_DEPENDS)
set(_newvalue "")
get_target_property(_oldvalue ${_module} ${_propname})
if(_oldvalue)
set(_newvalue ${_oldvalue})
endif()
list(FIND _list_properties ${_propname} _list_index)
if(NOT _list_index EQUAL -1)
# list property
list(APPEND _newvalue ${ARGN})
else()
# string property
foreach(_flag ${ARGN})
set(_newvalue "${_newvalue} ${_flag}")
endforeach()
endif()
set_property(TARGET ${_module} PROPERTY ${_propname} ${_newvalue})
endfunction()
# remove_target_compile_options
# Remove one option from the target COMPILE_OPTIONS property,
# previously added through add_compile_options
function(remove_target_compile_option _module _option)
get_target_property(_options ${_module} COMPILE_OPTIONS)
list(REMOVE_ITEM _options ${_option})
set_target_properties(${_module} PROPERTIES COMPILE_OPTIONS "${_options}")
endfunction()
# Wrapper functions for the important properties, using add_target_property
# where appropriate.
# Note that the functions for string properties take a single string
# argument while those for list properties can take a variable number of
# arguments, all of which will be added to the list
#
# Examples:
# add_target_link_flags(mymodule "-s --fatal-warnings")
function(add_target_link_flags _module _flags)
if(${ARGC} GREATER 2)
message(FATAL_ERROR "Excess arguments to add_target_link_flags! Module ${_module}, args ${ARGN}")
endif()
add_target_property(${_module} LINK_FLAGS ${_flags})
endfunction()
# replace_compiler_option
# (taken from LLVM)
# Replaces a compiler option or switch `_old' in `_var' by `_new'.
# If `_old' is not in `_var', appends `_new' to `_var'.
#
# Example:
# replace_compiler_option(CMAKE_CXX_FLAGS_RELEASE "-O3" "-O2")
macro(replace_compiler_option _var _old _new)
# If the option already is on the variable, don't add it:
if("${${_var}}" MATCHES "(^| )${_new}($| )")
set(__n "")
else()
set(__n "${_new}")
endif()
if("${${_var}}" MATCHES "(^| )${_old}($| )")
string(REGEX REPLACE "(^| )${_old}($| )" " ${__n} " ${_var} "${${_var}}")
else()
set(${_var} "${${_var}} ${__n}")
endif()
endmacro(replace_compiler_option)
# add_compile_flags
# Add or replace compiler flags in the global scope for either all source
# files or only those of the specified language.
#
# Examples:
# add_compile_flags("-pedantic -O5")
function(add_compile_flags _flags)
if(${ARGC} GREATER 1)
message(FATAL_ERROR "Excess arguments to add_compile_flags! Args ${ARGN}")
endif()
# Adds the compiler flag for all code files: C, C++, and assembly
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_flags}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flags}" PARENT_SCOPE)
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${_flags}" PARENT_SCOPE)
endfunction()