cmGeneratorTarget.cxx 36 KB

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