cmMakefileLibraryTargetGenerator.cxx 27.3 KB
Newer Older
1
2
3
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4

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

8
9
10
11
  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.
============================================================================*/
12
13
14
#include "cmMakefileLibraryTargetGenerator.h"

#include "cmGeneratedFileStream.h"
15
#include "cmGlobalUnixMakefileGenerator3.h"
16
17
18
19
#include "cmLocalUnixMakefileGenerator3.h"
#include "cmMakefile.h"
#include "cmSourceFile.h"
#include "cmTarget.h"
20
#include "cmake.h"
21

22
//----------------------------------------------------------------------------
23
24
cmMakefileLibraryTargetGenerator
::cmMakefileLibraryTargetGenerator(cmTarget* target):
25
  cmMakefileTargetGenerator(target)
26
{
27
  cmOSXBundleGenerator::PrepareTargetProperties(this->Target);
28

29
  this->CustomCommandDriver = OnDepends;
30
31
  this->Target->GetLibraryNames(
    this->TargetNameOut, this->TargetNameSO, this->TargetNameReal,
32
    this->TargetNameImport, this->TargetNamePDB, this->ConfigName);
33

34
35
36
37
38
39
40
41
42
43
  this->OSXBundleGenerator = new cmOSXBundleGenerator(this->Target,
                                                      this->ConfigName);
  this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
}

//----------------------------------------------------------------------------
cmMakefileLibraryTargetGenerator
::~cmMakefileLibraryTargetGenerator()
{
  delete this->OSXBundleGenerator;
44
45
}

46
47
48
49
50
51
//----------------------------------------------------------------------------
void cmMakefileLibraryTargetGenerator::WriteRuleFiles()
{
  // create the build.make file and directory, put in the common blocks
  this->CreateRuleFile();

52
  // write rules used to help build object files
53
  this->WriteCommonCodeRules();
54
55
56
57

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

58
59
60
  // write in rules for object files and custom commands
  this->WriteTargetBuildRules();

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

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

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

99
100
101
102
  // Write the dependency generation rule.  This must be done last so
  // that multiple output pair information is available.
  this->WriteTargetDependRules();

103
104
105
106
  // close the streams
  this->CloseFileStreams();
}

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//----------------------------------------------------------------------------
void cmMakefileLibraryTargetGenerator::WriteObjectLibraryRules()
{
  std::vector<std::string> commands;
  std::vector<std::string> depends;

  // Add post-build rules.
  this->LocalGenerator->
    AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
                         this->Target);

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

  // Write the rule.
  this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
                                      this->Target->GetName(),
                                      depends, commands, true);

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

130
131
132
//----------------------------------------------------------------------------
void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
{
133
134
  const char* linkLanguage =
    this->Target->GetLinkerLanguage(this->ConfigName);
135
136
137
138
139
140
141
  std::string linkRuleVar = "CMAKE_";
  if (linkLanguage)
    {
    linkRuleVar += linkLanguage;
    }
  linkRuleVar += "_CREATE_STATIC_LIBRARY";

142
143
144
145
146
147
  if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION") &&
     this->Makefile->GetDefinition((linkRuleVar+"_IPO").c_str()))
    {
    linkRuleVar += "_IPO";
    }

148
  std::string extraFlags;
Ken Martin's avatar
Ken Martin committed
149
150
  this->LocalGenerator->AppendFlags
    (extraFlags,this->Target->GetProperty("STATIC_LIBRARY_FLAGS"));
151
152
153
154
  std::string staticLibraryFlagsConfig = "STATIC_LIBRARY_FLAGS_";
  staticLibraryFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  this->LocalGenerator->AppendFlags
    (extraFlags, this->Target->GetProperty(staticLibraryFlagsConfig.c_str()));
155
  this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), false);
156
157
158
}

