cmake/Source/cmInstallExportGenerator.cxx

224 lines
7.5 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmInstallExportGenerator.h"
2016-10-30 18:24:19 +01:00
#include <algorithm>
2019-11-11 23:01:05 +01:00
#include <map>
2016-10-30 18:24:19 +01:00
#include <sstream>
#include <utility>
2020-08-30 11:54:41 +02:00
#include <cm/memory>
2020-02-01 23:06:01 +01:00
#ifndef CMAKE_BOOTSTRAP
2018-08-09 18:06:22 +02:00
# include "cmExportInstallAndroidMKGenerator.h"
2016-10-30 18:24:19 +01:00
#endif
#include "cmExportInstallFileGenerator.h"
2013-03-16 19:13:01 +02:00
#include "cmExportSet.h"
2016-10-30 18:24:19 +01:00
#include "cmInstallType.h"
#include "cmLocalGenerator.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h"
cmInstallExportGenerator::cmInstallExportGenerator(
2020-08-30 11:54:41 +02:00
cmExportSet* exportSet, std::string const& destination,
std::string file_permissions, std::vector<std::string> const& configurations,
std::string const& component, MessageLevel message, bool exclude_from_all,
2021-09-14 00:13:48 +02:00
std::string filename, std::string name_space, bool exportOld, bool android,
cmListFileBacktrace backtrace)
2016-07-09 11:21:54 +02:00
: cmInstallGenerator(destination, configurations, component, message,
2021-09-14 00:13:48 +02:00
exclude_from_all, false, std::move(backtrace))
2016-07-09 11:21:54 +02:00
, ExportSet(exportSet)
2020-08-30 11:54:41 +02:00
, FilePermissions(std::move(file_permissions))
, FileName(std::move(filename))
, Namespace(std::move(name_space))
2016-07-09 11:21:54 +02:00
, ExportOld(exportOld)
2018-01-26 17:06:56 +01:00
, LocalGenerator(nullptr)
{
2016-10-30 18:24:19 +01:00
if (android) {
2020-02-01 23:06:01 +01:00
#ifndef CMAKE_BOOTSTRAP
2020-08-30 11:54:41 +02:00
this->EFGen = cm::make_unique<cmExportInstallAndroidMKGenerator>(this);
2016-10-30 18:24:19 +01:00
#endif
} else {
2020-08-30 11:54:41 +02:00
this->EFGen = cm::make_unique<cmExportInstallFileGenerator>(this);
2016-10-30 18:24:19 +01:00
}
2013-03-16 19:13:01 +02:00
exportSet->AddInstallation(this);
}
2020-08-30 11:54:41 +02:00
cmInstallExportGenerator::~cmInstallExportGenerator() = default;
2019-11-11 23:01:05 +01:00
bool cmInstallExportGenerator::Compute(cmLocalGenerator* lg)
2015-11-17 17:22:37 +01:00
{
this->LocalGenerator = lg;
2016-03-13 13:35:51 +01:00
this->ExportSet->Compute(lg);
2019-11-11 23:01:05 +01:00
return true;
2015-11-17 17:22:37 +01:00
}
void cmInstallExportGenerator::ComputeTempDir()
{
// Choose a temporary directory in which to generate the import
// files to be installed.
2020-02-01 23:06:01 +01:00
this->TempDir = cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
"/CMakeFiles/Export");
2016-07-09 11:21:54 +02:00
if (this->Destination.empty()) {
return;
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
this->TempDir += "/";
// Enforce a maximum length.
bool useMD5 = false;
#if defined(_WIN32) || defined(__CYGWIN__)
std::string::size_type const max_total_len = 250;
#else
std::string::size_type const max_total_len = 1000;
#endif
2016-10-30 18:24:19 +01:00
// Will generate files of the form "<temp-dir>/<base>-<config>.<ext>".
std::string::size_type const len = this->TempDir.size() + 1 +
this->FileName.size() + 1 + this->GetMaxConfigLength();
if (len < max_total_len) {
// Keep the total path length below the limit.
2016-10-30 18:24:19 +01:00
std::string::size_type const max_len = max_total_len - len;
2016-07-09 11:21:54 +02:00
if (this->Destination.size() > max_len) {
useMD5 = true;
}
2016-07-09 11:21:54 +02:00
} else {
useMD5 = true;
2016-07-09 11:21:54 +02:00
}
if (useMD5) {
// Replace the destination path with a hash to keep it short.
2022-03-29 21:10:50 +02:00
#ifndef CMAKE_BOOTSTRAP
2016-07-09 11:21:54 +02:00
this->TempDir += cmSystemTools::ComputeStringMD5(this->Destination);
2022-03-29 21:10:50 +02:00
#endif
2016-07-09 11:21:54 +02:00
} else {
std::string dest = this->Destination;
// Avoid unix full paths.
2016-07-09 11:21:54 +02:00
if (dest[0] == '/') {
dest[0] = '_';
2016-07-09 11:21:54 +02:00
}
// Avoid windows full paths by removing colons.
2016-07-09 11:21:54 +02:00
std::replace(dest.begin(), dest.end(), ':', '_');
// Avoid relative paths that go up the tree.
cmSystemTools::ReplaceString(dest, "../", "__/");
// Avoid spaces.
2016-07-09 11:21:54 +02:00
std::replace(dest.begin(), dest.end(), ' ', '_');
this->TempDir += dest;
2016-07-09 11:21:54 +02:00
}
}
2016-10-30 18:24:19 +01:00
size_t cmInstallExportGenerator::GetMaxConfigLength() const
{
// Always use at least 8 for "noconfig".
size_t len = 8;
if (this->ConfigurationTypes->empty()) {
if (this->ConfigurationName.size() > 8) {
len = this->ConfigurationName.size();
}
} else {
2018-01-26 17:06:56 +01:00
for (std::string const& c : *this->ConfigurationTypes) {
if (c.size() > len) {
len = c.size();
2016-10-30 18:24:19 +01:00
}
}
}
return len;
}
void cmInstallExportGenerator::GenerateScript(std::ostream& os)
{
// Skip empty sets.
2021-09-14 00:13:48 +02:00
if (this->ExportSet->GetTargetExports().empty()) {
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2021-09-14 00:13:48 +02:00
e << "INSTALL(EXPORT) given unknown export \""
<< this->ExportSet->GetName() << "\"";
2019-11-11 23:01:05 +01:00
cmSystemTools::Error(e.str());
return;
2016-07-09 11:21:54 +02:00
}
// Create the temporary directory in which to store the files.
this->ComputeTempDir();
2018-04-23 21:13:27 +02:00
cmSystemTools::MakeDirectory(this->TempDir);
// Construct a temporary location for the file.
2020-02-01 23:06:01 +01:00
this->MainImportFile = cmStrCat(this->TempDir, '/', this->FileName);
// Generate the import file for this export set.
this->EFGen->SetExportFile(this->MainImportFile.c_str());
2015-04-27 22:25:09 +02:00
this->EFGen->SetNamespace(this->Namespace);
2013-11-03 12:27:13 +02:00
this->EFGen->SetExportOld(this->ExportOld);
2016-07-09 11:21:54 +02:00
if (this->ConfigurationTypes->empty()) {
if (!this->ConfigurationName.empty()) {
this->EFGen->AddConfiguration(this->ConfigurationName);
2016-07-09 11:21:54 +02:00
} else {
this->EFGen->AddConfiguration("");
}
2016-07-09 11:21:54 +02:00
} else {
2018-01-26 17:06:56 +01:00
for (std::string const& c : *this->ConfigurationTypes) {
this->EFGen->AddConfiguration(c);
}
2016-07-09 11:21:54 +02:00
}
this->EFGen->GenerateImportFile();
// Perform the main install script generation.
this->cmInstallGenerator::GenerateScript(os);
}
2016-07-09 11:21:54 +02:00
void cmInstallExportGenerator::GenerateScriptConfigs(std::ostream& os,
2017-07-20 19:35:53 +02:00
Indent indent)
{
// Create the main install rules first.
this->cmInstallGenerator::GenerateScriptConfigs(os, indent);
// Now create a configuration-specific install rule for the import
// file of each configuration.
std::vector<std::string> files;
2018-01-26 17:06:56 +01:00
for (auto const& i : this->EFGen->GetConfigImportFiles()) {
files.push_back(i.second);
std::string config_test = this->CreateConfigTest(i.first);
2014-08-03 19:52:23 +02:00
os << indent << "if(" << config_test << ")\n";
2016-07-09 11:21:54 +02:00
this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
2018-01-26 17:06:56 +01:00
false, this->FilePermissions.c_str(), nullptr,
nullptr, nullptr, indent.Next());
2014-08-03 19:52:23 +02:00
os << indent << "endif()\n";
files.clear();
2016-07-09 11:21:54 +02:00
}
}
void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
2017-07-20 19:35:53 +02:00
Indent indent)
{
// Remove old per-configuration export files if the main changes.
2021-09-14 00:13:48 +02:00
std::string installedDir = cmStrCat(
"$ENV{DESTDIR}", ConvertToAbsoluteDestination(this->Destination), '/');
2020-02-01 23:06:01 +01:00
std::string installedFile = cmStrCat(installedDir, this->FileName);
2014-08-03 19:52:23 +02:00
os << indent << "if(EXISTS \"" << installedFile << "\")\n";
Indent indentN = indent.Next();
Indent indentNN = indentN.Next();
Indent indentNNN = indentNN.Next();
2016-07-09 11:21:54 +02:00
/* clang-format off */
2014-08-03 19:52:23 +02:00
os << indentN << "file(DIFFERENT EXPORT_FILE_CHANGED FILES\n"
<< indentN << " \"" << installedFile << "\"\n"
<< indentN << " \"" << this->MainImportFile << "\")\n";
2014-08-03 19:52:23 +02:00
os << indentN << "if(EXPORT_FILE_CHANGED)\n";
os << indentNN << "file(GLOB OLD_CONFIG_FILES \"" << installedDir
<< this->EFGen->GetConfigImportFileGlob() << "\")\n";
2014-08-03 19:52:23 +02:00
os << indentNN << "if(OLD_CONFIG_FILES)\n";
2019-11-11 23:01:05 +01:00
os << indentNNN << R"(message(STATUS "Old export file \")" << installedFile
<< "\\\" will be replaced. Removing files [${OLD_CONFIG_FILES}].\")\n";
2014-08-03 19:52:23 +02:00
os << indentNNN << "file(REMOVE ${OLD_CONFIG_FILES})\n";
os << indentNN << "endif()\n";
os << indentN << "endif()\n";
os << indent << "endif()\n";
2016-07-09 11:21:54 +02:00
/* clang-format on */
// Install the main export file.
std::vector<std::string> files;
files.push_back(this->MainImportFile);
2016-07-09 11:21:54 +02:00
this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
2018-01-26 17:06:56 +01:00
false, this->FilePermissions.c_str(), nullptr, nullptr,
nullptr, indent);
}
2020-02-01 23:06:01 +01:00
std::string cmInstallExportGenerator::GetDestinationFile() const
{
return this->Destination + '/' + this->FileName;
}