cmNinjaTargetGenerator.cxx 38.5 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
7
#include "cmAlgorithms.h"
#include "cmComputeLinkInformation.h"
#include "cmCustomCommandGenerator.h"
8
#include "cmGeneratedFileStream.h"
9
#include "cmGeneratorTarget.h"
10
#include "cmGlobalNinjaGenerator.h"
11
#include "cmLocalGenerator.h"
12
13
#include "cmLocalNinjaGenerator.h"
#include "cmMakefile.h"
14
15
#include "cmNinjaNormalTargetGenerator.h"
#include "cmNinjaUtilityTargetGenerator.h"
16
#include "cmOutputConverter.h"
17
#include "cmSourceFile.h"
18
#include "cmState.h"
19
#include "cmSystemTools.h"
20
#include "cmake.h"
21

22
23
#include "cm_jsoncpp_writer.h"

24
#include <algorithm>
25
26
27
28
#include <iterator>
#include <map>
#include <sstream>
#include <string.h>
29

30
cmNinjaTargetGenerator* cmNinjaTargetGenerator::New(cmGeneratorTarget* target)
31
{
32
33
34
35
36
37
38
39
40
  switch (target->GetType()) {
    case cmState::EXECUTABLE:
    case cmState::SHARED_LIBRARY:
    case cmState::STATIC_LIBRARY:
    case cmState::MODULE_LIBRARY:
    case cmState::OBJECT_LIBRARY:
      return new cmNinjaNormalTargetGenerator(target);

    case cmState::UTILITY:
41
    case cmState::GLOBAL_TARGET:
42
43
44
      return new cmNinjaUtilityTargetGenerator(target);

    default:
Daniel Pfeifer's avatar
Daniel Pfeifer committed
45
      return CM_NULLPTR;
46
  }
47
48
}

49
cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmGeneratorTarget* target)
50
  : cmCommonTargetGenerator(target)
Daniel Pfeifer's avatar
Daniel Pfeifer committed
51
52
  , MacOSXContentGenerator(CM_NULLPTR)
  , OSXBundleGenerator(CM_NULLPTR)
53
54
55
56
  , MacContentFolders()
  , LocalGenerator(
      static_cast<cmLocalNinjaGenerator*>(target->GetLocalGenerator()))
  , Objects()
57
{
Peter Kümmel's avatar
Peter Kümmel committed
58
  MacOSXContentGenerator = new MacOSXContentGeneratorType(this);
59
60
61
62
}

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

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

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

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

81
82
83
84
std::string cmNinjaTargetGenerator::LanguageCompilerRule(
  const std::string& lang) const
{
  return lang + "_COMPILER__" +
85
    cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
86
87
}

88
89
90
91
92
93
94
95
96
97
98
99
100
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";
}

101
102
103
104
105
106
107
108
109
110
111
112
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";
}

113
std::string cmNinjaTargetGenerator::OrderDependsTargetForTarget()
114
115
116
117
{
  return "cmake_order_depends_target_" + this->GetTargetName();
}

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

127
  // Add Fortran format flags.
128
  if (language == "Fortran") {
129
    this->AppendFortranFormatFlags(flags, *source);
130
  }
131

Ben Boeckel's avatar
Ben Boeckel committed
132
133
  // Add source file specific flags.
  this->LocalGenerator->AppendFlags(flags,
134
                                    source->GetProperty("COMPILE_FLAGS"));
135
136
137
138

  return flags;
}

139
140
141
142
void cmNinjaTargetGenerator::AddIncludeFlags(std::string& languageFlags,
                                             std::string const& language)
{
  std::vector<std::string> includes;
143
144
  this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
                                              language, this->GetConfigName());
145
  // Add include directory flags.
146
147
  std::string includeFlags = this->LocalGenerator->GetIncludeFlags(
    includes, this->GeneratorTarget, language,
Daniel Pfeifer's avatar
Daniel Pfeifer committed
148
    language == "RC", // full include paths for RC needed by cmcldeps
149
    false, this->GetConfigName());
150
  if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
151
    std::replace(includeFlags.begin(), includeFlags.end(), '\\', '/');
152
  }
153
154
155
156

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

