cmNinjaTargetGenerator.cxx 47.8 KB
Newer Older
1
2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
#include "cmNinjaTargetGenerator.h"
Brad King's avatar
Brad King committed
4

5
6
#include "cm_jsoncpp_value.h"
#include "cm_jsoncpp_writer.h"
7
8
9
10
#include <algorithm>
#include <assert.h>
#include <iterator>
#include <map>
11
#include <memory> // IWYU pragma: keep
12
#include <sstream>
13
#include <utility>
14

15
16
17
#include "cmAlgorithms.h"
#include "cmComputeLinkInformation.h"
#include "cmCustomCommandGenerator.h"
18
#include "cmGeneratedFileStream.h"
19
#include "cmGeneratorExpression.h"
20
#include "cmGeneratorTarget.h"
21
#include "cmGlobalNinjaGenerator.h"
22
#include "cmListFileCache.h" // for BT
23
#include "cmLocalGenerator.h"
24
25
#include "cmLocalNinjaGenerator.h"
#include "cmMakefile.h"
26
27
#include "cmNinjaNormalTargetGenerator.h"
#include "cmNinjaUtilityTargetGenerator.h"
28
#include "cmOutputConverter.h"
29
#include "cmRulePlaceholderExpander.h"
30
#include "cmSourceFile.h"
31
#include "cmState.h"
32
#include "cmStateTypes.h"
33
#include "cmSystemTools.h"
34
#include "cmake.h"
35

36
cmNinjaTargetGenerator* cmNinjaTargetGenerator::New(cmGeneratorTarget* target)
37
{
38
  switch (target->GetType()) {
39
40
41
42
43
    case cmStateEnums::EXECUTABLE:
    case cmStateEnums::SHARED_LIBRARY:
    case cmStateEnums::STATIC_LIBRARY:
    case cmStateEnums::MODULE_LIBRARY:
    case cmStateEnums::OBJECT_LIBRARY:
44
45
      return new cmNinjaNormalTargetGenerator(target);

46
47
    case cmStateEnums::UTILITY:
    case cmStateEnums::GLOBAL_TARGET:
48
49
50
      return new cmNinjaUtilityTargetGenerator(target);

    default:
Daniel Pfeifer's avatar
Daniel Pfeifer committed
51
      return nullptr;
52
  }
53
54
}

55
cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmGeneratorTarget* target)
56
  : cmCommonTargetGenerator(target)
Daniel Pfeifer's avatar
Daniel Pfeifer committed
57
58
  , MacOSXContentGenerator(nullptr)
  , OSXBundleGenerator(nullptr)
59
60
  , LocalGenerator(
      static_cast<cmLocalNinjaGenerator*>(target->GetLocalGenerator()))
61
{
Peter Kümmel's avatar
Peter Kümmel committed
62
  MacOSXContentGenerator = new MacOSXContentGeneratorType(this);
63
64
65
66
}

cmNinjaTargetGenerator::~cmNinjaTargetGenerator()
{
Nicolas Despres's avatar
Nicolas Despres committed
67
  delete this->MacOSXContentGenerator;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
}

cmGeneratedFileStream& cmNinjaTargetGenerator::GetBuildFileStream() const
{
  return *this->GetGlobalGenerator()->GetBuildFileStream();
}

cmGeneratedFileStream& cmNinjaTargetGenerator::GetRulesFileStream() const
{
  return *this->GetGlobalGenerator()->GetRulesFileStream();
}

cmGlobalNinjaGenerator* cmNinjaTargetGenerator::GetGlobalGenerator() const
{
  return this->LocalGenerator->GetGlobalNinjaGenerator();
}

85
86
87
88
std::string cmNinjaTargetGenerator::LanguageCompilerRule(
  const std::string& lang) const
{
  return lang + "_COMPILER__" +
89
    cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
90
91
}

92
93
94
95
96
97
98
99
100
101
102
103
104
std::string cmNinjaTargetGenerator::LanguagePreprocessRule(
  std::string const& lang) const
{
  return lang + "_PREPROCESS__" +
    cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
}

bool cmNinjaTargetGenerator::NeedExplicitPreprocessing(
  std::string const& lang) const
{
  return lang == "Fortran";
}

105
106
107
108
109
110
111
112
113
114
115
116
std::string cmNinjaTargetGenerator::LanguageDyndepRule(
  const std::string& lang) const
{
  return lang + "_DYNDEP__" +
    cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
}

bool cmNinjaTargetGenerator::NeedDyndep(std::string const& lang) const
{
  return lang == "Fortran";
}

117
std::string cmNinjaTargetGenerator::OrderDependsTargetForTarget()
118
{
119
  return "cmake_object_order_depends_target_" + this->GetTargetName();
120
121
}

122
123
124
125
// TODO: Most of the code is picked up from
// void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink),
// void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
// Refactor it.
126
127
std::string cmNinjaTargetGenerator::ComputeFlagsForObject(
  cmSourceFile const* source, const std::string& language)
128
{
129
  std::string flags = this->GetFlags(language);
130

131
  // Add Fortran format flags.
132
  if (language == "Fortran") {
133
    this->AppendFortranFormatFlags(flags, *source);
134
  }
135

Ben Boeckel's avatar
Ben Boeckel committed
136
  // Add source file specific flags.
137
  cmGeneratorExpressionInterpreter genexInterpreter(
138
139
    this->LocalGenerator, this->LocalGenerator->GetConfigName(),
    this->GeneratorTarget, language);
140

141
142
143
144
  const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  if (const char* cflags = source->GetProperty(COMPILE_FLAGS)) {
    this->LocalGenerator->AppendFlags(
      flags, genexInterpreter.Evaluate(cflags, COMPILE_FLAGS));
145
  }
146

147
148
149
150
151
152
  const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  if (const char* coptions = source->GetProperty(COMPILE_OPTIONS)) {
    this->LocalGenerator->AppendCompileOptions(
      flags, genexInterpreter.Evaluate(coptions, COMPILE_OPTIONS));
  }

153
154
155
  return flags;
}

