/src/CMake/Source/cmCommonTargetGenerator.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 "cmCommonTargetGenerator.h" |
4 | | |
5 | | #include <algorithm> |
6 | | #include <sstream> |
7 | | #include <utility> |
8 | | |
9 | | #include <cm/filesystem> |
10 | | #include <cm/string_view> |
11 | | #include <cmext/string_view> |
12 | | |
13 | | #include "cmComputeLinkInformation.h" |
14 | | #include "cmGenExContext.h" |
15 | | #include "cmGeneratorExpression.h" |
16 | | #include "cmGeneratorExpressionDAGChecker.h" |
17 | | #include "cmGeneratorTarget.h" |
18 | | #include "cmGlobalCommonGenerator.h" |
19 | | #include "cmGlobalGenerator.h" |
20 | | #include "cmList.h" |
21 | | #include "cmLocalCommonGenerator.h" |
22 | | #include "cmLocalGenerator.h" |
23 | | #include "cmMakefile.h" |
24 | | #include "cmMessageType.h" |
25 | | #include "cmOutputConverter.h" |
26 | | #include "cmRange.h" |
27 | | #include "cmSourceFile.h" |
28 | | #include "cmState.h" |
29 | | #include "cmStateTypes.h" |
30 | | #include "cmStringAlgorithms.h" |
31 | | #include "cmSystemTools.h" |
32 | | #include "cmValue.h" |
33 | | |
34 | | cmCommonTargetGenerator::cmCommonTargetGenerator(cmGeneratorTarget* gt) |
35 | 0 | : GeneratorTarget(gt) |
36 | 0 | , Makefile(gt->Makefile) |
37 | | , LocalCommonGenerator( |
38 | 0 | static_cast<cmLocalCommonGenerator*>(gt->LocalGenerator)) |
39 | 0 | , GlobalCommonGenerator(static_cast<cmGlobalCommonGenerator*>( |
40 | 0 | gt->LocalGenerator->GetGlobalGenerator())) |
41 | 0 | , ConfigNames(this->LocalCommonGenerator->GetConfigNames()) |
42 | 0 | { |
43 | 0 | } |
44 | | |
45 | 0 | cmCommonTargetGenerator::~cmCommonTargetGenerator() = default; |
46 | | |
47 | | std::vector<std::string> const& cmCommonTargetGenerator::GetConfigNames() const |
48 | 0 | { |
49 | 0 | return this->ConfigNames; |
50 | 0 | } |
51 | | |
52 | | cmValue cmCommonTargetGenerator::GetFeature(std::string const& feature, |
53 | | std::string const& config) |
54 | 0 | { |
55 | 0 | return this->GeneratorTarget->GetFeature(feature, config); |
56 | 0 | } |
57 | | |
58 | | void cmCommonTargetGenerator::AppendFortranFormatFlags( |
59 | | std::string& flags, cmSourceFile const& source) |
60 | 0 | { |
61 | 0 | std::string const srcfmt = source.GetSafeProperty("Fortran_FORMAT"); |
62 | 0 | cmOutputConverter::FortranFormat format = |
63 | 0 | cmOutputConverter::GetFortranFormat(srcfmt); |
64 | 0 | if (format == cmOutputConverter::FortranFormatNone) { |
65 | 0 | std::string const& tgtfmt = |
66 | 0 | this->GeneratorTarget->GetSafeProperty("Fortran_FORMAT"); |
67 | 0 | format = cmOutputConverter::GetFortranFormat(tgtfmt); |
68 | 0 | } |
69 | 0 | char const* var = nullptr; |
70 | 0 | switch (format) { |
71 | 0 | case cmOutputConverter::FortranFormatFixed: |
72 | 0 | var = "CMAKE_Fortran_FORMAT_FIXED_FLAG"; |
73 | 0 | break; |
74 | 0 | case cmOutputConverter::FortranFormatFree: |
75 | 0 | var = "CMAKE_Fortran_FORMAT_FREE_FLAG"; |
76 | 0 | break; |
77 | 0 | default: |
78 | 0 | break; |
79 | 0 | } |
80 | 0 | if (var) { |
81 | 0 | this->LocalCommonGenerator->AppendFlags( |
82 | 0 | flags, this->Makefile->GetSafeDefinition(var)); |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | | void cmCommonTargetGenerator::AppendFortranPreprocessFlags( |
87 | | std::string& flags, cmSourceFile const& source, |
88 | | PreprocessFlagsRequired requires_pp) |
89 | 0 | { |
90 | 0 | std::string const srcpp = source.GetSafeProperty("Fortran_PREPROCESS"); |
91 | 0 | cmOutputConverter::FortranPreprocess preprocess = |
92 | 0 | cmOutputConverter::GetFortranPreprocess(srcpp); |
93 | 0 | if (preprocess == cmOutputConverter::FortranPreprocess::Unset) { |
94 | 0 | std::string const& tgtpp = |
95 | 0 | this->GeneratorTarget->GetSafeProperty("Fortran_PREPROCESS"); |
96 | 0 | preprocess = cmOutputConverter::GetFortranPreprocess(tgtpp); |
97 | 0 | } |
98 | 0 | char const* var = nullptr; |
99 | 0 | switch (preprocess) { |
100 | 0 | case cmOutputConverter::FortranPreprocess::Needed: |
101 | 0 | if (requires_pp == PreprocessFlagsRequired::YES) { |
102 | 0 | var = "CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_ON"; |
103 | 0 | } |
104 | 0 | break; |
105 | 0 | case cmOutputConverter::FortranPreprocess::NotNeeded: |
106 | 0 | var = "CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_OFF"; |
107 | 0 | break; |
108 | 0 | default: |
109 | 0 | break; |
110 | 0 | } |
111 | 0 | if (var) { |
112 | 0 | this->LocalCommonGenerator->AppendCompileOptions( |
113 | 0 | flags, this->Makefile->GetSafeDefinition(var)); |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | | std::string cmCommonTargetGenerator::GetFlags(std::string const& l, |
118 | | std::string const& config, |
119 | | std::string const& arch) |
120 | 0 | { |
121 | 0 | std::string const key = config + arch; |
122 | |
|
123 | 0 | auto i = this->Configs[key].FlagsByLanguage.find(l); |
124 | 0 | if (i == this->Configs[key].FlagsByLanguage.end()) { |
125 | 0 | std::string flags; |
126 | |
|
127 | 0 | this->LocalCommonGenerator->GetTargetCompileFlags(this->GeneratorTarget, |
128 | 0 | config, l, flags, arch); |
129 | |
|
130 | 0 | ByLanguageMap::value_type entry(l, flags); |
131 | 0 | i = this->Configs[key].FlagsByLanguage.insert(entry).first; |
132 | 0 | } |
133 | 0 | return i->second; |
134 | 0 | } |
135 | | |
136 | | std::string cmCommonTargetGenerator::GetDefines(std::string const& l, |
137 | | std::string const& config) |
138 | 0 | { |
139 | 0 | auto i = this->Configs[config].DefinesByLanguage.find(l); |
140 | 0 | if (i == this->Configs[config].DefinesByLanguage.end()) { |
141 | 0 | std::set<std::string> defines; |
142 | 0 | this->LocalCommonGenerator->GetTargetDefines(this->GeneratorTarget, config, |
143 | 0 | l, defines); |
144 | |
|
145 | 0 | std::string definesString; |
146 | 0 | this->LocalCommonGenerator->JoinDefines(defines, definesString, l); |
147 | |
|
148 | 0 | ByLanguageMap::value_type entry(l, definesString); |
149 | 0 | i = this->Configs[config].DefinesByLanguage.insert(entry).first; |
150 | 0 | } |
151 | 0 | return i->second; |
152 | 0 | } |
153 | | |
154 | | std::string cmCommonTargetGenerator::GetIncludes(std::string const& l, |
155 | | std::string const& config) |
156 | 0 | { |
157 | 0 | auto i = this->Configs[config].IncludesByLanguage.find(l); |
158 | 0 | if (i == this->Configs[config].IncludesByLanguage.end()) { |
159 | 0 | std::string includes; |
160 | 0 | this->AddIncludeFlags(includes, l, config); |
161 | 0 | ByLanguageMap::value_type entry(l, includes); |
162 | 0 | i = this->Configs[config].IncludesByLanguage.insert(entry).first; |
163 | 0 | } |
164 | 0 | return i->second; |
165 | 0 | } |
166 | | |
167 | | cmCommonTargetGenerator::LinkedTargetDirs |
168 | | cmCommonTargetGenerator::GetLinkedTargetDirectories( |
169 | | std::string const& lang, std::string const& config) const |
170 | 0 | { |
171 | 0 | LinkedTargetDirs dirs; |
172 | 0 | std::set<cmGeneratorTarget const*> forward_emitted; |
173 | 0 | std::set<cmGeneratorTarget const*> direct_emitted; |
174 | 0 | cmGlobalCommonGenerator* const gg = this->GlobalCommonGenerator; |
175 | |
|
176 | 0 | enum class Forwarding |
177 | 0 | { |
178 | 0 | Yes, |
179 | 0 | No |
180 | 0 | }; |
181 | |
|
182 | 0 | if (cmComputeLinkInformation* cli = |
183 | 0 | this->GeneratorTarget->GetLinkInformation(config)) { |
184 | |
|
185 | 0 | auto findSyntheticTarget = |
186 | 0 | [this, |
187 | 0 | &config](cmGeneratorTarget const* linkee) -> cmGeneratorTarget const* { |
188 | 0 | if (!linkee) { |
189 | 0 | return nullptr; |
190 | 0 | } |
191 | | |
192 | | // Check the map of direct synthetic dependencies for a substitute |
193 | 0 | auto const& synthDeps = this->GeneratorTarget->GetSyntheticDeps(config); |
194 | 0 | auto it = synthDeps.find(linkee); |
195 | 0 | if (it != synthDeps.end() && !it->second.empty()) { |
196 | 0 | return it->second.front(); |
197 | 0 | } |
198 | | |
199 | | // Check linked targets to finding synthetic targets for transitive deps |
200 | 0 | std::vector<cmGeneratorTarget const*> pending; |
201 | 0 | std::set<cmGeneratorTarget const*> visited; |
202 | 0 | for (auto const& dep : synthDeps) { |
203 | 0 | for (auto const* synth : dep.second) { |
204 | 0 | if (synth && visited.insert(synth).second) { |
205 | 0 | pending.push_back(synth); |
206 | 0 | } |
207 | 0 | } |
208 | 0 | } |
209 | |
|
210 | 0 | while (!pending.empty()) { |
211 | 0 | auto const* current = pending.back(); |
212 | 0 | pending.pop_back(); |
213 | 0 | auto const& transitiveSynthDeps = current->GetSyntheticDeps(config); |
214 | 0 | auto itLinkeeSynth = transitiveSynthDeps.find(linkee); |
215 | 0 | if (itLinkeeSynth != transitiveSynthDeps.end() && |
216 | 0 | !itLinkeeSynth->second.empty()) { |
217 | 0 | return itLinkeeSynth->second.front(); |
218 | 0 | } |
219 | 0 | for (auto const& entry : transitiveSynthDeps) { |
220 | 0 | for (auto const* synth : entry.second) { |
221 | 0 | if (synth && visited.insert(synth).second) { |
222 | 0 | pending.push_back(synth); |
223 | 0 | } |
224 | 0 | } |
225 | 0 | } |
226 | 0 | } |
227 | | |
228 | 0 | return nullptr; |
229 | 0 | }; |
230 | |
|
231 | 0 | auto addLinkedTarget = [this, &lang, &config, &dirs, &direct_emitted, |
232 | 0 | &forward_emitted, &findSyntheticTarget, |
233 | 0 | gg](cmGeneratorTarget const* linkee, |
234 | 0 | Forwarding forward) { |
235 | | // Check if the linkee has a synthetic target to use for importing |
236 | 0 | cmGeneratorTarget const* mappedLinkee = linkee; |
237 | 0 | if (auto const* synth = findSyntheticTarget(linkee)) { |
238 | 0 | mappedLinkee = synth; |
239 | 0 | } |
240 | |
|
241 | 0 | if (mappedLinkee && |
242 | 0 | !mappedLinkee->IsImported() |
243 | | // Skip targets that build after this one in a static lib cycle. |
244 | 0 | && gg->TargetOrderIndexLess(mappedLinkee, this->GeneratorTarget) |
245 | | // We can ignore the INTERFACE_LIBRARY items because |
246 | | // Target->GetLinkInformation already processed their |
247 | | // link interface and they don't have any output themselves. |
248 | 0 | && (mappedLinkee->GetType() != cmStateEnums::INTERFACE_LIBRARY |
249 | | // Synthesized targets may have relevant rules. |
250 | 0 | || mappedLinkee->IsSynthetic()) && |
251 | 0 | ((lang == "CXX"_s && mappedLinkee->HaveCxx20ModuleSources()) || |
252 | 0 | (lang == "Fortran"_s && |
253 | 0 | mappedLinkee->HaveFortranSources(config)))) { |
254 | 0 | cmLocalGenerator* lg = mappedLinkee->GetLocalGenerator(); |
255 | 0 | std::string di = mappedLinkee->GetSupportDirectory(); |
256 | 0 | if (lg->GetGlobalGenerator()->IsMultiConfig()) { |
257 | 0 | di = cmStrCat(di, '/', config); |
258 | 0 | } |
259 | 0 | if (forward == Forwarding::Yes && |
260 | 0 | forward_emitted.insert(mappedLinkee).second) { |
261 | 0 | dirs.Forward.push_back(di); |
262 | 0 | } |
263 | 0 | if (direct_emitted.insert(mappedLinkee).second) { |
264 | 0 | dirs.Direct.emplace_back(di); |
265 | 0 | } |
266 | 0 | } |
267 | 0 | }; |
268 | 0 | for (auto const& item : cli->GetItems()) { |
269 | 0 | if (item.Target) { |
270 | 0 | addLinkedTarget(item.Target, Forwarding::No); |
271 | 0 | } else if (item.ObjectSource && lang == "Fortran"_s |
272 | | /* Object source files do not have a language associated with |
273 | | them. */ |
274 | 0 | /* && item.ObjectSource->GetLanguage() == "Fortran"_s*/) { |
275 | | // Fortran modules provided by `$<TARGET_OBJECTS>` as linked items |
276 | | // should be collated for use in this target. |
277 | 0 | addLinkedTarget(this->LocalCommonGenerator->FindGeneratorTargetToUse( |
278 | 0 | item.ObjectSource->GetObjectLibrary()), |
279 | 0 | Forwarding::Yes); |
280 | 0 | } |
281 | 0 | } |
282 | 0 | for (cmGeneratorTarget const* target : cli->GetExternalObjectTargets()) { |
283 | 0 | addLinkedTarget(target, Forwarding::No); |
284 | 0 | } |
285 | 0 | if (lang == "Fortran"_s) { |
286 | | // Fortran modules provided by `$<TARGET_OBJECTS>` as sources should be |
287 | | // collated for use in this target. |
288 | 0 | for (cmGeneratorTarget const* target : |
289 | 0 | this->GeneratorTarget->GetSourceObjectLibraries(config)) { |
290 | 0 | addLinkedTarget(target, Forwarding::Yes); |
291 | 0 | } |
292 | 0 | } |
293 | 0 | } |
294 | 0 | return dirs; |
295 | 0 | } |
296 | | |
297 | | std::string cmCommonTargetGenerator::ComputeTargetCompilePDB( |
298 | | std::string const& config) const |
299 | 0 | { |
300 | 0 | std::string compilePdbPath; |
301 | 0 | if (this->GeneratorTarget->GetType() > cmStateEnums::OBJECT_LIBRARY) { |
302 | 0 | return compilePdbPath; |
303 | 0 | } |
304 | | |
305 | 0 | compilePdbPath = this->GeneratorTarget->GetCompilePDBPath(config); |
306 | 0 | if (compilePdbPath.empty()) { |
307 | | // Match VS default: `$(IntDir)vc$(PlatformToolsetVersion).pdb`. |
308 | | // A trailing slash tells the toolchain to add its default file name. |
309 | 0 | compilePdbPath = this->GeneratorTarget->GetSupportDirectory(); |
310 | 0 | if (this->GlobalCommonGenerator->IsMultiConfig()) { |
311 | 0 | compilePdbPath += "/"; |
312 | 0 | compilePdbPath += config; |
313 | 0 | } |
314 | 0 | compilePdbPath += "/"; |
315 | 0 | if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) { |
316 | | // Match VS default for static libs: `$(IntDir)$(ProjectName).pdb`. |
317 | 0 | compilePdbPath += this->GeneratorTarget->GetName(); |
318 | 0 | compilePdbPath += ".pdb"; |
319 | 0 | } |
320 | 0 | } |
321 | |
|
322 | 0 | return compilePdbPath; |
323 | 0 | } |
324 | | |
325 | | std::string cmCommonTargetGenerator::GetManifests(std::string const& config) |
326 | 0 | { |
327 | 0 | std::vector<cmSourceFile const*> manifest_srcs; |
328 | 0 | this->GeneratorTarget->GetManifests(manifest_srcs, config); |
329 | |
|
330 | 0 | std::vector<std::string> manifests; |
331 | 0 | manifests.reserve(manifest_srcs.size()); |
332 | |
|
333 | 0 | std::string lang = this->GeneratorTarget->GetLinkerLanguage(config); |
334 | 0 | std::string manifestFlag = this->Makefile->GetDefinition( |
335 | 0 | cmStrCat("CMAKE_", lang, "_LINKER_MANIFEST_FLAG")); |
336 | 0 | for (cmSourceFile const* manifest_src : manifest_srcs) { |
337 | 0 | manifests.push_back(manifestFlag + |
338 | 0 | this->LocalCommonGenerator->ConvertToOutputFormat( |
339 | 0 | this->LocalCommonGenerator->MaybeRelativeToWorkDir( |
340 | 0 | manifest_src->GetFullPath()), |
341 | 0 | cmOutputConverter::SHELL)); |
342 | 0 | } |
343 | |
|
344 | 0 | return cmJoin(manifests, " "); |
345 | 0 | } |
346 | | |
347 | | std::string cmCommonTargetGenerator::GetAIXExports(std::string const&) |
348 | 0 | { |
349 | 0 | std::string aixExports; |
350 | 0 | if (this->GeneratorTarget->IsAIX()) { |
351 | 0 | if (cmValue exportAll = |
352 | 0 | this->GeneratorTarget->GetProperty("AIX_EXPORT_ALL_SYMBOLS")) { |
353 | 0 | if (exportAll.IsOff()) { |
354 | 0 | aixExports = "-n"; |
355 | 0 | } |
356 | 0 | } |
357 | 0 | } |
358 | 0 | return aixExports; |
359 | 0 | } |
360 | | |
361 | | void cmCommonTargetGenerator::AppendOSXVerFlag(std::string& flags, |
362 | | std::string const& lang, |
363 | | char const* name, bool so) |
364 | 0 | { |
365 | | // Lookup the flag to specify the version. |
366 | 0 | std::string fvar = cmStrCat("CMAKE_", lang, "_OSX_", name, "_VERSION_FLAG"); |
367 | 0 | cmValue flag = this->Makefile->GetDefinition(fvar); |
368 | | |
369 | | // Skip if no such flag. |
370 | 0 | if (!flag) { |
371 | 0 | return; |
372 | 0 | } |
373 | | |
374 | | // Lookup the target version information. |
375 | 0 | int major; |
376 | 0 | int minor; |
377 | 0 | int patch; |
378 | 0 | std::string prop = cmStrCat("MACHO_", name, "_VERSION"); |
379 | 0 | std::string fallback_prop = so ? "SOVERSION" : "VERSION"; |
380 | 0 | this->GeneratorTarget->GetTargetVersionFallback(prop, fallback_prop, major, |
381 | 0 | minor, patch); |
382 | 0 | if (major > 0 || minor > 0 || patch > 0) { |
383 | | // Append the flag since a non-zero version is specified. |
384 | 0 | std::ostringstream vflag; |
385 | 0 | vflag << *flag << major << "." << minor << "." << patch; |
386 | 0 | this->LocalCommonGenerator->AppendFlags(flags, vflag.str()); |
387 | 0 | } |
388 | 0 | } |
389 | | |
390 | | std::string cmCommonTargetGenerator::GetCompilerLauncher( |
391 | | std::string const& lang, std::string const& config) |
392 | 0 | { |
393 | 0 | std::string compilerLauncher; |
394 | 0 | if (lang == "C" || lang == "CXX" || lang == "Fortran" || lang == "CUDA" || |
395 | 0 | lang == "HIP" || lang == "ISPC" || lang == "OBJC" || lang == "OBJCXX") { |
396 | 0 | std::string const clauncher_prop = cmStrCat(lang, "_COMPILER_LAUNCHER"); |
397 | 0 | cmValue clauncher = this->GeneratorTarget->GetProperty(clauncher_prop); |
398 | 0 | std::string const evaluatedClauncher = cmGeneratorExpression::Evaluate( |
399 | 0 | *clauncher, this->GeneratorTarget->GetLocalGenerator(), config, |
400 | 0 | this->GeneratorTarget, nullptr, this->GeneratorTarget, lang); |
401 | 0 | if (!evaluatedClauncher.empty()) { |
402 | 0 | compilerLauncher = evaluatedClauncher; |
403 | 0 | } |
404 | 0 | } |
405 | 0 | return compilerLauncher; |
406 | 0 | } |
407 | | |
408 | | std::string cmCommonTargetGenerator::GenerateCodeCheckRules( |
409 | | cmSourceFile const& source, std::string& compilerLauncher, |
410 | | std::string const& cmakeCmd, std::string const& config, |
411 | | std::function<std::string(std::string const&)> const& pathConverter) |
412 | 0 | { |
413 | 0 | auto const lang = source.GetLanguage(); |
414 | 0 | std::string tidy; |
415 | 0 | std::string iwyu; |
416 | 0 | std::string cpplint; |
417 | 0 | std::string cppcheck; |
418 | 0 | std::string icstat; |
419 | 0 | std::string pvs; |
420 | |
|
421 | 0 | auto evaluateProp = [&](std::string const& prop) -> std::string { |
422 | 0 | auto const value = this->GeneratorTarget->GetProperty(prop); |
423 | 0 | if (!value) { |
424 | 0 | return std::string{}; |
425 | 0 | } |
426 | 0 | auto evaluatedProp = cmGeneratorExpression::Evaluate( |
427 | 0 | *value, this->GeneratorTarget->GetLocalGenerator(), config, |
428 | 0 | this->GeneratorTarget, nullptr, this->GeneratorTarget, lang); |
429 | 0 | return evaluatedProp; |
430 | 0 | }; |
431 | 0 | std::string const tidy_prop = cmStrCat(lang, "_CLANG_TIDY"); |
432 | 0 | tidy = evaluateProp(tidy_prop); |
433 | |
|
434 | 0 | if (lang == "C" || lang == "CXX") { |
435 | 0 | std::string const iwyu_prop = cmStrCat(lang, "_INCLUDE_WHAT_YOU_USE"); |
436 | 0 | iwyu = evaluateProp(iwyu_prop); |
437 | |
|
438 | 0 | std::string const cpplint_prop = cmStrCat(lang, "_CPPLINT"); |
439 | 0 | cpplint = evaluateProp(cpplint_prop); |
440 | |
|
441 | 0 | std::string const cppcheck_prop = cmStrCat(lang, "_CPPCHECK"); |
442 | 0 | cppcheck = evaluateProp(cppcheck_prop); |
443 | |
|
444 | 0 | std::string const icstat_prop = cmStrCat(lang, "_ICSTAT"); |
445 | 0 | icstat = evaluateProp(icstat_prop); |
446 | |
|
447 | 0 | std::string const pvs_prop = cmStrCat(lang, "_PVS_STUDIO"); |
448 | 0 | pvs = evaluateProp(pvs_prop); |
449 | 0 | } |
450 | |
|
451 | 0 | if (cmNonempty(iwyu) || cmNonempty(tidy) || cmNonempty(cpplint) || |
452 | 0 | cmNonempty(cppcheck) || cmNonempty(icstat) || cmNonempty(pvs)) { |
453 | 0 | std::string code_check = cmakeCmd + " -E __run_co_compile"; |
454 | 0 | if (!compilerLauncher.empty()) { |
455 | | // In __run_co_compile case the launcher command is supplied |
456 | | // via --launcher=<maybe-list> and consumed |
457 | 0 | code_check = |
458 | 0 | cmStrCat(std::move(code_check), " --launcher=", |
459 | 0 | this->GeneratorTarget->GetLocalGenerator()->EscapeForShell( |
460 | 0 | compilerLauncher)); |
461 | 0 | compilerLauncher.clear(); |
462 | 0 | } |
463 | 0 | if (cmNonempty(iwyu)) { |
464 | 0 | code_check += " --iwyu="; |
465 | | |
466 | | // Only add --driver-mode if it is not already specified, as adding |
467 | | // it unconditionally might override a user-specified driver-mode |
468 | 0 | if (iwyu.find("--driver-mode=") == std::string::npos) { |
469 | 0 | cmValue const p = this->Makefile->GetDefinition( |
470 | 0 | cmStrCat("CMAKE_", lang, "_INCLUDE_WHAT_YOU_USE_DRIVER_MODE")); |
471 | 0 | std::string driverMode; |
472 | |
|
473 | 0 | if (cmNonempty(p)) { |
474 | 0 | driverMode = *p; |
475 | 0 | } else { |
476 | 0 | driverMode = lang == "C" ? "gcc" : "g++"; |
477 | 0 | } |
478 | |
|
479 | 0 | code_check += |
480 | 0 | this->GeneratorTarget->GetLocalGenerator()->EscapeForShell( |
481 | 0 | cmStrCat(iwyu, ";--driver-mode=", driverMode)); |
482 | 0 | } else { |
483 | 0 | code_check += |
484 | 0 | this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(iwyu); |
485 | 0 | } |
486 | 0 | } |
487 | 0 | if (cmNonempty(tidy)) { |
488 | 0 | code_check += " --tidy="; |
489 | 0 | cmValue const p = this->Makefile->GetDefinition( |
490 | 0 | cmStrCat("CMAKE_", lang, "_CLANG_TIDY_DRIVER_MODE")); |
491 | 0 | std::string driverMode; |
492 | 0 | if (cmNonempty(p)) { |
493 | 0 | driverMode = *p; |
494 | 0 | } else { |
495 | 0 | driverMode = lang == "C" ? "gcc" : "g++"; |
496 | 0 | } |
497 | |
|
498 | 0 | auto const generatorName = this->GeneratorTarget->GetLocalGenerator() |
499 | 0 | ->GetGlobalGenerator() |
500 | 0 | ->GetName(); |
501 | 0 | auto const clangTidyExportFixedDir = |
502 | 0 | this->GeneratorTarget->GetClangTidyExportFixesDirectory(lang); |
503 | 0 | auto fixesFile = this->GetClangTidyReplacementsFilePath( |
504 | 0 | clangTidyExportFixedDir, source, config); |
505 | 0 | std::string exportFixes; |
506 | 0 | if (!clangTidyExportFixedDir.empty()) { |
507 | 0 | this->GlobalCommonGenerator->AddClangTidyExportFixesDir( |
508 | 0 | clangTidyExportFixedDir); |
509 | 0 | } |
510 | 0 | if (generatorName.find("Make") != std::string::npos) { |
511 | 0 | if (!clangTidyExportFixedDir.empty()) { |
512 | 0 | this->GlobalCommonGenerator->AddClangTidyExportFixesFile(fixesFile); |
513 | 0 | cmSystemTools::MakeDirectory( |
514 | 0 | cmSystemTools::GetFilenamePath(fixesFile)); |
515 | 0 | fixesFile = this->GeneratorTarget->GetLocalGenerator() |
516 | 0 | ->MaybeRelativeToCurBinDir(fixesFile); |
517 | 0 | exportFixes = cmStrCat(";--export-fixes=", fixesFile); |
518 | 0 | } |
519 | 0 | code_check += |
520 | 0 | this->GeneratorTarget->GetLocalGenerator()->EscapeForShell( |
521 | 0 | cmStrCat(tidy, ";--extra-arg-before=--driver-mode=", driverMode, |
522 | 0 | exportFixes)); |
523 | 0 | } else if (generatorName.find("Ninja") != std::string::npos || |
524 | 0 | generatorName.find("FASTBuild") != std::string::npos) { |
525 | 0 | if (!clangTidyExportFixedDir.empty()) { |
526 | 0 | this->GlobalCommonGenerator->AddClangTidyExportFixesFile(fixesFile); |
527 | 0 | cmSystemTools::MakeDirectory( |
528 | 0 | cmSystemTools::GetFilenamePath(fixesFile)); |
529 | 0 | if (pathConverter) { |
530 | 0 | fixesFile = pathConverter(fixesFile); |
531 | 0 | } |
532 | 0 | exportFixes = cmStrCat(";--export-fixes=", fixesFile); |
533 | 0 | } |
534 | 0 | code_check += |
535 | 0 | this->GeneratorTarget->GetLocalGenerator()->EscapeForShell( |
536 | 0 | cmStrCat(tidy, ";--extra-arg-before=--driver-mode=", driverMode, |
537 | 0 | exportFixes)); |
538 | 0 | } |
539 | 0 | } |
540 | 0 | if (cmNonempty(pvs)) { |
541 | 0 | cmMakefile* mf = |
542 | 0 | this->GeneratorTarget->GetLocalGenerator()->GetMakefile(); |
543 | 0 | std::string extraPvsArgs; |
544 | 0 | if (lang == "CXX") { |
545 | 0 | extraPvsArgs += |
546 | 0 | cmStrCat(";--cxx;", mf->GetDefinition("CMAKE_CXX_COMPILER")); |
547 | 0 | } else if (lang == "C") { |
548 | 0 | extraPvsArgs += |
549 | 0 | cmStrCat(";--cc;", mf->GetDefinition("CMAKE_C_COMPILER")); |
550 | 0 | } |
551 | | // cocompile args |
552 | 0 | code_check += " --pvs-studio="; |
553 | 0 | code_check += this->GeneratorTarget->GetLocalGenerator()->EscapeForShell( |
554 | 0 | cmStrCat(pvs, extraPvsArgs)); |
555 | 0 | code_check += " --object="; |
556 | 0 | code_check += |
557 | 0 | this->GeneratorTarget->GetLocalGenerator()->ConvertToOutputFormat( |
558 | 0 | cmSystemTools::CollapseFullPath( |
559 | 0 | cmStrCat(this->GeneratorTarget->GetObjectDirectory(config), '/', |
560 | 0 | this->GeneratorTarget->GetObjectName(&source))), |
561 | 0 | cmOutputConverter::SHELL); |
562 | 0 | } |
563 | 0 | if (cmNonempty(cpplint)) { |
564 | 0 | code_check += " --cpplint="; |
565 | 0 | code_check += |
566 | 0 | this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(cpplint); |
567 | 0 | } |
568 | 0 | if (cmNonempty(cppcheck)) { |
569 | 0 | code_check += " --cppcheck="; |
570 | 0 | code_check += |
571 | 0 | this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(cppcheck); |
572 | 0 | } |
573 | 0 | if (cmNonempty(icstat)) { |
574 | 0 | code_check += " --icstat="; |
575 | 0 | std::string checksParam{}; |
576 | 0 | std::string dbParam{}; |
577 | | // Set default values for mandatory parameters |
578 | 0 | std::string checksFile{ "cstat_sel_checks.txt" }; |
579 | 0 | std::string dbFile{ "cstat.db" }; |
580 | | // Populate the command line with C-STAT |
581 | | // mandatory parameters unless specified |
582 | 0 | if (icstat.find("--checks=") == std::string::npos) { |
583 | 0 | checksParam = cmStrCat(";--checks=", checksFile); |
584 | 0 | } |
585 | 0 | if (icstat.find("--db=") == std::string::npos) { |
586 | 0 | dbParam = cmStrCat(";--db=", dbFile); |
587 | 0 | } |
588 | 0 | code_check += this->GeneratorTarget->GetLocalGenerator()->EscapeForShell( |
589 | 0 | cmStrCat(icstat, checksParam, dbParam)); |
590 | 0 | } |
591 | 0 | if (cmNonempty(tidy) || (cmNonempty(cpplint)) || (cmNonempty(cppcheck)) || |
592 | 0 | cmNonempty(pvs)) { |
593 | 0 | code_check += " --source="; |
594 | 0 | code_check += |
595 | 0 | this->GeneratorTarget->GetLocalGenerator()->ConvertToOutputFormat( |
596 | 0 | source.GetFullPath(), cmOutputConverter::SHELL); |
597 | 0 | } |
598 | 0 | code_check += " -- "; |
599 | 0 | return code_check; |
600 | 0 | } |
601 | 0 | return ""; |
602 | 0 | } |
603 | | |
604 | | std::string cmCommonTargetGenerator::GetLinkerLauncher( |
605 | | std::string const& config) |
606 | 0 | { |
607 | 0 | std::string lang = this->GeneratorTarget->GetLinkerLanguage(config); |
608 | 0 | std::string propName = lang + "_LINKER_LAUNCHER"; |
609 | 0 | cmValue launcherProp = this->GeneratorTarget->GetProperty(propName); |
610 | 0 | if (cmNonempty(launcherProp)) { |
611 | 0 | cm::GenEx::Context context(this->LocalCommonGenerator, config, lang); |
612 | 0 | cmGeneratorExpressionDAGChecker dagChecker{ this->GeneratorTarget, |
613 | 0 | propName, nullptr, nullptr, |
614 | 0 | context }; |
615 | 0 | std::string evaluatedLinklauncher = cmGeneratorExpression::Evaluate( |
616 | 0 | *launcherProp, context.LG, context.Config, this->GeneratorTarget, |
617 | 0 | &dagChecker, this->GeneratorTarget, context.Language); |
618 | | // Convert ;-delimited list to single string |
619 | 0 | cmList args{ evaluatedLinklauncher, cmList::EmptyElements::Yes }; |
620 | 0 | if (!args.empty()) { |
621 | 0 | args[0] = this->LocalCommonGenerator->ConvertToOutputFormat( |
622 | 0 | args[0], cmOutputConverter::SHELL); |
623 | 0 | for (std::string& i : cmMakeRange(args.begin() + 1, args.end())) { |
624 | 0 | i = this->LocalCommonGenerator->EscapeForShell(i); |
625 | 0 | } |
626 | 0 | return cmJoin(args, " "); |
627 | 0 | } |
628 | 0 | } |
629 | 0 | return std::string(); |
630 | 0 | } |
631 | | |
632 | | bool cmCommonTargetGenerator::HaveRequiredLanguages( |
633 | | std::vector<cmSourceFile const*> const& sources, |
634 | | std::set<std::string>& languagesNeeded) const |
635 | 0 | { |
636 | 0 | for (cmSourceFile const* sf : sources) { |
637 | 0 | languagesNeeded.insert(sf->GetLanguage()); |
638 | 0 | } |
639 | |
|
640 | 0 | auto* makefile = this->Makefile; |
641 | 0 | auto* state = makefile->GetState(); |
642 | 0 | auto unary = [&state, &makefile](std::string const& lang) -> bool { |
643 | 0 | bool const valid = state->GetLanguageEnabled(lang); |
644 | 0 | if (!valid) { |
645 | 0 | makefile->IssueMessage( |
646 | 0 | MessageType::FATAL_ERROR, |
647 | 0 | cmStrCat("The language ", lang, |
648 | 0 | " was requested for compilation but was not enabled." |
649 | 0 | " To enable a language it needs to be specified in a" |
650 | 0 | " 'project' or 'enable_language' command in the root" |
651 | 0 | " CMakeLists.txt")); |
652 | 0 | } |
653 | 0 | return valid; |
654 | 0 | }; |
655 | 0 | return std::all_of(languagesNeeded.cbegin(), languagesNeeded.cend(), unary); |
656 | 0 | } |
657 | | |
658 | | void cmCommonTargetGenerator::ComputeRustFlagsForObjects( |
659 | | std::string& linkCrates, std::string& nativeObjects, |
660 | | std::vector<std::string> const& objects) |
661 | 0 | { |
662 | 0 | std::stringstream rlibsArgs; |
663 | 0 | std::stringstream objectsArgs; |
664 | 0 | auto const processObject = [&](std::string const& obj) { |
665 | 0 | cm::filesystem::path const objPath(obj); |
666 | 0 | if (objPath.extension() == ".rlib") { |
667 | | // Drop the "lib..." prefix and the ".rs" suffix. The prefix is required |
668 | | // by Rust on the crate rlib file, but is hidden from the user when using |
669 | | // the crate from Rust source code, so we drop it to be consistent with |
670 | | // common usage in Rust. |
671 | 0 | std::string objStem = objPath.stem().string(); |
672 | 0 | objStem = objStem.substr(3, objStem.length() - 6); |
673 | 0 | rlibsArgs << " --extern=" << objStem << "=" |
674 | 0 | << this->LocalCommonGenerator->ConvertToOutputFormat( |
675 | 0 | obj, cmOutputConverter::SHELL); |
676 | 0 | } else { |
677 | 0 | objectsArgs << " -Clink-arg=" |
678 | 0 | << this->LocalCommonGenerator->ConvertToOutputFormat( |
679 | 0 | obj, cmOutputConverter::SHELL); |
680 | 0 | } |
681 | 0 | }; |
682 | 0 | for (auto const& obj : objects) { |
683 | 0 | processObject(obj); |
684 | 0 | } |
685 | 0 | linkCrates += rlibsArgs.str(); |
686 | 0 | nativeObjects += objectsArgs.str(); |
687 | 0 | } |