Imported Upstream version 3.24.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, 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)
35 {
36   if (android) {
37 #ifndef CMAKE_BOOTSTRAP
38     this->EFGen = cm::make_unique<cmExportInstallAndroidMKGenerator>(this);
39 #endif
40   } else {
41     this->EFGen = cm::make_unique<cmExportInstallFileGenerator>(this);
42   }
43   exportSet->AddInstallation(this);
44 }
45
46 cmInstallExportGenerator::~cmInstallExportGenerator() = default;
47
48 bool cmInstallExportGenerator::Compute(cmLocalGenerator* lg)
49 {
50   this->LocalGenerator = lg;
51   return this->ExportSet->Compute(lg);
52 }
53
54 std::string cmInstallExportGenerator::TempDirCalculate() const
55 {
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()) {
61     return path;
62   }
63
64 #ifndef CMAKE_BOOTSTRAP
65   path += '/';
66   // Replace the destination path with a hash to keep it short.
67   path += cmSystemTools::ComputeStringMD5(this->Destination);
68 #endif
69
70   return path;
71 }
72
73 void cmInstallExportGenerator::ComputeTempDir()
74 {
75   this->TempDir = this->TempDirCalculate();
76 }
77
78 std::string cmInstallExportGenerator::GetTempDir() const
79 {
80   if (this->TempDir.empty()) {
81     return this->TempDirCalculate();
82   }
83   return this->TempDir;
84 }
85
86 void cmInstallExportGenerator::GenerateScript(std::ostream& os)
87 {
88   // Skip empty sets.
89   if (this->ExportSet->GetTargetExports().empty()) {
90     std::ostringstream e;
91     e << "INSTALL(EXPORT) given unknown export \""
92       << this->ExportSet->GetName() << "\"";
93     cmSystemTools::Error(e.str());
94     return;
95   }
96
97   // Create the temporary directory in which to store the files.
98   this->ComputeTempDir();
99   cmSystemTools::MakeDirectory(this->TempDir);
100
101   // Construct a temporary location for the file.
102   this->MainImportFile = cmStrCat(this->TempDir, '/', this->FileName);
103
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);
111     } else {
112       this->EFGen->AddConfiguration("");
113     }
114   } else {
115     for (std::string const& c : *this->ConfigurationTypes) {
116       this->EFGen->AddConfiguration(c);
117     }
118   }
119   this->EFGen->GenerateImportFile();
120
121   // Perform the main install script generation.
122   this->cmInstallGenerator::GenerateScript(os);
123 }
124
125 void cmInstallExportGenerator::GenerateScriptConfigs(std::ostream& os,
126                                                      Indent indent)
127 {
128   // Create the main install rules first.
129   this->cmInstallGenerator::GenerateScriptConfigs(os, indent);
130
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";
142     files.clear();
143   }
144 }
145
146 void cmInstallExportGenerator::GenerateScriptActions(std::ostream& os,
147                                                      Indent indent)
148 {
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 */
176
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,
182                        nullptr, indent);
183 }
184
185 std::string cmInstallExportGenerator::GetDestinationFile() const
186 {
187   return this->Destination + '/' + this->FileName;
188 }