157
bool cmNinjaTargetGenerator::NeedDepTypeMSVC(const std::string& lang) const
158
{
159
160
161
  return strcmp(this->GetMakefile()->GetSafeDefinition("CMAKE_NINJA_DEPTYPE_" +
                                                       lang),
                "msvc") == 0;
162
163
}

164
165
// TODO: Refactor with
// void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
166
167
std::string cmNinjaTargetGenerator::ComputeDefines(cmSourceFile const* source,
                                                   const std::string& language)
168
{
169
  std::set<std::string> defines;
170
171
  this->LocalGenerator->AppendDefines(
    defines, source->GetProperty("COMPILE_DEFINITIONS"));
172
  {
173
174
175
176
    std::string defPropName = "COMPILE_DEFINITIONS_";
    defPropName += cmSystemTools::UpperCase(this->GetConfigName());
    this->LocalGenerator->AppendDefines(defines,
                                        source->GetProperty(defPropName));
177
178
  }

179
  std::string definesString = this->GetDefines(language);
180
  this->LocalGenerator->JoinDefines(defines, definesString, language);
181
182

  return definesString;
183
184
185
186
187
}

cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps() const
{
  // Static libraries never depend on other targets for linking.
188
  if (this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY ||
189
      this->GeneratorTarget->GetType() == cmState::OBJECT_LIBRARY) {
190
    return cmNinjaDeps();
191
  }
192
193

  cmComputeLinkInformation* cli =
194
    this->GeneratorTarget->GetLinkInformation(this->GetConfigName());
195
  if (!cli) {
196
    return cmNinjaDeps();
197
  }
198

199
  const std::vector<std::string>& deps = cli->GetDepends();
200
201
  cmNinjaDeps result(deps.size());
  std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
Peter Kuemmel's avatar
Peter Kuemmel committed
202
203

  // Add a dependency on the link definitions file, if any.
204
205
206
207
  if (this->ModuleDefinitionFile) {
    result.push_back(
      this->ConvertToNinjaPath(this->ModuleDefinitionFile->GetFullPath()));
  }
Peter Kuemmel's avatar
Peter Kuemmel committed
208

209
210
211
212
  // Add a dependency on user-specified manifest files, if any.
  std::vector<cmSourceFile const*> manifest_srcs;
  this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
  for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
213
       mi != manifest_srcs.end(); ++mi) {
214
    result.push_back(this->ConvertToNinjaPath((*mi)->GetFullPath()));
215
  }
216

217
  // Add user-specified dependencies.
218
  if (const char* linkDepends =
219
        this->GeneratorTarget->GetProperty("LINK_DEPENDS")) {
220
221
222
223
    std::vector<std::string> linkDeps;
    cmSystemTools::ExpandListArgument(linkDepends, linkDeps);
    std::transform(linkDeps.begin(), linkDeps.end(),
                   std::back_inserter(result), MapToNinjaPath());
224
  }
225

226
227
228
  return result;
}

229
230
std::string cmNinjaTargetGenerator::GetSourceFilePath(
  cmSourceFile const* source) const
231
{
232
  return ConvertToNinjaPath(source->GetFullPath());
233
234
}

235
236
std::string cmNinjaTargetGenerator::GetObjectFilePath(
  cmSourceFile const* source) const
237
238
{
  std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
239
  if (!path.empty()) {
240
    path += "/";
241
  }
242
  std::string const& objectName = this->GeneratorTarget->GetObjectName(source);
243
  path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
244
245
  path += "/";
  path += objectName;
246
247
248
  return path;
}

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
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();
  if (!path.empty())
    path += "/";
  path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  path += "/";
  path += ppName;
  return path;
}

284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
std::string cmNinjaTargetGenerator::GetDyndepFilePath(
  std::string const& lang) const
{
  std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  if (!path.empty())
    path += "/";
  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;
}

307
308
std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
{
309
  std::string dir = this->GeneratorTarget->GetDirectory(this->GetConfigName());
310
  return ConvertToNinjaPath(dir);
311
312
}

313
314
std::string cmNinjaTargetGenerator::GetTargetFilePath(
  const std::string& name) const
315
316
{
  std::string path = this->GetTargetOutputDir();
317
  if (path.empty() || path == ".") {
318
    return name;
319
  }
320
321
322
323
324
325
326
  path += "/";
  path += name;
  return path;
}

