cmTarget.cxx 198 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
#include "cmTarget.h"
13
#include "cmake.h"
14
#include "cmMakefile.h"
Ken Martin's avatar
Ken Martin committed
15
#include "cmSourceFile.h"
16
#include "cmLocalGenerator.h"
17
#include "cmGlobalGenerator.h"
18
#include "cmComputeLinkInformation.h"
19
#include "cmListFileCache.h"
20
#include "cmGeneratorExpression.h"
21
#include "cmGeneratorExpressionDAGChecker.h"
22
#include <cmsys/RegularExpression.hxx>
23
24
#include <map>
#include <set>
25
#include <stdlib.h> // required for atof
26
#include <assert.h>
27
#include <errno.h>
Nicolas Despres's avatar
Nicolas Despres committed
28
29
30
31
32
33
34
35
36
37
38

const char* cmTarget::GetTargetTypeName(TargetType targetType)
{
  switch( targetType )
    {
      case cmTarget::STATIC_LIBRARY:
        return "STATIC_LIBRARY";
      case cmTarget::MODULE_LIBRARY:
        return "MODULE_LIBRARY";
      case cmTarget::SHARED_LIBRARY:
        return "SHARED_LIBRARY";
Brad King's avatar
Brad King committed
39
40
      case cmTarget::OBJECT_LIBRARY:
        return "OBJECT_LIBRARY";
Nicolas Despres's avatar
Nicolas Despres committed
41
42
43
44
45
46
      case cmTarget::EXECUTABLE:
        return "EXECUTABLE";
      case cmTarget::UTILITY:
        return "UTILITY";
      case cmTarget::GLOBAL_TARGET:
        return "GLOBAL_TARGET";
47
48
      case cmTarget::INTERFACE_LIBRARY:
        return "INTERFACE_LIBRARY";
Nicolas Despres's avatar
Nicolas Despres committed
49
50
51
52
53
54
      case cmTarget::UNKNOWN_LIBRARY:
        return "UNKNOWN_LIBRARY";
    }
  assert(0 && "Unexpected target type");
  return 0;
}
55

56
57
58
59
60
//----------------------------------------------------------------------------
struct cmTarget::OutputInfo
{
  std::string OutDir;
  std::string ImpDir;
61
  std::string PdbDir;
62
63
64
65
66
67
68
69
70
71
72
73
};

//----------------------------------------------------------------------------
struct cmTarget::ImportInfo
{
  bool NoSOName;
  std::string Location;
  std::string SOName;
  std::string ImportLibrary;
  cmTarget::LinkInterface LinkInterface;
};

74
75
76
struct TargetConfigPair : public std::pair<cmTarget const* , std::string> {
  TargetConfigPair(cmTarget const* tgt, const std::string &config)
    : std::pair<cmTarget const* , std::string>(tgt, config) {}
77
78
};

79
80
81
82
83
84
//----------------------------------------------------------------------------
class cmTargetInternals
{
public:
  cmTargetInternals()
    {
85
    this->PolicyWarnedCMP0022 = false;
86
87
    this->SourceFileFlagsConstructed = false;
    }
88
  cmTargetInternals(cmTargetInternals const&)
89
    {
90
    this->PolicyWarnedCMP0022 = false;
91
    this->SourceFileFlagsConstructed = false;
92
    }
93
  ~cmTargetInternals();
94
  typedef cmTarget::SourceFileFlags SourceFileFlags;
95
96
  mutable std::map<cmSourceFile const*, SourceFileFlags> SourceFlagsMap;
  mutable bool SourceFileFlagsConstructed;
97
98
99

  // The backtrace when the target was created.
  cmListFileBacktrace Backtrace;
100
101
102
103
104
105
106

  // Cache link interface computation from each configuration.
  struct OptionalLinkInterface: public cmTarget::LinkInterface
  {
    OptionalLinkInterface(): Exists(false) {}
    bool Exists;
  };
107
108
  typedef std::map<TargetConfigPair, OptionalLinkInterface>
                                                          LinkInterfaceMapType;
109
  LinkInterfaceMapType LinkInterfaceMap;
110
  bool PolicyWarnedCMP0022;
111
112
113
114

  typedef std::map<cmStdString, cmTarget::OutputInfo> OutputInfoMapType;
  OutputInfoMapType OutputInfoMap;

115
116
  typedef std::map<TargetConfigPair, cmTarget::ImportInfo>
                                                            ImportInfoMapType;
117
  ImportInfoMapType ImportInfoMap;
118
119

