cmGeneratorTarget.cxx 34 KB

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