cmake/Source/cmInstallExportGenerator.cxx

260 lines
9.9 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"
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"
2022-08-04 22:12:04 +02:00
#include "cmListFileCache.h"
2016-10-30 18:24:19 +01:00
#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,
2022-11-16 20:14:03 +01:00
std::string filename, std::string name_space,
std::string cxx_modules_directory, bool exportOld, bool android,
2021-09-14 00:13:48 +02:00
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))
2022-11-16 20:14:03 +01:00
, CxxModulesDirectory(std::move(cxx_modules_directory))
2016-07-09 11:21:54 +02:00
, ExportOld(exportOld)
{
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;
2022-07-29 21:54:54 +02:00
return this->ExportSet->Compute(lg);
2015-11-17 17:22:37 +01:00
}
2022-08-04 22:12:04 +02:00
std::string cmInstallExportGenerator::TempDirCalculate() const
{
// Choose a temporary directory in which to generate the import
// files to be installed.
2022-08-04 22:12:04 +02:00
std::string path = cmStrCat(
this->LocalGenerator->GetCurrentBinaryDirectory(), "/CMakeFiles/Export");
2016-07-09 11:21:54 +02:00
if (this->Destination.empty()) {
2022-08-04 22:12:04 +02:00
return path;
2016-07-09 11:21:54 +02:00
}
2022-08-04 22:12:04 +02:00
2022-03-29 21:10:50 +02:00
#ifndef CMAKE_BOOTSTRAP
2022-08-04 22:12:04 +02:00
path += '/';
// Replace the destination path with a hash to keep it short.
path += cmSystemTools::ComputeStringMD5(this->Destination);
2022-03-29 21:10:50 +02:00
#endif
2022-08-04 22:12:04 +02:00
return path;
}
2022-08-04 22:12:04 +02:00
void cmInstallExportGenerator::ComputeTempDir()
2016-10-30 18:24:19 +01:00
{
2022-08-04 22:12:04 +02:00
this->TempDir = this->TempDirCalculate();
}
std::string cmInstallExportGenerator::GetTempDir() const
{
if (this->TempDir.empty()) {
return this->TempDirCalculate();
2016-10-30 18:24:19 +01:00
}
2022-08-04 22:12:04 +02:00
return this->TempDir;
2016-10-30 18:24:19 +01:00
}
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
}
2022-11-16 20:14:03 +01:00
// Now create a configuration-specific install rule for the C++ module import
// property file of each configuration.
auto cxx_module_dest =
cmStrCat(this->Destination, '/', this->CxxModulesDirectory);
std::string config_file_example;
for (auto const& i : this->EFGen->GetConfigCxxModuleFiles()) {
config_file_example = i.second;
break;
}
if (!config_file_example.empty()) {
// Remove old per-configuration export files if the main changes.
std::string installedDir = cmStrCat(
"$ENV{DESTDIR}", ConvertToAbsoluteDestination(cxx_module_dest), '/');
std::string installedFile = cmStrCat(installedDir, "/cxx-modules.cmake");
std::string toInstallFile =
cmStrCat(cmSystemTools::GetFilenamePath(config_file_example),
"/cxx-modules.cmake");
os << indent << "if(EXISTS \"" << installedFile << "\")\n";
Indent indentN = indent.Next();
Indent indentNN = indentN.Next();
Indent indentNNN = indentNN.Next();
/* clang-format off */
os << indentN << "file(DIFFERENT _cmake_export_file_changed FILES\n"
<< indentN << " \"" << installedFile << "\"\n"
<< indentN << " \"" << toInstallFile << "\")\n";
os << indentN << "if(_cmake_export_file_changed)\n";
os << indentNN << "file(GLOB _cmake_old_config_files \"" << installedDir
<< this->EFGen->GetConfigImportFileGlob() << "\")\n";
os << indentNN << "if(_cmake_old_config_files)\n";
os << indentNNN << "string(REPLACE \";\" \", \" _cmake_old_config_files_text \"${_cmake_old_config_files}\")\n";
os << indentNNN << R"(message(STATUS "Old C++ module export file \")" << installedFile
<< "\\\" will be replaced. Removing files [${_cmake_old_config_files_text}].\")\n";
os << indentNNN << "unset(_cmake_old_config_files_text)\n";
os << indentNNN << "file(REMOVE ${_cmake_old_config_files})\n";
os << indentNN << "endif()\n";
os << indentNN << "unset(_cmake_old_config_files)\n";
os << indentN << "endif()\n";
os << indentN << "unset(_cmake_export_file_changed)\n";
os << indent << "endif()\n";
/* clang-format on */
// All of these files are siblings; get its location to know where the
// "anchor" file is.
files.push_back(toInstallFile);
this->AddInstallRule(os, cxx_module_dest, cmInstallType_FILES, files,
false, this->FilePermissions.c_str(), nullptr,
nullptr, nullptr, indent);
files.clear();
}
for (auto const& i : this->EFGen->GetConfigCxxModuleFiles()) {
files.push_back(i.second);
std::string config_test = this->CreateConfigTest(i.first);
os << indent << "if(" << config_test << ")\n";
this->AddInstallRule(os, cxx_module_dest, cmInstallType_FILES, files,
false, this->FilePermissions.c_str(), nullptr,
nullptr, nullptr, indent.Next());
os << indent << "endif()\n";
files.clear();
}
for (auto const& i : this->EFGen->GetConfigCxxModuleTargetFiles()) {
std::string config_test = this->CreateConfigTest(i.first);
os << indent << "if(" << config_test << ")\n";
this->AddInstallRule(os, cxx_module_dest, cmInstallType_FILES, i.second,
false, this->FilePermissions.c_str(), nullptr,
nullptr, nullptr, indent.Next());
os << indent << "endif()\n";
files.clear();
}
}
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 */
2022-08-04 22:12:04 +02:00
os << indentN << "file(DIFFERENT _cmake_export_file_changed FILES\n"
<< indentN << " \"" << installedFile << "\"\n"
<< indentN << " \"" << this->MainImportFile << "\")\n";
2022-08-04 22:12:04 +02:00
os << indentN << "if(_cmake_export_file_changed)\n";
os << indentNN << "file(GLOB _cmake_old_config_files \"" << installedDir
<< this->EFGen->GetConfigImportFileGlob() << "\")\n";
2022-08-04 22:12:04 +02:00
os << indentNN << "if(_cmake_old_config_files)\n";
os << indentNNN << "string(REPLACE \";\" \", \" _cmake_old_config_files_text \"${_cmake_old_config_files}\")\n";
2019-11-11 23:01:05 +01:00
os << indentNNN << R"(message(STATUS "Old export file \")" << installedFile
2022-08-04 22:12:04 +02:00
<< "\\\" will be replaced. Removing files [${_cmake_old_config_files_text}].\")\n";
os << indentNNN << "unset(_cmake_old_config_files_text)\n";
os << indentNNN << "file(REMOVE ${_cmake_old_config_files})\n";
2014-08-03 19:52:23 +02:00
os << indentNN << "endif()\n";
2022-08-04 22:12:04 +02:00
os << indentNN << "unset(_cmake_old_config_files)\n";
2014-08-03 19:52:23 +02:00
os << indentN << "endif()\n";
2022-08-04 22:12:04 +02:00
os << indentN << "unset(_cmake_export_file_changed)\n";
2014-08-03 19:52:23 +02:00
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;
}