]> https://gitweb.dealii.org/ - dealii.git/commitdiff
CMake: refactor repetitive option handling into two macros
authorMatthias Maier <tamiko@43-1.org>
Thu, 30 Mar 2023 16:21:17 +0000 (11:21 -0500)
committerMatthias Maier <tamiko@43-1.org>
Thu, 30 Mar 2023 17:59:04 +0000 (12:59 -0500)
cmake/macros/macro_insource_setup_target.cmake
cmake/macros/macro_populate_target_properties.cmake
cmake/macros/macro_target_compile_flags.cmake [new file with mode: 0644]
cmake/macros/macro_target_link_flags.cmake [new file with mode: 0644]
source/CMakeLists.txt

index 924c5966a7d8fe7643e0749e5e34ad13bc448d96..d09defe34e84f72e9a00b74493e5234993498e9d 100644 (file)
@@ -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}
index 6ae0cd2bb20d26b18c44fdc9382465a8e9358fcd..db78d538d0b536a1bf448ecc24c5ecfd3e0e2dc6 100644 (file)
@@ -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 (file)
index 0000000..8399a55
--- /dev/null
@@ -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
+#      "$<generator expression>" "compile flags"
+#      )
+#
+# In case of the second variant the compile options will be sandwiched
+# between "$<$<generator expression>:" 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 (file)
index 0000000..0dffd9d
--- /dev/null
@@ -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
+#      "$<generator expression>" "link flags"
+#      )
+#
+# In case of the second variant the compile options will be sandwiched
+# between "$<$<generator expression>:" 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()
index a264a44f513f193933056163d4faa4dee99119bb..e64c3715499dc2c2b766db3d035adede5b54ad2d 100644 (file)
@@ -88,7 +88,6 @@ target_compile_options(${DEAL_II_TARGET_NAME} INTERFACE
   $<$<COMPILE_LANGUAGE:CXX>:${_compile_options}>
   )
 
-
 foreach(build ${DEAL_II_BUILD_TYPES})
   string(TOLOWER ${build} build_lowercase)
   if("${build}" MATCHES "DEBUG")

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.