//----------------------------------------------------------------------------
159
void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
160
{
161
  if(this->Target->IsFrameworkOnApple())
162
163
164
165
    {
    this->WriteFrameworkRules(relink);
    return;
    }
166
167
  const char* linkLanguage =
    this->Target->GetLinkerLanguage(this->ConfigName);
168
169
170
171
172
173
174
175
  std::string linkRuleVar = "CMAKE_";
  if (linkLanguage)
    {
    linkRuleVar += linkLanguage;
    }
  linkRuleVar += "_CREATE_SHARED_LIBRARY";

  std::string extraFlags;
Ken Martin's avatar
Ken Martin committed
176
177
  this->LocalGenerator->AppendFlags
    (extraFlags, this->Target->GetProperty("LINK_FLAGS"));
178
  std::string linkFlagsConfig = "LINK_FLAGS_";
179
  linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
Ken Martin's avatar
Ken Martin committed
180
181
  this->LocalGenerator->AppendFlags
    (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
182

Ken Martin's avatar
Ken Martin committed
183
  this->LocalGenerator->AddConfigVariableFlags
184
    (extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->ConfigName);
185
186
  this->AddModuleDefinitionFlag(extraFlags);

187
  this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
188
189
190
}

//----------------------------------------------------------------------------
191
void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
192
{
193
194
  const char* linkLanguage =
    this->Target->GetLinkerLanguage(this->ConfigName);
195
196
197
198
199
200
201
202
  std::string linkRuleVar = "CMAKE_";
  if (linkLanguage)
    {
    linkRuleVar += linkLanguage;
    }
  linkRuleVar += "_CREATE_SHARED_MODULE";

  std::string extraFlags;
203
  this->LocalGenerator->AppendFlags(extraFlags,
Ken Martin's avatar
Ken Martin committed
204
205
                                    this->Target->GetProperty("LINK_FLAGS"));
  std::string linkFlagsConfig = "LINK_FLAGS_";
206
  linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
Ken Martin's avatar
Ken Martin committed
207
208
209
  this->LocalGenerator->AppendFlags
    (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  this->LocalGenerator->AddConfigVariableFlags
210
    (extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->ConfigName);
211
  this->AddModuleDefinitionFlag(extraFlags);
Ken Martin's avatar
Ken Martin committed
212

213
  this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
214
215
}

216
217
218
//----------------------------------------------------------------------------
void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink)
{
219
220
  const char* linkLanguage =
    this->Target->GetLinkerLanguage(this->ConfigName);
221
222
223
224
225
226
227
228
  std::string linkRuleVar = "CMAKE_";
  if (linkLanguage)
    {
    linkRuleVar += linkLanguage;
    }
  linkRuleVar += "_CREATE_MACOSX_FRAMEWORK";

  std::string extraFlags;
229
  this->LocalGenerator->AppendFlags(extraFlags,
230
231
                                    this->Target->GetProperty("LINK_FLAGS"));
  std::string linkFlagsConfig = "LINK_FLAGS_";
232
  linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
233
234
235
  this->LocalGenerator->AppendFlags
    (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  this->LocalGenerator->AddConfigVariableFlags
236
    (extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->ConfigName);
237
238
239
240

  this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
}

241
242
//----------------------------------------------------------------------------
void cmMakefileLibraryTargetGenerator::WriteLibraryRules
243
(const char* linkRuleVar, const char* extraFlags, bool relink)
244
245
246
247
248
249
250
{
  // 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;
251
  this->AppendLinkDepends(depends);
252
253

  // Get the language to use for linking this library.
254
255
  const char* linkLanguage =
    this->Target->GetLinkerLanguage(this->ConfigName);
256
257
258
259
260
261
262
263
264
265
266
267
268

  // Make sure we have a link language.
  if(!linkLanguage)
    {
    cmSystemTools::Error("Cannot determine link language for target \"",
                         this->Target->GetName(), "\".");
    return;
    }

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

269
270
271
272
273
274
275
276
  // Add OSX version flags, if any.
  if(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
     this->Target->GetType() == cmTarget::MODULE_LIBRARY)
    {
    this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
    this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
    }

277
278
279
280
  // Construct the name of the library.
  std::string targetName;
  std::string targetNameSO;
  std::string targetNameReal;
281
  std::string targetNameImport;
282
  std::string targetNamePDB;
283
  this->Target->GetLibraryNames(
284
    targetName, targetNameSO, targetNameReal, targetNameImport, targetNamePDB,
285
    this->ConfigName);
286
287

  // Construct the full path version of the names.
288
  std::string outpath;
289
  std::string outpathImp;
290
291
  if(this->Target->IsFrameworkOnApple())
    {
292
293
294
    outpath = this->Target->GetDirectory(this->ConfigName);
    this->OSXBundleGenerator->CreateFramework(targetName, outpath);
    outpath += "/";
295
    }
296
297
298
  else if(this->Target->IsCFBundleOnApple())
    {
    outpath = this->Target->GetDirectory(this->ConfigName);
299
    this->OSXBundleGenerator->CreateCFBundle(targetName, outpath);
300
    outpath += "/";
301
    }
302
  else if(relink)
303
304
    {
    outpath = this->Makefile->GetStartOutputDirectory();
305
306
    outpath += cmake::GetCMakeFilesDirectory();
    outpath += "/CMakeRelink.dir";
307
308
    cmSystemTools::MakeDirectory(outpath.c_str());
    outpath += "/";
309
310
311
312
    if(!targetNameImport.empty())
      {
      outpathImp = outpath;
      }
313
    }
314
315
  else
    {
316
    outpath = this->Target->GetDirectory(this->ConfigName);
317
    cmSystemTools::MakeDirectory(outpath.c_str());
318
    outpath += "/";
319
320
    if(!targetNameImport.empty())
      {
321
      outpathImp = this->Target->GetDirectory(this->ConfigName, true);
322
      cmSystemTools::MakeDirectory(outpathImp.c_str());
323
324
      outpathImp += "/";
      }
325
    }
326

327
328
329
330
  std::string pdbOutputPath = this->Target->GetPDBDirectory();
  cmSystemTools::MakeDirectory(pdbOutputPath.c_str());
  pdbOutputPath += "/";

331
  std::string targetFullPath = outpath + targetName;
332
  std::string targetFullPathPDB = pdbOutputPath + targetNamePDB;
333
334
  std::string targetFullPathSO = outpath + targetNameSO;
  std::string targetFullPathReal = outpath + targetNameReal;
335
  std::string targetFullPathImport = outpathImp + targetNameImport;
336
337
338

  // Construct the output path version of the names for use in command
  // arguments.
339
  std::string targetOutPathPDB =
340
    this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::NONE,
341
                  cmLocalGenerator::SHELL);
342
  std::string targetOutPath =
343
    this->Convert(targetFullPath.c_str(),cmLocalGenerator::START_OUTPUT,
344
                  cmLocalGenerator::SHELL);
345
  std::string targetOutPathSO =
346
    this->Convert(targetFullPathSO.c_str(),cmLocalGenerator::START_OUTPUT,
347
                  cmLocalGenerator::SHELL);
348
  std::string targetOutPathReal =
349
    this->Convert(targetFullPathReal.c_str(),cmLocalGenerator::START_OUTPUT,
350
                  cmLocalGenerator::SHELL);
351
352
  std::string targetOutPathImport =
    this->Convert(targetFullPathImport.c_str(),cmLocalGenerator::START_OUTPUT,
353
                  cmLocalGenerator::SHELL);
354

355
  if(!this->NoRuleMessages)
356
    {
357
358
359
360
361
362
363
364
365
366
367
368
    // Add the link message.
    std::string buildEcho = "Linking ";
    buildEcho += linkLanguage;
    switch(this->Target->GetType())
      {
      case cmTarget::STATIC_LIBRARY:
        buildEcho += " static library ";
        break;
      case cmTarget::SHARED_LIBRARY:
        buildEcho += " shared library ";
        break;
      case cmTarget::MODULE_LIBRARY:
369
370
        if (this->Target->IsCFBundleOnApple())
            buildEcho += " CFBundle";
371
372
373
374
375
376
377
378
379
        buildEcho += " shared module ";
        break;
      default:
        buildEcho += " library ";
        break;
      }
    buildEcho += targetOutPath.c_str();
    this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
                                     cmLocalUnixMakefileGenerator3::EchoLink);
380
381
    }

Brad King's avatar
Brad King committed
382
383
384
385
386
387
388
389
390
391
392
393
  const char* forbiddenFlagVar = 0;
  switch(this->Target->GetType())
    {
    case cmTarget::SHARED_LIBRARY:
      forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
      break;
    case cmTarget::MODULE_LIBRARY:
      forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
      break;
    default: break;
    }

394
  // Clean files associated with this library.
395
  std::vector<std::string> libCleanFiles;
396
397
398
399
  libCleanFiles.push_back(this->Convert(targetFullPath.c_str(),
        cmLocalGenerator::START_OUTPUT,
        cmLocalGenerator::UNCHANGED));
  if(targetNameReal != targetName)
400
    {
401
402
403
    libCleanFiles.push_back(this->Convert(targetFullPathReal.c_str(),
        cmLocalGenerator::START_OUTPUT,
        cmLocalGenerator::UNCHANGED));
404
    }
405
406
  if(targetNameSO != targetName &&
     targetNameSO != targetNameReal)
407
    {
408
409
410
    libCleanFiles.push_back(this->Convert(targetFullPathSO.c_str(),
        cmLocalGenerator::START_OUTPUT,
        cmLocalGenerator::UNCHANGED));
411
    }
412
413
414
415
416
  if(!targetNameImport.empty())
    {
    libCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
        cmLocalGenerator::START_OUTPUT,
        cmLocalGenerator::UNCHANGED));