  // Cache link implementation computation from each configuration.
120
121
  typedef std::map<TargetConfigPair,
                   cmTarget::LinkImplementation> LinkImplMapType;
122
  LinkImplMapType LinkImplMap;
123

124
125
  typedef std::map<TargetConfigPair, cmTarget::LinkClosure>
                                                          LinkClosureMapType;
126
  LinkClosureMapType LinkClosureMap;
127

128
129
  struct TargetPropertyEntry {
    TargetPropertyEntry(cmsys::auto_ptr<cmCompiledGeneratorExpression> cge,
130
131
      const std::string &targetName = std::string())
      : ge(cge), TargetName(targetName)
132
133
    {}
    const cmsys::auto_ptr<cmCompiledGeneratorExpression> ge;
134
    std::vector<std::string> CachedEntries;
135
    const std::string TargetName;
136
  };
137
  std::vector<TargetPropertyEntry*> IncludeDirectoriesEntries;
138
  std::vector<TargetPropertyEntry*> CompileOptionsEntries;
139
  std::vector<TargetPropertyEntry*> CompileDefinitionsEntries;
140
  std::vector<cmValueWithOrigin> LinkImplementationPropertyEntries;
141

142
  mutable std::map<std::string, std::vector<TargetPropertyEntry*> >
143
                                CachedLinkInterfaceIncludeDirectoriesEntries;
144
  mutable std::map<std::string, std::vector<TargetPropertyEntry*> >
145
                                CachedLinkInterfaceCompileOptionsEntries;
146
  mutable std::map<std::string, std::vector<TargetPropertyEntry*> >
147
                                CachedLinkInterfaceCompileDefinitionsEntries;
148

149
150
151
  mutable std::map<std::string, bool> CacheLinkInterfaceIncludeDirectoriesDone;
  mutable std::map<std::string, bool> CacheLinkInterfaceCompileDefinitionsDone;
  mutable std::map<std::string, bool> CacheLinkInterfaceCompileOptionsDone;
152
153
};

154
155
//----------------------------------------------------------------------------
void deleteAndClear(
156
      std::vector<cmTargetInternals::TargetPropertyEntry*> &entries)
157
{
158
  for (std::vector<cmTargetInternals::TargetPropertyEntry*>::const_iterator
159
160
161
162
163
164
165
166
167
      it = entries.begin(),
      end = entries.end();
      it != end; ++it)
    {
      delete *it;
    }
  entries.clear();
}

168
169
170
//----------------------------------------------------------------------------
void deleteAndClear(
  std::map<std::string,
171
          std::vector<cmTargetInternals::TargetPropertyEntry*> > &entries)
172
173
{
  for (std::map<std::string,
174
          std::vector<cmTargetInternals::TargetPropertyEntry*> >::iterator
175
176
177
178
179
180
        it = entries.begin(), end = entries.end(); it != end; ++it)
    {
    deleteAndClear(it->second);
    }
}

181
182
183
//----------------------------------------------------------------------------
cmTargetInternals::~cmTargetInternals()
{
Stephen Kelly's avatar
Stephen Kelly committed
184
  deleteAndClear(this->CachedLinkInterfaceIncludeDirectoriesEntries);
185
  deleteAndClear(this->CachedLinkInterfaceCompileOptionsEntries);
186
  deleteAndClear(this->CachedLinkInterfaceCompileDefinitionsEntries);
187
188
}

189
190
191
//----------------------------------------------------------------------------
cmTarget::cmTarget()
{
192
193
194
195
196
197
198
#define INITIALIZE_TARGET_POLICY_MEMBER(POLICY) \
  this->PolicyStatus ## POLICY = cmPolicies::WARN;

  CM_FOR_EACH_TARGET_POLICY(INITIALIZE_TARGET_POLICY_MEMBER)

#undef INITIALIZE_TARGET_POLICY_MEMBER

Ken Martin's avatar
Ken Martin committed
199
200
201
  this->Makefile = 0;
  this->LinkLibrariesAnalyzed = false;
  this->HaveInstallRule = false;
202
  this->DLLPlatform = false;
203
  this->IsApple = false;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
204
  this->IsImportedTarget = false;
205
  this->BuildInterfaceIncludesAppended = false;
206
  this->DebugIncludesDone = false;
207
  this->DebugCompileOptionsDone = false;
208
  this->DebugCompileDefinitionsDone = false;
209
210
}

211
//----------------------------------------------------------------------------
212
213
void cmTarget::DefineProperties(cmake *cm)
{
214
215
  cm->DefineProperty
    ("RULE_LAUNCH_COMPILE", cmProperty::TARGET,
216
     "", "", true);
217
218
  cm->DefineProperty
    ("RULE_LAUNCH_LINK", cmProperty::TARGET,
219
     "", "", true);
220
221
  cm->DefineProperty
    ("RULE_LAUNCH_CUSTOM", cmProperty::TARGET,
222
     "", "", true);
223
}
224

225
void cmTarget::SetType(TargetType type, const char* name)
226
{
Ken Martin's avatar
Ken Martin committed
227
  this->Name = name;
228
  // only add dependency information for library targets
Ken Martin's avatar
Ken Martin committed
229
  this->TargetTypeValue = type;
230
231
  if(this->TargetTypeValue >= STATIC_LIBRARY
     && this->TargetTypeValue <= MODULE_LIBRARY)
Ken Martin's avatar
Ken Martin committed
232
233
    {
    this->RecordDependencies = true;
234
235
    }
  else
Ken Martin's avatar
Ken Martin committed
236
237
238
    {
    this->RecordDependencies = false;
    }
239
240
}