156
157
158
159
void cmNinjaTargetGenerator::AddIncludeFlags(std::string& languageFlags,
                                             std::string const& language)
{
  std::vector<std::string> includes;
160
161
  this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
                                              language, this->GetConfigName());
162
  // Add include directory flags.
163
164
  std::string includeFlags = this->LocalGenerator->GetIncludeFlags(
    includes, this->GeneratorTarget, language,
Daniel Pfeifer's avatar
Daniel Pfeifer committed
165
    language == "RC", // full include paths for RC needed by cmcldeps
166
    false, this->GetConfigName());
167
  if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
168
    std::replace(includeFlags.begin(), includeFlags.end(), '\\', '/');
169
  }
170
171
172
173

  this->LocalGenerator->AppendFlags(languageFlags, includeFlags);
}

174
bool cmNinjaTargetGenerator::NeedDepTypeMSVC(const std::string& lang) const
175
{
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  std::string const& deptype =
    this->GetMakefile()->GetSafeDefinition("CMAKE_NINJA_DEPTYPE_" + lang);
  if (deptype == "msvc") {
    return true;
  }
  if (deptype == "intel") {
    // Ninja does not really define "intel", but we use it to switch based
    // on whether this environment supports "gcc" or "msvc" deptype.
    if (!this->GetGlobalGenerator()->SupportsMultilineDepfile()) {
      // This ninja version is too old to support the Intel depfile format.
      // Fall back to msvc deptype.
      return true;
    }
    if ((this->Makefile->GetHomeDirectory().find(' ') != std::string::npos) ||
        (this->Makefile->GetHomeOutputDirectory().find(' ') !=
         std::string::npos)) {
      // The Intel compiler does not properly escape spaces in a depfile.
      // Fall back to msvc deptype.
      return true;
    }
  }
  return false;
198
199
}

200
201
// TODO: Refactor with
// void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
202
203
std::string cmNinjaTargetGenerator::ComputeDefines(cmSourceFile const* source,
                                                   const std::string& language)
204
{
205
  std::set<std::string> defines;
206
207
  const std::string config = this->LocalGenerator->GetConfigName();
  cmGeneratorExpressionInterpreter genexInterpreter(
208
    this->LocalGenerator, config, this->GeneratorTarget, language);
209

210
211
  const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  if (const char* compile_defs = source->GetProperty(COMPILE_DEFINITIONS)) {
212
    this->LocalGenerator->AppendDefines(
213
      defines, genexInterpreter.Evaluate(compile_defs, COMPILE_DEFINITIONS));
214
215
216
217
218
219
  }

  std::string defPropName = "COMPILE_DEFINITIONS_";
  defPropName += cmSystemTools::UpperCase(config);
  if (const char* config_compile_defs = source->GetProperty(defPropName)) {
    this->LocalGenerator->AppendDefines(
220
221
      defines,
      genexInterpreter.Evaluate(config_compile_defs, COMPILE_DEFINITIONS));
222
223
  }

224
  std::string definesString = this->GetDefines(language);
225
  this->LocalGenerator->JoinDefines(defines, definesString, language);
226
227

  return definesString;
228
229
}

230
231
232
233
234
235
std::string cmNinjaTargetGenerator::ComputeIncludes(
  cmSourceFile const* source, const std::string& language)
{
  std::vector<std::string> includes;
  const std::string config = this->LocalGenerator->GetConfigName();
  cmGeneratorExpressionInterpreter genexInterpreter(
236
    this->LocalGenerator, config, this->GeneratorTarget, language);
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

  const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  if (const char* cincludes = source->GetProperty(INCLUDE_DIRECTORIES)) {
    this->LocalGenerator->AppendIncludeDirectories(
      includes, genexInterpreter.Evaluate(cincludes, INCLUDE_DIRECTORIES),
      *source);
  }

  std::string includesString = this->LocalGenerator->GetIncludeFlags(
    includes, this->GeneratorTarget, language, true, false, config);
  this->LocalGenerator->AppendFlags(includesString,
                                    this->GetIncludes(language));

  return includesString;
}

253
254
cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps(
  const std::string& linkLanguage) const
255
256
{
  // Static libraries never depend on other targets for linking.
257
258
  if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
      this->GeneratorTarget->GetType() == cmStateEnums::OBJECT_LIBRARY) {
259
    return cmNinjaDeps();
260
  }
261
262

  cmComputeLinkInformation* cli =
263
    this->GeneratorTarget->GetLinkInformation(this->GetConfigName());
264
  if (!cli) {
265
    return cmNinjaDeps();
266
  }
267

268
  const std::vector<std::string>& deps = cli->GetDepends();
269
270
  cmNinjaDeps result(deps.size());
  std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
Peter Kuemmel's avatar
Peter Kuemmel committed
271
272

  // Add a dependency on the link definitions file, if any.
273
274
275
  if (cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
        this->GeneratorTarget->GetModuleDefinitionInfo(
          this->GetConfigName())) {
276
277
    for (cmSourceFile const* src : mdi->Sources) {
      result.push_back(this->ConvertToNinjaPath(src->GetFullPath()));
278
    }
279
  }