std::string cmNinjaTargetGenerator::GetTargetName() const
{
327
  return this->GeneratorTarget->GetName();
328
329
}

330
bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
331
{
332
333
  cmMakefile* mf = this->GetMakefile();
  if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
334
      mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID")) {
335
    std::string pdbPath;
336
    std::string compilePdbPath;
337
338
339
340
    if (this->GeneratorTarget->GetType() == cmState::EXECUTABLE ||
        this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY ||
        this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY ||
        this->GeneratorTarget->GetType() == cmState::MODULE_LIBRARY) {
341
      pdbPath = this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
342
      pdbPath += "/";
343
      pdbPath += this->GeneratorTarget->GetPDBName(this->GetConfigName());
344
345
    }
    if (this->GeneratorTarget->GetType() <= cmState::OBJECT_LIBRARY) {
346
      compilePdbPath =
347
348
        this->GeneratorTarget->GetCompilePDBPath(this->GetConfigName());
      if (compilePdbPath.empty()) {
349
        compilePdbPath = this->GeneratorTarget->GetSupportDirectory() + "/";
350
      }
351
    }
352

353
    vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
354
      ConvertToNinjaPath(pdbPath), cmOutputConverter::SHELL);
355
356
    vars["TARGET_COMPILE_PDB"] =
      this->GetLocalGenerator()->ConvertToOutputFormat(
357
        ConvertToNinjaPath(compilePdbPath), cmOutputConverter::SHELL);
358

359
    EnsureParentDirectoryExists(pdbPath);
360
    EnsureParentDirectoryExists(compilePdbPath);
361
    return true;
362
  }
363
  return false;
364
365
}

366
void cmNinjaTargetGenerator::WriteLanguageRules(const std::string& language)
367
{
368
#ifdef NINJA_GEN_VERBOSE_FILES
369
  this->GetRulesFileStream() << "# Rules for language " << language << "\n\n";
370
#endif
371
372
373
  this->WriteCompileRule(language);
}

374
void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang)
375
376
377
{
  cmLocalGenerator::RuleVariables vars;
  vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
378
  vars.CMTarget = this->GetGeneratorTarget();
379
  vars.Language = lang.c_str();
380
  vars.Source = "$IN_ABS";
381
382
  vars.Object = "$out";
  vars.Defines = "$DEFINES";
383
  vars.Includes = "$INCLUDES";
384
  vars.TargetPDB = "$TARGET_PDB";
385
  vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
386
  vars.ObjectDir = "$OBJECT_DIR";
387
  vars.ObjectFileDir = "$OBJECT_FILE_DIR";
388

389
390
  // For some cases we do an explicit preprocessor invocation.
  bool const explicitPP = this->NeedExplicitPreprocessing(lang);
391
  bool const needDyndep = this->NeedDyndep(lang);
392

393
  cmMakefile* mf = this->GetMakefile();
394

395
396
397
398
399
  std::string flags = "$FLAGS";
  std::string rspfile;
  std::string rspcontent;
  std::string responseFlag;

400
  if (lang != "RC" && this->ForceResponseFile()) {
401
402
403
404
405
406
    rspfile = "$RSP_FILE";
    responseFlag = "@" + rspfile;
    rspcontent = " $DEFINES $INCLUDES $FLAGS";
    flags = responseFlag;
    vars.Defines = "";
    vars.Includes = "";
407
  }
408

409
410
411
412
  // Tell ninja dependency format so all deps can be loaded into a database
  std::string deptype;
  std::string depfile;
  std::string cldeps;
413
414
415
  if (explicitPP) {
    // The explicit preprocessing step will handle dependency scanning.
  } else if (this->NeedDepTypeMSVC(lang)) {
416
417
418
    deptype = "msvc";
    depfile = "";
    flags += " /showIncludes";
419
  } else if (mf->IsOn("CMAKE_NINJA_CMCLDEPS_" + lang)) {
420
421
    // For the MS resource compiler we need cmcldeps, but skip dependencies
    // for source-file try_compile cases because they are always fresh.
422
    if (!mf->GetIsSourceFileTryCompile()) {
423
424
      deptype = "gcc";
      depfile = "$DEP_FILE";
425
426
427
428
      const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER")
        ? mf->GetSafeDefinition("CMAKE_C_COMPILER")
        : mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
      cldeps = "\"";
429
      cldeps += cmSystemTools::GetCMClDepsCommand();
430
      cldeps += "\" " + lang + " " + vars.Source + " \"$DEP_FILE\" $out \"";
431
432
      cldeps += mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
      cldeps += "\" \"" + cl + "\" ";
433
    }
434
  } else {
435
    deptype = "gcc";
436
    const char* langdeptype = mf->GetDefinition("CMAKE_NINJA_DEPTYPE_" + lang);
437
    if (langdeptype) {
438
      deptype = langdeptype;
439
    }
440
    depfile = "$DEP_FILE";
441
    const std::string flagsName = "CMAKE_DEPFILE_FLAGS_" + lang;
Stephen Kelly's avatar
Stephen Kelly committed
442
    std::string depfileFlags = mf->GetSafeDefinition(flagsName);
443
    if (!depfileFlags.empty()) {
444
      cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
445
      cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
446
447
448
      cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
                                   mf->GetDefinition("CMAKE_C_COMPILER"));
      flags += " " + depfileFlags;
449
    }
450
  }
