cmNinjaNormalTargetGenerator.cxx 24.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2011 Peter Collingbourne <peter@pcc.me.uk>
  Copyright 2011 Nicolas Despres <nicolas.despres@gmail.com>

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
#include "cmNinjaNormalTargetGenerator.h"
#include "cmLocalNinjaGenerator.h"
#include "cmGlobalNinjaGenerator.h"
#include "cmSourceFile.h"
#include "cmGeneratedFileStream.h"
#include "cmMakefile.h"
19
#include "cmOSXBundleGenerator.h"
20
#include "cmGeneratorTarget.h"
21
#include "cmCustomCommandGenerator.h"
22
#include "cmAlgorithms.h"
23
24

#include <assert.h>
25
#include <algorithm>
26
#include <limits>
27

28
29
30
31
32
#ifndef _WIN32
#include <unistd.h>
#endif


33
cmNinjaNormalTargetGenerator::
34
cmNinjaNormalTargetGenerator(cmGeneratorTarget* target)
35
  : cmNinjaTargetGenerator(target)
36
37
38
39
40
  , TargetNameOut()
  , TargetNameSO()
  , TargetNameReal()
  , TargetNameImport()
  , TargetNamePDB()
41
  , TargetLinkLanguage("")
42
{
43
44
  this->TargetLinkLanguage = target->Target
                                   ->GetLinkerLanguage(this->GetConfigName());
45
  if (target->GetType() == cmTarget::EXECUTABLE)
46
    target->Target->GetExecutableNames(this->TargetNameOut,
47
48
49
50
51
                               this->TargetNameReal,
                               this->TargetNameImport,
                               this->TargetNamePDB,
                               GetLocalGenerator()->GetConfigName());
  else
52
    target->Target->GetLibraryNames(this->TargetNameOut,
53
54
55
56
57
                            this->TargetNameSO,
                            this->TargetNameReal,
                            this->TargetNameImport,
                            this->TargetNamePDB,
                            GetLocalGenerator()->GetConfigName());
58

59
60
61
62
  if(target->GetType() != cmTarget::OBJECT_LIBRARY)
    {
    // on Windows the output dir is already needed at compile time
    // ensure the directory exists (OutDir test)
63
    EnsureDirectoryExists(target->Target->GetDirectory(this->GetConfigName()));
64
    }
65

66
67
68
  this->OSXBundleGenerator = new cmOSXBundleGenerator(target,
                                                      this->GetConfigName());
  this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
69
70
71
72
}

cmNinjaNormalTargetGenerator::~cmNinjaNormalTargetGenerator()
{
73
  delete this->OSXBundleGenerator;
74
75
76
77
}

void cmNinjaNormalTargetGenerator::Generate()
{
78
  if (this->TargetLinkLanguage.empty()) {
79
80
    cmSystemTools::Error("CMake can not determine linker language for "
                         "target: ",
81
                         this->GetTarget()->GetName().c_str());
82
83
84
85
86
87
88
89
90
    return;
  }

  // Write the rules for each language.
  this->WriteLanguagesRules();

  // Write the build statements
  this->WriteObjectBuildStatements();

91
92
93
94
95
96
97
98
  if(this->GetTarget()->GetType() == cmTarget::OBJECT_LIBRARY)
    {
    this->WriteObjectLibStatement();
    }
  else
    {
    this->WriteLinkStatement();
    }
99
100
101
102
}

