From 922883782bb0a3f80cdfb4d959ca11834b417d77 Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Mon, 11 Mar 2024 18:10:43 +0100 Subject: LINKER_TYPE: Document that linker tool should be in the PATH Issue: #25748 --- Help/variable/CMAKE_LANG_USING_LINKER_TYPE.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Help/variable/CMAKE_LANG_USING_LINKER_TYPE.rst b/Help/variable/CMAKE_LANG_USING_LINKER_TYPE.rst index e4d9fa6..d4714c3 100644 --- a/Help/variable/CMAKE_LANG_USING_LINKER_TYPE.rst +++ b/Help/variable/CMAKE_LANG_USING_LINKER_TYPE.rst @@ -9,6 +9,12 @@ property :prop_tgt:`LINKER_TYPE`. It can hold compiler flags for the link step or directly the linker tool. The type of data is given by the variable :variable:`CMAKE__USING_LINKER_MODE`. +.. note:: + + The specified linker tool is expected to be accessible through + the ``PATH`` environment variable, particularly when the + :variable:`CMAKE__USING_LINKER_MODE` variable is set to ``FLAG``. + For example, to specify the ``LLVM`` linker for ``GNU`` compilers, we have: .. code-block:: cmake -- cgit v0.12 From 939ac5287e9a04398a8eb01243e14e00d13d38ef Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Tue, 12 Mar 2024 18:18:25 +0100 Subject: LINKER_TYPE: fix spelling error in message --- Source/cmGeneratorTarget.cxx | 2 +- Source/cmLocalGenerator.cxx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 7dd9304..46c9082 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -5588,7 +5588,7 @@ std::string cmGeneratorTarget::GetLinkerTool(const std::string& lang, this->LocalGenerator->IssueMessage( MessageType::FATAL_ERROR, cmStrCat("LINKER_TYPE '", linkerType, - "' is unknown. Did you forgot to define '", usingLinker, + "' is unknown. Did you forget to define '", usingLinker, "' variable?")); } } diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 4afed43..1dd2f4a 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -3359,7 +3359,7 @@ void cmLocalGenerator::AppendLinkerTypeFlags(std::string& flags, } else if (linkerType != "DEFAULT"_s) { this->IssueMessage(MessageType::FATAL_ERROR, cmStrCat("LINKER_TYPE '", linkerType, - "' is unknown. Did you forgot to define '", + "' is unknown. Did you forget to define '", usingLinker, "' variable?")); } } -- cgit v0.12 From 801ae06952fc090d07c666b1e56573cae648f599 Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Mon, 11 Mar 2024 18:10:43 +0100 Subject: LINKER_TYPE: Support MOLD only on GCC versions that support it Fixes: #25748 --- Modules/Platform/Linux-GNU.cmake | 5 ++++- Source/cmGeneratorTarget.cxx | 22 +++++++++++++++++----- Source/cmLocalGenerator.cxx | 21 +++++++++++++++++---- .../LinkerSelection/InvalidLinkerType-result.txt | 1 - .../LinkerSelection/InvalidLinkerType-stderr.txt | 3 --- .../LinkerSelection/InvalidLinkerType.cmake | 5 ----- .../LinkerSelection/InvalidLinkerType1-result.txt | 1 + .../LinkerSelection/InvalidLinkerType1-stderr.txt | 2 ++ .../LinkerSelection/InvalidLinkerType1.cmake | 5 +++++ .../LinkerSelection/InvalidLinkerType2-result.txt | 1 + .../LinkerSelection/InvalidLinkerType2-stderr.txt | 3 +++ .../LinkerSelection/InvalidLinkerType2.cmake | 5 +++++ Tests/RunCMake/LinkerSelection/RunCMakeTest.cmake | 3 ++- 13 files changed, 57 insertions(+), 20 deletions(-) delete mode 100644 Tests/RunCMake/LinkerSelection/InvalidLinkerType-result.txt delete mode 100644 Tests/RunCMake/LinkerSelection/InvalidLinkerType-stderr.txt delete mode 100644 Tests/RunCMake/LinkerSelection/InvalidLinkerType.cmake create mode 100644 Tests/RunCMake/LinkerSelection/InvalidLinkerType1-result.txt create mode 100644 Tests/RunCMake/LinkerSelection/InvalidLinkerType1-stderr.txt create mode 100644 Tests/RunCMake/LinkerSelection/InvalidLinkerType1.cmake create mode 100644 Tests/RunCMake/LinkerSelection/InvalidLinkerType2-result.txt create mode 100644 Tests/RunCMake/LinkerSelection/InvalidLinkerType2-stderr.txt create mode 100644 Tests/RunCMake/LinkerSelection/InvalidLinkerType2.cmake diff --git a/Modules/Platform/Linux-GNU.cmake b/Modules/Platform/Linux-GNU.cmake index c3878eb..24bf1bb 100644 --- a/Modules/Platform/Linux-GNU.cmake +++ b/Modules/Platform/Linux-GNU.cmake @@ -20,5 +20,8 @@ macro(__linux_compiler_gnu lang) set(CMAKE_${lang}_USING_LINKER_LLD "-fuse-ld=lld") set(CMAKE_${lang}_USING_LINKER_BFD "-fuse-ld=bfd") set(CMAKE_${lang}_USING_LINKER_GOLD "-fuse-ld=gold") - set(CMAKE_${lang}_USING_LINKER_MOLD "-fuse-ld=mold") + if(NOT CMAKE_${lang}_COMPILER_ID STREQUAL "GNU" + OR CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL "12.1") + set(CMAKE_${lang}_USING_LINKER_MOLD "-fuse-ld=mold") + endif() endmacro() diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 46c9082..a40af8b 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -5585,11 +5586,22 @@ std::string cmGeneratorTarget::GetLinkerTool(const std::string& lang, linkerTool = this->Makefile->GetDefinition("CMAKE_LINKER"); if (linkerType != "DEFAULT"_s) { - this->LocalGenerator->IssueMessage( - MessageType::FATAL_ERROR, - cmStrCat("LINKER_TYPE '", linkerType, - "' is unknown. Did you forget to define '", usingLinker, - "' variable?")); + auto isCMakeLinkerType = [](const std::string& type) -> bool { + return std::all_of(type.cbegin(), type.cend(), + [](char c) { return std::isupper(c); }); + }; + if (isCMakeLinkerType(linkerType)) { + this->LocalGenerator->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat("LINKER_TYPE '", linkerType, + "' is unknown or not supported by this toolchain.")); + } else { + this->LocalGenerator->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat("LINKER_TYPE '", linkerType, + "' is unknown. Did you forget to define the '", usingLinker, + "' variable?")); + } } } diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 1dd2f4a..d38ed50 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -3357,10 +3358,22 @@ void cmLocalGenerator::AppendLinkerTypeFlags(std::string& flags, this->AppendFlags(flags, linkerFlags); } } else if (linkerType != "DEFAULT"_s) { - this->IssueMessage(MessageType::FATAL_ERROR, - cmStrCat("LINKER_TYPE '", linkerType, - "' is unknown. Did you forget to define '", - usingLinker, "' variable?")); + auto isCMakeLinkerType = [](const std::string& type) -> bool { + return std::all_of(type.cbegin(), type.cend(), + [](char c) { return std::isupper(c); }); + }; + if (isCMakeLinkerType(linkerType)) { + this->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat("LINKER_TYPE '", linkerType, + "' is unknown or not supported by this toolchain.")); + } else { + this->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat("LINKER_TYPE '", linkerType, + "' is unknown. Did you forget to define the '", usingLinker, + "' variable?")); + } } } diff --git a/Tests/RunCMake/LinkerSelection/InvalidLinkerType-result.txt b/Tests/RunCMake/LinkerSelection/InvalidLinkerType-result.txt deleted file mode 100644 index d00491f..0000000 --- a/Tests/RunCMake/LinkerSelection/InvalidLinkerType-result.txt +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/Tests/RunCMake/LinkerSelection/InvalidLinkerType-stderr.txt b/Tests/RunCMake/LinkerSelection/InvalidLinkerType-stderr.txt deleted file mode 100644 index 11aea7a..0000000 --- a/Tests/RunCMake/LinkerSelection/InvalidLinkerType-stderr.txt +++ /dev/null @@ -1,3 +0,0 @@ -CMake Error in CMakeLists.txt: - LINKER_TYPE 'FOO' is unknown. Did you forgot to define - 'CMAKE_C_USING_LINKER_FOO' variable\? diff --git a/Tests/RunCMake/LinkerSelection/InvalidLinkerType.cmake b/Tests/RunCMake/LinkerSelection/InvalidLinkerType.cmake deleted file mode 100644 index bbe398c..0000000 --- a/Tests/RunCMake/LinkerSelection/InvalidLinkerType.cmake +++ /dev/null @@ -1,5 +0,0 @@ - -enable_language(C) - -set(CMAKE_LINKER_TYPE FOO) -add_executable(main main.c) diff --git a/Tests/RunCMake/LinkerSelection/InvalidLinkerType1-result.txt b/Tests/RunCMake/LinkerSelection/InvalidLinkerType1-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/LinkerSelection/InvalidLinkerType1-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/LinkerSelection/InvalidLinkerType1-stderr.txt b/Tests/RunCMake/LinkerSelection/InvalidLinkerType1-stderr.txt new file mode 100644 index 0000000..5df644e --- /dev/null +++ b/Tests/RunCMake/LinkerSelection/InvalidLinkerType1-stderr.txt @@ -0,0 +1,2 @@ +CMake Error in CMakeLists.txt: + LINKER_TYPE 'FOO' is unknown or not supported by this toolchain. diff --git a/Tests/RunCMake/LinkerSelection/InvalidLinkerType1.cmake b/Tests/RunCMake/LinkerSelection/InvalidLinkerType1.cmake new file mode 100644 index 0000000..bbe398c --- /dev/null +++ b/Tests/RunCMake/LinkerSelection/InvalidLinkerType1.cmake @@ -0,0 +1,5 @@ + +enable_language(C) + +set(CMAKE_LINKER_TYPE FOO) +add_executable(main main.c) diff --git a/Tests/RunCMake/LinkerSelection/InvalidLinkerType2-result.txt b/Tests/RunCMake/LinkerSelection/InvalidLinkerType2-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/LinkerSelection/InvalidLinkerType2-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/LinkerSelection/InvalidLinkerType2-stderr.txt b/Tests/RunCMake/LinkerSelection/InvalidLinkerType2-stderr.txt new file mode 100644 index 0000000..b8c2391 --- /dev/null +++ b/Tests/RunCMake/LinkerSelection/InvalidLinkerType2-stderr.txt @@ -0,0 +1,3 @@ +CMake Error in CMakeLists.txt: + LINKER_TYPE 'foo' is unknown. Did you forget to define the + 'CMAKE_C_USING_LINKER_foo' variable\? diff --git a/Tests/RunCMake/LinkerSelection/InvalidLinkerType2.cmake b/Tests/RunCMake/LinkerSelection/InvalidLinkerType2.cmake new file mode 100644 index 0000000..9245512 --- /dev/null +++ b/Tests/RunCMake/LinkerSelection/InvalidLinkerType2.cmake @@ -0,0 +1,5 @@ + +enable_language(C) + +set(CMAKE_LINKER_TYPE foo) +add_executable(main main.c) diff --git a/Tests/RunCMake/LinkerSelection/RunCMakeTest.cmake b/Tests/RunCMake/LinkerSelection/RunCMakeTest.cmake index cae4ca4..8929a0d 100644 --- a/Tests/RunCMake/LinkerSelection/RunCMakeTest.cmake +++ b/Tests/RunCMake/LinkerSelection/RunCMakeTest.cmake @@ -5,7 +5,8 @@ if (RunCMake_GENERATOR MATCHES "Visual Studio 9 2008") return() endif() -run_cmake(InvalidLinkerType) +run_cmake(InvalidLinkerType1) +run_cmake(InvalidLinkerType2) # look-up for LLVM linker if (WIN32) -- cgit v0.12