cmGeneratorTarget: Adopt Swift-related methods from the Ninja generator

They will see more use as Swift bugs are fixed in the
Ninja Multi-Config generator.
This commit is contained in:
Dave Abrahams
2024-05-22 17:44:19 -07:00
parent 45b3387c50
commit b2e042d77a
4 changed files with 83 additions and 48 deletions

View File

@@ -5501,3 +5501,45 @@ void cmGeneratorTarget::BuildFileSetInfoCache(std::string const& config) const
per_config.BuiltFileSetCache = true;
}
std::string cmGeneratorTarget::GetSwiftModuleName() const
{
return this->GetPropertyOrDefault("Swift_MODULE_NAME", this->GetName());
}
std::string cmGeneratorTarget::GetSwiftModuleFileName() const
{
return this->GetPropertyOrDefault(
"Swift_MODULE", this->GetSwiftModuleName() + ".swiftmodule");
}
std::string cmGeneratorTarget::GetSwiftModuleDirectory(
std::string const& config) const
{
std::string moduleDirectory =
this->GetPropertyOrDefault("Swift_MODULE_DIRECTORY", "");
if (moduleDirectory.empty()) {
moduleDirectory = this->LocalGenerator->GetCurrentBinaryDirectory();
this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
"/", config, "", moduleDirectory);
}
return moduleDirectory;
}
std::string cmGeneratorTarget::GetSwiftModulePath(
std::string const& config) const
{
return this->GetSwiftModuleDirectory(config) + "/" +
this->GetSwiftModuleFileName();
}
std::string cmGeneratorTarget::GetPropertyOrDefault(
std::string const& property, std::string defaultValue) const
{
if (cmValue name = this->GetProperty(property)) {
return *name;
}
return defaultValue;
}

View File