void cmNinjaNormalTargetGenerator::WriteLanguagesRules()
{
103
#ifdef NINJA_GEN_VERBOSE_FILES
104
105
106
107
108
109
110
  cmGlobalNinjaGenerator::WriteDivider(this->GetRulesFileStream());
  this->GetRulesFileStream()
    << "# Rules for each languages for "
    << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
    << " target "
    << this->GetTargetName()
    << "\n\n";
111
#endif
112

113
  // Write rules for languages compiled in this target.
114
  std::set<std::string> languages;
115
116
117
118
119
120
121
122
123
124
125
126
  std::vector<cmSourceFile*> sourceFiles;
  this->GetTarget()->GetSourceFiles(sourceFiles,
    this->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  for(std::vector<cmSourceFile*>::const_iterator
        i = sourceFiles.begin(); i != sourceFiles.end(); ++i)
    {
    const std::string& lang = (*i)->GetLanguage();
    if(!lang.empty())
      {
      languages.insert(lang);
      }
    }
127
  for(std::set<std::string>::const_iterator l = languages.begin();
128
129
      l != languages.end();
      ++l)
130
    {
131
    this->WriteLanguageRules(*l);
132
    }
133
134
135
136
137
138
139
140
141
142
}

const char *cmNinjaNormalTargetGenerator::GetVisibleTypeName() const
{
  switch (this->GetTarget()->GetType()) {
    case cmTarget::STATIC_LIBRARY:
      return "static library";
    case cmTarget::SHARED_LIBRARY:
      return "shared library";
    case cmTarget::MODULE_LIBRARY:
143
144
145
146
      if (this->GetTarget()->IsCFBundleOnApple())
        return "CFBundle shared module";
      else
        return "shared module";
147
148
149
150
151
152
153
154
155
156
157
    case cmTarget::EXECUTABLE:
      return "executable";
    default:
      return 0;
  }
}

std::string
cmNinjaNormalTargetGenerator
::LanguageLinkerRule() const
{
158
  return this->TargetLinkLanguage
159
160
    + "_"
    + cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
161
162
163
    + "_LINKER__"
    + cmGlobalNinjaGenerator::EncodeRuleName(this->GetTarget()->GetName())
    ;
164
165
166
167
}

void
cmNinjaNormalTargetGenerator
168
::WriteLinkRule(bool useResponseFile)
169
170
171
172
{
  cmTarget::TargetType targetType = this->GetTarget()->GetType();
  std::string ruleName = this->LanguageLinkerRule();

173
174
  // Select whether to use a response file for objects.
  std::string rspfile;
175
  std::string rspcontent;
176
177
178
179
180

  if (!this->GetGlobalGenerator()->HasRule(ruleName)) {
    cmLocalGenerator::RuleVariables vars;
    vars.RuleLauncher = "RULE_LAUNCH_LINK";
    vars.CMTarget = this->GetTarget();
181
    vars.Language = this->TargetLinkLanguage.c_str();
182
183
184
185

    std::string responseFlag;
    if (!useResponseFile) {
      vars.Objects = "$in";
186
      vars.LinkLibraries = "$LINK_PATH $LINK_LIBRARIES";
187
    } else {
188
189
190
191
192
        std::string cmakeVarLang = "CMAKE_";
        cmakeVarLang += this->TargetLinkLanguage;

        // build response file name
        std::string cmakeLinkVar =  cmakeVarLang + "_RESPONSE_FILE_LINK_FLAG";
Stephen Kelly's avatar
Stephen Kelly committed
193
        const char * flag = GetMakefile()->GetDefinition(cmakeLinkVar);
194
195
196
197
198
        if(flag) {
          responseFlag = flag;
        } else {
          responseFlag = "@";
        }
199
        rspfile = "$RSP_FILE";
200
        responseFlag += rspfile;
201
202

        // build response file content
203
204
205
206
207
208
        if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
          rspcontent = "$in";
        } else {
          rspcontent = "$in_newline";
        }
        rspcontent += " $LINK_PATH $LINK_LIBRARIES";
209
        vars.Objects = responseFlag.c_str();
210
        vars.LinkLibraries = "";
211
    }
Peter Kuemmel's avatar
Peter Kuemmel committed
212
213

    vars.ObjectDir = "$OBJECT_DIR";
214

215
    vars.Target = "$TARGET_FILE";
216

217
    vars.SONameFlag = "$SONAME_FLAG";
218
219
    vars.TargetSOName = "$SONAME";
    vars.TargetInstallNameDir = "$INSTALLNAME_DIR";
220
    vars.TargetPDB = "$TARGET_PDB";
221
222
223
224
225

    // Setup the target version.
    std::string targetVersionMajor;
    std::string targetVersionMinor;
    {
226
227
    std::ostringstream majorStream;
    std::ostringstream minorStream;
228
229
230
231
232
233
234
235
236
237
238
    int major;
    int minor;
    this->GetTarget()->GetTargetVersion(major, minor);
    majorStream << major;
    minorStream << minor;
    targetVersionMajor = majorStream.str();
    targetVersionMinor = minorStream.str();
    }
    vars.TargetVersionMajor = targetVersionMajor.c_str();
    vars.TargetVersionMinor = targetVersionMinor.c_str();

239
    vars.Flags = "$FLAGS";
240
241
242
    vars.LinkFlags = "$LINK_FLAGS";

    std::string langFlags;
243
244
245
    if (targetType != cmTarget::EXECUTABLE)
      {
      langFlags += "$LANGUAGE_COMPILE_FLAGS $ARCH_FLAGS";
246
      vars.LanguageCompileFlags = langFlags.c_str();
247
      }
248

249
    // Rule for linking library/executable.
250
251
252
253
254
255
256
257
258
259
260
261
    std::vector<std::string> linkCmds = this->ComputeLinkCmd();
    for(std::vector<std::string>::iterator i = linkCmds.begin();
        i != linkCmds.end();
        ++i)
      {
      this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
      }
    linkCmds.insert(linkCmds.begin(), "$PRE_LINK");
    linkCmds.push_back("$POST_BUILD");
    std::string linkCmd =
      this->GetLocalGenerator()->BuildCommandLine(linkCmds);

262
    // Write the linker rule with response file if needed.
263
    std::ostringstream comment;
264
265
    comment << "Rule for linking " << this->TargetLinkLanguage << " "
            << this->GetVisibleTypeName() << ".";
266
    std::ostringstream description;
267
    description << "Linking " << this->TargetLinkLanguage << " "
268
                << this->GetVisibleTypeName() << " $TARGET_FILE";
269
270
271
    this->GetGlobalGenerator()->AddRule(ruleName,
                                        linkCmd,
                                        description.str(),
272
273
                                        comment.str(),
                                        /*depfile*/ "",
274
                                        /*deptype*/ "",
275
                                        rspfile,
276
                                        rspcontent,
277
                                        /*restat*/ "$RESTAT",
278
                                        /*generator*/ false);
279
280
  }

281
282
  if (this->TargetNameOut != this->TargetNameReal &&
    !this->GetTarget()->IsFrameworkOnApple()) {
283
    std::string cmakeCommand =
284
      this->GetLocalGenerator()->ConvertToOutputFormat(
285
        cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
286
287
288
289
290
291
    if (targetType == cmTarget::EXECUTABLE)
      this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_EXECUTABLE",
                                          cmakeCommand +
                                          " -E cmake_symlink_executable"
                                          " $in $out && $POST_BUILD",
                                          "Creating executable symlink $out",
292
293
294
295
296
297
                                          "Rule for creating "
                                          "executable symlink.",
                                          /*depfile*/ "",
                                          /*deptype*/ "",
                                          /*rspfile*/ "",
                                          /*rspcontent*/ "",
298
                                          /*restat*/ "",
299
                                          /*generator*/ false);
300
301
302
303
304
305
    else
      this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_LIBRARY",
                                          cmakeCommand +
                                          " -E cmake_symlink_library"
                                          " $in $SONAME $out && $POST_BUILD",
                                          "Creating library symlink $out",
306
307
308
309
310
311
                                          "Rule for creating "
                                          "library symlink.",
                                          /*depfile*/ "",
                                          /*deptype*/ "",
                                          /*rspfile*/ "",
                                          /*rspcontent*/ "",
312
                                          /*restat*/ "",
313
                                          /*generator*/ false);
314
315
316
317
318
319
320
  }
}

