Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Source / cmInstallExportGenerator.cxx
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"
4
5 #include <map>
6 #include <sstream>
7 #include <utility>
8
9 #include <cm/memory>
10
11 #ifndef CMAKE_BOOTSTRAP
12 #  include "cmExportInstallAndroidMKGenerator.h"
13 #endif
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"
21
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,
27   std::string cxx_modules_directory, bool exportOld, bool android,
28   cmListFileBacktrace backtrace)
29   : cmInstallGenerator(destination, configurations, component, message,
30                        exclude_from_all, false, std::move(backtrace))
31   , ExportSet(exportSet)
32   , FilePermissions(std::move(file_permissions))
33   , FileName(std::move(filename))
34   , Namespace(std::move(name_space))
35   , CxxModulesDirectory(std::move(cxx_modules_directory))
36   , ExportOld(exportOld)
37 {
38   if (android) {
39 #ifndef CMAKE_BOOTSTRAP
40     this->EFGen = cm::make_unique<cmExportInstallAndroidMKGenerator>(this);
41 #endif
42   } else {
43     this->EFGen = cm::make_unique<cmExportInstallFileGenerator>(this);
44   }
45   exportSet->AddInstallation(this);
46 }
47
48 cmInstallExportGenerator::~cmInstallExportGenerator() = default;
49
50 bool cmInstallExportGenerator::Compute(cmLocalGenerator* lg)
51 {
52   this->LocalGenerator = lg;
53   return this->ExportSet->Compute(lg);
54 }
55
56 std::string cmInstallExportGenerator::TempDirCalculate() const
57 {
58   // Choose a temporary directory in which to generate the import
59   // files to be installed.
60   std::string path = cmStrCat(
61     this->LocalGenerator->GetCurrentBinaryDirectory(), "/CMakeFiles/Export");
62   if (this->Destination.empty()) {
63     return path;
64   }
65
66 #ifndef CMAKE_BOOTSTRAP
67   path += '/';
68   // Replace the destination path with a hash to keep it short.
69   path += cmSystemTools::ComputeStringMD5(this->Destination);
70 #endif
71
72   return path;
73 }
74
75 void cmInstallExportGenerator::ComputeTempDir()
76 {
77   this->TempDir = this->TempDirCalculate();
78 }
79
80 std::string cmInstallExportGenerator::GetTempDir() const
81 {
82   if (this->TempDir.empty()) {
83     return this->TempDirCalculate();
84   }
85   return this->TempDir;
86 }
87
88 void cmInstallExportGenerator::GenerateScript(std::ostream& os)
89 {
90   // Skip empty sets.
91   if (this->ExportSet->GetTargetExports().empty()) {
92     std::ostringstream e;
93     e << "INSTALL(EXPORT) given unknown export \""
94       << this->ExportSet->GetName() << "\"";
95     cmSystemTools::Error(e.str());
96     return;
97   }
98
99   // Create the temporary directory in which to store the files.
100   this->ComputeTempDir();
101   cmSystemTools::MakeDirectory(this->TempDir);
102
103   // Construct a temporary location for the file.
104   this->MainImportFile = cmStrCat(this->TempDir, '/', this->FileName);
105
106   // Generate the import file for this export set.
107   this->EFGen->SetExportFile(this->MainImportFile.c_str());
108   this->EFGen->SetNamespace(this->Namespace);
109   this->EFGen->SetExportOld(this->ExportOld);
110   if (this->ConfigurationTypes->empty()) {
111     if (!this->ConfigurationName.empty()) {
112       this->EFGen->AddConfiguration(this->ConfigurationName);
113     } else {
114       this->EFGen->AddConfiguration("");
115     }
116   } else {
117     for (std::string const& c : *this->ConfigurationTypes) {
118       this->EFGen->AddConfiguration(c);
119     }
120   }
121   this->EFGen->GenerateImportFile();
122
123   // Perform the main install script generation.
124   this->cmInstallGenerator::GenerateScript(os);
125 }
126
127 void cmInstallExportGenerator::GenerateScriptConfigs(std::ostream& os,
128                                                      Indent indent)
129 {
130   // Create the main install rules first.
131   this->cmInstallGenerator::GenerateScriptConfigs(os, indent);
132
133   // Now create a configuration-specific install rule for the import
134   // file of each configuration.
135   std::vector<std::string> files;
136   for (auto const& i : this->EFGen->GetConfigImportFiles()) {
137     files.push_back(i.second);
138     std::string config_test = this->CreateConfigTest(i.first);
139     os << indent << "if(" << config_test << ")\n";
140     this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
141                          false, this->FilePermissions.c_str(), nullptr,
142                          nullptr, nullptr, indent.Next());
143     os << indent << "endif()\n";
144     files.clear();
145   }
146
147   // Now create a configuration-specific install rule for the C++ module import
148   // property file of each configuration.
149   auto cxx_module_dest =
150     cmStrCat(this->Destination, '/', this->CxxModulesDirectory);
151   std::string config_file_example;
152   for (auto const& i : this->EFGen->GetConfigCxxModuleFiles()) {
153     config_file_example = i.second;
154     break;
155   }
156   if (!config_file_example.empty()) {
157     // Remove old per-configuration export files if the main changes.
158     std::string installedDir = cmStrCat(
159       "$ENV{DESTDIR}", ConvertToAbsoluteDestination(cxx_module_dest), '/');
160     std::string installedFile = cmStrCat(installedDir, "/cxx-modules.cmake");
161     std::string toInstallFile =
162       cmStrCat(cmSystemTools::GetFilenamePath(config_file_example),
163                "/cxx-modules.cmake");
164     os << indent << "if(EXISTS \"" << installedFile << "\")\n";
165     Indent indentN = indent.Next();
166     Indent indentNN = indentN.Next();
167     Indent indentNNN = indentNN.Next();
168     /* clang-format off */
169     os << indentN << "file(DIFFERENT _cmake_export_file_changed FILES\n"
170        << indentN << "     \"" << installedFile << "\"\n"
171        << indentN << "     \"" << toInstallFile << "\")\n";
172     os << indentN << "if(_cmake_export_file_changed)\n";
173     os << indentNN << "file(GLOB _cmake_old_config_files \"" << installedDir
174        << this->EFGen->GetConfigImportFileGlob() << "\")\n";
175     os << indentNN << "if(_cmake_old_config_files)\n";
176     os << indentNNN << "string(REPLACE \";\" \", \" _cmake_old_config_files_text \"${_cmake_old_config_files}\")\n";
177     os << indentNNN << R"(message(STATUS "Old C++ module export file \")" << installedFile
178        << "\\\" will be replaced.  Removing files [${_cmake_old_config_files_text}].\")\n";
179     os << indentNNN << "unset(_cmake_old_config_files_text)\n";
180     os << indentNNN << "file(REMOVE ${_cmake_old_config_files})\n";
181     os << indentNN << "endif()\n";
182     os << indentNN << "unset(_cmake_old_config_files)\n";
183     os << indentN << "endif()\n";
184     os << indentN << "unset(_cmake_export_file_changed)\n";
185     os << indent << "endif()\n";
186     /* clang-format on */
187
188     // All of these files are siblings; get its location to know where the
189     // "anchor" file is.
190     files.push_back(toInstallFile);
191     this->AddInstallRule(os, cxx_module_dest, cmInstallType_FILES, files,
192                          false, this->FilePermissions.c_str(), nullptr,
193                          nullptr, nullptr, indent);
194     files.clear();
195   }
196   for (auto const& i : this->EFGen->GetConfigCxxModuleFiles()) {
197     files.push_back(i.second);
198     std::string config_test = this->CreateConfigTest(i.first);
199     os << indent << "if(" << config_test << ")\n";
200     this->AddInstallRule(os, cxx_module_dest, cmInstallType_FILES, files,
201                          false, this->FilePermissions.c_str(), nullptr,
202                          nullptr, nullptr, indent.Next());
203     os << indent << "endif()\n";
204     files.clear();
205   }
206   for (auto const& i : this->EFGen->GetConfigCxxModuleTargetFiles()) {
207     std::string config_test = this->CreateConfigTest(i.first);
208     os << indent << "if(" << config_test << ")\n";
209     this->AddInstallRule(os, cxx_module_dest, cmInstallType_FILES, i.second,
210                          false, this->FilePermissions.c_str(), nullptr,
211                          nullptr, nullptr, indent.Next());
212     os << indent << "endif()\n";
213     files.clear();
214   }
215 }
216
217 void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
218                                                      Indent indent)
219 {
220   // Remove old per-configuration export files if the main changes.
221   std::string installedDir = cmStrCat(
222     "$ENV{DESTDIR}", ConvertToAbsoluteDestination(this->Destination), '/');
223   std::string installedFile = cmStrCat(installedDir, this->FileName);
224   os << indent << "if(EXISTS \"" << installedFile << "\")\n";
225   Indent indentN = indent.Next();
226   Indent indentNN = indentN.Next();
227   Indent indentNNN = indentNN.Next();
228   /* clang-format off */
229   os << indentN << "file(DIFFERENT _cmake_export_file_changed FILES\n"
230      << indentN << "     \"" << installedFile << "\"\n"
231      << indentN << "     \"" << this->MainImportFile << "\")\n";
232   os << indentN << "if(_cmake_export_file_changed)\n";
233   os << indentNN << "file(GLOB _cmake_old_config_files \"" << installedDir
234      << this->EFGen->GetConfigImportFileGlob() << "\")\n";
235   os << indentNN << "if(_cmake_old_config_files)\n";
236   os << indentNNN << "string(REPLACE \";\" \", \" _cmake_old_config_files_text \"${_cmake_old_config_files}\")\n";
237   os << indentNNN << R"(message(STATUS "Old export file \")" << installedFile
238      << "\\\" will be replaced.  Removing files [${_cmake_old_config_files_text}].\")\n";
239   os << indentNNN << "unset(_cmake_old_config_files_text)\n";
240   os << indentNNN << "file(REMOVE ${_cmake_old_config_files})\n";
241   os << indentNN << "endif()\n";
242   os << indentNN << "unset(_cmake_old_config_files)\n";
243   os << indentN << "endif()\n";
244   os << indentN << "unset(_cmake_export_file_changed)\n";
245   os << indent << "endif()\n";
246   /* clang-format on */
247
248   // Install the main export file.
249   std::vector<std::string> files;
250   files.push_back(this->MainImportFile);
251   this->AddInstallRule(os, this->Destination, cmInstallType_FILES, files,
252                        false, this->FilePermissions.c_str(), nullptr, nullptr,
253                        nullptr, indent);
254 }
255
256 std::string cmInstallExportGenerator::GetDestinationFile() const
257 {
258   return this->Destination + '/' + this->FileName;
259 }