451

452
453
  vars.Flags = flags.c_str();
  vars.DependencyFile = depfile.c_str();
Peter Kuemmel's avatar
Peter Kuemmel committed
454

455
456
457
458
  std::string const tdi = this->GetLocalGenerator()->ConvertToOutputFormat(
    ConvertToNinjaPath(this->GetTargetDependInfoPath(lang)),
    cmLocalGenerator::SHELL);

459
460
461
462
463
464
465
  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.
466
    std::string const ppDeptype = ""; // no deps= for multiple outputs
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
    std::string const ppDepfile = "$DEP_FILE";

    cmLocalGenerator::RuleVariables ppVars;
    ppVars.RuleLauncher = vars.RuleLauncher;
    ppVars.CMTarget = vars.CMTarget;
    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.
    ppVars.Flags = vars.Flags;

    // 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;

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

    for (std::vector<std::string>::iterator i = ppCmds.begin();
         i != ppCmds.end(); ++i) {
      this->GetLocalGenerator()->ExpandRuleVariables(*i, ppVars);
    }

502
503
504
505
506
507
508
509
510
511
    // Run CMake dependency scanner on preprocessed output.
    std::string const cmake = this->GetLocalGenerator()->ConvertToOutputFormat(
      cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
    ppCmds.push_back(
      cmake + " -E cmake_ninja_depends"
              " --tdi=" +
      tdi + " --pp=$out"
            " --dep=$DEP_FILE" +
      (needDyndep ? " --obj=$OBJ_FILE --ddi=$DYNDEP_INTERMEDIATE_FILE" : ""));

512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
    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";
    this->GetGlobalGenerator()->AddRule(this->LanguagePreprocessRule(lang),
                                        ppCmdLine, ppDesc.str(),
                                        ppComment.str(), ppDepfile, ppDeptype,
                                        /*rspfile*/ "",
                                        /*rspcontent*/ "",
                                        /*restat*/ "",
                                        /*generator*/ false);
  }

529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
  if (needDyndep) {
    // Write the rule for ninja dyndep file generation.
    std::vector<std::string> ddCmds;

    // Run CMake dependency scanner on preprocessed output.
    std::string const cmake = this->GetLocalGenerator()->ConvertToOutputFormat(
      cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
    ddCmds.push_back(cmake + " -E cmake_ninja_dyndep"
                             " --tdi=" +
                     tdi + " --dd=$out"
                           " $in");

    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*/ "",
      /*deps*/ "",
      /*rspfile*/ "",
      /*rspcontent*/ "",
      /*restat*/ "",
      /*generator*/ false);
  }

558
  // Rule for compiling object file.
559
  const std::string cmdVar = std::string("CMAKE_") + lang + "_COMPILE_OBJECT";
Stephen Kelly's avatar
Stephen Kelly committed
560
  std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
561
562
  std::vector<std::string> compileCmds;
  cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
563

564
  // Maybe insert an include-what-you-use runner.
565
  if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
566
    std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
567
    const char* iwyu = this->GeneratorTarget->GetProperty(iwyu_prop);
568
    std::string const tidy_prop = lang + "_CLANG_TIDY";
