1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 02:08:27 +08:00

Refactor: Move common methods into cmInstallGenerator

This commit is contained in:
Kyle Edwards
2021-05-25 16:18:49 -04:00
parent dd6fd62615
commit 60e752ced8
5 changed files with 88 additions and 78 deletions

View File

@@ -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();

View File

@@ -2,10 +2,11 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmInstallGenerator.h"
#include <ostream>
#include <sstream>
#include <utility>
#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<std::string> 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";
}
}
}

View File

@@ -4,6 +4,7 @@
#include "cmConfigure.h" // IWYU pragma: keep
#include <functional>
#include <iosfwd>
#include <string>
#include <vector>
@@ -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<void(std::ostream& os, Indent indent,
const std::string& config, const std::string& file)>;
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<std::string> const& files,
const TweakMethod& tweak);
// Information shared by most generator types.
std::string const Destination;
std::string const Component;

View File

@@ -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<std::string> 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,

View File

@@ -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<std::string> 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);