std::vector<std::string>
cmNinjaNormalTargetGenerator
::ComputeLinkCmd()
{
321
  std::vector<std::string> linkCmds;
322
323
  cmMakefile* mf = this->GetMakefile();
  {
324
325
  std::string linkCmdVar = this->GetGeneratorTarget()
    ->GetCreateRuleVariable(this->TargetLinkLanguage, this->GetConfigName());
326
327
328
329
330
331
332
333
  const char *linkCmd = mf->GetDefinition(linkCmdVar);
  if (linkCmd)
    {
    cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
    return linkCmds;
    }
  }
  switch (this->GetTarget()->GetType()) {
334
335
    case cmTarget::STATIC_LIBRARY: {
      // We have archive link commands set. First, delete the existing archive.
336
      {
337
      std::string cmakeCommand =
338
        this->GetLocalGenerator()->ConvertToOutputFormat(
339
          cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
340
      linkCmds.push_back(cmakeCommand + " -E remove $TARGET_FILE");
341
      }
342
343
344
345
346
      // TODO: Use ARCHIVE_APPEND for archives over a certain size.
      {
      std::string linkCmdVar = "CMAKE_";
      linkCmdVar += this->TargetLinkLanguage;
      linkCmdVar += "_ARCHIVE_CREATE";
347
      const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
348
      cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
349
350
351
352
353
      }
      {
      std::string linkCmdVar = "CMAKE_";
      linkCmdVar += this->TargetLinkLanguage;
      linkCmdVar += "_ARCHIVE_FINISH";
354
      const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
355
      cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
356
357
358
359
360
      }
      return linkCmds;
    }
    case cmTarget::SHARED_LIBRARY:
    case cmTarget::MODULE_LIBRARY:
361
362
    case cmTarget::EXECUTABLE:
      break;
363
364
365
    default:
      assert(0 && "Unexpected target type");
  }
366
  return std::vector<std::string>();
367
368
}

369
370

static int calculateCommandLineLengthLimit(int linkRuleLength)
371
{
372
  static int const limits[] = {
373
#ifdef _WIN32
374
    8000,
375
#endif
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#if defined(__APPLE__) || defined(__HAIKU__) || defined(__linux)
    // for instance ARG_MAX is 2096152 on Ubuntu or 262144 on Mac
    ((int)sysconf(_SC_ARG_MAX)) - 1000,
#endif
#if defined(__linux)
    // #define MAX_ARG_STRLEN (PAGE_SIZE * 32) in Linux's binfmts.h
    ((int)sysconf(_SC_PAGESIZE) * 32) - 1000,
#endif
    std::numeric_limits<int>::max()
  };

  size_t const arrSz = cmArraySize(limits);
  int const sz = *std::min_element(limits, limits + arrSz);
  if (sz == std::numeric_limits<int>::max())
    {
    return -1;
    }

  return sz - linkRuleLength;
395
396
}

397

398
399
400
401
void cmNinjaNormalTargetGenerator::WriteLinkStatement()
{
  cmTarget& target = *this->GetTarget();
  const std::string cfgName = this->GetConfigName();
402
  std::string targetOutput = ConvertToNinjaPath(
403
                               target.GetFullPath(cfgName));
404
  std::string targetOutputReal = ConvertToNinjaPath(
405
406
                                   target.GetFullPath(cfgName,
                                      /*implib=*/false,
407
                                      /*realpath=*/true));
408
  std::string targetOutputImplib = ConvertToNinjaPath(
409
                                     target.GetFullPath(cfgName,
410
                                       /*implib=*/true));
411

412
  if (target.IsAppBundleOnApple())
413
414
    {
    // Create the app bundle
415
    std::string outpath = target.GetDirectory(cfgName);
416
    this->OSXBundleGenerator->CreateAppBundle(this->TargetNameOut, outpath);
417
418

    // Calculate the output path
419
420
421
    targetOutput = outpath;
    targetOutput += "/";
    targetOutput += this->TargetNameOut;
422
    targetOutput = this->ConvertToNinjaPath(targetOutput);
423
424
425
    targetOutputReal = outpath;
    targetOutputReal += "/";
    targetOutputReal += this->TargetNameReal;
426
    targetOutputReal = this->ConvertToNinjaPath(targetOutputReal);
427
    }
428
  else if (target.IsFrameworkOnApple())
429
430
    {
    // Create the library framework.
431
432
    this->OSXBundleGenerator->CreateFramework(this->TargetNameOut,
                                              target.GetDirectory(cfgName));
433
    }
434
  else if(target.IsCFBundleOnApple())
435
    {
436
    // Create the core foundation bundle.
437
438
    this->OSXBundleGenerator->CreateCFBundle(this->TargetNameOut,
                                             target.GetDirectory(cfgName));
439
    }
440

441
442
  // Write comments.
  cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
443
  const cmTarget::TargetType targetType = target.GetType();
444
445
446
447
448
449
450
451
452
453
454
  this->GetBuildFileStream()
    << "# Link build statements for "
    << cmTarget::GetTargetTypeName(targetType)
    << " target "
    << this->GetTargetName()
    << "\n\n";

  cmNinjaDeps emptyDeps;
  cmNinjaVars vars;

  // Compute the comment.
455
  std::ostringstream comment;
456
457
  comment <<
    "Link the " << this->GetVisibleTypeName() << " " << targetOutputReal;
458
459
460
461
462
463

  // Compute outputs.
  cmNinjaDeps outputs;
  outputs.push_back(targetOutputReal);

  // Compute specific libraries to link with.
464
465
  cmNinjaDeps explicitDeps = this->GetObjects();
  cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
466

467
468
  cmMakefile* mf = this->GetMakefile();

469
470
  std::string frameworkPath;
  std::string linkPath;
471
  cmGeneratorTarget& genTarget = *this->GetGeneratorTarget();
472

473
474
475
  std::string createRule =
    genTarget.GetCreateRuleVariable(this->TargetLinkLanguage,
                                    this->GetConfigName());
476
  bool useWatcomQuote = mf->IsOn(createRule+"_USE_WATCOM_QUOTE");
477
  cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
478
479
480
481

  vars["TARGET_FILE"] =
    localGen.ConvertToOutputFormat(targetOutputReal, cmLocalGenerator::SHELL);

482
483
484
485
486
487
488
  localGen.GetTargetFlags(vars["LINK_LIBRARIES"],
                          vars["FLAGS"],
                          vars["LINK_FLAGS"],
                          frameworkPath,
                          linkPath,
                          &genTarget,
                          useWatcomQuote);
489

490
  this->addPoolNinjaVariable("JOB_POOL_LINK", &target, vars);
491

492
  this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]);