569
570
571
    const char* tidy = this->GeneratorTarget->GetProperty(tidy_prop);
    if ((iwyu && *iwyu) || (tidy && *tidy)) {
      std::string run_iwyu = this->GetLocalGenerator()->ConvertToOutputFormat(
572
        cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
573
      run_iwyu += " -E __run_iwyu";
574
      if (iwyu && *iwyu) {
575
576
        run_iwyu += " --iwyu=";
        run_iwyu += this->GetLocalGenerator()->EscapeForShell(iwyu);
577
578
      }
      if (tidy && *tidy) {
579
580
581
        run_iwyu += " --tidy=";
        run_iwyu += this->GetLocalGenerator()->EscapeForShell(tidy);
        run_iwyu += " --source=$in";
582
      }
583
584
585
      run_iwyu += " -- ";
      compileCmds.front().insert(0, run_iwyu);
    }
586
  }
587

588
  // Maybe insert a compiler launcher like ccache or distcc
589
  if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
590
    std::string const clauncher_prop = lang + "_COMPILER_LAUNCHER";
591
592
    const char* clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
    if (clauncher && *clauncher) {
593
594
595
      std::vector<std::string> launcher_cmd;
      cmSystemTools::ExpandListArgument(clauncher, launcher_cmd, true);
      for (std::vector<std::string>::iterator i = launcher_cmd.begin(),
596
597
                                              e = launcher_cmd.end();
           i != e; ++i) {
598
        *i = this->LocalGenerator->EscapeForShell(*i);
599
      }
600
601
602
      std::string const& run_launcher = cmJoin(launcher_cmd, " ") + " ";
      compileCmds.front().insert(0, run_launcher);
    }
603
  }
604

605
  if (!compileCmds.empty()) {
606
    compileCmds.front().insert(0, cldeps);
607
  }
608

609
  for (std::vector<std::string>::iterator i = compileCmds.begin();
610
       i != compileCmds.end(); ++i) {
611
    this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
612
  }
613

614
615
  std::string cmdLine =
    this->GetLocalGenerator()->BuildCommandLine(compileCmds);
616

617
  // Write the rule for compiling file of the given language.
618
  std::ostringstream comment;
619
  comment << "Rule for compiling " << lang << " files.";
620
  std::ostringstream description;
621
  description << "Building " << lang << " object $out";
622
623
624
625
626
  this->GetGlobalGenerator()->AddRule(
    this->LanguageCompilerRule(lang), cmdLine, description.str(),
    comment.str(), depfile, deptype, rspfile, rspcontent,
    /*restat*/ "",
    /*generator*/ false);
627
628
}

629
void cmNinjaTargetGenerator::WriteObjectBuildStatements()
630
631
632
633
634
{
  // Write comments.
  cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  this->GetBuildFileStream()
    << "# Object build statements for "
635
    << cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType())
636
    << " target " << this->GetTargetName() << "\n\n";
637

638
  std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
639
  std::vector<cmSourceFile const*> customCommands;
640
  this->GeneratorTarget->GetCustomCommands(customCommands, config);
641
642
643
644
645
646
647
648
649
650
  for (std::vector<cmSourceFile const*>::const_iterator si =
         customCommands.begin();
       si != customCommands.end(); ++si) {
    cmCustomCommand const* cc = (*si)->GetCustomCommand();
    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);
  }
651
  std::vector<cmSourceFile const*> headerSources;
652
  this->GeneratorTarget->GetHeaderSources(headerSources, config);
653
  this->OSXBundleGenerator->GenerateMacOSXContentStatements(
654
    headerSources, this->MacOSXContentGenerator);
655
  std::vector<cmSourceFile const*> extraSources;
656
  this->GeneratorTarget->GetExtraSources(extraSources, config);
657
  this->OSXBundleGenerator->GenerateMacOSXContentStatements(
658
    extraSources, this->MacOSXContentGenerator);
659
  std::vector<cmSourceFile const*> externalObjects;
660
  this->GeneratorTarget->GetExternalObjects(externalObjects, config);
661
662
663
  for (std::vector<cmSourceFile const*>::const_iterator si =
         externalObjects.begin();
       si != externalObjects.end(); ++si) {
664
    this->Objects.push_back(this->GetSourceFilePath(*si));
665
  }
