cmMakefileLibraryTargetGenerator.cxx 27 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
4
5
#include "cmMakefileLibraryTargetGenerator.h"

#include "cmGeneratedFileStream.h"
6
#include "cmGeneratorTarget.h"
7
#include "cmGlobalUnixMakefileGenerator3.h"
8
#include "cmLocalGenerator.h"
9
10
#include "cmLocalUnixMakefileGenerator3.h"
#include "cmMakefile.h"
11
12
13
14
#include "cmOSXBundleGenerator.h"
#include "cmOutputConverter.h"
#include "cmState.h"
#include "cmSystemTools.h"
15
#include "cmake.h"
16

17
18
19
#include <sstream>
#include <vector>

20
21
22
cmMakefileLibraryTargetGenerator::cmMakefileLibraryTargetGenerator(
  cmGeneratorTarget* target)
  : cmMakefileTargetGenerator(target)
23
{
24
  this->CustomCommandDriver = OnDepends;
25
  if (this->GeneratorTarget->GetType() != cmState::INTERFACE_LIBRARY) {
26
    this->GeneratorTarget->GetLibraryNames(
27
28
      this->TargetNameOut, this->TargetNameSO, this->TargetNameReal,
      this->TargetNameImport, this->TargetNamePDB, this->ConfigName);
29
  }
30

31
32
  this->OSXBundleGenerator =
    new cmOSXBundleGenerator(target, this->ConfigName);
33
34
35
  this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
}

36
cmMakefileLibraryTargetGenerator::~cmMakefileLibraryTargetGenerator()
37
38
{
  delete this->OSXBundleGenerator;
39
40
}

41
42
43
44
45
void cmMakefileLibraryTargetGenerator::WriteRuleFiles()
{
  // create the build.make file and directory, put in the common blocks
  this->CreateRuleFile();

46
  // write rules used to help build object files
47
  this->WriteCommonCodeRules();
48
49
50
51

  // write the per-target per-language flags
  this->WriteTargetLanguageFlags();

52
53
54
  // write in rules for object files and custom commands
  this->WriteTargetBuildRules();

55
56
  // write the link rules
  // Write the rule for this target type.
57
  switch (this->GeneratorTarget->GetType()) {
58
    case cmState::STATIC_LIBRARY:
59
60
      this->WriteStaticLibraryRules();
      break;
61
    case cmState::SHARED_LIBRARY:
62
      this->WriteSharedLibraryRules(false);
63
      if (this->GeneratorTarget->NeedRelinkBeforeInstall(this->ConfigName)) {
64
65
        // Write rules to link an installable version of the target.
        this->WriteSharedLibraryRules(true);
66
      }
67
      break;
68
    case cmState::MODULE_LIBRARY:
69
      this->WriteModuleLibraryRules(false);
70
      if (this->GeneratorTarget->NeedRelinkBeforeInstall(this->ConfigName)) {
71
72
        // Write rules to link an installable version of the target.
        this->WriteModuleLibraryRules(true);
73
      }
74
      break;
75
    case cmState::OBJECT_LIBRARY:
76
77
      this->WriteObjectLibraryRules();
      break;
78
79
80
81
    default:
      // If language is not known, this is an error.
      cmSystemTools::Error("Unknown Library Type");
      break;
82
  }
83
84
85
86
87
88
89

  // Write the requires target.
  this->WriteTargetRequiresRules();

  // Write clean target
  this->WriteTargetCleanRules();

90
91
92
93
  // Write the dependency generation rule.  This must be done last so
  // that multiple output pair information is available.
  this->WriteTargetDependRules();

94
95
96
97
  // close the streams
  this->CloseFileStreams();
}

98
99
100
101
102
103
void cmMakefileLibraryTargetGenerator::WriteObjectLibraryRules()
{
  std::vector<std::string> commands;
  std::vector<std::string> depends;

  // Add post-build rules.
104
105
  this->LocalGenerator->AppendCustomCommands(
    commands, this->GeneratorTarget->GetPostBuildCommands(),
106
    this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
107
108
109
110
111

  // Depend on the object files.
  this->AppendObjectDepends(depends);

  // Write the rule.
Daniel Pfeifer's avatar
Daniel Pfeifer committed
112
  this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR,
113
                                      this->GeneratorTarget->GetName(),
114
115
116
                                      depends, commands, true);

  // Write the main driver rule to build everything in this target.
117
  this->WriteTargetDriverRule(this->GeneratorTarget->GetName(), false);
118
119
}