241
242
243
244
//----------------------------------------------------------------------------
void cmTarget::SetMakefile(cmMakefile* mf)
{
  // Set our makefile.
Ken Martin's avatar
Ken Martin committed
245
  this->Makefile = mf;
246

247
248
249
  // set the cmake instance of the properties
  this->Properties.SetCMakeInstance(mf->GetCMakeInstance());

250
251
252
253
254
  // Check whether this is a DLL platform.
  this->DLLPlatform = (this->Makefile->IsOn("WIN32") ||
                       this->Makefile->IsOn("CYGWIN") ||
                       this->Makefile->IsOn("MINGW"));

255
256
257
  // Check whether we are targeting an Apple platform.
  this->IsApple = this->Makefile->IsOn("APPLE");

258
  // Setup default property values.
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
284
285
286
  if (this->GetType() != INTERFACE_LIBRARY)
    {
    this->SetPropertyDefault("INSTALL_NAME_DIR", 0);
    this->SetPropertyDefault("INSTALL_RPATH", "");
    this->SetPropertyDefault("INSTALL_RPATH_USE_LINK_PATH", "OFF");
    this->SetPropertyDefault("SKIP_BUILD_RPATH", "OFF");
    this->SetPropertyDefault("BUILD_WITH_INSTALL_RPATH", "OFF");
    this->SetPropertyDefault("ARCHIVE_OUTPUT_DIRECTORY", 0);
    this->SetPropertyDefault("LIBRARY_OUTPUT_DIRECTORY", 0);
    this->SetPropertyDefault("RUNTIME_OUTPUT_DIRECTORY", 0);
    this->SetPropertyDefault("PDB_OUTPUT_DIRECTORY", 0);
    this->SetPropertyDefault("Fortran_FORMAT", 0);
    this->SetPropertyDefault("Fortran_MODULE_DIRECTORY", 0);
    this->SetPropertyDefault("GNUtoMS", 0);
    this->SetPropertyDefault("OSX_ARCHITECTURES", 0);
    this->SetPropertyDefault("AUTOMOC", 0);
    this->SetPropertyDefault("AUTOUIC", 0);
    this->SetPropertyDefault("AUTORCC", 0);
    this->SetPropertyDefault("AUTOMOC_MOC_OPTIONS", 0);
    this->SetPropertyDefault("AUTOUIC_OPTIONS", 0);
    this->SetPropertyDefault("AUTORCC_OPTIONS", 0);
    this->SetPropertyDefault("LINK_DEPENDS_NO_SHARED", 0);
    this->SetPropertyDefault("LINK_INTERFACE_LIBRARIES", 0);
    this->SetPropertyDefault("WIN32_EXECUTABLE", 0);
    this->SetPropertyDefault("MACOSX_BUNDLE", 0);
    this->SetPropertyDefault("MACOSX_RPATH", 0);
    this->SetPropertyDefault("NO_SYSTEM_FROM_IMPORTED", 0);
    }
287
288
289

  // Collect the set of configuration types.
  std::vector<std::string> configNames;
290
  mf->GetConfigurations(configNames);
291
292

  // Setup per-configuration property default values.
293
294
295
296
  const char* configProps[] = {
    "ARCHIVE_OUTPUT_DIRECTORY_",
    "LIBRARY_OUTPUT_DIRECTORY_",
    "RUNTIME_OUTPUT_DIRECTORY_",
297
    "PDB_OUTPUT_DIRECTORY_",
298
    "MAP_IMPORTED_CONFIG_",
299
    0};
300
301
302
  for(std::vector<std::string>::iterator ci = configNames.begin();
      ci != configNames.end(); ++ci)
    {
303
304
305
    std::string configUpper = cmSystemTools::UpperCase(*ci);
    for(const char** p = configProps; *p; ++p)
      {
306
307
308
309
310
      if (this->TargetTypeValue == INTERFACE_LIBRARY
          && strcmp(*p, "MAP_IMPORTED_CONFIG_") != 0)
        {
        continue;
        }
311
312
313
314
315
      std::string property = *p;
      property += configUpper;
      this->SetPropertyDefault(property.c_str(), 0);
      }

316
317
318
319
320
321
    // Initialize per-configuration name postfix property from the
    // variable only for non-executable targets.  This preserves
    // compatibility with previous CMake versions in which executables
    // did not support this variable.  Projects may still specify the
    // property directly.  TODO: Make this depend on backwards
    // compatibility setting.
322
323
    if(this->TargetTypeValue != cmTarget::EXECUTABLE
        && this->TargetTypeValue != cmTarget::INTERFACE_LIBRARY)
324
325
326
327
328
329
      {
      std::string property = cmSystemTools::UpperCase(*ci);
      property += "_POSTFIX";
      this->SetPropertyDefault(property.c_str(), 0);
      }
    }
330
331
332

  // Save the backtrace of target construction.
  this->Makefile->GetBacktrace(this->Internal->Backtrace);
333

334
  if (!this->IsImported())
335
    {
336
337
338
339
    // Initialize the INCLUDE_DIRECTORIES property based on the current value
    // of the same directory property:
    const std::vector<cmValueWithOrigin> parentIncludes =
                                this->Makefile->GetIncludeDirectoriesEntries();
340

341
342
343
344
345
346
347
    for (std::vector<cmValueWithOrigin>::const_iterator it
                = parentIncludes.begin(); it != parentIncludes.end(); ++it)
      {
      this->InsertInclude(*it);
      }
    const std::set<cmStdString> parentSystemIncludes =
                                this->Makefile->GetSystemIncludeDirectories();
348

349
350
351
352
353
354
    for (std::set<cmStdString>::const_iterator it
          = parentSystemIncludes.begin();
          it != parentSystemIncludes.end(); ++it)
      {
      this->SystemIncludeDirectories.insert(*it);
      }
355

356
357
    const std::vector<cmValueWithOrigin> parentOptions =
                                this->Makefile->GetCompileOptionsEntries();
358

359
360
361
362
363
    for (std::vector<cmValueWithOrigin>::const_iterator it
                = parentOptions.begin(); it != parentOptions.end(); ++it)
      {
      this->InsertCompileOption(*it);
      }
364
    }
365

366
367
368
369
370
371
  if (this->GetType() != INTERFACE_LIBRARY)
    {
    this->SetPropertyDefault("C_VISIBILITY_PRESET", 0);
    this->SetPropertyDefault("CXX_VISIBILITY_PRESET", 0);
    this->SetPropertyDefault("VISIBILITY_INLINES_HIDDEN", 0);
    }
372

373
374
375
376
377
  if(this->TargetTypeValue == cmTarget::SHARED_LIBRARY
      || this->TargetTypeValue == cmTarget::MODULE_LIBRARY)
    {
    this->SetProperty("POSITION_INDEPENDENT_CODE", "True");
    }
378
379
380
381
  if (this->GetType() != INTERFACE_LIBRARY)
    {
    this->SetPropertyDefault("POSITION_INDEPENDENT_CODE", 0);
    }
382

383
  // Record current policies for later use.
384
385
386
387
388
389
390
#define CAPTURE_TARGET_POLICY(POLICY) \
  this->PolicyStatus ## POLICY = \
    this->Makefile->GetPolicyStatus(cmPolicies::POLICY);

  CM_FOR_EACH_TARGET_POLICY(CAPTURE_TARGET_POLICY)

#undef CAPTURE_TARGET_POLICY
391
392
393
394
395
396
397
398

  if (this->TargetTypeValue == INTERFACE_LIBRARY)
    {
    // This policy is checked in a few conditions. The properties relevant
    // to the policy are always ignored for INTERFACE_LIBRARY targets,
    // so ensure that the conditions don't lead to nonsense.
    this->PolicyStatusCMP0022 = cmPolicies::NEW;
    }
399
400
401

  this->SetPropertyDefault("JOB_POOL_COMPILE", 0);
  this->SetPropertyDefault("JOB_POOL_LINK", 0);
402
403
}

