cmGeneratorTarget.cxx 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. std::ostringstream 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. for(std::vector<cmSourceFile const*>::const_iterator
  475. it = objectFiles.begin(); it != objectFiles.end(); ++it)
  476. {
  477. std::string objLib = (*it)->GetObjectLibrary();
  478. if (cmTarget* tgt = this->Makefile->FindTargetToUse(objLib))
  479. {
  480. objectLibraries.push_back(tgt);
  481. }
  482. }
  483. std::vector<cmTarget*>::const_iterator end
  484. = cmRemoveDuplicates(objectLibraries);
  485. for(std::vector<cmTarget*>::const_iterator
  486. ti = objectLibraries.begin();
  487. ti != end; ++ti)
  488. {
  489. cmTarget* objLib = *ti;
  490. cmGeneratorTarget* ogt =
  491. this->GlobalGenerator->GetGeneratorTarget(objLib);
  492. std::vector<cmSourceFile const*> objectSources;
  493. ogt->GetObjectSources(objectSources, config);
  494. for(std::vector<cmSourceFile const*>::const_iterator
  495. si = objectSources.begin();
  496. si != objectSources.end(); ++si)
  497. {
  498. std::string obj = ogt->ObjectDirectory;
  499. obj += ogt->Objects[*si];
  500. objs.push_back(obj);
  501. }
  502. }
  503. }
  504. //----------------------------------------------------------------------------
  505. class cmTargetTraceDependencies
  506. {
  507. public:
  508. cmTargetTraceDependencies(cmGeneratorTarget* target);
  509. void Trace();
  510. private:
  511. cmTarget* Target;
  512. cmGeneratorTarget* GeneratorTarget;
  513. cmMakefile* Makefile;
  514. cmGlobalGenerator const* GlobalGenerator;
  515. typedef cmGeneratorTarget::SourceEntry SourceEntry;
  516. SourceEntry* CurrentEntry;
  517. std::queue<cmSourceFile*> SourceQueue;
  518. std::set<cmSourceFile*> SourcesQueued;
  519. typedef std::map<std::string, cmSourceFile*> NameMapType;
  520. NameMapType NameMap;
  521. std::vector<std::string> NewSources;
  522. void QueueSource(cmSourceFile* sf);
  523. void FollowName(std::string const& name);
  524. void FollowNames(std::vector<std::string> const& names);
  525. bool IsUtility(std::string const& dep);
  526. void CheckCustomCommand(cmCustomCommand const& cc);
  527. void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
  528. void FollowCommandDepends(cmCustomCommand const& cc,
  529. const std::string& config,
  530. std::set<std::string>& emitted);
  531. };
  532. //----------------------------------------------------------------------------
  533. cmTargetTraceDependencies
  534. ::cmTargetTraceDependencies(cmGeneratorTarget* target):
  535. Target(target->Target), GeneratorTarget(target)
  536. {
  537. // Convenience.
  538. this->Makefile = this->Target->GetMakefile();
  539. this->GlobalGenerator =
  540. this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
  541. this->CurrentEntry = 0;
  542. // Queue all the source files already specified for the target.
  543. if (this->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  544. {
  545. std::vector<std::string> configs;
  546. this->Makefile->GetConfigurations(configs);
  547. if (configs.empty())
  548. {
  549. configs.push_back("");
  550. }
  551. std::set<cmSourceFile*> emitted;
  552. for(std::vector<std::string>::const_iterator ci = configs.begin();
  553. ci != configs.end(); ++ci)
  554. {
  555. std::vector<cmSourceFile*> sources;
  556. this->Target->GetSourceFiles(sources, *ci);
  557. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  558. si != sources.end(); ++si)
  559. {
  560. cmSourceFile* sf = *si;
  561. const std::set<cmTarget const*> tgts =
  562. this->GlobalGenerator->GetFilenameTargetDepends(sf);
  563. if (tgts.find(this->Target) != tgts.end())
  564. {
  565. std::ostringstream e;
  566. e << "Evaluation output file\n \"" << sf->GetFullPath()
  567. << "\"\ndepends on the sources of a target it is used in. This "
  568. "is a dependency loop and is not allowed.";
  569. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  570. return;
  571. }
  572. if(emitted.insert(sf).second && this->SourcesQueued.insert(sf).second)
  573. {
  574. this->SourceQueue.push(sf);
  575. }
  576. }
  577. }
  578. }
  579. // Queue pre-build, pre-link, and post-build rule dependencies.
  580. this->CheckCustomCommands(this->Target->GetPreBuildCommands());
  581. this->CheckCustomCommands(this->Target->GetPreLinkCommands());
  582. this->CheckCustomCommands(this->Target->GetPostBuildCommands());
  583. }
  584. //----------------------------------------------------------------------------
  585. void cmTargetTraceDependencies::Trace()
  586. {
  587. // Process one dependency at a time until the queue is empty.
  588. while(!this->SourceQueue.empty())
  589. {
  590. // Get the next source from the queue.
  591. cmSourceFile* sf = this->SourceQueue.front();
  592. this->SourceQueue.pop();
  593. this->CurrentEntry = &this->GeneratorTarget->SourceEntries[sf];
  594. // Queue dependencies added explicitly by the user.
  595. if(const char* additionalDeps = sf->GetProperty("OBJECT_DEPENDS"))
  596. {
  597. std::vector<std::string> objDeps;
  598. cmSystemTools::ExpandListArgument(additionalDeps, objDeps);
  599. for(std::vector<std::string>::iterator odi = objDeps.begin();
  600. odi != objDeps.end(); ++odi)
  601. {
  602. if (cmSystemTools::FileIsFullPath(*odi))
  603. {
  604. *odi = cmSystemTools::CollapseFullPath(*odi);
  605. }
  606. }
  607. this->FollowNames(objDeps);
  608. }
  609. // Queue the source needed to generate this file, if any.
  610. this->FollowName(sf->GetFullPath());
  611. // Queue dependencies added programatically by commands.
  612. this->FollowNames(sf->GetDepends());
  613. // Queue custom command dependencies.
  614. if(cmCustomCommand const* cc = sf->GetCustomCommand())
  615. {
  616. this->CheckCustomCommand(*cc);
  617. }
  618. }
  619. this->CurrentEntry = 0;
  620. this->Target->AddTracedSources(this->NewSources);
  621. }
  622. //----------------------------------------------------------------------------
  623. void cmTargetTraceDependencies::QueueSource(cmSourceFile* sf)
  624. {
  625. if(this->SourcesQueued.insert(sf).second)
  626. {
  627. this->SourceQueue.push(sf);
  628. // Make sure this file is in the target at the end.
  629. this->NewSources.push_back(sf->GetFullPath());
  630. }
  631. }
  632. //----------------------------------------------------------------------------
  633. void cmTargetTraceDependencies::FollowName(std::string const& name)
  634. {
  635. NameMapType::iterator i = this->NameMap.find(name);
  636. if(i == this->NameMap.end())
  637. {
  638. // Check if we know how to generate this file.
  639. cmSourceFile* sf = this->Makefile->GetSourceFileWithOutput(name);
  640. NameMapType::value_type entry(name, sf);
  641. i = this->NameMap.insert(entry).first;
  642. }
  643. if(cmSourceFile* sf = i->second)
  644. {
  645. // Record the dependency we just followed.
  646. if(this->CurrentEntry)
  647. {
  648. this->CurrentEntry->Depends.push_back(sf);
  649. }
  650. this->QueueSource(sf);
  651. }
  652. }
  653. //----------------------------------------------------------------------------
  654. void
  655. cmTargetTraceDependencies::FollowNames(std::vector<std::string> const& names)
  656. {
  657. for(std::vector<std::string>::const_iterator i = names.begin();
  658. i != names.end(); ++i)
  659. {
  660. this->FollowName(*i);
  661. }
  662. }
  663. //----------------------------------------------------------------------------
  664. bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
  665. {
  666. // Dependencies on targets (utilities) are supposed to be named by
  667. // just the target name. However for compatibility we support
  668. // naming the output file generated by the target (assuming there is
  669. // no output-name property which old code would not have set). In
  670. // that case the target name will be the file basename of the
  671. // dependency.
  672. std::string util = cmSystemTools::GetFilenameName(dep);
  673. if(cmSystemTools::GetFilenameLastExtension(util) == ".exe")
  674. {
  675. util = cmSystemTools::GetFilenameWithoutLastExtension(util);
  676. }
  677. // Check for a target with this name.
  678. if(cmTarget* t = this->Makefile->FindTargetToUse(util))
  679. {
  680. // If we find the target and the dep was given as a full path,
  681. // then make sure it was not a full path to something else, and
  682. // the fact that the name matched a target was just a coincidence.
  683. if(cmSystemTools::FileIsFullPath(dep.c_str()))
  684. {
  685. if(t->GetType() >= cmTarget::EXECUTABLE &&
  686. t->GetType() <= cmTarget::MODULE_LIBRARY)
  687. {
  688. // This is really only for compatibility so we do not need to
  689. // worry about configuration names and output names.
  690. std::string tLocation = t->GetLocationForBuild();
  691. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  692. std::string depLocation = cmSystemTools::GetFilenamePath(dep);
  693. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  694. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  695. if(depLocation == tLocation)
  696. {
  697. this->Target->AddUtility(util);
  698. return true;
  699. }
  700. }
  701. }
  702. else
  703. {
  704. // The original name of the dependency was not a full path. It
  705. // must name a target, so add the target-level dependency.
  706. this->Target->AddUtility(util);
  707. return true;
  708. }
  709. }
  710. // The dependency does not name a target built in this project.
  711. return false;
  712. }
  713. //----------------------------------------------------------------------------
  714. void
  715. cmTargetTraceDependencies
  716. ::CheckCustomCommand(cmCustomCommand const& cc)
  717. {
  718. // Transform command names that reference targets built in this
  719. // project to corresponding target-level dependencies.
  720. cmGeneratorExpression ge(&cc.GetBacktrace());
  721. // Add target-level dependencies referenced by generator expressions.
  722. std::set<cmTarget*> targets;
  723. for(cmCustomCommandLines::const_iterator cit = cc.GetCommandLines().begin();
  724. cit != cc.GetCommandLines().end(); ++cit)
  725. {
  726. std::string const& command = *cit->begin();
  727. // Check for a target with this name.
  728. if(cmTarget* t = this->Makefile->FindTargetToUse(command))
  729. {
  730. if(t->GetType() == cmTarget::EXECUTABLE)
  731. {
  732. // The command refers to an executable target built in
  733. // this project. Add the target-level dependency to make
  734. // sure the executable is up to date before this custom
  735. // command possibly runs.
  736. this->Target->AddUtility(command);
  737. }
  738. }
  739. // Check for target references in generator expressions.
  740. for(cmCustomCommandLine::const_iterator cli = cit->begin();
  741. cli != cit->end(); ++cli)
  742. {
  743. const cmsys::auto_ptr<cmCompiledGeneratorExpression> cge
  744. = ge.Parse(*cli);
  745. cge->Evaluate(this->Makefile, "", true);
  746. std::set<cmTarget*> geTargets = cge->GetTargets();
  747. targets.insert(geTargets.begin(), geTargets.end());
  748. }
  749. }
  750. for(std::set<cmTarget*>::iterator ti = targets.begin();
  751. ti != targets.end(); ++ti)
  752. {
  753. this->Target->AddUtility((*ti)->GetName());
  754. }
  755. // Queue the custom command dependencies.
  756. std::vector<std::string> configs;
  757. std::set<std::string> emitted;
  758. this->Makefile->GetConfigurations(configs);
  759. if (configs.empty())
  760. {
  761. configs.push_back("");
  762. }
  763. for(std::vector<std::string>::const_iterator ci = configs.begin();
  764. ci != configs.end(); ++ci)
  765. {
  766. this->FollowCommandDepends(cc, *ci, emitted);
  767. }
  768. }
  769. //----------------------------------------------------------------------------
  770. void cmTargetTraceDependencies::FollowCommandDepends(cmCustomCommand const& cc,
  771. const std::string& config,
  772. std::set<std::string>& emitted)
  773. {
  774. cmCustomCommandGenerator ccg(cc, config, this->Makefile);
  775. const std::vector<std::string>& depends = ccg.GetDepends();
  776. for(std::vector<std::string>::const_iterator di = depends.begin();
  777. di != depends.end(); ++di)
  778. {
  779. std::string const& dep = *di;
  780. if(emitted.insert(dep).second)
  781. {
  782. if(!this->IsUtility(dep))
  783. {
  784. // The dependency does not name a target and may be a file we
  785. // know how to generate. Queue it.
  786. this->FollowName(dep);
  787. }
  788. }
  789. }
  790. }
  791. //----------------------------------------------------------------------------
  792. void
  793. cmTargetTraceDependencies
  794. ::CheckCustomCommands(const std::vector<cmCustomCommand>& commands)
  795. {
  796. for(std::vector<cmCustomCommand>::const_iterator cli = commands.begin();
  797. cli != commands.end(); ++cli)
  798. {
  799. this->CheckCustomCommand(*cli);
  800. }
  801. }
  802. //----------------------------------------------------------------------------
  803. void cmGeneratorTarget::TraceDependencies()
  804. {
  805. // CMake-generated targets have no dependencies to trace. Normally tracing
  806. // would find nothing anyway, but when building CMake itself the "install"
  807. // target command ends up referencing the "cmake" target but we do not
  808. // really want the dependency because "install" depend on "all" anyway.
  809. if(this->GetType() == cmTarget::GLOBAL_TARGET)
  810. {
  811. return;
  812. }
  813. // Use a helper object to trace the dependencies.
  814. cmTargetTraceDependencies tracer(this);
  815. tracer.Trace();
  816. }
  817. //----------------------------------------------------------------------------
  818. void cmGeneratorTarget::GetAppleArchs(const std::string& config,
  819. std::vector<std::string>& archVec) const
  820. {
  821. const char* archs = 0;
  822. if(!config.empty())
  823. {
  824. std::string defVarName = "OSX_ARCHITECTURES_";
  825. defVarName += cmSystemTools::UpperCase(config);
  826. archs = this->Target->GetProperty(defVarName);
  827. }
  828. if(!archs)
  829. {
  830. archs = this->Target->GetProperty("OSX_ARCHITECTURES");
  831. }
  832. if(archs)
  833. {
  834. cmSystemTools::ExpandListArgument(std::string(archs), archVec);
  835. }
  836. }
  837. //----------------------------------------------------------------------------
  838. std::string
  839. cmGeneratorTarget::GetCreateRuleVariable(std::string const& lang,
  840. std::string const& config) const
  841. {
  842. switch(this->GetType())
  843. {
  844. case cmTarget::STATIC_LIBRARY:
  845. {
  846. std::string var = "CMAKE_" + lang + "_CREATE_STATIC_LIBRARY";
  847. if(this->Target->GetFeatureAsBool(
  848. "INTERPROCEDURAL_OPTIMIZATION", config))
  849. {
  850. std::string varIPO = var + "_IPO";
  851. if(this->Makefile->GetDefinition(varIPO))
  852. {
  853. return varIPO;
  854. }
  855. }
  856. return var;
  857. }
  858. case cmTarget::SHARED_LIBRARY:
  859. return "CMAKE_" + lang + "_CREATE_SHARED_LIBRARY";
  860. case cmTarget::MODULE_LIBRARY:
  861. return "CMAKE_" + lang + "_CREATE_SHARED_MODULE";
  862. case cmTarget::EXECUTABLE:
  863. return "CMAKE_" + lang + "_LINK_EXECUTABLE";
  864. default:
  865. break;
  866. }
  867. return "";
  868. }
  869. //----------------------------------------------------------------------------
  870. std::vector<std::string>
  871. cmGeneratorTarget::GetIncludeDirectories(const std::string& config) const
  872. {
  873. return this->Target->GetIncludeDirectories(config);
  874. }
  875. //----------------------------------------------------------------------------
  876. void cmGeneratorTarget::GenerateTargetManifest(
  877. const std::string& config) const
  878. {
  879. if (this->Target->IsImported())
  880. {
  881. return;
  882. }
  883. cmMakefile* mf = this->Target->GetMakefile();
  884. cmLocalGenerator* lg = mf->GetLocalGenerator();
  885. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  886. // Get the names.
  887. std::string name;
  888. std::string soName;
  889. std::string realName;
  890. std::string impName;
  891. std::string pdbName;
  892. if(this->GetType() == cmTarget::EXECUTABLE)
  893. {
  894. this->Target->GetExecutableNames(name, realName, impName, pdbName,
  895. config);
  896. }
  897. else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
  898. this->GetType() == cmTarget::SHARED_LIBRARY ||
  899. this->GetType() == cmTarget::MODULE_LIBRARY)
  900. {
  901. this->Target->GetLibraryNames(name, soName, realName, impName, pdbName,
  902. config);
  903. }
  904. else
  905. {
  906. return;
  907. }
  908. // Get the directory.
  909. std::string dir = this->Target->GetDirectory(config, false);
  910. // Add each name.
  911. std::string f;
  912. if(!name.empty())
  913. {
  914. f = dir;
  915. f += "/";
  916. f += name;
  917. gg->AddToManifest(config, f);
  918. }
  919. if(!soName.empty())
  920. {
  921. f = dir;
  922. f += "/";
  923. f += soName;
  924. gg->AddToManifest(config, f);
  925. }
  926. if(!realName.empty())
  927. {
  928. f = dir;
  929. f += "/";
  930. f += realName;
  931. gg->AddToManifest(config, f);
  932. }
  933. if(!pdbName.empty())
  934. {
  935. f = dir;
  936. f += "/";
  937. f += pdbName;
  938. gg->AddToManifest(config, f);
  939. }
  940. if(!impName.empty())
  941. {
  942. f = this->Target->GetDirectory(config, true);
  943. f += "/";
  944. f += impName;
  945. gg->AddToManifest(config, f);
  946. }
  947. }
  948. bool cmStrictTargetComparison::operator()(cmTarget const* t1,
  949. cmTarget const* t2) const
  950. {
  951. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  952. if (nameResult == 0)
  953. {
  954. return strcmp(t1->GetMakefile()->GetStartOutputDirectory(),
  955. t2->GetMakefile()->GetStartOutputDirectory()) < 0;
  956. }
  957. return nameResult < 0;
  958. }
  959. //----------------------------------------------------------------------------
  960. struct cmGeneratorTarget::SourceFileFlags
  961. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  962. {
  963. struct SourceFileFlags flags;
  964. this->ConstructSourceFileFlags();
  965. std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
  966. this->SourceFlagsMap.find(sf);
  967. if(si != this->SourceFlagsMap.end())
  968. {
  969. flags = si->second;
  970. }
  971. else
  972. {
  973. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  974. // were not listed in one of the other lists.
  975. if(const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION"))
  976. {
  977. flags.MacFolder = location;
  978. if(strcmp(location, "Resources") == 0)
  979. {
  980. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  981. }
  982. else
  983. {
  984. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  985. }
  986. }
  987. }
  988. return flags;
  989. }
  990. //----------------------------------------------------------------------------
  991. void cmGeneratorTarget::ConstructSourceFileFlags() const
  992. {
  993. if(this->SourceFileFlagsConstructed)
  994. {
  995. return;
  996. }
  997. this->SourceFileFlagsConstructed = true;
  998. // Process public headers to mark the source files.
  999. if(const char* files = this->Target->GetProperty("PUBLIC_HEADER"))
  1000. {
  1001. std::vector<std::string> relFiles;
  1002. cmSystemTools::ExpandListArgument(files, relFiles);
  1003. for(std::vector<std::string>::iterator it = relFiles.begin();
  1004. it != relFiles.end(); ++it)
  1005. {
  1006. if(cmSourceFile* sf = this->Makefile->GetSource(*it))
  1007. {
  1008. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  1009. flags.MacFolder = "Headers";
  1010. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  1011. }
  1012. }
  1013. }
  1014. // Process private headers after public headers so that they take
  1015. // precedence if a file is listed in both.
  1016. if(const char* files = this->Target->GetProperty("PRIVATE_HEADER"))
  1017. {
  1018. std::vector<std::string> relFiles;
  1019. cmSystemTools::ExpandListArgument(files, relFiles);
  1020. for(std::vector<std::string>::iterator it = relFiles.begin();
  1021. it != relFiles.end(); ++it)
  1022. {
  1023. if(cmSourceFile* sf = this->Makefile->GetSource(*it))
  1024. {
  1025. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  1026. flags.MacFolder = "PrivateHeaders";
  1027. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  1028. }
  1029. }
  1030. }
  1031. // Mark sources listed as resources.
  1032. if(const char* files = this->Target->GetProperty("RESOURCE"))
  1033. {
  1034. std::vector<std::string> relFiles;
  1035. cmSystemTools::ExpandListArgument(files, relFiles);
  1036. for(std::vector<std::string>::iterator it = relFiles.begin();
  1037. it != relFiles.end(); ++it)
  1038. {
  1039. if(cmSourceFile* sf = this->Makefile->GetSource(*it))
  1040. {
  1041. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  1042. flags.MacFolder = "Resources";
  1043. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  1044. }
  1045. }
  1046. }
  1047. }