417
418
419
420
421
422
423
    std::string implib;
    if(this->Target->GetImplibGNUtoMS(targetFullPathImport, implib))
      {
      libCleanFiles.push_back(this->Convert(implib.c_str(),
                                            cmLocalGenerator::START_OUTPUT,
                                            cmLocalGenerator::UNCHANGED));
      }
424
425
426
427
428
429
430
431
432
    }

  // 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.
  this->CleanFiles.push_back
    (this->Convert(targetFullPathPDB.c_str(),
                   cmLocalGenerator::START_OUTPUT,
                   cmLocalGenerator::UNCHANGED));
433
434
435
436
437
438
439
440
441
442
443
444
445

#ifdef _WIN32
  // There may be a manifest file for this target.  Add it to the
  // clean set just in case.
  if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
    {
    libCleanFiles.push_back(
      this->Convert((targetFullPath+".manifest").c_str(),
                    cmLocalGenerator::START_OUTPUT,
                    cmLocalGenerator::UNCHANGED));
    }
#endif

446
  std::vector<std::string> commands1;
447
  // Add a command to remove any existing files for this library.
448
  // for static libs only
449
450
451
452
453
454
455
  if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
    {
    this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
                                             *this->Target, "target");
    this->LocalGenerator->CreateCDCommand
      (commands1,
       this->Makefile->GetStartOutputDirectory(),
456
       cmLocalGenerator::HOME_OUTPUT);
