cmake/Source/cmInstallExportGenerator.cxx

220 lines
7.3 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>
#include <sstream>
#include <utility>
2016-10-30 18:24:19 +01:00
#ifdef CMAKE_BUILD_WITH_CMAKE
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"
#include "cmSystemTools.h"
#include "cmake.h"
cmInstallExportGenerator::cmInstallExportGenerator(
2016-07-09 11:21:54 +02:00
cmExportSet* exportSet, const char* destination,
const char* file_permissions, std::vector<std::string> const& configurations,
const char* component, MessageLevel message, bool exclude_from_all,
2016-10-30 18:24:19 +01:00
const char* filename, const char* name_space, bool exportOld, bool android)
2016-07-09 11:21:54 +02:00
: cmInstallGenerator(destination, configurations, component, message,
exclude_from_all)
, ExportSet(exportSet)
, FilePermissions(file_permissions)
, FileName(filename)
, Namespace(name_space)
, ExportOld(exportOld)
2018-01-26 17:06:56 +01:00
, LocalGenerator(nullptr)
{
2016-10-30 18:24:19 +01:00
if (android) {
#ifdef CMAKE_BUILD_WITH_CMAKE
this->EFGen = new cmExportInstallAndroidMKGenerator(this);
#endif
} else {
this->EFGen = new cmExportInstallFileGenerator(this);
}
2013-03-16 19:13:01 +02:00
exportSet->AddInstallation(this);
}
cmInstallExportGenerator::~cmInstallExportGenerator()
{
delete this->EFGen;
}
2015-11-17 17:22:37 +01:00
void cmInstallExportGenerator::Compute(cmLocalGenerator* lg)
{
this->LocalGenerator = lg;
2016-03-13 13:35:51 +01:00
this->ExportSet->Compute(lg);
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.
2016-07-09 11:21:54 +02:00
this->TempDir = this->LocalGenerator->GetCurrentBinaryDirectory();
this->TempDir += cmake::GetCMakeFilesDirectory();
this->TempDir += "/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.
2016-07-09 11:21:54 +02:00
this->TempDir += cmSystemTools::ComputeStringMD5(this->Destination);
} 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.
2016-07-09 11:21:54 +02:00
if (ExportSet->GetTargetExports()->empty()) {
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2016-07-09 11:21:54 +02:00
e << "INSTALL(EXPORT) given unknown export \"" << ExportSet->GetName()
<< "\"";
cmSystemTools::Error(e.str().c_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.
this->MainImportFile = this->TempDir;
this->MainImportFile += "/";
this->MainImportFile += 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.
std::string installedDir = "$ENV{DESTDIR}";
2015-08-17 11:37:30 +02:00
installedDir += this->ConvertToAbsoluteDestination(this->Destination);
installedDir += "/";
std::string installedFile = installedDir;
installedFile += 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";
os << indentNNN << "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);
}