493
494
495
  vars["LINK_FLAGS"] = cmGlobalNinjaGenerator
                        ::EncodeLiteral(vars["LINK_FLAGS"]);

496
  vars["LINK_PATH"] = frameworkPath + linkPath;
497

498
499
500
  // Compute architecture specific link flags.  Yes, these go into a different
  // variable for executables, probably due to a mistake made when duplicating
  // code between the Makefile executable and library generators.
501
502
  if (targetType == cmTarget::EXECUTABLE)
    {
503
504
505
    std::string t = vars["FLAGS"];
    localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
    vars["FLAGS"] = t;
506
507
508
    }
  else
    {
509
510
511
512
513
514
    std::string t = vars["ARCH_FLAGS"];
    localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
    vars["ARCH_FLAGS"] = t;
    t = "";
    localGen.AddLanguageFlags(t, TargetLinkLanguage, cfgName);
    vars["LANGUAGE_COMPILE_FLAGS"] = t;
515
    }
516

517
518
519
  if (target.HasSOName(cfgName))
    {
    vars["SONAME_FLAG"] = mf->GetSONameFlag(this->TargetLinkLanguage);
520
    vars["SONAME"] = this->TargetNameSO;
521
522
523
524
525
526
527
    if (targetType == cmTarget::SHARED_LIBRARY)
      {
      std::string install_dir = target.GetInstallNameDirForBuildTree(cfgName);
      if (!install_dir.empty())
        {
        vars["INSTALLNAME_DIR"] = localGen.Convert(install_dir,
                                                   cmLocalGenerator::NONE,
528
                                                   cmLocalGenerator::SHELL);
529
        }
530
      }
531
532
    }

