From be0d4042082db4dcd263dd181570b6f406202ed9 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 5 Jun 2019 22:01:16 -0700 Subject: Support per-language library link flags This changes the behaviour of the generators to use a per-language library search path flag. This is needed for multi-language projects with different compilers (e.g. cl + gfortran). Since the adjusted variable has been part of the user settings, we control this based on a policy. Fixes: #19307 --- Modules/CMakeSwiftInformation.cmake | 2 ++ Source/cmLocalGenerator.cxx | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Modules/CMakeSwiftInformation.cmake b/Modules/CMakeSwiftInformation.cmake index 21f18d4..6f99c1f 100644 --- a/Modules/CMakeSwiftInformation.cmake +++ b/Modules/CMakeSwiftInformation.cmake @@ -23,6 +23,8 @@ set(CMAKE_Swift_COMPILE_OPTIONS_TARGET "-target ") set(CMAKE_Swift_COMPILER_ARG1 -frontend) set(CMAKE_Swift_DEFINE_FLAG -D) set(CMAKE_Swift_FRAMEWORK_SEARCH_FLAG "-F ") +set(CMAKE_Swift_LIBRARY_PATH_FLAG "-L ") +set(CMAKE_Swift_LIBRARY_PATH_TERMINATOR "") set(CMAKE_Swift_LINKER_WRAPPER_FLAG "-Xlinker" " ") set(CMAKE_Swift_RESPONSE_FILE_LINK_FLAG @) diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index fe5c8af..3abf2dd 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -1444,10 +1444,23 @@ void cmLocalGenerator::OutputLinkLibraries( std::string linkLanguage = cli.GetLinkLanguage(); - const std::string& libPathFlag = - this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG"); - const std::string& libPathTerminator = - this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR"); + std::string libPathFlag; + if (const char* value = this->Makefile->GetDefinition( + "CMAKE_" + cli.GetLinkLanguage() + "_LIBRARY_PATH_FLAG")) { + libPathFlag = value; + } else { + libPathFlag = + this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG"); + } + + std::string libPathTerminator; + if (const char* value = this->Makefile->GetDefinition( + "CMAKE_" + cli.GetLinkLanguage() + "_LIBRARY_PATH_TERMINATOR")) { + libPathTerminator = value; + } else { + libPathTerminator = + this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_TERMINATOR"); + } // Add standard libraries for this language. std::string standardLibsVar = "CMAKE_"; -- cgit v0.12 From c8741e8ff5ed93467812378413f1b3338c0ee6b2 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 9 Jun 2019 12:51:00 -0700 Subject: Swift: Add library search paths for dependencies When building Swift executables and libraries which import a module, an implicit link will be added by the driver. Because this links by name rather than path, the library search path needs to be provided to indicate where to find the library. For all local dependencies, add the library paths for the targets when linking. This ensures that you can link against local libraries without explicitly setting a library path. Fixes: #19304 --- Source/cmLinkLineComputer.cxx | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/Source/cmLinkLineComputer.cxx b/Source/cmLinkLineComputer.cxx index 2a8bee6..469faca 100644 --- a/Source/cmLinkLineComputer.cxx +++ b/Source/cmLinkLineComputer.cxx @@ -99,14 +99,34 @@ std::string cmLinkLineComputer::ComputeLinkPath( std::string const& libPathTerminator) { std::string linkPath; - std::vector const& libDirs = cli.GetDirectories(); - for (std::string const& libDir : libDirs) { - std::string libpath = this->ConvertToOutputForExisting(libDir); - linkPath += " " + libPathFlag; - linkPath += libpath; - linkPath += libPathTerminator; - linkPath += " "; + + if (cli.GetLinkLanguage() == "Swift") { + for (const cmComputeLinkInformation::Item& item : cli.GetItems()) { + const cmGeneratorTarget* target = item.Target; + if (!target) { + continue; + } + + if (target->GetType() == cmStateEnums::STATIC_LIBRARY || + target->GetType() == cmStateEnums::SHARED_LIBRARY) { + cmStateEnums::ArtifactType type = cmStateEnums::RuntimeBinaryArtifact; + if (target->GetType() == cmStateEnums::SHARED_LIBRARY && + target->IsDLLPlatform()) { + type = cmStateEnums::ImportLibraryArtifact; + } + + linkPath += " " + libPathFlag + + item.Target->GetDirectory(cli.GetConfig(), type) + + libPathTerminator + " "; + } + } } + + for (std::string const& libDir : cli.GetDirectories()) { + linkPath += " " + libPathFlag + this->ConvertToOutputForExisting(libDir) + + libPathTerminator + " "; + } + return linkPath; } -- cgit v0.12 From 6814567e8c1c73c09e98ebcd1296e4fda717f0a4 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Thu, 27 Jun 2019 21:28:48 -0700 Subject: Swift: add rules for static linking The Swift driver recently learnt how to generate static libraries using the `-static` flag. This enables us to generate proper static libraries with dependency tracking with Swift as well. --- Modules/CMakeSwiftInformation.cmake | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Modules/CMakeSwiftInformation.cmake b/Modules/CMakeSwiftInformation.cmake index 6f99c1f..f62f61b 100644 --- a/Modules/CMakeSwiftInformation.cmake +++ b/Modules/CMakeSwiftInformation.cmake @@ -70,6 +70,12 @@ if(NOT CMAKE_Swift_LINK_EXECUTABLE) endif() if(NOT CMAKE_Swift_CREATE_STATIC_LIBRARY) + if(CMAKE_Swift_COMPILER_TARGET) + set(CMAKE_Swift_CREATE_STATIC_LIBRARY "${CMAKE_Swift_COMPILER} -target -output-file-map -incremental -emit-library -static -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") + else() + set(CMAKE_Swift_CREATE_STATIC_LIBRARY "${CMAKE_Swift_COMPILER} -output-file-map -incremental -emit-library -static -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") + endif() + set(CMAKE_Swift_ARCHIVE_CREATE " crs ") set(CMAKE_Swift_ARCHIVE_FINISH "") endif() -- cgit v0.12 From 47e28cbe1faf7b70bda0d7d88db1d146d69a0123 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 30 Jun 2019 20:50:08 -0700 Subject: Swift: support multithreaded compilation Query the number of logical CPUs available to enable parallel compilation for Swift. --- Help/manual/cmake-variables.7.rst | 1 + Help/variable/CMAKE_Swift_NUM_THREADS.rst | 8 ++++++++ Modules/CMakeSwiftInformation.cmake | 16 ++++++++++------ 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 Help/variable/CMAKE_Swift_NUM_THREADS.rst diff --git a/Help/manual/cmake-variables.7.rst b/Help/manual/cmake-variables.7.rst index 75ddd5d..e3c2f9f 100644 --- a/Help/manual/cmake-variables.7.rst +++ b/Help/manual/cmake-variables.7.rst @@ -98,6 +98,7 @@ Variables that Provide Information /variable/CMAKE_STATIC_LIBRARY_PREFIX /variable/CMAKE_STATIC_LIBRARY_SUFFIX /variable/CMAKE_Swift_MODULE_DIRECTORY + /variable/CMAKE_Swift_NUM_THREADS /variable/CMAKE_TOOLCHAIN_FILE /variable/CMAKE_TWEAK_VERSION /variable/CMAKE_VERBOSE_MAKEFILE diff --git a/Help/variable/CMAKE_Swift_NUM_THREADS.rst b/Help/variable/CMAKE_Swift_NUM_THREADS.rst new file mode 100644 index 0000000..cb33678 --- /dev/null +++ b/Help/variable/CMAKE_Swift_NUM_THREADS.rst @@ -0,0 +1,8 @@ +CMAKE_Swift_NUM_THREADS +----------------------- + +Number of threads for parallel compilation for Swift targets. + +This variable controls the number of parallel jobs that the swift driver creates +for building targets. If not specified, it will default to the number of +logical CPUs on the host. diff --git a/Modules/CMakeSwiftInformation.cmake b/Modules/CMakeSwiftInformation.cmake index f62f61b..0f84cce 100644 --- a/Modules/CMakeSwiftInformation.cmake +++ b/Modules/CMakeSwiftInformation.cmake @@ -49,11 +49,15 @@ if(NOT CMAKE_Swift_COMPILE_OBJECT) set(CMAKE_Swift_COMPILE_OBJECT ":") endif() +if(NOT CMAKE_Swift_NUM_THREADS MATCHES "^[0-9]+$") + cmake_host_system_information(RESULT CMAKE_Swift_NUM_THREADS QUERY NUMBER_OF_LOGICAL_CORES) +endif() + if(NOT CMAKE_Swift_CREATE_SHARED_LIBRARY) if(CMAKE_Swift_COMPILER_TARGET) - set(CMAKE_Swift_CREATE_SHARED_LIBRARY "${CMAKE_Swift_COMPILER} -target -output-file-map -incremental -emit-library -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") + set(CMAKE_Swift_CREATE_SHARED_LIBRARY "${CMAKE_Swift_COMPILER} -target -output-file-map -incremental -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-library -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") else() - set(CMAKE_Swift_CREATE_SHARED_LIBRARY "${CMAKE_Swift_COMPILER} -output-file-map -incremental -emit-library -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") + set(CMAKE_Swift_CREATE_SHARED_LIBRARY "${CMAKE_Swift_COMPILER} -output-file-map -incremental -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-library -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") endif() endif() @@ -63,17 +67,17 @@ endif() if(NOT CMAKE_Swift_LINK_EXECUTABLE) if(CMAKE_Swift_COMPILER_TARGET) - set(CMAKE_Swift_LINK_EXECUTABLE "${CMAKE_Swift_COMPILER} -target -output-file-map -incremental -emit-executable -o -emit-module -emit-module-path -emit-dependencies ") + set(CMAKE_Swift_LINK_EXECUTABLE "${CMAKE_Swift_COMPILER} -target -output-file-map -incremental -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-executable -o -emit-module -emit-module-path -emit-dependencies ") else() - set(CMAKE_Swift_LINK_EXECUTABLE "${CMAKE_Swift_COMPILER} -output-file-map -incremental -emit-executable -o -emit-module -emit-module-path -emit-dependencies ") + set(CMAKE_Swift_LINK_EXECUTABLE "${CMAKE_Swift_COMPILER} -output-file-map -incremental -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-executable -o -emit-module -emit-module-path -emit-dependencies ") endif() endif() if(NOT CMAKE_Swift_CREATE_STATIC_LIBRARY) if(CMAKE_Swift_COMPILER_TARGET) - set(CMAKE_Swift_CREATE_STATIC_LIBRARY "${CMAKE_Swift_COMPILER} -target -output-file-map -incremental -emit-library -static -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") + set(CMAKE_Swift_CREATE_STATIC_LIBRARY "${CMAKE_Swift_COMPILER} -target -output-file-map -incremental -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-library -static -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") else() - set(CMAKE_Swift_CREATE_STATIC_LIBRARY "${CMAKE_Swift_COMPILER} -output-file-map -incremental -emit-library -static -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") + set(CMAKE_Swift_CREATE_STATIC_LIBRARY "${CMAKE_Swift_COMPILER} -output-file-map -incremental -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-library -static -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") endif() set(CMAKE_Swift_ARCHIVE_CREATE " crs ") -- cgit v0.12 From 5803a8a2f7e0894810e9bd2e8c433dce7346cf76 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Thu, 11 Jul 2019 11:12:26 -0700 Subject: Swift: support SONAME on ELFish targets We should enable the soname to be setup for ELF shared libraries. Failure to do so generates binaries with incorrect names which prevents loading. --- Modules/CMakeSwiftInformation.cmake | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Modules/CMakeSwiftInformation.cmake b/Modules/CMakeSwiftInformation.cmake index 0f84cce..f6510b9 100644 --- a/Modules/CMakeSwiftInformation.cmake +++ b/Modules/CMakeSwiftInformation.cmake @@ -18,6 +18,9 @@ if(CMAKE_Swift_COMPILER_ID) endif() set(CMAKE_INCLUDE_FLAG_Swift "-I ") +if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows AND NOT CMAKE_SYSTEM_NAME STREQUAL Darwin) + set(CMAKE_SHARED_LIBRARY_SONAME_Swift_FLAG "-Xlinker -soname -Xlinker ") +endif() set(CMAKE_Swift_COMPILE_OPTIONS_TARGET "-target ") set(CMAKE_Swift_COMPILER_ARG1 -frontend) @@ -55,9 +58,9 @@ endif() if(NOT CMAKE_Swift_CREATE_SHARED_LIBRARY) if(CMAKE_Swift_COMPILER_TARGET) - set(CMAKE_Swift_CREATE_SHARED_LIBRARY "${CMAKE_Swift_COMPILER} -target -output-file-map -incremental -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-library -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") + set(CMAKE_Swift_CREATE_SHARED_LIBRARY "${CMAKE_Swift_COMPILER} -target -output-file-map -incremental -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-library -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") else() - set(CMAKE_Swift_CREATE_SHARED_LIBRARY "${CMAKE_Swift_COMPILER} -output-file-map -incremental -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-library -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") + set(CMAKE_Swift_CREATE_SHARED_LIBRARY "${CMAKE_Swift_COMPILER} -output-file-map -incremental -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-library -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") endif() endif() -- cgit v0.12