- Add replace_compile_flags macros. Thanks to Amine and LLVM.
- Clean up compilerflags.cmake a bit, and make invalid macro/function invocations fatal so we actually notice them

svn path=/trunk/; revision=59286
This commit is contained in:
Thomas Faber 2013-06-22 12:15:50 +00:00
parent 06b7934445
commit e29060bb8d

View file

@ -31,40 +31,20 @@ endfunction()
# arguments, all of which will be added to the list
#
# Examples:
# add_compile_flags("-pedantic -O5")
# add_target_link_flags(mymodule "-s --fatal-warnings")
# add_target_compile_flags(mymodule "-pedantic -O5")
# add_target_link_flags(mymodule "-s --fatal-warnings")
# add_target_compile_definitions(mymodule WIN32 _WIN32 INLINE=inline)
# add_target_include_directories(mymodule include ../include)
# add_compile_flags_language("-std=gnu99" "C")
function(add_compile_flags _flags)
if(${ARGC} GREATER 1)
message(STATUS "Excess arguments to add_compile_flags! Args ${ARGN}")
endif()
# Adds the compiler flag to both CMAKE_C_FLAGS and CMAKE_CXX_FLAGS
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()
function(add_compile_flags_language _flags _lang)
if(NOT ${ARGC} EQUAL 2)
message(STATUS "Wrong arguments to add_compile_flags_language! Args ${ARGN}")
endif()
# Adds the compiler flag to both CMAKE_C_FLAGS and CMAKE_CXX_FLAGS
set(CMAKE_${_lang}_FLAGS "${CMAKE_${_lang}_FLAGS} ${_flags}" PARENT_SCOPE)
endfunction()
function(add_target_compile_flags _module _flags)
if(${ARGC} GREATER 2)
message(STATUS "Excess arguments to add_target_compile_flags! Module ${_module}, args ${ARGN}")
message(FATAL_ERROR "Excess arguments to add_target_compile_flags! Module ${_module}, args ${ARGN}")
endif()
add_target_property(${_module} COMPILE_FLAGS ${_flags})
endfunction()
function(add_target_link_flags _module _flags)
if(${ARGC} GREATER 2)
message(STATUS "Excess arguments to add_target_link_flags! Module ${_module}, args ${ARGN}")
message(FATAL_ERROR "Excess arguments to add_target_link_flags! Module ${_module}, args ${ARGN}")
endif()
add_target_property(${_module} LINK_FLAGS ${_flags})
endfunction()
@ -76,3 +56,70 @@ endfunction()
function(add_target_include_directories _module)
add_target_property(${_module} INCLUDE_DIRECTORIES ${ARGN})
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_compile_flags_language
# replace_compile_flags
# replace_compile_flags_language
# 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")
# add_compile_flags_language("-std=gnu99" "C")
# replace_compile_flags("-O5" "-O3")
# replace_compile_flags_language("-fno-exceptions" "-fexceptions" "CXX")
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()
function(add_compile_flags_language _flags _lang)
if(NOT ${ARGC} EQUAL 2)
message(FATAL_ERROR "Wrong arguments to add_compile_flags_language! Args ${ARGN}")
endif()
# Adds the compiler flag for the specified language only, e.g. CMAKE_C_FLAGS
set(CMAKE_${_lang}_FLAGS "${CMAKE_${_lang}_FLAGS} ${_flags}" PARENT_SCOPE)
endfunction()
macro(replace_compile_flags _oldflags _newflags)
if(NOT ${ARGC} EQUAL 2)
message(FATAL_ERROR "Wrong arguments to replace_compile_flags! Args ${ARGN}")
endif()
replace_compiler_option(CMAKE_C_FLAGS ${_oldflags} ${_newflags})
replace_compiler_option(CMAKE_CXX_FLAGS ${_oldflags} ${_newflags})
replace_compiler_option(CMAKE_ASM_FLAGS ${_oldflags} ${_newflags})
endmacro()
macro(replace_compile_flags_language _oldflags _newflags _lang)
if(NOT ${ARGC} EQUAL 3)
message(FATAL_ERROR "Wrong arguments to replace_compile_flags_language! Args ${ARGN}")
endif()
replace_compiler_option(CMAKE_${_lang}_FLAGS ${_oldflags} ${_newflags})
endmacro()