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:
@@ -184,9 +184,8 @@ void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
|
|||||||
Indent indent)
|
Indent indent)
|
||||||
{
|
{
|
||||||
// Remove old per-configuration export files if the main changes.
|
// Remove old per-configuration export files if the main changes.
|
||||||
std::string installedDir =
|
std::string installedDir = cmStrCat(
|
||||||
cmStrCat("$ENV{DESTDIR}",
|
"$ENV{DESTDIR}", ConvertToAbsoluteDestination(this->Destination), '/');
|
||||||
this->ConvertToAbsoluteDestination(this->Destination), '/');
|
|
||||||
std::string installedFile = cmStrCat(installedDir, this->FileName);
|
std::string installedFile = cmStrCat(installedDir, this->FileName);
|
||||||
os << indent << "if(EXISTS \"" << installedFile << "\")\n";
|
os << indent << "if(EXISTS \"" << installedFile << "\")\n";
|
||||||
Indent indentN = indent.Next();
|
Indent indentN = indent.Next();
|
||||||
|
@@ -2,10 +2,11 @@
|
|||||||
file Copyright.txt or https://cmake.org/licensing for details. */
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
||||||
#include "cmInstallGenerator.h"
|
#include "cmInstallGenerator.h"
|
||||||
|
|
||||||
#include <ostream>
|
#include <sstream>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "cmMakefile.h"
|
#include "cmMakefile.h"
|
||||||
|
#include "cmStringAlgorithms.h"
|
||||||
#include "cmSystemTools.h"
|
#include "cmSystemTools.h"
|
||||||
|
|
||||||
cmInstallGenerator::cmInstallGenerator(
|
cmInstallGenerator::cmInstallGenerator(
|
||||||
@@ -98,7 +99,7 @@ void cmInstallGenerator::AddInstallRule(
|
|||||||
<< "${CMAKE_ABSOLUTE_DESTINATION_FILES}\")\n";
|
<< "${CMAKE_ABSOLUTE_DESTINATION_FILES}\")\n";
|
||||||
os << indent << "endif()\n";
|
os << indent << "endif()\n";
|
||||||
}
|
}
|
||||||
std::string absDest = this->ConvertToAbsoluteDestination(dest);
|
std::string absDest = ConvertToAbsoluteDestination(dest);
|
||||||
os << "file(INSTALL DESTINATION \"" << absDest << "\" TYPE " << stype;
|
os << "file(INSTALL DESTINATION \"" << absDest << "\" TYPE " << stype;
|
||||||
if (optional) {
|
if (optional) {
|
||||||
os << " OPTIONAL";
|
os << " OPTIONAL";
|
||||||
@@ -183,7 +184,7 @@ bool cmInstallGenerator::InstallsForConfig(const std::string& config)
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string cmInstallGenerator::ConvertToAbsoluteDestination(
|
std::string cmInstallGenerator::ConvertToAbsoluteDestination(
|
||||||
std::string const& dest) const
|
std::string const& dest)
|
||||||
{
|
{
|
||||||
std::string result;
|
std::string result;
|
||||||
if (!dest.empty() && !cmSystemTools::FileIsFullPath(dest)) {
|
if (!dest.empty() && !cmSystemTools::FileIsFullPath(dest)) {
|
||||||
@@ -211,3 +212,59 @@ cmInstallGenerator::MessageLevel cmInstallGenerator::SelectMessageLevel(
|
|||||||
}
|
}
|
||||||
return MessageDefault;
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "cmConfigure.h" // IWYU pragma: keep
|
#include "cmConfigure.h" // IWYU pragma: keep
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -53,7 +54,7 @@ public:
|
|||||||
|
|
||||||
/** Get the install destination as it should appear in the
|
/** Get the install destination as it should appear in the
|
||||||
installation script. */
|
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. */
|
/** Test if this generator installs something for a given configuration. */
|
||||||
bool InstallsForConfig(const std::string& config);
|
bool InstallsForConfig(const std::string& config);
|
||||||
@@ -70,12 +71,25 @@ public:
|
|||||||
|
|
||||||
cmListFileBacktrace const& GetBacktrace() const { return this->Backtrace; }
|
cmListFileBacktrace const& GetBacktrace() const { return this->Backtrace; }
|
||||||
|
|
||||||
|
static std::string GetDestDirPath(std::string const& file);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void GenerateScript(std::ostream& os) override;
|
void GenerateScript(std::ostream& os) override;
|
||||||
|
|
||||||
std::string CreateComponentTest(const std::string& component,
|
std::string CreateComponentTest(const std::string& component,
|
||||||
bool exclude_from_all);
|
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.
|
// Information shared by most generator types.
|
||||||
std::string const Destination;
|
std::string const Destination;
|
||||||
std::string const Component;
|
std::string const Component;
|
||||||
|
@@ -77,12 +77,15 @@ void cmInstallTargetGenerator::GenerateScriptForConfig(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Tweak files located in the destination directory.
|
// Tweak files located in the destination directory.
|
||||||
std::string toDir = cmStrCat(this->ConvertToAbsoluteDestination(dest), '/');
|
std::string toDir = cmStrCat(ConvertToAbsoluteDestination(dest), '/');
|
||||||
|
|
||||||
// Add pre-installation tweaks.
|
// Add pre-installation tweaks.
|
||||||
if (!files.NoTweak) {
|
if (!files.NoTweak) {
|
||||||
this->AddTweak(os, indent, config, toDir, files.To,
|
AddTweak(os, indent, config, toDir, files.To,
|
||||||
&cmInstallTargetGenerator::PreReplacementTweaks);
|
[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.
|
// Write code to install the target file.
|
||||||
@@ -102,8 +105,11 @@ void cmInstallTargetGenerator::GenerateScriptForConfig(
|
|||||||
|
|
||||||
// Add post-installation tweaks.
|
// Add post-installation tweaks.
|
||||||
if (!files.NoTweak) {
|
if (!files.NoTweak) {
|
||||||
this->AddTweak(os, indent, config, toDir, files.To,
|
AddTweak(os, indent, config, toDir, files.To,
|
||||||
&cmInstallTargetGenerator::PostReplacementTweaks);
|
[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;
|
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,
|
void cmInstallTargetGenerator::PreReplacementTweaks(std::ostream& os,
|
||||||
Indent indent,
|
Indent indent,
|
||||||
const std::string& config,
|
const std::string& config,
|
||||||
|
@@ -93,15 +93,6 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
void GenerateScriptForConfig(std::ostream& os, const std::string& config,
|
void GenerateScriptForConfig(std::ostream& os, const std::string& config,
|
||||||
Indent indent) override;
|
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,
|
void PreReplacementTweaks(std::ostream& os, Indent indent,
|
||||||
const std::string& config,
|
const std::string& config,
|
||||||
std::string const& file);
|
std::string const& file);
|
||||||
|
Reference in New Issue
Block a user