404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//----------------------------------------------------------------------------
void cmTarget::AddUtility(const char *u, cmMakefile *makefile)
{
  this->Utilities.insert(u);
  if(makefile)
  {
    makefile->GetBacktrace(UtilityBacktraces[u]);
  }
}

//----------------------------------------------------------------------------
cmListFileBacktrace const* cmTarget::GetUtilityBacktrace(const char *u) const
{
  std::map<cmStdString, cmListFileBacktrace>::const_iterator i =
    this->UtilityBacktraces.find(u);
  if(i == this->UtilityBacktraces.end()) return 0;

  return &i->second;
}

424
425
426
//----------------------------------------------------------------------------
void cmTarget::FinishConfigure()
{
427
428
429
430
431
432
  // Erase any cached link information that might have been comptued
  // on-demand during the configuration.  This ensures that build
  // system generation uses up-to-date information even if other cache
  // invalidation code in this source file is buggy.
  this->ClearLinkMaps();

433
434
435
436
  // Do old-style link dependency analysis.
  this->AnalyzeLibDependencies(*this->Makefile);
}

437
438
439
440
441
442
//----------------------------------------------------------------------------
void cmTarget::ClearLinkMaps()
{
  this->Internal->LinkImplMap.clear();
  this->Internal->LinkInterfaceMap.clear();
  this->Internal->LinkClosureMap.clear();
443
444
445
446
447
448
449
  for (cmTargetLinkInformationMap::const_iterator it
      = this->LinkInformation.begin();
      it != this->LinkInformation.end(); ++it)
    {
    delete it->second;
    }
  this->LinkInformation.clear();
450
451
}

