cmGeneratorTarget.cxx 38 KB

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