533
534
535
536
537
  if (!this->TargetNameImport.empty())
    {
    const std::string impLibPath = localGen.ConvertToOutputFormat(
                                              targetOutputImplib,
                                              cmLocalGenerator::SHELL);
538
539
    vars["TARGET_IMPLIB"] = impLibPath;
    EnsureParentDirectoryExists(impLibPath);
540
541
542
543
    if(target.HasImportLibrary())
      {
      outputs.push_back(targetOutputImplib);
      }
544
    }
545

546
  if (!this->SetMsvcTargetPdbVariable(vars))
547
548
549
    {
    // It is common to place debug symbols at a specific place,
    // so we need a plain target name in the rule available.
550
    std::string prefix;
551
552
    std::string base;
    std::string suffix;
553
    target.GetFullNameComponents(prefix, base, suffix);
554
555
556
    std::string dbg_suffix = ".dbg";
    // TODO: Where to document?
    if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX"))
557
      {
558
      dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX");
559
      }
560
    vars["TARGET_PDB"] = base + suffix + dbg_suffix;
561
    }
Peter Kuemmel's avatar
Peter Kuemmel committed
562

563
  if (this->GetGlobalGenerator()->IsGCCOnWindows())
Peter Kuemmel's avatar
Peter Kuemmel committed
564
    {
565
    const std::string objPath = GetTarget()->GetSupportDirectory();
566
    vars["OBJECT_DIR"] = ConvertToNinjaPath(objPath);
567
    EnsureDirectoryExists(objPath);
568
    // ar.exe can't handle backslashes in rsp files (implicitly used by gcc)
569
570
    std::string& linkLibraries = vars["LINK_LIBRARIES"];
    std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
571
572
    std::string& link_path = vars["LINK_PATH"];
    std::replace(link_path.begin(), link_path.end(), '\\', '/');
Peter Kuemmel's avatar
Peter Kuemmel committed
573
    }