452
453
454
455
//----------------------------------------------------------------------------
cmListFileBacktrace const& cmTarget::GetBacktrace() const
{
  return this->Internal->Backtrace;
456
}
457

458
459
460
461
462
463
464
//----------------------------------------------------------------------------
std::string cmTarget::GetSupportDirectory() const
{
  std::string dir = this->Makefile->GetCurrentOutputDirectory();
  dir += cmake::GetCMakeFilesDirectory();
  dir += "/";
  dir += this->Name;
465
466
467
#if defined(__VMS)
  dir += "_dir";
#else
468
  dir += ".dir";
469
#endif
470
471
472
  return dir;
}

473
//----------------------------------------------------------------------------
474
bool cmTarget::IsExecutableWithExports() const
475
{
476
477
478
479
  return (this->GetType() == cmTarget::EXECUTABLE &&
          this->GetPropertyAsBool("ENABLE_EXPORTS"));
}

480
//----------------------------------------------------------------------------
481
bool cmTarget::IsLinkable() const
482
483
484
485
486
{
  return (this->GetType() == cmTarget::STATIC_LIBRARY ||
          this->GetType() == cmTarget::SHARED_LIBRARY ||
          this->GetType() == cmTarget::MODULE_LIBRARY ||
          this->GetType() == cmTarget::UNKNOWN_LIBRARY ||
487
          this->GetType() == cmTarget::INTERFACE_LIBRARY ||
488
489
490
          this->IsExecutableWithExports());
}

Brad King's avatar
Brad King committed
491
//----------------------------------------------------------------------------
492
bool cmTarget::HasImportLibrary() const
Brad King's avatar
Brad King committed
493
494
495
496
497
498
{
  return (this->DLLPlatform &&
          (this->GetType() == cmTarget::SHARED_LIBRARY ||
           this->IsExecutableWithExports()));
}

499
//----------------------------------------------------------------------------
500
bool cmTarget::IsFrameworkOnApple() const
501
502
503
504
{
  return (this->GetType() == cmTarget::SHARED_LIBRARY &&
          this->Makefile->IsOn("APPLE") &&
          this->GetPropertyAsBool("FRAMEWORK"));
505
506
}

507
//----------------------------------------------------------------------------
508
bool cmTarget::IsAppBundleOnApple() const
509
510
511
512
513
514
{
  return (this->GetType() == cmTarget::EXECUTABLE &&
          this->Makefile->IsOn("APPLE") &&
          this->GetPropertyAsBool("MACOSX_BUNDLE"));
}

515
//----------------------------------------------------------------------------
516
bool cmTarget::IsCFBundleOnApple() const
517
518
519
520
521
522
{
  return (this->GetType() == cmTarget::MODULE_LIBRARY &&
          this->Makefile->IsOn("APPLE") &&
          this->GetPropertyAsBool("BUNDLE"));
}

523
//----------------------------------------------------------------------------
524
bool cmTarget::IsBundleOnApple() const
525
526
527
528
529
{
  return this->IsFrameworkOnApple() || this->IsAppBundleOnApple() ||
         this->IsCFBundleOnApple();
}

530
531
532
533
534
535
536
//----------------------------------------------------------------------------
bool cmTarget::FindSourceFiles()
{
  for(std::vector<cmSourceFile*>::const_iterator
        si = this->SourceFiles.begin();
      si != this->SourceFiles.end(); ++si)
    {
537
538
    std::string e;
    if((*si)->GetFullPath(&e).empty())
539
      {
540
541
542
543
544
545
      if(!e.empty())
        {
        cmake* cm = this->Makefile->GetCMakeInstance();
        cm->IssueMessage(cmake::FATAL_ERROR, e,
                         this->GetBacktrace());
        }
546
547
548
549
550
551
      return false;
      }
    }
  return true;
}

552
//----------------------------------------------------------------------------
553
void cmTarget::GetSourceFiles(std::vector<cmSourceFile*> &files) const
554
{
555
  files = this->SourceFiles;
556
557
558
559
560
}

//----------------------------------------------------------------------------
void cmTarget::AddSourceFile(cmSourceFile* sf)
{
561
562
  if (std::find(this->SourceFiles.begin(), this->SourceFiles.end(), sf)
                                            == this->SourceFiles.end())
563
564
565
566
567
    {
    this->SourceFiles.push_back(sf);
    }
}

568
569
570
571
572
//----------------------------------------------------------------------------
void cmTarget::AddSources(std::vector<std::string> const& srcs)
{
  for(std::vector<std::string>::const_iterator i = srcs.begin();
      i != srcs.end(); ++i)
573
    {
574
575
576
577
578
579
580
581
582
    const char* src = i->c_str();
    if(src[0] == '$' && src[1] == '<')
      {
      this->ProcessSourceExpression(*i);
      }
    else
      {
      this->AddSource(src);
      }
583
584
    }
}
585

