From: Matthias Maier Date: Thu, 30 Mar 2023 16:21:17 +0000 (-0500) Subject: CMake: refactor repetitive option handling into two macros X-Git-Tag: v9.5.0-rc1~392^2~3 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=29a7ba33b74a9d53c8ec9307ddc06d9c081658f3;p=dealii.git CMake: refactor repetitive option handling into two macros --- diff --git a/cmake/macros/macro_insource_setup_target.cmake b/cmake/macros/macro_insource_setup_target.cmake index 924c5966a7..d09defe34e 100644 --- a/cmake/macros/macro_insource_setup_target.cmake +++ b/cmake/macros/macro_insource_setup_target.cmake @@ -31,19 +31,15 @@ function(insource_setup_target _target _build) RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" ) - separate_arguments(_compile_options UNIX_COMMAND + target_compile_flags(${_target} PRIVATE "${DEAL_II_WARNING_FLAGS} ${DEAL_II_CXX_FLAGS} ${DEAL_II_CXX_FLAGS_${_build}}" ) - shell_escape_option_groups(_compile_options) - target_compile_options(${_target} PRIVATE ${_compile_options}) get_property(_type TARGET ${_target} PROPERTY TYPE) if(NOT "${_type}" STREQUAL "OBJECT_LIBRARY") - separate_arguments(_link_options UNIX_COMMAND + target_link_flags(${_target} PRIVATE "${DEAL_II_LINKER_FLAGS} ${DEAL_II_LINKER_FLAGS_${_build}}" ) - shell_escape_option_groups(_link_options) - target_link_options(${_target} PRIVATE ${_link_options}) endif() target_include_directories(${_target} diff --git a/cmake/macros/macro_populate_target_properties.cmake b/cmake/macros/macro_populate_target_properties.cmake index 6ae0cd2bb2..db78d538d0 100644 --- a/cmake/macros/macro_populate_target_properties.cmake +++ b/cmake/macros/macro_populate_target_properties.cmake @@ -104,19 +104,15 @@ function(populate_target_properties _target _build) # interface: # - separate_arguments(_compile_options UNIX_COMMAND + target_compile_flags(${_target} PRIVATE "${DEAL_II_WARNING_FLAGS} ${DEAL_II_CXX_FLAGS} ${DEAL_II_CXX_FLAGS_${_build}}" ) - shell_escape_option_groups(_compile_options) - target_compile_options(${_target} PRIVATE ${_compile_options}) get_property(_type TARGET ${_target} PROPERTY TYPE) if(NOT "${_type}" STREQUAL "OBJECT_LIBRARY") - separate_arguments(_link_options UNIX_COMMAND + target_link_flags(${_target} PRIVATE "${DEAL_II_LINKER_FLAGS} ${DEAL_II_LINKER_FLAGS_${_build}}" ) - shell_escape_option_groups(_link_options) - target_link_options(${_target} PRIVATE ${_link_options}) endif() target_link_libraries(${_target} ${_visibility} diff --git a/cmake/macros/macro_target_compile_flags.cmake b/cmake/macros/macro_target_compile_flags.cmake new file mode 100644 index 0000000000..8399a55f4d --- /dev/null +++ b/cmake/macros/macro_target_compile_flags.cmake @@ -0,0 +1,53 @@ +## --------------------------------------------------------------------- +## +## Copyright (C) 2023 - 2023 by the deal.II authors +## +## This file is part of the deal.II library. +## +## The deal.II library is free software; you can use it, 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 2.1 of the License, or (at your option) any later version. +## The full text of the license can be found in the file LICENSE.md at +## the top level directory of deal.II. +## +## --------------------------------------------------------------------- + +# +# A small macro that takes a target, keyword and a string as argument and +# applies the given string as compile options to the target: +# +# Usage: +# target_compile_flags(target INTERFACE|PUBLIC|PRIVATE "compile flags") +# +# target_compile_flags(target INTERFACE|PUBLIC|PRIVATE +# "$" "compile flags" +# ) +# +# In case of the second variant the compile options will be sandwiched +# between "$<$:" and ">". +# +function(target_compile_flags _target _keyword _string) + if(NOT TARGET ${_target}) + message(FATAL_ERROR "»${_target}« is not a valid target") + endif() + if(NOT ${_keyword} MATCHES "(INTERFACE|PUBLIC|PRIVATE)") + message(FATAL_ERROR + "The supplied keyword has to be one of INTERFACE, PUBLIC, or PRIVATE" + ) + endif() + + set(_guard_left "") + set(_guard_right "") + if("${_string}" MATCHES "^\\$<.*>$") + set(_guard_left "$<${_string}:") + set(_guard_right ">") + set(_string "${ARGN}") + endif() + + separate_arguments(_compile_options UNIX_COMMAND "${_string}") + shell_escape_option_groups(_compile_options) + target_compile_options(${_target} ${_keyword} + ${_guard_left}${_compile_options}${_guard_right} + ) +endfunction() diff --git a/cmake/macros/macro_target_link_flags.cmake b/cmake/macros/macro_target_link_flags.cmake new file mode 100644 index 0000000000..0dffd9d902 --- /dev/null +++ b/cmake/macros/macro_target_link_flags.cmake @@ -0,0 +1,54 @@ +## --------------------------------------------------------------------- +## +## Copyright (C) 2023 - 2023 by the deal.II authors +## +## This file is part of the deal.II library. +## +## The deal.II library is free software; you can use it, 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 2.1 of the License, or (at your option) any later version. +## The full text of the license can be found in the file LICENSE.md at +## the top level directory of deal.II. +## +## --------------------------------------------------------------------- + +# +# A small macro that takes a target, keyword and a string as argument and +# applies the given string as link options to the target: +# +# Usage: +# target_link_flags(target INTERFACE|PUBLIC|PRIVATE "link flags") +# +# target_link_flags(target INTERFACE|PUBLIC|PRIVATE +# "$" "link flags" +# ) +# +# In case of the second variant the compile options will be sandwiched +# between "$<$:" and ">". +# +function(target_link_flags _target _keyword _string) + if(NOT TARGET ${_target}) + message(FATAL_ERROR "»${_target}« is not a valid target") + endif() + + if(NOT ${_keyword} MATCHES "(INTERFACE|PUBLIC|PRIVATE)") + message(FATAL_ERROR + "The supplied keyword has to be one of INTERFACE, PUBLIC, or PRIVATE" + ) + endif() + + set(_guard_left "") + set(_guard_right "") + if("${_string}" MATCHES "^\\$<.*>$") + set(_guard_left "$<${_string}:") + set(_guard_right ">") + set(_string "${ARGN}") + endif() + + separate_arguments(_link_options UNIX_COMMAND "${_string}") + shell_escape_option_groups(_link_options) + target_link_options(${_target} ${_keyword} + ${_guard_left}${_link_options}${_guard_right} + ) +endfunction() diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index a264a44f51..e64c371549 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -88,7 +88,6 @@ target_compile_options(${DEAL_II_TARGET_NAME} INTERFACE $<$:${_compile_options}> ) - foreach(build ${DEAL_II_BUILD_TYPES}) string(TOLOWER ${build} build_lowercase) if("${build}" MATCHES "DEBUG")