@@ -537,7 +537,27 @@ public:
std::string GetClangTidyExportFixesDirectory(const std::string& lang) const;
/** Return the swift module name for this target. */
std::string GetSwiftModuleName() const;
/** Return the path of the `.swiftmodule` for this target in
the given configuration. */
std::string GetSwiftModulePath(std::string const& config) const;
/** Return the given property of this target if it exists; otherwise
return defaultValue. */
std::string GetPropertyOrDefault(std::string const& property,
std::string defaultValue) const;
/** 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>>>;

View File

@@ -1078,34 +1078,16 @@ void cmNinjaNormalTargetGenerator::WriteNvidiaDeviceLinkStatement(
this->WriteNvidiaDeviceLinkRule(usedResponseFile, config);
}
/// Get the target property if it exists, or return a default
static std::string GetTargetPropertyOrDefault(cmGeneratorTarget const* target,
std::string const& property,
std::string defaultValue)
{
if (cmValue name = target->GetProperty(property)) {
return *name;
}
return defaultValue;
}
/// Compute the swift module name for target
static std::string GetSwiftModuleName(cmGeneratorTarget const* target)
{
return GetTargetPropertyOrDefault(target, "Swift_MODULE_NAME",
target->GetName());
}
/// Compute the swift module path for the target
/// 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 GetSwiftModulePath(cmGeneratorTarget const* target)
static std::string GetSwiftModulePathTree(cmGeneratorTarget const* target)
{
std::string moduleName = GetSwiftModuleName(target);
std::string moduleDirectory = GetTargetPropertyOrDefault(
target, "Swift_MODULE_DIRECTORY",
std::string moduleName = target->GetSwiftModuleName();
std::string moduleDirectory = target->GetPropertyOrDefault(
"Swift_MODULE_DIRECTORY",
target->LocalGenerator->GetCurrentBinaryDirectory());
std::string moduleFileName = GetTargetPropertyOrDefault(
target, "Swift_MODULE", moduleName + ".swiftmodule");
std::string moduleFileName = target->GetSwiftModuleFileName();
return moduleDirectory + "/" + moduleFileName;
}
@@ -1221,9 +1203,9 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement(
return targetNames.Base;
}();
vars["SWIFT_MODULE_NAME"] = GetSwiftModuleName(gt);
vars["SWIFT_MODULE_NAME"] = gt->GetSwiftModuleName();
vars["SWIFT_MODULE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
this->ConvertToNinjaPath(GetSwiftModulePath(gt)),
this->ConvertToNinjaPath(GetSwiftModulePathTree(gt)),
cmOutputConverter::SHELL);
vars["SWIFT_SOURCES"] = [this, config]() -> std::string {
@@ -1555,8 +1537,8 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement(
// in order for there to be a swiftmodule to depend on
if (dependency.Target &&
dependency.Target->GetLinkerLanguage(config) == "Swift") {
std::string swiftmodule =
this->ConvertToNinjaPath(GetSwiftModulePath(dependency.Target));
std::string swiftmodule = this->ConvertToNinjaPath(
GetSwiftModulePathTree(dependency.Target));
linkBuild.ImplicitDeps.emplace_back(swiftmodule);
}
}

View File

@@ -1947,15 +1947,6 @@ void cmNinjaTargetGenerator::WriteSwiftObjectBuildStatement(
return;
}
auto getTargetPropertyOrDefault =
[](cmGeneratorTarget const& target, std::string const& property,
std::string defaultValue) -> std::string {
if (cmValue value = target.GetProperty(property)) {
return *value;
}
return defaultValue;
};
std::string const language = "Swift";
std::string const objectDir = this->ConvertToNinjaPath(
cmStrCat(this->GeneratorTarget->GetSupportDirectory(),
@@ -1971,12 +1962,12 @@ void cmNinjaTargetGenerator::WriteSwiftObjectBuildStatement(
vars.emplace("restat", "1");
std::string const moduleName =
getTargetPropertyOrDefault(target, "Swift_MODULE_NAME", target.GetName());
std::string const moduleDirectory = getTargetPropertyOrDefault(
target, "Swift_MODULE_DIRECTORY",
target.GetPropertyOrDefault("Swift_MODULE_NAME", target.GetName());
std::string const moduleDirectory = target.GetPropertyOrDefault(
"Swift_MODULE_DIRECTORY",
target.LocalGenerator->GetCurrentBinaryDirectory());
std::string const moduleFilename = getTargetPropertyOrDefault(
target, "Swift_MODULE", cmStrCat(moduleName, ".swiftmodule"));
std::string const moduleFilename = target.GetPropertyOrDefault(
"Swift_MODULE", cmStrCat(moduleName, ".swiftmodule"));
std::string const moduleFilepath =
this->ConvertToNinjaPath(cmStrCat(moduleDirectory, '/', moduleFilename));
@@ -2097,12 +2088,12 @@ void cmNinjaTargetGenerator::WriteSwiftObjectBuildStatement(
// swiftmodule to the ninja build graph.
if (isImportableTarget(*dep)) {
std::string const depModuleName =
getTargetPropertyOrDefault(*dep, "Swift_MODULE_NAME", dep->GetName());
std::string const depModuleDir = getTargetPropertyOrDefault(
*dep, "Swift_MODULE_DIRECTORY",
dep->GetPropertyOrDefault("Swift_MODULE_NAME", dep->GetName());
std::string const depModuleDir = dep->GetPropertyOrDefault(
"Swift_MODULE_DIRECTORY",
dep->LocalGenerator->GetCurrentBinaryDirectory());
std::string const depModuleFilename = getTargetPropertyOrDefault(
*dep, "Swift_MODULE", cmStrCat(depModuleName, ".swiftmodule"));
std::string const depModuleFilename = dep->GetPropertyOrDefault(
"Swift_MODULE", cmStrCat(depModuleName, ".swiftmodule"));
std::string const depModuleFilepath = this->ConvertToNinjaPath(
cmStrCat(depModuleDir, '/', depModuleFilename));
objBuild.ImplicitDeps.push_back(depModuleFilepath);