586
//----------------------------------------------------------------------------
587
588
589
590
591
592
593
594
595
596
597
598
599
cmSourceFile* cmTarget::AddSource(const char* s)
{
  std::string src = s;

  // For backwards compatibility replace varibles in source names.
  // This should eventually be removed.
  this->Makefile->ExpandVariablesInString(src);

  cmSourceFile* sf = this->Makefile->GetOrCreateSource(src.c_str());
  this->AddSourceFile(sf);
  return sf;
}

600
601
602
//----------------------------------------------------------------------------
void cmTarget::ProcessSourceExpression(std::string const& expr)
{
603
  if(cmHasLiteralPrefix(expr.c_str(), "$<TARGET_OBJECTS:") &&
604
605
606
607
608
609
610
611
612
613
614
615
616
617
     expr[expr.size()-1] == '>')
    {
    std::string objLibName = expr.substr(17, expr.size()-18);
    this->ObjectLibraries.push_back(objLibName);
    }
  else
    {
    cmOStringStream e;
    e << "Unrecognized generator expression:\n"
      << "  " << expr;
    this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
    }
}

618
619
//----------------------------------------------------------------------------
struct cmTarget::SourceFileFlags
620
cmTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
621
622
{
  struct SourceFileFlags flags;
623
624
625
626
627
628
629
630
631
  this->ConstructSourceFileFlags();
  std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
    this->Internal->SourceFlagsMap.find(sf);
  if(si != this->Internal->SourceFlagsMap.end())
    {
    flags = si->second;
    }
  return flags;
}
632

633
//----------------------------------------------------------------------------
634
void cmTarget::ConstructSourceFileFlags() const
635
636
637
638
639
640
{
  if(this->Internal->SourceFileFlagsConstructed)
    {
    return;
    }
  this->Internal->SourceFileFlagsConstructed = true;
641

642
643
  // Process public headers to mark the source files.
  if(const char* files = this->GetProperty("PUBLIC_HEADER"))
644
645
646
    {
    std::vector<std::string> relFiles;
    cmSystemTools::ExpandListArgument(files, relFiles);
647
648
    for(std::vector<std::string>::iterator it = relFiles.begin();
        it != relFiles.end(); ++it)
649
      {
650
      if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
651
        {
652
653
654
        SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
        flags.MacFolder = "Headers";
        flags.Type = cmTarget::SourceFileTypePublicHeader;
655
656
657
658
        }
      }
    }

659
660
661
  // Process private headers after public headers so that they take
  // precedence if a file is listed in both.
  if(const char* files = this->GetProperty("PRIVATE_HEADER"))
662
    {
663
664
665
666
    std::vector<std::string> relFiles;
    cmSystemTools::ExpandListArgument(files, relFiles);
    for(std::vector<std::string>::iterator it = relFiles.begin();
        it != relFiles.end(); ++it)
667
      {
668
      if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
669
        {
670
671
672
        SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
        flags.MacFolder = "PrivateHeaders";
        flags.Type = cmTarget::SourceFileTypePrivateHeader;
673
674
675
676
        }
      }
    }

677
678
  // Mark sources listed as resources.
  if(const char* files = this->GetProperty("RESOURCE"))
679
    {
680
681
682
683
684
685
686
687
688
689
690
691
    std::vector<std::string> relFiles;
    cmSystemTools::ExpandListArgument(files, relFiles);
    for(std::vector<std::string>::iterator it = relFiles.begin();
        it != relFiles.end(); ++it)
      {
      if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
        {
        SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
        flags.MacFolder = "Resources";
        flags.Type = cmTarget::SourceFileTypeResource;
        }
      }
692
693
    }

694
695
  // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  // were not listed in one of the other lists.
696
697
  std::vector<cmSourceFile*> sources;
  this->GetSourceFiles(sources);
698
699
  for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
      si != sources.end(); ++si)
700
    {
701
702
    cmSourceFile* sf = *si;
    if(const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION"))
703
      {
704
705
      SourceFileFlags& flags = this->Internal->SourceFlagsMap[sf];
      if(flags.Type == cmTarget::SourceFileTypeNormal)
706
        {
707
708
        flags.MacFolder = location;
        if(strcmp(location, "Resources") == 0)
709
          {
710
711
712
713
714
          flags.Type = cmTarget::SourceFileTypeResource;
          }
        else
          {
          flags.Type = cmTarget::SourceFileTypeMacContent;
715
716
717
718
719
720
          }
        }
      }
    }
}

721
//----------------------------------------------------------------------------
722
723
void cmTarget::MergeLinkLibraries( cmMakefile& mf,
                                   const char *selfname,
Ken Martin's avatar
Ken Martin committed
724
                                   const LinkLibraryVectorType& libs )