457
458
459
    commands.insert(commands.end(), commands1.begin(), commands1.end());
    commands1.clear();
    }
460
461
462
463
464

  // Add the pre-build and pre-link rules building but not when relinking.
  if(!relink)
    {
    this->LocalGenerator
465
466
      ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands(),
                             this->Target);
467
    this->LocalGenerator
468
469
      ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands(),
                             this->Target);
470
    }
471

472
473
  // Determine whether a link script will be used.
  bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
474

475
476
477
478
479
480
481
482
483
484
485
486
  // Select whether to use a response file for objects.
  bool useResponseFile = false;
  {
  std::string responseVar = "CMAKE_";
  responseVar += linkLanguage;
  responseVar += "_USE_RESPONSE_FILE_FOR_OBJECTS";
  if(this->Makefile->IsOn(responseVar.c_str()))
    {
    useResponseFile = true;
    }
  }

487
  // For static libraries there might be archiving rules.
488
  bool haveStaticLibraryRule = false;
489
490
491
492
  std::vector<std::string> archiveCreateCommands;
  std::vector<std::string> archiveAppendCommands;
  std::vector<std::string> archiveFinishCommands;
  std::string::size_type archiveCommandLimit = std::string::npos;
493
  if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
494
    {
495
496
    haveStaticLibraryRule =
      this->Makefile->GetDefinition(linkRuleVar)? true:false;
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
    std::string arCreateVar = "CMAKE_";
    arCreateVar += linkLanguage;
    arCreateVar += "_ARCHIVE_CREATE";
    if(const char* rule = this->Makefile->GetDefinition(arCreateVar.c_str()))
      {
      cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
      }
    std::string arAppendVar = "CMAKE_";
    arAppendVar += linkLanguage;
    arAppendVar += "_ARCHIVE_APPEND";
    if(const char* rule = this->Makefile->GetDefinition(arAppendVar.c_str()))
      {
      cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
      }
    std::string arFinishVar = "CMAKE_";
    arFinishVar += linkLanguage;
    arFinishVar += "_ARCHIVE_FINISH";
    if(const char* rule = this->Makefile->GetDefinition(arFinishVar.c_str()))
      {
      cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
      }
518
519
520
521
    }

  // Decide whether to use archiving rules.
  bool useArchiveRules =
522
    !haveStaticLibraryRule &&
