1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmInstallExportGenerator.h"
11 #ifndef CMAKE_BOOTSTRAP
12 # include "cmExportInstallAndroidMKGenerator.h"
14 #include "cmExportInstallFileGenerator.h"
15 #include "cmExportSet.h"
16 #include "cmInstallType.h"
17 #include "cmListFileCache.h"
18 #include "cmLocalGenerator.h"
19 #include "cmStringAlgorithms.h"
20 #include "cmSystemTools.h"
22 cmInstallExportGenerator::cmInstallExportGenerator(
23 cmExportSet* exportSet, std::string const& destination,
24 std::string file_permissions, std::vector<std::string> const& configurations,
25 std::string const& component, MessageLevel message, bool exclude_from_all,
26 std::string filename, std::string name_space, bool exportOld, bool android,
27 cmListFileBacktrace backtrace)
28 : cmInstallGenerator(destination, configurations, component, message,
29 exclude_from_all, false, std::move(backtrace))
30 , ExportSet(exportSet)
31 , FilePermissions(std::move(file_permissions))
32 , FileName(std::move(filename))
33 , Namespace(std::move(name_space))
34 , ExportOld(exportOld)
37 #ifndef CMAKE_BOOTSTRAP
38 this->EFGen = cm::make_unique<cmExportInstallAndroidMKGenerator>(this);
41 this->EFGen = cm::make_unique<cmExportInstallFileGenerator>(this);
43 exportSet->AddInstallation(this);
46 cmInstallExportGenerator::~cmInstallExportGenerator() = default;
48 bool cmInstallExportGenerator::Compute(cmLocalGenerator* lg)
50 this->LocalGenerator = lg;
51 return this->ExportSet->Compute(lg);
54 std::string cmInstallExportGenerator::TempDirCalculate() const
56 // Choose a temporary directory in which to generate the import
57 // files to be installed.
58 std::string path = cmStrCat(
59 this->LocalGenerator->GetCurrentBinaryDirectory(), "/CMakeFiles/Export");
60 if (this->Destination.empty()) {
64 #ifndef CMAKE_BOOTSTRAP
66 // Replace the destination path with a hash to keep it short.
67 path += cmSystemTools::ComputeStringMD5(this->Destination);
73 void cmInstallExportGenerator::ComputeTempDir()
75 this->TempDir = this->TempDirCalculate();
78 std::string cmInstallExportGenerator::GetTempDir() const
80 if (this->TempDir.empty()) {
81 return this->TempDirCalculate();
86 void cmInstallExportGenerator::GenerateScript(std::ostream& os)
89 if (this->ExportSet->GetTargetExports().empty()) {
91 e << "INSTALL(EXPORT) given unknown export \""
92 << this->ExportSet->GetName() << "\"";
93 cmSystemTools::Error(e.str());
97 // Create the temporary directory in which to store the files.
98 this->ComputeTempDir();
99 cmSystemTools::MakeDirectory(this->TempDir);
101 // Construct a temporary location for the file.
102 this->MainImportFile = cmStrCat(this->TempDir, '/', this->FileName);
104 // Generate the import file for this export set.
105 this->EFGen->SetExportFile(this->MainImportFile.c_str());
106 this->EFGen->SetNamespace(this->Namespace);
107 this->EFGen->SetExportOld(this->ExportOld);
108 if (this->ConfigurationTypes->empty()) {
109 if (!this->ConfigurationName.empty()) {
110 this->EFGen->AddConfiguration(this->ConfigurationName);
112 this->EFGen->AddConfiguration("");
115 for (std::string const& c : *this->ConfigurationTypes) {
116 this->EFGen->AddConfiguration(c);
119 this->EFGen->GenerateImportFile();
121 // Perform the main install script generation.
122 this->cmInstallGenerator::GenerateScript(os);
125 void cmInstallExportGenerator::GenerateScriptConfigs(std::ostream& os,
128 // Create the main install rules first.
129 this->cmInstallGenerator::GenerateScriptConfigs(os, indent);
131 // Now create a configuration-specific install rule for the import
132 // file of each configuration.
133 std::vector<std::string> files;
134 for (auto const& i : this->EFGen->GetConfigImportFiles()) {
135 files.push_back(i.second);
136 std::string config_test = this->CreateConfigTest(i.first);
137 os << indent << "if(" << config_test << ")\n";
138 this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
139 false, this->FilePermissions.c_str(), nullptr,
140 nullptr, nullptr, indent.Next());
141 os << indent << "endif()\n";
146 void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
149 // Remove old per-configuration export files if the main changes.
150 std::string installedDir = cmStrCat(
151 "$ENV{DESTDIR}", ConvertToAbsoluteDestination(this->Destination), '/');
152 std::string installedFile = cmStrCat(installedDir, this->FileName);
153 os << indent << "if(EXISTS \"" << installedFile << "\")\n";
154 Indent indentN = indent.Next();
155 Indent indentNN = indentN.Next();
156 Indent indentNNN = indentNN.Next();
157 /* clang-format off */
158 os << indentN << "file(DIFFERENT _cmake_export_file_changed FILES\n"
159 << indentN << " \"" << installedFile << "\"\n"
160 << indentN << " \"" << this->MainImportFile << "\")\n";
161 os << indentN << "if(_cmake_export_file_changed)\n";
162 os << indentNN << "file(GLOB _cmake_old_config_files \"" << installedDir
163 << this->EFGen->GetConfigImportFileGlob() << "\")\n";
164 os << indentNN << "if(_cmake_old_config_files)\n";
165 os << indentNNN << "string(REPLACE \";\" \", \" _cmake_old_config_files_text \"${_cmake_old_config_files}\")\n";
166 os << indentNNN << R"(message(STATUS "Old export file \")" << installedFile
167 << "\\\" will be replaced. Removing files [${_cmake_old_config_files_text}].\")\n";
168 os << indentNNN << "unset(_cmake_old_config_files_text)\n";
169 os << indentNNN << "file(REMOVE ${_cmake_old_config_files})\n";
170 os << indentNN << "endif()\n";
171 os << indentNN << "unset(_cmake_old_config_files)\n";
172 os << indentN << "endif()\n";
173 os << indentN << "unset(_cmake_export_file_changed)\n";
174 os << indent << "endif()\n";
175 /* clang-format on */
177 // Install the main export file.
178 std::vector<std::string> files;
179 files.push_back(this->MainImportFile);
180 this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
181 false, this->FilePermissions.c_str(), nullptr, nullptr,
185 std::string cmInstallExportGenerator::GetDestinationFile() const
187 return this->Destination + '/' + this->FileName;