cmGeneratorTarget.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2012 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmGeneratorTarget.h"
  11. #include "cmTarget.h"
  12. #include "cmMakefile.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmSourceFile.h"
  16. #include "cmGeneratorExpression.h"
  17. #include "cmGeneratorExpressionDAGChecker.h"
  18. #include "cmComputeLinkInformation.h"
  19. #include <queue>
  20. #include "assert.h"
  21. //----------------------------------------------------------------------------
  22. cmGeneratorTarget::cmGeneratorTarget(cmTarget* t): Target(t),
  23. SourceFileFlagsConstructed(false)
  24. {
  25. this->Makefile = this->Target->GetMakefile();
  26. this->LocalGenerator = this->Makefile->GetLocalGenerator();
  27. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  28. }
  29. //----------------------------------------------------------------------------
  30. int cmGeneratorTarget::GetType() const
  31. {
  32. return this->Target->GetType();
  33. }
  34. //----------------------------------------------------------------------------
  35. const char *cmGeneratorTarget::GetName() const
  36. {
  37. return this->Target->GetName();
  38. }
  39. //----------------------------------------------------------------------------
  40. const char *cmGeneratorTarget::GetProperty(const char *prop) const
  41. {
  42. return this->Target->GetProperty(prop);
  43. }
  44. //----------------------------------------------------------------------------
  45. std::vector<cmSourceFile*> const*
  46. cmGeneratorTarget::GetSourceDepends(cmSourceFile* sf) const
  47. {
  48. SourceEntriesType::const_iterator i = this->SourceEntries.find(sf);
  49. if(i != this->SourceEntries.end())
  50. {
  51. return &i->second.Depends;
  52. }
  53. return 0;
  54. }
  55. static void handleSystemIncludesDep(cmMakefile *mf, cmTarget* depTgt,
  56. const char *config, cmTarget *headTarget,
  57. cmGeneratorExpressionDAGChecker *dagChecker,
  58. std::vector<std::string>& result,
  59. bool excludeImported)
  60. {
  61. cmListFileBacktrace lfbt;
  62. if (const char* dirs =
  63. depTgt->GetProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES"))
  64. {
  65. cmGeneratorExpression ge(lfbt);
  66. cmSystemTools::ExpandListArgument(ge.Parse(dirs)
  67. ->Evaluate(mf,
  68. config, false, headTarget,
  69. depTgt, dagChecker), result);
  70. }
  71. if (!depTgt->IsImported() || excludeImported)
  72. {
  73. return;
  74. }
  75. if (const char* dirs =
  76. depTgt->GetProperty("INTERFACE_INCLUDE_DIRECTORIES"))
  77. {
  78. cmGeneratorExpression ge(lfbt);
  79. cmSystemTools::ExpandListArgument(ge.Parse(dirs)
  80. ->Evaluate(mf,
  81. config, false, headTarget,
  82. depTgt, dagChecker), result);
  83. }
  84. }
  85. //----------------------------------------------------------------------------
  86. void
  87. cmGeneratorTarget::GetObjectSources(std::vector<cmSourceFile*> &objs) const
  88. {
  89. objs = this->ObjectSources;
  90. }
  91. //----------------------------------------------------------------------------
  92. const std::string& cmGeneratorTarget::GetObjectName(cmSourceFile const* file)
  93. {
  94. return this->Objects[file];
  95. }
  96. void cmGeneratorTarget::AddObject(cmSourceFile *sf, std::string const&name)
  97. {
  98. this->Objects[sf] = name;
  99. }
  100. //----------------------------------------------------------------------------
  101. void cmGeneratorTarget::AddExplicitObjectName(cmSourceFile* sf)
  102. {
  103. this->ExplicitObjectName.insert(sf);
  104. }
  105. //----------------------------------------------------------------------------
  106. bool cmGeneratorTarget::HasExplicitObjectName(cmSourceFile const* file) const
  107. {
  108. std::set<cmSourceFile const*>::const_iterator it
  109. = this->ExplicitObjectName.find(file);
  110. return it != this->ExplicitObjectName.end();
  111. }
  112. //----------------------------------------------------------------------------
  113. void cmGeneratorTarget::GetResxSources(std::vector<cmSourceFile*>& srcs) const
  114. {
  115. srcs = this->ResxSources;
  116. }
  117. //----------------------------------------------------------------------------
  118. void cmGeneratorTarget::GetIDLSources(std::vector<cmSourceFile*>& srcs) const
  119. {
  120. srcs = this->IDLSources;
  121. }
  122. //----------------------------------------------------------------------------
  123. void
  124. cmGeneratorTarget::GetHeaderSources(std::vector<cmSourceFile*>& srcs) const
  125. {
  126. srcs = this->HeaderSources;
  127. }
  128. //----------------------------------------------------------------------------
  129. void cmGeneratorTarget::GetExtraSources(std::vector<cmSourceFile*>& srcs) const
  130. {
  131. srcs = this->ExtraSources;
  132. }
  133. //----------------------------------------------------------------------------
  134. void
  135. cmGeneratorTarget::GetCustomCommands(std::vector<cmSourceFile*>& srcs) const
  136. {
  137. srcs = this->CustomCommands;
  138. }
  139. //----------------------------------------------------------------------------
  140. void
  141. cmGeneratorTarget::GetExpectedResxHeaders(std::set<std::string>& srcs) const
  142. {
  143. srcs = this->ExpectedResxHeaders;
  144. }
  145. //----------------------------------------------------------------------------
  146. void
  147. cmGeneratorTarget::GetExternalObjects(std::vector<cmSourceFile*>& srcs) const
  148. {
  149. srcs = this->ExternalObjects;
  150. }
  151. //----------------------------------------------------------------------------
  152. bool cmGeneratorTarget::IsSystemIncludeDirectory(const char *dir,
  153. const char *config) const
  154. {
  155. assert(this->GetType() != cmTarget::INTERFACE_LIBRARY);
  156. std::string config_upper;
  157. if(config && *config)
  158. {
  159. config_upper = cmSystemTools::UpperCase(config);
  160. }
  161. typedef std::map<std::string, std::vector<std::string> > IncludeCacheType;
  162. IncludeCacheType::const_iterator iter =
  163. this->SystemIncludesCache.find(config_upper);
  164. if (iter == this->SystemIncludesCache.end())
  165. {
  166. cmTarget::LinkImplementation const* impl
  167. = this->Target->GetLinkImplementation(config, this->Target);
  168. if(!impl)
  169. {
  170. return false;
  171. }
  172. cmListFileBacktrace lfbt;
  173. cmGeneratorExpressionDAGChecker dagChecker(lfbt,
  174. this->GetName(),
  175. "SYSTEM_INCLUDE_DIRECTORIES", 0, 0);
  176. bool excludeImported
  177. = this->Target->GetPropertyAsBool("NO_SYSTEM_FROM_IMPORTED");
  178. std::vector<std::string> result;
  179. for (std::set<cmStdString>::const_iterator
  180. it = this->Target->GetSystemIncludeDirectories().begin();
  181. it != this->Target->GetSystemIncludeDirectories().end(); ++it)
  182. {
  183. cmGeneratorExpression ge(lfbt);
  184. cmSystemTools::ExpandListArgument(ge.Parse(*it)
  185. ->Evaluate(this->Makefile,
  186. config, false, this->Target,
  187. &dagChecker), result);
  188. }
  189. std::set<cmTarget*> uniqueDeps;
  190. for(std::vector<std::string>::const_iterator li = impl->Libraries.begin();
  191. li != impl->Libraries.end(); ++li)
  192. {
  193. cmTarget* tgt = this->Makefile->FindTargetToUse(*li);
  194. if (!tgt)
  195. {
  196. continue;
  197. }
  198. if (uniqueDeps.insert(tgt).second)
  199. {
  200. handleSystemIncludesDep(this->Makefile, tgt, config, this->Target,
  201. &dagChecker, result, excludeImported);
  202. std::vector<cmTarget*> deps;
  203. tgt->GetTransitivePropertyTargets(config, this->Target, deps);
  204. for(std::vector<cmTarget*>::const_iterator di = deps.begin();
  205. di != deps.end(); ++di)
  206. {
  207. if (uniqueDeps.insert(*di).second)
  208. {
  209. handleSystemIncludesDep(this->Makefile, *di, config, this->Target,
  210. &dagChecker, result, excludeImported);
  211. }
  212. }
  213. }
  214. }
  215. std::set<cmStdString> unique;
  216. for(std::vector<std::string>::iterator li = result.begin();
  217. li != result.end(); ++li)
  218. {
  219. cmSystemTools::ConvertToUnixSlashes(*li);
  220. unique.insert(*li);
  221. }
  222. result.clear();
  223. for(std::set<cmStdString>::iterator li = unique.begin();
  224. li != unique.end(); ++li)
  225. {
  226. result.push_back(*li);
  227. }
  228. IncludeCacheType::value_type entry(config_upper, result);
  229. iter = this->SystemIncludesCache.insert(entry).first;
  230. }
  231. std::string dirString = dir;
  232. return std::binary_search(iter->second.begin(), iter->second.end(),
  233. dirString);
  234. }
  235. //----------------------------------------------------------------------------
  236. bool cmGeneratorTarget::GetPropertyAsBool(const char *prop) const
  237. {
  238. return this->Target->GetPropertyAsBool(prop);
  239. }
  240. //----------------------------------------------------------------------------
  241. void cmGeneratorTarget::GetSourceFiles(std::vector<cmSourceFile*> &files) const
  242. {
  243. this->Target->GetSourceFiles(files);
  244. }
  245. //----------------------------------------------------------------------------
  246. void cmGeneratorTarget::ClassifySources()
  247. {
  248. cmsys::RegularExpression header(CM_HEADER_REGEX);
  249. cmTarget::TargetType targetType = this->Target->GetType();
  250. bool isObjLib = targetType == cmTarget::OBJECT_LIBRARY;
  251. std::vector<cmSourceFile*> badObjLib;
  252. std::vector<cmSourceFile*> sources;
  253. this->Target->GetSourceFiles(sources);
  254. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  255. si != sources.end(); ++si)
  256. {
  257. cmSourceFile* sf = *si;
  258. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  259. if(sf->GetCustomCommand())
  260. {
  261. this->CustomCommands.push_back(sf);
  262. }
  263. else if(targetType == cmTarget::UTILITY)
  264. {
  265. this->ExtraSources.push_back(sf);
  266. }
  267. else if(sf->GetPropertyAsBool("HEADER_FILE_ONLY"))
  268. {
  269. this->HeaderSources.push_back(sf);
  270. }
  271. else if(sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
  272. {
  273. this->ExternalObjects.push_back(sf);
  274. if(isObjLib) { badObjLib.push_back(sf); }
  275. }
  276. else if(sf->GetLanguage())
  277. {
  278. this->ObjectSources.push_back(sf);
  279. }
  280. else if(ext == "def")
  281. {
  282. this->ModuleDefinitionFile = sf->GetFullPath();
  283. if(isObjLib) { badObjLib.push_back(sf); }
  284. }
  285. else if(ext == "idl")
  286. {
  287. this->IDLSources.push_back(sf);
  288. if(isObjLib) { badObjLib.push_back(sf); }
  289. }
  290. else if(ext == "resx")
  291. {
  292. // Build and save the name of the corresponding .h file
  293. // This relationship will be used later when building the project files.
  294. // Both names would have been auto generated from Visual Studio
  295. // where the user supplied the file name and Visual Studio
  296. // appended the suffix.
  297. std::string resx = sf->GetFullPath();
  298. std::string hFileName = resx.substr(0, resx.find_last_of(".")) + ".h";
  299. this->ExpectedResxHeaders.insert(hFileName);
  300. this->ResxSources.push_back(sf);
  301. }
  302. else if(header.find(sf->GetFullPath().c_str()))
  303. {
  304. this->HeaderSources.push_back(sf);
  305. }
  306. else if(this->GlobalGenerator->IgnoreFile(sf->GetExtension().c_str()))
  307. {
  308. // We only get here if a source file is not an external object
  309. // and has an extension that is listed as an ignored file type.
  310. // No message or diagnosis should be given.
  311. this->ExtraSources.push_back(sf);
  312. }
  313. else
  314. {
  315. this->ExtraSources.push_back(sf);
  316. if(isObjLib && ext != "txt")
  317. {
  318. badObjLib.push_back(sf);
  319. }
  320. }
  321. }
  322. if(!badObjLib.empty())
  323. {
  324. cmOStringStream e;
  325. e << "OBJECT library \"" << this->Target->GetName() << "\" contains:\n";
  326. for(std::vector<cmSourceFile*>::iterator i = badObjLib.begin();
  327. i != badObjLib.end(); ++i)
  328. {
  329. e << " " << (*i)->GetLocation().GetName() << "\n";
  330. }
  331. e << "but may contain only headers and sources that compile.";
  332. this->GlobalGenerator->GetCMakeInstance()
  333. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  334. this->Target->GetBacktrace());
  335. }
  336. }
  337. //----------------------------------------------------------------------------
  338. void cmGeneratorTarget::LookupObjectLibraries()
  339. {
  340. std::vector<std::string> const& objLibs =
  341. this->Target->GetObjectLibraries();
  342. for(std::vector<std::string>::const_iterator oli = objLibs.begin();
  343. oli != objLibs.end(); ++oli)
  344. {
  345. std::string const& objLibName = *oli;
  346. if(cmTarget* objLib = this->Makefile->FindTargetToUse(objLibName))
  347. {
  348. if(objLib->GetType() == cmTarget::OBJECT_LIBRARY)
  349. {
  350. if(this->Target->GetType() != cmTarget::EXECUTABLE &&
  351. this->Target->GetType() != cmTarget::STATIC_LIBRARY &&
  352. this->Target->GetType() != cmTarget::SHARED_LIBRARY &&
  353. this->Target->GetType() != cmTarget::MODULE_LIBRARY)
  354. {
  355. this->GlobalGenerator->GetCMakeInstance()
  356. ->IssueMessage(cmake::FATAL_ERROR,
  357. "Only executables and non-OBJECT libraries may "
  358. "reference target objects.",
  359. this->Target->GetBacktrace());
  360. return;
  361. }
  362. this->Target->AddUtility(objLib->GetName());
  363. this->ObjectLibraries.push_back(objLib);
  364. }
  365. else
  366. {
  367. cmOStringStream e;
  368. e << "Objects of target \"" << objLibName
  369. << "\" referenced but is not an OBJECT library.";
  370. this->GlobalGenerator->GetCMakeInstance()
  371. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  372. this->Target->GetBacktrace());
  373. return;
  374. }
  375. }
  376. else
  377. {
  378. cmOStringStream e;
  379. e << "Objects of target \"" << objLibName
  380. << "\" referenced but no such target exists.";
  381. this->GlobalGenerator->GetCMakeInstance()
  382. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  383. this->Target->GetBacktrace());
  384. return;
  385. }
  386. }
  387. }
  388. //----------------------------------------------------------------------------
  389. void
  390. cmGeneratorTarget::UseObjectLibraries(std::vector<std::string>& objs) const
  391. {
  392. for(std::vector<cmTarget*>::const_iterator
  393. ti = this->ObjectLibraries.begin();
  394. ti != this->ObjectLibraries.end(); ++ti)
  395. {
  396. cmTarget* objLib = *ti;
  397. cmGeneratorTarget* ogt =
  398. this->GlobalGenerator->GetGeneratorTarget(objLib);
  399. for(std::vector<cmSourceFile*>::const_iterator
  400. si = ogt->ObjectSources.begin();
  401. si != ogt->ObjectSources.end(); ++si)
  402. {
  403. std::string obj = ogt->ObjectDirectory;
  404. obj += ogt->Objects[*si];
  405. objs.push_back(obj);
  406. }
  407. }
  408. }
  409. //----------------------------------------------------------------------------
  410. class cmTargetTraceDependencies
  411. {
  412. public:
  413. cmTargetTraceDependencies(cmGeneratorTarget* target);
  414. void Trace();
  415. private:
  416. cmTarget* Target;
  417. cmGeneratorTarget* GeneratorTarget;
  418. cmMakefile* Makefile;
  419. cmGlobalGenerator const* GlobalGenerator;
  420. typedef cmGeneratorTarget::SourceEntry SourceEntry;
  421. SourceEntry* CurrentEntry;
  422. std::queue<cmSourceFile*> SourceQueue;
  423. std::set<cmSourceFile*> SourcesQueued;
  424. typedef std::map<cmStdString, cmSourceFile*> NameMapType;
  425. NameMapType NameMap;
  426. void QueueSource(cmSourceFile* sf);
  427. void FollowName(std::string const& name);
  428. void FollowNames(std::vector<std::string> const& names);
  429. bool IsUtility(std::string const& dep);
  430. void CheckCustomCommand(cmCustomCommand const& cc);
  431. void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
  432. };
  433. //----------------------------------------------------------------------------
  434. cmTargetTraceDependencies
  435. ::cmTargetTraceDependencies(cmGeneratorTarget* target):
  436. Target(target->Target), GeneratorTarget(target)
  437. {
  438. // Convenience.
  439. this->Makefile = this->Target->GetMakefile();
  440. this->GlobalGenerator =
  441. this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
  442. this->CurrentEntry = 0;
  443. // Queue all the source files already specified for the target.
  444. std::vector<cmSourceFile*> sources;
  445. if (this->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  446. {
  447. this->Target->GetSourceFiles(sources);
  448. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  449. si != sources.end(); ++si)
  450. {
  451. this->QueueSource(*si);
  452. }
  453. }
  454. // Queue pre-build, pre-link, and post-build rule dependencies.
  455. this->CheckCustomCommands(this->Target->GetPreBuildCommands());
  456. this->CheckCustomCommands(this->Target->GetPreLinkCommands());
  457. this->CheckCustomCommands(this->Target->GetPostBuildCommands());
  458. }
  459. //----------------------------------------------------------------------------
  460. void cmTargetTraceDependencies::Trace()
  461. {
  462. // Process one dependency at a time until the queue is empty.
  463. while(!this->SourceQueue.empty())
  464. {
  465. // Get the next source from the queue.
  466. cmSourceFile* sf = this->SourceQueue.front();
  467. this->SourceQueue.pop();
  468. this->CurrentEntry = &this->GeneratorTarget->SourceEntries[sf];
  469. // Queue dependencies added explicitly by the user.
  470. if(const char* additionalDeps = sf->GetProperty("OBJECT_DEPENDS"))
  471. {
  472. std::vector<std::string> objDeps;
  473. cmSystemTools::ExpandListArgument(additionalDeps, objDeps);
  474. this->FollowNames(objDeps);
  475. }
  476. // Queue the source needed to generate this file, if any.
  477. this->FollowName(sf->GetFullPath());
  478. // Queue dependencies added programatically by commands.
  479. this->FollowNames(sf->GetDepends());
  480. // Queue custom command dependencies.
  481. if(cmCustomCommand const* cc = sf->GetCustomCommand())
  482. {
  483. this->CheckCustomCommand(*cc);
  484. }
  485. }
  486. this->CurrentEntry = 0;
  487. }
  488. //----------------------------------------------------------------------------
  489. void cmTargetTraceDependencies::QueueSource(cmSourceFile* sf)
  490. {
  491. if(this->SourcesQueued.insert(sf).second)
  492. {
  493. this->SourceQueue.push(sf);
  494. // Make sure this file is in the target.
  495. this->Target->AddSourceFile(sf);
  496. }
  497. }
  498. //----------------------------------------------------------------------------
  499. void cmTargetTraceDependencies::FollowName(std::string const& name)
  500. {
  501. NameMapType::iterator i = this->NameMap.find(name);
  502. if(i == this->NameMap.end())
  503. {
  504. // Check if we know how to generate this file.
  505. cmSourceFile* sf = this->Makefile->GetSourceFileWithOutput(name.c_str());
  506. NameMapType::value_type entry(name, sf);
  507. i = this->NameMap.insert(entry).first;
  508. }
  509. if(cmSourceFile* sf = i->second)
  510. {
  511. // Record the dependency we just followed.
  512. if(this->CurrentEntry)
  513. {
  514. this->CurrentEntry->Depends.push_back(sf);
  515. }
  516. this->QueueSource(sf);
  517. }
  518. }
  519. //----------------------------------------------------------------------------
  520. void
  521. cmTargetTraceDependencies::FollowNames(std::vector<std::string> const& names)
  522. {
  523. for(std::vector<std::string>::const_iterator i = names.begin();
  524. i != names.end(); ++i)
  525. {
  526. this->FollowName(*i);
  527. }
  528. }
  529. //----------------------------------------------------------------------------
  530. bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
  531. {
  532. // Dependencies on targets (utilities) are supposed to be named by
  533. // just the target name. However for compatibility we support
  534. // naming the output file generated by the target (assuming there is
  535. // no output-name property which old code would not have set). In
  536. // that case the target name will be the file basename of the
  537. // dependency.
  538. std::string util = cmSystemTools::GetFilenameName(dep);
  539. if(cmSystemTools::GetFilenameLastExtension(util) == ".exe")
  540. {
  541. util = cmSystemTools::GetFilenameWithoutLastExtension(util);
  542. }
  543. // Check for a target with this name.
  544. if(cmTarget* t = this->Makefile->FindTargetToUse(util))
  545. {
  546. // If we find the target and the dep was given as a full path,
  547. // then make sure it was not a full path to something else, and
  548. // the fact that the name matched a target was just a coincidence.
  549. if(cmSystemTools::FileIsFullPath(dep.c_str()))
  550. {
  551. if(t->GetType() >= cmTarget::EXECUTABLE &&
  552. t->GetType() <= cmTarget::MODULE_LIBRARY)
  553. {
  554. // This is really only for compatibility so we do not need to
  555. // worry about configuration names and output names.
  556. std::string tLocation = t->GetLocation(0);
  557. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  558. std::string depLocation = cmSystemTools::GetFilenamePath(dep);
  559. depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
  560. tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
  561. if(depLocation == tLocation)
  562. {
  563. this->Target->AddUtility(util.c_str());
  564. return true;
  565. }
  566. }
  567. }
  568. else
  569. {
  570. // The original name of the dependency was not a full path. It
  571. // must name a target, so add the target-level dependency.
  572. this->Target->AddUtility(util.c_str());
  573. return true;
  574. }
  575. }
  576. // The dependency does not name a target built in this project.
  577. return false;
  578. }
  579. //----------------------------------------------------------------------------
  580. void
  581. cmTargetTraceDependencies
  582. ::CheckCustomCommand(cmCustomCommand const& cc)
  583. {
  584. // Transform command names that reference targets built in this
  585. // project to corresponding target-level dependencies.
  586. cmGeneratorExpression ge(cc.GetBacktrace());
  587. // Add target-level dependencies referenced by generator expressions.
  588. std::set<cmTarget*> targets;
  589. for(cmCustomCommandLines::const_iterator cit = cc.GetCommandLines().begin();
  590. cit != cc.GetCommandLines().end(); ++cit)
  591. {
  592. std::string const& command = *cit->begin();
  593. // Check for a target with this name.
  594. if(cmTarget* t = this->Makefile->FindTargetToUse(command))
  595. {
  596. if(t->GetType() == cmTarget::EXECUTABLE)
  597. {
  598. // The command refers to an executable target built in
  599. // this project. Add the target-level dependency to make
  600. // sure the executable is up to date before this custom
  601. // command possibly runs.
  602. this->Target->AddUtility(command.c_str());
  603. }
  604. }
  605. // Check for target references in generator expressions.
  606. for(cmCustomCommandLine::const_iterator cli = cit->begin();
  607. cli != cit->end(); ++cli)
  608. {
  609. const cmsys::auto_ptr<cmCompiledGeneratorExpression> cge
  610. = ge.Parse(*cli);
  611. cge->Evaluate(this->Makefile, 0, true);
  612. std::set<cmTarget*> geTargets = cge->GetTargets();
  613. for(std::set<cmTarget*>::const_iterator it = geTargets.begin();
  614. it != geTargets.end(); ++it)
  615. {
  616. targets.insert(*it);
  617. }
  618. }
  619. }
  620. for(std::set<cmTarget*>::iterator ti = targets.begin();
  621. ti != targets.end(); ++ti)
  622. {
  623. this->Target->AddUtility((*ti)->GetName());
  624. }
  625. // Queue the custom command dependencies.
  626. std::vector<std::string> const& depends = cc.GetDepends();
  627. for(std::vector<std::string>::const_iterator di = depends.begin();
  628. di != depends.end(); ++di)
  629. {
  630. std::string const& dep = *di;
  631. if(!this->IsUtility(dep))
  632. {
  633. // The dependency does not name a target and may be a file we
  634. // know how to generate. Queue it.
  635. this->FollowName(dep);
  636. }
  637. }
  638. }
  639. //----------------------------------------------------------------------------
  640. void
  641. cmTargetTraceDependencies
  642. ::CheckCustomCommands(const std::vector<cmCustomCommand>& commands)
  643. {
  644. for(std::vector<cmCustomCommand>::const_iterator cli = commands.begin();
  645. cli != commands.end(); ++cli)
  646. {
  647. this->CheckCustomCommand(*cli);
  648. }
  649. }
  650. //----------------------------------------------------------------------------
  651. void cmGeneratorTarget::TraceDependencies()
  652. {
  653. // CMake-generated targets have no dependencies to trace. Normally tracing
  654. // would find nothing anyway, but when building CMake itself the "install"
  655. // target command ends up referencing the "cmake" target but we do not
  656. // really want the dependency because "install" depend on "all" anyway.
  657. if(this->GetType() == cmTarget::GLOBAL_TARGET)
  658. {
  659. return;
  660. }
  661. // Use a helper object to trace the dependencies.
  662. cmTargetTraceDependencies tracer(this);
  663. tracer.Trace();
  664. }
  665. //----------------------------------------------------------------------------
  666. void cmGeneratorTarget::GetAppleArchs(const char* config,
  667. std::vector<std::string>& archVec) const
  668. {
  669. const char* archs = 0;
  670. if(config && *config)
  671. {
  672. std::string defVarName = "OSX_ARCHITECTURES_";
  673. defVarName += cmSystemTools::UpperCase(config);
  674. archs = this->Target->GetProperty(defVarName.c_str());
  675. }
  676. if(!archs)
  677. {
  678. archs = this->Target->GetProperty("OSX_ARCHITECTURES");
  679. }
  680. if(archs)
  681. {
  682. cmSystemTools::ExpandListArgument(std::string(archs), archVec);
  683. }
  684. }
  685. //----------------------------------------------------------------------------
  686. const char* cmGeneratorTarget::GetCreateRuleVariable() const
  687. {
  688. switch(this->GetType())
  689. {
  690. case cmTarget::STATIC_LIBRARY:
  691. return "_CREATE_STATIC_LIBRARY";
  692. case cmTarget::SHARED_LIBRARY:
  693. return "_CREATE_SHARED_LIBRARY";
  694. case cmTarget::MODULE_LIBRARY:
  695. return "_CREATE_SHARED_MODULE";
  696. case cmTarget::EXECUTABLE:
  697. return "_LINK_EXECUTABLE";
  698. default:
  699. break;
  700. }
  701. return "";
  702. }
  703. //----------------------------------------------------------------------------
  704. std::vector<std::string>
  705. cmGeneratorTarget::GetIncludeDirectories(const char *config) const
  706. {
  707. return this->Target->GetIncludeDirectories(config);
  708. }
  709. //----------------------------------------------------------------------------
  710. void cmGeneratorTarget::GenerateTargetManifest(const char* config) const
  711. {
  712. if (this->Target->IsImported())
  713. {
  714. return;
  715. }
  716. cmMakefile* mf = this->Target->GetMakefile();
  717. cmLocalGenerator* lg = mf->GetLocalGenerator();
  718. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  719. // Get the names.
  720. std::string name;
  721. std::string soName;
  722. std::string realName;
  723. std::string impName;
  724. std::string pdbName;
  725. if(this->GetType() == cmTarget::EXECUTABLE)
  726. {
  727. this->Target->GetExecutableNames(name, realName, impName, pdbName,
  728. config);
  729. }
  730. else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
  731. this->GetType() == cmTarget::SHARED_LIBRARY ||
  732. this->GetType() == cmTarget::MODULE_LIBRARY)
  733. {
  734. this->Target->GetLibraryNames(name, soName, realName, impName, pdbName,
  735. config);
  736. }
  737. else
  738. {
  739. return;
  740. }
  741. // Get the directory.
  742. std::string dir = this->Target->GetDirectory(config, false);
  743. // Add each name.
  744. std::string f;
  745. if(!name.empty())
  746. {
  747. f = dir;
  748. f += "/";
  749. f += name;
  750. gg->AddToManifest(config? config:"", f);
  751. }
  752. if(!soName.empty())
  753. {
  754. f = dir;
  755. f += "/";
  756. f += soName;
  757. gg->AddToManifest(config? config:"", f);
  758. }
  759. if(!realName.empty())
  760. {
  761. f = dir;
  762. f += "/";
  763. f += realName;
  764. gg->AddToManifest(config? config:"", f);
  765. }
  766. if(!pdbName.empty())
  767. {
  768. f = dir;
  769. f += "/";
  770. f += pdbName;
  771. gg->AddToManifest(config? config:"", f);
  772. }
  773. if(!impName.empty())
  774. {
  775. f = this->Target->GetDirectory(config, true);
  776. f += "/";
  777. f += impName;
  778. gg->AddToManifest(config? config:"", f);
  779. }
  780. }
  781. bool cmStrictTargetComparison::operator()(cmTarget const* t1,
  782. cmTarget const* t2) const
  783. {
  784. int nameResult = strcmp(t1->GetName(), t2->GetName());
  785. if (nameResult == 0)
  786. {
  787. return strcmp(t1->GetMakefile()->GetStartOutputDirectory(),
  788. t2->GetMakefile()->GetStartOutputDirectory()) < 0;
  789. }
  790. return nameResult < 0;
  791. }
  792. //----------------------------------------------------------------------------
  793. struct cmGeneratorTarget::SourceFileFlags
  794. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  795. {
  796. struct SourceFileFlags flags;
  797. this->ConstructSourceFileFlags();
  798. std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
  799. this->SourceFlagsMap.find(sf);
  800. if(si != this->SourceFlagsMap.end())
  801. {
  802. flags = si->second;
  803. }
  804. return flags;
  805. }
  806. //----------------------------------------------------------------------------
  807. void cmGeneratorTarget::ConstructSourceFileFlags() const
  808. {
  809. if(this->SourceFileFlagsConstructed)
  810. {
  811. return;
  812. }
  813. this->SourceFileFlagsConstructed = true;
  814. // Process public headers to mark the source files.
  815. if(const char* files = this->Target->GetProperty("PUBLIC_HEADER"))
  816. {
  817. std::vector<std::string> relFiles;
  818. cmSystemTools::ExpandListArgument(files, relFiles);
  819. for(std::vector<std::string>::iterator it = relFiles.begin();
  820. it != relFiles.end(); ++it)
  821. {
  822. if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
  823. {
  824. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  825. flags.MacFolder = "Headers";
  826. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  827. }
  828. }
  829. }
  830. // Process private headers after public headers so that they take
  831. // precedence if a file is listed in both.
  832. if(const char* files = this->Target->GetProperty("PRIVATE_HEADER"))
  833. {
  834. std::vector<std::string> relFiles;
  835. cmSystemTools::ExpandListArgument(files, relFiles);
  836. for(std::vector<std::string>::iterator it = relFiles.begin();
  837. it != relFiles.end(); ++it)
  838. {
  839. if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
  840. {
  841. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  842. flags.MacFolder = "PrivateHeaders";
  843. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  844. }
  845. }
  846. }
  847. // Mark sources listed as resources.
  848. if(const char* files = this->Target->GetProperty("RESOURCE"))
  849. {
  850. std::vector<std::string> relFiles;
  851. cmSystemTools::ExpandListArgument(files, relFiles);
  852. for(std::vector<std::string>::iterator it = relFiles.begin();
  853. it != relFiles.end(); ++it)
  854. {
  855. if(cmSourceFile* sf = this->Makefile->GetSource(it->c_str()))
  856. {
  857. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  858. flags.MacFolder = "Resources";
  859. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  860. }
  861. }
  862. }
  863. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  864. // were not listed in one of the other lists.
  865. std::vector<cmSourceFile*> sources;
  866. this->GetSourceFiles(sources);
  867. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  868. si != sources.end(); ++si)
  869. {
  870. cmSourceFile* sf = *si;
  871. if(const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION"))
  872. {
  873. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  874. if(flags.Type == cmGeneratorTarget::SourceFileTypeNormal)
  875. {
  876. flags.MacFolder = location;
  877. if(strcmp(location, "Resources") == 0)
  878. {
  879. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  880. }
  881. else
  882. {
  883. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  884. }
  885. }
  886. }
  887. }
  888. }