cmGeneratorTarget.cxx 34 KB

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