/src/CMake/Source/cmExportInstallCMakeConfigGenerator.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 "cmExportInstallCMakeConfigGenerator.h" |
4 | | |
5 | | #include <functional> |
6 | | #include <map> |
7 | | #include <memory> |
8 | | #include <set> |
9 | | #include <sstream> |
10 | | #include <utility> |
11 | | #include <vector> |
12 | | |
13 | | #include "cmExportFileGenerator.h" |
14 | | #include "cmExportSet.h" |
15 | | #include "cmGenExContext.h" |
16 | | #include "cmGeneratedFileStream.h" |
17 | | #include "cmGeneratorExpression.h" |
18 | | #include "cmGeneratorFileSet.h" |
19 | | #include "cmGeneratorTarget.h" |
20 | | #include "cmInstallExportGenerator.h" |
21 | | #include "cmInstallFileSetGenerator.h" |
22 | | #include "cmLocalGenerator.h" |
23 | | #include "cmMakefile.h" |
24 | | #include "cmOutputConverter.h" |
25 | | #include "cmStateTypes.h" |
26 | | #include "cmStringAlgorithms.h" |
27 | | #include "cmSystemTools.h" |
28 | | #include "cmTargetExport.h" |
29 | | #include "cmValue.h" |
30 | | |
31 | | cmExportInstallCMakeConfigGenerator::cmExportInstallCMakeConfigGenerator( |
32 | | cmInstallExportGenerator* iegen) |
33 | 0 | : cmExportInstallFileGenerator(iegen) |
34 | 0 | { |
35 | 0 | } Unexecuted instantiation: cmExportInstallCMakeConfigGenerator::cmExportInstallCMakeConfigGenerator(cmInstallExportGenerator*) Unexecuted instantiation: cmExportInstallCMakeConfigGenerator::cmExportInstallCMakeConfigGenerator(cmInstallExportGenerator*) |
36 | | |
37 | | std::string cmExportInstallCMakeConfigGenerator::GetConfigImportFileGlob() |
38 | | const |
39 | 0 | { |
40 | 0 | std::string glob = cmStrCat(this->FileBase, "-*", this->FileExt); |
41 | 0 | return glob; |
42 | 0 | } |
43 | | |
44 | | bool cmExportInstallCMakeConfigGenerator::GenerateMainFile(std::ostream& os) |
45 | 0 | { |
46 | 0 | std::vector<cmTargetExport const*> allTargets; |
47 | 0 | { |
48 | 0 | std::string expectedTargets; |
49 | 0 | std::string sep; |
50 | 0 | auto visitor = [&](cmTargetExport const* te) { |
51 | 0 | allTargets.push_back(te); |
52 | 0 | expectedTargets += sep + this->Namespace + te->Target->GetExportName(); |
53 | 0 | sep = " "; |
54 | 0 | }; |
55 | |
|
56 | 0 | if (!this->CollectExports(visitor)) { |
57 | 0 | return false; |
58 | 0 | } |
59 | | |
60 | 0 | this->GenerateExpectedTargetsCode(os, expectedTargets); |
61 | 0 | } |
62 | | |
63 | | // Compute the relative import prefix for the file |
64 | 0 | this->GenerateImportPrefix(os); |
65 | |
|
66 | 0 | bool requiresConfigFiles = false; |
67 | | // Create all the imported targets. |
68 | 0 | for (cmTargetExport const* te : allTargets) { |
69 | 0 | cmGeneratorTarget* gt = te->Target; |
70 | 0 | cmStateEnums::TargetType targetType = this->GetExportTargetType(te); |
71 | |
|
72 | 0 | requiresConfigFiles = |
73 | 0 | requiresConfigFiles || targetType != cmStateEnums::INTERFACE_LIBRARY; |
74 | |
|
75 | 0 | this->GenerateImportTargetCode(os, gt, targetType); |
76 | |
|
77 | 0 | ImportPropertyMap properties; |
78 | 0 | if (!this->PopulateInterfaceProperties(te, properties)) { |
79 | 0 | return false; |
80 | 0 | } |
81 | | |
82 | 0 | ImportFileSetPropertyMap fsProperties; |
83 | 0 | if (!this->PopulateFileSetInterfaceProperties(te, fsProperties)) { |
84 | 0 | return false; |
85 | 0 | } |
86 | | |
87 | 0 | if (this->PopulateInterfaceLinkLibrariesProperty( |
88 | 0 | gt, cmGeneratorExpression::InstallInterface, properties) && |
89 | 0 | !this->ExportOld) { |
90 | 0 | this->SetRequiredCMakeVersion(2, 8, 12); |
91 | 0 | } |
92 | 0 | if (targetType == cmStateEnums::INTERFACE_LIBRARY) { |
93 | 0 | this->SetRequiredCMakeVersion(3, 0, 0); |
94 | 0 | } |
95 | 0 | if (gt->GetProperty("INTERFACE_SOURCES")) { |
96 | | // We can only generate INTERFACE_SOURCES in CMake 3.3, but CMake 3.1 |
97 | | // can consume them. |
98 | 0 | this->SetRequiredCMakeVersion(3, 1, 0); |
99 | 0 | } |
100 | |
|
101 | 0 | this->GenerateInterfaceProperties(gt, os, properties); |
102 | |
|
103 | 0 | this->GenerateTargetFileSets(gt, os, fsProperties, te); |
104 | 0 | } |
105 | | |
106 | 0 | this->LoadConfigFiles(os); |
107 | |
|
108 | 0 | bool result = true; |
109 | |
|
110 | 0 | std::string cxx_modules_name = this->GetExportSet()->GetName(); |
111 | 0 | this->GenerateCxxModuleInformation(cxx_modules_name, os); |
112 | 0 | if (requiresConfigFiles) { |
113 | 0 | for (std::string const& c : this->Configurations) { |
114 | 0 | if (!this->GenerateImportCxxModuleConfigTargetInclusion(cxx_modules_name, |
115 | 0 | c)) { |
116 | 0 | result = false; |
117 | 0 | } |
118 | 0 | } |
119 | 0 | } |
120 | |
|
121 | 0 | this->CleanupTemporaryVariables(os); |
122 | 0 | this->GenerateImportedFileCheckLoop(os); |
123 | | |
124 | | // Generate an import file for each configuration. |
125 | | // Don't do this if we only export INTERFACE_LIBRARY targets. |
126 | 0 | if (requiresConfigFiles) { |
127 | 0 | for (std::string const& c : this->Configurations) { |
128 | 0 | if (!this->GenerateImportFileConfig(c)) { |
129 | 0 | result = false; |
130 | 0 | } |
131 | 0 | } |
132 | 0 | } |
133 | |
|
134 | 0 | this->GenerateMissingTargetsCheckCode(os); |
135 | |
|
136 | 0 | return result; |
137 | 0 | } |
138 | | |
139 | | void cmExportInstallCMakeConfigGenerator::GenerateImportPrefix( |
140 | | std::ostream& os) |
141 | 0 | { |
142 | | // Set an _IMPORT_PREFIX variable for import location properties |
143 | | // to reference if they are relative to the install prefix. |
144 | 0 | std::string installPrefix = |
145 | 0 | this->IEGen->GetLocalGenerator()->GetMakefile()->GetSafeDefinition( |
146 | 0 | "CMAKE_INSTALL_PREFIX"); |
147 | 0 | std::string const& expDest = this->IEGen->GetDestination(); |
148 | 0 | if (cmSystemTools::FileIsFullPath(expDest)) { |
149 | | // The export file is being installed to an absolute path so the |
150 | | // package is not relocatable. Use the configured install prefix. |
151 | | /* clang-format off */ |
152 | 0 | os << |
153 | 0 | "# The installation prefix configured by this project.\n" |
154 | 0 | "set(_IMPORT_PREFIX \"" << installPrefix << "\")\n" |
155 | 0 | "\n"; |
156 | | /* clang-format on */ |
157 | 0 | } else { |
158 | | // Add code to compute the installation prefix relative to the |
159 | | // import file location. |
160 | 0 | std::string absDest = cmStrCat(installPrefix, '/', expDest); |
161 | 0 | std::string absDestS = absDest + '/'; |
162 | 0 | os << "# Compute the installation prefix relative to this file.\n" |
163 | 0 | "get_filename_component(_IMPORT_PREFIX" |
164 | 0 | " \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n"; |
165 | 0 | if (cmHasLiteralPrefix(absDestS, "/lib/") || |
166 | 0 | cmHasLiteralPrefix(absDestS, "/lib64/") || |
167 | 0 | cmHasLiteralPrefix(absDestS, "/libx32/") || |
168 | 0 | cmHasLiteralPrefix(absDestS, "/usr/lib/") || |
169 | 0 | cmHasLiteralPrefix(absDestS, "/usr/lib64/") || |
170 | 0 | cmHasLiteralPrefix(absDestS, "/usr/libx32/")) { |
171 | | // Handle "/usr move" symlinks created by some Linux distros. |
172 | | /* clang-format off */ |
173 | 0 | os << |
174 | 0 | "# Use original install prefix when loaded through a\n" |
175 | 0 | "# cross-prefix symbolic link such as /lib -> /usr/lib.\n" |
176 | 0 | "get_filename_component(_realCurr \"${_IMPORT_PREFIX}\" REALPATH)\n" |
177 | 0 | "get_filename_component(_realOrig \"" << absDest << "\" REALPATH)\n" |
178 | 0 | "if(_realCurr STREQUAL _realOrig)\n" |
179 | 0 | " set(_IMPORT_PREFIX \"" << absDest << "\")\n" |
180 | 0 | "endif()\n" |
181 | 0 | "unset(_realOrig)\n" |
182 | 0 | "unset(_realCurr)\n"; |
183 | | /* clang-format on */ |
184 | 0 | } |
185 | 0 | std::string dest = expDest; |
186 | 0 | while (!dest.empty()) { |
187 | 0 | os << "get_filename_component(_IMPORT_PREFIX \"${_IMPORT_PREFIX}\" " |
188 | 0 | "PATH)\n"; |
189 | 0 | dest = cmSystemTools::GetFilenamePath(dest); |
190 | 0 | } |
191 | 0 | os << "if(_IMPORT_PREFIX STREQUAL \"/\")\n" |
192 | 0 | " set(_IMPORT_PREFIX \"\")\n" |
193 | 0 | "endif()\n" |
194 | 0 | "\n"; |
195 | 0 | } |
196 | 0 | } |
197 | | |
198 | | void cmExportInstallCMakeConfigGenerator::CleanupTemporaryVariables( |
199 | | std::ostream& os) |
200 | 0 | { |
201 | | /* clang-format off */ |
202 | 0 | os << "# Cleanup temporary variables.\n" |
203 | 0 | "set(_IMPORT_PREFIX)\n" |
204 | 0 | "\n"; |
205 | | /* clang-format on */ |
206 | 0 | } |
207 | | |
208 | | void cmExportInstallCMakeConfigGenerator::LoadConfigFiles(std::ostream& os) |
209 | 0 | { |
210 | | // Now load per-configuration properties for them. |
211 | | /* clang-format off */ |
212 | 0 | os << "# Load information for each installed configuration.\n" |
213 | 0 | "file(GLOB _cmake_config_files \"${CMAKE_CURRENT_LIST_DIR}/" |
214 | 0 | << this->GetConfigImportFileGlob() << "\")\n" |
215 | 0 | "foreach(_cmake_config_file IN LISTS _cmake_config_files)\n" |
216 | 0 | " include(\"${_cmake_config_file}\")\n" |
217 | 0 | "endforeach()\n" |
218 | 0 | "unset(_cmake_config_file)\n" |
219 | 0 | "unset(_cmake_config_files)\n" |
220 | 0 | "\n"; |
221 | | /* clang-format on */ |
222 | 0 | } |
223 | | |
224 | | void cmExportInstallCMakeConfigGenerator::GenerateImportConfig( |
225 | | std::ostream& os, std::string const& config) |
226 | 0 | { |
227 | | // Start with the import file header. |
228 | 0 | this->GenerateImportHeaderCode(os, config); |
229 | | |
230 | | // Generate the per-config target information. |
231 | 0 | this->cmExportFileGenerator::GenerateImportConfig(os, config); |
232 | | |
233 | | // End with the import file footer. |
234 | 0 | this->GenerateImportFooterCode(os); |
235 | 0 | } |
236 | | |
237 | | void cmExportInstallCMakeConfigGenerator::GenerateImportTargetsConfig( |
238 | | std::ostream& os, std::string const& config, std::string const& suffix) |
239 | 0 | { |
240 | | // Add each target in the set to the export. |
241 | 0 | for (std::unique_ptr<cmTargetExport> const& te : |
242 | 0 | this->GetExportSet()->GetTargetExports()) { |
243 | | // Collect import properties for this target. |
244 | 0 | if (this->GetExportTargetType(te.get()) == |
245 | 0 | cmStateEnums::INTERFACE_LIBRARY) { |
246 | 0 | continue; |
247 | 0 | } |
248 | | |
249 | 0 | ImportPropertyMap properties; |
250 | 0 | std::set<std::string> importedLocations; |
251 | |
|
252 | 0 | this->PopulateImportProperties(config, suffix, te.get(), properties, |
253 | 0 | importedLocations); |
254 | | |
255 | | // If any file location was set for the target add it to the |
256 | | // import file. |
257 | 0 | if (!properties.empty()) { |
258 | 0 | cmGeneratorTarget const* const gtgt = te->Target; |
259 | 0 | std::string const importedXcFrameworkLocation = |
260 | 0 | this->GetImportXcFrameworkLocation(config, te.get()); |
261 | |
|
262 | 0 | this->SetImportLinkInterface(config, suffix, |
263 | 0 | cmGeneratorExpression::InstallInterface, |
264 | 0 | gtgt, properties); |
265 | |
|
266 | 0 | this->GenerateImportPropertyCode(os, config, suffix, gtgt, properties, |
267 | 0 | importedXcFrameworkLocation); |
268 | 0 | this->GenerateImportedFileChecksCode( |
269 | 0 | os, gtgt, properties, importedLocations, importedXcFrameworkLocation); |
270 | 0 | } |
271 | 0 | } |
272 | 0 | } |
273 | | |
274 | | std::string cmExportInstallCMakeConfigGenerator::GetFileSetDirectories( |
275 | | cmGeneratorTarget* gte, cmGeneratorFileSet const* fileSet, |
276 | | cmTargetExport const* te) |
277 | 0 | { |
278 | 0 | std::vector<std::string> resultVector; |
279 | |
|
280 | 0 | auto configs = |
281 | 0 | gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig); |
282 | |
|
283 | 0 | for (auto const& config : configs) { |
284 | 0 | cmInstallFileSetGenerator::DestinationContext result = |
285 | 0 | te->FileSetGenerators.at(fileSet->GetName()) |
286 | 0 | ->GetDestination(gte, config); |
287 | |
|
288 | 0 | std::string dest = cmOutputConverter::EscapeForCMake( |
289 | 0 | result.UnescapedDestination, cmOutputConverter::WrapQuotes::NoWrap); |
290 | 0 | if (!cmSystemTools::FileIsFullPath(result.UnescapedDestination)) { |
291 | 0 | dest = cmStrCat("${_IMPORT_PREFIX}/", dest); |
292 | 0 | } |
293 | |
|
294 | 0 | if (result.HadContextSensitiveCondition && configs.size() != 1) { |
295 | 0 | resultVector.push_back( |
296 | 0 | cmStrCat("\"$<$<CONFIG:", config, ">:", dest, ">\"")); |
297 | 0 | } else { |
298 | 0 | resultVector.emplace_back(cmStrCat('"', dest, '"')); |
299 | 0 | break; |
300 | 0 | } |
301 | 0 | } |
302 | |
|
303 | 0 | return cmJoin(resultVector, " "); |
304 | 0 | } |
305 | | |
306 | | std::string cmExportInstallCMakeConfigGenerator::GetFileSetFiles( |
307 | | cmGeneratorTarget* gte, cmGeneratorFileSet const* fileSet, |
308 | | cmTargetExport const* te) |
309 | 0 | { |
310 | 0 | std::vector<std::string> resultVector; |
311 | |
|
312 | 0 | auto configs = |
313 | 0 | gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig); |
314 | |
|
315 | 0 | cmGeneratorExpression destGe(*gte->Makefile->GetCMakeInstance()); |
316 | 0 | auto destCge = destGe.Parse( |
317 | 0 | te->FileSetGenerators.at(fileSet->GetName())->GetDestination()); |
318 | |
|
319 | 0 | for (auto const& config : configs) { |
320 | 0 | cm::GenEx::Context context(gte->LocalGenerator, config); |
321 | 0 | auto files = fileSet->GetFiles(context, gte); |
322 | |
|
323 | 0 | auto unescapedDest = destCge->Evaluate(gte->LocalGenerator, config, gte); |
324 | 0 | auto dest = |
325 | 0 | cmStrCat(cmOutputConverter::EscapeForCMake( |
326 | 0 | unescapedDest, cmOutputConverter::WrapQuotes::NoWrap), |
327 | 0 | '/'); |
328 | 0 | if (!cmSystemTools::FileIsFullPath(unescapedDest)) { |
329 | 0 | dest = cmStrCat("${_IMPORT_PREFIX}/", dest); |
330 | 0 | } |
331 | |
|
332 | 0 | bool const contextSensitive = |
333 | 0 | destCge->GetHadContextSensitiveCondition() || files.second; |
334 | 0 | for (auto const& it : files.first) { |
335 | 0 | auto prefix = it.first.empty() ? "" : cmStrCat(it.first, '/'); |
336 | 0 | for (auto const& filename : it.second) { |
337 | 0 | auto relFile = |
338 | 0 | cmStrCat(prefix, cmSystemTools::GetFilenameNameView(filename)); |
339 | 0 | auto escapedFile = |
340 | 0 | cmStrCat(dest, |
341 | 0 | cmOutputConverter::EscapeForCMake( |
342 | 0 | relFile, cmOutputConverter::WrapQuotes::NoWrap)); |
343 | 0 | if (contextSensitive && configs.size() != 1) { |
344 | 0 | resultVector.push_back( |
345 | 0 | cmStrCat("\"$<$<CONFIG:", config, ">:", escapedFile, ">\"")); |
346 | 0 | } else { |
347 | 0 | resultVector.emplace_back(cmStrCat('"', escapedFile, '"')); |
348 | 0 | } |
349 | 0 | } |
350 | 0 | } |
351 | |
|
352 | 0 | if (!(contextSensitive && configs.size() != 1)) { |
353 | 0 | break; |
354 | 0 | } |
355 | 0 | } |
356 | |
|
357 | 0 | return cmJoin(resultVector, " "); |
358 | 0 | } |
359 | | |
360 | | std::string cmExportInstallCMakeConfigGenerator::GetCxxModulesDirectory() const |
361 | 0 | { |
362 | 0 | return IEGen->GetCxxModuleDirectory(); |
363 | 0 | } |
364 | | |
365 | | void cmExportInstallCMakeConfigGenerator::GenerateCxxModuleConfigInformation( |
366 | | std::string const& name, std::ostream& os) const |
367 | 0 | { |
368 | | // Now load per-configuration properties for them. |
369 | | /* clang-format off */ |
370 | 0 | os << "# Load information for each installed configuration.\n" |
371 | 0 | "file(GLOB _cmake_cxx_module_includes \"${CMAKE_CURRENT_LIST_DIR}/cxx-modules-" << name << "-*.cmake\")\n" |
372 | 0 | "foreach(_cmake_cxx_module_include IN LISTS _cmake_cxx_module_includes)\n" |
373 | 0 | " include(\"${_cmake_cxx_module_include}\")\n" |
374 | 0 | "endforeach()\n" |
375 | 0 | "unset(_cmake_cxx_module_include)\n" |
376 | 0 | "unset(_cmake_cxx_module_includes)\n"; |
377 | | /* clang-format on */ |
378 | 0 | } |
379 | | |
380 | | bool cmExportInstallCMakeConfigGenerator:: |
381 | | GenerateImportCxxModuleConfigTargetInclusion(std::string const& name, |
382 | | std::string const& config) |
383 | 0 | { |
384 | 0 | auto cxx_modules_dirname = this->GetCxxModulesDirectory(); |
385 | 0 | if (cxx_modules_dirname.empty()) { |
386 | 0 | return true; |
387 | 0 | } |
388 | | |
389 | 0 | std::string filename_config = config; |
390 | 0 | if (filename_config.empty()) { |
391 | 0 | filename_config = "noconfig"; |
392 | 0 | } |
393 | |
|
394 | 0 | std::string const dest = |
395 | 0 | cmStrCat(this->FileDir, '/', cxx_modules_dirname, '/'); |
396 | 0 | std::string fileName = |
397 | 0 | cmStrCat(dest, "cxx-modules-", name, '-', filename_config, ".cmake"); |
398 | |
|
399 | 0 | cmGeneratedFileStream os(fileName, true); |
400 | 0 | if (!os) { |
401 | 0 | std::string se = cmSystemTools::GetLastSystemError(); |
402 | 0 | std::ostringstream e; |
403 | 0 | e << "cannot write to file \"" << fileName << "\": " << se; |
404 | 0 | cmSystemTools::Error(e.str()); |
405 | 0 | return false; |
406 | 0 | } |
407 | 0 | os.SetCopyIfDifferent(true); |
408 | | |
409 | | // Record this per-config import file. |
410 | 0 | this->ConfigCxxModuleFiles[config] = fileName; |
411 | |
|
412 | 0 | auto& prop_files = this->ConfigCxxModuleTargetFiles[config]; |
413 | 0 | for (auto const* tgt : this->ExportedTargets) { |
414 | | // Only targets with C++ module sources will have a |
415 | | // collator-generated install script. |
416 | 0 | if (!tgt->HaveCxx20ModuleSources()) { |
417 | 0 | continue; |
418 | 0 | } |
419 | | |
420 | 0 | auto prop_filename = cmStrCat("target-", tgt->GetFilesystemExportName(), |
421 | 0 | '-', filename_config, ".cmake"); |
422 | 0 | prop_files.emplace_back(cmStrCat(dest, prop_filename)); |
423 | 0 | os << "include(\"${CMAKE_CURRENT_LIST_DIR}/" << prop_filename << "\")\n"; |
424 | 0 | } |
425 | |
|
426 | 0 | return true; |
427 | 0 | } |