From 60e752ced8e9b604591082ee3b21d545fafa8ad8 Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Tue, 25 May 2021 16:18:49 -0400 Subject: Refactor: Move common methods into cmInstallGenerator --- Source/cmInstallExportGenerator.cxx | 5 +-- Source/cmInstallGenerator.cxx | 63 ++++++++++++++++++++++++++++++-- Source/cmInstallGenerator.h | 16 +++++++- Source/cmInstallTargetGenerator.cxx | 73 ++++++------------------------------- Source/cmInstallTargetGenerator.h | 9 ----- 5 files changed, 88 insertions(+), 78 deletions(-) diff --git a/Source/cmInstallExportGenerator.cxx b/Source/cmInstallExportGenerator.cxx index ccefd92..d932fd9 100644 --- a/Source/cmInstallExportGenerator.cxx +++ b/Source/cmInstallExportGenerator.cxx @@ -184,9 +184,8 @@ void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os, Indent indent) { // Remove old per-configuration export files if the main changes. - std::string installedDir = - cmStrCat("$ENV{DESTDIR}", - this->ConvertToAbsoluteDestination(this->Destination), '/'); + std::string installedDir = cmStrCat( + "$ENV{DESTDIR}", ConvertToAbsoluteDestination(this->Destination), '/'); std::string installedFile = cmStrCat(installedDir, this->FileName); os << indent << "if(EXISTS \"" << installedFile << "\")\n"; Indent indentN = indent.Next(); diff --git a/Source/cmInstallGenerator.cxx b/Source/cmInstallGenerator.cxx index cf5f45e..9f0d119 100644 --- a/Source/cmInstallGenerator.cxx +++ b/Source/cmInstallGenerator.cxx @@ -2,10 +2,11 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmInstallGenerator.h" -#include +#include #include #include "cmMakefile.h" +#include "cmStringAlgorithms.h" #include "cmSystemTools.h" cmInstallGenerator::cmInstallGenerator( @@ -98,7 +99,7 @@ void cmInstallGenerator::AddInstallRule( << "${CMAKE_ABSOLUTE_DESTINATION_FILES}\")\n"; os << indent << "endif()\n"; } - std::string absDest = this->ConvertToAbsoluteDestination(dest); + std::string absDest = ConvertToAbsoluteDestination(dest); os << "file(INSTALL DESTINATION \"" << absDest << "\" TYPE " << stype; if (optional) { os << " OPTIONAL"; @@ -183,7 +184,7 @@ bool cmInstallGenerator::InstallsForConfig(const std::string& config) } std::string cmInstallGenerator::ConvertToAbsoluteDestination( - std::string const& dest) const + std::string const& dest) { std::string result; if (!dest.empty() && !cmSystemTools::FileIsFullPath(dest)) { @@ -211,3 +212,59 @@ cmInstallGenerator::MessageLevel cmInstallGenerator::SelectMessageLevel( } return MessageDefault; } + +std::string cmInstallGenerator::GetDestDirPath(std::string const& file) +{ + // Construct the path of the file on disk after installation on + // which tweaks may be performed. + std::string toDestDirPath = "$ENV{DESTDIR}"; + if (file[0] != '/' && file[0] != '$') { + toDestDirPath += "/"; + } + toDestDirPath += file; + return toDestDirPath; +} + +void cmInstallGenerator::AddTweak(std::ostream& os, Indent indent, + const std::string& config, + std::string const& file, + const TweakMethod& tweak) +{ + std::ostringstream tw; + tweak(tw, indent.Next(), config, file); + std::string tws = tw.str(); + if (!tws.empty()) { + os << indent << "if(EXISTS \"" << file << "\" AND\n" + << indent << " NOT IS_SYMLINK \"" << file << "\")\n"; + os << tws; + os << indent << "endif()\n"; + } +} + +void cmInstallGenerator::AddTweak(std::ostream& os, Indent indent, + const std::string& config, + std::string const& dir, + std::vector const& files, + const TweakMethod& tweak) +{ + if (files.size() == 1) { + // Tweak a single file. + AddTweak(os, indent, config, GetDestDirPath(cmStrCat(dir, files[0])), + tweak); + } else { + // Generate a foreach loop to tweak multiple files. + std::ostringstream tw; + AddTweak(tw, indent.Next(), config, "${file}", tweak); + std::string tws = tw.str(); + if (!tws.empty()) { + Indent indent2 = indent.Next().Next(); + os << indent << "foreach(file\n"; + for (std::string const& f : files) { + os << indent2 << "\"" << GetDestDirPath(cmStrCat(dir, f)) << "\"\n"; + } + os << indent2 << ")\n"; + os << tws; + os << indent << "endforeach()\n"; + } + } +} diff --git a/Source/cmInstallGenerator.h b/Source/cmInstallGenerator.h index 0117617..97acb88 100644 --- a/Source/cmInstallGenerator.h +++ b/Source/cmInstallGenerator.h @@ -4,6 +4,7 @@ #include "cmConfigure.h" // IWYU pragma: keep +#include #include #include #include @@ -53,7 +54,7 @@ public: /** Get the install destination as it should appear in the installation script. */ - std::string ConvertToAbsoluteDestination(std::string const& dest) const; + static std::string ConvertToAbsoluteDestination(std::string const& dest); /** Test if this generator installs something for a given configuration. */ bool InstallsForConfig(const std::string& config); @@ -70,12 +71,25 @@ public: cmListFileBacktrace const& GetBacktrace() const { return this->Backtrace; } + static std::string GetDestDirPath(std::string const& file); + protected: void GenerateScript(std::ostream& os) override; std::string CreateComponentTest(const std::string& component, bool exclude_from_all); + using TweakMethod = + std::function; + static void AddTweak(std::ostream& os, Indent indent, + const std::string& config, std::string const& file, + const TweakMethod& tweak); + static void AddTweak(std::ostream& os, Indent indent, + const std::string& config, std::string const& dir, + std::vector const& files, + const TweakMethod& tweak); + // Information shared by most generator types. std::string const Destination; std::string const Component; diff --git a/Source/cmInstallTargetGenerator.cxx b/Source/cmInstallTargetGenerator.cxx index 3e79ad8..35165cf 100644 --- a/Source/cmInstallTargetGenerator.cxx +++ b/Source/cmInstallTargetGenerator.cxx @@ -77,12 +77,15 @@ void cmInstallTargetGenerator::GenerateScriptForConfig( } // Tweak files located in the destination directory. - std::string toDir = cmStrCat(this->ConvertToAbsoluteDestination(dest), '/'); + std::string toDir = cmStrCat(ConvertToAbsoluteDestination(dest), '/'); // Add pre-installation tweaks. if (!files.NoTweak) { - this->AddTweak(os, indent, config, toDir, files.To, - &cmInstallTargetGenerator::PreReplacementTweaks); + AddTweak(os, indent, config, toDir, files.To, + [this](std::ostream& o, Indent i, const std::string& c, + const std::string& f) { + this->PreReplacementTweaks(o, i, c, f); + }); } // Write code to install the target file. @@ -102,8 +105,11 @@ void cmInstallTargetGenerator::GenerateScriptForConfig( // Add post-installation tweaks. if (!files.NoTweak) { - this->AddTweak(os, indent, config, toDir, files.To, - &cmInstallTargetGenerator::PostReplacementTweaks); + AddTweak(os, indent, config, toDir, files.To, + [this](std::ostream& o, Indent i, const std::string& c, + const std::string& f) { + this->PostReplacementTweaks(o, i, c, f); + }); } } @@ -461,63 +467,6 @@ bool cmInstallTargetGenerator::Compute(cmLocalGenerator* lg) return true; } -void cmInstallTargetGenerator::AddTweak(std::ostream& os, Indent indent, - const std::string& config, - std::string const& file, - TweakMethod tweak) -{ - std::ostringstream tw; - (this->*tweak)(tw, indent.Next(), config, file); - std::string tws = tw.str(); - if (!tws.empty()) { - os << indent << "if(EXISTS \"" << file << "\" AND\n" - << indent << " NOT IS_SYMLINK \"" << file << "\")\n"; - os << tws; - os << indent << "endif()\n"; - } -} - -void cmInstallTargetGenerator::AddTweak(std::ostream& os, Indent indent, - const std::string& config, - std::string const& dir, - std::vector const& files, - TweakMethod tweak) -{ - if (files.size() == 1) { - // Tweak a single file. - this->AddTweak(os, indent, config, - this->GetDestDirPath(cmStrCat(dir, files[0])), tweak); - } else { - // Generate a foreach loop to tweak multiple files. - std::ostringstream tw; - this->AddTweak(tw, indent.Next(), config, "${file}", tweak); - std::string tws = tw.str(); - if (!tws.empty()) { - Indent indent2 = indent.Next().Next(); - os << indent << "foreach(file\n"; - for (std::string const& f : files) { - os << indent2 << "\"" << this->GetDestDirPath(cmStrCat(dir, f)) - << "\"\n"; - } - os << indent2 << ")\n"; - os << tws; - os << indent << "endforeach()\n"; - } - } -} - -std::string cmInstallTargetGenerator::GetDestDirPath(std::string const& file) -{ - // Construct the path of the file on disk after installation on - // which tweaks may be performed. - std::string toDestDirPath = "$ENV{DESTDIR}"; - if (file[0] != '/' && file[0] != '$') { - toDestDirPath += "/"; - } - toDestDirPath += file; - return toDestDirPath; -} - void cmInstallTargetGenerator::PreReplacementTweaks(std::ostream& os, Indent indent, const std::string& config, diff --git a/Source/cmInstallTargetGenerator.h b/Source/cmInstallTargetGenerator.h index 84fce42..6173f2c 100644 --- a/Source/cmInstallTargetGenerator.h +++ b/Source/cmInstallTargetGenerator.h @@ -93,15 +93,6 @@ public: protected: void GenerateScriptForConfig(std::ostream& os, const std::string& config, Indent indent) override; - using TweakMethod = void (cmInstallTargetGenerator::*)(std::ostream&, Indent, - const std::string&, - const std::string&); - void AddTweak(std::ostream& os, Indent indent, const std::string& config, - std::string const& file, TweakMethod tweak); - void AddTweak(std::ostream& os, Indent indent, const std::string& config, - std::string const& dir, std::vector const& files, - TweakMethod tweak); - std::string GetDestDirPath(std::string const& file); void PreReplacementTweaks(std::ostream& os, Indent indent, const std::string& config, std::string const& file); -- cgit v0.12