/src/CMake/Source/cmNinjaNormalTargetGenerator.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 "cmNinjaNormalTargetGenerator.h" |
4 | | |
5 | | #include <algorithm> |
6 | | #include <iterator> |
7 | | #include <set> |
8 | | #include <sstream> |
9 | | #include <unordered_set> |
10 | | #include <utility> |
11 | | |
12 | | #include <cm/memory> |
13 | | #include <cm/optional> |
14 | | #include <cm/vector> |
15 | | |
16 | | #include "cmComputeLinkInformation.h" |
17 | | #include "cmCustomCommand.h" // IWYU pragma: keep |
18 | | #include "cmCustomCommandGenerator.h" |
19 | | #include "cmGeneratedFileStream.h" |
20 | | #include "cmGeneratorExpression.h" |
21 | | #include "cmGeneratorOptions.h" |
22 | | #include "cmGeneratorTarget.h" |
23 | | #include "cmGlobalNinjaGenerator.h" |
24 | | #include "cmLinkLineComputer.h" |
25 | | #include "cmLinkLineDeviceComputer.h" |
26 | | #include "cmList.h" |
27 | | #include "cmLocalCommonGenerator.h" |
28 | | #include "cmLocalGenerator.h" |
29 | | #include "cmLocalNinjaGenerator.h" |
30 | | #include "cmMakefile.h" |
31 | | #include "cmMessageType.h" |
32 | | #include "cmNinjaLinkLineDeviceComputer.h" |
33 | | #include "cmNinjaTypes.h" |
34 | | #include "cmOSXBundleGenerator.h" |
35 | | #include "cmOutputConverter.h" |
36 | | #include "cmRulePlaceholderExpander.h" |
37 | | #include "cmSourceFile.h" |
38 | | #include "cmState.h" |
39 | | #include "cmStateDirectory.h" |
40 | | #include "cmStateSnapshot.h" |
41 | | #include "cmStateTypes.h" |
42 | | #include "cmStringAlgorithms.h" |
43 | | #include "cmSystemTools.h" |
44 | | #include "cmTarget.h" |
45 | | #include "cmTargetTypes.h" |
46 | | #include "cmUnreachable.h" |
47 | | #include "cmValue.h" |
48 | | |
49 | | cmNinjaNormalTargetGenerator::cmNinjaNormalTargetGenerator( |
50 | | cmGeneratorTarget* target) |
51 | 0 | : cmNinjaTargetGenerator(target) |
52 | 0 | { |
53 | 0 | if (target->GetType() != cm::TargetType::OBJECT_LIBRARY) { |
54 | | // on Windows the output dir is already needed at compile time |
55 | | // ensure the directory exists (OutDir test) |
56 | 0 | for (auto const& config : this->GetConfigNames()) { |
57 | 0 | this->EnsureDirectoryExists(target->GetDirectory(config)); |
58 | 0 | } |
59 | 0 | } |
60 | |
|
61 | 0 | this->OSXBundleGenerator = cm::make_unique<cmOSXBundleGenerator>(target); |
62 | 0 | this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders); |
63 | 0 | } |
64 | | |
65 | 0 | cmNinjaNormalTargetGenerator::~cmNinjaNormalTargetGenerator() = default; |
66 | | |
67 | | void cmNinjaNormalTargetGenerator::Generate(std::string const& config) |
68 | 0 | { |
69 | 0 | if (this->GetGeneratorTarget()->GetType() != |
70 | 0 | cm::TargetType::INTERFACE_LIBRARY) { |
71 | 0 | std::string lang = this->GeneratorTarget->GetLinkerLanguage(config); |
72 | 0 | if (this->TargetLinkLanguage(config).empty()) { |
73 | 0 | cmSystemTools::Error( |
74 | 0 | cmStrCat("CMake can not determine linker language for target: ", |
75 | 0 | this->GetGeneratorTarget()->GetName())); |
76 | 0 | return; |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | | // Write the rules for each language. |
81 | 0 | this->WriteLanguagesRules(config); |
82 | | |
83 | | // Write the build statements |
84 | 0 | bool firstForConfig = true; |
85 | 0 | for (auto const& fileConfig : this->GetConfigNames()) { |
86 | 0 | if (!this->GetGlobalGenerator() |
87 | 0 | ->GetCrossConfigs(fileConfig) |
88 | 0 | .count(config)) { |
89 | 0 | continue; |
90 | 0 | } |
91 | 0 | this->WriteObjectBuildStatements(config, fileConfig, firstForConfig); |
92 | 0 | firstForConfig = false; |
93 | 0 | } |
94 | |
|
95 | 0 | if (this->GetGeneratorTarget()->GetType() == |
96 | 0 | cm::TargetType::OBJECT_LIBRARY) { |
97 | 0 | this->WriteObjectLibStatement(config); |
98 | 0 | } else if (this->GetGeneratorTarget()->GetType() == |
99 | 0 | cm::TargetType::INTERFACE_LIBRARY) { |
100 | 0 | bool haveCxxModuleSources = false; |
101 | 0 | if (this->GetGeneratorTarget()->HaveCxx20ModuleSources()) { |
102 | 0 | haveCxxModuleSources = true; |
103 | 0 | } |
104 | |
|
105 | 0 | if (!haveCxxModuleSources) { |
106 | 0 | cmSystemTools::Error(cmStrCat( |
107 | 0 | "Ninja does not support INTERFACE libraries without C++ module " |
108 | 0 | "sources as a normal target: ", |
109 | 0 | this->GetGeneratorTarget()->GetName())); |
110 | 0 | return; |
111 | 0 | } |
112 | | |
113 | 0 | firstForConfig = true; |
114 | 0 | for (auto const& fileConfig : this->GetConfigNames()) { |
115 | 0 | if (!this->GetGlobalGenerator() |
116 | 0 | ->GetCrossConfigs(fileConfig) |
117 | 0 | .count(config)) { |
118 | 0 | continue; |
119 | 0 | } |
120 | 0 | if (haveCxxModuleSources) { |
121 | 0 | this->WriteCxxModuleLibraryStatement(config, fileConfig, |
122 | 0 | firstForConfig); |
123 | 0 | } |
124 | 0 | firstForConfig = false; |
125 | 0 | } |
126 | 0 | } else { |
127 | 0 | firstForConfig = true; |
128 | 0 | for (auto const& fileConfig : this->GetConfigNames()) { |
129 | 0 | if (!this->GetGlobalGenerator() |
130 | 0 | ->GetCrossConfigs(fileConfig) |
131 | 0 | .count(config)) { |
132 | 0 | continue; |
133 | 0 | } |
134 | | // If this target has cuda language link inputs, and we need to do |
135 | | // device linking |
136 | 0 | this->WriteDeviceLinkStatement(config, fileConfig, firstForConfig); |
137 | 0 | this->WriteLinkStatement(config, fileConfig, firstForConfig); |
138 | 0 | firstForConfig = false; |
139 | 0 | } |
140 | 0 | } |
141 | 0 | if (this->GetGlobalGenerator()->EnableCrossConfigBuild()) { |
142 | 0 | this->GetGlobalGenerator()->AddTargetAlias( |
143 | 0 | this->GetTargetName(), this->GetGeneratorTarget(), "all"); |
144 | 0 | } |
145 | | |
146 | | // Find ADDITIONAL_CLEAN_FILES |
147 | 0 | this->AdditionalCleanFiles(config); |
148 | 0 | } |
149 | | |
150 | | void cmNinjaNormalTargetGenerator::WriteLanguagesRules( |
151 | | std::string const& config) |
152 | 0 | { |
153 | | #ifdef NINJA_GEN_VERBOSE_FILES |
154 | | cmGlobalNinjaGenerator::WriteDivider(this->GetRulesFileStream()); |
155 | | this->GetRulesFileStream() |
156 | | << "# Rules for each language for " |
157 | | << cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()) |
158 | | << " target " << this->GetTargetName() << "\n\n"; |
159 | | #endif |
160 | | |
161 | | // Write rules for languages compiled in this target. |
162 | 0 | { |
163 | 0 | std::set<std::string> languages; |
164 | 0 | std::vector<cmSourceFile const*> sourceFiles; |
165 | 0 | this->GetGeneratorTarget()->GetObjectSources(sourceFiles, config); |
166 | 0 | if (this->HaveRequiredLanguages(sourceFiles, config, languages)) { |
167 | 0 | for (std::string const& language : languages) { |
168 | 0 | this->WriteLanguageRules(language, config); |
169 | 0 | } |
170 | 0 | } |
171 | 0 | } |
172 | | |
173 | | // Write rules for languages in BMI-only rules. |
174 | 0 | { |
175 | 0 | std::set<std::string> languages; |
176 | 0 | std::vector<cmSourceFile const*> sourceFiles; |
177 | 0 | this->GetGeneratorTarget()->GetCxxModuleSources(sourceFiles, config); |
178 | 0 | if (this->HaveRequiredLanguages(sourceFiles, config, languages)) { |
179 | 0 | for (std::string const& language : languages) { |
180 | 0 | this->WriteLanguageRules(language, config); |
181 | 0 | } |
182 | 0 | } |
183 | 0 | } |
184 | 0 | } |
185 | | |
186 | | char const* cmNinjaNormalTargetGenerator::GetVisibleTypeName() const |
187 | 0 | { |
188 | 0 | switch (this->GetGeneratorTarget()->GetType()) { |
189 | 0 | case cm::TargetType::STATIC_LIBRARY: |
190 | 0 | return "static library"; |
191 | 0 | case cm::TargetType::SHARED_LIBRARY: |
192 | 0 | return "shared library"; |
193 | 0 | case cm::TargetType::MODULE_LIBRARY: |
194 | 0 | if (this->GetGeneratorTarget()->IsCFBundleOnApple()) { |
195 | 0 | return "CFBundle shared module"; |
196 | 0 | } else { |
197 | 0 | return "shared module"; |
198 | 0 | } |
199 | 0 | case cm::TargetType::EXECUTABLE: |
200 | 0 | return "executable"; |
201 | 0 | default: |
202 | 0 | return nullptr; |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | | std::string cmNinjaNormalTargetGenerator::LanguageLinkerRule( |
207 | | std::string const& config) const |
208 | 0 | { |
209 | 0 | return cmStrCat( |
210 | 0 | this->TargetLinkLanguage(config), '_', |
211 | 0 | cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()), |
212 | 0 | "_LINKER__", |
213 | 0 | cmGlobalNinjaGenerator::EncodeRuleName( |
214 | 0 | this->GetGeneratorTarget()->GetName()), |
215 | 0 | '_', config); |
216 | 0 | } |
217 | | |
218 | | std::string cmNinjaNormalTargetGenerator::LanguageLinkerDeviceRule( |
219 | | std::string const& config) const |
220 | 0 | { |
221 | 0 | return cmStrCat( |
222 | 0 | this->DeviceLinkLanguage, '_', |
223 | 0 | cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()), |
224 | 0 | "_DEVICE_LINKER__", |
225 | 0 | cmGlobalNinjaGenerator::EncodeRuleName( |
226 | 0 | this->GetGeneratorTarget()->GetName()), |
227 | 0 | '_', config); |
228 | 0 | } |
229 | | |
230 | | std::string cmNinjaNormalTargetGenerator::LanguageLinkerCudaDeviceRule( |
231 | | std::string const& config) const |
232 | 0 | { |
233 | 0 | return cmStrCat( |
234 | 0 | this->TargetLinkLanguage(config), "_DEVICE_LINK__", |
235 | 0 | cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()), |
236 | 0 | '_', config); |
237 | 0 | } |
238 | | |
239 | | std::string cmNinjaNormalTargetGenerator::LanguageLinkerCudaDeviceCompileRule( |
240 | | std::string const& config) const |
241 | 0 | { |
242 | 0 | return cmStrCat( |
243 | 0 | this->TargetLinkLanguage(config), "_DEVICE_LINK_COMPILE__", |
244 | 0 | cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()), |
245 | 0 | '_', config); |
246 | 0 | } |
247 | | |
248 | | std::string cmNinjaNormalTargetGenerator::LanguageLinkerCudaFatbinaryRule( |
249 | | std::string const& config) const |
250 | 0 | { |
251 | 0 | return cmStrCat( |
252 | 0 | this->TargetLinkLanguage(config), "_FATBINARY__", |
253 | 0 | cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()), |
254 | 0 | '_', config); |
255 | 0 | } |
256 | | |
257 | | std::string cmNinjaNormalTargetGenerator::TextStubsGeneratorRule( |
258 | | std::string const& config) const |
259 | 0 | { |
260 | 0 | return cmStrCat( |
261 | 0 | "TEXT_STUBS_GENERATOR__", |
262 | 0 | cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()), |
263 | 0 | '_', config); |
264 | 0 | } |
265 | | |
266 | | bool cmNinjaNormalTargetGenerator::CheckUseResponseFileForLibraries( |
267 | | std::string const& l) const |
268 | 0 | { |
269 | | // Check for an explicit setting one way or the other. |
270 | 0 | std::string const responseVar = |
271 | 0 | cmStrCat("CMAKE_", l, "_USE_RESPONSE_FILE_FOR_LIBRARIES"); |
272 | | |
273 | | // If the option is defined, read it's value |
274 | 0 | if (cmValue val = this->Makefile->GetDefinition(responseVar)) { |
275 | 0 | return val.IsOn(); |
276 | 0 | } |
277 | | |
278 | | // Default to true |
279 | 0 | return true; |
280 | 0 | } |
281 | | |
282 | | struct cmNinjaRemoveNoOpCommands |
283 | | { |
284 | | bool operator()(std::string const& cmd) |
285 | 0 | { |
286 | 0 | return cmd.empty() || cmd[0] == ':'; |
287 | 0 | } |
288 | | }; |
289 | | |
290 | | void cmNinjaNormalTargetGenerator::WriteDriverDeviceLinkRule( |
291 | | bool useResponseFile, std::string const& config) |
292 | 0 | { |
293 | 0 | cmNinjaRule rule(this->LanguageLinkerDeviceRule(config)); |
294 | 0 | if (!this->GetGlobalGenerator()->HasRule(rule.Name)) { |
295 | 0 | cmRulePlaceholderExpander::RuleVariables vars; |
296 | 0 | vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str(); |
297 | 0 | vars.CMTargetType = |
298 | 0 | cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()) |
299 | 0 | .c_str(); |
300 | 0 | vars.Language = this->DeviceLinkLanguage.c_str(); |
301 | 0 | std::string linker = this->GetGeneratorTarget()->GetLinkerTool( |
302 | 0 | this->DeviceLinkLanguage, config); |
303 | 0 | vars.Linker = linker.c_str(); |
304 | | |
305 | | // build response file name |
306 | 0 | std::string responseFlag = this->GetMakefile()->GetSafeDefinition(cmStrCat( |
307 | 0 | "CMAKE_", this->DeviceLinkLanguage, "_RESPONSE_FILE_DEVICE_LINK_FLAG")); |
308 | |
|
309 | 0 | if (!useResponseFile || responseFlag.empty()) { |
310 | 0 | vars.Objects = "$in"; |
311 | 0 | vars.LinkLibraries = "$LINK_PATH $LINK_LIBRARIES"; |
312 | 0 | } else { |
313 | 0 | rule.RspFile = "$RSP_FILE"; |
314 | 0 | responseFlag += rule.RspFile; |
315 | | |
316 | | // build response file content |
317 | 0 | if (this->GetGlobalGenerator()->IsGCCOnWindows()) { |
318 | 0 | rule.RspContent = "$in"; |
319 | 0 | } else { |
320 | 0 | rule.RspContent = "$in_newline"; |
321 | 0 | } |
322 | | |
323 | | // add the link command in the file if necessary |
324 | 0 | if (this->CheckUseResponseFileForLibraries(this->DeviceLinkLanguage)) { |
325 | 0 | rule.RspContent += " $LINK_LIBRARIES"; |
326 | 0 | vars.LinkLibraries = ""; |
327 | 0 | } else { |
328 | 0 | vars.LinkLibraries = "$LINK_PATH $LINK_LIBRARIES"; |
329 | 0 | } |
330 | |
|
331 | 0 | vars.Objects = responseFlag.c_str(); |
332 | 0 | } |
333 | |
|
334 | 0 | vars.ObjectDir = "$OBJECT_DIR"; |
335 | 0 | vars.TargetSupportDir = "$TARGET_SUPPORT_DIR"; |
336 | |
|
337 | 0 | vars.Target = "$TARGET_FILE"; |
338 | |
|
339 | 0 | vars.SONameFlag = "$SONAME_FLAG"; |
340 | 0 | vars.TargetSOName = "$SONAME"; |
341 | 0 | vars.TargetPDB = "$TARGET_PDB"; |
342 | 0 | vars.TargetCompilePDB = "$TARGET_COMPILE_PDB"; |
343 | |
|
344 | 0 | vars.Flags = "$FLAGS"; |
345 | 0 | vars.LinkFlags = "$LINK_FLAGS"; |
346 | 0 | vars.Manifests = "$MANIFESTS"; |
347 | 0 | vars.Config = "$CONFIG"; |
348 | |
|
349 | 0 | vars.LanguageCompileFlags = "$LANGUAGE_COMPILE_FLAGS"; |
350 | |
|
351 | 0 | std::string launcher; |
352 | 0 | std::string val = this->GetLocalGenerator()->GetRuleLauncher( |
353 | 0 | this->GetGeneratorTarget(), "RULE_LAUNCH_LINK", config); |
354 | 0 | if (cmNonempty(val)) { |
355 | 0 | launcher = cmStrCat(val, ' '); |
356 | 0 | } |
357 | |
|
358 | 0 | auto rulePlaceholderExpander = |
359 | 0 | this->GetLocalGenerator()->CreateRulePlaceholderExpander( |
360 | 0 | cmBuildStep::Link); |
361 | | |
362 | | // Rule for linking library/executable. |
363 | 0 | std::vector<std::string> linkCmds = this->ComputeDeviceLinkCmd(); |
364 | 0 | for (std::string& linkCmd : linkCmds) { |
365 | 0 | linkCmd = cmStrCat(launcher, std::move(linkCmd)); |
366 | 0 | rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), |
367 | 0 | linkCmd, vars); |
368 | 0 | } |
369 | | |
370 | | // If there is no ranlib the command will be ":". Skip it. |
371 | 0 | cm::erase_if(linkCmds, cmNinjaRemoveNoOpCommands()); |
372 | |
|
373 | 0 | rule.Command = |
374 | 0 | this->GetLocalGenerator()->BuildCommandLine(linkCmds, config, config); |
375 | | |
376 | | // Write the linker rule with response file if needed. |
377 | 0 | rule.Comment = |
378 | 0 | cmStrCat("Rule for linking ", this->TargetLinkLanguage(config), ' ', |
379 | 0 | this->GetVisibleTypeName(), '.'); |
380 | 0 | rule.Description = |
381 | 0 | cmStrCat("Linking ", this->TargetLinkLanguage(config), ' ', |
382 | 0 | this->GetVisibleTypeName(), " $TARGET_FILE"); |
383 | 0 | rule.Restat = "$RESTAT"; |
384 | |
|
385 | 0 | this->GetGlobalGenerator()->AddRule(rule); |
386 | 0 | } |
387 | 0 | } |
388 | | |
389 | | void cmNinjaNormalTargetGenerator::WriteDeviceLinkRules( |
390 | | std::string const& config) |
391 | 0 | { |
392 | 0 | cmMakefile const* mf = this->GetMakefile(); |
393 | |
|
394 | 0 | cmNinjaRule rule(this->LanguageLinkerCudaDeviceRule(config)); |
395 | 0 | rule.Command = this->GetLocalGenerator()->BuildCommandLine( |
396 | 0 | { cmStrCat(mf->GetRequiredDefinition("CMAKE_CUDA_DEVICE_LINKER"), |
397 | 0 | " -arch=$ARCH $REGISTER -o=$out $in") }, |
398 | 0 | config, config); |
399 | 0 | rule.Comment = "Rule for CUDA device linking."; |
400 | 0 | rule.Description = "Linking CUDA $out"; |
401 | 0 | this->GetGlobalGenerator()->AddRule(rule); |
402 | |
|
403 | 0 | cmRulePlaceholderExpander::RuleVariables vars; |
404 | 0 | vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str(); |
405 | 0 | vars.CMTargetType = |
406 | 0 | cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()).c_str(); |
407 | 0 | vars.Language = "CUDA"; |
408 | 0 | vars.Object = "$out"; |
409 | 0 | vars.Fatbinary = "$FATBIN"; |
410 | 0 | vars.RegisterFile = "$REGISTER"; |
411 | 0 | vars.LinkFlags = "$LINK_FLAGS"; |
412 | 0 | std::string linker = |
413 | 0 | this->GetGeneratorTarget()->GetLinkerTool("CUDA", config); |
414 | 0 | vars.Linker = linker.c_str(); |
415 | |
|
416 | 0 | std::string flags = this->GetFlags("CUDA", config); |
417 | 0 | vars.Flags = flags.c_str(); |
418 | 0 | vars.Config = "$CONFIG"; |
419 | |
|
420 | 0 | std::string compileCmd = this->GetMakefile()->GetRequiredDefinition( |
421 | 0 | "CMAKE_CUDA_DEVICE_LINK_COMPILE"); |
422 | 0 | auto rulePlaceholderExpander = |
423 | 0 | this->GetLocalGenerator()->CreateRulePlaceholderExpander( |
424 | 0 | cmBuildStep::Link); |
425 | 0 | rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), |
426 | 0 | compileCmd, vars); |
427 | |
|
428 | 0 | rule.Name = this->LanguageLinkerCudaDeviceCompileRule(config); |
429 | 0 | rule.Command = this->GetLocalGenerator()->BuildCommandLine({ compileCmd }, |
430 | 0 | config, config); |
431 | 0 | rule.Comment = "Rule for compiling CUDA device stubs."; |
432 | 0 | rule.Description = "Compiling CUDA device stub $out"; |
433 | 0 | this->GetGlobalGenerator()->AddRule(rule); |
434 | |
|
435 | 0 | rule.Name = this->LanguageLinkerCudaFatbinaryRule(config); |
436 | 0 | rule.Command = this->GetLocalGenerator()->BuildCommandLine( |
437 | 0 | { cmStrCat(mf->GetRequiredDefinition("CMAKE_CUDA_FATBINARY"), |
438 | 0 | " -64 -cmdline=--compile-only -compress-all -link " |
439 | 0 | "--embedded-fatbin=$out $PROFILES") }, |
440 | 0 | config, config); |
441 | 0 | rule.Comment = "Rule for CUDA fatbinaries."; |
442 | 0 | rule.Description = "Creating fatbinary $out"; |
443 | 0 | this->GetGlobalGenerator()->AddRule(rule); |
444 | 0 | } |
445 | | |
446 | | static void NinjaSafeComment(std::string& comment) |
447 | 0 | { |
448 | | // Replace control characters in comments. |
449 | 0 | cmSystemTools::ReplaceString(comment, "\n", " / "); |
450 | 0 | cmSystemTools::ReplaceString(comment, "$", "$$"); |
451 | 0 | } |
452 | | |
453 | | void cmNinjaNormalTargetGenerator::WriteLinkRule( |
454 | | bool useResponseFile, std::string const& config, |
455 | | std::vector<std::string> const& preLinkComments, |
456 | | std::vector<std::string> const& postBuildComments, bool hasPreLink, |
457 | | bool hasPostBuild) |
458 | 0 | { |
459 | 0 | cm::TargetType targetType = this->GetGeneratorTarget()->GetType(); |
460 | |
|
461 | 0 | std::string linkRuleName = this->LanguageLinkerRule(config); |
462 | 0 | if (!this->GetGlobalGenerator()->HasRule(linkRuleName)) { |
463 | 0 | cmNinjaRule rule(std::move(linkRuleName)); |
464 | 0 | cmRulePlaceholderExpander::RuleVariables vars; |
465 | 0 | vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str(); |
466 | 0 | vars.CMTargetType = cmState::GetTargetTypeName(targetType).c_str(); |
467 | 0 | std::string linker = this->GetGeneratorTarget()->GetLinkerTool(config); |
468 | 0 | vars.Linker = linker.c_str(); |
469 | 0 | std::string lang = this->TargetLinkLanguage(config); |
470 | 0 | vars.Language = lang.c_str(); |
471 | 0 | vars.AIXExports = "$AIX_EXPORTS"; |
472 | |
|
473 | 0 | if (!this->GetLocalGenerator()->IsSplitSwiftBuild() && |
474 | 0 | this->TargetLinkLanguage(config) == "Swift") { |
475 | 0 | vars.SwiftLibraryName = "$SWIFT_LIBRARY_NAME"; |
476 | 0 | vars.SwiftModule = "$SWIFT_MODULE"; |
477 | 0 | vars.SwiftModuleName = "$SWIFT_MODULE_NAME"; |
478 | 0 | vars.SwiftSources = "$SWIFT_SOURCES"; |
479 | |
|
480 | 0 | vars.Defines = "$DEFINES"; |
481 | 0 | vars.Flags = "$FLAGS"; |
482 | 0 | vars.Includes = "$INCLUDES"; |
483 | 0 | } |
484 | |
|
485 | 0 | if (this->TargetLinkLanguage(config) == "Rust") { |
486 | 0 | vars.RustMainCrateRoot = "$RUST_MAIN_CRATE_ROOT"; |
487 | 0 | vars.RustLinkCrates = "$RUST_LINK_CRATES"; |
488 | 0 | vars.RustNativeObjects = "$RUST_NATIVE_OBJECTS"; |
489 | 0 | } |
490 | |
|
491 | 0 | std::string responseFlag; |
492 | |
|
493 | 0 | std::string cmakeVarLang = |
494 | 0 | cmStrCat("CMAKE_", this->TargetLinkLanguage(config)); |
495 | |
|
496 | 0 | if (this->GeneratorTarget->HasLinkDependencyFile(config)) { |
497 | 0 | auto DepFileFormat = this->GetMakefile()->GetDefinition( |
498 | 0 | cmStrCat(cmakeVarLang, "_LINKER_DEPFILE_FORMAT")); |
499 | 0 | rule.DepType = DepFileFormat; |
500 | 0 | rule.DepFile = "$DEP_FILE"; |
501 | 0 | } |
502 | | |
503 | | // build response file name |
504 | 0 | cmValue flag; |
505 | 0 | if (targetType == cm::TargetType::STATIC_LIBRARY) { |
506 | 0 | std::string cmakeLinkVar = cmakeVarLang + "_RESPONSE_FILE_ARCHIVE_FLAG"; |
507 | 0 | flag = this->GetMakefile()->GetDefinition(cmakeLinkVar); |
508 | 0 | } |
509 | 0 | if (!flag) { |
510 | 0 | std::string cmakeLinkVar = cmakeVarLang + "_RESPONSE_FILE_LINK_FLAG"; |
511 | 0 | flag = this->GetMakefile()->GetDefinition(cmakeLinkVar); |
512 | 0 | } |
513 | |
|
514 | 0 | if (flag) { |
515 | 0 | responseFlag = *flag; |
516 | 0 | } else { |
517 | 0 | responseFlag = "@"; |
518 | 0 | } |
519 | |
|
520 | 0 | if (!useResponseFile || responseFlag.empty()) { |
521 | 0 | vars.Objects = "$in"; |
522 | 0 | vars.LinkLibraries = "$LINK_PATH $LINK_LIBRARIES"; |
523 | 0 | } else { |
524 | 0 | rule.RspFile = "$RSP_FILE"; |
525 | 0 | responseFlag += rule.RspFile; |
526 | | |
527 | | // build response file content |
528 | 0 | if (this->GetGlobalGenerator()->IsGCCOnWindows()) { |
529 | 0 | rule.RspContent = "$in"; |
530 | 0 | } else { |
531 | 0 | rule.RspContent = "$in_newline"; |
532 | 0 | } |
533 | | |
534 | | // If libraries in rsp is enable |
535 | 0 | if (this->CheckUseResponseFileForLibraries(lang)) { |
536 | 0 | rule.RspContent += " $LINK_PATH $LINK_LIBRARIES"; |
537 | 0 | vars.LinkLibraries = ""; |
538 | 0 | } else { |
539 | 0 | vars.LinkLibraries = "$LINK_PATH $LINK_LIBRARIES"; |
540 | 0 | } |
541 | |
|
542 | 0 | if (!this->GetLocalGenerator()->IsSplitSwiftBuild() && |
543 | 0 | this->TargetLinkLanguage(config) == "Swift") { |
544 | 0 | vars.SwiftSources = responseFlag.c_str(); |
545 | 0 | } else { |
546 | 0 | vars.Objects = responseFlag.c_str(); |
547 | 0 | } |
548 | 0 | } |
549 | |
|
550 | 0 | vars.ObjectDir = "$OBJECT_DIR"; |
551 | 0 | vars.TargetSupportDir = "$TARGET_SUPPORT_DIR"; |
552 | |
|
553 | 0 | vars.Target = "$TARGET_FILE"; |
554 | |
|
555 | 0 | vars.SONameFlag = "$SONAME_FLAG"; |
556 | 0 | vars.TargetSOName = "$SONAME"; |
557 | 0 | vars.TargetInstallNameDir = "$INSTALLNAME_DIR"; |
558 | 0 | vars.TargetPDB = "$TARGET_PDB"; |
559 | | |
560 | | // Setup the target version. |
561 | 0 | std::string targetVersionMajor; |
562 | 0 | std::string targetVersionMinor; |
563 | 0 | { |
564 | 0 | int major; |
565 | 0 | int minor; |
566 | 0 | this->GetGeneratorTarget()->GetTargetVersion(major, minor); |
567 | 0 | targetVersionMajor = std::to_string(major); |
568 | 0 | targetVersionMinor = std::to_string(minor); |
569 | 0 | } |
570 | 0 | vars.TargetVersionMajor = targetVersionMajor.c_str(); |
571 | 0 | vars.TargetVersionMinor = targetVersionMinor.c_str(); |
572 | |
|
573 | 0 | vars.Flags = "$FLAGS"; |
574 | 0 | vars.LinkFlags = "$LINK_FLAGS"; |
575 | 0 | vars.Manifests = "$MANIFESTS"; |
576 | 0 | vars.Config = "$CONFIG"; |
577 | |
|
578 | 0 | std::string langFlags; |
579 | 0 | if (targetType != cm::TargetType::EXECUTABLE) { |
580 | 0 | langFlags += "$LANGUAGE_COMPILE_FLAGS $ARCH_FLAGS"; |
581 | 0 | vars.LanguageCompileFlags = langFlags.c_str(); |
582 | 0 | } |
583 | |
|
584 | 0 | std::string const linkerLauncher = |
585 | 0 | this->ConvertLauncherToShell(this->GetLinkerLauncher(config)); |
586 | 0 | if (cmNonempty(linkerLauncher)) { |
587 | 0 | vars.Launcher = linkerLauncher.c_str(); |
588 | 0 | } |
589 | |
|
590 | 0 | std::string launcher; |
591 | 0 | std::string val = this->GetLocalGenerator()->GetRuleLauncher( |
592 | 0 | this->GetGeneratorTarget(), "RULE_LAUNCH_LINK", config); |
593 | 0 | if (cmNonempty(val)) { |
594 | 0 | launcher = cmStrCat(val, ' '); |
595 | 0 | } |
596 | |
|
597 | 0 | auto rulePlaceholderExpander = |
598 | 0 | this->GetLocalGenerator()->CreateRulePlaceholderExpander( |
599 | 0 | cmBuildStep::Link); |
600 | | |
601 | | // Rule for linking library/executable. |
602 | 0 | std::vector<std::string> linkCmds = this->ComputeLinkCmd(config); |
603 | 0 | for (std::string& linkCmd : linkCmds) { |
604 | 0 | linkCmd = cmStrCat(launcher, std::move(linkCmd)); |
605 | 0 | rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), |
606 | 0 | linkCmd, vars); |
607 | 0 | } |
608 | | |
609 | | // If there is no ranlib the command will be ":". Skip it. |
610 | 0 | cm::erase_if(linkCmds, cmNinjaRemoveNoOpCommands()); |
611 | | |
612 | | // The rule is specific to this target and configuration, so only |
613 | | // reference $PRE_LINK and $POST_BUILD when the target has such steps. |
614 | | // Otherwise they expand to a no-op that still forces a shell on Windows |
615 | | // (cmd.exe /C), one more process for every link. If the rule has no |
616 | | // commands of its own, keep them: an empty rule would otherwise become |
617 | | // the bare shell no-op, which is not a program on Windows. |
618 | 0 | bool const noLinkCmds = linkCmds.empty(); |
619 | 0 | if (hasPreLink || noLinkCmds) { |
620 | 0 | linkCmds.insert(linkCmds.begin(), "$PRE_LINK"); |
621 | 0 | } |
622 | 0 | if (hasPostBuild || noLinkCmds) { |
623 | 0 | linkCmds.emplace_back("$POST_BUILD"); |
624 | 0 | } |
625 | 0 | rule.Command = |
626 | 0 | this->GetLocalGenerator()->BuildCommandLine(linkCmds, config, config); |
627 | | |
628 | | // Write the linker rule with response file if needed. |
629 | 0 | rule.Comment = |
630 | 0 | cmStrCat("Rule for linking ", this->TargetLinkLanguage(config), ' ', |
631 | 0 | this->GetVisibleTypeName(), '.'); |
632 | 0 | char const* presep = ""; |
633 | 0 | char const* postsep = ""; |
634 | 0 | auto prelink = cmJoin(preLinkComments, "; "); |
635 | 0 | NinjaSafeComment(prelink); |
636 | 0 | if (!prelink.empty()) { |
637 | 0 | presep = "; "; |
638 | 0 | } |
639 | 0 | auto postbuild = cmJoin(postBuildComments, "; "); |
640 | 0 | NinjaSafeComment(postbuild); |
641 | 0 | if (!postbuild.empty()) { |
642 | 0 | postsep = "; "; |
643 | 0 | } |
644 | 0 | rule.Description = cmStrCat( |
645 | 0 | prelink, presep, "Linking ", this->TargetLinkLanguage(config), ' ', |
646 | 0 | this->GetVisibleTypeName(), " $TARGET_FILE", postsep, postbuild); |
647 | 0 | rule.Restat = "$RESTAT"; |
648 | 0 | this->GetGlobalGenerator()->AddRule(rule); |
649 | 0 | } |
650 | |
|
651 | 0 | auto const tgtNames = this->TargetNames(config); |
652 | 0 | if (tgtNames.Output != tgtNames.Real && |
653 | 0 | !this->GetGeneratorTarget()->IsFrameworkOnApple()) { |
654 | 0 | std::string cmakeCommand = |
655 | 0 | this->GetLocalGenerator()->ConvertToOutputFormat( |
656 | 0 | cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL); |
657 | 0 | if (targetType == cm::TargetType::EXECUTABLE) { |
658 | 0 | cmNinjaRule rule("CMAKE_SYMLINK_EXECUTABLE"); |
659 | 0 | { |
660 | 0 | std::vector<std::string> cmd; |
661 | 0 | cmd.push_back(cmakeCommand + " -E cmake_symlink_executable $in $out"); |
662 | 0 | cmd.emplace_back("$POST_BUILD"); |
663 | 0 | rule.Command = |
664 | 0 | this->GetLocalGenerator()->BuildCommandLine(cmd, config, config); |
665 | 0 | } |
666 | 0 | rule.Description = "Creating executable symlink $out"; |
667 | 0 | rule.Comment = "Rule for creating executable symlink."; |
668 | 0 | this->GetGlobalGenerator()->AddRule(rule); |
669 | 0 | } else { |
670 | 0 | cmNinjaRule rule("CMAKE_SYMLINK_LIBRARY"); |
671 | 0 | { |
672 | 0 | std::vector<std::string> cmd; |
673 | 0 | cmd.push_back(cmakeCommand + |
674 | 0 | " -E cmake_symlink_library $in $SONAME $out"); |
675 | 0 | cmd.emplace_back("$POST_BUILD"); |
676 | 0 | rule.Command = |
677 | 0 | this->GetLocalGenerator()->BuildCommandLine(cmd, config, config); |
678 | 0 | } |
679 | 0 | rule.Description = "Creating library symlink $out"; |
680 | 0 | rule.Comment = "Rule for creating library symlink."; |
681 | 0 | this->GetGlobalGenerator()->AddRule(rule); |
682 | 0 | } |
683 | 0 | } |
684 | |
|
685 | 0 | if (this->GetGeneratorTarget()->IsApple() && |
686 | 0 | this->GetGeneratorTarget()->HasImportLibrary(config)) { |
687 | 0 | cmNinjaRule rule(this->TextStubsGeneratorRule(config)); |
688 | 0 | rule.Comment = cmStrCat("Rule for generating text-based stubs for ", |
689 | 0 | this->GetVisibleTypeName(), '.'); |
690 | 0 | rule.Description = "Creating text-based stubs $out"; |
691 | |
|
692 | 0 | std::string cmd = |
693 | 0 | this->GetMakefile()->GetDefinition("CMAKE_CREATE_TEXT_STUBS"); |
694 | 0 | auto rulePlaceholderExpander = |
695 | 0 | this->GetLocalGenerator()->CreateRulePlaceholderExpander(); |
696 | 0 | cmRulePlaceholderExpander::RuleVariables vars; |
697 | 0 | vars.Target = "$in"; |
698 | 0 | rulePlaceholderExpander->SetTargetImpLib("$out"); |
699 | 0 | rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), |
700 | 0 | cmd, vars); |
701 | |
|
702 | 0 | rule.Command = |
703 | 0 | this->GetLocalGenerator()->BuildCommandLine({ cmd }, config, config); |
704 | 0 | this->GetGlobalGenerator()->AddRule(rule); |
705 | |
|
706 | 0 | if (tgtNames.ImportOutput != tgtNames.ImportReal && |
707 | 0 | !this->GetGeneratorTarget()->IsFrameworkOnApple()) { |
708 | 0 | cmNinjaRule slRule("CMAKE_SYMLINK_IMPORT_LIBRARY"); |
709 | 0 | { |
710 | 0 | std::string cmakeCommand = |
711 | 0 | this->GetLocalGenerator()->ConvertToOutputFormat( |
712 | 0 | cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL); |
713 | 0 | std::string slCmd = |
714 | 0 | cmStrCat(cmakeCommand, " -E cmake_symlink_library $in $SONAME $out"); |
715 | 0 | slRule.Command = this->GetLocalGenerator()->BuildCommandLine( |
716 | 0 | { slCmd }, config, config); |
717 | 0 | } |
718 | 0 | slRule.Description = "Creating import library symlink $out"; |
719 | 0 | slRule.Comment = "Rule for creating import library symlink."; |
720 | 0 | this->GetGlobalGenerator()->AddRule(slRule); |
721 | 0 | } |
722 | 0 | } |
723 | 0 | } |
724 | | |
725 | | std::vector<std::string> cmNinjaNormalTargetGenerator::ComputeDeviceLinkCmd() |
726 | 0 | { |
727 | 0 | cmList linkCmds; |
728 | | |
729 | | // this target requires separable cuda compilation |
730 | | // now build the correct command depending on if the target is |
731 | | // an executable or a dynamic library. |
732 | 0 | switch (this->GetGeneratorTarget()->GetType()) { |
733 | 0 | case cm::TargetType::STATIC_LIBRARY: |
734 | 0 | case cm::TargetType::SHARED_LIBRARY: |
735 | 0 | case cm::TargetType::MODULE_LIBRARY: { |
736 | 0 | linkCmds.assign(this->GetMakefile()->GetDefinition( |
737 | 0 | cmStrCat("CMAKE_", this->DeviceLinkLanguage, "_DEVICE_LINK_LIBRARY"))); |
738 | 0 | } break; |
739 | 0 | case cm::TargetType::EXECUTABLE: { |
740 | 0 | linkCmds.assign(this->GetMakefile()->GetDefinition(cmStrCat( |
741 | 0 | "CMAKE_", this->DeviceLinkLanguage, "_DEVICE_LINK_EXECUTABLE"))); |
742 | 0 | } break; |
743 | 0 | default: |
744 | 0 | break; |
745 | 0 | } |
746 | 0 | return std::move(linkCmds.data()); |
747 | 0 | } |
748 | | |
749 | | std::vector<std::string> cmNinjaNormalTargetGenerator::ComputeLinkCmd( |
750 | | std::string const& config) |
751 | 0 | { |
752 | 0 | cmList linkCmds; |
753 | 0 | cmMakefile* mf = this->GetMakefile(); |
754 | 0 | { |
755 | | // If we have a rule variable prefer it. In the case of static libraries |
756 | | // this occurs when things like IPO is enabled, and we need to use the |
757 | | // CMAKE_<lang>_CREATE_STATIC_LIBRARY_IPO define instead. |
758 | 0 | std::string linkCmdVar = this->GetGeneratorTarget()->GetCreateRuleVariable( |
759 | 0 | this->TargetLinkLanguage(config), config); |
760 | 0 | cmValue linkCmd = mf->GetDefinition(linkCmdVar); |
761 | 0 | if (linkCmd) { |
762 | 0 | std::string linkCmdStr = *linkCmd; |
763 | 0 | if (this->GetGeneratorTarget()->HasImplibGNUtoMS(config)) { |
764 | 0 | std::string ruleVar = |
765 | 0 | cmStrCat("CMAKE_", this->GeneratorTarget->GetLinkerLanguage(config), |
766 | 0 | "_GNUtoMS_RULE"); |
767 | 0 | if (cmValue rule = this->Makefile->GetDefinition(ruleVar)) { |
768 | 0 | linkCmdStr += *rule; |
769 | 0 | } |
770 | 0 | } |
771 | 0 | linkCmds.assign(linkCmdStr); |
772 | 0 | if (this->UseLWYU) { |
773 | 0 | cmValue lwyuCheck = mf->GetDefinition("CMAKE_LINK_WHAT_YOU_USE_CHECK"); |
774 | 0 | if (lwyuCheck) { |
775 | 0 | std::string cmakeCommand = cmStrCat( |
776 | 0 | this->GetLocalGenerator()->ConvertToOutputFormat( |
777 | 0 | cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL), |
778 | 0 | " -E __run_co_compile --lwyu="); |
779 | 0 | cmakeCommand += |
780 | 0 | this->GetLocalGenerator()->EscapeForShell(*lwyuCheck); |
781 | |
|
782 | 0 | std::string targetOutputReal = |
783 | 0 | this->ConvertToNinjaPath(this->GetGeneratorTarget()->GetFullPath( |
784 | 0 | config, cmStateEnums::RuntimeBinaryArtifact, |
785 | 0 | /*realname=*/true)); |
786 | 0 | cmakeCommand = |
787 | 0 | cmStrCat(std::move(cmakeCommand), " --source=", targetOutputReal); |
788 | 0 | linkCmds.push_back(std::move(cmakeCommand)); |
789 | 0 | } |
790 | 0 | } |
791 | 0 | return std::move(linkCmds.data()); |
792 | 0 | } |
793 | 0 | } |
794 | 0 | switch (this->GetGeneratorTarget()->GetType()) { |
795 | 0 | case cm::TargetType::STATIC_LIBRARY: { |
796 | | // We have archive link commands set. First, delete the existing archive. |
797 | 0 | { |
798 | | #ifdef _WIN32 |
799 | | // This rule always has more than one command, so it runs under |
800 | | // cmd.exe: delete with its builtin instead of starting cmake.exe. |
801 | | // A RULE_LAUNCH_LINK launcher runs each command itself, without |
802 | | // cmd.exe, so keep using cmake.exe when there is one. |
803 | | bool const useDel = this->GetLocalGenerator() |
804 | | ->GetRuleLauncher(this->GetGeneratorTarget(), |
805 | | "RULE_LAUNCH_LINK", config) |
806 | | .empty(); |
807 | | #else |
808 | 0 | bool const useDel = false; |
809 | 0 | #endif |
810 | 0 | if (useDel) { |
811 | | // Discard the message 'del' prints for a missing file. |
812 | 0 | linkCmds.emplace_back("del /f /q $TARGET_FILE 2>nul"); |
813 | 0 | } else { |
814 | 0 | std::string cmakeCommand = |
815 | 0 | this->GetLocalGenerator()->ConvertToOutputFormat( |
816 | 0 | cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL); |
817 | 0 | linkCmds.push_back(cmakeCommand + " -E rm -f $TARGET_FILE"); |
818 | 0 | } |
819 | 0 | } |
820 | | // TODO: Use ARCHIVE_APPEND for archives over a certain size. |
821 | 0 | { |
822 | 0 | std::string linkCmdVar = cmStrCat( |
823 | 0 | "CMAKE_", this->TargetLinkLanguage(config), "_ARCHIVE_CREATE"); |
824 | |
|
825 | 0 | linkCmdVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable( |
826 | 0 | linkCmdVar, this->TargetLinkLanguage(config), config); |
827 | |
|
828 | 0 | std::string const& linkCmd = mf->GetRequiredDefinition(linkCmdVar); |
829 | 0 | linkCmds.append(linkCmd); |
830 | 0 | } |
831 | 0 | { |
832 | 0 | std::string linkCmdVar = cmStrCat( |
833 | 0 | "CMAKE_", this->TargetLinkLanguage(config), "_ARCHIVE_FINISH"); |
834 | |
|
835 | 0 | linkCmdVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable( |
836 | 0 | linkCmdVar, this->TargetLinkLanguage(config), config); |
837 | |
|
838 | 0 | std::string const& linkCmd = mf->GetRequiredDefinition(linkCmdVar); |
839 | 0 | linkCmds.append(linkCmd); |
840 | 0 | } |
841 | | #ifdef __APPLE__ |
842 | | // On macOS ranlib truncates the fractional part of the static archive |
843 | | // file modification time. If the archive and at least one contained |
844 | | // object file were created within the same second this will make look |
845 | | // the archive older than the object file. On subsequent ninja runs this |
846 | | // leads to re-archiving and updating dependent targets. |
847 | | // As a work-around we touch the archive after ranlib (see #19222). |
848 | | { |
849 | | std::string cmakeCommand = |
850 | | this->GetLocalGenerator()->ConvertToOutputFormat( |
851 | | cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL); |
852 | | linkCmds.push_back(cmakeCommand + " -E touch $TARGET_FILE"); |
853 | | } |
854 | | #endif |
855 | 0 | } break; |
856 | 0 | case cm::TargetType::SHARED_LIBRARY: |
857 | 0 | case cm::TargetType::MODULE_LIBRARY: |
858 | 0 | case cm::TargetType::EXECUTABLE: |
859 | 0 | break; |
860 | 0 | default: |
861 | 0 | CM_UNREACHABLE; |
862 | 0 | } |
863 | 0 | return std::move(linkCmds.data()); |
864 | 0 | } |
865 | | |
866 | | void cmNinjaNormalTargetGenerator::WriteDeviceLinkStatement( |
867 | | std::string const& config, std::string const& fileConfig, |
868 | | bool firstForConfig) |
869 | 0 | { |
870 | 0 | cmGlobalNinjaGenerator* globalGen = this->GetGlobalGenerator(); |
871 | 0 | cmGeneratorTarget* genTarget = this->GetGeneratorTarget(); |
872 | 0 | this->DeviceLinkLanguage = |
873 | 0 | deviceLinkLanguage(*genTarget, *this->GetLocalGenerator(), config); |
874 | 0 | if (this->DeviceLinkLanguage.empty()) { |
875 | 0 | return; |
876 | 0 | } |
877 | 0 | genTarget->GetLinkInformation(config); |
878 | 0 | cmGeneratorTarget::DeviceLinkSetter deviceLink(*genTarget); |
879 | | |
880 | | // First and very important step is to make sure while inside this |
881 | | // step our link language is set to CUDA |
882 | 0 | std::string const& objExt = this->Makefile->GetSafeDefinition( |
883 | 0 | cmStrCat("CMAKE_", this->DeviceLinkLanguage, "_OUTPUT_EXTENSION")); |
884 | |
|
885 | 0 | std::string targetOutputDir = |
886 | 0 | this->GetLocalGenerator()->MaybeRelativeToTopBinDir( |
887 | 0 | cmStrCat(genTarget->GetSupportDirectory(), |
888 | 0 | globalGen->ConfigDirectory(config), '/')); |
889 | 0 | targetOutputDir = globalGen->ExpandCFGIntDir(targetOutputDir, config); |
890 | |
|
891 | 0 | std::string targetOutputReal = this->ConvertToNinjaPath( |
892 | 0 | cmStrCat(targetOutputDir, "cmake_device_link", objExt)); |
893 | |
|
894 | 0 | if (firstForConfig) { |
895 | 0 | globalGen->GetByproductsForCleanTarget(config).push_back(targetOutputReal); |
896 | 0 | } |
897 | 0 | this->DeviceLinkObject = targetOutputReal; |
898 | | |
899 | | // Write comments. |
900 | 0 | cmGlobalNinjaGenerator::WriteDivider(this->GetCommonFileStream()); |
901 | 0 | this->GetCommonFileStream() |
902 | 0 | << "# Device Link build statements for " |
903 | 0 | << cmState::GetTargetTypeName(genTarget->GetType()) << " target " |
904 | 0 | << this->GetTargetName() << "\n\n"; |
905 | |
|
906 | 0 | if (this->DeviceLinkLanguage == "CUDA" && |
907 | 0 | this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID") == "Clang") { |
908 | 0 | std::string architecturesStr = |
909 | 0 | this->GeneratorTarget->GetSafeProperty("CUDA_ARCHITECTURES"); |
910 | |
|
911 | 0 | if (cmIsOff(architecturesStr)) { |
912 | 0 | this->Makefile->IssueMessage(MessageType::FATAL_ERROR, |
913 | 0 | "CUDA_SEPARABLE_COMPILATION on Clang " |
914 | 0 | "requires CUDA_ARCHITECTURES to be set."); |
915 | 0 | return; |
916 | 0 | } |
917 | | |
918 | 0 | this->WriteDeviceLinkRules(config); |
919 | 0 | this->WriteDeviceLinkStatements(config, cmList{ architecturesStr }, |
920 | 0 | targetOutputReal); |
921 | 0 | } else { |
922 | 0 | this->WriteDriverDeviceLinkStatement(config, fileConfig, targetOutputDir, |
923 | 0 | targetOutputReal); |
924 | 0 | } |
925 | 0 | } |
926 | | |
927 | | void cmNinjaNormalTargetGenerator::WriteDeviceLinkStatements( |
928 | | std::string const& config, std::vector<std::string> const& architectures, |
929 | | std::string const& output) |
930 | 0 | { |
931 | | // Ensure there are no duplicates. |
932 | 0 | cmNinjaDeps const explicitDeps = [&]() -> std::vector<std::string> { |
933 | 0 | std::unordered_set<std::string> depsSet; |
934 | 0 | cmNinjaDeps const linkDeps = |
935 | 0 | this->ComputeLinkDeps(this->TargetLinkLanguage(config), config, true); |
936 | 0 | cmNinjaDeps const objects = this->GetObjects(config); |
937 | 0 | depsSet.insert(linkDeps.begin(), linkDeps.end()); |
938 | 0 | depsSet.insert(objects.begin(), objects.end()); |
939 | |
|
940 | 0 | std::vector<std::string> deps; |
941 | 0 | std::copy(depsSet.begin(), depsSet.end(), std::back_inserter(deps)); |
942 | 0 | return deps; |
943 | 0 | }(); |
944 | |
|
945 | 0 | cmGlobalNinjaGenerator* globalGen{ this->GetGlobalGenerator() }; |
946 | 0 | std::string const objectDir = |
947 | 0 | cmStrCat(this->GeneratorTarget->GetSupportDirectory(), |
948 | 0 | globalGen->ConfigDirectory(config)); |
949 | 0 | std::string const ninjaOutputDir = this->ConvertToNinjaPath(objectDir); |
950 | |
|
951 | 0 | cmNinjaBuild fatbinary(this->LanguageLinkerCudaFatbinaryRule(config)); |
952 | | |
953 | | // Link device code for each architecture. |
954 | 0 | for (std::string const& architectureKind : architectures) { |
955 | | // Clang always generates real code, so strip the specifier. |
956 | 0 | std::string const architecture = |
957 | 0 | architectureKind.substr(0, architectureKind.find('-')); |
958 | 0 | std::string const cubin = |
959 | 0 | cmStrCat(ninjaOutputDir, "/sm_", architecture, ".cubin"); |
960 | |
|
961 | 0 | cmNinjaBuild dlink(this->LanguageLinkerCudaDeviceRule(config)); |
962 | 0 | dlink.ExplicitDeps = explicitDeps; |
963 | 0 | dlink.Outputs = { cubin }; |
964 | 0 | dlink.Variables["ARCH"] = cmStrCat("sm_", architecture); |
965 | | |
966 | | // The generated register file contains macros that when expanded register |
967 | | // the device routines. Because the routines are the same for all |
968 | | // architectures the register file will be the same too. Thus generate it |
969 | | // only on the first invocation to reduce overhead. |
970 | 0 | if (fatbinary.ExplicitDeps.empty()) { |
971 | 0 | dlink.Variables["REGISTER"] = cmStrCat( |
972 | 0 | "--register-link-binaries=", ninjaOutputDir, "/cmake_cuda_register.h"); |
973 | 0 | } |
974 | |
|
975 | 0 | fatbinary.Variables["PROFILES"] = |
976 | 0 | cmStrCat(std::move(fatbinary.Variables["PROFILES"]), " -im=profile=sm_", |
977 | 0 | architecture, ",file=", cubin); |
978 | 0 | fatbinary.ExplicitDeps.emplace_back(cubin); |
979 | |
|
980 | 0 | globalGen->WriteBuild(this->GetCommonFileStream(), dlink); |
981 | 0 | } |
982 | | |
983 | | // Combine all architectures into a single fatbinary. |
984 | 0 | fatbinary.Outputs = { cmStrCat(ninjaOutputDir, "/cmake_cuda_fatbin.h") }; |
985 | 0 | globalGen->WriteBuild(this->GetCommonFileStream(), fatbinary); |
986 | | |
987 | | // Compile the stub that registers the kernels and contains the fatbinaries. |
988 | 0 | cmLocalNinjaGenerator* localGen{ this->GetLocalGenerator() }; |
989 | 0 | cmNinjaBuild dcompile(this->LanguageLinkerCudaDeviceCompileRule(config)); |
990 | 0 | dcompile.Outputs = { output }; |
991 | 0 | dcompile.ExplicitDeps = { cmStrCat(ninjaOutputDir, "/cmake_cuda_fatbin.h") }; |
992 | 0 | dcompile.Variables["FATBIN"] = localGen->ConvertToOutputFormat( |
993 | 0 | cmStrCat(objectDir, "/cmake_cuda_fatbin.h"), cmOutputConverter::SHELL); |
994 | 0 | dcompile.Variables["REGISTER"] = localGen->ConvertToOutputFormat( |
995 | 0 | cmStrCat(objectDir, "/cmake_cuda_register.h"), cmOutputConverter::SHELL); |
996 | |
|
997 | 0 | cmNinjaLinkLineDeviceComputer linkLineComputer( |
998 | 0 | localGen, localGen->GetStateSnapshot().GetDirectory(), globalGen); |
999 | 0 | linkLineComputer.SetUseNinjaMulti(globalGen->IsMultiConfig()); |
1000 | | |
1001 | | // Link libraries and paths are only used during the final executable/library |
1002 | | // link. |
1003 | 0 | std::string frameworkPath; |
1004 | 0 | std::string linkPath; |
1005 | 0 | std::string linkLibs; |
1006 | 0 | localGen->GetDeviceLinkFlags(linkLineComputer, config, linkLibs, |
1007 | 0 | dcompile.Variables["LINK_FLAGS"], frameworkPath, |
1008 | 0 | linkPath, this->GetGeneratorTarget()); |
1009 | |
|
1010 | 0 | globalGen->WriteBuild(this->GetCommonFileStream(), dcompile); |
1011 | 0 | } |
1012 | | |
1013 | | void cmNinjaNormalTargetGenerator::WriteDriverDeviceLinkStatement( |
1014 | | std::string const& config, std::string const& fileConfig, |
1015 | | std::string const& outputDir, std::string const& output) |
1016 | 0 | { |
1017 | 0 | cmGeneratorTarget* genTarget = this->GetGeneratorTarget(); |
1018 | 0 | cmGlobalNinjaGenerator* globalGen = this->GetGlobalGenerator(); |
1019 | |
|
1020 | 0 | std::string targetOutputImplib = this->ConvertToNinjaPath( |
1021 | 0 | genTarget->GetFullPath(config, cmStateEnums::ImportLibraryArtifact)); |
1022 | |
|
1023 | 0 | if (config != fileConfig) { |
1024 | 0 | std::string targetOutputFileConfigDir = |
1025 | 0 | this->GetLocalGenerator()->MaybeRelativeToTopBinDir( |
1026 | 0 | cmStrCat(genTarget->GetSupportDirectory(), |
1027 | 0 | globalGen->ConfigDirectory(config), '/')); |
1028 | 0 | targetOutputFileConfigDir = |
1029 | 0 | globalGen->ExpandCFGIntDir(outputDir, fileConfig); |
1030 | 0 | if (outputDir == targetOutputFileConfigDir) { |
1031 | 0 | return; |
1032 | 0 | } |
1033 | | |
1034 | 0 | if (!genTarget->GetFullName(config, cmStateEnums::ImportLibraryArtifact) |
1035 | 0 | .empty() && |
1036 | 0 | !genTarget |
1037 | 0 | ->GetFullName(fileConfig, cmStateEnums::ImportLibraryArtifact) |
1038 | 0 | .empty() && |
1039 | 0 | targetOutputImplib == |
1040 | 0 | this->ConvertToNinjaPath(genTarget->GetFullPath( |
1041 | 0 | fileConfig, cmStateEnums::ImportLibraryArtifact))) { |
1042 | 0 | return; |
1043 | 0 | } |
1044 | 0 | } |
1045 | | |
1046 | | // Compute the comment. |
1047 | 0 | cmNinjaBuild build(this->LanguageLinkerDeviceRule(config)); |
1048 | 0 | build.Comment = |
1049 | 0 | cmStrCat("Link the ", this->GetVisibleTypeName(), ' ', output); |
1050 | |
|
1051 | 0 | cmNinjaVars& vars = build.Variables; |
1052 | | |
1053 | | // Compute outputs. |
1054 | 0 | build.Outputs.push_back(output); |
1055 | | // Compute specific libraries to link with. |
1056 | 0 | build.ExplicitDeps = this->GetObjects(config); |
1057 | 0 | build.ImplicitDeps = |
1058 | 0 | this->ComputeLinkDeps(this->TargetLinkLanguage(config), config); |
1059 | |
|
1060 | 0 | std::string frameworkPath; |
1061 | 0 | std::string linkPath; |
1062 | |
|
1063 | 0 | std::string createRule = |
1064 | 0 | genTarget->GetCreateRuleVariable(this->TargetLinkLanguage(config), config); |
1065 | 0 | cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator(); |
1066 | |
|
1067 | 0 | vars["TARGET_FILE"] = |
1068 | 0 | localGen.ConvertToOutputFormat(output, cmOutputConverter::SHELL); |
1069 | |
|
1070 | 0 | cmNinjaLinkLineDeviceComputer linkLineComputer( |
1071 | 0 | this->GetLocalGenerator(), |
1072 | 0 | this->GetLocalGenerator()->GetStateSnapshot().GetDirectory(), globalGen, |
1073 | 0 | this->DeviceLinkLanguage); |
1074 | 0 | linkLineComputer.SetUseNinjaMulti(globalGen->IsMultiConfig()); |
1075 | |
|
1076 | 0 | localGen.GetDeviceLinkFlags(linkLineComputer, config, vars["LINK_LIBRARIES"], |
1077 | 0 | vars["LINK_FLAGS"], frameworkPath, linkPath, |
1078 | 0 | genTarget); |
1079 | |
|
1080 | 0 | this->addPoolNinjaVariable("JOB_POOL_LINK", config, genTarget, nullptr, |
1081 | 0 | vars); |
1082 | |
|
1083 | 0 | vars["MANIFESTS"] = this->GetManifests(config); |
1084 | |
|
1085 | 0 | vars["LINK_PATH"] = frameworkPath + linkPath; |
1086 | | |
1087 | | // Compute language specific link flags. |
1088 | 0 | std::string langFlags; |
1089 | 0 | localGen.AddLanguageFlagsForLinking(langFlags, genTarget, |
1090 | 0 | this->DeviceLinkLanguage, config); |
1091 | 0 | vars["LANGUAGE_COMPILE_FLAGS"] = langFlags; |
1092 | |
|
1093 | 0 | auto const tgtNames = this->TargetNames(config); |
1094 | 0 | if (genTarget->HasSOName(config) || |
1095 | 0 | genTarget->IsArchivedAIXSharedLibrary()) { |
1096 | 0 | vars["SONAME_FLAG"] = |
1097 | 0 | this->GetMakefile()->GetSONameFlag(this->TargetLinkLanguage(config)); |
1098 | 0 | vars["SONAME"] = localGen.ConvertToOutputFormat(tgtNames.SharedObject, |
1099 | 0 | cmOutputConverter::SHELL); |
1100 | 0 | if (genTarget->GetType() == cm::TargetType::SHARED_LIBRARY) { |
1101 | 0 | std::string install_dir = |
1102 | 0 | this->GetGeneratorTarget()->GetInstallNameDirForBuildTree(config); |
1103 | 0 | if (!install_dir.empty()) { |
1104 | | // The install name is target data embedded in the binary, |
1105 | | // not a host path: escape it without converting separators. |
1106 | 0 | vars["INSTALLNAME_DIR"] = localGen.EscapeForShell(install_dir, true); |
1107 | 0 | } |
1108 | 0 | } |
1109 | 0 | } |
1110 | |
|
1111 | 0 | if (!tgtNames.ImportLibrary.empty()) { |
1112 | 0 | std::string const impLibPath = localGen.ConvertToOutputFormat( |
1113 | 0 | targetOutputImplib, cmOutputConverter::SHELL); |
1114 | 0 | vars["TARGET_IMPLIB"] = impLibPath; |
1115 | 0 | this->EnsureParentDirectoryExists(targetOutputImplib); |
1116 | 0 | } |
1117 | |
|
1118 | 0 | std::string const objPath = |
1119 | 0 | cmStrCat(this->GetGeneratorTarget()->GetSupportDirectory(), |
1120 | 0 | globalGen->ConfigDirectory(config)); |
1121 | |
|
1122 | 0 | vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat( |
1123 | 0 | this->ConvertToNinjaPath(objPath), cmOutputConverter::SHELL); |
1124 | 0 | this->EnsureDirectoryExists(objPath); |
1125 | |
|
1126 | 0 | std::string const targetSupportPath = |
1127 | 0 | this->GetGeneratorTarget()->GetCMFSupportDirectory(); |
1128 | |
|
1129 | 0 | vars["TARGET_SUPPORT_DIR"] = |
1130 | 0 | this->GetLocalGenerator()->ConvertToOutputFormat( |
1131 | 0 | this->ConvertToNinjaPath(targetSupportPath), cmOutputConverter::SHELL); |
1132 | 0 | this->EnsureDirectoryExists(targetSupportPath); |
1133 | |
|
1134 | 0 | this->SetMsvcTargetPdbVariable(vars, config); |
1135 | |
|
1136 | 0 | std::string& linkLibraries = vars["LINK_LIBRARIES"]; |
1137 | 0 | std::string& link_path = vars["LINK_PATH"]; |
1138 | 0 | if (globalGen->IsGCCOnWindows()) { |
1139 | | // ar.exe can't handle backslashes in rsp files (implicitly used by gcc) |
1140 | 0 | std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/'); |
1141 | 0 | std::replace(link_path.begin(), link_path.end(), '\\', '/'); |
1142 | 0 | } |
1143 | | |
1144 | | // Device linking currently doesn't support response files so |
1145 | | // do not check if the user has explicitly forced a response file. |
1146 | 0 | int const commandLineLengthLimit = |
1147 | 0 | static_cast<int>(cmSystemTools::CalculateCommandLineLengthLimit()) - |
1148 | 0 | globalGen->GetRuleCmdLength(build.Rule); |
1149 | |
|
1150 | 0 | build.RspFile = this->ConvertToNinjaPath( |
1151 | 0 | cmStrCat("CMakeFiles/", genTarget->GetName(), |
1152 | 0 | globalGen->IsMultiConfig() ? cmStrCat('.', config) : "", ".rsp")); |
1153 | | |
1154 | | // Gather order-only dependencies. |
1155 | 0 | this->GetLocalGenerator()->AppendTargetDepends( |
1156 | 0 | this->GetGeneratorTarget(), build.OrderOnlyDeps, config, config, |
1157 | 0 | DependOnTargetArtifact); |
1158 | | |
1159 | | // Write the build statement for this target. |
1160 | 0 | bool usedResponseFile = false; |
1161 | 0 | globalGen->WriteBuild(this->GetCommonFileStream(), build, |
1162 | 0 | commandLineLengthLimit, &usedResponseFile); |
1163 | 0 | this->WriteDriverDeviceLinkRule(usedResponseFile, config); |
1164 | 0 | } |
1165 | | |
1166 | | void cmNinjaNormalTargetGenerator::WriteLinkStatement( |
1167 | | std::string const& config, std::string const& fileConfig, |
1168 | | bool firstForConfig) |
1169 | 0 | { |
1170 | 0 | cmMakefile* mf = this->GetMakefile(); |
1171 | 0 | cmGlobalNinjaGenerator* globalGen = this->GetGlobalGenerator(); |
1172 | 0 | cmGeneratorTarget* gt = this->GetGeneratorTarget(); |
1173 | |
|
1174 | 0 | std::string targetOutput = this->ConvertToNinjaPath(gt->GetFullPath(config)); |
1175 | 0 | std::string targetOutputReal = this->ConvertToNinjaPath( |
1176 | 0 | gt->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact, |
1177 | 0 | /*realname=*/true)); |
1178 | 0 | std::string targetOutputImplib = this->ConvertToNinjaPath( |
1179 | 0 | gt->GetFullPath(config, cmStateEnums::ImportLibraryArtifact)); |
1180 | |
|
1181 | 0 | if (config != fileConfig) { |
1182 | 0 | if (targetOutput == |
1183 | 0 | this->ConvertToNinjaPath(gt->GetFullPath(fileConfig))) { |
1184 | 0 | return; |
1185 | 0 | } |
1186 | 0 | if (targetOutputReal == |
1187 | 0 | this->ConvertToNinjaPath( |
1188 | 0 | gt->GetFullPath(fileConfig, cmStateEnums::RuntimeBinaryArtifact, |
1189 | 0 | /*realname=*/true))) { |
1190 | 0 | return; |
1191 | 0 | } |
1192 | 0 | if (!gt->GetFullName(config, cmStateEnums::ImportLibraryArtifact) |
1193 | 0 | .empty() && |
1194 | 0 | !gt->GetFullName(fileConfig, cmStateEnums::ImportLibraryArtifact) |
1195 | 0 | .empty() && |
1196 | 0 | targetOutputImplib == |
1197 | 0 | this->ConvertToNinjaPath(gt->GetFullPath( |
1198 | 0 | fileConfig, cmStateEnums::ImportLibraryArtifact))) { |
1199 | 0 | return; |
1200 | 0 | } |
1201 | 0 | } |
1202 | | |
1203 | 0 | auto const tgtNames = this->TargetNames(config); |
1204 | 0 | if (gt->IsAppBundleOnApple()) { |
1205 | | // Create the app bundle |
1206 | 0 | std::string outpath = gt->GetDirectory(config); |
1207 | 0 | this->OSXBundleGenerator->CreateAppBundle(tgtNames.Output, outpath, |
1208 | 0 | config); |
1209 | | |
1210 | | // Calculate the output path |
1211 | 0 | targetOutput = cmStrCat(outpath, '/', tgtNames.Output); |
1212 | 0 | targetOutput = this->ConvertToNinjaPath(targetOutput); |
1213 | 0 | targetOutputReal = cmStrCat(outpath, '/', tgtNames.Real); |
1214 | 0 | targetOutputReal = this->ConvertToNinjaPath(targetOutputReal); |
1215 | 0 | } else if (gt->IsFrameworkOnApple()) { |
1216 | | // Create the library framework. |
1217 | |
|
1218 | 0 | cmOSXBundleGenerator::SkipParts bundleSkipParts; |
1219 | 0 | if (globalGen->GetName() == "Ninja Multi-Config") { |
1220 | 0 | auto const postFix = this->GeneratorTarget->GetFilePostfix(config); |
1221 | | // Skip creating Info.plist when there are multiple configurations, and |
1222 | | // the current configuration has a postfix. The non-postfix configuration |
1223 | | // Info.plist can be used by all the other configurations. |
1224 | 0 | if (!postFix.empty()) { |
1225 | 0 | bundleSkipParts.InfoPlist = true; |
1226 | 0 | } |
1227 | 0 | } |
1228 | 0 | if (gt->HasImportLibrary(config)) { |
1229 | 0 | bundleSkipParts.TextStubs = false; |
1230 | 0 | } |
1231 | |
|
1232 | 0 | this->OSXBundleGenerator->CreateFramework( |
1233 | 0 | tgtNames.Output, gt->GetDirectory(config), config, bundleSkipParts); |
1234 | 0 | } else if (gt->IsCFBundleOnApple()) { |
1235 | | // Create the core foundation bundle. |
1236 | 0 | this->OSXBundleGenerator->CreateCFBundle(tgtNames.Output, |
1237 | 0 | gt->GetDirectory(config), config); |
1238 | 0 | } |
1239 | | |
1240 | | // Write comments. |
1241 | 0 | cmGlobalNinjaGenerator::WriteDivider(this->GetImplFileStream(fileConfig)); |
1242 | 0 | cm::TargetType const targetType = gt->GetType(); |
1243 | 0 | this->GetImplFileStream(fileConfig) |
1244 | 0 | << "# Link build statements for " << cmState::GetTargetTypeName(targetType) |
1245 | 0 | << " target " << this->GetTargetName() << "\n\n"; |
1246 | |
|
1247 | 0 | cmNinjaBuild linkBuild(this->LanguageLinkerRule(config)); |
1248 | 0 | cmNinjaVars& vars = linkBuild.Variables; |
1249 | |
|
1250 | 0 | if (this->GeneratorTarget->HasLinkDependencyFile(config)) { |
1251 | 0 | this->AddDepfileBinding(vars, |
1252 | 0 | this->ConvertToNinjaPath( |
1253 | 0 | this->GetLocalGenerator()->GetLinkDependencyFile( |
1254 | 0 | this->GeneratorTarget, config))); |
1255 | 0 | } |
1256 | | |
1257 | | // Compute the comment. |
1258 | 0 | linkBuild.Comment = |
1259 | 0 | cmStrCat("Link the ", this->GetVisibleTypeName(), ' ', targetOutputReal); |
1260 | | |
1261 | | // Compute outputs. |
1262 | 0 | linkBuild.Outputs.push_back(targetOutputReal); |
1263 | 0 | if (firstForConfig) { |
1264 | 0 | globalGen->GetByproductsForCleanTarget(config).push_back(targetOutputReal); |
1265 | 0 | } |
1266 | | |
1267 | | // If we can't split the Swift build model (CMP0157 is OLD or unset), fall |
1268 | | // back on the old one-step "build/link" logic. |
1269 | 0 | if (!this->GetLocalGenerator()->IsSplitSwiftBuild() && |
1270 | 0 | this->TargetLinkLanguage(config) == "Swift") { |
1271 | 0 | vars["SWIFT_LIBRARY_NAME"] = [this, config]() -> std::string { |
1272 | 0 | cmGeneratorTarget::Names targetNames = |
1273 | 0 | this->GetGeneratorTarget()->GetLibraryNames(config); |
1274 | 0 | return targetNames.Base; |
1275 | 0 | }(); |
1276 | |
|
1277 | 0 | vars["SWIFT_MODULE_NAME"] = gt->GetSwiftModuleName(); |
1278 | 0 | vars["SWIFT_MODULE"] = this->GetLocalGenerator()->ConvertToOutputFormat( |
1279 | 0 | this->ConvertToNinjaPath(gt->GetSwiftModulePath(config)), |
1280 | 0 | cmOutputConverter::SHELL); |
1281 | |
|
1282 | 0 | vars["SWIFT_SOURCES"] = [this, config]() -> std::string { |
1283 | 0 | std::vector<cmSourceFile const*> sourceFiles; |
1284 | 0 | std::string result; |
1285 | |
|
1286 | 0 | this->GetGeneratorTarget()->GetObjectSources(sourceFiles, config); |
1287 | 0 | cmLocalGenerator const* LocalGen = this->GetLocalGenerator(); |
1288 | 0 | for (auto const& source : sourceFiles) { |
1289 | 0 | std::string const sourcePath = |
1290 | 0 | this->GetGeneratorTarget()->GetSourceFileLanguage(source, config) == |
1291 | 0 | "Swift" |
1292 | 0 | ? this->GetCompiledSourceNinjaPath(source) |
1293 | 0 | : this->GetObjectFilePath(source, config); |
1294 | 0 | result = cmStrCat(std::move(result), ' ', |
1295 | 0 | LocalGen->ConvertToOutputFormat( |
1296 | 0 | sourcePath, cmOutputConverter::SHELL)); |
1297 | 0 | } |
1298 | 0 | return result; |
1299 | 0 | }(); |
1300 | | |
1301 | | // Since we do not perform object builds, compute the |
1302 | | // defines/flags/includes here so that they can be passed along |
1303 | | // appropriately. |
1304 | 0 | vars["DEFINES"] = this->GetDefines("Swift", config); |
1305 | 0 | vars["FLAGS"] = this->GetFlags("Swift", config); |
1306 | 0 | vars["INCLUDES"] = this->GetIncludes("Swift", config); |
1307 | 0 | this->GenerateSwiftOutputFileMap(config, vars["FLAGS"]); |
1308 | | |
1309 | | // Compute specific libraries to link with. |
1310 | 0 | std::vector<cmSourceFile const*> sources; |
1311 | 0 | gt->GetObjectSources(sources, config); |
1312 | 0 | for (auto const& source : sources) { |
1313 | 0 | if (gt->GetSourceFileLanguage(source, config) == "Swift") { |
1314 | 0 | linkBuild.Outputs.push_back( |
1315 | 0 | this->ConvertToNinjaPath(this->GetObjectFilePath(source, config))); |
1316 | 0 | linkBuild.ExplicitDeps.emplace_back( |
1317 | 0 | this->GetCompiledSourceNinjaPath(source)); |
1318 | 0 | } else { |
1319 | 0 | linkBuild.ExplicitDeps.emplace_back( |
1320 | 0 | this->GetObjectFilePath(source, config)); |
1321 | 0 | } |
1322 | 0 | } |
1323 | 0 | if (targetType != cm::TargetType::EXECUTABLE || |
1324 | 0 | gt->IsExecutableWithExports()) { |
1325 | 0 | linkBuild.Outputs.push_back(vars["SWIFT_MODULE"]); |
1326 | 0 | } |
1327 | 0 | } else if (this->TargetLinkLanguage(config) == "Rust") { |
1328 | | // Use one-step build/link for Rust. |
1329 | | // Compute specific libraries to link with. |
1330 | 0 | cmLocalGenerator const* lg = this->GetLocalGenerator(); |
1331 | |
|
1332 | 0 | linkBuild.ExplicitDeps = this->GetObjects(config); |
1333 | | |
1334 | | // First we handle Rust rlib and normal native objects. |
1335 | 0 | this->ComputeRustFlagsForObjects(vars["RUST_LINK_CRATES"], |
1336 | 0 | vars["RUST_NATIVE_OBJECTS"], |
1337 | 0 | linkBuild.ExplicitDeps); |
1338 | | |
1339 | | // Then, we handle the main crate root that is build as part of the link |
1340 | | // step. |
1341 | 0 | cmSourceFile const* mainCrateRoot = gt->GetRustMainCrateRoot(config); |
1342 | 0 | if (!mainCrateRoot) { |
1343 | 0 | this->Makefile->IssueMessage( |
1344 | 0 | MessageType::FATAL_ERROR, |
1345 | 0 | cmStrCat("Target ", gt->GetName(), " has no main crate root.")); |
1346 | 0 | return; |
1347 | 0 | } |
1348 | 0 | std::string mainCrateRootPath = |
1349 | 0 | this->GetCompiledSourceNinjaPath(mainCrateRoot); |
1350 | 0 | linkBuild.ExplicitDeps.emplace_back(mainCrateRootPath); |
1351 | 0 | mainCrateRootPath = |
1352 | 0 | lg->ConvertToOutputFormat(mainCrateRootPath, cmOutputConverter::SHELL); |
1353 | 0 | vars["RUST_MAIN_CRATE_ROOT"] = mainCrateRootPath; |
1354 | 0 | } else { |
1355 | 0 | linkBuild.ExplicitDeps = this->GetObjects(config); |
1356 | 0 | } |
1357 | | |
1358 | 0 | auto extraISPCObjects = |
1359 | 0 | this->GetGeneratorTarget()->GetGeneratedISPCObjects(config); |
1360 | 0 | auto const mapToNinjaPath = this->MapToNinjaPath(); |
1361 | 0 | std::transform( |
1362 | 0 | extraISPCObjects.begin(), extraISPCObjects.end(), |
1363 | 0 | std::back_inserter(linkBuild.ExplicitDeps), |
1364 | 0 | [&mapToNinjaPath](std::pair<cmSourceFile const*, std::string> const& obj) |
1365 | 0 | -> std::string { return mapToNinjaPath(obj.second); }); |
1366 | |
|
1367 | 0 | linkBuild.ImplicitDeps = |
1368 | 0 | this->ComputeLinkDeps(this->TargetLinkLanguage(config), config); |
1369 | |
|
1370 | 0 | if (!this->DeviceLinkObject.empty()) { |
1371 | 0 | linkBuild.ExplicitDeps.push_back(this->DeviceLinkObject); |
1372 | 0 | } |
1373 | |
|
1374 | 0 | std::string frameworkPath; |
1375 | 0 | std::string linkPath; |
1376 | |
|
1377 | 0 | std::string createRule = |
1378 | 0 | gt->GetCreateRuleVariable(this->TargetLinkLanguage(config), config); |
1379 | 0 | bool useWatcomQuote = mf->IsOn(createRule + "_USE_WATCOM_QUOTE"); |
1380 | 0 | cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator(); |
1381 | |
|
1382 | 0 | vars["TARGET_FILE"] = |
1383 | 0 | localGen.ConvertToOutputFormat(targetOutputReal, cmOutputConverter::SHELL); |
1384 | |
|
1385 | 0 | std::unique_ptr<cmLinkLineComputer> linkLineComputer = |
1386 | 0 | globalGen->CreateLinkLineComputer( |
1387 | 0 | this->GetLocalGenerator(), |
1388 | 0 | this->GetLocalGenerator()->GetStateSnapshot().GetDirectory()); |
1389 | 0 | linkLineComputer->SetUseWatcomQuote(useWatcomQuote); |
1390 | 0 | linkLineComputer->SetUseNinjaMulti(globalGen->IsMultiConfig()); |
1391 | |
|
1392 | 0 | localGen.GetTargetFlags(linkLineComputer.get(), config, |
1393 | 0 | vars["LINK_LIBRARIES"], vars["FLAGS"], |
1394 | 0 | vars["LINK_FLAGS"], frameworkPath, linkPath, gt); |
1395 | |
|
1396 | 0 | localGen.AppendDependencyInfoLinkerFlags(vars["LINK_FLAGS"], gt, config, |
1397 | 0 | this->TargetLinkLanguage(config)); |
1398 | | |
1399 | | // Add OS X version flags, if any. |
1400 | 0 | if (this->GeneratorTarget->GetType() == cm::TargetType::SHARED_LIBRARY || |
1401 | 0 | this->GeneratorTarget->GetType() == cm::TargetType::MODULE_LIBRARY) { |
1402 | 0 | this->AppendOSXVerFlag(vars["LINK_FLAGS"], |
1403 | 0 | this->TargetLinkLanguage(config), "COMPATIBILITY", |
1404 | 0 | true); |
1405 | 0 | this->AppendOSXVerFlag(vars["LINK_FLAGS"], |
1406 | 0 | this->TargetLinkLanguage(config), "CURRENT", false); |
1407 | 0 | } |
1408 | |
|
1409 | 0 | this->addPoolNinjaVariable("JOB_POOL_LINK", config, gt, nullptr, vars); |
1410 | |
|
1411 | 0 | this->UseLWYU = this->GetLocalGenerator()->AppendLWYUFlags( |
1412 | 0 | vars["LINK_FLAGS"], this->GetGeneratorTarget(), |
1413 | 0 | this->TargetLinkLanguage(config)); |
1414 | |
|
1415 | 0 | vars["MANIFESTS"] = this->GetManifests(config); |
1416 | 0 | vars["AIX_EXPORTS"] = this->GetAIXExports(config); |
1417 | |
|
1418 | 0 | vars["LINK_PATH"] = frameworkPath + linkPath; |
1419 | 0 | vars["CONFIG"] = config; |
1420 | | |
1421 | | // Only add the interface objects response file when C++ collation generates |
1422 | | // it. Linking a module provider does not imply that this target scans C++. |
1423 | 0 | if (this->HasCxxModuleDyndep(config)) { |
1424 | 0 | bool multiConfig = this->GetGlobalGenerator()->IsMultiConfig(); |
1425 | 0 | std::string configDir = multiConfig ? cmStrCat('/', config) : ""; |
1426 | 0 | bool hasInterfaceObjects = false; |
1427 | 0 | for (auto const& pair : |
1428 | 0 | this->GetGeneratorTarget()->GetSyntheticDeps(config)) { |
1429 | 0 | if (pair.first->Target->CxxModuleNeedsInterfaceObjects()) { |
1430 | 0 | hasInterfaceObjects = true; |
1431 | 0 | break; |
1432 | 0 | } |
1433 | 0 | } |
1434 | 0 | if (hasInterfaceObjects) { |
1435 | 0 | std::string rspPath = |
1436 | 0 | cmStrCat(this->GeneratorTarget->GetSupportDirectory(), configDir, |
1437 | 0 | "/CXXInterfaceObjects.rsp"); |
1438 | 0 | vars["LINK_FLAGS"] = cmStrCat( |
1439 | 0 | std::move(vars["LINK_FLAGS"]), " @", |
1440 | 0 | this->GetLocalGenerator()->ConvertToOutputFormat( |
1441 | 0 | this->ConvertToNinjaPath(rspPath), cmOutputConverter::SHELL)); |
1442 | 0 | linkBuild.ImplicitDeps.emplace_back(this->ConvertToNinjaPath(rspPath)); |
1443 | 0 | } |
1444 | 0 | } |
1445 | | |
1446 | | // Compute architecture specific link flags. Yes, these go into a different |
1447 | | // variable for executables, probably due to a mistake made when duplicating |
1448 | | // code between the Makefile executable and library generators. |
1449 | 0 | if (targetType == cm::TargetType::EXECUTABLE) { |
1450 | 0 | std::string t = vars["FLAGS"]; |
1451 | 0 | localGen.AddArchitectureFlags(t, gt, this->TargetLinkLanguage(config), |
1452 | 0 | config); |
1453 | 0 | vars["FLAGS"] = t; |
1454 | 0 | } else { |
1455 | 0 | std::string t = vars["ARCH_FLAGS"]; |
1456 | 0 | localGen.AddArchitectureFlags(t, gt, this->TargetLinkLanguage(config), |
1457 | 0 | config); |
1458 | 0 | vars["ARCH_FLAGS"] = t; |
1459 | 0 | t.clear(); |
1460 | 0 | localGen.AddLanguageFlagsForLinking( |
1461 | 0 | t, gt, this->TargetLinkLanguage(config), config); |
1462 | 0 | vars["LANGUAGE_COMPILE_FLAGS"] = t; |
1463 | 0 | } |
1464 | 0 | if (gt->HasSOName(config) || gt->IsArchivedAIXSharedLibrary()) { |
1465 | 0 | vars["SONAME_FLAG"] = mf->GetSONameFlag(this->TargetLinkLanguage(config)); |
1466 | 0 | vars["SONAME"] = localGen.ConvertToOutputFormat(tgtNames.SharedObject, |
1467 | 0 | cmOutputConverter::SHELL); |
1468 | 0 | if (targetType == cm::TargetType::SHARED_LIBRARY) { |
1469 | 0 | std::string install_dir = gt->GetInstallNameDirForBuildTree(config); |
1470 | 0 | if (!install_dir.empty()) { |
1471 | | // The install name is target data embedded in the binary, |
1472 | | // not a host path: escape it without converting separators. |
1473 | 0 | vars["INSTALLNAME_DIR"] = localGen.EscapeForShell(install_dir, true); |
1474 | 0 | } |
1475 | 0 | } |
1476 | 0 | } |
1477 | |
|
1478 | 0 | cmGlobalNinjaGenerator::CCOutputs byproducts(this->GetGlobalGenerator()); |
1479 | |
|
1480 | 0 | if (!gt->IsApple() && !tgtNames.ImportLibrary.empty()) { |
1481 | 0 | std::string const impLibPath = localGen.ConvertToOutputFormat( |
1482 | 0 | targetOutputImplib, cmOutputConverter::SHELL); |
1483 | 0 | vars["TARGET_IMPLIB"] = impLibPath; |
1484 | 0 | this->EnsureParentDirectoryExists(targetOutputImplib); |
1485 | 0 | if (gt->HasImportLibrary(config)) { |
1486 | | // Some linkers may update a binary without touching its import lib. |
1487 | 0 | byproducts.ExplicitOuts.emplace_back(targetOutputImplib); |
1488 | 0 | if (firstForConfig) { |
1489 | 0 | globalGen->GetByproductsForCleanTarget(config).push_back( |
1490 | 0 | targetOutputImplib); |
1491 | 0 | } |
1492 | 0 | } |
1493 | 0 | } |
1494 | |
|
1495 | 0 | if (!this->SetMsvcTargetPdbVariable(vars, config)) { |
1496 | | // It is common to place debug symbols at a specific place, |
1497 | | // so we need a plain target name in the rule available. |
1498 | 0 | cmGeneratorTarget::NameComponents const& components = |
1499 | 0 | gt->GetFullNameComponents(config); |
1500 | 0 | std::string dbg_suffix = ".dbg"; |
1501 | | // TODO: Where to document? |
1502 | 0 | if (cmValue d = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX")) { |
1503 | 0 | dbg_suffix = *d; |
1504 | 0 | } |
1505 | 0 | vars["TARGET_PDB"] = |
1506 | 0 | cmStrCat(components.base, components.suffix, dbg_suffix); |
1507 | 0 | } |
1508 | |
|
1509 | 0 | std::string const objPath = |
1510 | 0 | cmStrCat(gt->GetSupportDirectory(), globalGen->ConfigDirectory(config)); |
1511 | 0 | vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat( |
1512 | 0 | this->ConvertToNinjaPath(objPath), cmOutputConverter::SHELL); |
1513 | 0 | this->EnsureDirectoryExists(objPath); |
1514 | |
|
1515 | 0 | std::string const targetSupportPath = gt->GetCMFSupportDirectory(); |
1516 | 0 | vars["TARGET_SUPPORT_DIR"] = |
1517 | 0 | this->GetLocalGenerator()->ConvertToOutputFormat( |
1518 | 0 | this->ConvertToNinjaPath(targetSupportPath), cmOutputConverter::SHELL); |
1519 | 0 | this->EnsureDirectoryExists(targetSupportPath); |
1520 | |
|
1521 | 0 | std::string& linkLibraries = vars["LINK_LIBRARIES"]; |
1522 | 0 | std::string& link_path = vars["LINK_PATH"]; |
1523 | 0 | if (globalGen->IsGCCOnWindows()) { |
1524 | | // ar.exe can't handle backslashes in rsp files (implicitly used by gcc) |
1525 | 0 | std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/'); |
1526 | 0 | std::replace(link_path.begin(), link_path.end(), '\\', '/'); |
1527 | 0 | } |
1528 | |
|
1529 | 0 | std::vector<cmCustomCommand> const* cmdLists[3] = { |
1530 | 0 | >->GetPreBuildCommands(), >->GetPreLinkCommands(), |
1531 | 0 | >->GetPostBuildCommands() |
1532 | 0 | }; |
1533 | |
|
1534 | 0 | std::vector<std::string> preLinkComments; |
1535 | 0 | std::vector<std::string> postBuildComments; |
1536 | |
|
1537 | 0 | std::vector<std::string> preLinkCmdLines; |
1538 | 0 | std::vector<std::string> postBuildCmdLines; |
1539 | |
|
1540 | 0 | std::vector<std::string>* cmdComments[3] = { &preLinkComments, |
1541 | 0 | &preLinkComments, |
1542 | 0 | &postBuildComments }; |
1543 | 0 | std::vector<std::string>* cmdLineLists[3] = { &preLinkCmdLines, |
1544 | 0 | &preLinkCmdLines, |
1545 | 0 | &postBuildCmdLines }; |
1546 | 0 | cmGeneratorExpression ge(*this->GetLocalGenerator()->GetCMakeInstance()); |
1547 | |
|
1548 | 0 | for (unsigned i = 0; i != 3; ++i) { |
1549 | 0 | for (cmCustomCommand const& cc : *cmdLists[i]) { |
1550 | 0 | if (config == fileConfig || |
1551 | 0 | this->GetLocalGenerator()->HasUniqueByproducts(cc.GetByproducts(), |
1552 | 0 | cc.GetBacktrace())) { |
1553 | 0 | cmCustomCommandGenerator ccg(cc, fileConfig, this->GetLocalGenerator(), |
1554 | 0 | true, config); |
1555 | 0 | localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]); |
1556 | 0 | if (cc.GetComment()) { |
1557 | 0 | auto cge = ge.Parse(cc.GetComment().value()); |
1558 | 0 | cmdComments[i]->emplace_back( |
1559 | 0 | cge->Evaluate(this->GetLocalGenerator(), config)); |
1560 | 0 | } |
1561 | 0 | std::vector<std::string> const& ccByproducts = ccg.GetByproducts(); |
1562 | 0 | byproducts.Add(ccByproducts); |
1563 | 0 | std::transform( |
1564 | 0 | ccByproducts.begin(), ccByproducts.end(), |
1565 | 0 | std::back_inserter(globalGen->GetByproductsForCleanTarget()), |
1566 | 0 | mapToNinjaPath); |
1567 | 0 | } |
1568 | 0 | } |
1569 | 0 | } |
1570 | | |
1571 | | // If we have any PRE_LINK commands, we need to go back to CMAKE_BINARY_DIR |
1572 | | // for the link commands. |
1573 | 0 | if (!preLinkCmdLines.empty()) { |
1574 | 0 | std::string const homeOutDir = localGen.ConvertToOutputFormat( |
1575 | 0 | localGen.GetBinaryDirectory(), cmOutputConverter::SHELL); |
1576 | 0 | preLinkCmdLines.push_back("cd " + homeOutDir); |
1577 | 0 | } |
1578 | | |
1579 | | // maybe create .def file from list of objects |
1580 | 0 | cmGeneratorTarget::ModuleDefinitionInfo const* mdi = |
1581 | 0 | gt->GetModuleDefinitionInfo(config); |
1582 | 0 | if (mdi && mdi->DefFileGenerated) { |
1583 | 0 | std::string cmakeCommand = |
1584 | 0 | this->GetLocalGenerator()->ConvertToOutputFormat( |
1585 | 0 | cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL); |
1586 | 0 | std::string cmd = |
1587 | 0 | cmStrCat(cmakeCommand, " -E __create_def ", |
1588 | 0 | this->GetLocalGenerator()->ConvertToOutputFormat( |
1589 | 0 | mdi->DefFile, cmOutputConverter::SHELL), |
1590 | 0 | ' '); |
1591 | 0 | std::string obj_list_file = mdi->DefFile + ".objs"; |
1592 | 0 | cmd += this->GetLocalGenerator()->ConvertToOutputFormat( |
1593 | 0 | obj_list_file, cmOutputConverter::SHELL); |
1594 | |
|
1595 | 0 | cmValue nm_executable = this->GetMakefile()->GetDefinition("CMAKE_NM"); |
1596 | 0 | if (!cmIsOff(nm_executable)) { |
1597 | 0 | cmd += " --nm="; |
1598 | 0 | cmd += this->LocalCommonGenerator->ConvertToOutputFormat( |
1599 | 0 | *nm_executable, cmOutputConverter::SHELL); |
1600 | 0 | } |
1601 | 0 | preLinkCmdLines.push_back(std::move(cmd)); |
1602 | | |
1603 | | // create a list of obj files for the -E __create_def to read |
1604 | 0 | cmGeneratedFileStream fout(obj_list_file); |
1605 | |
|
1606 | 0 | if (mdi->WindowsExportAllSymbols) { |
1607 | 0 | cmNinjaDeps objs = this->GetObjects(config); |
1608 | 0 | for (std::string const& obj : objs) { |
1609 | 0 | if (cmHasLiteralSuffix(obj, ".obj")) { |
1610 | 0 | fout << obj << "\n"; |
1611 | 0 | } |
1612 | 0 | } |
1613 | 0 | } |
1614 | |
|
1615 | 0 | for (cmSourceFile const* src : mdi->Sources) { |
1616 | 0 | fout << src->GetFullPath() << "\n"; |
1617 | 0 | } |
1618 | 0 | } |
1619 | |
|
1620 | 0 | vars["PRE_LINK"] = localGen.BuildCommandLine( |
1621 | 0 | preLinkCmdLines, config, fileConfig, "pre-link", this->GeneratorTarget); |
1622 | 0 | std::string postBuildCmdLine = |
1623 | 0 | localGen.BuildCommandLine(postBuildCmdLines, config, fileConfig, |
1624 | 0 | "post-build", this->GeneratorTarget); |
1625 | |
|
1626 | 0 | cmNinjaVars symlinkVars; |
1627 | 0 | bool const symlinkNeeded = |
1628 | 0 | (targetOutput != targetOutputReal && !gt->IsFrameworkOnApple() && |
1629 | 0 | !gt->IsArchivedAIXSharedLibrary()); |
1630 | | // Whether the link rule needs $PRE_LINK / $POST_BUILD. Decide from the |
1631 | | // target's commands, not from this statement's lines: the rule is shared |
1632 | | // by every statement for this target and config, and in a cross-config |
1633 | | // statement the commands above may have been skipped. |
1634 | 0 | bool const hasPreLink = !gt->GetPreBuildCommands().empty() || |
1635 | 0 | !gt->GetPreLinkCommands().empty() || (mdi && mdi->DefFileGenerated); |
1636 | 0 | bool const hasPostBuild = |
1637 | 0 | !symlinkNeeded && !gt->GetPostBuildCommands().empty(); |
1638 | 0 | if (!symlinkNeeded) { |
1639 | 0 | vars["POST_BUILD"] = postBuildCmdLine; |
1640 | 0 | } else { |
1641 | 0 | vars["POST_BUILD"] = cmGlobalNinjaGenerator::SHELL_NOOP; |
1642 | 0 | symlinkVars["POST_BUILD"] = postBuildCmdLine; |
1643 | 0 | } |
1644 | |
|
1645 | 0 | std::string cmakeVarLang = |
1646 | 0 | cmStrCat("CMAKE_", this->TargetLinkLanguage(config)); |
1647 | | |
1648 | | // build response file name |
1649 | 0 | cmValue flag; |
1650 | 0 | if (targetType == cm::TargetType::STATIC_LIBRARY) { |
1651 | 0 | std::string cmakeLinkVar = cmakeVarLang + "_RESPONSE_FILE_ARCHIVE_FLAG"; |
1652 | 0 | flag = this->GetMakefile()->GetDefinition(cmakeLinkVar); |
1653 | 0 | } |
1654 | 0 | if (!flag) { |
1655 | 0 | std::string cmakeLinkVar = cmakeVarLang + "_RESPONSE_FILE_LINK_FLAG"; |
1656 | 0 | flag = this->GetMakefile()->GetDefinition(cmakeLinkVar); |
1657 | 0 | } |
1658 | |
|
1659 | 0 | bool const lang_supports_response = |
1660 | 0 | !(this->TargetLinkLanguage(config) == "RC" || |
1661 | 0 | (this->TargetLinkLanguage(config) == "CUDA" && !flag)); |
1662 | 0 | int commandLineLengthLimit = -1; |
1663 | 0 | if (!lang_supports_response || !this->ForceResponseFile()) { |
1664 | 0 | commandLineLengthLimit = |
1665 | 0 | static_cast<int>(cmSystemTools::CalculateCommandLineLengthLimit()) - |
1666 | 0 | globalGen->GetRuleCmdLength(linkBuild.Rule); |
1667 | 0 | } |
1668 | |
|
1669 | 0 | linkBuild.RspFile = this->ConvertToNinjaPath( |
1670 | 0 | cmStrCat("CMakeFiles/", gt->GetName(), |
1671 | 0 | globalGen->IsMultiConfig() ? cmStrCat('.', config) : "", ".rsp")); |
1672 | | |
1673 | | // Gather order-only dependencies. |
1674 | 0 | this->GetLocalGenerator()->AppendTargetDepends( |
1675 | 0 | gt, linkBuild.OrderOnlyDeps, config, fileConfig, DependOnTargetArtifact); |
1676 | | |
1677 | | // Add order-only dependencies on versioning symlinks of shared libs we link. |
1678 | | // If our target is not producing a runtime binary, it doesn't need the |
1679 | | // symlinks (anything that links to the target might, but that consumer will |
1680 | | // get its own order-only dependency). |
1681 | 0 | if (!gt->IsDLLPlatform() && gt->IsRuntimeBinary()) { |
1682 | 0 | if (cmComputeLinkInformation* cli = gt->GetLinkInformation(config)) { |
1683 | 0 | for (auto const& item : cli->GetItems()) { |
1684 | 0 | if (item.Target && |
1685 | 0 | item.Target->GetType() == cm::TargetType::SHARED_LIBRARY && |
1686 | 0 | !item.Target->IsFrameworkOnApple()) { |
1687 | 0 | std::string const& lib = |
1688 | 0 | this->ConvertToNinjaPath(item.Target->GetFullPath(config)); |
1689 | 0 | if (std::find(linkBuild.ImplicitDeps.begin(), |
1690 | 0 | linkBuild.ImplicitDeps.end(), |
1691 | 0 | lib) == linkBuild.ImplicitDeps.end()) { |
1692 | 0 | linkBuild.OrderOnlyDeps.emplace_back(lib); |
1693 | 0 | } |
1694 | 0 | } |
1695 | 0 | } |
1696 | 0 | } |
1697 | 0 | } |
1698 | | |
1699 | | // Add dependencies on swiftmodule files when using the swift linker |
1700 | 0 | if (!this->GetLocalGenerator()->IsSplitSwiftBuild() && |
1701 | 0 | this->TargetLinkLanguage(config) == "Swift") { |
1702 | 0 | if (cmComputeLinkInformation* cli = |
1703 | 0 | this->GeneratorTarget->GetLinkInformation(config)) { |
1704 | 0 | for (auto const& dependency : cli->GetItems()) { |
1705 | | // Only depend on swiftmodule from targets that actually compile |
1706 | | // Swift sources. A C/C++ target may have Swift as its linker |
1707 | | // language (due to language propagation) without producing one. |
1708 | 0 | if (dependency.Target && |
1709 | 0 | dependency.Target->IsLanguageUsed("Swift", config)) { |
1710 | 0 | std::string swiftmodule = this->ConvertToNinjaPath( |
1711 | 0 | dependency.Target->GetSwiftModulePath(config)); |
1712 | 0 | linkBuild.ImplicitDeps.emplace_back(swiftmodule); |
1713 | 0 | } |
1714 | 0 | } |
1715 | 0 | } |
1716 | 0 | } |
1717 | | |
1718 | | // For split Swift builds, ensure the link edge depends on the target's own |
1719 | | // .swiftmodule so the emit-module edge runs even when no other target in |
1720 | | // the build depends on it (e.g. install-only targets). |
1721 | 0 | std::string swiftModuleOutput = this->GetSwiftModuleOutput(config); |
1722 | 0 | if (!swiftModuleOutput.empty()) { |
1723 | 0 | linkBuild.ImplicitDeps.emplace_back(std::move(swiftModuleOutput)); |
1724 | 0 | } |
1725 | | |
1726 | | // Ninja should restat after linking if and only if there are byproducts. |
1727 | 0 | vars["RESTAT"] = byproducts.ExplicitOuts.empty() ? "" : "1"; |
1728 | |
|
1729 | 0 | linkBuild.Outputs.reserve(linkBuild.Outputs.size() + |
1730 | 0 | byproducts.ExplicitOuts.size()); |
1731 | 0 | std::move(byproducts.ExplicitOuts.begin(), byproducts.ExplicitOuts.end(), |
1732 | 0 | std::back_inserter(linkBuild.Outputs)); |
1733 | 0 | linkBuild.WorkDirOuts = std::move(byproducts.WorkDirOuts); |
1734 | | |
1735 | | // Write the build statement for this target. |
1736 | 0 | bool usedResponseFile = false; |
1737 | 0 | globalGen->WriteBuild(this->GetImplFileStream(fileConfig), linkBuild, |
1738 | 0 | commandLineLengthLimit, &usedResponseFile); |
1739 | 0 | this->WriteLinkRule(usedResponseFile, config, preLinkComments, |
1740 | 0 | postBuildComments, hasPreLink, hasPostBuild); |
1741 | |
|
1742 | 0 | if (symlinkNeeded) { |
1743 | 0 | if (targetType == cm::TargetType::EXECUTABLE) { |
1744 | 0 | cmNinjaBuild build("CMAKE_SYMLINK_EXECUTABLE"); |
1745 | 0 | build.Comment = "Create executable symlink " + targetOutput; |
1746 | 0 | build.Outputs.push_back(targetOutput); |
1747 | 0 | if (firstForConfig) { |
1748 | 0 | globalGen->GetByproductsForCleanTarget(config).push_back(targetOutput); |
1749 | 0 | } |
1750 | 0 | build.ExplicitDeps.push_back(targetOutputReal); |
1751 | 0 | build.Variables = std::move(symlinkVars); |
1752 | 0 | globalGen->WriteBuild(this->GetImplFileStream(fileConfig), build); |
1753 | 0 | } else { |
1754 | 0 | cmNinjaBuild build("CMAKE_SYMLINK_LIBRARY"); |
1755 | 0 | build.Comment = "Create library symlink " + targetOutput; |
1756 | |
|
1757 | 0 | std::string const soName = this->ConvertToNinjaPath( |
1758 | 0 | this->GetTargetFilePath(tgtNames.SharedObject, config)); |
1759 | | // If one link has to be created. |
1760 | 0 | if (targetOutputReal == soName || targetOutput == soName) { |
1761 | 0 | symlinkVars["SONAME"] = |
1762 | 0 | this->GetLocalGenerator()->ConvertToOutputFormat( |
1763 | 0 | soName, cmOutputConverter::SHELL); |
1764 | 0 | } else { |
1765 | 0 | symlinkVars["SONAME"].clear(); |
1766 | 0 | build.Outputs.push_back(soName); |
1767 | 0 | if (firstForConfig) { |
1768 | 0 | globalGen->GetByproductsForCleanTarget(config).push_back(soName); |
1769 | 0 | } |
1770 | 0 | } |
1771 | 0 | build.Outputs.push_back(targetOutput); |
1772 | 0 | if (firstForConfig) { |
1773 | 0 | globalGen->GetByproductsForCleanTarget(config).push_back(targetOutput); |
1774 | 0 | } |
1775 | 0 | build.ExplicitDeps.push_back(targetOutputReal); |
1776 | 0 | build.Variables = std::move(symlinkVars); |
1777 | |
|
1778 | 0 | globalGen->WriteBuild(this->GetImplFileStream(fileConfig), build); |
1779 | 0 | } |
1780 | 0 | } |
1781 | | |
1782 | | // Add aliases for the file name and the target name. |
1783 | 0 | globalGen->AddTargetAlias(tgtNames.Output, gt, config); |
1784 | 0 | globalGen->AddTargetAlias(this->GetTargetName(), gt, config); |
1785 | |
|
1786 | 0 | if (this->GetGeneratorTarget()->IsApple() && |
1787 | 0 | this->GetGeneratorTarget()->HasImportLibrary(config)) { |
1788 | 0 | auto dirTBD = |
1789 | 0 | gt->GetDirectory(config, cmStateEnums::ImportLibraryArtifact); |
1790 | 0 | auto targetTBD = |
1791 | 0 | this->ConvertToNinjaPath(cmStrCat(dirTBD, '/', tgtNames.ImportReal)); |
1792 | 0 | this->EnsureParentDirectoryExists(targetTBD); |
1793 | 0 | cmNinjaBuild build(this->TextStubsGeneratorRule(config)); |
1794 | 0 | build.Comment = cmStrCat("Generate the text-based stubs file ", targetTBD); |
1795 | 0 | build.Outputs.push_back(targetTBD); |
1796 | 0 | build.ExplicitDeps.push_back(targetOutputReal); |
1797 | 0 | globalGen->WriteBuild(this->GetImplFileStream(fileConfig), build); |
1798 | |
|
1799 | 0 | if (tgtNames.ImportOutput != tgtNames.ImportReal && |
1800 | 0 | !this->GetGeneratorTarget()->IsFrameworkOnApple()) { |
1801 | 0 | auto outputTBD = |
1802 | 0 | this->ConvertToNinjaPath(cmStrCat(dirTBD, '/', tgtNames.ImportOutput)); |
1803 | 0 | std::string const soNameTBD = this->ConvertToNinjaPath( |
1804 | 0 | cmStrCat(dirTBD, '/', tgtNames.ImportLibrary)); |
1805 | |
|
1806 | 0 | cmNinjaBuild slBuild("CMAKE_SYMLINK_IMPORT_LIBRARY"); |
1807 | 0 | slBuild.Comment = cmStrCat("Create import library symlink ", outputTBD); |
1808 | 0 | cmNinjaVars slVars; |
1809 | | |
1810 | | // If one link has to be created. |
1811 | 0 | if (targetTBD == soNameTBD || outputTBD == soNameTBD) { |
1812 | 0 | slVars["SONAME"] = this->GetLocalGenerator()->ConvertToOutputFormat( |
1813 | 0 | soNameTBD, cmOutputConverter::SHELL); |
1814 | 0 | } else { |
1815 | 0 | slVars["SONAME"].clear(); |
1816 | 0 | slBuild.Outputs.push_back(soNameTBD); |
1817 | 0 | if (firstForConfig) { |
1818 | 0 | globalGen->GetByproductsForCleanTarget(config).push_back(soNameTBD); |
1819 | 0 | } |
1820 | 0 | } |
1821 | 0 | slBuild.Outputs.push_back(outputTBD); |
1822 | 0 | if (firstForConfig) { |
1823 | 0 | globalGen->GetByproductsForCleanTarget(config).push_back(outputTBD); |
1824 | 0 | } |
1825 | 0 | slBuild.ExplicitDeps.push_back(targetTBD); |
1826 | 0 | slBuild.Variables = std::move(slVars); |
1827 | |
|
1828 | 0 | globalGen->WriteBuild(this->GetImplFileStream(fileConfig), slBuild); |
1829 | 0 | } |
1830 | | |
1831 | | // Add alias for the import file name |
1832 | 0 | globalGen->AddTargetAlias(tgtNames.ImportOutput, gt, config); |
1833 | 0 | } |
1834 | 0 | } |
1835 | | |
1836 | | void cmNinjaNormalTargetGenerator::WriteObjectLibStatement( |
1837 | | std::string const& config) |
1838 | 0 | { |
1839 | | // Write a phony output that depends on all object files. |
1840 | 0 | { |
1841 | 0 | cmNinjaBuild build("phony"); |
1842 | 0 | build.Comment = "Object library " + this->GetTargetName(); |
1843 | 0 | this->GetLocalGenerator()->AppendTargetOutputs(this->GetGeneratorTarget(), |
1844 | 0 | build.Outputs, config); |
1845 | 0 | this->GetLocalGenerator()->AppendTargetOutputs( |
1846 | 0 | this->GetGeneratorTarget(), |
1847 | 0 | this->GetGlobalGenerator()->GetByproductsForCleanTarget(config), config); |
1848 | 0 | build.ExplicitDeps = this->GetObjects(config); |
1849 | 0 | this->GetGlobalGenerator()->WriteBuild(this->GetCommonFileStream(), build); |
1850 | 0 | } |
1851 | | |
1852 | | // Add aliases for the target name. |
1853 | 0 | this->GetGlobalGenerator()->AddTargetAlias( |
1854 | 0 | this->GetTargetName(), this->GetGeneratorTarget(), config); |
1855 | 0 | } |
1856 | | |
1857 | | void cmNinjaNormalTargetGenerator::WriteCxxModuleLibraryStatement( |
1858 | | std::string const& config, std::string const& /*fileConfig*/, |
1859 | | bool firstForConfig) |
1860 | 0 | { |
1861 | | // TODO: How to use `fileConfig` properly? |
1862 | | |
1863 | | // Write a phony output that depends on the scanning output. |
1864 | 0 | { |
1865 | 0 | cmNinjaBuild build("phony"); |
1866 | 0 | build.Comment = |
1867 | 0 | cmStrCat("Imported C++ module library ", this->GetTargetName()); |
1868 | 0 | this->GetLocalGenerator()->AppendTargetOutputs(this->GetGeneratorTarget(), |
1869 | 0 | build.Outputs, config); |
1870 | 0 | if (firstForConfig) { |
1871 | 0 | this->GetLocalGenerator()->AppendTargetOutputs( |
1872 | 0 | this->GetGeneratorTarget(), |
1873 | 0 | this->GetGlobalGenerator()->GetByproductsForCleanTarget(config), |
1874 | 0 | config); |
1875 | 0 | } |
1876 | 0 | build.ExplicitDeps.emplace_back(this->GetDyndepFilePath("CXX", config)); |
1877 | 0 | this->GetGlobalGenerator()->WriteBuild(this->GetCommonFileStream(), build); |
1878 | 0 | } |
1879 | | |
1880 | | // Add aliases for the target name. |
1881 | 0 | this->GetGlobalGenerator()->AddTargetAlias( |
1882 | 0 | this->GetTargetName(), this->GetGeneratorTarget(), config); |
1883 | 0 | } |
1884 | | |
1885 | | cmGeneratorTarget::Names cmNinjaNormalTargetGenerator::TargetNames( |
1886 | | std::string const& config) const |
1887 | 0 | { |
1888 | 0 | if (this->GeneratorTarget->GetType() == cm::TargetType::EXECUTABLE) { |
1889 | 0 | return this->GeneratorTarget->GetExecutableNames(config); |
1890 | 0 | } |
1891 | 0 | return this->GeneratorTarget->GetLibraryNames(config); |
1892 | 0 | } |
1893 | | |
1894 | | std::string cmNinjaNormalTargetGenerator::TargetLinkLanguage( |
1895 | | std::string const& config) const |
1896 | 0 | { |
1897 | 0 | return this->GeneratorTarget->GetLinkerLanguage(config); |
1898 | 0 | } |