/src/CMake/Source/cmExportBuildCMakeConfigGenerator.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 "cmExportBuildCMakeConfigGenerator.h" |
4 | | |
5 | | #include <cstddef> |
6 | | #include <functional> |
7 | | #include <map> |
8 | | #include <ostream> |
9 | | #include <set> |
10 | | #include <utility> |
11 | | #include <vector> |
12 | | |
13 | | #include "cmCryptoHash.h" |
14 | | #include "cmDiagnosticContext.h" |
15 | | #include "cmExportSet.h" |
16 | | #include "cmGenExContext.h" |
17 | | #include "cmGeneratedFileStream.h" |
18 | | #include "cmGeneratorExpression.h" |
19 | | #include "cmGeneratorFileSet.h" |
20 | | #include "cmGeneratorTarget.h" |
21 | | #include "cmLocalGenerator.h" |
22 | | #include "cmMakefile.h" |
23 | | #include "cmOutputConverter.h" |
24 | | #include "cmStringAlgorithms.h" |
25 | | #include "cmSystemTools.h" |
26 | | #include "cmTarget.h" |
27 | | #include "cmTargetTypes.h" |
28 | | |
29 | | cmExportBuildCMakeConfigGenerator::cmExportBuildCMakeConfigGenerator( |
30 | | cmDiagnosticContext context) |
31 | 0 | : cmExportBuildFileGenerator(std::move(context)) |
32 | 0 | { |
33 | 0 | this->LG = nullptr; |
34 | 0 | this->ExportSet = nullptr; |
35 | 0 | } Unexecuted instantiation: cmExportBuildCMakeConfigGenerator::cmExportBuildCMakeConfigGenerator(cmDiagnosticContext) Unexecuted instantiation: cmExportBuildCMakeConfigGenerator::cmExportBuildCMakeConfigGenerator(cmDiagnosticContext) |
36 | | |
37 | | bool cmExportBuildCMakeConfigGenerator::GenerateMainFile(std::ostream& os) |
38 | 0 | { |
39 | 0 | { |
40 | 0 | std::string expectedTargets; |
41 | 0 | std::string sep; |
42 | 0 | bool generatedInterfaceRequired = false; |
43 | 0 | auto visitor = [&](cmGeneratorTarget const* te) { |
44 | 0 | expectedTargets += sep + this->Namespace + te->GetExportName(); |
45 | 0 | sep = " "; |
46 | |
|
47 | 0 | generatedInterfaceRequired |= |
48 | 0 | this->GetExportTargetType(te) == cm::TargetType::INTERFACE_LIBRARY; |
49 | 0 | }; |
50 | |
|
51 | 0 | if (!this->CollectExports(visitor)) { |
52 | 0 | return false; |
53 | 0 | } |
54 | | |
55 | 0 | if (generatedInterfaceRequired) { |
56 | 0 | this->SetRequiredCMakeVersion(3, 0, 0); |
57 | 0 | } |
58 | 0 | this->GenerateExpectedTargetsCode(os, expectedTargets); |
59 | 0 | } |
60 | | |
61 | | // Create all the imported targets. |
62 | 0 | for (auto const& exp : this->Exports) { |
63 | 0 | cmGeneratorTarget* gte = exp.Target; |
64 | 0 | this->GenerateImportTargetCode(os, gte, this->GetExportTargetType(gte)); |
65 | |
|
66 | 0 | gte->Target->AppendBuildInterfaceIncludes(); |
67 | |
|
68 | 0 | ImportPropertyMap properties; |
69 | 0 | if (!this->PopulateInterfaceProperties(gte, properties)) { |
70 | 0 | return false; |
71 | 0 | } |
72 | | |
73 | 0 | ImportFileSetPropertyMap fsProperties; |
74 | 0 | if (!this->PopulateFileSetInterfaceProperties(gte, fsProperties)) { |
75 | 0 | return false; |
76 | 0 | } |
77 | | |
78 | 0 | this->PopulateInterfaceLinkLibrariesProperty( |
79 | 0 | gte, cmGeneratorExpression::BuildInterface, properties); |
80 | |
|
81 | 0 | this->GenerateInterfaceProperties(gte, os, properties); |
82 | |
|
83 | 0 | this->GenerateTargetFileSets(gte, os, fsProperties); |
84 | 0 | } |
85 | | |
86 | 0 | std::string cxx_modules_name; |
87 | 0 | if (this->ExportSet) { |
88 | 0 | cxx_modules_name = this->ExportSet->GetName(); |
89 | 0 | } else { |
90 | 0 | cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_512); |
91 | 0 | constexpr std::size_t HASH_TRUNCATION = 12; |
92 | 0 | for (auto const& target : this->Targets) { |
93 | 0 | hasher.Append(target.Name); |
94 | 0 | } |
95 | 0 | cxx_modules_name = hasher.FinalizeHex().substr(0, HASH_TRUNCATION); |
96 | 0 | } |
97 | |
|
98 | 0 | this->GenerateCxxModuleInformation(cxx_modules_name, os); |
99 | | |
100 | | // Generate import file content for each configuration. |
101 | 0 | for (std::string const& c : this->Configurations) { |
102 | 0 | this->GenerateImportConfig(os, c); |
103 | 0 | } |
104 | | |
105 | | // Generate import file content for each configuration. |
106 | 0 | for (std::string const& c : this->Configurations) { |
107 | 0 | this->GenerateImportCxxModuleConfigTargetInclusion(cxx_modules_name, c); |
108 | 0 | } |
109 | |
|
110 | 0 | this->GenerateMissingTargetsCheckCode(os); |
111 | |
|
112 | 0 | return true; |
113 | 0 | } |
114 | | |
115 | | void cmExportBuildCMakeConfigGenerator::GenerateImportTargetsConfig( |
116 | | std::ostream& os, std::string const& config, std::string const& suffix) |
117 | 0 | { |
118 | 0 | for (auto const& exp : this->Exports) { |
119 | 0 | cmGeneratorTarget* target = exp.Target; |
120 | | |
121 | | // Collect import properties for this target. |
122 | 0 | ImportPropertyMap properties; |
123 | |
|
124 | 0 | if (this->GetExportTargetType(target) != |
125 | 0 | cm::TargetType::INTERFACE_LIBRARY) { |
126 | 0 | this->SetImportLocationProperty(config, suffix, target, properties); |
127 | 0 | } |
128 | 0 | if (!properties.empty()) { |
129 | | // Get the rest of the target details. |
130 | 0 | if (this->GetExportTargetType(target) != |
131 | 0 | cm::TargetType::INTERFACE_LIBRARY) { |
132 | 0 | this->SetImportDetailProperties(config, suffix, target, properties); |
133 | 0 | this->SetImportLinkInterface(config, suffix, |
134 | 0 | cmGeneratorExpression::BuildInterface, |
135 | 0 | target, properties); |
136 | 0 | } |
137 | | |
138 | | // TODO: PUBLIC_HEADER_LOCATION |
139 | | // This should wait until the build feature propagation stuff |
140 | | // is done. Then this can be a propagated include directory. |
141 | | // this->GenerateImportProperty(config, te->HeaderGenerator, |
142 | | // properties); |
143 | | |
144 | | // Generate code in the export file. |
145 | 0 | std::string importedXcFrameworkLocation = exp.XcFrameworkLocation; |
146 | 0 | if (!importedXcFrameworkLocation.empty()) { |
147 | 0 | importedXcFrameworkLocation = cmGeneratorExpression::Preprocess( |
148 | 0 | importedXcFrameworkLocation, |
149 | 0 | cmGeneratorExpression::PreprocessContext::BuildInterface); |
150 | 0 | importedXcFrameworkLocation = cmGeneratorExpression::Evaluate( |
151 | 0 | importedXcFrameworkLocation, exp.Target->GetLocalGenerator(), config, |
152 | 0 | exp.Target, nullptr, exp.Target); |
153 | 0 | if (!importedXcFrameworkLocation.empty() && |
154 | 0 | !cmSystemTools::FileIsFullPath(importedXcFrameworkLocation)) { |
155 | 0 | importedXcFrameworkLocation = |
156 | 0 | cmStrCat(this->LG->GetCurrentBinaryDirectory(), '/', |
157 | 0 | importedXcFrameworkLocation); |
158 | 0 | } |
159 | 0 | } |
160 | 0 | this->GenerateImportPropertyCode(os, config, suffix, target, properties, |
161 | 0 | importedXcFrameworkLocation); |
162 | 0 | } |
163 | 0 | } |
164 | 0 | } |
165 | | |
166 | | std::string cmExportBuildCMakeConfigGenerator::GetFileSetDirectories( |
167 | | cmGeneratorTarget* gte, cmGeneratorFileSet const* fileSet, |
168 | | cmTargetExport const* /*te*/) |
169 | 0 | { |
170 | 0 | std::vector<std::string> resultVector; |
171 | |
|
172 | 0 | auto configs = |
173 | 0 | gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig); |
174 | |
|
175 | 0 | for (auto const& config : configs) { |
176 | 0 | cm::GenEx::Context context(gte->LocalGenerator, config); |
177 | 0 | auto directories = fileSet->GetDirectories(context, gte); |
178 | 0 | bool const contextSensitive = directories.second; |
179 | |
|
180 | 0 | for (auto const& directory : directories.first) { |
181 | 0 | auto dest = cmOutputConverter::EscapeForCMake( |
182 | 0 | directory, cmOutputConverter::WrapQuotes::NoWrap); |
183 | |
|
184 | 0 | if (contextSensitive && configs.size() != 1) { |
185 | 0 | resultVector.push_back( |
186 | 0 | cmStrCat("\"$<$<CONFIG:", config, ">:", dest, ">\"")); |
187 | 0 | } else { |
188 | 0 | resultVector.emplace_back(cmStrCat('"', dest, '"')); |
189 | 0 | } |
190 | 0 | } |
191 | 0 | } |
192 | |
|
193 | 0 | return cmJoin(resultVector, " "); |
194 | 0 | } |
195 | | |
196 | | std::string cmExportBuildCMakeConfigGenerator::GetFileSetFiles( |
197 | | cmGeneratorTarget* gte, cmGeneratorFileSet const* fileSet, |
198 | | cmTargetExport const* /*te*/) |
199 | 0 | { |
200 | 0 | std::vector<std::string> resultVector; |
201 | |
|
202 | 0 | auto configs = |
203 | 0 | gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig); |
204 | |
|
205 | 0 | for (auto const& config : configs) { |
206 | 0 | cm::GenEx::Context context(gte->LocalGenerator, config); |
207 | |
|
208 | 0 | auto files = fileSet->GetFiles(context, gte); |
209 | 0 | bool const contextSensitive = files.second; |
210 | |
|
211 | 0 | for (auto const& it : files.first) { |
212 | 0 | for (auto const& filename : it.second) { |
213 | 0 | auto escapedFile = cmOutputConverter::EscapeForCMake( |
214 | 0 | filename, cmOutputConverter::WrapQuotes::NoWrap); |
215 | 0 | if (contextSensitive && configs.size() != 1) { |
216 | 0 | resultVector.push_back( |
217 | 0 | cmStrCat("\"$<$<CONFIG:", config, ">:", escapedFile, ">\"")); |
218 | 0 | } else { |
219 | 0 | resultVector.emplace_back(cmStrCat('"', escapedFile, '"')); |
220 | 0 | } |
221 | 0 | } |
222 | 0 | } |
223 | |
|
224 | 0 | if (!(contextSensitive && configs.size() != 1)) { |
225 | 0 | break; |
226 | 0 | } |
227 | 0 | } |
228 | |
|
229 | 0 | return cmJoin(resultVector, " "); |
230 | 0 | } |
231 | | |
232 | | void cmExportBuildCMakeConfigGenerator::GenerateCxxModuleConfigInformation( |
233 | | std::string const& name, std::ostream& os) const |
234 | 0 | { |
235 | 0 | char const* opt = ""; |
236 | 0 | if (this->Configurations.size() > 1) { |
237 | | // With more than one configuration, each individual file is optional. |
238 | 0 | opt = " OPTIONAL"; |
239 | 0 | } |
240 | | |
241 | | // Generate import file content for each configuration. |
242 | 0 | for (std::string c : this->Configurations) { |
243 | 0 | if (c.empty()) { |
244 | 0 | c = "noconfig"; |
245 | 0 | } |
246 | 0 | os << "include(\"${CMAKE_CURRENT_LIST_DIR}/cxx-modules-" << name << '-' |
247 | 0 | << c << ".cmake\"" << opt << ")\n"; |
248 | 0 | } |
249 | 0 | } |
250 | | |
251 | | bool cmExportBuildCMakeConfigGenerator:: |
252 | | GenerateImportCxxModuleConfigTargetInclusion(std::string const& name, |
253 | | std::string config) const |
254 | 0 | { |
255 | 0 | auto cxx_modules_dirname = this->GetCxxModulesDirectory(); |
256 | 0 | if (cxx_modules_dirname.empty()) { |
257 | 0 | return true; |
258 | 0 | } |
259 | | |
260 | 0 | if (config.empty()) { |
261 | 0 | config = "noconfig"; |
262 | 0 | } |
263 | |
|
264 | 0 | std::string fileName = |
265 | 0 | cmStrCat(this->FileDir, '/', cxx_modules_dirname, "/cxx-modules-", name, |
266 | 0 | '-', config, ".cmake"); |
267 | |
|
268 | 0 | cmGeneratedFileStream os(fileName, true); |
269 | 0 | if (!os) { |
270 | 0 | std::string se = cmSystemTools::GetLastSystemError(); |
271 | 0 | cmSystemTools::Error( |
272 | 0 | cmStrCat("cannot write to file \"", fileName, "\": ", se)); |
273 | 0 | return false; |
274 | 0 | } |
275 | 0 | os.SetCopyIfDifferent(true); |
276 | |
|
277 | 0 | for (auto const* tgt : this->ExportedTargets) { |
278 | | // Only targets with C++ module sources will have a |
279 | | // collator-generated install script. |
280 | 0 | if (!tgt->HaveCxx20ModuleSources()) { |
281 | 0 | continue; |
282 | 0 | } |
283 | | |
284 | 0 | os << "include(\"${CMAKE_CURRENT_LIST_DIR}/target-" |
285 | 0 | << tgt->GetFilesystemExportName() << '-' << config << ".cmake\")\n"; |
286 | 0 | } |
287 | |
|
288 | 0 | return true; |
289 | 0 | } |