574

575
  const std::vector<cmCustomCommand> *cmdLists[3] = {
576
577
578
    &target.GetPreBuildCommands(),
    &target.GetPreLinkCommands(),
    &target.GetPostBuildCommands()
579
580
581
582
583
584
585
586
587
  };

  std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
  std::vector<std::string> *cmdLineLists[3] = {
    &preLinkCmdLines,
    &preLinkCmdLines,
    &postBuildCmdLines
  };

588
  cmNinjaDeps byproducts;
589
590
  for (unsigned i = 0; i != 3; ++i)
    {
591
592
    for (std::vector<cmCustomCommand>::const_iterator
         ci = cmdLists[i]->begin();
593
594
595
596
         ci != cmdLists[i]->end(); ++ci)
      {
      cmCustomCommandGenerator ccg(*ci, cfgName, mf);
      localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
597
598
599
      std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
      std::transform(ccByproducts.begin(), ccByproducts.end(),
                     std::back_inserter(byproducts), MapToNinjaPath());
600
      }
601
602
603
604
    }

  // If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for
  // the link commands.
605
606
607
608
609
  if (!preLinkCmdLines.empty())
    {
    const std::string homeOutDir = localGen.ConvertToOutputFormat(
                                              mf->GetHomeOutputDirectory(),
                                              cmLocalGenerator::SHELL);
610
    preLinkCmdLines.push_back("cd " + homeOutDir);
611
    }
612

613
614
  vars["PRE_LINK"] = localGen.BuildCommandLine(preLinkCmdLines);
  std::string postBuildCmdLine = localGen.BuildCommandLine(postBuildCmdLines);
615
616

  cmNinjaVars symlinkVars;
617
618
  if (targetOutput == targetOutputReal)
    {
619
    vars["POST_BUILD"] = postBuildCmdLine;
620
621
622
    }
  else
    {
623
624
    vars["POST_BUILD"] = ":";
    symlinkVars["POST_BUILD"] = postBuildCmdLine;
625
    }
626