Peter Kuemmel's avatar
Peter Kuemmel committed
280

281
282
283
  // Add a dependency on user-specified manifest files, if any.
  std::vector<cmSourceFile const*> manifest_srcs;
  this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
284
285
  for (cmSourceFile const* manifest_src : manifest_srcs) {
    result.push_back(this->ConvertToNinjaPath(manifest_src->GetFullPath()));
286
  }
287

288
  // Add user-specified dependencies.
289
290
291
292
293
  std::vector<std::string> linkDeps;
  this->GeneratorTarget->GetLinkDepends(linkDeps, this->ConfigName,
                                        linkLanguage);
  std::transform(linkDeps.begin(), linkDeps.end(), std::back_inserter(result),
                 MapToNinjaPath());
294

295
296
297
  return result;
}

298
299
std::string cmNinjaTargetGenerator::GetSourceFilePath(
  cmSourceFile const* source) const
300
{
301
  return ConvertToNinjaPath(source->GetFullPath());
302
303
}

304
305
std::string cmNinjaTargetGenerator::GetObjectFilePath(
  cmSourceFile const* source) const
306
307
{
  std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
308
  if (!path.empty()) {
309
    path += "/";
310
  }
311
  std::string const& objectName = this->GeneratorTarget->GetObjectName(source);
312
  path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
313
314
  path += "/";
  path += objectName;
315
316
317
  return path;
}

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
std::string cmNinjaTargetGenerator::GetPreprocessedFilePath(
  cmSourceFile const* source) const
{
  // Choose an extension to compile already-preprocessed source.
  std::string ppExt = source->GetExtension();
  if (cmHasLiteralPrefix(ppExt, "F")) {
    // Some Fortran compilers automatically enable preprocessing for
    // upper-case extensions.  Since the source is already preprocessed,
    // use a lower-case extension.
    ppExt = cmSystemTools::LowerCase(ppExt);
  }
  if (ppExt == "fpp") {
    // Some Fortran compilers automatically enable preprocessing for
    // the ".fpp" extension.  Since the source is already preprocessed,
    // use the ".f" extension.
    ppExt = "f";
  }

  // Take the object file name and replace the extension.
  std::string const& objName = this->GeneratorTarget->GetObjectName(source);
  std::string const& objExt =
    this->GetGlobalGenerator()->GetLanguageOutputExtension(*source);
  assert(objName.size() >= objExt.size());
  std::string const ppName =
    objName.substr(0, objName.size() - objExt.size()) + "-pp." + ppExt;

  std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
345
  if (!path.empty()) {
346
    path += "/";
347
  }
348
349
350
351
352
353
  path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  path += "/";
  path += ppName;
  return path;
}

354
355
356
357
std::string cmNinjaTargetGenerator::GetDyndepFilePath(
  std::string const& lang) const
{
  std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
358
  if (!path.empty()) {
359
    path += "/";
360
  }
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
  path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  path += "/";
  path += lang;
  path += ".dd";
  return path;
}

std::string cmNinjaTargetGenerator::GetTargetDependInfoPath(
  std::string const& lang) const
{
  std::string path = this->Makefile->GetCurrentBinaryDirectory();
  path += "/";
  path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  path += "/" + lang + "DependInfo.json";
  return path;
}

378
379
std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
{
380
  std::string dir = this->GeneratorTarget->GetDirectory(this->GetConfigName());
381
  return ConvertToNinjaPath(dir);
382
383
}

384
385
std::string cmNinjaTargetGenerator::GetTargetFilePath(
  const std::string& name) const
386
387
{
  std::string path = this->GetTargetOutputDir();
388
  if (path.empty() || path == ".") {
389
    return name;
390
  }
391
392
393
394
395
396
397
  path += "/";
  path += name;
  return path;
}

std::string cmNinjaTargetGenerator::GetTargetName() const
{
398
  return this->GeneratorTarget->GetName();
399
400
}

401
bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
402
{
403
404
  cmMakefile* mf = this->GetMakefile();
  if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
Brad King's avatar
Brad King committed
405
406
      mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID") ||
      mf->GetDefinition("MSVC_CUDA_ARCHITECTURE_ID")) {
407
    std::string pdbPath;
408
    std::string compilePdbPath = this->ComputeTargetCompilePDB();
409
410
411
412
    if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
        this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
        this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
        this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
413
      pdbPath = this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
414
      pdbPath += "/";
415
      pdbPath += this->GeneratorTarget->GetPDBName(this->GetConfigName());
416
    }
417

418
    vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
419
      ConvertToNinjaPath(pdbPath), cmOutputConverter::SHELL);
420
421
    vars["TARGET_COMPILE_PDB"] =
      this->GetLocalGenerator()->ConvertToOutputFormat(
422
        ConvertToNinjaPath(compilePdbPath), cmOutputConverter::SHELL);
423

424
    EnsureParentDirectoryExists(pdbPath);
425
    EnsureParentDirectoryExists(compilePdbPath);
426
    return true;
427
  }
428
  return false;
429
430
}

431
void cmNinjaTargetGenerator::WriteLanguageRules(const std::string& language)
432
{
433
#ifdef NINJA_GEN_VERBOSE_FILES
434
  this->GetRulesFileStream() << "# Rules for language " << language << "\n\n";
435
#endif
436
437
438
  this->WriteCompileRule(language);
}