725
{
726
727
  // Only add on libraries we haven't added on before.
  // Assumption: the global link libraries could only grow, never shrink
Ken Martin's avatar
Ken Martin committed
728
729
  LinkLibraryVectorType::const_iterator i = libs.begin();
  i += this->PrevLinkedLibraries.size();
730
  for( ; i != libs.end(); ++i )
731
    {
732
733
734
735
    // This is equivalent to the target_link_libraries plain signature.
    this->AddLinkLibrary( mf, selfname, i->first.c_str(), i->second );
    this->AppendProperty("INTERFACE_LINK_LIBRARIES",
      this->GetDebugGeneratorExpressions(i->first.c_str(), i->second).c_str());
736
    }
Ken Martin's avatar
Ken Martin committed
737
  this->PrevLinkedLibraries = libs;
738
739
}

740
//----------------------------------------------------------------------------
741
742
743
void cmTarget::AddLinkDirectory(const char* d)
{
  // Make sure we don't add unnecessary search directories.
744
  if(this->LinkDirectoriesEmmitted.insert(d).second)
745
    {
746
    this->LinkDirectories.push_back(d);
747
    }
748
749
}

750
//----------------------------------------------------------------------------
751
const std::vector<std::string>& cmTarget::GetLinkDirectories() const
752
{
Ken Martin's avatar
Ken Martin committed
753
  return this->LinkDirectories;
754
}
755

756
//----------------------------------------------------------------------------
757
cmTarget::LinkLibraryType cmTarget::ComputeLinkType(const char* config) const
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
{
  // No configuration is always optimized.
  if(!(config && *config))
    {
    return cmTarget::OPTIMIZED;
    }

  // Get the list of configurations considered to be DEBUG.
  std::vector<std::string> const& debugConfigs =
    this->Makefile->GetCMakeInstance()->GetDebugConfigs();

  // Check if any entry in the list matches this configuration.
  std::string configUpper = cmSystemTools::UpperCase(config);
  for(std::vector<std::string>::const_iterator i = debugConfigs.begin();
      i != debugConfigs.end(); ++i)
    {
    if(*i == configUpper)
      {
      return cmTarget::DEBUG;
      }
    }

  // The current configuration is not a debug configuration.
  return cmTarget::OPTIMIZED;
}

784
785
//----------------------------------------------------------------------------
void cmTarget::ClearDependencyInformation( cmMakefile& mf,
Ken Martin's avatar
Ken Martin committed
786
                                           const char* target )
787
788
789
790
791
{
  // Clear the dependencies. The cache variable must exist iff we are
  // recording dependency information for this target.
  std::string depname = target;
  depname += "_LIB_DEPENDS";
Ken Martin's avatar
Ken Martin committed
792
  if (this->RecordDependencies)
793
794
795
796
797
798
799
800
801
802
803
804
805
806
    {
    mf.AddCacheDefinition(depname.c_str(), "",
                          "Dependencies for target", cmCacheManager::STATIC);
    }
  else
    {
    if (mf.GetDefinition( depname.c_str() ))
      {
      std::string message = "Target ";
      message += target;
      message += " has dependency information when it shouldn't.\n";
      message += "Your cache is probably stale. Please remove the entry\n  ";
      message += depname;
      message += "\nfrom the cache.";
807
      cmSystemTools::Error( message.c_str() );
808
809
810
811
      }
    }
}

812
//----------------------------------------------------------------------------
813
bool cmTarget::NameResolvesToFramework(const std::string& libname) const
814
{
815
  return this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
816
817
818
    NameResolvesToFramework(libname);
}

819
820
//----------------------------------------------------------------------------
void cmTarget::GetDirectLinkLibraries(const char *config,
821
822
                            std::vector<std::string> &libs,
                            cmTarget const* head) const
823
824
825
826
827
828
{
  const char *prop = this->GetProperty("LINK_LIBRARIES");
  if (prop)
    {
    cmListFileBacktrace lfbt;
    cmGeneratorExpression ge(lfbt);
829
    const cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);
830
831
832
833

    cmGeneratorExpressionDAGChecker dagChecker(lfbt,
                                        this->GetName(),
                                        "LINK_LIBRARIES", 0, 0);
834
    cmSystemTools::ExpandListArgument(cge->Evaluate(this->Makefile,
835
836
837
838
839
                                        config,
                                        false,
                                        head,
                                        &dagChecker),
                                      libs);
840

841
842
843
844
845
846
847
848
849
    std::set<cmStdString> seenProps = cge->GetSeenTargetProperties();
    for (std::set<cmStdString>::const_iterator it = seenProps.begin();
        it != seenProps.end(); ++it)
      {
      if (!this->GetProperty(it->c_str()))
        {
        this->LinkImplicitNullProperties.insert(*it);
        }
      }
850
851
852
    }
}

