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