439
void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang)
440
{
441
  cmRulePlaceholderExpander::RuleVariables vars;
442
443
444
  vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
  vars.CMTargetType =
    cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType());
445
  vars.Language = lang.c_str();
446
  vars.Source = "$in";
447
448
  vars.Object = "$out";
  vars.Defines = "$DEFINES";
449
  vars.Includes = "$INCLUDES";
450
  vars.TargetPDB = "$TARGET_PDB";
451
  vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
452
  vars.ObjectDir = "$OBJECT_DIR";
453
  vars.ObjectFileDir = "$OBJECT_FILE_DIR";
454
455
  if (lang == "Swift") {
    vars.SwiftAuxiliarySources = "$SWIFT_AUXILIARY_SOURCES";
456
    vars.SwiftModuleName = "$SWIFT_MODULE_NAME";
457
  }
458

459
460
  // For some cases we do an explicit preprocessor invocation.
  bool const explicitPP = this->NeedExplicitPreprocessing(lang);
461
  bool const needDyndep = this->NeedDyndep(lang);
462

463
  cmMakefile* mf = this->GetMakefile();
464

465
466
  std::string flags = "$FLAGS";

467
  std::string responseFlag;
468
469
  bool const lang_supports_response = !(lang == "RC" || lang == "CUDA");
  if (lang_supports_response && this->ForceResponseFile()) {
470
471
    std::string const responseFlagVar =
      "CMAKE_" + lang + "_RESPONSE_FILE_FLAG";
472
    responseFlag = this->Makefile->GetSafeDefinition(responseFlagVar);
473
474
475
    if (responseFlag.empty()) {
      responseFlag = "@";
    }
476
  }
477

478
  std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
479
480
    this->GetLocalGenerator()->CreateRulePlaceholderExpander());

481
482
483
484
  std::string const tdi = this->GetLocalGenerator()->ConvertToOutputFormat(
    ConvertToNinjaPath(this->GetTargetDependInfoPath(lang)),
    cmLocalGenerator::SHELL);

485
486
487
488
489
490
491
492
  std::string launcher;
  const char* val = this->GetLocalGenerator()->GetRuleLauncher(
    this->GetGeneratorTarget(), "RULE_LAUNCH_COMPILE");
  if (val && *val) {
    launcher = val;
    launcher += " ";
  }

493
494
495
496
497
498
499
  if (explicitPP) {
    // Lookup the explicit preprocessing rule.
    std::string const ppVar = "CMAKE_" + lang + "_PREPROCESS_SOURCE";
    std::string const ppCmd =
      this->GetMakefile()->GetRequiredDefinition(ppVar);

    // Explicit preprocessing always uses a depfile.
500
    std::string const ppDeptype; // no deps= for multiple outputs
501
502
    std::string const ppDepfile = "$DEP_FILE";

503
    cmRulePlaceholderExpander::RuleVariables ppVars;
504
505
    ppVars.CMTargetName = vars.CMTargetName;
    ppVars.CMTargetType = vars.CMTargetType;
506
507
508
509
510
511
512
513
514
515
516
    ppVars.Language = vars.Language;
    ppVars.Object = "$out"; // for RULE_LAUNCH_COMPILE
    ppVars.PreprocessedSource = "$out";
    ppVars.DependencyFile = ppDepfile.c_str();

    // Preprocessing uses the original source,
    // compilation uses preprocessed output.
    ppVars.Source = vars.Source;
    vars.Source = "$in";

    // Preprocessing and compilation use the same flags.
517
    std::string ppFlags = flags;
518
519
520
521
522
523
524
525
526

    // Move preprocessor definitions to the preprocessor rule.
    ppVars.Defines = vars.Defines;
    vars.Defines = "";

    // Copy include directories to the preprocessor rule.  The Fortran
    // compilation rule still needs them for the INCLUDE directive.
    ppVars.Includes = vars.Includes;

527
528
529
530
531
532
533
534
535
536
537
538
    // If using a response file, move defines, includes, and flags into it.
    std::string ppRspFile;
    std::string ppRspContent;
    if (!responseFlag.empty()) {
      ppRspFile = "$RSP_FILE";
      ppRspContent = std::string(" ") + ppVars.Defines + " " +
        ppVars.Includes + " " + ppFlags;
      ppFlags = responseFlag + ppRspFile;
      ppVars.Defines = "";
      ppVars.Includes = "";
    }

539
540
    ppVars.Flags = ppFlags.c_str();

541
542
543
544
    // Rule for preprocessing source file.
    std::vector<std::string> ppCmds;
    cmSystemTools::ExpandListArgument(ppCmd, ppCmds);

545
546
    for (std::string& i : ppCmds) {
      i = launcher + i;
547
      rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
548
                                                   i, ppVars);
549
550
    }

551
552
553
554
    // Run CMake dependency scanner on preprocessed output.
    std::string const cmake = this->GetLocalGenerator()->ConvertToOutputFormat(
      cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
    ppCmds.push_back(
555
556
557
558
559
560
      cmake +
      " -E cmake_ninja_depends"
      " --tdi=" +
      tdi +
      " --pp=$out"
      " --dep=$DEP_FILE" +
561
562
      (needDyndep ? " --obj=$OBJ_FILE --ddi=$DYNDEP_INTERMEDIATE_FILE" : ""));

563
564
565
566
567
568
569
570
    std::string const ppCmdLine =
      this->GetLocalGenerator()->BuildCommandLine(ppCmds);

    // Write the rule for preprocessing file of the given language.
    std::ostringstream ppComment;
    ppComment << "Rule for preprocessing " << lang << " files.";
    std::ostringstream ppDesc;
    ppDesc << "Building " << lang << " preprocessed $out";
571
572
573
574
575
    this->GetGlobalGenerator()->AddRule(
      this->LanguagePreprocessRule(lang), ppCmdLine, ppDesc.str(),
      ppComment.str(), ppDepfile, ppDeptype, ppRspFile, ppRspContent,
      /*restat*/ "",
      /*generator*/ false);
576
577
  }