666
667

  cmNinjaDeps orderOnlyDeps;
668
669
  this->GetLocalGenerator()->AppendTargetDepends(this->GeneratorTarget,
                                                 orderOnlyDeps);
670

671
672
673
674
  // Add order-only dependencies on other files associated with the target.
  orderOnlyDeps.insert(orderOnlyDeps.end(), this->ExtraFiles.begin(),
                       this->ExtraFiles.end());

675
  // Add order-only dependencies on custom command outputs.
676
677
678
  for (std::vector<cmCustomCommand const*>::const_iterator cci =
         this->CustomCommands.begin();
       cci != this->CustomCommands.end(); ++cci) {
679
680
    cmCustomCommand const* cc = *cci;
    cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
681
                                 this->GetLocalGenerator());
682
    const std::vector<std::string>& ccoutputs = ccg.GetOutputs();
683
    const std::vector<std::string>& ccbyproducts = ccg.GetByproducts();
684
685
    std::transform(ccoutputs.begin(), ccoutputs.end(),
                   std::back_inserter(orderOnlyDeps), MapToNinjaPath());
686
687
    std::transform(ccbyproducts.begin(), ccbyproducts.end(),
                   std::back_inserter(orderOnlyDeps), MapToNinjaPath());
688
  }
689

690
  if (!orderOnlyDeps.empty()) {
691
692
    cmNinjaDeps orderOnlyTarget;
    orderOnlyTarget.push_back(this->OrderDependsTargetForTarget());
693
694
695
696
697
    this->GetGlobalGenerator()->WritePhonyBuild(
      this->GetBuildFileStream(),
      "Order-only phony target for " + this->GetTargetName(), orderOnlyTarget,
      cmNinjaDeps(), cmNinjaDeps(), orderOnlyDeps);
  }
698
  std::vector<cmSourceFile const*> objectSources;
699
  this->GeneratorTarget->GetObjectSources(objectSources, config);
700
701
702
  for (std::vector<cmSourceFile const*>::const_iterator si =
         objectSources.begin();
       si != objectSources.end(); ++si) {
703
    this->WriteObjectBuildStatement(*si, !orderOnlyDeps.empty());
704
  }
705

706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
  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"));

    this->GetGlobalGenerator()->WriteBuild(
      this->GetBuildFileStream(), ddComment, ddRule, ddOutputs, ddImplicitOuts,
      ddExplicitDeps, ddImplicitDeps, ddOrderOnlyDeps, ddVars);
  }

725
726
727
  this->GetBuildFileStream() << "\n";
}

728
void cmNinjaTargetGenerator::WriteObjectBuildStatement(
729
  cmSourceFile const* source, bool writeOrderDependsTargetForTarget)
730
{
731
  std::string const language = source->GetLanguage();
732
  std::string const sourceFileName = this->GetSourceFilePath(source);
733
734
735
736
  std::string const objectDir =
    this->ConvertToNinjaPath(this->GeneratorTarget->GetSupportDirectory());
  std::string const objectFileName =
    this->ConvertToNinjaPath(this->GetObjectFilePath(source));
737
738
739
740
  std::string const objectFileDir =
    cmSystemTools::GetFilenamePath(objectFileName);

  cmNinjaVars vars;
741
742
  vars["IN_ABS"] = this->GetLocalGenerator()->ConvertToOutputFormat(
    source->GetFullPath(), cmOutputConverter::SHELL);
743
744
745
  vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  vars["DEFINES"] = this->ComputeDefines(source, language);
  vars["INCLUDES"] = this->GetIncludes(language);
746
  if (!this->NeedDepTypeMSVC(language)) {
747
748
    vars["DEP_FILE"] =
      cmGlobalNinjaGenerator::EncodeDepfileSpace(objectFileName + ".d");
749
  }
750
751

  this->ExportObjectCompileCommand(
752
753
    language, sourceFileName, objectDir, objectFileName, objectFileDir,
    vars["FLAGS"], vars["DEFINES"], vars["INCLUDES"]);
754

755
756
757
758
759
760
761
762
763
764
765
  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);

766
  cmNinjaDeps implicitDeps;