120
121
void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
{
122
  std::string linkLanguage =
123
    this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
124
  std::string linkRuleVar = "CMAKE_";
125
  linkRuleVar += linkLanguage;
126
127
  linkRuleVar += "_CREATE_STATIC_LIBRARY";

128
129
  if (this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION") &&
      this->Makefile->GetDefinition(linkRuleVar + "_IPO")) {
130
    linkRuleVar += "_IPO";
131
  }
132

133
  std::string extraFlags;
134
135
136
  this->LocalGenerator->GetStaticLibraryFlags(
    extraFlags, cmSystemTools::UpperCase(this->ConfigName),
    this->GeneratorTarget);
Stephen Kelly's avatar
Stephen Kelly committed
137
  this->WriteLibraryRules(linkRuleVar, extraFlags, false);
138
139
}

140
void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
141
{
142
  if (this->GeneratorTarget->IsFrameworkOnApple()) {
143
144
    this->WriteFrameworkRules(relink);
    return;
145
  }
146
  std::string linkLanguage =
147
    this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
148
  std::string linkRuleVar = "CMAKE_";
149
  linkRuleVar += linkLanguage;
150
151
152
  linkRuleVar += "_CREATE_SHARED_LIBRARY";

  std::string extraFlags;
153
154
  this->LocalGenerator->AppendFlags(
    extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
155
  std::string linkFlagsConfig = "LINK_FLAGS_";
156
  linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
157
158
  this->LocalGenerator->AppendFlags(
    extraFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
159

160
161
  this->LocalGenerator->AddConfigVariableFlags(
    extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->ConfigName);
162
163
  this->AddModuleDefinitionFlag(extraFlags);

164
  if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
165
166
    this->LocalGenerator->AppendFlags(extraFlags, " -Wl,--no-as-needed");
  }
Stephen Kelly's avatar
Stephen Kelly committed
167
  this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
168
169
}

170
void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
171
{
172
  std::string linkLanguage =
173
    this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
174
  std::string linkRuleVar = "CMAKE_";
175
  linkRuleVar += linkLanguage;
176
177
178
  linkRuleVar += "_CREATE_SHARED_MODULE";

  std::string extraFlags;
179
180
  this->LocalGenerator->AppendFlags(
    extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
Ken Martin's avatar
Ken Martin committed
181
  std::string linkFlagsConfig = "LINK_FLAGS_";
182
  linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
183
184
185
186
  this->LocalGenerator->AppendFlags(
    extraFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
  this->LocalGenerator->AddConfigVariableFlags(
    extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->ConfigName);
187
  this->AddModuleDefinitionFlag(extraFlags);
Ken Martin's avatar
Ken Martin committed
188

Stephen Kelly's avatar
Stephen Kelly committed
189
  this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
190
191
}

192
193
void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink)
{
194
  std::string linkLanguage =
195
    this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
196
  std::string linkRuleVar = "CMAKE_";
197
  linkRuleVar += linkLanguage;
198
199
200
  linkRuleVar += "_CREATE_MACOSX_FRAMEWORK";

  std::string extraFlags;
201
202
  this->LocalGenerator->AppendFlags(
    extraFlags, this->GeneratorTarget->GetProperty("LINK_FLAGS"));
203
  std::string linkFlagsConfig = "LINK_FLAGS_";
204
  linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
205
206
207
208
  this->LocalGenerator->AppendFlags(
    extraFlags, this->GeneratorTarget->GetProperty(linkFlagsConfig));
  this->LocalGenerator->AddConfigVariableFlags(
    extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->ConfigName);
209

Stephen Kelly's avatar
Stephen Kelly committed
210
  this->WriteLibraryRules(linkRuleVar, extraFlags, relink);
211
212
}

213
214
void cmMakefileLibraryTargetGenerator::WriteLibraryRules(
  const std::string& linkRuleVar, const std::string& extraFlags, bool relink)
215
216
217
218
219
220
221
{
  // TODO: Merge the methods that call this method to avoid
  // code duplication.
  std::vector<std::string> commands;

  // Build list of dependencies.
  std::vector<std::string> depends;
222
  this->AppendLinkDepends(depends);
223
224

  // Get the language to use for linking this library.
225
  std::string linkLanguage =
226
    this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
227
228

  // Make sure we have a link language.
229
  if (linkLanguage.empty()) {
230
    cmSystemTools::Error("Cannot determine link language for target \"",
231
                         this->GeneratorTarget->GetName().c_str(), "\".");
232
    return;
233
  }
234
235
236

  // Create set of linking flags.
  std::string linkFlags;
237
  this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
238

239
  // Add OSX version flags, if any.
240
241
  if (this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY ||
      this->GeneratorTarget->GetType() == cmState::MODULE_LIBRARY) {
242
243
    this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
    this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
244
  }
245

246
247
248
249
  // Construct the name of the library.
  std::string targetName;
  std::string targetNameSO;
  std::string targetNameReal;
250
  std::string targetNameImport;
251
  std::string targetNamePDB;
252
253
254
  this->GeneratorTarget->GetLibraryNames(targetName, targetNameSO,
                                         targetNameReal, targetNameImport,
                                         targetNamePDB, this->ConfigName);
255
256

  // Construct the full path version of the names.
257
  std::string outpath;
258
  std::string outpathImp;
259
  if (this->GeneratorTarget->IsFrameworkOnApple()) {
260
    outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
261
262
    this->OSXBundleGenerator->CreateFramework(targetName, outpath);
    outpath += "/";
263
  } else if (this->GeneratorTarget->IsCFBundleOnApple()) {
264
    outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
265
    this->OSXBundleGenerator->CreateCFBundle(targetName, outpath);
266
    outpath += "/";
267
  } else if (relink) {
268
    outpath = this->Makefile->GetCurrentBinaryDirectory();
269
270
    outpath += cmake::GetCMakeFilesDirectory();
    outpath += "/CMakeRelink.dir";
271
272
    cmSystemTools::MakeDirectory(outpath.c_str());
    outpath += "/";
273
    if (!targetNameImport.empty()) {
274
      outpathImp = outpath;
275
    }
276
  } else {
277
    outpath = this->GeneratorTarget->GetDirectory(this->ConfigName);
278
    cmSystemTools::MakeDirectory(outpath.c_str());
279
    outpath += "/";
280
    if (!targetNameImport.empty()) {
281
      outpathImp = this->GeneratorTarget->GetDirectory(this->ConfigName, true);
282
      cmSystemTools::MakeDirectory(outpathImp.c_str());
283
      outpathImp += "/";
284
    }
285
  }
286

287
  std::string compilePdbOutputPath =
288
    this->GeneratorTarget->GetCompilePDBDirectory(this->ConfigName);
289
290
  cmSystemTools::MakeDirectory(compilePdbOutputPath.c_str());

291
  std::string pdbOutputPath =
292
    this->GeneratorTarget->GetPDBDirectory(this->ConfigName);
293
294
295
  cmSystemTools::MakeDirectory(pdbOutputPath.c_str());
  pdbOutputPath += "/";

296
  std::string targetFullPath = outpath + targetName;
297
  std::string targetFullPathPDB = pdbOutputPath + targetNamePDB;
298
299
  std::string targetFullPathSO = outpath + targetNameSO;
  std::string targetFullPathReal = outpath + targetNameReal;
300
  std::string targetFullPathImport = outpathImp + targetNameImport;
301
302
303

  // Construct the output path version of the names for use in command
  // arguments.
304
305
  std::string targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
    targetFullPathPDB, cmOutputConverter::SHELL);
306
307
308
309
310
311
312
313
314
315
316
317
318

  std::string targetOutPath = this->LocalGenerator->ConvertToOutputFormat(
    this->LocalGenerator->ConvertToRelativePath(
      this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath),
    cmOutputConverter::SHELL);
  std::string targetOutPathSO = this->LocalGenerator->ConvertToOutputFormat(
    this->LocalGenerator->ConvertToRelativePath(
      this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathSO),
    cmOutputConverter::SHELL);
  std::string targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
    this->LocalGenerator->ConvertToRelativePath(
      this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
    cmOutputConverter::SHELL);
319
  std::string targetOutPathImport =
320
321
322
323
324
    this->LocalGenerator->ConvertToOutputFormat(
      this->LocalGenerator->ConvertToRelativePath(
        this->LocalGenerator->GetCurrentBinaryDirectory(),
        targetFullPathImport),
      cmOutputConverter::SHELL);
325

326
  this->NumberOfProgressActions++;
327
  if (!this->NoRuleMessages) {
328
329
    cmLocalUnixMakefileGenerator3::EchoProgress progress;
    this->MakeEchoProgress(progress);
330
331
332
    // Add the link message.
    std::string buildEcho = "Linking ";
    buildEcho += linkLanguage;
333
    switch (this->GeneratorTarget->GetType()) {
334
      case cmState::STATIC_LIBRARY:
335
336
        buildEcho += " static library ";
        break;
337
      case cmState::SHARED_LIBRARY:
338
339
        buildEcho += " shared library ";
        break;
340
      case cmState::MODULE_LIBRARY:
341
        if (this->GeneratorTarget->IsCFBundleOnApple()) {
342
          buildEcho += " CFBundle";
343
        }
344
345
346
347
348
        buildEcho += " shared module ";
        break;
      default:
        buildEcho += " library ";
        break;
349
    }
350
351
352
    buildEcho += targetOutPath;
    this->LocalGenerator->AppendEcho(
      commands, buildEcho, cmLocalUnixMakefileGenerator3::EchoLink, &progress);
353
  }
354

Daniel Pfeifer's avatar
Daniel Pfeifer committed
355
  const char* forbiddenFlagVar = CM_NULLPTR;
356
  switch (this->GeneratorTarget->GetType()) {
357
    case cmState::SHARED_LIBRARY:
Brad King's avatar
Brad King committed
358
359
      forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
      break;
360
    case cmState::MODULE_LIBRARY:
Brad King's avatar
Brad King committed
361
362
      forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
      break;
363
364
365
    default:
      break;
  }
Brad King's avatar
Brad King committed
366

367
  // Clean files associated with this library.
368
  std::vector<std::string> libCleanFiles;
369
  libCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
370
    this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPath));
