cmGeneratorTarget.cxx 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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 "cmCustomCommandGenerator.h"
  20. #include <queue>
  21. #include "assert.h"
  22. //----------------------------------------------------------------------------
  23. void reportBadObjLib(std::vector<cmSourceFile*> const& badObjLib,
  24. cmTarget *target, cmake *cm)
  25. {
  26. if(!badObjLib.empty())
  27. {
  28. cmOStringStream e;
  29. e << "OBJECT library \"" << target->GetName() << "\" contains:\n";
  30. for(std::vector<cmSourceFile*>::const_iterator i = badObjLib.begin();
  31. i != badObjLib.end(); ++i)
  32. {
  33. e << " " << (*i)->GetLocation().GetName() << "\n";
  34. }
  35. e << "but may contain only sources that compile, header files, and "
  36. "other files that would not affect linking of a normal library.";
  37. cm->IssueMessage(cmake::FATAL_ERROR, e.str(),
  38. target->GetBacktrace());
  39. }
  40. }
  41. struct ObjectSourcesTag {};
  42. struct CustomCommandsTag {};
  43. struct ExtraSourcesTag {};
  44. struct HeaderSourcesTag {};
  45. struct ExternalObjectsTag {};
  46. struct IDLSourcesTag {};
  47. struct ResxTag {};
  48. struct ModuleDefinitionFileTag {};
  49. struct AppManifestTag{};
  50. struct CertificatesTag{};
  51. template<typename Tag, typename OtherTag>
  52. struct IsSameTag
  53. {
  54. enum {
  55. Result = false
  56. };
  57. };
  58. template<typename Tag>
  59. struct IsSameTag<Tag, Tag>
  60. {
  61. enum {
  62. Result = true
  63. };
  64. };
  65. template<bool>
  66. struct DoAccept
  67. {
  68. template <typename T> static void Do(T&, cmSourceFile*) {}
  69. };
  70. template<>
  71. struct DoAccept<true>
  72. {
  73. static void Do(std::vector<cmSourceFile const*>& files, cmSourceFile* f)
  74. {
  75. files.push_back(f);
  76. }
  77. static void Do(cmGeneratorTarget::ResxData& data, cmSourceFile* f)
  78. {
  79. // Build and save the name of the corresponding .h file
  80. // This relationship will be used later when building the project files.
  81. // Both names would have been auto generated from Visual Studio
  82. // where the user supplied the file name and Visual Studio
  83. // appended the suffix.
  84. std::string resx = f->GetFullPath();
  85. std::string hFileName = resx.substr(0, resx.find_last_of(".")) + ".h";
  86. data.ExpectedResxHeaders.insert(hFileName);
  87. data.ResxSources.push_back(f);
  88. }
  89. static void Do(std::string& data, cmSourceFile* f)
  90. {
  91. data = f->GetFullPath();
  92. }
  93. };
  94. //----------------------------------------------------------------------------
  95. template<typename Tag, typename DataType = std::vector<cmSourceFile const*> >
  96. struct TagVisitor
  97. {
  98. DataType& Data;
  99. std::vector<cmSourceFile*> BadObjLibFiles;
  100. cmTarget *Target;
  101. cmGlobalGenerator *GlobalGenerator;
  102. cmsys::RegularExpression Header;
  103. bool IsObjLib;
  104. TagVisitor(cmTarget *target, DataType& data)
  105. : Data(data), Target(target),
  106. GlobalGenerator(target->GetMakefile()
  107. ->GetLocalGenerator()->GetGlobalGenerator()),
  108. Header(CM_HEADER_REGEX),
  109. IsObjLib(target->GetType() == cmTarget::OBJECT_LIBRARY)
  110. {
  111. }
  112. ~TagVisitor()
  113. {
  114. reportBadObjLib(this->BadObjLibFiles, this->Target,
  115. this->GlobalGenerator->GetCMakeInstance());
  116. }
  117. void Accept(cmSourceFile *sf)
  118. {
  119. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  120. if(sf->GetCustomCommand())
  121. {
  122. DoAccept<IsSameTag<Tag, CustomCommandsTag>::Result>::Do(this->Data, sf);
  123. }
  124. else if(this->Target->GetType() == cmTarget::UTILITY)
  125. {
  126. DoAccept<IsSameTag<Tag, ExtraSourcesTag>::Result>::Do(this->Data, sf);
  127. }
  128. else if(sf->GetPropertyAsBool("HEADER_FILE_ONLY"))
  129. {
  130. DoAccept<IsSameTag<Tag, HeaderSourcesTag>::Result>::Do(this->Data, sf);
  131. }
  132. else if(sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
  133. {
  134. DoAccept<IsSameTag<Tag, ExternalObjectsTag>::Result>::Do(this->Data, sf);
  135. if(this->IsObjLib)
  136. {
  137. this->BadObjLibFiles.push_back(sf);
  138. }
  139. }
  140. else if(!sf->GetLanguage().empty())
  141. {
  142. DoAccept<IsSameTag<Tag, ObjectSourcesTag>::Result>::Do(this->Data, sf);
  143. }
  144. else if(ext == "def")
  145. {
  146. DoAccept<IsSameTag<Tag, ModuleDefinitionFileTag>::Result>::Do(this->Data,
  147. sf);
  148. if(this->IsObjLib)
  149. {
  150. this->BadObjLibFiles.push_back(sf);
  151. }
  152. }
  153. else if(ext == "idl")
  154. {
  155. DoAccept<IsSameTag<Tag, IDLSourcesTag>::Result>::Do(this->Data, sf);
  156. if(this->IsObjLib)
  157. {
  158. this->BadObjLibFiles.push_back(sf);
  159. }
  160. }
  161. else if(ext == "resx")
  162. {
  163. DoAccept<IsSameTag<Tag, ResxTag>::Result>::Do(this->Data, sf);
  164. }
  165. else if (ext == "appxmanifest")
  166. {
  167. DoAccept<IsSameTag<Tag, AppManifestTag>::Result>::Do(this->Data, sf);
  168. }
  169. else if (ext == "pfx")
  170. {
  171. DoAccept<IsSameTag<Tag, CertificatesTag>::Result>::Do(this->Data, sf);
  172. }
  173. else if(this->Header.find(sf->GetFullPath().c_str()))
  174. {
  175. DoAccept<IsSameTag<Tag, HeaderSourcesTag>::Result>::Do(this->Data, sf);
  176. }
  177. else if(this->GlobalGenerator->IgnoreFile(sf->GetExtension().c_str()))
  178. {
  179. DoAccept<IsSameTag<Tag, ExtraSourcesTag>::Result>::Do(this->Data, sf);
  180. }
  181. else
  182. {
  183. DoAccept<IsSameTag<Tag, ExtraSourcesTag>::Result>::Do(this->Data, sf);
  184. }
  185. }
  186. };
  187. //----------------------------------------------------------------------------
  188. cmGeneratorTarget::cmGeneratorTarget(cmTarget* t): Target(t),
  189. SourceFileFlagsConstructed(false)
  190. {
  191. this->Makefile = this->Target->GetMakefile();
  192. this->LocalGenerator = this->Makefile->GetLocalGenerator();
  193. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  194. }
  195. //----------------------------------------------------------------------------
  196. int cmGeneratorTarget::GetType() const
  197. {
  198. return this->Target->GetType();
  199. }
  200. //----------------------------------------------------------------------------
  201. std::string cmGeneratorTarget::GetName() const
  202. {
  203. return this->Target->GetName();
  204. }
  205. //----------------------------------------------------------------------------
  206. const char *cmGeneratorTarget::GetProperty(const std::string& prop) const
  207. {
  208. return this->Target->GetProperty(prop);
  209. }
  210. //----------------------------------------------------------------------------
  211. std::vector<cmSourceFile*> const*
  212. cmGeneratorTarget::GetSourceDepends(cmSourceFile const* sf) const
  213. {
  214. SourceEntriesType::const_iterator i = this->SourceEntries.find(sf);
  215. if(i != this->SourceEntries.end())
  216. {
  217. return &i->second.Depends;
  218. }
  219. return 0;
  220. }
  221. static void handleSystemIncludesDep(cmMakefile *mf, cmTarget const* depTgt,
  222. const std::string& config,
  223. cmTarget *headTarget,
  224. cmGeneratorExpressionDAGChecker *dagChecker,
  225. std::vector<std::string>& result,
  226. bool excludeImported)
  227. {
  228. if (const char* dirs =
  229. depTgt->GetProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES"))
  230. {
  231. cmGeneratorExpression ge;
  232. cmSystemTools::ExpandListArgument(ge.Parse(dirs)
  233. ->Evaluate(mf,
  234. config, false, headTarget,
  235. depTgt, dagChecker), result);
  236. }
  237. if (!depTgt->IsImported() || excludeImported)
  238. {
  239. return;
  240. }
  241. if (const char* dirs =
  242. depTgt->GetProperty("INTERFACE_INCLUDE_DIRECTORIES"))
  243. {
  244. cmGeneratorExpression ge;
  245. cmSystemTools::ExpandListArgument(ge.Parse(dirs)
  246. ->Evaluate(mf,
  247. config, false, headTarget,
  248. depTgt, dagChecker), result);
  249. }
  250. }
  251. #define IMPLEMENT_VISIT_IMPL(DATA, DATATYPE) \
  252. { \
  253. std::vector<cmSourceFile*> sourceFiles; \
  254. this->Target->GetSourceFiles(sourceFiles, config); \
  255. TagVisitor<DATA ## Tag DATATYPE> visitor(this->Target, data); \
  256. for(std::vector<cmSourceFile*>::const_iterator si = sourceFiles.begin(); \
  257. si != sourceFiles.end(); ++si) \
  258. { \
  259. visitor.Accept(*si); \
  260. } \
  261. } \
  262. #define IMPLEMENT_VISIT(DATA) \
  263. IMPLEMENT_VISIT_IMPL(DATA, EMPTY) \
  264. #define EMPTY
  265. #define COMMA ,
  266. //----------------------------------------------------------------------------
  267. void
  268. cmGeneratorTarget
  269. ::GetObjectSources(std::vector<cmSourceFile const*> &data,
  270. const std::string& config) const
  271. {
  272. IMPLEMENT_VISIT(ObjectSources);
  273. if (!this->Objects.empty())
  274. {
  275. return;
  276. }
  277. for(std::vector<cmSourceFile const*>::const_iterator it = data.begin();
  278. it != data.end(); ++it)
  279. {
  280. this->Objects[*it];
  281. }
  282. this->LocalGenerator->ComputeObjectFilenames(this->Objects, this);
  283. }
  284. void cmGeneratorTarget::ComputeObjectMapping()
  285. {
  286. if(!this->Objects.empty())
  287. {
  288. return;
  289. }
  290. std::vector<std::string> configs;
  291. this->Makefile->GetConfigurations(configs);
  292. if (configs.empty())
  293. {
  294. configs.push_back("");
  295. }
  296. for(std::vector<std::string>::const_iterator ci = configs.begin();
  297. ci != configs.end(); ++ci)
  298. {
  299. std::vector<cmSourceFile const*> sourceFiles;
  300. this->GetObjectSources(sourceFiles, *ci);
  301. }
  302. }
  303. //----------------------------------------------------------------------------
  304. const std::string& cmGeneratorTarget::GetObjectName(cmSourceFile const* file)
  305. {
  306. this->ComputeObjectMapping();
  307. return this->Objects[file];
  308. }
  309. //----------------------------------------------------------------------------
  310. void cmGeneratorTarget::AddExplicitObjectName(cmSourceFile const* sf)
  311. {
  312. this->ExplicitObjectName.insert(sf);
  313. }
  314. //----------------------------------------------------------------------------
  315. bool cmGeneratorTarget::HasExplicitObjectName(cmSourceFile const* file) const
  316. {
  317. const_cast<cmGeneratorTarget*>(this)->ComputeObjectMapping();
  318. std::set<cmSourceFile const*>::const_iterator it
  319. = this->ExplicitObjectName.find(file);
  320. return it != this->ExplicitObjectName.end();
  321. }
  322. //----------------------------------------------------------------------------
  323. void cmGeneratorTarget
  324. ::GetIDLSources(std::vector<cmSourceFile const*>& data,
  325. const std::string& config) const
  326. {
  327. IMPLEMENT_VISIT(IDLSources);
  328. }
  329. //----------------------------------------------------------------------------
  330. void
  331. cmGeneratorTarget
  332. ::GetHeaderSources(std::vector<cmSourceFile const*>& data,
  333. const std::string& config) const
  334. {
  335. IMPLEMENT_VISIT(HeaderSources);
  336. }
  337. //----------------------------------------------------------------------------
  338. void cmGeneratorTarget
  339. ::GetExtraSources(std::vector<cmSourceFile const*>& data,
  340. const std::string& config) const
  341. {
  342. IMPLEMENT_VISIT(ExtraSources);
  343. }
  344. //----------------------------------------------------------------------------
  345. void
  346. cmGeneratorTarget
  347. ::GetCustomCommands(std::vector<cmSourceFile const*>& data,
  348. const std::string& config) const
  349. {
  350. IMPLEMENT_VISIT(CustomCommands);
  351. }
  352. //----------------------------------------------------------------------------
  353. void
  354. cmGeneratorTarget
  355. ::GetExternalObjects(std::vector<cmSourceFile const*>& data,
  356. const std::string& config) const
  357. {
  358. IMPLEMENT_VISIT(ExternalObjects);
  359. }
  360. //----------------------------------------------------------------------------
  361. void
  362. cmGeneratorTarget::GetExpectedResxHeaders(std::set<std::string>& srcs,
  363. const std::string& config) const
  364. {
  365. ResxData data;
  366. IMPLEMENT_VISIT_IMPL(Resx, COMMA cmGeneratorTarget::ResxData)
  367. srcs = data.ExpectedResxHeaders;
  368. }
  369. //----------------------------------------------------------------------------
  370. void cmGeneratorTarget
  371. ::GetResxSources(std::vector<cmSourceFile const*>& srcs,
  372. const std::string& config) const
  373. {
  374. ResxData data;
  375. IMPLEMENT_VISIT_IMPL(Resx, COMMA cmGeneratorTarget::ResxData)
  376. srcs = data.ResxSources;
  377. }
  378. //----------------------------------------------------------------------------
  379. void
  380. cmGeneratorTarget
  381. ::GetAppManifest(std::vector<cmSourceFile const*>& data,
  382. const std::string& config) const
  383. {
  384. IMPLEMENT_VISIT(AppManifest);
  385. }
  386. //----------------------------------------------------------------------------
  387. void
  388. cmGeneratorTarget
  389. ::GetCertificates(std::vector<cmSourceFile const*>& data,
  390. const std::string& config) const
  391. {
  392. IMPLEMENT_VISIT(Certificates);
  393. }
  394. //----------------------------------------------------------------------------
  395. bool cmGeneratorTarget::IsSystemIncludeDirectory(const std::string& dir,
  396. const std::string& config) const
  397. {
  398. assert(this->GetType() != cmTarget::INTERFACE_LIBRARY);
  399. std::string config_upper;
  400. if(!config.empty())
  401. {
  402. config_upper = cmSystemTools::UpperCase(config);
  403. }
  404. typedef std::map<std::string, std::vector<std::string> > IncludeCacheType;
  405. IncludeCacheType::const_iterator iter =
  406. this->SystemIncludesCache.find(config_upper);
  407. if (iter == this->SystemIncludesCache.end())
  408. {
  409. cmGeneratorExpressionDAGChecker dagChecker(
  410. this->GetName(),
  411. "SYSTEM_INCLUDE_DIRECTORIES", 0, 0);
  412. bool excludeImported
  413. = this->Target->GetPropertyAsBool("NO_SYSTEM_FROM_IMPORTED");
  414. std::vector<std::string> result;
  415. for (std::set<std::string>::const_iterator
  416. it = this->Target->GetSystemIncludeDirectories().begin();
  417. it != this->Target->GetSystemIncludeDirectories().end(); ++it)
  418. {
  419. cmGeneratorExpression ge;
  420. cmSystemTools::ExpandListArgument(ge.Parse(*it)
  421. ->Evaluate(this->Makefile,
  422. config, false, this->Target,
  423. &dagChecker), result);
  424. }
  425. std::vector<cmTarget const*> const& deps =
  426. this->Target->GetLinkImplementationClosure(config);
  427. for(std::vector<cmTarget const*>::const_iterator
  428. li = deps.begin(), le = deps.end(); li != le; ++li)
  429. {
  430. handleSystemIncludesDep(this->Makefile, *li, config, this->Target,
  431. &dagChecker, result, excludeImported);
  432. }
  433. std::set<std::string> unique;
  434. for(std::vector<std::string>::iterator li = result.begin();
  435. li != result.end(); ++li)
  436. {
  437. cmSystemTools::ConvertToUnixSlashes(*li);
  438. unique.insert(*li);
  439. }
  440. result.clear();
  441. result.insert(result.end(), unique.begin(), unique.end());
  442. IncludeCacheType::value_type entry(config_upper, result);
  443. iter = this->SystemIncludesCache.insert(entry).first;
  444. }
  445. return std::binary_search(iter->second.begin(), iter->second.end(), dir);
  446. }
  447. //----------------------------------------------------------------------------
  448. bool cmGeneratorTarget::GetPropertyAsBool(const std::string& prop) const
  449. {
  450. return this->Target->GetPropertyAsBool(prop);
  451. }
  452. //----------------------------------------------------------------------------
  453. void cmGeneratorTarget::GetSourceFiles(std::vector<cmSourceFile*> &files,
  454. const std::string& config) const
  455. {
  456. this->Target->GetSourceFiles(files, config);
  457. }
  458. //----------------------------------------------------------------------------
  459. std::string
  460. cmGeneratorTarget::GetModuleDefinitionFile(const std::string& config) const
  461. {
  462. std::string data;
  463. IMPLEMENT_VISIT_IMPL(ModuleDefinitionFile, COMMA std::string)
  464. return data;
  465. }
  466. //----------------------------------------------------------------------------
  467. void
  468. cmGeneratorTarget::UseObjectLibraries(std::vector<std::string>& objs,
  469. const std::string &config) const
  470. {
  471. std::vector<cmSourceFile const*> objectFiles;
  472. this->GetExternalObjects(objectFiles, config);
  473. std::vector<cmTarget*> objectLibraries;
  474. std::set<cmTarget*> emitted;
  475. for(std::vector<cmSourceFile const*>::const_iterator
  476. it = objectFiles.begin(); it != objectFiles.end(); ++it)
  477. {
  478. std::string objLib = (*it)->GetObjectLibrary();
  479. if (cmTarget* tgt = this->Makefile->FindTargetToUse(objLib))
  480. {
  481. if (emitted.insert(tgt).second)
  482. {
  483. objectLibraries.push_back(tgt);
  484. }
  485. }
  486. }
  487. for(std::vector<cmTarget*>::const_iterator
  488. ti = objectLibraries.begin();
  489. ti != objectLibraries.end(); ++ti)
  490. {
  491. cmTarget* objLib = *ti;
  492. cmGeneratorTarget* ogt =
  493. this->GlobalGenerator->GetGeneratorTarget(objLib);
  494. std::vector<cmSourceFile const*> objectSources;
  495. ogt->GetObjectSources(objectSources, config);
  496. for(std::vector<cmSourceFile const*>::const_iterator
  497. si = objectSources.begin();
  498. si != objectSources.end(); ++si)
  499. {
  500. std::string obj = ogt->ObjectDirectory;
  501. obj += ogt->Objects[*si];
  502. objs.push_back(obj);
  503. }
  504. }
  505. }
  506. //----------------------------------------------------------------------------
  507. class cmTargetTraceDependencies
  508. {
  509. public:
  510. cmTargetTraceDependencies(cmGeneratorTarget* target);
  511. void Trace();
  512. private:
  513. cmTarget* Target;
  514. cmGeneratorTarget* GeneratorTarget;
  515. cmMakefile* Makefile;
  516. cmGlobalGenerator const* GlobalGenerator;
  517. typedef cmGeneratorTarget::SourceEntry SourceEntry;
  518. SourceEntry* CurrentEntry;
  519. std::queue<cmSourceFile*> SourceQueue;
  520. std::set<cmSourceFile*> SourcesQueued;
  521. typedef std::map<std::string, cmSourceFile*> NameMapType;
  522. NameMapType NameMap;
  523. std::vector<std::string> NewSources;
  524. void QueueSource(cmSourceFile* sf);
  525. void FollowName(std::string const& name);
  526. void FollowNames(std::vector<std::string> const& names);
  527. bool IsUtility(std::string const& dep);
  528. void CheckCustomCommand(cmCustomCommand const& cc);
  529. void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
  530. void FollowCommandDepends(cmCustomCommand const& cc,
  531. const std::string& config,
  532. std::set<std::string>& emitted);
  533. };
  534. //----------------------------------------------------------------------------
  535. cmTargetTraceDependencies
  536. ::cmTargetTraceDependencies(cmGeneratorTarget* target):
  537. Target(target->Target), GeneratorTarget(target)
  538. {
  539. // Convenience.
  540. this->Makefile = this->Target->GetMakefile();
  541. this->GlobalGenerator =
  542. this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
  543. this->CurrentEntry = 0;
  544. // Queue all the source files already specified for the target.
  545. if (this->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  546. {
  547. std::vector<std::string> configs;
  548. this->Makefile->GetConfigurations(configs);
  549. if (configs.empty())
  550. {
  551. configs.push_back("");
  552. }
  553. std::set<cmSourceFile*> emitted;
  554. for(std::vector<std::string>::const_iterator ci = configs.begin();
  555. ci != configs.end(); ++ci)
  556. {
  557. std::vector<cmSourceFile*> sources;
  558. this->Target->GetSourceFiles(sources, *ci);
  559. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  560. si != sources.end(); ++si)
  561. {
  562. cmSourceFile* sf = *si;
  563. const std::set<cmTarget const*> tgts =
  564. this->GlobalGenerator->GetFilenameTargetDepends(sf);
  565. if (tgts.find(this->Target) != tgts.end())
  566. {
  567. cmOStringStream e;
  568. e << "Evaluation output file\n \"" << sf->GetFullPath()
  569. << "\"\ndepends on the sources of a target it is used in. This "
  570. "is a dependency loop and is not allowed.";
  571. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  572. return;
  573. }
  574. if(emitted.insert(sf).second && this->SourcesQueued.insert(sf).second)
  575. {
  576. this->SourceQueue.push(sf);
  577. }
  578. }
  579. }
  580. }
  581. // Queue pre-build, pre-link, and post-build rule dependencies.
  582. this->CheckCustomCommands(this->Target->GetPreBuildCommands());
  583. this->CheckCustomCommands(this->Target->GetPreLinkCommands());
  584. this->CheckCustomCommands(this->Target->GetPostBuildCommands());
  585. }
  586. //----------------------------------------------------------------------------
  587. void cmTargetTraceDependencies::Trace()
  588. {
  589. // Process one dependency at a time until the queue is empty.
  590. while(!this->SourceQueue.empty())
  591. {
  592. // Get the next source from the queue.
  593. cmSourceFile* sf = this->SourceQueue.front();
  594. this->SourceQueue.pop();
  595. this->CurrentEntry = &this->GeneratorTarget->SourceEntries[sf];
  596. // Queue dependencies added explicitly by the user.
  597. if(const char* additionalDeps = sf->GetProperty("OBJECT_DEPENDS"))
  598. {
  599. std::vector<std::string> objDeps;
  600. cmSystemTools::ExpandListArgument(additionalDeps, objDeps);
  601. this->FollowNames(objDeps);
  602. }
  603. // Queue the source needed to generate this file, if any.
  604. this->FollowName(sf->GetFullPath());
  605. // Queue dependencies added programatically by commands.
  606. this->FollowNames(sf->GetDepends());
  607. // Queue custom command dependencies.
  608. if(cmCustomCommand const* cc = sf->GetCustomCommand())
  609. {
  610. this->CheckCustomCommand(*cc);
  611. }
  612. }
  613. this->CurrentEntry = 0;
  614. this->Target->AddTracedSources(this->NewSources);
  615. }
  616. //----------------------------------------------------------------------------
  617. void cmTargetTraceDependencies::QueueSource(cmSourceFile* sf)
  618. {
  619. if(this->SourcesQueued.insert(sf).second)
  620. {
  621. this->SourceQueue.push(sf);
  622. // Make sure this file is in the target at the end.
  623. this->NewSources.push_back(sf->GetFullPath());
  624. }
  625. }
  626. //----------------------------------------------------------------------------
  627. void cmTargetTraceDependencies::FollowName(std::string const& name)
  628. {
  629. NameMapType::iterator i = this->NameMap.find(name);
  630. if(i == this->NameMap.end())
  631. {
  632. // Check if we know how to generate this file.
  633. cmSourceFile* sf = this->Makefile->GetSourceFileWithOutput(name);
  634. NameMapType::value_type entry(name, sf);
  635. i = this->NameMap.insert(entry).first;
  636. }
  637. if(cmSourceFile* sf = i->second)
  638. {
  639. // Record the dependency we just followed.
  640. if(this->CurrentEntry)
  641. {
  642. this->CurrentEntry->Depends.push_back(sf);
  643. }
  644. this->QueueSource(sf);
  645. }
  646. }
  647. //----------------------------------------------------------------------------
  648. void
  649. cmTargetTraceDependencies::FollowNames(std::vector<std::string> const& names)
  650. {
  651. for(std::vector<std::string>::const_iterator i = names.begin();
  652. i != names.end(); ++i)
  653. {
  654. this->FollowName(*i);
  655. }
  656. }
  657. //----------------------------------------------------------------------------
  658. bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
  659. {
  660. // Dependencies on targets (utilities) are supposed to be named by
  661. // just the target name. However for compatibility we support
  662. // naming the output file generated by the target (assuming there is
  663. // no output-name property which old code would not have set). In
  664. // that case the target name will be the file basename of the
  665. // dependency.
  666. std::string util = cmSystemTools::GetFilenameName(dep);
  667. if(cmSystemTools::GetFilenameLastExtension(util) == ".exe")
  668. {
  669. util = cmSystemTools::GetFilenameWithoutLastExtension(util);
  670. }
  671. // Check for a target with this name.
  672. if(cmTarget* t = this->Makefile->FindTargetToUse(util))
  673. {
  674. // If we find the target and the dep was given as a full path,
  675. // then make sure it was not a full path to something else, and
  676. // the fact that the name matched a target was just a coincidence.
  677. if(cmSystemTools::FileIsFullPath(dep.c_str()))
  678. {
  679. if(t->GetType() >= cmTarget::EXECUTABLE &&
  680. t->GetType() <= cmTarget::MODULE_LIBRARY)
  681. {
  682. // This is really only for compatibility so we do not need to
  683. // worry about configuration names and output names.
  684. std::string tLocation = t->GetLocationForBuild();
  685. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  686. std::string depLocation = cmSystemTools::GetFilenamePath(dep);
  687. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  688. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  689. if(depLocation == tLocation)
  690. {
  691. this->Target->AddUtility(util);
  692. return true;
  693. }
  694. }
  695. }
  696. else
  697. {
  698. // The original name of the dependency was not a full path. It
  699. // must name a target, so add the target-level dependency.
  700. this->Target->AddUtility(util);
  701. return true;
  702. }
  703. }
  704. // The dependency does not name a target built in this project.
  705. return false;
  706. }
  707. //----------------------------------------------------------------------------
  708. void
  709. cmTargetTraceDependencies
  710. ::CheckCustomCommand(cmCustomCommand const& cc)
  711. {
  712. // Transform command names that reference targets built in this
  713. // project to corresponding target-level dependencies.
  714. cmGeneratorExpression ge(&cc.GetBacktrace());
  715. // Add target-level dependencies referenced by generator expressions.
  716. std::set<cmTarget*> targets;
  717. for(cmCustomCommandLines::const_iterator cit = cc.GetCommandLines().begin();
  718. cit != cc.GetCommandLines().end(); ++cit)
  719. {
  720. std::string const& command = *cit->begin();
  721. // Check for a target with this name.
  722. if(cmTarget* t = this->Makefile->FindTargetToUse(command))
  723. {
  724. if(t->GetType() == cmTarget::EXECUTABLE)
  725. {
  726. // The command refers to an executable target built in
  727. // this project. Add the target-level dependency to make
  728. // sure the executable is up to date before this custom
  729. // command possibly runs.
  730. this->Target->AddUtility(command);
  731. }
  732. }
  733. // Check for target references in generator expressions.
  734. for(cmCustomCommandLine::const_iterator cli = cit->begin();
  735. cli != cit->end(); ++cli)
  736. {
  737. const cmsys::auto_ptr<cmCompiledGeneratorExpression> cge
  738. = ge.Parse(*cli);
  739. cge->Evaluate(this->Makefile, "", true);
  740. std::set<cmTarget*> geTargets = cge->GetTargets();
  741. for(std::set<cmTarget*>::const_iterator it = geTargets.begin();
  742. it != geTargets.end(); ++it)
  743. {
  744. targets.insert(*it);
  745. }
  746. }
  747. }
  748. for(std::set<cmTarget*>::iterator ti = targets.begin();
  749. ti != targets.end(); ++ti)
  750. {
  751. this->Target->AddUtility((*ti)->GetName());
  752. }
  753. // Queue the custom command dependencies.
  754. std::vector<std::string> configs;
  755. std::set<std::string> emitted;
  756. this->Makefile->GetConfigurations(configs);
  757. if (configs.empty())
  758. {
  759. configs.push_back("");
  760. }
  761. for(std::vector<std::string>::const_iterator ci = configs.begin();
  762. ci != configs.end(); ++ci)
  763. {
  764. this->FollowCommandDepends(cc, *ci, emitted);
  765. }
  766. }
  767. //----------------------------------------------------------------------------
  768. void cmTargetTraceDependencies::FollowCommandDepends(cmCustomCommand const& cc,
  769. const std::string& config,
  770. std::set<std::string>& emitted)
  771. {
  772. cmCustomCommandGenerator ccg(cc, config, this->Makefile);
  773. const std::vector<std::string>& depends = ccg.GetDepends();
  774. for(std::vector<std::string>::const_iterator di = depends.begin();
  775. di != depends.end(); ++di)
  776. {
  777. std::string const& dep = *di;
  778. if(emitted.insert(dep).second)
  779. {
  780. if(!this->IsUtility(dep))
  781. {
  782. // The dependency does not name a target and may be a file we
  783. // know how to generate. Queue it.
  784. this->FollowName(dep);
  785. }
  786. }
  787. }
  788. }
  789. //----------------------------------------------------------------------------
  790. void
  791. cmTargetTraceDependencies
  792. ::CheckCustomCommands(const std::vector<cmCustomCommand>& commands)
  793. {
  794. for(std::vector<cmCustomCommand>::const_iterator cli = commands.begin();
  795. cli != commands.end(); ++cli)
  796. {
  797. this->CheckCustomCommand(*cli);
  798. }
  799. }
  800. //----------------------------------------------------------------------------
  801. void cmGeneratorTarget::TraceDependencies()
  802. {
  803. // CMake-generated targets have no dependencies to trace. Normally tracing
  804. // would find nothing anyway, but when building CMake itself the "install"
  805. // target command ends up referencing the "cmake" target but we do not
  806. // really want the dependency because "install" depend on "all" anyway.
  807. if(this->GetType() == cmTarget::GLOBAL_TARGET)
  808. {
  809. return;
  810. }
  811. // Use a helper object to trace the dependencies.
  812. cmTargetTraceDependencies tracer(this);
  813. tracer.Trace();
  814. }
  815. //----------------------------------------------------------------------------
  816. void cmGeneratorTarget::GetAppleArchs(const std::string& config,
  817. std::vector<std::string>& archVec) const
  818. {
  819. const char* archs = 0;
  820. if(!config.empty())
  821. {
  822. std::string defVarName = "OSX_ARCHITECTURES_";
  823. defVarName += cmSystemTools::UpperCase(config);
  824. archs = this->Target->GetProperty(defVarName);
  825. }
  826. if(!archs)
  827. {
  828. archs = this->Target->GetProperty("OSX_ARCHITECTURES");
  829. }
  830. if(archs)
  831. {
  832. cmSystemTools::ExpandListArgument(std::string(archs), archVec);
  833. }
  834. }
  835. //----------------------------------------------------------------------------
  836. std::string
  837. cmGeneratorTarget::GetCreateRuleVariable(std::string const& lang,
  838. std::string const& config) const
  839. {
  840. switch(this->GetType())
  841. {
  842. case cmTarget::STATIC_LIBRARY:
  843. {
  844. std::string var = "CMAKE_" + lang + "_CREATE_STATIC_LIBRARY";
  845. if(this->Target->GetFeatureAsBool(
  846. "INTERPROCEDURAL_OPTIMIZATION", config))
  847. {
  848. std::string varIPO = var + "_IPO";
  849. if(this->Makefile->GetDefinition(varIPO))
  850. {
  851. return varIPO;
  852. }
  853. }
  854. return var;
  855. }
  856. case cmTarget::SHARED_LIBRARY:
  857. return "CMAKE_" + lang + "_CREATE_SHARED_LIBRARY";
  858. case cmTarget::MODULE_LIBRARY:
  859. return "CMAKE_" + lang + "_CREATE_SHARED_MODULE";
  860. case cmTarget::EXECUTABLE:
  861. return "CMAKE_" + lang + "_LINK_EXECUTABLE";
  862. default:
  863. break;
  864. }
  865. return "";
  866. }
  867. //----------------------------------------------------------------------------
  868. std::vector<std::string>
  869. cmGeneratorTarget::GetIncludeDirectories(const std::string& config) const
  870. {
  871. return this->Target->GetIncludeDirectories(config);
  872. }
  873. //----------------------------------------------------------------------------
  874. void cmGeneratorTarget::GenerateTargetManifest(
  875. const std::string& config) const
  876. {
  877. if (this->Target->IsImported())
  878. {
  879. return;
  880. }
  881. cmMakefile* mf = this->Target->GetMakefile();
  882. cmLocalGenerator* lg = mf->GetLocalGenerator();
  883. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  884. // Get the names.
  885. std::string name;
  886. std::string soName;
  887. std::string realName;
  888. std::string impName;
  889. std::string pdbName;
  890. if(this->GetType() == cmTarget::EXECUTABLE)
  891. {
  892. this->Target->GetExecutableNames(name, realName, impName, pdbName,
  893. config);
  894. }
  895. else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
  896. this->GetType() == cmTarget::SHARED_LIBRARY ||
  897. this->GetType() == cmTarget::MODULE_LIBRARY)
  898. {
  899. this->Target->GetLibraryNames(name, soName, realName, impName, pdbName,
  900. config);
  901. }
  902. else
  903. {
  904. return;
  905. }
  906. // Get the directory.
  907. std::string dir = this->Target->GetDirectory(config, false);
  908. // Add each name.
  909. std::string f;
  910. if(!name.empty())
  911. {
  912. f = dir;
  913. f += "/";
  914. f += name;
  915. gg->AddToManifest(config, f);
  916. }
  917. if(!soName.empty())
  918. {
  919. f = dir;
  920. f += "/";
  921. f += soName;
  922. gg->AddToManifest(config, f);
  923. }
  924. if(!realName.empty())
  925. {
  926. f = dir;
  927. f += "/";
  928. f += realName;
  929. gg->AddToManifest(config, f);
  930. }
  931. if(!pdbName.empty())
  932. {
  933. f = dir;
  934. f += "/";
  935. f += pdbName;
  936. gg->AddToManifest(config, f);
  937. }
  938. if(!impName.empty())
  939. {
  940. f = this->Target->GetDirectory(config, true);
  941. f += "/";
  942. f += impName;
  943. gg->AddToManifest(config, f);
  944. }
  945. }
  946. bool cmStrictTargetComparison::operator()(cmTarget const* t1,
  947. cmTarget const* t2) const
  948. {
  949. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  950. if (nameResult == 0)
  951. {
  952. return strcmp(t1->GetMakefile()->GetStartOutputDirectory(),
  953. t2->GetMakefile()->GetStartOutputDirectory()) < 0;
  954. }
  955. return nameResult < 0;
  956. }
  957. //----------------------------------------------------------------------------
  958. struct cmGeneratorTarget::SourceFileFlags
  959. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  960. {
  961. struct SourceFileFlags flags;
  962. this->ConstructSourceFileFlags();
  963. std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
  964. this->SourceFlagsMap.find(sf);
  965. if(si != this->SourceFlagsMap.end())
  966. {
  967. flags = si->second;
  968. }
  969. else
  970. {
  971. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  972. // were not listed in one of the other lists.
  973. if(const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION"))
  974. {
  975. flags.MacFolder = location;
  976. if(strcmp(location, "Resources") == 0)
  977. {
  978. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  979. }
  980. else
  981. {
  982. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  983. }
  984. }
  985. }
  986. return flags;
  987. }
  988. //----------------------------------------------------------------------------
  989. void cmGeneratorTarget::ConstructSourceFileFlags() const
  990. {
  991. if(this->SourceFileFlagsConstructed)
  992. {
  993. return;
  994. }
  995. this->SourceFileFlagsConstructed = true;
  996. // Process public headers to mark the source files.
  997. if(const char* files = this->Target->GetProperty("PUBLIC_HEADER"))
  998. {
  999. std::vector<std::string> relFiles;
  1000. cmSystemTools::ExpandListArgument(files, relFiles);
  1001. for(std::vector<std::string>::iterator it = relFiles.begin();
  1002. it != relFiles.end(); ++it)
  1003. {
  1004. if(cmSourceFile* sf = this->Makefile->GetSource(*it))
  1005. {
  1006. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  1007. flags.MacFolder = "Headers";
  1008. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  1009. }
  1010. }
  1011. }
  1012. // Process private headers after public headers so that they take
  1013. // precedence if a file is listed in both.
  1014. if(const char* files = this->Target->GetProperty("PRIVATE_HEADER"))
  1015. {
  1016. std::vector<std::string> relFiles;
  1017. cmSystemTools::ExpandListArgument(files, relFiles);
  1018. for(std::vector<std::string>::iterator it = relFiles.begin();
  1019. it != relFiles.end(); ++it)
  1020. {
  1021. if(cmSourceFile* sf = this->Makefile->GetSource(*it))
  1022. {
  1023. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  1024. flags.MacFolder = "PrivateHeaders";
  1025. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  1026. }
  1027. }
  1028. }
  1029. // Mark sources listed as resources.
  1030. if(const char* files = this->Target->GetProperty("RESOURCE"))
  1031. {
  1032. std::vector<std::string> relFiles;
  1033. cmSystemTools::ExpandListArgument(files, relFiles);
  1034. for(std::vector<std::string>::iterator it = relFiles.begin();
  1035. it != relFiles.end(); ++it)
  1036. {
  1037. if(cmSourceFile* sf = this->Makefile->GetSource(*it))
  1038. {
  1039. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  1040. flags.MacFolder = "Resources";
  1041. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  1042. }
  1043. }
  1044. }
  1045. }