/src/CMake/Source/cmExportBuildFileGenerator.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 "cmExportBuildFileGenerator.h" |
4 | | |
5 | | #include <algorithm> |
6 | | #include <memory> |
7 | | #include <set> |
8 | | #include <sstream> |
9 | | #include <utility> |
10 | | |
11 | | #include "cmExportSet.h" |
12 | | #include "cmGeneratorExpression.h" |
13 | | #include "cmGeneratorFileSet.h" |
14 | | #include "cmGeneratorFileSets.h" |
15 | | #include "cmGeneratorTarget.h" |
16 | | #include "cmGlobalGenerator.h" |
17 | | #include "cmList.h" |
18 | | #include "cmLocalGenerator.h" |
19 | | #include "cmMakefile.h" |
20 | | #include "cmStateTypes.h" |
21 | | #include "cmStringAlgorithms.h" |
22 | | #include "cmTarget.h" |
23 | | #include "cmTargetExport.h" |
24 | | #include "cmValue.h" |
25 | | |
26 | | class cmSourceFile; |
27 | | |
28 | | cmExportBuildFileGenerator::cmExportBuildFileGenerator() |
29 | 0 | { |
30 | 0 | this->LG = nullptr; |
31 | 0 | this->ExportSet = nullptr; |
32 | 0 | } |
33 | | |
34 | | void cmExportBuildFileGenerator::Compute(cmLocalGenerator* lg) |
35 | 0 | { |
36 | 0 | this->LG = lg; |
37 | 0 | if (this->ExportSet) { |
38 | 0 | this->ExportSet->Compute(lg); |
39 | 0 | } |
40 | 0 | } |
41 | | |
42 | | cmStateEnums::TargetType cmExportBuildFileGenerator::GetExportTargetType( |
43 | | cmGeneratorTarget const* target) const |
44 | 0 | { |
45 | 0 | cmStateEnums::TargetType targetType = target->GetType(); |
46 | | // An object library exports as an interface library if we cannot |
47 | | // tell clients where to find the objects. This is sufficient |
48 | | // to support transitive usage requirements on other targets that |
49 | | // use the object library. |
50 | 0 | if (targetType == cmStateEnums::OBJECT_LIBRARY && |
51 | 0 | !target->Target->HasKnownObjectFileLocation(nullptr)) { |
52 | 0 | targetType = cmStateEnums::INTERFACE_LIBRARY; |
53 | 0 | } |
54 | 0 | return targetType; |
55 | 0 | } |
56 | | |
57 | | void cmExportBuildFileGenerator::SetExportSet(cmExportSet* exportSet) |
58 | 0 | { |
59 | 0 | this->ExportSet = exportSet; |
60 | 0 | } |
61 | | |
62 | | void cmExportBuildFileGenerator::SetImportLocationProperty( |
63 | | std::string const& config, std::string const& suffix, |
64 | | cmGeneratorTarget* target, ImportPropertyMap& properties) |
65 | 0 | { |
66 | | // Get the makefile in which to lookup target information. |
67 | 0 | cmMakefile* mf = target->Makefile; |
68 | |
|
69 | 0 | if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) { |
70 | 0 | std::string prop = cmStrCat("IMPORTED_OBJECTS", suffix); |
71 | | |
72 | | // Compute all the object files inside this target and setup |
73 | | // IMPORTED_OBJECTS as a list of object files |
74 | 0 | std::vector<cmSourceFile const*> objectSources; |
75 | 0 | target->GetObjectSources(objectSources, config); |
76 | 0 | std::string const obj_dir = target->GetObjectDirectory(config); |
77 | 0 | std::vector<std::string> objects; |
78 | 0 | for (cmSourceFile const* sf : objectSources) { |
79 | 0 | std::string const& obj = target->GetObjectName(sf); |
80 | 0 | objects.push_back(obj_dir + obj); |
81 | 0 | } |
82 | | |
83 | | // Store the property. |
84 | 0 | properties[prop] = cmList::to_string(objects); |
85 | 0 | } else { |
86 | | // Add the main target file. |
87 | 0 | { |
88 | 0 | std::string prop = cmStrCat("IMPORTED_LOCATION", suffix); |
89 | 0 | std::string value; |
90 | 0 | if (target->IsAppBundleOnApple()) { |
91 | 0 | value = |
92 | 0 | target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact); |
93 | 0 | } else { |
94 | 0 | value = target->GetFullPath(config, |
95 | 0 | cmStateEnums::RuntimeBinaryArtifact, true); |
96 | 0 | } |
97 | 0 | properties[prop] = value; |
98 | 0 | } |
99 | | |
100 | | // Add the import library for windows DLLs. |
101 | 0 | if (target->HasImportLibrary(config)) { |
102 | 0 | std::string prop = cmStrCat("IMPORTED_IMPLIB", suffix); |
103 | 0 | std::string value = |
104 | 0 | target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact, true); |
105 | 0 | if (mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) { |
106 | 0 | target->GetImplibGNUtoMS(config, value, value, |
107 | 0 | "${CMAKE_IMPORT_LIBRARY_SUFFIX}"); |
108 | 0 | } |
109 | 0 | properties[prop] = value; |
110 | 0 | } |
111 | 0 | } |
112 | 0 | } |
113 | | |
114 | | bool cmExportBuildFileGenerator::CollectExports( |
115 | | std::function<void(cmGeneratorTarget const*)> visitor) |
116 | 0 | { |
117 | 0 | auto pred = [&](cmExportBuildFileGenerator::TargetExport& tei) -> bool { |
118 | 0 | cmGeneratorTarget* te = this->LG->FindGeneratorTargetToUse(tei.Name); |
119 | 0 | if (this->ExportedTargets.insert(te).second) { |
120 | 0 | this->Exports.emplace_back(te, tei.XcFrameworkLocation); |
121 | 0 | visitor(te); |
122 | 0 | return true; |
123 | 0 | } |
124 | | |
125 | 0 | this->ComplainAboutDuplicateTarget(te->GetName()); |
126 | 0 | return false; |
127 | 0 | }; |
128 | |
|
129 | 0 | std::vector<TargetExport> targets; |
130 | 0 | this->GetTargets(targets); |
131 | 0 | return std::all_of(targets.begin(), targets.end(), pred); |
132 | 0 | } |
133 | | |
134 | | void cmExportBuildFileGenerator::HandleMissingTarget( |
135 | | std::string& link_libs, cmGeneratorTarget const* depender, |
136 | | cmGeneratorTarget* dependee) |
137 | 0 | { |
138 | | // The target is not in the export. |
139 | 0 | if (!this->AppendMode) { |
140 | 0 | auto const& exportInfo = this->FindExportInfo(dependee); |
141 | |
|
142 | 0 | if (exportInfo.Namespaces.size() == 1 && exportInfo.Sets.size() == 1) { |
143 | 0 | std::string missingTarget = *exportInfo.Namespaces.begin(); |
144 | |
|
145 | 0 | missingTarget += dependee->GetExportName(); |
146 | 0 | link_libs += missingTarget; |
147 | 0 | this->MissingTargets.emplace_back(std::move(missingTarget)); |
148 | 0 | return; |
149 | 0 | } |
150 | | // We are not appending, so all exported targets should be |
151 | | // known here. This is probably user-error. |
152 | 0 | this->ComplainAboutMissingTarget(depender, dependee, exportInfo); |
153 | 0 | } |
154 | | // Assume the target will be exported by another command. |
155 | | // Append it with the export namespace. |
156 | 0 | link_libs += this->Namespace; |
157 | 0 | link_libs += dependee->GetExportName(); |
158 | 0 | } |
159 | | |
160 | | void cmExportBuildFileGenerator::GetTargets( |
161 | | std::vector<TargetExport>& targets) const |
162 | 0 | { |
163 | 0 | if (this->ExportSet) { |
164 | 0 | for (std::unique_ptr<cmTargetExport> const& te : |
165 | 0 | this->ExportSet->GetTargetExports()) { |
166 | 0 | if (te->NamelinkOnly) { |
167 | 0 | continue; |
168 | 0 | } |
169 | 0 | targets.emplace_back(te->TargetName, te->XcFrameworkLocation); |
170 | 0 | } |
171 | 0 | return; |
172 | 0 | } |
173 | 0 | targets = this->Targets; |
174 | 0 | } |
175 | | |
176 | | cmExportFileGenerator::ExportInfo cmExportBuildFileGenerator::FindExportInfo( |
177 | | cmGeneratorTarget const* target) const |
178 | 0 | { |
179 | 0 | return target->GetLocalGenerator() |
180 | 0 | ->GetGlobalGenerator() |
181 | 0 | ->FindBuildExportInfo(target); |
182 | 0 | } |
183 | | |
184 | | cm::optional<cmExportBuildFileGenerator::ExportRecord> |
185 | | cmExportBuildFileGenerator::FindRecordForTarget( |
186 | | cmGeneratorTarget const* target) const |
187 | 0 | { |
188 | 0 | auto const& name = target->GetName(); |
189 | 0 | std::vector<TargetExport> targets; |
190 | 0 | this->GetTargets(targets); |
191 | 0 | bool const contains = |
192 | 0 | std::any_of(targets.begin(), targets.end(), |
193 | 0 | [&name](TargetExport const& te) { return te.Name == name; }); |
194 | 0 | cm::optional<ExportRecord> result; |
195 | 0 | if (contains) { |
196 | 0 | ExportRecord rec; |
197 | 0 | rec.Name = this->ExportSet ? this->ExportSet->GetName() : std::string{}; |
198 | 0 | rec.Namespace = this->GetNamespace(); |
199 | 0 | result = rec; |
200 | 0 | } |
201 | 0 | return result; |
202 | 0 | } |
203 | | |
204 | | void cmExportBuildFileGenerator::ComplainAboutMissingTarget( |
205 | | cmGeneratorTarget const* depender, cmGeneratorTarget const* dependee, |
206 | | ExportInfo const& exportInfo) const |
207 | 0 | { |
208 | 0 | std::ostringstream e; |
209 | 0 | e << "export called with target \"" << depender->GetName() |
210 | 0 | << "\" which requires target \"" << dependee->GetName() << "\" "; |
211 | 0 | if (exportInfo.Sets.empty()) { |
212 | 0 | e << "that is not in any export set."; |
213 | 0 | } else { |
214 | 0 | if (exportInfo.Sets.size() == 1) { |
215 | 0 | e << "that is not in this export set, but in another export set which " |
216 | 0 | "is " |
217 | 0 | "exported multiple times with different namespaces: "; |
218 | 0 | } else { |
219 | 0 | e << "that is not in this export set, but in multiple other export " |
220 | 0 | "sets: "; |
221 | 0 | } |
222 | 0 | e << cmJoin(exportInfo.Files, ", ") << ".\n" |
223 | 0 | << "An exported target cannot depend upon another target which is " |
224 | 0 | "exported in more than one export set or with more than one " |
225 | 0 | "namespace. Consider consolidating the exports of the \"" |
226 | 0 | << dependee->GetName() << "\" target to a single export."; |
227 | 0 | } |
228 | |
|
229 | 0 | this->ReportError(e.str()); |
230 | 0 | } |
231 | | |
232 | | void cmExportBuildFileGenerator::ComplainAboutDuplicateTarget( |
233 | | std::string const& targetName) const |
234 | 0 | { |
235 | 0 | std::ostringstream e; |
236 | 0 | e << "given target \"" << targetName << "\" more than once."; |
237 | 0 | this->ReportError(e.str()); |
238 | 0 | } |
239 | | |
240 | | void cmExportBuildFileGenerator::IssueMessage(MessageType type, |
241 | | std::string const& message) const |
242 | 0 | { |
243 | 0 | this->LG->GetMakefile()->IssueMessage(type, message); |
244 | 0 | } |
245 | | |
246 | | void cmExportBuildFileGenerator::IssueDiagnostic( |
247 | | cmDiagnosticCategory category, std::string const& message) const |
248 | 0 | { |
249 | 0 | this->LG->GetMakefile()->IssueDiagnostic(category, message); |
250 | 0 | } |
251 | | |
252 | | std::string cmExportBuildFileGenerator::InstallNameDir( |
253 | | cmGeneratorTarget const* target, std::string const& config) |
254 | 0 | { |
255 | 0 | std::string install_name_dir; |
256 | |
|
257 | 0 | cmMakefile* mf = target->Target->GetMakefile(); |
258 | 0 | if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) { |
259 | 0 | install_name_dir = target->GetInstallNameDirForBuildTree(config); |
260 | 0 | } |
261 | |
|
262 | 0 | return install_name_dir; |
263 | 0 | } |
264 | | |
265 | | bool cmExportBuildFileGenerator::PopulateInterfaceProperties( |
266 | | cmGeneratorTarget const* target, ImportPropertyMap& properties) |
267 | 0 | { |
268 | 0 | this->PopulateInterfaceProperty("INTERFACE_INCLUDE_DIRECTORIES", target, |
269 | 0 | cmGeneratorExpression::BuildInterface, |
270 | 0 | properties); |
271 | 0 | this->PopulateInterfaceProperty("INTERFACE_LINK_DIRECTORIES", target, |
272 | 0 | cmGeneratorExpression::BuildInterface, |
273 | 0 | properties); |
274 | 0 | this->PopulateInterfaceProperty("INTERFACE_LINK_DEPENDS", target, |
275 | 0 | cmGeneratorExpression::BuildInterface, |
276 | 0 | properties); |
277 | 0 | this->PopulateInterfaceProperty("INTERFACE_SOURCES", target, |
278 | 0 | cmGeneratorExpression::BuildInterface, |
279 | 0 | properties); |
280 | |
|
281 | 0 | return this->PopulateInterfaceProperties( |
282 | 0 | target, {}, cmGeneratorExpression::BuildInterface, properties); |
283 | 0 | } |
284 | | |
285 | | bool cmExportBuildFileGenerator::PopulateFileSetInterfaceProperties( |
286 | | cmGeneratorTarget const* target, ImportFileSetPropertyMap& properties) |
287 | 0 | { |
288 | 0 | cmGeneratorFileSets const* const gfs = target->GetGeneratorFileSets(); |
289 | 0 | bool result = true; |
290 | |
|
291 | 0 | for (auto const& type : gfs->GetInterfaceFileSetTypes()) { |
292 | 0 | for (auto const* fileSet : gfs->GetInterfaceFileSets(type)) { |
293 | 0 | ImportPropertyMap& fsProperties = properties[fileSet->GetName()]; |
294 | 0 | this->PopulateFileSetInterfaceProperty( |
295 | 0 | "INTERFACE_INCLUDE_DIRECTORIES", target, fileSet, |
296 | 0 | cmGeneratorExpression::BuildInterface, fsProperties); |
297 | 0 | result = result && |
298 | 0 | this->PopulateFileSetInterfaceProperties( |
299 | 0 | target, fileSet, cmGeneratorExpression::InstallInterface, |
300 | 0 | fsProperties); |
301 | 0 | } |
302 | 0 | } |
303 | 0 | return result; |
304 | 0 | } |