Ninja: NFC: refactor swift module name computations

In order to handle determining the swiftmodule name to add to the ninja
dependency graph, we'll need to be able to compute the swiftmodule name
for the dependency target, not just the current target. This patch
refactors the computation of the module name out of inaccessible lambdas
and into static functions that can compute the swiftmodule name from the
generator and the target.
This commit is contained in:
Evan Wilde
2023-01-01 22:11:46 -08:00
parent 06c7e88b91
commit d0b469b7e0
+35 -25
View File
@@ -940,6 +940,37 @@ 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
/// The returned path will need to be converted to the generator path
static std::string GetSwiftModulePath(cmGeneratorTarget const* target)
{
std::string moduleName = GetSwiftModuleName(target);
std::string moduleDirectory = GetTargetPropertyOrDefault(
target, "Swift_MODULE_DIRECTORY",
target->LocalGenerator->GetCurrentBinaryDirectory());
std::string moduleFileName = GetTargetPropertyOrDefault(
target, "Swift_MODULE", moduleName + ".swiftmodule");
return moduleDirectory + "/" + moduleFileName;
}
void cmNinjaNormalTargetGenerator::WriteLinkStatement(
const std::string& config, const std::string& fileConfig,
bool firstForConfig)
@@ -1038,31 +1069,10 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement(
return targetNames.Base;
}();
vars["SWIFT_MODULE_NAME"] = [gt]() -> std::string {
if (cmValue name = gt->GetProperty("Swift_MODULE_NAME")) {
return *name;
}
return gt->GetName();
}();
vars["SWIFT_MODULE"] = [this](const std::string& module) -> std::string {
std::string directory =
this->GetLocalGenerator()->GetCurrentBinaryDirectory();
if (cmValue prop = this->GetGeneratorTarget()->GetProperty(
"Swift_MODULE_DIRECTORY")) {
directory = *prop;
}
std::string name = module + ".swiftmodule";
if (cmValue prop =
this->GetGeneratorTarget()->GetProperty("Swift_MODULE")) {
name = *prop;
}
return this->GetLocalGenerator()->ConvertToOutputFormat(
this->ConvertToNinjaPath(directory + "/" + name),
cmOutputConverter::SHELL);
}(vars["SWIFT_MODULE_NAME"]);
vars["SWIFT_MODULE_NAME"] = GetSwiftModuleName(gt);
vars["SWIFT_MODULE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
this->ConvertToNinjaPath(GetSwiftModulePath(gt)),
cmOutputConverter::SHELL);
vars["SWIFT_SOURCES"] = [this, config]() -> std::string {
std::vector<cmSourceFile const*> sources;