Coverage Report

Created: 2026-02-09 06:05

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