578
579
580
581
  if (needDyndep) {
    // Write the rule for ninja dyndep file generation.
    std::vector<std::string> ddCmds;

582
583
    // Command line length is almost always limited -> use response file for
    // dyndep rules
584
585
586
587
    std::string ddRspFile = "$out.rsp";
    std::string ddRspContent = "$in";
    std::string ddInput = "@" + ddRspFile;

588
589
590
    // Run CMake dependency scanner on preprocessed output.
    std::string const cmake = this->GetLocalGenerator()->ConvertToOutputFormat(
      cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
591
592
593
594
595
596
    ddCmds.push_back(cmake +
                     " -E cmake_ninja_dyndep"
                     " --tdi=" +
                     tdi +
                     " --dd=$out"
                     " " +
597
                     ddInput);
598
599
600
601
602
603
604
605
606
607
608

    std::string const ddCmdLine =
      this->GetLocalGenerator()->BuildCommandLine(ddCmds);

    std::ostringstream ddComment;
    ddComment << "Rule to generate ninja dyndep files for " << lang << ".";
    std::ostringstream ddDesc;
    ddDesc << "Generating " << lang << " dyndep file $out";
    this->GetGlobalGenerator()->AddRule(
      this->LanguageDyndepRule(lang), ddCmdLine, ddDesc.str(), ddComment.str(),
      /*depfile*/ "",
609
      /*deps*/ "", ddRspFile, ddRspContent,
610
611
612
613
      /*restat*/ "",
      /*generator*/ false);
  }

614
615
616
617
618
619
620
621
622
623
624
625
  // If using a response file, move defines, includes, and flags into it.
  std::string rspfile;
  std::string rspcontent;
  if (!responseFlag.empty()) {
    rspfile = "$RSP_FILE";
    rspcontent =
      std::string(" ") + vars.Defines + " " + vars.Includes + " " + flags;
    flags = responseFlag + rspfile;
    vars.Defines = "";
    vars.Includes = "";
  }

626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
  // Tell ninja dependency format so all deps can be loaded into a database
  std::string deptype;
  std::string depfile;
  std::string cldeps;
  if (explicitPP) {
    // The explicit preprocessing step will handle dependency scanning.
  } else if (this->NeedDepTypeMSVC(lang)) {
    deptype = "msvc";
    depfile.clear();
    flags += " /showIncludes";
  } else if (mf->IsOn("CMAKE_NINJA_CMCLDEPS_" + lang)) {
    // For the MS resource compiler we need cmcldeps, but skip dependencies
    // for source-file try_compile cases because they are always fresh.
    if (!mf->GetIsSourceFileTryCompile()) {
      deptype = "gcc";
      depfile = "$DEP_FILE";
      const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER")
        ? mf->GetSafeDefinition("CMAKE_C_COMPILER")
        : mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
      cldeps = "\"";
      cldeps += cmSystemTools::GetCMClDepsCommand();
      cldeps += "\" " + lang + " " + vars.Source + " $DEP_FILE $out \"";
      cldeps += mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
      cldeps += "\" \"" + cl + "\" ";
    }
  } else {
    deptype = "gcc";
    depfile = "$DEP_FILE";
    const std::string flagsName = "CMAKE_DEPFILE_FLAGS_" + lang;
    std::string depfileFlags = mf->GetSafeDefinition(flagsName);
    if (!depfileFlags.empty()) {
      cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
      cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
      cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
                                   mf->GetDefinition("CMAKE_C_COMPILER"));
      flags += " " + depfileFlags;
    }
  }

  vars.Flags = flags.c_str();
  vars.DependencyFile = depfile.c_str();

668
  // Rule for compiling object file.
669
  std::vector<std::string> compileCmds;
670
671
  if (lang == "CUDA") {
    std::string cmdVar;
672
673
    if (this->GeneratorTarget->GetPropertyAsBool(
          "CUDA_SEPARABLE_COMPILATION")) {
674
      cmdVar = std::string("CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION");
675
676
677
    } else if (this->GeneratorTarget->GetPropertyAsBool(
                 "CUDA_PTX_COMPILATION")) {
      cmdVar = std::string("CMAKE_CUDA_COMPILE_PTX_COMPILATION");
678
679
680
681
682
683
684
685
686
687
688
    } else {
      cmdVar = std::string("CMAKE_CUDA_COMPILE_WHOLE_COMPILATION");
    }
    std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
    cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  } else {
    const std::string cmdVar =
      std::string("CMAKE_") + lang + "_COMPILE_OBJECT";
    std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
    cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  }
689

690
691
692
693
694
695
696
697
698
699
700
  // See if we need to use a compiler launcher like ccache or distcc
  std::string compilerLauncher;
  if (!compileCmds.empty() &&
      (lang == "C" || lang == "CXX" || lang == "Fortran" || lang == "CUDA")) {
    std::string const clauncher_prop = lang + "_COMPILER_LAUNCHER";
    const char* clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
    if (clauncher && *clauncher) {
      compilerLauncher = clauncher;
    }
  }

701
  // Maybe insert an include-what-you-use runner.
702
  if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