767
  if (const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
768
769
    std::vector<std::string> depList;
    cmSystemTools::ExpandListArgument(objectDeps, depList);
770
771
772
    for (std::vector<std::string>::iterator odi = depList.begin();
         odi != depList.end(); ++odi) {
      if (cmSystemTools::FileIsFullPath(*odi)) {
773
774
        *odi = cmSystemTools::CollapseFullPath(*odi);
      }
775
    }
776
    std::transform(depList.begin(), depList.end(),
777
                   std::back_inserter(implicitDeps), MapToNinjaPath());
778
779
  }

780
  cmNinjaDeps orderOnlyDeps;
781
  if (writeOrderDependsTargetForTarget) {
782
    orderOnlyDeps.push_back(this->OrderDependsTargetForTarget());
783
  }
784

785
786
787
788
789
790
791
792
793
794
  // 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.
  if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
      !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
    this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
                                                             orderOnlyDeps);
  }

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

798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
  // 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);

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

    // Move preprocessor definitions to the preprocessor build statement.
    std::swap(ppVars["DEFINES"], vars["DEFINES"]);

    // Copy include directories to the preprocessor build statement.  The
    // Fortran compilation build statement still needs them for the INCLUDE
    // directive.
    ppVars["INCLUDES"] = vars["INCLUDES"];

834
835
836
837
838
839
840
841
842
843
844
845
    // Prepend source file's original directory as an include directory
    // so e.g. Fortran INCLUDE statements can look for files in it.
    std::vector<std::string> sourceDirectory;
    sourceDirectory.push_back(
      cmSystemTools::GetParentDirectory(source->GetFullPath()));

    std::string sourceDirectoryFlag = this->LocalGenerator->GetIncludeFlags(
      sourceDirectory, this->GeneratorTarget, language, false, false,
      this->GetConfigName());

    vars["INCLUDES"] = sourceDirectoryFlag + " " + vars["INCLUDES"];

846
847
848
849
850
851
852
    // Explicit preprocessing always uses a depfile.
    ppVars["DEP_FILE"] =
      cmGlobalNinjaGenerator::EncodeDepfileSpace(ppFileName + ".d");
    // The actual compilation does not need a depfile because it
    // depends on the already-preprocessed source.
    vars.erase("DEP_FILE");

853
854
855
856
857
858
859
860
861
862
863
864
    if (needDyndep) {
      // Tell dependency scanner the object file that will result from
      // compiling the preprocessed source.
      ppVars["OBJ_FILE"] = objectFileName;

      // Tell dependency scanner where to store dyndep intermediate results.
      std::string const ddiFile = ppFileName + ".ddi";
      ppVars["DYNDEP_INTERMEDIATE_FILE"] = ddiFile;
      ppImplicitOuts.push_back(ddiFile);
      this->DDIFiles.push_back(ddiFile);
    }

865
866
867
868
869
870
871
    this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
                               ppVars);

    this->GetGlobalGenerator()->WriteBuild(
      this->GetBuildFileStream(), ppComment, ppRule, ppOutputs, ppImplicitOuts,
      ppExplicitDeps, ppImplicitDeps, ppOrderOnlyDeps, ppVars);
  }
872
873
874
875
876
  if (needDyndep) {
    std::string const dyndep = this->GetDyndepFilePath(language);
    orderOnlyDeps.push_back(dyndep);
    vars["dyndep"] = dyndep;
  }
877

878
  EnsureParentDirectoryExists(objectFileName);
Peter Kuemmel's avatar
Peter Kuemmel committed
879

880
  vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
881
    objectDir, cmOutputConverter::SHELL);
882
  vars["OBJECT_FILE_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
883
    objectFileDir, cmOutputConverter::SHELL);
884

885
886
  this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
                             vars);
887

888
  this->SetMsvcTargetPdbVariable(vars);
889

890
891
892
  bool const isRC = (language == "RC");
  int const commandLineLengthLimit =
    ((!isRC && this->ForceResponseFile())) ? -1 : 0;
893
894
  std::string const rspfile = objectFileName + ".rsp";

895
  this->GetGlobalGenerator()->WriteBuild(
896
897
898
    this->GetBuildFileStream(), comment, rule, outputs,
    /*implicitOuts=*/cmNinjaDeps(), explicitDeps, implicitDeps, orderOnlyDeps,
    vars, rspfile, commandLineLengthLimit);
899