627
  cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
628

629
630
  int commandLineLengthLimit = 1;
  const char* forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
631
  if (!mf->IsDefinitionSet(forceRspFile) &&
632
633
634
635
636
      cmSystemTools::GetEnv(forceRspFile) == 0)
    {
    commandLineLengthLimit = calculateCommandLineLengthLimit(
                globalGen.GetRuleCmdLength(this->LanguageLinkerRule()));
    }
637

638
639
640
  const std::string rspfile =
      std::string(cmake::GetCMakeFilesDirectoryPostSlash())
      + target.GetName() + ".rsp";
641

642
643
644
645
646
  // Gather order-only dependencies.
  cmNinjaDeps orderOnlyDeps;
  this->GetLocalGenerator()->AppendTargetDepends(this->GetTarget(),
    orderOnlyDeps);

647
648
649
650
651
652
653
654
655
656
657
  // Ninja should restat after linking if and only if there are byproducts.
  vars["RESTAT"] = byproducts.empty()? "" : "1";

  for (cmNinjaDeps::const_iterator oi = byproducts.begin(),
         oe = byproducts.end();
       oi != oe; ++oi)
    {
    this->GetGlobalGenerator()->SeenCustomCommandOutput(*oi);
    outputs.push_back(*oi);
    }

658
  // Write the build statement for this target.
659
  bool usedResponseFile = false;
660
661
662
663
664
665
  globalGen.WriteBuild(this->GetBuildFileStream(),
                        comment.str(),
                        this->LanguageLinkerRule(),
                        outputs,
                        explicitDeps,
                        implicitDeps,
666
                        orderOnlyDeps,
667
668
                        vars,
                        rspfile,
669
670
671
                        commandLineLengthLimit,
                        &usedResponseFile);
  this->WriteLinkRule(usedResponseFile);
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687

  if (targetOutput != targetOutputReal && !target.IsFrameworkOnApple())
    {
    if (targetType == cmTarget::EXECUTABLE)
      {
      globalGen.WriteBuild(this->GetBuildFileStream(),
                            "Create executable symlink " + targetOutput,
                            "CMAKE_SYMLINK_EXECUTABLE",
                            cmNinjaDeps(1, targetOutput),
                            cmNinjaDeps(1, targetOutputReal),
                            emptyDeps,
                            emptyDeps,
                            symlinkVars);
      }
   else
     {
688
      cmNinjaDeps symlinks;
689
690
      const std::string soName = this->GetTargetFilePath(this->TargetNameSO);
      // If one link has to be created.
691
692
      if (targetOutputReal == soName || targetOutput == soName)
        {
693
        symlinkVars["SONAME"] = soName;
694
695
696
        }
      else
        {
697
        symlinkVars["SONAME"] = "";
698
        symlinks.push_back(soName);
699
        }
700
      symlinks.push_back(targetOutput);
701
      globalGen.WriteBuild(this->GetBuildFileStream(),
702
703
704
705
706
707
708
                                  "Create library symlink " + targetOutput,
                                     "CMAKE_SYMLINK_LIBRARY",
                                  symlinks,
                                  cmNinjaDeps(1, targetOutputReal),
                                  emptyDeps,
                                  emptyDeps,
                                  symlinkVars);
709
      }
710
    }
711

712
  // Add aliases for the file name and the target name.
713
714
  globalGen.AddTargetAlias(this->TargetNameOut, &target);
  globalGen.AddTargetAlias(this->GetTargetName(), &target);
715
}
716
717
718
719
720
721
722
723

//----------------------------------------------------------------------------
void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
{
  // Write a phony output that depends on all object files.
  cmNinjaDeps outputs;
  this->GetLocalGenerator()->AppendTargetOutputs(this->GetTarget(), outputs);
  cmNinjaDeps depends = this->GetObjects();
724
725
726
727
728
  this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
                                              "Object library "
                                                + this->GetTargetName(),
                                              outputs,
                                              depends);
729
730
731
732
733

  // Add aliases for the target name.
  this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
                                             this->GetTarget());
}