703
    std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
704
    const char* iwyu = this->GeneratorTarget->GetProperty(iwyu_prop);
705
    std::string const tidy_prop = lang + "_CLANG_TIDY";
706
    const char* tidy = this->GeneratorTarget->GetProperty(tidy_prop);
707
708
    std::string const cpplint_prop = lang + "_CPPLINT";
    const char* cpplint = this->GeneratorTarget->GetProperty(cpplint_prop);
709
710
711
712
    std::string const cppcheck_prop = lang + "_CPPCHECK";
    const char* cppcheck = this->GeneratorTarget->GetProperty(cppcheck_prop);
    if ((iwyu && *iwyu) || (tidy && *tidy) || (cpplint && *cpplint) ||
        (cppcheck && *cppcheck)) {
713
      std::string run_iwyu = this->GetLocalGenerator()->ConvertToOutputFormat(
714
        cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
715
      run_iwyu += " -E __run_co_compile";
716
717
718
719
720
721
722
      if (!compilerLauncher.empty()) {
        // In __run_co_compile case the launcher command is supplied
        // via --launcher=<maybe-list> and consumed
        run_iwyu += " --launcher=";
        run_iwyu += this->LocalGenerator->EscapeForShell(compilerLauncher);
        compilerLauncher.clear();
      }
723
      if (iwyu && *iwyu) {
724
725
        run_iwyu += " --iwyu=";
        run_iwyu += this->GetLocalGenerator()->EscapeForShell(iwyu);
726
727
      }
      if (tidy && *tidy) {
728
729
        run_iwyu += " --tidy=";
        run_iwyu += this->GetLocalGenerator()->EscapeForShell(tidy);
730
731
732
733
734
      }
      if (cpplint && *cpplint) {
        run_iwyu += " --cpplint=";
        run_iwyu += this->GetLocalGenerator()->EscapeForShell(cpplint);
      }
735
736
737
738
739
740
      if (cppcheck && *cppcheck) {
        run_iwyu += " --cppcheck=";
        run_iwyu += this->GetLocalGenerator()->EscapeForShell(cppcheck);
      }
      if ((tidy && *tidy) || (cpplint && *cpplint) ||
          (cppcheck && *cppcheck)) {
741
        run_iwyu += " --source=$in";
742
      }
743
744
745
      run_iwyu += " -- ";
      compileCmds.front().insert(0, run_iwyu);
    }
746
  }
747

748
749
750
751
752
753
754
  // If compiler launcher was specified and not consumed above, it
  // goes to the beginning of the command line.
  if (!compileCmds.empty() && !compilerLauncher.empty()) {
    std::vector<std::string> args;
    cmSystemTools::ExpandListArgument(compilerLauncher, args, true);
    for (std::string& i : args) {
      i = this->LocalGenerator->EscapeForShell(i);
755
    }
756
    compileCmds.front().insert(0, cmJoin(args, " ") + " ");
757
  }
758

759
  if (!compileCmds.empty()) {
760
    compileCmds.front().insert(0, cldeps);
761
  }
762

763
764
765
  for (std::string& i : compileCmds) {
    i = launcher + i;
    rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), i,
766
                                                 vars);
767
  }
768

769
770
  std::string cmdLine =
    this->GetLocalGenerator()->BuildCommandLine(compileCmds);
771

772
  // Write the rule for compiling file of the given language.
773
  std::ostringstream comment;
774
  comment << "Rule for compiling " << lang << " files.";
775
  std::ostringstream description;
776
  description << "Building " << lang << " object $out";
777
778
779
780
781
  this->GetGlobalGenerator()->AddRule(
    this->LanguageCompilerRule(lang), cmdLine, description.str(),
    comment.str(), depfile, deptype, rspfile, rspcontent,
    /*restat*/ "",
    /*generator*/ false);
782
783
}

784
void cmNinjaTargetGenerator::WriteObjectBuildStatements()
785
786
787
788
789
{
  // Write comments.
  cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  this->GetBuildFileStream()
    << "# Object build statements for "
790
    << cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType())
791
    << " target " << this->GetTargetName() << "\n\n";
792

793
  std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
794
  std::vector<cmSourceFile const*> customCommands;
795
  this->GeneratorTarget->GetCustomCommands(customCommands, config);
796
797
  for (cmSourceFile const* sf : customCommands) {
    cmCustomCommand const* cc = sf->GetCustomCommand();
798
799
800
801
802
803
    this->GetLocalGenerator()->AddCustomCommandTarget(
      cc, this->GetGeneratorTarget());
    // Record the custom commands for this target. The container is used
    // in WriteObjectBuildStatement when called in a loop below.
    this->CustomCommands.push_back(cc);
  }
804
  std::vector<cmSourceFile const*> headerSources;
805
  this->GeneratorTarget->GetHeaderSources(headerSources, config);
806
  this->OSXBundleGenerator->GenerateMacOSXContentStatements(
807
    headerSources, this->MacOSXContentGenerator);
808
  std::vector<cmSourceFile const*> extraSources;
809
  this->GeneratorTarget->GetExtraSources(extraSources, config);
810
  this->OSXBundleGenerator->GenerateMacOSXContentStatements(
811
    extraSources, this->MacOSXContentGenerator);
812
  std::vector<cmSourceFile const*> externalObjects;
813
  this->GeneratorTarget->GetExternalObjects(externalObjects, config);
814
815
  for (cmSourceFile const* sf : externalObjects) {
    this->Objects.push_back(this->GetSourceFilePath(sf));
816
  }