371
  if (targetNameReal != targetName) {
372
    libCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
373
      this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal));
374
375
  }
  if (targetNameSO != targetName && targetNameSO != targetNameReal) {
376
    libCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
377
      this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathSO));
378
379
  }
  if (!targetNameImport.empty()) {
380
    libCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
381
382
      this->LocalGenerator->GetCurrentBinaryDirectory(),
      targetFullPathImport));
383
    std::string implib;
384
385
    if (this->GeneratorTarget->GetImplibGNUtoMS(targetFullPathImport,
                                                implib)) {
386
      libCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
387
        this->LocalGenerator->GetCurrentBinaryDirectory(), implib));
388
    }
389
  }
390
391
392
393

  // List the PDB for cleaning only when the whole target is
  // cleaned.  We do not want to delete the .pdb file just before
  // linking the target.
394
  this->CleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
395
    this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathPDB));
396
397
398
399

#ifdef _WIN32
  // There may be a manifest file for this target.  Add it to the
  // clean set just in case.
400
  if (this->GeneratorTarget->GetType() != cmState::STATIC_LIBRARY) {
401
    libCleanFiles.push_back(this->LocalGenerator->ConvertToRelativePath(
402
403
      this->LocalGenerator->GetCurrentBinaryDirectory(),
      (targetFullPath + ".manifest").c_str()));
404
  }
