1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCommonTargetGenerator.h"
#include <algorithm>
#include <sstream>
#include <type_traits>
#include <utility>
#include <cm/string_view>
#include <cmext/string_view>
#include "cmComputeLinkInformation.h"
#include "cmGeneratorExpression.h"
#include "cmGeneratorExpressionDAGChecker.h"
#include "cmGeneratorTarget.h"
#include "cmGlobalCommonGenerator.h"
#include "cmGlobalGenerator.h"
#include "cmList.h"
#include "cmLocalCommonGenerator.h"
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
#include "cmMessageType.h"
#include "cmOutputConverter.h"
#include "cmRange.h"
#include "cmSourceFile.h"
#include "cmState.h"
#include "cmStateTypes.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmValue.h"
cmCommonTargetGenerator::cmCommonTargetGenerator(cmGeneratorTarget* gt)
: GeneratorTarget(gt)
, Makefile(gt->Makefile)
, LocalCommonGenerator(
static_cast<cmLocalCommonGenerator*>(gt->LocalGenerator))
, GlobalCommonGenerator(static_cast<cmGlobalCommonGenerator*>(
gt->LocalGenerator->GetGlobalGenerator()))
, ConfigNames(this->LocalCommonGenerator->GetConfigNames())
{
}
cmCommonTargetGenerator::~cmCommonTargetGenerator() = default;
std::vector<std::string> const& cmCommonTargetGenerator::GetConfigNames() const
{
return this->ConfigNames;
}
cmValue cmCommonTargetGenerator::GetFeature(const std::string& feature,
const std::string& config)
{
return this->GeneratorTarget->GetFeature(feature, config);
}
void cmCommonTargetGenerator::AppendFortranFormatFlags(
std::string& flags, cmSourceFile const& source)
{
const std::string srcfmt = source.GetSafeProperty("Fortran_FORMAT");
cmOutputConverter::FortranFormat format =
cmOutputConverter::GetFortranFormat(srcfmt);
if (format == cmOutputConverter::FortranFormatNone) {
std::string const& tgtfmt =
this->GeneratorTarget->GetSafeProperty("Fortran_FORMAT");
format = cmOutputConverter::GetFortranFormat(tgtfmt);
}
const char* var = nullptr;
switch (format) {
case cmOutputConverter::FortranFormatFixed:
var = "CMAKE_Fortran_FORMAT_FIXED_FLAG";
break;
case cmOutputConverter::FortranFormatFree:
var = "CMAKE_Fortran_FORMAT_FREE_FLAG";
break;
default:
break;
}
if (var) {
this->LocalCommonGenerator->AppendFlags(
flags, this->Makefile->GetSafeDefinition(var));
}
}
void cmCommonTargetGenerator::AppendFortranPreprocessFlags(
std::string& flags, cmSourceFile const& source,
PreprocessFlagsRequired requires_pp)
{
const std::string srcpp = source.GetSafeProperty("Fortran_PREPROCESS");
cmOutputConverter::FortranPreprocess preprocess =
cmOutputConverter::GetFortranPreprocess(srcpp);
if (preprocess == cmOutputConverter::FortranPreprocess::Unset) {
std::string const& tgtpp =
this->GeneratorTarget->GetSafeProperty("Fortran_PREPROCESS");
preprocess = cmOutputConverter::GetFortranPreprocess(tgtpp);
}
const char* var = nullptr;
switch (preprocess) {
case cmOutputConverter::FortranPreprocess::Needed:
if (requires_pp == PreprocessFlagsRequired::YES) {
var = "CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_ON";
}
break;
case cmOutputConverter::FortranPreprocess::NotNeeded:
var = "CMAKE_Fortran_COMPILE_OPTIONS_PREPROCESS_OFF";
break;
default:
break;
}
if (var) {
this->LocalCommonGenerator->AppendCompileOptions(
flags, this->Makefile->GetSafeDefinition(var));
}
}
std::string cmCommonTargetGenerator::GetFlags(const std::string& l,
const std::string& config,
const std::string& arch)
{
const std::string key = config + arch;
auto i = this->Configs[key].FlagsByLanguage.find(l);
if (i == this->Configs[key].FlagsByLanguage.end()) {
std::string flags;
this->LocalCommonGenerator->GetTargetCompileFlags(this->GeneratorTarget,
config, l, flags, arch);
ByLanguageMap::value_type entry(l, flags);
i = this->Configs[key].FlagsByLanguage.insert(entry).first;
}
return i->second;
}
std::string cmCommonTargetGenerator::GetDefines(const std::string& l,
const std::string& config)
{
auto i = this->Configs[config].DefinesByLanguage.find(l);
if (i == this->Configs[config].DefinesByLanguage.end()) {
std::set<std::string> defines;
this->LocalCommonGenerator->GetTargetDefines(this->GeneratorTarget, config,
l, defines);
std::string definesString;
this->LocalCommonGenerator->JoinDefines(defines, definesString, l);
ByLanguageMap::value_type entry(l, definesString);
i = this->Configs[config].DefinesByLanguage.insert(entry).first;
}
return i->second;
}
std::string cmCommonTargetGenerator::GetIncludes(std::string const& l,
const std::string& config)
{
auto i = this->Configs[config].IncludesByLanguage.find(l);
if (i == this->Configs[config].IncludesByLanguage.end()) {
std::string includes;
this->AddIncludeFlags(includes, l, config);
ByLanguageMap::value_type entry(l, includes);
i = this->Configs[config].IncludesByLanguage.insert(entry).first;
}
return i->second;
}
cmCommonTargetGenerator::LinkedTargetDirs
cmCommonTargetGenerator::GetLinkedTargetDirectories(
const std::string& lang, const std::string& config) const
{
LinkedTargetDirs dirs;
std::set<cmGeneratorTarget const*> forward_emitted;
std::set<cmGeneratorTarget const*> direct_emitted;
cmGlobalCommonGenerator* const gg = this->GlobalCommonGenerator;
enum class Forwarding
{
Yes,
No
};
if (cmComputeLinkInformation* cli =
this->GeneratorTarget->GetLinkInformation(config)) {
auto addLinkedTarget =
[this, &lang, &config, &dirs, &direct_emitted, &forward_emitted,
gg](cmGeneratorTarget const* linkee, Forwarding forward) {
if (linkee &&
!linkee->IsImported()
// Skip targets that build after this one in a static lib cycle.
&& gg->TargetOrderIndexLess(linkee, this->GeneratorTarget)
// We can ignore the INTERFACE_LIBRARY items because
// Target->GetLinkInformation already processed their
// link interface and they don't have any output themselves.
&& (linkee->GetType() != cmStateEnums::INTERFACE_LIBRARY
// Synthesized targets may have relevant rules.
|| linkee->IsSynthetic()) &&
((lang == "CXX"_s && linkee->HaveCxx20ModuleSources()) ||
(lang == "Fortran"_s && linkee->HaveFortranSources(config)))) {
cmLocalGenerator* lg = linkee->GetLocalGenerator();
std::string di = cmStrCat(lg->GetCurrentBinaryDirectory(), '/',
lg->GetTargetDirectory(linkee));
if (lg->GetGlobalGenerator()->IsMultiConfig()) {
di = cmStrCat(di, '/', config);
}
if (forward == Forwarding::Yes &&
forward_emitted.insert(linkee).second) {
dirs.Forward.push_back(di);
}
if (direct_emitted.insert(linkee).second) {
dirs.Direct.emplace_back(di);
}
}
};
for (auto const& item : cli->GetItems()) {
if (item.Target) {
addLinkedTarget(item.Target, Forwarding::No);
} else if (item.ObjectSource && lang == "Fortran"_s
/* Object source files do not have a language associated with
them. */
/* && item.ObjectSource->GetLanguage() == "Fortran"_s*/) {
// Fortran modules provided by `$<TARGET_OBJECTS>` as linked items
// should be collated for use in this target.
addLinkedTarget(this->LocalCommonGenerator->FindGeneratorTargetToUse(
item.ObjectSource->GetObjectLibrary()),
Forwarding::Yes);
}
}
for (cmGeneratorTarget const* target : cli->GetExternalObjectTargets()) {
addLinkedTarget(target, Forwarding::No);
}
if (lang == "Fortran"_s) {
// Fortran modules provided by `$<TARGET_OBJECTS>` as sources should be
// collated for use in this target.
for (cmGeneratorTarget const* target :
this->GeneratorTarget->GetSourceObjectLibraries(config)) {
addLinkedTarget(target, Forwarding::Yes);
}
}
}
return dirs;
}
std::string cmCommonTargetGenerator::ComputeTargetCompilePDB(
const std::string& config) const
{
std::string compilePdbPath;
if (this->GeneratorTarget->GetType() > cmStateEnums::OBJECT_LIBRARY) {
return compilePdbPath;
}
compilePdbPath = this->GeneratorTarget->GetCompilePDBPath(config);
if (compilePdbPath.empty()) {
// Match VS default: `$(IntDir)vc$(PlatformToolsetVersion).pdb`.
// A trailing slash tells the toolchain to add its default file name.
compilePdbPath = this->GeneratorTarget->GetSupportDirectory();
if (this->GlobalCommonGenerator->IsMultiConfig()) {
compilePdbPath += "/";
compilePdbPath += config;
}
compilePdbPath += "/";
if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
// Match VS default for static libs: `$(IntDir)$(ProjectName).pdb`.
compilePdbPath += this->GeneratorTarget->GetName();
compilePdbPath += ".pdb";
}
}
return compilePdbPath;
}
std::string cmCommonTargetGenerator::GetManifests(const std::string& config)
{
std::vector<cmSourceFile const*> manifest_srcs;
this->GeneratorTarget->GetManifests(manifest_srcs, config);
std::vector<std::string> manifests;
manifests.reserve(manifest_srcs.size());
std::string lang = this->GeneratorTarget->GetLinkerLanguage(config);
std::string manifestFlag =
this->Makefile->GetDefinition("CMAKE_" + lang + "_LINKER_MANIFEST_FLAG");
for (cmSourceFile const* manifest_src : manifest_srcs) {
manifests.push_back(manifestFlag +
this->LocalCommonGenerator->ConvertToOutputFormat(
this->LocalCommonGenerator->MaybeRelativeToWorkDir(
manifest_src->GetFullPath()),
cmOutputConverter::SHELL));
}
return cmJoin(manifests, " ");
}
std::string cmCommonTargetGenerator::GetAIXExports(std::string const&)
{
std::string aixExports;
if (this->GeneratorTarget->IsAIX()) {
if (cmValue exportAll =
this->GeneratorTarget->GetProperty("AIX_EXPORT_ALL_SYMBOLS")) {
if (cmIsOff(*exportAll)) {
aixExports = "-n";
}
}
}
return aixExports;
}
void cmCommonTargetGenerator::AppendOSXVerFlag(std::string& flags,
const std::string& lang,
const char* name, bool so)
{
// Lookup the flag to specify the version.
std::string fvar = cmStrCat("CMAKE_", lang, "_OSX_", name, "_VERSION_FLAG");
cmValue flag = this->Makefile->GetDefinition(fvar);
// Skip if no such flag.
if (!flag) {
return;
}
// Lookup the target version information.
int major;
int minor;
int patch;
std::string prop = cmStrCat("MACHO_", name, "_VERSION");
std::string fallback_prop = so ? "SOVERSION" : "VERSION";
this->GeneratorTarget->GetTargetVersionFallback(prop, fallback_prop, major,
minor, patch);
if (major > 0 || minor > 0 || patch > 0) {
// Append the flag since a non-zero version is specified.
std::ostringstream vflag;
vflag << *flag << major << "." << minor << "." << patch;
this->LocalCommonGenerator->AppendFlags(flags, vflag.str());
}
}
std::string cmCommonTargetGenerator::GetCompilerLauncher(
std::string const& lang, std::string const& config)
{
std::string compilerLauncher;
if (lang == "C" || lang == "CXX" || lang == "Fortran" || lang == "CUDA" ||
lang == "HIP" || lang == "ISPC" || lang == "OBJC" || lang == "OBJCXX") {
std::string const clauncher_prop = cmStrCat(lang, "_COMPILER_LAUNCHER");
cmValue clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
std::string const evaluatedClauncher = cmGeneratorExpression::Evaluate(
*clauncher, this->GeneratorTarget->GetLocalGenerator(), config,
this->GeneratorTarget, nullptr, this->GeneratorTarget, lang);
if (!evaluatedClauncher.empty()) {
compilerLauncher = evaluatedClauncher;
}
}
return compilerLauncher;
}
std::string cmCommonTargetGenerator::GenerateCodeCheckRules(
cmSourceFile const& source, std::string& compilerLauncher,
std::string const& cmakeCmd, std::string const& config,
std::function<std::string(std::string const&)> const& pathConverter)
{
auto const lang = source.GetLanguage();
std::string tidy;
std::string iwyu;
std::string cpplint;
std::string cppcheck;
auto evaluateProp = [&](std::string const& prop) -> std::string {
auto const value = this->GeneratorTarget->GetProperty(prop);
if (!value) {
return std::string{};
}
auto evaluatedProp = cmGeneratorExpression::Evaluate(
*value, this->GeneratorTarget->GetLocalGenerator(), config,
this->GeneratorTarget, nullptr, this->GeneratorTarget, lang);
return evaluatedProp;
};
std::string const tidy_prop = cmStrCat(lang, "_CLANG_TIDY");
tidy = evaluateProp(tidy_prop);
if (lang == "C" || lang == "CXX") {
std::string const iwyu_prop = cmStrCat(lang, "_INCLUDE_WHAT_YOU_USE");
iwyu = evaluateProp(iwyu_prop);
std::string const cpplint_prop = cmStrCat(lang, "_CPPLINT");
cpplint = evaluateProp(cpplint_prop);
std::string const cppcheck_prop = cmStrCat(lang, "_CPPCHECK");
cppcheck = evaluateProp(cppcheck_prop);
}
if (cmNonempty(iwyu) || cmNonempty(tidy) || cmNonempty(cpplint) ||
cmNonempty(cppcheck)) {
std::string code_check = cmakeCmd + " -E __run_co_compile";
if (!compilerLauncher.empty()) {
// In __run_co_compile case the launcher command is supplied
// via --launcher=<maybe-list> and consumed
code_check += " --launcher=";
code_check += this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
compilerLauncher);
compilerLauncher.clear();
}
if (cmNonempty(iwyu)) {
code_check += " --iwyu=";
// Only add --driver-mode if it is not already specified, as adding
// it unconditionally might override a user-specified driver-mode
if (iwyu.find("--driver-mode=") == std::string::npos) {
cmValue const p = this->Makefile->GetDefinition(
cmStrCat("CMAKE_", lang, "_INCLUDE_WHAT_YOU_USE_DRIVER_MODE"));
std::string driverMode;
if (cmNonempty(p)) {
driverMode = *p;
} else {
driverMode = lang == "C" ? "gcc" : "g++";
}
code_check +=
this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
cmStrCat(iwyu, ";--driver-mode=", driverMode));
} else {
code_check +=
this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(iwyu);
}
}
if (cmNonempty(tidy)) {
code_check += " --tidy=";
cmValue const p = this->Makefile->GetDefinition(
"CMAKE_" + lang + "_CLANG_TIDY_DRIVER_MODE");
std::string driverMode;
if (cmNonempty(p)) {
driverMode = *p;
} else {
driverMode = lang == "C" ? "gcc" : "g++";
}
auto const generatorName = this->GeneratorTarget->GetLocalGenerator()
->GetGlobalGenerator()
->GetName();
auto const clangTidyExportFixedDir =
this->GeneratorTarget->GetClangTidyExportFixesDirectory(lang);
auto fixesFile = this->GetClangTidyReplacementsFilePath(
clangTidyExportFixedDir, source, config);
std::string exportFixes;
if (!clangTidyExportFixedDir.empty()) {
this->GlobalCommonGenerator->AddClangTidyExportFixesDir(
clangTidyExportFixedDir);
}
if (generatorName.find("Make") != std::string::npos) {
if (!clangTidyExportFixedDir.empty()) {
this->GlobalCommonGenerator->AddClangTidyExportFixesFile(fixesFile);
cmSystemTools::MakeDirectory(
cmSystemTools::GetFilenamePath(fixesFile));
fixesFile = this->GeneratorTarget->GetLocalGenerator()
->MaybeRelativeToCurBinDir(fixesFile);
exportFixes = cmStrCat(";--export-fixes=", fixesFile);
}
code_check +=
this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
cmStrCat(tidy, ";--extra-arg-before=--driver-mode=", driverMode,
exportFixes));
} else if (generatorName.find("Ninja") != std::string::npos) {
if (!clangTidyExportFixedDir.empty()) {
this->GlobalCommonGenerator->AddClangTidyExportFixesFile(fixesFile);
cmSystemTools::MakeDirectory(
cmSystemTools::GetFilenamePath(fixesFile));
if (!pathConverter) {
fixesFile = pathConverter(fixesFile);
}
exportFixes = cmStrCat(";--export-fixes=", fixesFile);
}
code_check +=
this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(
cmStrCat(tidy, ";--extra-arg-before=--driver-mode=", driverMode,
exportFixes));
}
}
if (cmNonempty(cpplint)) {
code_check += " --cpplint=";
code_check +=
this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(cpplint);
}
if (cmNonempty(cppcheck)) {
code_check += " --cppcheck=";
code_check +=
this->GeneratorTarget->GetLocalGenerator()->EscapeForShell(cppcheck);
}
if (cmNonempty(tidy) || (cmNonempty(cpplint)) || (cmNonempty(cppcheck))) {
code_check += " --source=";
code_check +=
this->GeneratorTarget->GetLocalGenerator()->ConvertToOutputFormat(
source.GetFullPath(), cmOutputConverter::SHELL);
}
code_check += " -- ";
return code_check;
}
return "";
}
std::string cmCommonTargetGenerator::GetLinkerLauncher(
const std::string& config)
{
std::string lang = this->GeneratorTarget->GetLinkerLanguage(config);
std::string propName = lang + "_LINKER_LAUNCHER";
cmValue launcherProp = this->GeneratorTarget->GetProperty(propName);
if (cmNonempty(launcherProp)) {
cmGeneratorExpressionDAGChecker dagChecker(this->GeneratorTarget, propName,
nullptr, nullptr);
std::string evaluatedLinklauncher = cmGeneratorExpression::Evaluate(
*launcherProp, this->LocalCommonGenerator, config, this->GeneratorTarget,
&dagChecker, this->GeneratorTarget, lang);
// Convert ;-delimited list to single string
cmList args{ evaluatedLinklauncher, cmList::EmptyElements::Yes };
if (!args.empty()) {
args[0] = this->LocalCommonGenerator->ConvertToOutputFormat(
args[0], cmOutputConverter::SHELL);
for (std::string& i : cmMakeRange(args.begin() + 1, args.end())) {
i = this->LocalCommonGenerator->EscapeForShell(i);
}
return cmJoin(args, " ");
}
}
return std::string();
}
bool cmCommonTargetGenerator::HaveRequiredLanguages(
const std::vector<cmSourceFile const*>& sources,
std::set<std::string>& languagesNeeded) const
{
for (cmSourceFile const* sf : sources) {
languagesNeeded.insert(sf->GetLanguage());
}
auto* makefile = this->Makefile;
auto* state = makefile->GetState();
auto unary = [&state, &makefile](const std::string& lang) -> bool {
const bool valid = state->GetLanguageEnabled(lang);
if (!valid) {
makefile->IssueMessage(
MessageType::FATAL_ERROR,
cmStrCat("The language ", lang,
" was requested for compilation but was not enabled."
" To enable a language it needs to be specified in a"
" 'project' or 'enable_language' command in the root"
" CMakeLists.txt"));
}
return valid;
};
return std::all_of(languagesNeeded.cbegin(), languagesNeeded.cend(), unary);
}
|