From d0b469b7e03e0d902b3d8e2bf2f0b694438f0bf1 Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Sun, 1 Jan 2023 22:11:46 -0800 Subject: 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. --- Source/cmNinjaNormalTargetGenerator.cxx | 60 +++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/Source/cmNinjaNormalTargetGenerator.cxx b/Source/cmNinjaNormalTargetGenerator.cxx index d481b64..c427bde 100644 --- a/Source/cmNinjaNormalTargetGenerator.cxx +++ b/Source/cmNinjaNormalTargetGenerator.cxx @@ -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 sources; -- cgit v0.12