/src/CMake/Source/cmExportInstallPackageInfoGenerator.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 "cmExportInstallPackageInfoGenerator.h" |
4 | | |
5 | | #include <functional> |
6 | | #include <map> |
7 | | #include <memory> |
8 | | #include <set> |
9 | | #include <utility> |
10 | | #include <vector> |
11 | | |
12 | | #include <cm/optional> |
13 | | #include <cm/string_view> |
14 | | #include <cmext/algorithm> |
15 | | #include <cmext/string_view> |
16 | | |
17 | | #include <cm3p/json/value.h> |
18 | | |
19 | | #include "cmAlgorithms.h" |
20 | | #include "cmExportSet.h" |
21 | | #include "cmFileSetMetadata.h" |
22 | | #include "cmGeneratorExpression.h" |
23 | | #include "cmGeneratorFileSet.h" |
24 | | #include "cmGeneratorTarget.h" |
25 | | #include "cmInstallExportGenerator.h" |
26 | | #include "cmInstallFileSetGenerator.h" |
27 | | #include "cmList.h" |
28 | | #include "cmLocalGenerator.h" |
29 | | #include "cmMakefile.h" |
30 | | #include "cmMessageType.h" |
31 | | #include "cmOutputConverter.h" |
32 | | #include "cmPackageInfoArguments.h" |
33 | | #include "cmStateTypes.h" |
34 | | #include "cmStringAlgorithms.h" |
35 | | #include "cmSystemTools.h" |
36 | | #include "cmTarget.h" |
37 | | #include "cmTargetExport.h" |
38 | | |
39 | | cmExportInstallPackageInfoGenerator::cmExportInstallPackageInfoGenerator( |
40 | | cmInstallExportGenerator* iegen, cmPackageInfoArguments arguments) |
41 | 0 | : cmExportPackageInfoGenerator(std::move(arguments)) |
42 | 0 | , cmExportInstallFileGenerator(iegen) |
43 | 0 | { |
44 | 0 | } Unexecuted instantiation: cmExportInstallPackageInfoGenerator::cmExportInstallPackageInfoGenerator(cmInstallExportGenerator*, cmPackageInfoArguments) Unexecuted instantiation: cmExportInstallPackageInfoGenerator::cmExportInstallPackageInfoGenerator(cmInstallExportGenerator*, cmPackageInfoArguments) |
45 | | |
46 | | std::string cmExportInstallPackageInfoGenerator::GetConfigImportFileGlob() |
47 | | const |
48 | 0 | { |
49 | 0 | std::string glob = cmStrCat(this->FileBase, "@*", this->FileExt); |
50 | 0 | return glob; |
51 | 0 | } |
52 | | |
53 | | std::string const& cmExportInstallPackageInfoGenerator::GetExportName() const |
54 | 0 | { |
55 | 0 | return this->GetPackageName(); |
56 | 0 | } |
57 | | |
58 | | bool cmExportInstallPackageInfoGenerator::GenerateMainFile(std::ostream& os) |
59 | 0 | { |
60 | 0 | std::vector<cmTargetExport const*> allTargets; |
61 | 0 | { |
62 | 0 | auto visitor = [&](cmTargetExport const* te) { allTargets.push_back(te); }; |
63 | |
|
64 | 0 | if (!this->CollectExports(visitor)) { |
65 | 0 | return false; |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | 0 | if (!this->CheckPackage()) { |
70 | 0 | return false; |
71 | 0 | } |
72 | | |
73 | 0 | Json::Value root = this->GeneratePackageInfo(); |
74 | 0 | Json::Value& components = root["components"]; |
75 | | |
76 | | // Compute the relative import prefix for the file |
77 | 0 | std::string const& packagePath = this->GenerateImportPrefix(); |
78 | 0 | if (packagePath.empty()) { |
79 | 0 | return false; |
80 | 0 | } |
81 | 0 | root["cps_path"] = packagePath; |
82 | | |
83 | | // Create all the imported targets. |
84 | 0 | for (cmTargetExport const* te : allTargets) { |
85 | 0 | cmGeneratorTarget* gt = te->Target; |
86 | 0 | cmStateEnums::TargetType targetType = this->GetExportTargetType(te); |
87 | |
|
88 | 0 | Json::Value* const component = |
89 | 0 | this->GenerateImportTarget(components, gt, targetType); |
90 | 0 | if (!component) { |
91 | 0 | return false; |
92 | 0 | } |
93 | | |
94 | 0 | ImportPropertyMap properties; |
95 | 0 | if (!this->PopulateInterfaceProperties(te, properties)) { |
96 | 0 | return false; |
97 | 0 | } |
98 | 0 | this->PopulateInterfaceLinkLibrariesProperty( |
99 | 0 | gt, cmGeneratorExpression::InstallInterface, properties); |
100 | |
|
101 | 0 | if (targetType != cmStateEnums::INTERFACE_LIBRARY) { |
102 | 0 | this->RequiresConfigFiles = true; |
103 | 0 | } |
104 | | |
105 | | // De-duplicate include directories prior to generation. |
106 | 0 | auto it = properties.find("INTERFACE_INCLUDE_DIRECTORIES"); |
107 | 0 | if (it != properties.end()) { |
108 | 0 | auto list = cmList{ it->second }; |
109 | 0 | list = cmList{ list.begin(), cmRemoveDuplicates(list) }; |
110 | 0 | properties["INTERFACE_INCLUDE_DIRECTORIES"] = list.to_string(); |
111 | 0 | } |
112 | | |
113 | | // Set configuration-agnostic properties for component. |
114 | 0 | this->GenerateInterfaceProperties(*component, gt, properties); |
115 | 0 | if (!this->GenerateFileSetProperties(*component, gt, te, packagePath)) { |
116 | 0 | return false; |
117 | 0 | } |
118 | 0 | } |
119 | | |
120 | 0 | this->GeneratePackageRequires(root); |
121 | | |
122 | | // Write the primary packing information file. |
123 | 0 | this->WritePackageInfo(root, os); |
124 | |
|
125 | 0 | bool result = true; |
126 | | |
127 | | // Generate an import file for each configuration. |
128 | 0 | if (this->RequiresConfigFiles) { |
129 | 0 | for (std::string const& c : this->Configurations) { |
130 | 0 | if (!this->GenerateImportFileConfig(c)) { |
131 | 0 | result = false; |
132 | 0 | } |
133 | 0 | } |
134 | 0 | } |
135 | |
|
136 | 0 | return result; |
137 | 0 | } |
138 | | |
139 | | void cmExportInstallPackageInfoGenerator::GenerateImportTargetsConfig( |
140 | | std::ostream& os, std::string const& config, std::string const& suffix) |
141 | 0 | { |
142 | 0 | Json::Value root; |
143 | 0 | root["name"] = this->GetPackageName(); |
144 | 0 | root["configuration"] = (config.empty() ? "noconfig" : config); |
145 | |
|
146 | 0 | std::string const& packagePath = this->GenerateImportPrefix(); |
147 | |
|
148 | 0 | Json::Value& components = root["components"]; |
149 | |
|
150 | 0 | for (auto const& te : this->GetExportSet()->GetTargetExports()) { |
151 | | // Collect import properties for this target. |
152 | 0 | ImportPropertyMap properties; |
153 | 0 | std::set<std::string> importedLocations; |
154 | |
|
155 | 0 | if (this->GetExportTargetType(te.get()) != |
156 | 0 | cmStateEnums::INTERFACE_LIBRARY) { |
157 | 0 | this->PopulateImportProperties(config, suffix, te.get(), properties, |
158 | 0 | importedLocations); |
159 | 0 | } |
160 | |
|
161 | 0 | Json::Value component = |
162 | 0 | this->GenerateInterfaceConfigProperties(suffix, properties); |
163 | 0 | this->GenerateFileSetProperties(component, te->Target, te.get(), |
164 | 0 | packagePath, config); |
165 | |
|
166 | 0 | if (!component.empty()) { |
167 | 0 | components[te->Target->GetExportName()] = std::move(component); |
168 | 0 | } |
169 | 0 | } |
170 | |
|
171 | 0 | this->WritePackageInfo(root, os); |
172 | 0 | } |
173 | | |
174 | | std::string cmExportInstallPackageInfoGenerator::GenerateImportPrefix() const |
175 | 0 | { |
176 | 0 | std::string expDest = this->IEGen->GetDestination(); |
177 | 0 | if (cmSystemTools::FileIsFullPath(expDest)) { |
178 | 0 | std::string const& installPrefix = |
179 | 0 | this->IEGen->GetLocalGenerator()->GetMakefile()->GetSafeDefinition( |
180 | 0 | "CMAKE_INSTALL_PREFIX"); |
181 | 0 | if (cmHasPrefix(expDest, installPrefix)) { |
182 | 0 | auto n = installPrefix.length(); |
183 | 0 | while (n < expDest.length() && expDest[n] == '/') { |
184 | 0 | ++n; |
185 | 0 | } |
186 | 0 | expDest = expDest.substr(n); |
187 | 0 | } else { |
188 | 0 | this->ReportError( |
189 | 0 | cmStrCat("install(PACKAGE_INFO \"", this->GetExportName(), |
190 | 0 | "\" ...) specifies DESTINATION \"", expDest, |
191 | 0 | "\" which is not a subdirectory of the install prefix.")); |
192 | 0 | return {}; |
193 | 0 | } |
194 | 0 | } |
195 | | |
196 | 0 | if (expDest.empty()) { |
197 | 0 | return this->GetInstallPrefix(); |
198 | 0 | } |
199 | 0 | return cmStrCat(this->GetImportPrefixWithSlash(), expDest); |
200 | 0 | } |
201 | | |
202 | | std::string cmExportInstallPackageInfoGenerator::InstallNameDir( |
203 | | cmGeneratorTarget const* target, std::string const& config) |
204 | 0 | { |
205 | 0 | std::string install_name_dir; |
206 | |
|
207 | 0 | cmMakefile* mf = target->Target->GetMakefile(); |
208 | 0 | if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) { |
209 | 0 | install_name_dir = |
210 | 0 | target->GetInstallNameDirForInstallTree(config, "@prefix@"); |
211 | 0 | } |
212 | |
|
213 | 0 | return install_name_dir; |
214 | 0 | } |
215 | | |
216 | | std::string cmExportInstallPackageInfoGenerator::GetCxxModulesDirectory() const |
217 | 0 | { |
218 | 0 | return IEGen->GetCxxModuleDirectory(); |
219 | 0 | } |
220 | | |
221 | | cm::optional<std::string> |
222 | | cmExportInstallPackageInfoGenerator::GetFileSetDirectory( |
223 | | cmGeneratorTarget* gte, cmTargetExport const* te, |
224 | | cmGeneratorFileSet const* fileSet, cm::optional<std::string> const& config) |
225 | 0 | { |
226 | 0 | cmInstallFileSetGenerator::DestinationContext result = |
227 | 0 | te->FileSetGenerators.at(fileSet->GetName()) |
228 | 0 | ->GetDestination(gte, config.value_or("")); |
229 | |
|
230 | 0 | if (config && !result.HadContextSensitiveCondition) { |
231 | 0 | return {}; |
232 | 0 | } |
233 | 0 | if (!config && result.HadContextSensitiveCondition) { |
234 | 0 | this->RequiresConfigFiles = true; |
235 | 0 | return {}; |
236 | 0 | } |
237 | | |
238 | 0 | cm::optional<std::string> dest = cmOutputConverter::EscapeForCMake( |
239 | 0 | result.UnescapedDestination, cmOutputConverter::WrapQuotes::NoWrap); |
240 | |
|
241 | 0 | if (!cmSystemTools::FileIsFullPath(result.UnescapedDestination)) { |
242 | 0 | dest = cmStrCat("@prefix@/"_s, *dest); |
243 | 0 | } |
244 | |
|
245 | 0 | return dest; |
246 | 0 | } |
247 | | |
248 | | bool cmExportInstallPackageInfoGenerator::GenerateFileSetProperties( |
249 | | Json::Value& component, cmGeneratorTarget* gte, cmTargetExport const* te, |
250 | | std::string const& packagePath, cm::optional<std::string> config) |
251 | 0 | { |
252 | 0 | bool hasModules = false; |
253 | 0 | std::set<std::string> seenIncludeDirectories; |
254 | 0 | for (auto const& name : gte->Target->GetAllInterfaceFileSets()) { |
255 | 0 | cmGeneratorFileSet const* fileSet = gte->GetFileSet(name); |
256 | |
|
257 | 0 | if (!fileSet) { |
258 | 0 | gte->Makefile->IssueMessage( |
259 | 0 | MessageType::FATAL_ERROR, |
260 | 0 | cmStrCat("File set \"", name, |
261 | 0 | "\" is listed in interface file sets of ", gte->GetName(), |
262 | 0 | " but has not been created")); |
263 | 0 | return false; |
264 | 0 | } |
265 | | |
266 | 0 | cm::optional<std::string> const& fileSetDirectory = |
267 | 0 | this->GetFileSetDirectory(gte, te, fileSet, config); |
268 | |
|
269 | 0 | if (fileSet->GetType() == cm::FileSetMetadata::HEADERS) { |
270 | 0 | if (fileSetDirectory && |
271 | 0 | !cm::contains(seenIncludeDirectories, *fileSetDirectory)) { |
272 | 0 | component["includes"].append(*fileSetDirectory); |
273 | 0 | seenIncludeDirectories.insert(*fileSetDirectory); |
274 | 0 | } |
275 | 0 | } else if (fileSet->GetType() == cm::FileSetMetadata::CXX_MODULES) { |
276 | 0 | hasModules = true; |
277 | 0 | this->RequiresConfigFiles = true; |
278 | 0 | } |
279 | 0 | } |
280 | | |
281 | 0 | if (hasModules && config) { |
282 | 0 | std::string const manifestPath = |
283 | 0 | this->GenerateCxxModules(component, gte, packagePath, *config); |
284 | 0 | if (!manifestPath.empty()) { |
285 | 0 | this->ConfigCxxModuleFiles[*config] = |
286 | 0 | cmStrCat(this->FileDir, '/', manifestPath); |
287 | 0 | } |
288 | 0 | } |
289 | 0 | return true; |
290 | 0 | } |