cmGeneratorTarget.cxx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  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. }
  287. //----------------------------------------------------------------------------
  288. const std::string& cmGeneratorTarget::GetObjectName(cmSourceFile const* file)
  289. {
  290. return this->Objects[file];
  291. }
  292. void cmGeneratorTarget::AddObject(cmSourceFile const* sf,
  293. std::string const&name)
  294. {
  295. this->Objects[sf] = name;
  296. }
  297. //----------------------------------------------------------------------------
  298. void cmGeneratorTarget::AddExplicitObjectName(cmSourceFile const* sf)
  299. {
  300. this->ExplicitObjectName.insert(sf);
  301. }
  302. //----------------------------------------------------------------------------
  303. bool cmGeneratorTarget::HasExplicitObjectName(cmSourceFile const* file) const
  304. {
  305. std::set<cmSourceFile const*>::const_iterator it
  306. = this->ExplicitObjectName.find(file);
  307. return it != this->ExplicitObjectName.end();
  308. }
  309. //----------------------------------------------------------------------------
  310. void cmGeneratorTarget
  311. ::GetIDLSources(std::vector<cmSourceFile const*>& data) const
  312. {
  313. IMPLEMENT_VISIT(IDLSources);
  314. }
  315. //----------------------------------------------------------------------------
  316. void
  317. cmGeneratorTarget
  318. ::GetHeaderSources(std::vector<cmSourceFile const*>& data) const
  319. {
  320. IMPLEMENT_VISIT(HeaderSources);
  321. }
  322. //----------------------------------------------------------------------------
  323. void cmGeneratorTarget
  324. ::GetExtraSources(std::vector<cmSourceFile const*>& data) const
  325. {
  326. IMPLEMENT_VISIT(ExtraSources);
  327. }
  328. //----------------------------------------------------------------------------
  329. void
  330. cmGeneratorTarget
  331. ::GetCustomCommands(std::vector<cmSourceFile const*>& data) const
  332. {
  333. IMPLEMENT_VISIT(CustomCommands);
  334. }
  335. //----------------------------------------------------------------------------
  336. void
  337. cmGeneratorTarget
  338. ::GetExternalObjects(std::vector<cmSourceFile const*>& data) const
  339. {
  340. IMPLEMENT_VISIT(ExternalObjects);
  341. }
  342. //----------------------------------------------------------------------------
  343. void
  344. cmGeneratorTarget::GetExpectedResxHeaders(std::set<std::string>& srcs) const
  345. {
  346. ResxData data;
  347. IMPLEMENT_VISIT_IMPL(Resx, COMMA cmGeneratorTarget::ResxData)
  348. srcs = data.ExpectedResxHeaders;
  349. }
  350. //----------------------------------------------------------------------------
  351. void cmGeneratorTarget
  352. ::GetResxSources(std::vector<cmSourceFile const*>& srcs) const
  353. {
  354. ResxData data;
  355. IMPLEMENT_VISIT_IMPL(Resx, COMMA cmGeneratorTarget::ResxData)
  356. srcs = data.ResxSources;
  357. }
  358. //----------------------------------------------------------------------------
  359. bool cmGeneratorTarget::IsSystemIncludeDirectory(const std::string& dir,
  360. const std::string& config) const
  361. {
  362. assert(this->GetType() != cmTarget::INTERFACE_LIBRARY);
  363. std::string config_upper;
  364. if(!config.empty())
  365. {
  366. config_upper = cmSystemTools::UpperCase(config);
  367. }
  368. typedef std::map<std::string, std::vector<std::string> > IncludeCacheType;
  369. IncludeCacheType::const_iterator iter =
  370. this->SystemIncludesCache.find(config_upper);
  371. if (iter == this->SystemIncludesCache.end())
  372. {
  373. cmTarget::LinkImplementation const* impl
  374. = this->Target->GetLinkImplementation(config, this->Target);
  375. if(!impl)
  376. {
  377. return false;
  378. }
  379. cmListFileBacktrace lfbt;
  380. cmGeneratorExpressionDAGChecker dagChecker(lfbt,
  381. this->GetName(),
  382. "SYSTEM_INCLUDE_DIRECTORIES", 0, 0);
  383. bool excludeImported
  384. = this->Target->GetPropertyAsBool("NO_SYSTEM_FROM_IMPORTED");
  385. std::vector<std::string> result;
  386. for (std::set<std::string>::const_iterator
  387. it = this->Target->GetSystemIncludeDirectories().begin();
  388. it != this->Target->GetSystemIncludeDirectories().end(); ++it)
  389. {
  390. cmGeneratorExpression ge(lfbt);
  391. cmSystemTools::ExpandListArgument(ge.Parse(*it)
  392. ->Evaluate(this->Makefile,
  393. config, false, this->Target,
  394. &dagChecker), result);
  395. }
  396. std::set<cmTarget*> uniqueDeps;
  397. for(std::vector<std::string>::const_iterator li = impl->Libraries.begin();
  398. li != impl->Libraries.end(); ++li)
  399. {
  400. cmTarget* tgt = this->Makefile->FindTargetToUse(*li);
  401. if (!tgt)
  402. {
  403. continue;
  404. }
  405. if (uniqueDeps.insert(tgt).second)
  406. {
  407. handleSystemIncludesDep(this->Makefile, tgt, config, this->Target,
  408. &dagChecker, result, excludeImported);
  409. std::vector<cmTarget*> deps;
  410. tgt->GetTransitivePropertyTargets(config, this->Target, deps);
  411. for(std::vector<cmTarget*>::const_iterator di = deps.begin();
  412. di != deps.end(); ++di)
  413. {
  414. if (uniqueDeps.insert(*di).second)
  415. {
  416. handleSystemIncludesDep(this->Makefile, *di, config, this->Target,
  417. &dagChecker, result, excludeImported);
  418. }
  419. }
  420. }
  421. }
  422. std::set<std::string> unique;
  423. for(std::vector<std::string>::iterator li = result.begin();
  424. li != result.end(); ++li)
  425. {
  426. cmSystemTools::ConvertToUnixSlashes(*li);
  427. unique.insert(*li);
  428. }
  429. result.clear();
  430. for(std::set<std::string>::iterator li = unique.begin();
  431. li != unique.end(); ++li)
  432. {
  433. result.push_back(*li);
  434. }
  435. IncludeCacheType::value_type entry(config_upper, result);
  436. iter = this->SystemIncludesCache.insert(entry).first;
  437. }
  438. std::string dirString = dir;
  439. return std::binary_search(iter->second.begin(), iter->second.end(),
  440. dirString);
  441. }
  442. //----------------------------------------------------------------------------
  443. bool cmGeneratorTarget::GetPropertyAsBool(const std::string& prop) const
  444. {
  445. return this->Target->GetPropertyAsBool(prop);
  446. }
  447. //----------------------------------------------------------------------------
  448. void cmGeneratorTarget::GetSourceFiles(std::vector<cmSourceFile*> &files) const
  449. {
  450. this->Target->GetSourceFiles(files);
  451. }
  452. //----------------------------------------------------------------------------
  453. void cmGeneratorTarget::LookupObjectLibraries()
  454. {
  455. std::vector<std::string> const& objLibs =
  456. this->Target->GetObjectLibraries();
  457. for(std::vector<std::string>::const_iterator oli = objLibs.begin();
  458. oli != objLibs.end(); ++oli)
  459. {
  460. std::string const& objLibName = *oli;
  461. if(cmTarget* objLib = this->Makefile->FindTargetToUse(objLibName))
  462. {
  463. if(objLib->GetType() == cmTarget::OBJECT_LIBRARY)
  464. {
  465. if(this->Target->GetType() != cmTarget::EXECUTABLE &&
  466. this->Target->GetType() != cmTarget::STATIC_LIBRARY &&
  467. this->Target->GetType() != cmTarget::SHARED_LIBRARY &&
  468. this->Target->GetType() != cmTarget::MODULE_LIBRARY)
  469. {
  470. this->GlobalGenerator->GetCMakeInstance()
  471. ->IssueMessage(cmake::FATAL_ERROR,
  472. "Only executables and non-OBJECT libraries may "
  473. "reference target objects.",
  474. this->Target->GetBacktrace());
  475. return;
  476. }
  477. this->ObjectLibraries.push_back(objLib);
  478. }
  479. else
  480. {
  481. cmOStringStream e;
  482. e << "Objects of target \"" << objLibName
  483. << "\" referenced but is not an OBJECT library.";
  484. this->GlobalGenerator->GetCMakeInstance()
  485. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  486. this->Target->GetBacktrace());
  487. return;
  488. }
  489. }
  490. else
  491. {
  492. cmOStringStream e;
  493. e << "Objects of target \"" << objLibName
  494. << "\" referenced but no such target exists.";
  495. this->GlobalGenerator->GetCMakeInstance()
  496. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  497. this->Target->GetBacktrace());
  498. return;
  499. }
  500. }
  501. }
  502. //----------------------------------------------------------------------------
  503. std::string cmGeneratorTarget::GetModuleDefinitionFile() const
  504. {
  505. std::string data;
  506. IMPLEMENT_VISIT_IMPL(ModuleDefinitionFile, COMMA std::string)
  507. return data;
  508. }
  509. //----------------------------------------------------------------------------
  510. void
  511. cmGeneratorTarget::UseObjectLibraries(std::vector<std::string>& objs) const
  512. {
  513. for(std::vector<cmTarget*>::const_iterator
  514. ti = this->ObjectLibraries.begin();
  515. ti != this->ObjectLibraries.end(); ++ti)
  516. {
  517. cmTarget* objLib = *ti;
  518. cmGeneratorTarget* ogt =
  519. this->GlobalGenerator->GetGeneratorTarget(objLib);
  520. std::vector<cmSourceFile const*> objectSources;
  521. ogt->GetObjectSources(objectSources);
  522. for(std::vector<cmSourceFile const*>::const_iterator
  523. si = objectSources.begin();
  524. si != objectSources.end(); ++si)
  525. {
  526. std::string obj = ogt->ObjectDirectory;
  527. obj += ogt->Objects[*si];
  528. objs.push_back(obj);
  529. }
  530. }
  531. }
  532. //----------------------------------------------------------------------------
  533. class cmTargetTraceDependencies
  534. {
  535. public:
  536. cmTargetTraceDependencies(cmGeneratorTarget* target);
  537. void Trace();
  538. private:
  539. cmTarget* Target;
  540. cmGeneratorTarget* GeneratorTarget;
  541. cmMakefile* Makefile;
  542. cmGlobalGenerator const* GlobalGenerator;
  543. typedef cmGeneratorTarget::SourceEntry SourceEntry;
  544. SourceEntry* CurrentEntry;
  545. std::queue<std::string> SourceQueue;
  546. std::set<std::string> SourcesQueued;
  547. typedef std::map<std::string, cmSourceFile*> NameMapType;
  548. NameMapType NameMap;
  549. void QueueSource(std::string const& name);
  550. void FollowName(std::string const& name);
  551. void FollowNames(std::vector<std::string> const& names);
  552. bool IsUtility(std::string const& dep);
  553. void CheckCustomCommand(cmCustomCommand const& cc);
  554. void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
  555. void FollowCommandDepends(cmCustomCommand const& cc,
  556. const std::string& config,
  557. std::set<std::string>& emitted);
  558. };
  559. //----------------------------------------------------------------------------
  560. cmTargetTraceDependencies
  561. ::cmTargetTraceDependencies(cmGeneratorTarget* target):
  562. Target(target->Target), GeneratorTarget(target)
  563. {
  564. // Convenience.
  565. this->Makefile = this->Target->GetMakefile();
  566. this->GlobalGenerator =
  567. this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
  568. this->CurrentEntry = 0;
  569. // Queue all the source files already specified for the target.
  570. if (this->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  571. {
  572. std::vector<std::string> sources;
  573. this->Target->GetSourceFiles(sources);
  574. for(std::vector<std::string>::const_iterator si = sources.begin();
  575. si != sources.end(); ++si)
  576. {
  577. this->QueueSource(*si);
  578. }
  579. }
  580. // Queue pre-build, pre-link, and post-build rule dependencies.
  581. this->CheckCustomCommands(this->Target->GetPreBuildCommands());
  582. this->CheckCustomCommands(this->Target->GetPreLinkCommands());
  583. this->CheckCustomCommands(this->Target->GetPostBuildCommands());
  584. }
  585. //----------------------------------------------------------------------------
  586. void cmTargetTraceDependencies::Trace()
  587. {
  588. // Process one dependency at a time until the queue is empty.
  589. while(!this->SourceQueue.empty())
  590. {
  591. // Get the next source from the queue.
  592. std::string src = this->SourceQueue.front();
  593. cmSourceFile* sf = this->Makefile->GetSource(src);
  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. }
  615. //----------------------------------------------------------------------------
  616. void cmTargetTraceDependencies::QueueSource(std::string const& name)
  617. {
  618. if(this->SourcesQueued.insert(name).second)
  619. {
  620. this->SourceQueue.push(name);
  621. // Make sure this file is in the target.
  622. this->Target->AddSource(name);
  623. }
  624. }
  625. //----------------------------------------------------------------------------
  626. void cmTargetTraceDependencies::FollowName(std::string const& name)
  627. {
  628. NameMapType::iterator i = this->NameMap.find(name);
  629. if(i == this->NameMap.end())
  630. {
  631. // Check if we know how to generate this file.
  632. cmSourceFile* sf = this->Makefile->GetSourceFileWithOutput(name);
  633. NameMapType::value_type entry(name, sf);
  634. i = this->NameMap.insert(entry).first;
  635. }
  636. if(cmSourceFile* sf = i->second)
  637. {
  638. // Record the dependency we just followed.
  639. if(this->CurrentEntry)
  640. {
  641. this->CurrentEntry->Depends.push_back(sf);
  642. }
  643. this->QueueSource(sf->GetFullPath());
  644. }
  645. }
  646. //----------------------------------------------------------------------------
  647. void
  648. cmTargetTraceDependencies::FollowNames(std::vector<std::string> const& names)
  649. {
  650. for(std::vector<std::string>::const_iterator i = names.begin();
  651. i != names.end(); ++i)
  652. {
  653. this->FollowName(*i);
  654. }
  655. }
  656. //----------------------------------------------------------------------------
  657. bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
  658. {
  659. // Dependencies on targets (utilities) are supposed to be named by
  660. // just the target name. However for compatibility we support
  661. // naming the output file generated by the target (assuming there is
  662. // no output-name property which old code would not have set). In
  663. // that case the target name will be the file basename of the
  664. // dependency.
  665. std::string util = cmSystemTools::GetFilenameName(dep);
  666. if(cmSystemTools::GetFilenameLastExtension(util) == ".exe")
  667. {
  668. util = cmSystemTools::GetFilenameWithoutLastExtension(util);
  669. }
  670. // Check for a target with this name.
  671. if(cmTarget* t = this->Makefile->FindTargetToUse(util))
  672. {
  673. // If we find the target and the dep was given as a full path,
  674. // then make sure it was not a full path to something else, and
  675. // the fact that the name matched a target was just a coincidence.
  676. if(cmSystemTools::FileIsFullPath(dep.c_str()))
  677. {
  678. if(t->GetType() >= cmTarget::EXECUTABLE &&
  679. t->GetType() <= cmTarget::MODULE_LIBRARY)
  680. {
  681. // This is really only for compatibility so we do not need to
  682. // worry about configuration names and output names.
  683. std::string tLocation = t->GetLocationForBuild();
  684. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  685. std::string depLocation = cmSystemTools::GetFilenamePath(dep);
  686. depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
  687. tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
  688. if(depLocation == tLocation)
  689. {
  690. this->Target->AddUtility(util);
  691. return true;
  692. }
  693. }
  694. }
  695. else
  696. {
  697. // The original name of the dependency was not a full path. It
  698. // must name a target, so add the target-level dependency.
  699. this->Target->AddUtility(util);
  700. return true;
  701. }
  702. }
  703. // The dependency does not name a target built in this project.
  704. return false;
  705. }
  706. //----------------------------------------------------------------------------
  707. void
  708. cmTargetTraceDependencies
  709. ::CheckCustomCommand(cmCustomCommand const& cc)
  710. {
  711. // Transform command names that reference targets built in this
  712. // project to corresponding target-level dependencies.
  713. cmGeneratorExpression ge(cc.GetBacktrace());
  714. // Add target-level dependencies referenced by generator expressions.
  715. std::set<cmTarget*> targets;
  716. for(cmCustomCommandLines::const_iterator cit = cc.GetCommandLines().begin();
  717. cit != cc.GetCommandLines().end(); ++cit)
  718. {
  719. std::string const& command = *cit->begin();
  720. // Check for a target with this name.
  721. if(cmTarget* t = this->Makefile->FindTargetToUse(command))
  722. {
  723. if(t->GetType() == cmTarget::EXECUTABLE)
  724. {
  725. // The command refers to an executable target built in
  726. // this project. Add the target-level dependency to make
  727. // sure the executable is up to date before this custom
  728. // command possibly runs.
  729. this->Target->AddUtility(command);
  730. }
  731. }
  732. // Check for target references in generator expressions.
  733. for(cmCustomCommandLine::const_iterator cli = cit->begin();
  734. cli != cit->end(); ++cli)
  735. {
  736. const cmsys::auto_ptr<cmCompiledGeneratorExpression> cge
  737. = ge.Parse(*cli);
  738. cge->Evaluate(this->Makefile, "", true);
  739. std::set<cmTarget*> geTargets = cge->GetTargets();
  740. for(std::set<cmTarget*>::const_iterator it = geTargets.begin();
  741. it != geTargets.end(); ++it)
  742. {
  743. targets.insert(*it);
  744. }
  745. }
  746. }
  747. for(std::set<cmTarget*>::iterator ti = targets.begin();
  748. ti != targets.end(); ++ti)
  749. {
  750. this->Target->AddUtility((*ti)->GetName());
  751. }
  752. // Queue the custom command dependencies.
  753. std::vector<std::string> configs;
  754. std::set<std::string> emitted;
  755. this->Makefile->GetConfigurations(configs);
  756. if (configs.empty())
  757. {
  758. configs.push_back("");
  759. }
  760. for(std::vector<std::string>::const_iterator ci = configs.begin();
  761. ci != configs.end(); ++ci)
  762. {
  763. this->FollowCommandDepends(cc, *ci, emitted);
  764. }
  765. }
  766. //----------------------------------------------------------------------------
  767. void cmTargetTraceDependencies::FollowCommandDepends(cmCustomCommand const& cc,
  768. const std::string& config,
  769. std::set<std::string>& emitted)
  770. {
  771. cmCustomCommandGenerator ccg(cc, config, this->Makefile);
  772. const std::vector<std::string>& depends = ccg.GetDepends();
  773. for(std::vector<std::string>::const_iterator di = depends.begin();
  774. di != depends.end(); ++di)
  775. {
  776. std::string const& dep = *di;
  777. if(emitted.insert(dep).second)
  778. {
  779. if(!this->IsUtility(dep))
  780. {
  781. // The dependency does not name a target and may be a file we
  782. // know how to generate. Queue it.
  783. this->FollowName(dep);
  784. }
  785. }
  786. }
  787. }
  788. //----------------------------------------------------------------------------
  789. void
  790. cmTargetTraceDependencies
  791. ::CheckCustomCommands(const std::vector<cmCustomCommand>& commands)
  792. {
  793. for(std::vector<cmCustomCommand>::const_iterator cli = commands.begin();
  794. cli != commands.end(); ++cli)
  795. {
  796. this->CheckCustomCommand(*cli);
  797. }
  798. }
  799. //----------------------------------------------------------------------------
  800. void cmGeneratorTarget::TraceDependencies()
  801. {
  802. // CMake-generated targets have no dependencies to trace. Normally tracing
  803. // would find nothing anyway, but when building CMake itself the "install"
  804. // target command ends up referencing the "cmake" target but we do not
  805. // really want the dependency because "install" depend on "all" anyway.
  806. if(this->GetType() == cmTarget::GLOBAL_TARGET)
  807. {
  808. return;
  809. }
  810. // Use a helper object to trace the dependencies.
  811. cmTargetTraceDependencies tracer(this);
  812. tracer.Trace();
  813. }
  814. //----------------------------------------------------------------------------
  815. void cmGeneratorTarget::GetAppleArchs(const std::string& config,
  816. std::vector<std::string>& archVec) const
  817. {
  818. const char* archs = 0;
  819. if(!config.empty())
  820. {
  821. std::string defVarName = "OSX_ARCHITECTURES_";
  822. defVarName += cmSystemTools::UpperCase(config);
  823. archs = this->Target->GetProperty(defVarName);
  824. }
  825. if(!archs)
  826. {
  827. archs = this->Target->GetProperty("OSX_ARCHITECTURES");
  828. }
  829. if(archs)
  830. {
  831. cmSystemTools::ExpandListArgument(std::string(archs), archVec);
  832. }
  833. }
  834. //----------------------------------------------------------------------------
  835. const char* cmGeneratorTarget::GetCreateRuleVariable() const
  836. {
  837. switch(this->GetType())
  838. {
  839. case cmTarget::STATIC_LIBRARY:
  840. return "_CREATE_STATIC_LIBRARY";
  841. case cmTarget::SHARED_LIBRARY:
  842. return "_CREATE_SHARED_LIBRARY";
  843. case cmTarget::MODULE_LIBRARY:
  844. return "_CREATE_SHARED_MODULE";
  845. case cmTarget::EXECUTABLE:
  846. return "_LINK_EXECUTABLE";
  847. default:
  848. break;
  849. }
  850. return "";
  851. }
  852. //----------------------------------------------------------------------------
  853. std::vector<std::string>
  854. cmGeneratorTarget::GetIncludeDirectories(const std::string& config) const
  855. {
  856. return this->Target->GetIncludeDirectories(config);
  857. }
  858. //----------------------------------------------------------------------------
  859. void cmGeneratorTarget::GenerateTargetManifest(
  860. const std::string& config) const
  861. {
  862. if (this->Target->IsImported())
  863. {
  864. return;
  865. }
  866. cmMakefile* mf = this->Target->GetMakefile();
  867. cmLocalGenerator* lg = mf->GetLocalGenerator();
  868. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  869. // Get the names.
  870. std::string name;
  871. std::string soName;
  872. std::string realName;
  873. std::string impName;
  874. std::string pdbName;
  875. if(this->GetType() == cmTarget::EXECUTABLE)
  876. {
  877. this->Target->GetExecutableNames(name, realName, impName, pdbName,
  878. config);
  879. }
  880. else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
  881. this->GetType() == cmTarget::SHARED_LIBRARY ||
  882. this->GetType() == cmTarget::MODULE_LIBRARY)
  883. {
  884. this->Target->GetLibraryNames(name, soName, realName, impName, pdbName,
  885. config);
  886. }
  887. else
  888. {
  889. return;
  890. }
  891. // Get the directory.
  892. std::string dir = this->Target->GetDirectory(config, false);
  893. // Add each name.
  894. std::string f;
  895. if(!name.empty())
  896. {
  897. f = dir;
  898. f += "/";
  899. f += name;
  900. gg->AddToManifest(config, f);
  901. }
  902. if(!soName.empty())
  903. {
  904. f = dir;
  905. f += "/";
  906. f += soName;
  907. gg->AddToManifest(config, f);
  908. }
  909. if(!realName.empty())
  910. {
  911. f = dir;
  912. f += "/";
  913. f += realName;
  914. gg->AddToManifest(config, f);
  915. }
  916. if(!pdbName.empty())
  917. {
  918. f = dir;
  919. f += "/";
  920. f += pdbName;
  921. gg->AddToManifest(config, f);
  922. }
  923. if(!impName.empty())
  924. {
  925. f = this->Target->GetDirectory(config, true);
  926. f += "/";
  927. f += impName;
  928. gg->AddToManifest(config, f);
  929. }
  930. }
  931. bool cmStrictTargetComparison::operator()(cmTarget const* t1,
  932. cmTarget const* t2) const
  933. {
  934. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  935. if (nameResult == 0)
  936. {
  937. return strcmp(t1->GetMakefile()->GetStartOutputDirectory(),
  938. t2->GetMakefile()->GetStartOutputDirectory()) < 0;
  939. }
  940. return nameResult < 0;
  941. }
  942. //----------------------------------------------------------------------------
  943. struct cmGeneratorTarget::SourceFileFlags
  944. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  945. {
  946. struct SourceFileFlags flags;
  947. this->ConstructSourceFileFlags();
  948. std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
  949. this->SourceFlagsMap.find(sf);
  950. if(si != this->SourceFlagsMap.end())
  951. {
  952. flags = si->second;
  953. }
  954. else
  955. {
  956. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  957. // were not listed in one of the other lists.
  958. if(const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION"))
  959. {
  960. flags.MacFolder = location;
  961. if(strcmp(location, "Resources") == 0)
  962. {
  963. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  964. }
  965. else
  966. {
  967. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  968. }
  969. }
  970. }
  971. return flags;
  972. }
  973. //----------------------------------------------------------------------------
  974. void cmGeneratorTarget::ConstructSourceFileFlags() const
  975. {
  976. if(this->SourceFileFlagsConstructed)
  977. {
  978. return;
  979. }
  980. this->SourceFileFlagsConstructed = true;
  981. // Process public headers to mark the source files.
  982. if(const char* files = this->Target->GetProperty("PUBLIC_HEADER"))
  983. {
  984. std::vector<std::string> relFiles;
  985. cmSystemTools::ExpandListArgument(files, relFiles);
  986. for(std::vector<std::string>::iterator it = relFiles.begin();
  987. it != relFiles.end(); ++it)
  988. {
  989. if(cmSourceFile* sf = this->Makefile->GetSource(*it))
  990. {
  991. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  992. flags.MacFolder = "Headers";
  993. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  994. }
  995. }
  996. }
  997. // Process private headers after public headers so that they take
  998. // precedence if a file is listed in both.
  999. if(const char* files = this->Target->GetProperty("PRIVATE_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 = "PrivateHeaders";
  1010. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  1011. }
  1012. }
  1013. }
  1014. // Mark sources listed as resources.
  1015. if(const char* files = this->Target->GetProperty("RESOURCE"))
  1016. {
  1017. std::vector<std::string> relFiles;
  1018. cmSystemTools::ExpandListArgument(files, relFiles);
  1019. for(std::vector<std::string>::iterator it = relFiles.begin();
  1020. it != relFiles.end(); ++it)
  1021. {
  1022. if(cmSourceFile* sf = this->Makefile->GetSource(*it))
  1023. {
  1024. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  1025. flags.MacFolder = "Resources";
  1026. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  1027. }
  1028. }
  1029. }
  1030. }