#
# @file CMakeFlagsHandling.cmake
#
# @brief
#
# @copyright
# Copyright (©) 2021 EPFL (Ecole Polytechnique Fédérale de Lausanne)
# SPC (Swiss Plasma Center)
#
# futils is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# futils is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see .
#
# @authors
# (in alphabetical order)
# @author Nicolas Richart
#
if(_CMAKE_FLAGS_HANDLING)
return()
endif()
set(_CMAKE_FLAGS_HANDLING TRUE)
#===============================================================================
# Compilation options handling
#===============================================================================
macro(_get_flags_message lang type desc)
if(${lang} MATCHES "C.." OR ${lang} MATCHES "Fortran")
set(${desc} "Flags used by the compiler")
elseif(${lang} MATCHES ".*_LINKER")
set(${desc} "Flags used by the linker")
endif()
if(${lang} MATCHES "SHARED_LINKER")
set(${desc} "${desc} during the creation of shared libraries")
elseif(${lang} MATCHES "MODULE_LINKER")
set(${desc} "${desc} during the creation of modules")
elseif(${lang} MATCHES "STATIC_LINKER")
set(${desc} "${desc} linker during the creation of static libraries")
endif()
if(${type} MATCHES "ALL")
set(${desc} "${desc} during all build types")
else()
set(${desc} "${desc} during ${type} builds")
endif()
endmacro()
#===============================================================================
function(handle_flags)
include(CMakeParseArguments)
cmake_parse_arguments(_flags
"ADD;REMOVE" "LANG;TYPE" ""
${ARGN}
)
if(NOT _flags_LANG)
set(_flags_LANG ${FLAGS_HANDLING_DEFAULT_LANGUAGE})
endif()
set(_variable CMAKE_${_flags_LANG}_FLAGS)
if (_flags_TYPE)
set(_variable ${_variable}_${_flags_TYPE})
else()
set(_flags_TYPE "ALL")
endif()
_get_flags_message(${_flags_LANG} ${_flags_TYPE} _desc)
foreach(flag ${_flags_UNPARSED_ARGUMENTS})
if (_flags_ADD)
string(REPLACE "${flag}" "match" _temp_var "${${_variable}}")
if(NOT _temp_var MATCHES "match")
set(${_variable} "${flag} ${${_variable}}" CACHE STRING ${_desc} FORCE)
endif()
elseif(_flags_REMOVE)
string(REPLACE "${flag} " "" ${_variable} "${${_variable}}")
set(${_variable} "${${_variable}}" CACHE STRING ${_desc} FORCE)
endif()
endforeach()
endfunction()
#===============================================================================
function(add_flags)
handle_flags(ADD ${ARGN})
endfunction()
#===============================================================================
function(remove_flags)
handle_flags(REMOVE ${ARGN})
endfunction()
#===============================================================================