817
818

  cmNinjaDeps orderOnlyDeps;
819
820
  this->GetLocalGenerator()->AppendTargetDepends(
    this->GeneratorTarget, orderOnlyDeps, DependOnTargetOrdering);
821

822
823
824
825
  // Add order-only dependencies on other files associated with the target.
  orderOnlyDeps.insert(orderOnlyDeps.end(), this->ExtraFiles.begin(),
                       this->ExtraFiles.end());

826
  // Add order-only dependencies on custom command outputs.
827
  for (cmCustomCommand const* cc : this->CustomCommands) {
828
    cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
829
                                 this->GetLocalGenerator());
830
    const std::vector<std::string>& ccoutputs = ccg.GetOutputs();
831
    const std::vector<std::string>& ccbyproducts = ccg.GetByproducts();
832
833
    std::transform(ccoutputs.begin(), ccoutputs.end(),
                   std::back_inserter(orderOnlyDeps), MapToNinjaPath());
834
835
    std::transform(ccbyproducts.begin(), ccbyproducts.end(),
                   std::back_inserter(orderOnlyDeps), MapToNinjaPath());
836
  }
837

838
839
840
841
  std::sort(orderOnlyDeps.begin(), orderOnlyDeps.end());
  orderOnlyDeps.erase(std::unique(orderOnlyDeps.begin(), orderOnlyDeps.end()),
                      orderOnlyDeps.end());

842
843
844
845
846
847
848
849
850
851
852
853
854
  // The phony target must depend on at least one input or ninja will explain
  // that "output ... of phony edge with no inputs doesn't exist" and consider
  // the phony output "dirty".
  if (orderOnlyDeps.empty()) {
    // Any path that always exists will work here.  It would be nice to
    // use just "." but that is not supported by Ninja < 1.7.
    std::string tgtDir;
    tgtDir += this->LocalGenerator->GetCurrentBinaryDirectory();
    tgtDir += "/";
    tgtDir += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
    orderOnlyDeps.push_back(this->ConvertToNinjaPath(tgtDir));
  }

855
  {
856
857
    cmNinjaDeps orderOnlyTarget;
    orderOnlyTarget.push_back(this->OrderDependsTargetForTarget());
858
859
860
861
862
    this->GetGlobalGenerator()->WritePhonyBuild(
      this->GetBuildFileStream(),
      "Order-only phony target for " + this->GetTargetName(), orderOnlyTarget,
      cmNinjaDeps(), cmNinjaDeps(), orderOnlyDeps);
  }
863
  std::vector<cmSourceFile const*> objectSources;
864
  this->GeneratorTarget->GetObjectSources(objectSources, config);
865
866
  for (cmSourceFile const* sf : objectSources) {
    this->WriteObjectBuildStatement(sf);
867
  }
868

869
870
871
872
873
874
875
876
877
878
879
880
881
882
  if (!this->DDIFiles.empty()) {
    std::string const ddComment;
    std::string const ddRule = this->LanguageDyndepRule("Fortran");
    cmNinjaDeps ddOutputs;
    cmNinjaDeps ddImplicitOuts;
    cmNinjaDeps const& ddExplicitDeps = this->DDIFiles;
    cmNinjaDeps ddImplicitDeps;
    cmNinjaDeps ddOrderOnlyDeps;
    cmNinjaVars ddVars;

    this->WriteTargetDependInfo("Fortran");

    ddOutputs.push_back(this->GetDyndepFilePath("Fortran"));

883
884
885
886
887
888
889
890
    // Make sure dyndep files for all our dependencies have already
    // been generated so that the 'FortranModules.json' files they
    // produced as side-effects are available for us to read.
    // Ideally we should depend on the 'FortranModules.json' files
    // from our dependencies directly, but we don't know which of
    // our dependencies produces them.  Fixing this will require
    // refactoring the Ninja generator to generate targets in
    // dependency order so that we can collect the needed information.
891
892
    this->GetLocalGenerator()->AppendTargetDepends(
      this->GeneratorTarget, ddOrderOnlyDeps, DependOnTargetArtifact);
893

894
895
896
897
898
    this->GetGlobalGenerator()->WriteBuild(
      this->GetBuildFileStream(), ddComment, ddRule, ddOutputs, ddImplicitOuts,
      ddExplicitDeps, ddImplicitDeps, ddOrderOnlyDeps, ddVars);
  }

899
900
901
  this->GetBuildFileStream() << "\n";
}

902
void cmNinjaTargetGenerator::WriteObjectBuildStatement(
903
  cmSourceFile const* source)
