From bb96984ec932c899c9544ce21b6b63ef4c455e55 Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Wed, 31 Mar 2021 11:25:03 +0200 Subject: CUDA Device link: Ensure all link options are correctly formatted Fixes: #21994 --- Source/cmGeneratorTarget.cxx | 60 +++++++++++++++++++--- .../target_link_options/RunCMakeTest.cmake | 7 +++ ...genex_DEVICE_LINK-host_link_options-check.cmake | 4 ++ .../genex_DEVICE_LINK-host_link_options-result.txt | 1 + .../target_link_options/genex_DEVICE_LINK.cmake | 8 +++ 5 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 Tests/RunCMake/target_link_options/genex_DEVICE_LINK-host_link_options-check.cmake create mode 100644 Tests/RunCMake/target_link_options/genex_DEVICE_LINK-host_link_options-result.txt diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index d7e9952..efac06a 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -3641,13 +3641,58 @@ std::vector> wrapOptions( return result; } - if (wrapperFlag.empty() || cmHasLiteralPrefix(options.front(), "LINKER:")) { - // nothing specified or LINKER wrapper, insert elements as is + if (wrapperFlag.empty()) { + // nothing specified, insert elements as is result.reserve(options.size()); for (std::string& o : options) { result.emplace_back(std::move(o), bt); } - } else { + return result; + } + + for (std::vector::size_type index = 0; index < options.size(); + index++) { + if (cmHasLiteralPrefix(options[index], "LINKER:")) { + // LINKER wrapper specified, insert elements as is + result.emplace_back(std::move(options[index]), bt); + continue; + } + if (cmHasLiteralPrefix(options[index], "-Wl,")) { + // replace option by LINKER wrapper + result.emplace_back(options[index].replace(0, 4, "LINKER:"), bt); + continue; + } + if (cmHasLiteralPrefix(options[index], "-Xlinker=")) { + // replace option by LINKER wrapper + result.emplace_back(options[index].replace(0, 9, "LINKER:"), bt); + continue; + } + if (options[index] == "-Xlinker") { + // replace option by LINKER wrapper + if (index + 1 < options.size()) { + result.emplace_back("LINKER:" + options[++index], bt); + } else { + result.emplace_back(std::move(options[index]), bt); + } + continue; + } + + // collect all options which must be transformed + std::vector opts; + while (index < options.size()) { + if (!cmHasLiteralPrefix(options[index], "LINKER:") && + !cmHasLiteralPrefix(options[index], "-Wl,") && + !cmHasLiteralPrefix(options[index], "-Xlinker")) { + opts.emplace_back(std::move(options[index++])); + } else { + --index; + break; + } + } + if (opts.empty()) { + continue; + } + if (!wrapperSep.empty()) { if (concatFlagAndArgs) { // insert flag elements except last one @@ -3656,24 +3701,23 @@ std::vector> wrapOptions( } // concatenate last flag element and all list values // in one option - result.emplace_back(wrapperFlag.back() + cmJoin(options, wrapperSep), - bt); + result.emplace_back(wrapperFlag.back() + cmJoin(opts, wrapperSep), bt); } else { for (std::string const& i : wrapperFlag) { result.emplace_back(i, bt); } // concatenate all list values in one option - result.emplace_back(cmJoin(options, wrapperSep), bt); + result.emplace_back(cmJoin(opts, wrapperSep), bt); } } else { // prefix each element of list with wrapper if (concatFlagAndArgs) { - std::transform(options.begin(), options.end(), options.begin(), + std::transform(opts.begin(), opts.end(), opts.begin(), [&wrapperFlag](std::string const& o) -> std::string { return wrapperFlag.back() + o; }); } - for (std::string& o : options) { + for (std::string& o : opts) { for (auto i = wrapperFlag.begin(), e = concatFlagAndArgs ? wrapperFlag.end() - 1 : wrapperFlag.end(); diff --git a/Tests/RunCMake/target_link_options/RunCMakeTest.cmake b/Tests/RunCMake/target_link_options/RunCMakeTest.cmake index 8ef13f9..a707383 100644 --- a/Tests/RunCMake/target_link_options/RunCMakeTest.cmake +++ b/Tests/RunCMake/target_link_options/RunCMakeTest.cmake @@ -17,6 +17,9 @@ if (NOT CMAKE_C_COMPILER_ID STREQUAL "Intel") if (NOT RunCMake_GENERATOR_IS_MULTI_CONFIG) list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_BUILD_TYPE=Release) endif() + if (RunCMake_GENERATOR MATCHES "Ninja") + set(VERBOSE -- -v) + endif() run_cmake(LINK_OPTIONS) @@ -56,6 +59,10 @@ if (NOT CMAKE_C_COMPILER_ID STREQUAL "Intel") run_cmake_target(genex_DEVICE_LINK CMP0105_OLD LinkOptions_CMP0105_OLD --config Release) run_cmake_target(genex_DEVICE_LINK CMP0105_NEW LinkOptions_CMP0105_NEW --config Release) run_cmake_target(genex_DEVICE_LINK device LinkOptions_device --config Release) + + if (RunCMake_GENERATOR MATCHES "(Ninja|Unix Makefiles)") + run_cmake_target(genex_DEVICE_LINK host_link_options LinkOptions_host_link_options --config Release ${VERBOSE}) + endif() endif() run_cmake_target(genex_DEVICE_LINK no_device LinkOptions_no_device --config Release) diff --git a/Tests/RunCMake/target_link_options/genex_DEVICE_LINK-host_link_options-check.cmake b/Tests/RunCMake/target_link_options/genex_DEVICE_LINK-host_link_options-check.cmake new file mode 100644 index 0000000..31ffe7f --- /dev/null +++ b/Tests/RunCMake/target_link_options/genex_DEVICE_LINK-host_link_options-check.cmake @@ -0,0 +1,4 @@ + +if (NOT actual_stdout MATCHES "-Xlinker=OPT1 -Xlinker=OPT2 -Xlinker=OPT3 -Xlinker=OPT4 -Xlinker=OPT5") + set (RunCMake_TEST_FAILED "Not found expected '-Xlinker=OPT1 -Xlinker=OPT2 -Xlinker=OPT3 -Xlinker=OPT4 -Xlinker=OPT5'.") +endif() diff --git a/Tests/RunCMake/target_link_options/genex_DEVICE_LINK-host_link_options-result.txt b/Tests/RunCMake/target_link_options/genex_DEVICE_LINK-host_link_options-result.txt new file mode 100644 index 0000000..8d98f9d --- /dev/null +++ b/Tests/RunCMake/target_link_options/genex_DEVICE_LINK-host_link_options-result.txt @@ -0,0 +1 @@ +.* diff --git a/Tests/RunCMake/target_link_options/genex_DEVICE_LINK.cmake b/Tests/RunCMake/target_link_options/genex_DEVICE_LINK.cmake index 0126080..a53ab20 100644 --- a/Tests/RunCMake/target_link_options/genex_DEVICE_LINK.cmake +++ b/Tests/RunCMake/target_link_options/genex_DEVICE_LINK.cmake @@ -1,6 +1,10 @@ enable_language(C) +set(CMAKE_VERBOSE_MAKEFILE TRUE) +set(CMAKE_C_USE_RESPONSE_FILE_FOR_LIBRARIES FALSE) +set(CMAKE_CXX_USE_RESPONSE_FILE_FOR_LIBRARIES FALSE) + set (obj "${CMAKE_C_OUTPUT_EXTENSION}") if(BORLAND) set(pre -) @@ -43,6 +47,10 @@ if (CMake_TEST_CUDA) set_property(TARGET LinkOptions_device PROPERTY CUDA_SEPARABLE_COMPILATION ON) target_link_options(LinkOptions_device PRIVATE $ $) + + add_executable(LinkOptions_host_link_options LinkOptionsDevice.cu) + set_property(TARGET LinkOptions_host_link_options PROPERTY CUDA_SEPARABLE_COMPILATION ON) + target_link_options(LinkOptions_host_link_options PRIVATE -Wl,OPT1 -Xlinker=OPT2 "SHELL:-Xlinker OPT3" "SHELL:LINKER:OPT4 LINKER:OPT5") endif() add_executable(LinkOptions_no_device LinkOptionsDevice.cu) -- cgit v0.12