900
  if (const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
901
902
903
904
    std::vector<std::string> outputList;
    cmSystemTools::ExpandListArgument(objectOutputs, outputList);
    std::transform(outputList.begin(), outputList.end(), outputList.begin(),
                   MapToNinjaPath());
905
906
    this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
                                                "Additional output files.",
907
                                                outputList, outputs);
908
  }
909
}
Peter Kuemmel's avatar
Peter Kuemmel committed
910

911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
void cmNinjaTargetGenerator::WriteTargetDependInfo(std::string const& lang)
{
  Json::Value tdi(Json::objectValue);
  tdi["language"] = lang;
  tdi["compiler-id"] =
    this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER_ID");

  if (lang == "Fortran") {
    std::string mod_dir = this->GeneratorTarget->GetFortranModuleDirectory(
      this->Makefile->GetHomeOutputDirectory());
    if (mod_dir.empty()) {
      mod_dir = this->Makefile->GetCurrentBinaryDirectory();
    }
    tdi["module-dir"] = mod_dir;
  }

  tdi["dir-cur-bld"] = this->Makefile->GetCurrentBinaryDirectory();
  tdi["dir-cur-src"] = this->Makefile->GetCurrentSourceDirectory();
  tdi["dir-top-bld"] = this->Makefile->GetHomeOutputDirectory();
  tdi["dir-top-src"] = this->Makefile->GetHomeDirectory();

  Json::Value& tdi_include_dirs = tdi["include-dirs"] = Json::arrayValue;
  std::vector<std::string> includes;
  this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
                                              lang, this->GetConfigName());
  for (std::vector<std::string>::iterator i = includes.begin();
       i != includes.end(); ++i) {
    tdi_include_dirs.append(*i);
  }

  Json::Value& tdi_linked_target_dirs = tdi["linked-target-dirs"] =
    Json::arrayValue;
  std::vector<std::string> linked = this->GetLinkedTargetDirectories();
  for (std::vector<std::string>::iterator i = linked.begin();
       i != linked.end(); ++i) {
    tdi_linked_target_dirs.append(*i);
  }

  std::string const tdin = this->GetTargetDependInfoPath(lang);
  cmGeneratedFileStream tdif(tdin.c_str());
  tdif << tdi;
}

954
955
956
957
958
void cmNinjaTargetGenerator::ExportObjectCompileCommand(
  std::string const& language, std::string const& sourceFileName,
  std::string const& objectDir, std::string const& objectFileName,
  std::string const& objectFileDir, std::string const& flags,
  std::string const& defines, std::string const& includes)
959
{
960
  if (!this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS")) {
961
    return;
962
  }
963
964
965
966
967
968

  cmLocalGenerator::RuleVariables compileObjectVars;
  compileObjectVars.Language = language.c_str();

  std::string escapedSourceFileName = sourceFileName;

969
  if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str())) {
970
    escapedSourceFileName = cmSystemTools::CollapseFullPath(
971
972
973
974
      escapedSourceFileName, this->GetGlobalGenerator()
                               ->GetCMakeInstance()
                               ->GetHomeOutputDirectory());
  }
975

976
  escapedSourceFileName = this->LocalGenerator->ConvertToOutputFormat(
977
    escapedSourceFileName, cmOutputConverter::SHELL);
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996

  compileObjectVars.Source = escapedSourceFileName.c_str();
  compileObjectVars.Object = objectFileName.c_str();
  compileObjectVars.ObjectDir = objectDir.c_str();
  compileObjectVars.ObjectFileDir = objectFileDir.c_str();
  compileObjectVars.Flags = flags.c_str();
  compileObjectVars.Defines = defines.c_str();
  compileObjectVars.Includes = includes.c_str();

  // Rule for compiling object file.
  std::string compileCmdVar = "CMAKE_";
  compileCmdVar += language;
  compileCmdVar += "_COMPILE_OBJECT";
  std::string compileCmd =
    this->GetMakefile()->GetRequiredDefinition(compileCmdVar);
  std::vector<std::string> compileCmds;
  cmSystemTools::ExpandListArgument(compileCmd, compileCmds);

  for (std::vector<std::string>::iterator i = compileCmds.begin();
997
       i != compileCmds.end(); ++i) {
998
    this->GetLocalGenerator()->ExpandRuleVariables(*i, compileObjectVars);