/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 <map> |
7 | | #include <memory> |
8 | | #include <set> |
9 | | #include <sstream> |
10 | | #include <utility> |
11 | | |
12 | | #include "cmExportSet.h" |
13 | | #include "cmGeneratorExpression.h" |
14 | | #include "cmGeneratorTarget.h" |
15 | | #include "cmGlobalGenerator.h" |
16 | | #include "cmList.h" |
17 | | #include "cmLocalGenerator.h" |
18 | | #include "cmMakefile.h" |
19 | | #include "cmStateTypes.h" |
20 | | #include "cmStringAlgorithms.h" |
21 | | #include "cmTarget.h" |
22 | | #include "cmTargetExport.h" |
23 | | #include "cmValue.h" |
24 | | #include "cmake.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 | std::vector<std::string> exportFiles; |
180 | 0 | std::set<std::string> exportSets; |
181 | 0 | std::set<std::string> namespaces; |
182 | |
|
183 | 0 | auto const& name = target->GetName(); |
184 | 0 | auto& allExportSets = |
185 | 0 | target->GetLocalGenerator()->GetGlobalGenerator()->GetBuildExportSets(); |
186 | |
|
187 | 0 | for (auto const& exp : allExportSets) { |
188 | 0 | cmExportBuildFileGenerator const* const bfg = exp.second; |
189 | 0 | cmExportSet const* const exportSet = bfg->GetExportSet(); |
190 | 0 | std::vector<TargetExport> targets; |
191 | 0 | bfg->GetTargets(targets); |
192 | 0 | if (std::any_of( |
193 | 0 | targets.begin(), targets.end(), |
194 | 0 | [&name](TargetExport const& te) { return te.Name == name; })) { |
195 | 0 | if (exportSet) { |
196 | 0 | exportSets.insert(exportSet->GetName()); |
197 | 0 | } else { |
198 | 0 | exportSets.insert(exp.first); |
199 | 0 | } |
200 | 0 | exportFiles.push_back(exp.first); |
201 | 0 | namespaces.insert(bfg->GetNamespace()); |
202 | 0 | } |
203 | 0 | } |
204 | |
|
205 | 0 | return { exportFiles, exportSets, namespaces }; |
206 | 0 | } |
207 | | |
208 | | void cmExportBuildFileGenerator::ComplainAboutMissingTarget( |
209 | | cmGeneratorTarget const* depender, cmGeneratorTarget const* dependee, |
210 | | ExportInfo const& exportInfo) const |
211 | 0 | { |
212 | 0 | std::ostringstream e; |
213 | 0 | e << "export called with target \"" << depender->GetName() |
214 | 0 | << "\" which requires target \"" << dependee->GetName() << "\" "; |
215 | 0 | if (exportInfo.Sets.empty()) { |
216 | 0 | e << "that is not in any export set."; |
217 | 0 | } else { |
218 | 0 | if (exportInfo.Sets.size() == 1) { |
219 | 0 | e << "that is not in this export set, but in another export set which " |
220 | 0 | "is " |
221 | 0 | "exported multiple times with different namespaces: "; |
222 | 0 | } else { |
223 | 0 | e << "that is not in this export set, but in multiple other export " |
224 | 0 | "sets: "; |
225 | 0 | } |
226 | 0 | e << cmJoin(exportInfo.Files, ", ") << ".\n" |
227 | 0 | << "An exported target cannot depend upon another target which is " |
228 | 0 | "exported in more than one export set or with more than one " |
229 | 0 | "namespace. Consider consolidating the exports of the \"" |
230 | 0 | << dependee->GetName() << "\" target to a single export."; |
231 | 0 | } |
232 | |
|
233 | 0 | this->ReportError(e.str()); |
234 | 0 | } |
235 | | |
236 | | void cmExportBuildFileGenerator::ComplainAboutDuplicateTarget( |
237 | | std::string const& targetName) const |
238 | 0 | { |
239 | 0 | std::ostringstream e; |
240 | 0 | e << "given target \"" << targetName << "\" more than once."; |
241 | 0 | this->ReportError(e.str()); |
242 | 0 | } |
243 | | |
244 | | void cmExportBuildFileGenerator::IssueMessage(MessageType type, |
245 | | std::string const& message) const |
246 | 0 | { |
247 | 0 | this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage( |
248 | 0 | type, message, this->LG->GetMakefile()->GetBacktrace()); |
249 | 0 | } |
250 | | |
251 | | std::string cmExportBuildFileGenerator::InstallNameDir( |
252 | | cmGeneratorTarget const* target, std::string const& config) |
253 | 0 | { |
254 | 0 | std::string install_name_dir; |
255 | |
|
256 | 0 | cmMakefile* mf = target->Target->GetMakefile(); |
257 | 0 | if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) { |
258 | 0 | install_name_dir = target->GetInstallNameDirForBuildTree(config); |
259 | 0 | } |
260 | |
|
261 | 0 | return install_name_dir; |
262 | 0 | } |
263 | | |
264 | | bool cmExportBuildFileGenerator::PopulateInterfaceProperties( |
265 | | cmGeneratorTarget const* target, ImportPropertyMap& properties) |
266 | 0 | { |
267 | 0 | this->PopulateInterfaceProperty("INTERFACE_INCLUDE_DIRECTORIES", target, |
268 | 0 | cmGeneratorExpression::BuildInterface, |
269 | 0 | properties); |
270 | 0 | this->PopulateInterfaceProperty("INTERFACE_LINK_DIRECTORIES", target, |
271 | 0 | cmGeneratorExpression::BuildInterface, |
272 | 0 | properties); |
273 | 0 | this->PopulateInterfaceProperty("INTERFACE_LINK_DEPENDS", target, |
274 | 0 | cmGeneratorExpression::BuildInterface, |
275 | 0 | properties); |
276 | 0 | this->PopulateInterfaceProperty("INTERFACE_SOURCES", target, |
277 | 0 | cmGeneratorExpression::BuildInterface, |
278 | 0 | properties); |
279 | |
|
280 | 0 | return this->PopulateInterfaceProperties( |
281 | 0 | target, {}, cmGeneratorExpression::BuildInterface, properties); |
282 | 0 | } |