cmGeneratorTarget.cxx 36 KB

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