From a5403dfca95552ec211fc81fef48f57bc135dcbc Mon Sep 17 00:00:00 2001 From: Matthias Maier Date: Mon, 6 Mar 2023 14:28:26 -0600 Subject: [PATCH] CMake: Bugfix: Properly shell-escape -Xcudafe and -Xlinker --- cmake/config/CMakeLists.txt | 7 +-- cmake/macros/macro_deal_ii_setup_target.cmake | 2 + .../macro_define_interface_target.cmake | 2 + .../macros/macro_insource_setup_target.cmake | 2 + .../macro_populate_target_properties.cmake | 2 + .../macro_print_target_properties.cmake | 2 +- .../macro_shell_escape_option_groups.cmake | 53 +++++++++++++++++++ 7 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 cmake/macros/macro_shell_escape_option_groups.cmake diff --git a/cmake/config/CMakeLists.txt b/cmake/config/CMakeLists.txt index aaa53f90e1..7fb02f104f 100644 --- a/cmake/config/CMakeLists.txt +++ b/cmake/config/CMakeLists.txt @@ -42,12 +42,13 @@ configure_file( # for binary dir: ######################################################################## set(_macros + ${CMAKE_SOURCE_DIR}/cmake/macros/macro_deal_ii_add_test.cmake ${CMAKE_SOURCE_DIR}/cmake/macros/macro_deal_ii_initialize_cached_variables.cmake ${CMAKE_SOURCE_DIR}/cmake/macros/macro_deal_ii_invoke_autopilot.cmake - ${CMAKE_SOURCE_DIR}/cmake/macros/macro_deal_ii_setup_target.cmake - ${CMAKE_SOURCE_DIR}/cmake/macros/macro_deal_ii_query_git_information.cmake - ${CMAKE_SOURCE_DIR}/cmake/macros/macro_deal_ii_add_test.cmake ${CMAKE_SOURCE_DIR}/cmake/macros/macro_deal_ii_pickup_tests.cmake + ${CMAKE_SOURCE_DIR}/cmake/macros/macro_deal_ii_query_git_information.cmake + ${CMAKE_SOURCE_DIR}/cmake/macros/macro_deal_ii_setup_target.cmake + ${CMAKE_SOURCE_DIR}/cmake/macros/macro_shell_escape_option_groups.cmake ) file(COPY ${_macros} DESTINATION ${CMAKE_BINARY_DIR}/${DEAL_II_SHARE_RELDIR}/macros diff --git a/cmake/macros/macro_deal_ii_setup_target.cmake b/cmake/macros/macro_deal_ii_setup_target.cmake index f330445317..3c41999f09 100644 --- a/cmake/macros/macro_deal_ii_setup_target.cmake +++ b/cmake/macros/macro_deal_ii_setup_target.cmake @@ -93,6 +93,7 @@ macro(deal_ii_setup_target _target) separate_arguments(_compile_options UNIX_COMMAND "${DEAL_II_CXX_FLAGS} ${DEAL_II_CXX_FLAGS_${_build}}" ) + shell_escape_option_groups(_compile_options) target_compile_options(${_target} PRIVATE $<$:${_compile_options}> ) @@ -102,6 +103,7 @@ macro(deal_ii_setup_target _target) separate_arguments(_link_options UNIX_COMMAND "${DEAL_II_LINKER_FLAGS} ${DEAL_II_LINKER_FLAGS_${_build}}" ) + shell_escape_option_groups(_link_options) target_link_options(${_target} PRIVATE $<$:${_link_options}> ) diff --git a/cmake/macros/macro_define_interface_target.cmake b/cmake/macros/macro_define_interface_target.cmake index b63ab41534..f0444761b8 100644 --- a/cmake/macros/macro_define_interface_target.cmake +++ b/cmake/macros/macro_define_interface_target.cmake @@ -114,6 +114,7 @@ function(define_interface_target _feature) separate_arguments(_compile_options UNIX_COMMAND "${${_feature}_CXX_FLAGS} ${${_feature}_CXX_FLAGS_${_build}}" ) + shell_escape_option_groups(_compile_options) if(NOT "${_compile_options}" STREQUAL "") message(STATUS " COMPILE_OPTIONS: ${_compile_options}") target_compile_options(${_interface_target} INTERFACE $<$:${_compile_options}>) @@ -122,6 +123,7 @@ function(define_interface_target _feature) separate_arguments(_link_options UNIX_COMMAND "${${_feature}_LINKER_FLAGS} ${${_feature}_LINKER_FLAGS_${_build}}" ) + shell_escape_option_groups(_link_options) if(NOT "${_link_options}" STREQUAL "") message(STATUS " LINK_OPTIONS: ${_link_options}") target_link_options(${_interface_target} INTERFACE $<$:${_link_options}>) diff --git a/cmake/macros/macro_insource_setup_target.cmake b/cmake/macros/macro_insource_setup_target.cmake index c67e83e747..fb67006e38 100644 --- a/cmake/macros/macro_insource_setup_target.cmake +++ b/cmake/macros/macro_insource_setup_target.cmake @@ -34,6 +34,7 @@ function(insource_setup_target _target _build) separate_arguments(_compile_options UNIX_COMMAND "${DEAL_II_CXX_FLAGS} ${DEAL_II_CXX_FLAGS_${_build}}" ) + shell_escape_option_groups(_compile_options) target_compile_options(${_target} PRIVATE $<$:${_compile_options}> ) @@ -43,6 +44,7 @@ function(insource_setup_target _target _build) separate_arguments(_link_options UNIX_COMMAND "${DEAL_II_LINKER_FLAGS} ${DEAL_II_LINKER_FLAGS_${_build}}" ) + shell_escape_option_groups(_link_options) target_link_options(${_target} PRIVATE $<$:${_link_options}> ) diff --git a/cmake/macros/macro_populate_target_properties.cmake b/cmake/macros/macro_populate_target_properties.cmake index 8363484ea7..9cb79f18b6 100644 --- a/cmake/macros/macro_populate_target_properties.cmake +++ b/cmake/macros/macro_populate_target_properties.cmake @@ -97,6 +97,7 @@ function(populate_target_properties _target _build) separate_arguments(_compile_options UNIX_COMMAND "${DEAL_II_CXX_FLAGS} ${DEAL_II_CXX_FLAGS_${_build}}" ) + shell_escape_option_groups(_compile_options) target_compile_options(${_target} PRIVATE $<$:${_compile_options}> ) @@ -106,6 +107,7 @@ function(populate_target_properties _target _build) separate_arguments(_link_options UNIX_COMMAND "${DEAL_II_LINKER_FLAGS} ${DEAL_II_LINKER_FLAGS_${_build}}" ) + shell_escape_option_groups(_link_options) target_link_options(${_target} PRIVATE $<$:${_link_options}> ) diff --git a/cmake/macros/macro_print_target_properties.cmake b/cmake/macros/macro_print_target_properties.cmake index d6f9b9b5ff..daaea35267 100644 --- a/cmake/macros/macro_print_target_properties.cmake +++ b/cmake/macros/macro_print_target_properties.cmake @@ -44,7 +44,7 @@ function(print_target_properties _target) foreach(_property ${_properties}) get_target_property(_value ${_target} ${_property}) if(NOT "${_value}" MATCHES "-NOTFOUND" AND NOT "${_value}" STREQUAL "") - string(REPLACE ";" " " _value "${_value}") + string(REPLACE ";" "," _value "${_value}") # workaround: we cannot use ";" list(APPEND _messages " ${_property}: ${_value}") endif() endforeach() diff --git a/cmake/macros/macro_shell_escape_option_groups.cmake b/cmake/macros/macro_shell_escape_option_groups.cmake new file mode 100644 index 0000000000..5c04853cff --- /dev/null +++ b/cmake/macros/macro_shell_escape_option_groups.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. +## +## --------------------------------------------------------------------- + +# +# Shell escape known option groups such as -Xlinker [...], or +# -Xcudafe [...] so that the toggle parameter (-Xlinker) does not get +# deduplicated by CMake. +# +# Prerequisite: variable must be the name of a _list_ variable containing +# compile or link options. +# +# Usage: +# shell_escape_option_groups(variable) +# + +macro(shell_escape_option_groups _variable) + # + # We have to capture simple option groups of the form + # "-Xlinker --as-needed" + # but also multiple escape sequences such as + # "-Xlinker -rpath -Xlinker /path". + # If we happen to escape the latter in the following form: + # "SHELL:-Xlinker -rpath" "SHELL:-Xlinker /path" then the first statement + # "SHELL:-Xlinker -rpath" might still get deduplicated... + # + # Let's play this game only over the two options groups above in order to + # keep the regex sane. + # + + # Matches: "-Xlinker;[option]" and replaces it with "SHELL:-Xlinker [option]" + string(REGEX REPLACE + "(-Xcudafe|-Xlinker);([^;]+)" + "SHELL:\\1 \\2" ${_variable} "${${_variable}}" + ) + # Matches: "SHELL:-Xlinker [option1];SHELL:-Xlinker [option2]" and replaces + # it with "SHELL:-Xlinker [option1] -Xlinker [option2]" + string(REGEX REPLACE + "SHELL:(-Xcudafe [^;]+|-Xlinker [^;]+);SHELL:(-Xcudafe [^;]+|-Xlinker [^;]+)" + "SHELL:\\1 \\2" ${_variable} "${${_variable}}" + ) +endmacro() -- 2.39.5