523
524
525
526
527
    !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  if(useArchiveRules)
    {
    // Archiving rules are always run with a link script.
    useLinkScript = true;
528

529
530
531
    // Archiving rules never use a response file.
    useResponseFile = false;

532
533
534
535
536
    // 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;
    }
537

538
  // Expand the rule variables.
539
  std::vector<std::string> real_link_commands;
540
  {
541
542
543
  // Set path conversion for link script shells.
  this->LocalGenerator->SetLinkScriptShell(useLinkScript);

544
  // Collect up flags to link in needed libraries.
545
  std::string linkLibs;
546
547
  if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
    {
548
549
    std::string frameworkPath;
    std::string linkPath;
550
    this->LocalGenerator
551
552
553
      ->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
                            *this->GeneratorTarget, relink);
    linkLibs = frameworkPath + linkPath + linkLibs;
554
    }
555
556
557

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

562
  cmLocalGenerator::RuleVariables vars;
563
  vars.TargetPDB = targetOutPathPDB.c_str();
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581

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

582
583
  vars.RuleLauncher = "RULE_LAUNCH_LINK";
  vars.CMTarget = this->Target;
584
585
  vars.Language = linkLanguage;
  vars.Objects = buildObjs.c_str();
586
  std::string objdir = cmake::GetCMakeFilesDirectoryPostSlash();
587
588
  objdir += this->Target->GetName();
  objdir += ".dir";
589
590
591
592
  objdir = this->Convert(objdir.c_str(),
                         cmLocalGenerator::START_OUTPUT,
                         cmLocalGenerator::SHELL);
  vars.ObjectDir = objdir.c_str();
593
  vars.Target = targetOutPathReal.c_str();
594
  vars.LinkLibraries = linkLibs.c_str();
595
  vars.ObjectsQuoted = buildObjs.c_str();
596
597
598
599
600
  if (this->Target->HasSOName(this->ConfigName))
    {
    vars.SONameFlag = this->Makefile->GetSONameFlag(linkLanguage);
    vars.TargetSOName= targetNameSO.c_str();
    }
601
  vars.LinkFlags = linkFlags.c_str();
602
603
604
605
606

  // Compute the directory portion of the install_name setting.
  std::string install_name_dir;
  if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
    {
607
    // Get the install_name directory for the build tree.
608
609
    install_name_dir =
      this->Target->GetInstallNameDirForBuildTree(this->ConfigName);
610
611
612
613
614
615
616
617
618
619
620

    // Set the rule variable replacement value.
    if(install_name_dir.empty())
      {
      vars.TargetInstallNameDir = "";
      }
    else
      {
      // Convert to a path for the native build tool.
      install_name_dir =
        this->LocalGenerator->Convert(install_name_dir.c_str(),
621
                                      cmLocalGenerator::NONE,
622
623
624
625
                                      cmLocalGenerator::SHELL, false);
      vars.TargetInstallNameDir = install_name_dir.c_str();
      }
    }
626
627

  // Add language feature flags.
628
  std::string langFlags;
629
630
  this->AddFeatureFlags(langFlags, linkLanguage);

631
  this->LocalGenerator->AddArchitectureFlags(langFlags, this->GeneratorTarget,
Brad King's avatar
Brad King committed
632
                                             linkLanguage, this->ConfigName);
633

634
635
636
637
638
639
640
  // remove any language flags that might not work with the
  // particular os
  if(forbiddenFlagVar)
    {
    this->RemoveForbiddenFlags(forbiddenFlagVar,
                               linkLanguage, langFlags);
    }
641
  vars.LanguageCompileFlags = langFlags.c_str();
642
643

  // Construct the main link rule and expand placeholders.
Ken Martin's avatar
Ken Martin committed
644
  this->LocalGenerator->TargetImplib = targetOutPathImport;
645
646
647
648
649
650
651
652
  if(useArchiveRules)
    {
    // Construct the individual object list strings.
    std::vector<std::string> object_strings;
    this->WriteObjectsStrings(object_strings, archiveCommandLimit);

    // Create the archive with the first set of objects.
    std::vector<std::string>::iterator osi = object_strings.begin();
653
    {
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
    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);
      }
    }
    // 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)
      {
      std::string cmd = *i;
      this->LocalGenerator->ExpandRuleVariables(cmd, vars);
      real_link_commands.push_back(cmd);
      }
    }
  else
    {
    // Get the set of commands.
691
    std::string linkRule = this->GetLinkRule(linkRuleVar);
692
693
694
695
696
697
698
699
    cmSystemTools::ExpandListArgument(linkRule, real_link_commands);

    // Expand placeholders.
    for(std::vector<std::string>::iterator i = real_link_commands.begin();
        i != real_link_commands.end(); ++i)
      {
      this->LocalGenerator->ExpandRuleVariables(*i, vars);
      }
700
    }
