1
0

cmGeneratorTarget.cxx 38 KB

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