summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Abrahams <dabrahams@adobe.com>2024-05-23 00:55:30 (GMT)
committerDave Abrahams <dabrahams@adobe.com>2024-05-24 22:23:41 (GMT)
commit5bb7f8a4dd4671f9343461e5377759b8a4947a4e (patch)
tree92fc45ac7b467907a0845db25fa4635681b8385c
parentb2e042d77a0bfe21712a2f802668d04df2518bdc (diff)
downloadCMake-5bb7f8a4dd4671f9343461e5377759b8a4947a4e.zip
CMake-5bb7f8a4dd4671f9343461e5377759b8a4947a4e.tar.gz
CMake-5bb7f8a4dd4671f9343461e5377759b8a4947a4e.tar.bz2
Swift: Use per-config module file locations in multi-config generators
Place `.swiftmodule` files a subdirectory named after the configuration. Fixes: #25864 Fixes: #25997 - Swift/RunCMakeTest.cmake: - CMP0157-OLD was enabled for Xcode, where it works. - A test was added that verifies .swiftmodule's are generated into separate directories with multi-config generators. - Tests/SwiftOnly/CMakeLists.txt: tests were added that validate that cross-subdirectory module dependencies (via target_link_libraries) work.
-rw-r--r--Source/cmGeneratorTarget.h12
-rw-r--r--Source/cmGeneratorTarget_IncludeDirectories.cxx5
-rw-r--r--Source/cmNinjaNormalTargetGenerator.cxx17
-rw-r--r--Source/cmNinjaTargetGenerator.cxx21
-rw-r--r--Tests/RunCMake/Swift/RunCMakeTest.cmake22
-rw-r--r--Tests/SwiftOnly/CMakeLists.txt4
-rw-r--r--Tests/SwiftOnly/SubC/CMakeLists.txt2
-rw-r--r--Tests/SwiftOnly/SubC/SubC.swift1
-rw-r--r--Tests/SwiftOnly/SubD/CMakeLists.txt1
-rw-r--r--Tests/SwiftOnly/SubD/SubD.swift1
-rw-r--r--Tests/SwiftOnly/SubE/CMakeLists.txt2
-rw-r--r--Tests/SwiftOnly/SubE/main.swift1
12 files changed, 46 insertions, 43 deletions
diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h
index 1193481..4521fd4 100644
--- a/Source/cmGeneratorTarget.h
+++ b/Source/cmGeneratorTarget.h
@@ -544,6 +544,12 @@ public:
the given configuration. */
std::string GetSwiftModulePath(std::string const& config) const;
+ /** Return the directory containing Swift module interface
+ descriptions for this target (including its `.swiftmodule`,
+ `.abi.json`, and `.swiftdoc`) in the given configuration. */
+ std::string GetSwiftModuleDirectory(std::string const& config) const;
+
+private:
/** Return the given property of this target if it exists; otherwise
return defaultValue. */
std::string GetPropertyOrDefault(std::string const& property,
@@ -552,12 +558,6 @@ public:
/** Return the name of the `.swiftmodule` file for this target. */
std::string GetSwiftModuleFileName() const;
-private:
- /** Return the directory containing Swift module interface
- descriptions for this target (including its `.swiftmodule`,
- `.abi.json`, and `.swiftdoc`) in the given configuration. */
- std::string GetSwiftModuleDirectory(std::string const& config) const;
-
using ConfigAndLanguage = std::pair<std::string, std::string>;
using ConfigAndLanguageToBTStrings =
std::map<ConfigAndLanguage, std::vector<BT<std::string>>>;
diff --git a/Source/cmGeneratorTarget_IncludeDirectories.cxx b/Source/cmGeneratorTarget_IncludeDirectories.cxx
index 2b45e1c..6db467f 100644
--- a/Source/cmGeneratorTarget_IncludeDirectories.cxx
+++ b/Source/cmGeneratorTarget_IncludeDirectories.cxx
@@ -114,7 +114,10 @@ void AddLangSpecificImplicitIncludeDirectories(
auto* lg = dependency->GetLocalGenerator();
EvaluatedTargetPropertyEntry entry{ library, library.Backtrace };
- if (cmValue val = dependency->GetProperty(propertyName)) {
+ if (lang == "Swift") {
+ entry.Values.emplace_back(
+ dependency->GetSwiftModuleDirectory(config));
+ } else if (cmValue val = dependency->GetProperty(propertyName)) {
entry.Values.emplace_back(*val);
} else {
if (mode == IncludeDirectoryFallBack::BINARY) {
diff --git a/Source/cmNinjaNormalTargetGenerator.cxx b/Source/cmNinjaNormalTargetGenerator.cxx
index fefe3a1..77c3e6a 100644
--- a/Source/cmNinjaNormalTargetGenerator.cxx
+++ b/Source/cmNinjaNormalTargetGenerator.cxx
@@ -1078,19 +1078,6 @@ void cmNinjaNormalTargetGenerator::WriteNvidiaDeviceLinkStatement(
this->WriteNvidiaDeviceLinkRule(usedResponseFile, config);
}
-/// Compute the swift module path for the target, sans any config-specific
-/// subdirectory.
-/// The returned path will need to be converted to the generator path
-static std::string GetSwiftModulePathTree(cmGeneratorTarget const* target)
-{
- std::string moduleName = target->GetSwiftModuleName();
- std::string moduleDirectory = target->GetPropertyOrDefault(
- "Swift_MODULE_DIRECTORY",
- target->LocalGenerator->GetCurrentBinaryDirectory());
- std::string moduleFileName = target->GetSwiftModuleFileName();
- return moduleDirectory + "/" + moduleFileName;
-}
-
void cmNinjaNormalTargetGenerator::WriteLinkStatement(
const std::string& config, const std::string& fileConfig,
bool firstForConfig)
@@ -1205,7 +1192,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement(
vars["SWIFT_MODULE_NAME"] = gt->GetSwiftModuleName();
vars["SWIFT_MODULE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
- this->ConvertToNinjaPath(GetSwiftModulePathTree(gt)),
+ this->ConvertToNinjaPath(gt->GetSwiftModulePath(config)),
cmOutputConverter::SHELL);
vars["SWIFT_SOURCES"] = [this, config]() -> std::string {
@@ -1538,7 +1525,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement(
if (dependency.Target &&
dependency.Target->GetLinkerLanguage(config) == "Swift") {
std::string swiftmodule = this->ConvertToNinjaPath(
- GetSwiftModulePathTree(dependency.Target));
+ dependency.Target->GetSwiftModulePath(config));
linkBuild.ImplicitDeps.emplace_back(swiftmodule);
}
}
diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx
index a4e08fb..160689c 100644
--- a/Source/cmNinjaTargetGenerator.cxx
+++ b/Source/cmNinjaTargetGenerator.cxx
@@ -1961,15 +1961,9 @@ void cmNinjaTargetGenerator::WriteSwiftObjectBuildStatement(
// changes to input files (e.g. addition of a comment).
vars.emplace("restat", "1");
- std::string const moduleName =
- target.GetPropertyOrDefault("Swift_MODULE_NAME", target.GetName());
- std::string const moduleDirectory = target.GetPropertyOrDefault(
- "Swift_MODULE_DIRECTORY",
- target.LocalGenerator->GetCurrentBinaryDirectory());
- std::string const moduleFilename = target.GetPropertyOrDefault(
- "Swift_MODULE", cmStrCat(moduleName, ".swiftmodule"));
+ std::string const moduleName = target.GetSwiftModuleName();
std::string const moduleFilepath =
- this->ConvertToNinjaPath(cmStrCat(moduleDirectory, '/', moduleFilename));
+ this->ConvertToNinjaPath(target.GetSwiftModulePath(config));
vars.emplace("description",
cmStrCat("Building Swift Module '", moduleName, "' with ",
@@ -2087,15 +2081,8 @@ void cmNinjaTargetGenerator::WriteSwiftObjectBuildStatement(
// If the dependency emits a swiftmodule, add a dependency edge on that
// swiftmodule to the ninja build graph.
if (isImportableTarget(*dep)) {
- std::string const depModuleName =
- dep->GetPropertyOrDefault("Swift_MODULE_NAME", dep->GetName());
- std::string const depModuleDir = dep->GetPropertyOrDefault(
- "Swift_MODULE_DIRECTORY",
- dep->LocalGenerator->GetCurrentBinaryDirectory());
- std::string const depModuleFilename = dep->GetPropertyOrDefault(
- "Swift_MODULE", cmStrCat(depModuleName, ".swiftmodule"));
- std::string const depModuleFilepath = this->ConvertToNinjaPath(
- cmStrCat(depModuleDir, '/', depModuleFilename));
+ std::string const depModuleFilepath =
+ this->ConvertToNinjaPath(dep->GetSwiftModulePath(config));
objBuild.ImplicitDeps.push_back(depModuleFilepath);
}
}
diff --git a/Tests/RunCMake/Swift/RunCMakeTest.cmake b/Tests/RunCMake/Swift/RunCMakeTest.cmake
index 39f754c..716075e 100644
--- a/Tests/RunCMake/Swift/RunCMakeTest.cmake
+++ b/Tests/RunCMake/Swift/RunCMakeTest.cmake
@@ -19,9 +19,7 @@ block()
run_cmake(CMP0157-NEW)
run_cmake(CMP0157-WARN)
- if(RunCMake_GENERATOR MATCHES "Ninja.*")
- set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CMP0157-OLD-build)
- endif()
+ set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CMP0157-OLD-build)
run_cmake(CMP0157-OLD)
@@ -32,9 +30,25 @@ block()
endif()
endblock()
-if(RunCMake_GENERATOR MATCHES "Ninja")
+block()
+ set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/SwiftSimple-build)
run_cmake(SwiftSimple)
+ if(RunCMake_GENERATOR_IS_MULTI_CONFIG AND
+ # Older Xcode versions didn't support Swift static libraries.
+ NOT (RunCMake_GENERATOR STREQUAL "Xcode" AND XCODE_VERSION VERSION_LESS 9.0))
+ # Check that .swiftmodule files get their own directories
+ set(RunCMake_TEST_NO_CLEAN 1)
+ run_cmake_command(SwiftSimple-build-Debug ${CMAKE_COMMAND} --build . --config Debug)
+ run_cmake_command(SwiftSimple-build-Release ${CMAKE_COMMAND} --build . --config Release)
+ # Will fail if either path doesn't exist. Passing -r because Xcode
+ # generates .swiftmodule directories.
+ run_cmake_command(SwiftSimple-verify ${CMAKE_COMMAND} -E
+ rm -r Debug/L.swiftmodule Release/L.swiftmodule)
+ endif()
+endblock()
+
+if(RunCMake_GENERATOR MATCHES "Ninja")
block()
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
run_cmake_with_options(Win32ExecutableDisallowed)
diff --git a/Tests/SwiftOnly/CMakeLists.txt b/Tests/SwiftOnly/CMakeLists.txt
index 25892f1..e933ea1 100644
--- a/Tests/SwiftOnly/CMakeLists.txt
+++ b/Tests/SwiftOnly/CMakeLists.txt
@@ -28,6 +28,10 @@ endif()
add_subdirectory(SubA)
add_subdirectory(SubB)
+add_subdirectory(SubC)
+add_subdirectory(SubD)
+add_subdirectory(SubE)
+
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
add_executable(SwiftOnly main.swift)
diff --git a/Tests/SwiftOnly/SubC/CMakeLists.txt b/Tests/SwiftOnly/SubC/CMakeLists.txt
new file mode 100644
index 0000000..4fdb3d3
--- /dev/null
+++ b/Tests/SwiftOnly/SubC/CMakeLists.txt
@@ -0,0 +1,2 @@
+add_library(SubC SubC.swift)
+target_link_libraries(SubC PUBLIC SubD)
diff --git a/Tests/SwiftOnly/SubC/SubC.swift b/Tests/SwiftOnly/SubC/SubC.swift
new file mode 100644
index 0000000..1ca44f4
--- /dev/null
+++ b/Tests/SwiftOnly/SubC/SubC.swift
@@ -0,0 +1 @@
+import SubD
diff --git a/Tests/SwiftOnly/SubD/CMakeLists.txt b/Tests/SwiftOnly/SubD/CMakeLists.txt
new file mode 100644
index 0000000..01b38d3
--- /dev/null
+++ b/Tests/SwiftOnly/SubD/CMakeLists.txt
@@ -0,0 +1 @@
+add_library(SubD SubD.swift)
diff --git a/Tests/SwiftOnly/SubD/SubD.swift b/Tests/SwiftOnly/SubD/SubD.swift
new file mode 100644
index 0000000..857d3f4
--- /dev/null
+++ b/Tests/SwiftOnly/SubD/SubD.swift
@@ -0,0 +1 @@
+public let x = 42
diff --git a/Tests/SwiftOnly/SubE/CMakeLists.txt b/Tests/SwiftOnly/SubE/CMakeLists.txt
new file mode 100644
index 0000000..099a81c
--- /dev/null
+++ b/Tests/SwiftOnly/SubE/CMakeLists.txt
@@ -0,0 +1,2 @@
+add_executable(SubE main.swift)
+target_link_libraries(SubE PUBLIC SubD)
diff --git a/Tests/SwiftOnly/SubE/main.swift b/Tests/SwiftOnly/SubE/main.swift
new file mode 100644
index 0000000..1ca44f4
--- /dev/null
+++ b/Tests/SwiftOnly/SubE/main.swift
@@ -0,0 +1 @@
+import SubD