904
{
905
  std::string const language = source->GetLanguage();
906
907
  std::string const sourceFileName =
    language == "RC" ? source->GetFullPath() : this->GetSourceFilePath(source);
908
909
910
911
  std::string const objectDir =
    this->ConvertToNinjaPath(this->GeneratorTarget->GetSupportDirectory());
  std::string const objectFileName =
    this->ConvertToNinjaPath(this->GetObjectFilePath(source));
912
913
914
  std::string const objectFileDir =
    cmSystemTools::GetFilenamePath(objectFileName);

915
916
917
918
919
  bool const lang_supports_response =
    !(language == "RC" || language == "CUDA");
  int const commandLineLengthLimit =
    ((lang_supports_response && this->ForceResponseFile())) ? -1 : 0;

920
921
922
  cmNinjaVars vars;
  vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  vars["DEFINES"] = this->ComputeDefines(source, language);
923
  vars["INCLUDES"] = this->ComputeIncludes(source, language);
924
  if (language == "Swift") {
925
926
    // The swift compiler needs all the sources besides the one being compiled
    // in order to do the type checking.  List all these "auxiliary" sources.
927
928
929
930
931
932
933
934
935
936
    std::string aux_sources;
    cmGeneratorTarget::KindedSources const& sources =
      this->GeneratorTarget->GetKindedSources(this->GetConfigName());
    for (cmGeneratorTarget::SourceAndKind const& src : sources.Sources) {
      if (src.Source.Value == source) {
        continue;
      }
      aux_sources += " " + this->GetSourceFilePath(src.Source.Value);
    }
    vars["SWIFT_AUXILIARY_SOURCES"] = aux_sources;
937

938
939
940
941
942
943
    if (const char* name =
          this->GeneratorTarget->GetProperty("SWIFT_MODULE_NAME")) {
      vars["SWIFT_MODULE_NAME"] = name;
    } else {
      vars["SWIFT_MODULE_NAME"] = this->GeneratorTarget->GetName();
    }
944
  }
945

946
  if (!this->NeedDepTypeMSVC(language)) {
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
    bool replaceExt(false);
    if (!language.empty()) {
      std::string repVar = "CMAKE_";
      repVar += language;
      repVar += "_DEPFILE_EXTENSION_REPLACE";
      replaceExt = this->Makefile->IsOn(repVar);
    }
    if (!replaceExt) {
      // use original code
      vars["DEP_FILE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
        objectFileName + ".d", cmOutputConverter::SHELL);
    } else {
      // Replace the original source file extension with the
      // depend file extension.
      std::string dependFileName =
        cmSystemTools::GetFilenameWithoutLastExtension(objectFileName) + ".d";
      vars["DEP_FILE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
        objectFileDir + "/" + dependFileName, cmOutputConverter::SHELL);
    }
966
  }
967
968

  this->ExportObjectCompileCommand(
969
970
    language, sourceFileName, objectDir, objectFileName, objectFileDir,
    vars["FLAGS"], vars["DEFINES"], vars["INCLUDES"]);
971

972
973
974
975
976
977
978
979
980
981
982
  std::string comment;
  std::string rule = this->LanguageCompilerRule(language);

  cmNinjaDeps outputs;
  outputs.push_back(objectFileName);
  // Add this object to the list of object files.
  this->Objects.push_back(objectFileName);

  cmNinjaDeps explicitDeps;
  explicitDeps.push_back(sourceFileName);

983
  cmNinjaDeps implicitDeps;
984
  if (const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
985
986
    std::vector<std::string> depList;
    cmSystemTools::ExpandListArgument(objectDeps, depList);
987
988
989
    for (std::string& odi : depList) {
      if (cmSystemTools::FileIsFullPath(odi)) {
        odi = cmSystemTools::CollapseFullPath(odi);
990
      }
991
    }
992
    std::transform(depList.begin(), depList.end(),
993
                   std::back_inserter(implicitDeps), MapToNinjaPath());
994
995
  }

996
  cmNinjaDeps orderOnlyDeps;
997
  orderOnlyDeps.push_back(this->OrderDependsTargetForTarget());
998

999
1000
1001
1002
  // If the source file is GENERATED and does not have a custom command
  // (either attached to this source file or another one), assume that one of
  // the target dependencies, OBJECT_DEPENDS or header file custom commands
  // will rebuild the file.
1003
1004
1005
  if (source->GetPropertyAsBool("GENERATED") &&
      !source->GetPropertyAsBool("__CMAKE_GENERATED_BY_CMAKE") &&
      !source->GetCustomCommand() &&
1006
1007
1008
1009
1010
      !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
    this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
                                                             orderOnlyDeps);
  }

1011
1012
1013
  // For some cases we need to generate a ninja dyndep file.
  bool const needDyndep = this->NeedDyndep(language);

1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
  // For some cases we do an explicit preprocessor invocation.
  bool const explicitPP = this->NeedExplicitPreprocessing(language);
  if (explicitPP) {
    std::string const ppComment;
    std::string const ppRule = this->LanguagePreprocessRule(language);
    cmNinjaDeps ppOutputs;
    cmNinjaDeps ppImplicitOuts;
    cmNinjaDeps ppExplicitDeps;
    cmNinjaDeps ppImplicitDeps;
    cmNinjaDeps ppOrderOnlyDeps;
    cmNinjaVars ppVars;

    std::string const ppFileName =
      this->ConvertToNinjaPath(this->GetPreprocessedFilePath(source));
    ppOutputs.push_back(ppFileName);

    // Move compilation dependencies to the preprocessing build statement.
    std::swap(ppExplicitDeps, explicitDeps);
    std::swap(ppImplicitDeps, implicitDeps);
    std::swap(ppOrderOnlyDeps, orderOnlyDeps);
    std::swap(ppVars["IN_ABS"], vars["IN_ABS"]);

    // The actual compilation will now use the preprocessed source.
    explicitDeps.push_back(ppFileName);

1039
    // Preprocessing and compilation generally use the same flags.
1040
1041
    ppVars["FLAGS"] = vars["FLAGS"];

1042
1043
1044
1045
1046
1047
    // In case compilation requires flags that are incompatible with
    // preprocessing, include them here.
    std::string const postFlag =
      this->Makefile->GetSafeDefinition("CMAKE_Fortran_POSTPROCESS_FLAG");
    this->LocalGenerator->AppendFlags(vars["FLAGS"], postFlag);

1048
1049
1050
1051
1052
1053
1054
1055
    // Move preprocessor definitions to the preprocessor build statement.