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