cmGeneratorTarget.cxx 34 KB

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