cmGeneratorTarget.cxx 35 KB

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