405
406
#endif

407
  std::vector<std::string> commands1;
408
  // Add a command to remove any existing files for this library.
409
  // for static libs only
410
  if (this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY) {
411
    this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
412
                                             this->GeneratorTarget, "target");
413
414
    this->LocalGenerator->CreateCDCommand(
      commands1, this->Makefile->GetCurrentBinaryDirectory(),
415
      this->LocalGenerator->GetBinaryDirectory());
416
417
    commands.insert(commands.end(), commands1.begin(), commands1.end());
    commands1.clear();
418
  }
419
420

  // Add the pre-build and pre-link rules building but not when relinking.
421
422
423
  if (!relink) {
    this->LocalGenerator->AppendCustomCommands(
      commands, this->GeneratorTarget->GetPreBuildCommands(),
424
      this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
425
426
    this->LocalGenerator->AppendCustomCommands(
      commands, this->GeneratorTarget->GetPreLinkCommands(),
427
      this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
428
  }
429

430
431
  // Determine whether a link script will be used.
  bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
432

433
434
435
436
  bool useResponseFileForObjects =
    this->CheckUseResponseFileForObjects(linkLanguage);
  bool const useResponseFileForLibs =
    this->CheckUseResponseFileForLibraries(linkLanguage);
437

438
  // For static libraries there might be archiving rules.
439
  bool haveStaticLibraryRule = false;
440
441
442
443
  std::vector<std::string> archiveCreateCommands;
  std::vector<std::string> archiveAppendCommands;
  std::vector<std::string> archiveFinishCommands;
  std::string::size_type archiveCommandLimit = std::string::npos;
444
  if (this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY) {
Daniel Pfeifer's avatar
Daniel Pfeifer committed
445
    haveStaticLibraryRule = this->Makefile->IsDefinitionSet(linkRuleVar);
446
447
448
    std::string arCreateVar = "CMAKE_";
    arCreateVar += linkLanguage;
    arCreateVar += "_ARCHIVE_CREATE";
449
    if (const char* rule = this->Makefile->GetDefinition(arCreateVar)) {
450
      cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
451
    }
452
453
454
    std::string arAppendVar = "CMAKE_";
    arAppendVar += linkLanguage;
    arAppendVar += "_ARCHIVE_APPEND";
455
    if (const char* rule = this->Makefile->GetDefinition(arAppendVar)) {
456
      cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
457
    }
458
459
460
    std::string arFinishVar = "CMAKE_";
    arFinishVar += linkLanguage;
    arFinishVar += "_ARCHIVE_FINISH";
461
    if (const char* rule = this->Makefile->GetDefinition(arFinishVar)) {
462
      cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
463
    }
464
  }
465
466

  // Decide whether to use archiving rules.
467
  bool useArchiveRules = !haveStaticLibraryRule &&
468
    !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
469
  if (useArchiveRules) {
470
471
    // Archiving rules are always run with a link script.
    useLinkScript = true;
472

473
    // Archiving rules never use a response file.
474
    useResponseFileForObjects = false;
475

476
477
478
479
    // Limit the length of individual object lists to less than the
    // 32K command line length limit on Windows.  We could make this a
    // platform file variable but this should work everywhere.
    archiveCommandLimit = 30000;
480
  }
481

482
  // Expand the rule variables.
483
  std::vector<std::string> real_link_commands;
484
  {
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
    bool useWatcomQuote =
      this->Makefile->IsOn(linkRuleVar + "_USE_WATCOM_QUOTE");

    // Set path conversion for link script shells.
    this->LocalGenerator->SetLinkScriptShell(useLinkScript);

    // Collect up flags to link in needed libraries.
    std::string linkLibs;
    if (this->GeneratorTarget->GetType() != cmState::STATIC_LIBRARY) {
      this->CreateLinkLibs(linkLibs, relink, useResponseFileForLibs, depends,
                           useWatcomQuote);
    }

    // Construct object file lists that may be needed to expand the
    // rule.
    std::string buildObjs;
    this->CreateObjectLists(useLinkScript, useArchiveRules,
                            useResponseFileForObjects, buildObjs, depends,
                            useWatcomQuote);

    // maybe create .def file from list of objects
    if (this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY &&
        this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS")) {
508
      this->GenDefFile(real_link_commands, linkFlags);
509
510
    }

511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
    std::string manifests = this->GetManifests();

    cmLocalGenerator::RuleVariables vars;
    vars.TargetPDB = targetOutPathPDB.c_str();

    // Setup the target version.
    std::string targetVersionMajor;
    std::string targetVersionMinor;
    {
      std::ostringstream majorStream;
      std::ostringstream minorStream;
      int major;
      int minor;
      this->GeneratorTarget->GetTargetVersion(major, minor);
      majorStream << major;
      minorStream << minor;
      targetVersionMajor = majorStream.str();
      targetVersionMinor = minorStream.str();
    }
    vars.TargetVersionMajor = targetVersionMajor.c_str();
    vars.TargetVersionMinor = targetVersionMinor.c_str();

    vars.RuleLauncher = "RULE_LAUNCH_LINK";
    vars.CMTarget = this->GeneratorTarget;
    vars.Language = linkLanguage.c_str();
    vars.Objects = buildObjs.c_str();
    std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
538
539
540
541
542
543

    objectDir = this->LocalGenerator->ConvertToOutputFormat(
      this->LocalGenerator->ConvertToRelativePath(
        this->LocalGenerator->GetCurrentBinaryDirectory(), objectDir),
      cmOutputConverter::SHELL);

544
    vars.ObjectDir = objectDir.c_str();
545
546
547
    cmOutputConverter::OutputFormat output = (useWatcomQuote)
      ? cmOutputConverter::WATCOMQUOTE
      : cmOutputConverter::SHELL;
548
549
550
551
    std::string target = this->LocalGenerator->ConvertToOutputFormat(
      this->LocalGenerator->ConvertToRelativePath(
        this->LocalGenerator->GetCurrentBinaryDirectory(), targetFullPathReal),
      output);
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
    vars.Target = target.c_str();
    vars.LinkLibraries = linkLibs.c_str();
    vars.ObjectsQuoted = buildObjs.c_str();
    if (this->GeneratorTarget->HasSOName(this->ConfigName)) {
      vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
      vars.TargetSOName = targetNameSO.c_str();
    }
    vars.LinkFlags = linkFlags.c_str();

    vars.Manifests = manifests.c_str();

    // Compute the directory portion of the install_name setting.
    std::string install_name_dir;
    if (this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY) {
      // Get the install_name directory for the build tree.
567
      install_name_dir =
568
569
570
571
572
573
574
        this->GeneratorTarget->GetInstallNameDirForBuildTree(this->ConfigName);

      // Set the rule variable replacement value.
      if (install_name_dir.empty()) {
        vars.TargetInstallNameDir = "";
      } else {
        // Convert to a path for the native build tool.
575
576
        install_name_dir = this->LocalGenerator->ConvertToOutputFormat(
          install_name_dir, cmOutputConverter::SHELL);
577
        vars.TargetInstallNameDir = install_name_dir.c_str();
578
579
      }
    }
580

581
582
583
    // Add language feature flags.
    std::string langFlags;
    this->AddFeatureFlags(langFlags, linkLanguage);
584

585
586
    this->LocalGenerator->AddArchitectureFlags(
      langFlags, this->GeneratorTarget, linkLanguage, this->ConfigName);
587

588
589
590
591
    // remove any language flags that might not work with the
    // particular os
    if (forbiddenFlagVar) {
      this->RemoveForbiddenFlags(forbiddenFlagVar, linkLanguage, langFlags);
592
    }
593
    vars.LanguageCompileFlags = langFlags.c_str();
594

595
596
597
598
599
600
    // Construct the main link rule and expand placeholders.
    this->LocalGenerator->TargetImplib = targetOutPathImport;
    if (useArchiveRules) {
      // Construct the individual object list strings.
      std::vector<std::string> object_strings;
      this->WriteObjectsStrings(object_strings, archiveCommandLimit);
601

602
603
      // Create the archive with the first set of objects.
      std::vector<std::string>::iterator osi = object_strings.begin();
604
      {
605
606
607
608
609
610
611
612
        vars.Objects = osi->c_str();
        for (std::vector<std::string>::const_iterator i =
               archiveCreateCommands.begin();
             i != archiveCreateCommands.end(); ++i) {
          std::string cmd = *i;
          this->LocalGenerator->ExpandRuleVariables(cmd, vars);
          real_link_commands.push_back(cmd);
        }
613
      }
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
      // Append to the archive with the other object sets.
      for (++osi; osi != object_strings.end(); ++osi) {
        vars.Objects = osi->c_str();
        for (std::vector<std::string>::const_iterator i =
               archiveAppendCommands.begin();
             i != archiveAppendCommands.end(); ++i) {
          std::string cmd = *i;
          this->LocalGenerator->ExpandRuleVariables(cmd, vars);
          real_link_commands.push_back(cmd);
        }
      }
      // Finish the archive.
      vars.Objects = "";
      for (std::vector<std::string>::const_iterator i =
             archiveFinishCommands.begin();
           i != archiveFinishCommands.end(); ++i) {
630
631
        std::string cmd = *i;
        this->LocalGenerator->ExpandRuleVariables(cmd, vars);
632
633
634
635
        // If there is no ranlib the command will be ":".  Skip it.
        if (!cmd.empty() && cmd[0] != ':') {
          real_link_commands.push_back(cmd);
        }
636
      }
637
638
639
640
    } else {
      // Get the set of commands.
      std::string linkRule = this->GetLinkRule(linkRuleVar);
      cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
641
      if (this->GeneratorTarget->GetPropertyAsBool("LINK_WHAT_YOU_USE") &&
642
          (this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY)) {
643
644
        std::string cmakeCommand = this->LocalGenerator->ConvertToOutputFormat(
          cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
645
646
647
648
        cmakeCommand += " -E __run_iwyu --lwyu=";
        cmakeCommand += targetOutPathReal;
        real_link_commands.push_back(cmakeCommand);
      }
649
650
651
652
653

      // Expand placeholders.
      for (std::vector<std::string>::iterator i = real_link_commands.begin();
           i != real_link_commands.end(); ++i) {
        this->LocalGenerator->ExpandRuleVariables(*i, vars);
654
      }
655
    }
656
    this->LocalGenerator->TargetImplib = "";
657

658
659
    // Restore path conversion to normal shells.
    this->LocalGenerator->SetLinkScriptShell(false);
660
  }
661

662
663
  // Optionally convert the build rule to use a script to avoid long
  // command lines in the make shell.
664
  if (useLinkScript) {
665
    // Use a link script.
666
    const char* name = (relink ? "relink.txt" : "link.txt");
667
    this->CreateLinkScript(name, real_link_commands, commands1, depends);
668
  } else {
669
670
    // No link script.  Just use the link rule directly.
    commands1 = real_link_commands;
671
672
673
  }
  this->LocalGenerator->CreateCDCommand(
    commands1, this->Makefile->GetCurrentBinaryDirectory(),
674
    this->LocalGenerator->GetBinaryDirectory());
675
676
677
678
  commands.insert(commands.end(), commands1.begin(), commands1.end());
  commands1.clear();

  // Add a rule to create necessary symlinks for the library.
679
  // Frameworks are handled by cmOSXBundleGenerator.
680
681
  if (targetOutPath != targetOutPathReal &&
      !this->GeneratorTarget->IsFrameworkOnApple()) {
682
683
684
685
686
687
688
    std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
    symlink += targetOutPathReal;
    symlink += " ";
    symlink += targetOutPathSO;
    symlink += " ";
    symlink += targetOutPath;
    commands1.push_back(symlink);
689
690
    this->LocalGenerator->CreateCDCommand(
      commands1, this->Makefile->GetCurrentBinaryDirectory(),
691
      this->LocalGenerator->GetBinaryDirectory());
692
693
    commands.insert(commands.end(), commands1.begin(), commands1.end());
    commands1.clear();
694
  }
695

696
  // Add the post-build rules when building but not when relinking.
697
698
699
  if (!relink) {
    this->LocalGenerator->AppendCustomCommands(
      commands, this->GeneratorTarget->GetPostBuildCommands(),
700
      this->GeneratorTarget, this->LocalGenerator->GetBinaryDirectory());
701
  }
702

703
704
  // Compute the list of outputs.
  std::vector<std::string> outputs(1, targetFullPathReal);
705
  if (targetNameSO != targetNameReal) {
706
    outputs.push_back(targetFullPathSO);
707
708
  }
  if (targetName != targetNameSO && targetName != targetNameReal) {
709
    outputs.push_back(targetFullPath);
710
  }
711

712
  // Write the build rule.
Daniel Pfeifer's avatar
Daniel Pfeifer committed
713
714
  this->WriteMakeRule(*this->BuildFileStream, CM_NULLPTR, outputs, depends,
                      commands, false);
715

716
  // Write the main driver rule to build everything in this target.
Stephen Kelly's avatar
Stephen Kelly committed
717
  this->WriteTargetDriverRule(targetFullPath, relink);
718

719
  // Clean all the possible library names and symlinks.
720
721
  this->CleanFiles.insert(this->CleanFiles.end(), libCleanFiles.begin(),
                          libCleanFiles.end());
722
}