853
854
//----------------------------------------------------------------------------
void cmTarget::GetInterfaceLinkLibraries(const char *config,
855
856
                                         std::vector<std::string> &libs,
                                         cmTarget const* head) const
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
{
  const char *prop = this->GetProperty("INTERFACE_LINK_LIBRARIES");
  if (prop)
    {
    cmListFileBacktrace lfbt;
    cmGeneratorExpression ge(lfbt);
    const cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(prop);

    cmGeneratorExpressionDAGChecker dagChecker(lfbt,
                                        this->GetName(),
                                        "INTERFACE_LINK_LIBRARIES", 0, 0);
    cmSystemTools::ExpandListArgument(cge->Evaluate(this->Makefile,
                                        config,
                                        false,
                                        head,
                                        &dagChecker),
                                      libs);
    }
}

877
878
//----------------------------------------------------------------------------
std::string cmTarget::GetDebugGeneratorExpressions(const std::string &value,
879
                                  cmTarget::LinkLibraryType llt) const
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
{
  if (llt == GENERAL)
    {
    return value;
    }

  // Get the list of configurations considered to be DEBUG.
  std::vector<std::string> const& debugConfigs =
                      this->Makefile->GetCMakeInstance()->GetDebugConfigs();

  std::string configString = "$<CONFIG:" + debugConfigs[0] + ">";

  if (debugConfigs.size() > 1)
    {
    for(std::vector<std::string>::const_iterator
          li = debugConfigs.begin() + 1; li != debugConfigs.end(); ++li)
      {
      configString += ",$<CONFIG:" + *li + ">";
      }
    configString = "$<OR:" + configString + ">";
    }

  if (llt == OPTIMIZED)
    {
    configString = "$<NOT:" + configString + ">";
    }
  return "$<" + configString + ":" + value + ">";
}

//----------------------------------------------------------------------------
static std::string targetNameGenex(const char *lib)
{
  return std::string("$<TARGET_NAME:") + lib + ">";
}

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
//----------------------------------------------------------------------------
bool cmTarget::PushTLLCommandTrace(TLLSignature signature)
{
  bool ret = true;
  if (!this->TLLCommands.empty())
    {
    if (this->TLLCommands.back().first != signature)
      {
      ret = false;
      }
    }
  cmListFileBacktrace lfbt;
  this->Makefile->GetBacktrace(lfbt);
  this->TLLCommands.push_back(std::make_pair(signature, lfbt));
  return ret;
}

//----------------------------------------------------------------------------
void cmTarget::GetTllSignatureTraces(cmOStringStream &s,
                                     TLLSignature sig) const
{
  std::vector<cmListFileBacktrace> sigs;
  typedef std::vector<std::pair<TLLSignature, cmListFileBacktrace> > Container;
  for(Container::const_iterator it = this->TLLCommands.begin();
      it != this->TLLCommands.end(); ++it)
    {
    if (it->first == sig)
      {
      sigs.push_back(it->second);
      }
    }
  if (!sigs.empty())
    {
    const char *sigString
                        = (sig == cmTarget::KeywordTLLSignature ? "keyword"
                                                                : "plain");
    s << "The uses of the " << sigString << " signature are here:\n";
952
    std::set<cmStdString> emitted;
953
954
955
956
957
958
959
    for(std::vector<cmListFileBacktrace>::const_iterator it = sigs.begin();
        it != sigs.end(); ++it)
      {
      cmListFileBacktrace::const_iterator i = it->begin();
      if(i != it->end())
        {
        cmListFileContext const& lfc = *i;
960
961
962
963
964
965
        cmOStringStream line;
        line << " * " << (lfc.Line? "": " in ") << lfc << std::endl;
        if (emitted.insert(line.str()).second)
          {
          s << line.str();
          }
966
967
968
969
970
971
        ++i;
        }
      }
    }
}

972
//----------------------------------------------------------------------------
973
void cmTarget::AddLinkLibrary(cmMakefile& mf,
974
                              const char *target, const char* lib,
975
976
                              LinkLibraryType llt)
{
977
  cmTarget *tgt = this->Makefile->FindTargetToUse(lib);
978
  {
979
980
  const bool isNonImportedTarget = tgt && !tgt->IsImported();

981
982
983
  const std::string libName = (isNonImportedTarget && llt != GENERAL)
                                                        ? targetNameGenex(lib)
                                                        : std::string(lib);
984
985
986
987
988
  this->AppendProperty("LINK_LIBRARIES",
                       this->GetDebugGeneratorExpressions(libName,
                                                          llt).c_str());
  }

989
  if (cmGeneratorExpression::Find(lib) != std::string::npos
990
991
      || (tgt && tgt->GetType() == INTERFACE_LIBRARY)
      || (strcmp( target, lib ) == 0))
992
993
994
995
    {
    return;
    }

996
  cmTarget::LibraryID tmp;
Ken Martin's avatar
Ken Martin committed
997
998
999
  tmp.first = lib;
  tmp.second = llt;
  this->LinkLibraries.push_back( tmp );
1000
  this->OriginalLinkLibraries.push_back(tmp);
For faster browsing, not all history is shown. View entire blame