Ken Martin's avatar
Ken Martin committed
701
  this->LocalGenerator->TargetImplib = "";
702
703
704

  // Restore path conversion to normal shells.
  this->LocalGenerator->SetLinkScriptShell(false);
705
  }
706

707
708
709
710
  // Optionally convert the build rule to use a script to avoid long
  // command lines in the make shell.
  if(useLinkScript)
    {
711
712
    // Use a link script.
    const char* name = (relink? "relink.txt" : "link.txt");
713
    this->CreateLinkScript(name, real_link_commands, commands1, depends);
714
715
716
717
718
719
720
721
722
    }
  else
    {
    // No link script.  Just use the link rule directly.
    commands1 = real_link_commands;
    }
  this->LocalGenerator->CreateCDCommand
    (commands1,
     this->Makefile->GetStartOutputDirectory(),
723
     cmLocalGenerator::HOME_OUTPUT);
724
725
726
727
  commands.insert(commands.end(), commands1.begin(), commands1.end());
  commands1.clear();

  // Add a rule to create necessary symlinks for the library.
728
729
  // Frameworks are handled by cmOSXBundleGenerator.
  if(targetOutPath != targetOutPathReal && !this->Target->IsFrameworkOnApple())
730
731
732
733
734
735
736
737
738
739
    {
    std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
    symlink += targetOutPathReal;
    symlink += " ";
    symlink += targetOutPathSO;
    symlink += " ";
    symlink += targetOutPath;
    commands1.push_back(symlink);
    this->LocalGenerator->CreateCDCommand(commands1,
                                  this->Makefile->GetStartOutputDirectory(),
740
                                  cmLocalGenerator::HOME_OUTPUT);
741
742
743
744
745
746
747
    commands.insert(commands.end(), commands1.begin(), commands1.end());
    commands1.clear();
    }
  // Add the post-build rules when building but not when relinking.
  if(!relink)
    {
    this->LocalGenerator->
748
749
      AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
                           this->Target);
750
751
    }

752
753
  // Write the build rule.
  this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
754
755
                                      targetFullPathReal.c_str(),
                                      depends, commands, false);
756

757
758
759
760
  // Some targets have more than one output file.  Create rules to
  // drive the build if any extra outputs are missing.
  std::vector<std::string> extraOutputs;
  if(targetNameSO != targetNameReal)
761
    {
762
763
    this->GenerateExtraOutput(targetFullPathSO.c_str(),
                              targetFullPathReal.c_str());
764
    }
765
766
  if(targetName != targetNameSO &&
     targetName != targetNameReal)
767
    {
768
769
    this->GenerateExtraOutput(targetFullPath.c_str(),
                              targetFullPathReal.c_str());
770
771
    }

772
773
  // Write the main driver rule to build everything in this target.
  this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
774

775
  // Clean all the possible library names and symlinks.
776
  this->CleanFiles.insert(this->CleanFiles.end(),
777
                          libCleanFiles.begin(),libCleanFiles.end());
778
}
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812

//----------------------------------------------------------------------------
void
cmMakefileLibraryTargetGenerator
::AppendOSXVerFlag(std::string& flags, const char* lang,
                   const char* name, bool so)
{
  // Lookup the flag to specify the version.
  std::string fvar = "CMAKE_";
  fvar += lang;
  fvar += "_OSX_";
  fvar += name;
  fvar += "_VERSION_FLAG";
  const char* flag = this->Makefile->GetDefinition(fvar.c_str());

  // Skip if no such flag.
  if(!flag)
    {
    return;
    }

  // Lookup the target version information.
  int major;
  int minor;
  int patch;
  this->Target->GetTargetVersion(so, major, minor, patch);
  if(major > 0 || minor > 0 || patch > 0)
    {
    // Append the flag since a non-zero version is specified.
    cmOStringStream vflag;
    vflag << flag << major << "." << minor << "." << patch;
    this->LocalGenerator->AppendFlags(flags, vflag.str().c_str());
    }
}