cmGeneratorTarget.cxx 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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 sources that compile, header files, and "
  36. "other files that would not affect linking of a normal library.";
  37. cm->IssueMessage(cmake::FATAL_ERROR, e.str(),
  38. target->GetBacktrace());
  39. }
  40. }
  41. struct ObjectSourcesTag {};
  42. struct CustomCommandsTag {};
  43. struct ExtraSourcesTag {};
  44. struct HeaderSourcesTag {};
  45. struct ExternalObjectsTag {};
  46. struct IDLSourcesTag {};
  47. struct ResxTag {};
  48. struct ModuleDefinitionFileTag {};
  49. #if !defined(_MSC_VER) || _MSC_VER >= 1310
  50. template<typename Tag, typename OtherTag>
  51. struct IsSameTag
  52. {
  53. enum {
  54. Result = false
  55. };
  56. };
  57. template<typename Tag>
  58. struct IsSameTag<Tag, Tag>
  59. {
  60. enum {
  61. Result = true
  62. };
  63. };
  64. #else
  65. struct IsSameTagBase
  66. {
  67. typedef char (&no_type)[1];
  68. typedef char (&yes_type)[2];
  69. template<typename T> struct Check;
  70. template<typename T> static yes_type check(Check<T>*, Check<T>*);
  71. static no_type check(...);
  72. };
  73. template<typename Tag1, typename Tag2>
  74. struct IsSameTag: public IsSameTagBase
  75. {
  76. enum {
  77. Result = (sizeof(check(static_cast< Check<Tag1>* >(0),
  78. static_cast< Check<Tag2>* >(0))) ==
  79. sizeof(yes_type))
  80. };
  81. };
  82. #endif
  83. template<bool>
  84. struct DoAccept
  85. {
  86. template <typename T> static void Do(T&, cmSourceFile*) {}
  87. };
  88. template<>
  89. struct DoAccept<true>
  90. {
  91. static void Do(std::vector<cmSourceFile const*>& files, cmSourceFile* f)
  92. {
  93. files.push_back(f);
  94. }
  95. static void Do(cmGeneratorTarget::ResxData& data, cmSourceFile* f)
  96. {
  97. // Build and save the name of the corresponding .h file
  98. // This relationship will be used later when building the project files.
  99. // Both names would have been auto generated from Visual Studio
  100. // where the user supplied the file name and Visual Studio
  101. // appended the suffix.
  102. std::string resx = f->GetFullPath();
  103. std::string hFileName = resx.substr(0, resx.find_last_of(".")) + ".h";
  104. data.ExpectedResxHeaders.insert(hFileName);
  105. data.ResxSources.push_back(f);
  106. }
  107. static void Do(std::string& data, cmSourceFile* f)
  108. {
  109. data = f->GetFullPath();
  110. }
  111. };
  112. //----------------------------------------------------------------------------
  113. template<typename Tag, typename DataType = std::vector<cmSourceFile const*> >
  114. struct TagVisitor
  115. {
  116. DataType& Data;
  117. std::vector<cmSourceFile*> BadObjLibFiles;
  118. cmTarget *Target;
  119. cmGlobalGenerator *GlobalGenerator;
  120. cmsys::RegularExpression Header;
  121. bool IsObjLib;
  122. TagVisitor(cmTarget *target, DataType& data)
  123. : Data(data), Target(target),
  124. GlobalGenerator(target->GetMakefile()
  125. ->GetLocalGenerator()->GetGlobalGenerator()),
  126. Header(CM_HEADER_REGEX),
  127. IsObjLib(target->GetType() == cmTarget::OBJECT_LIBRARY)
  128. {
  129. }
  130. ~TagVisitor()
  131. {
  132. reportBadObjLib(this->BadObjLibFiles, this->Target,
  133. this->GlobalGenerator->GetCMakeInstance());
  134. }
  135. void Accept(cmSourceFile *sf)
  136. {
  137. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  138. if(sf->GetCustomCommand())
  139. {
  140. DoAccept<IsSameTag<Tag, CustomCommandsTag>::Result>::Do(this->Data, sf);
  141. }
  142. else if(this->Target->GetType() == cmTarget::UTILITY)
  143. {
  144. DoAccept<IsSameTag<Tag, ExtraSourcesTag>::Result>::Do(this->Data, sf);
  145. }
  146. else if(sf->GetPropertyAsBool("HEADER_FILE_ONLY"))
  147. {
  148. DoAccept<IsSameTag<Tag, HeaderSourcesTag>::Result>::Do(this->Data, sf);
  149. }
  150. else if(sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
  151. {
  152. DoAccept<IsSameTag<Tag, ExternalObjectsTag>::Result>::Do(this->Data, sf);
  153. if(this->IsObjLib)
  154. {
  155. this->BadObjLibFiles.push_back(sf);
  156. }
  157. }
  158. else if(!sf->GetLanguage().empty())
  159. {
  160. DoAccept<IsSameTag<Tag, ObjectSourcesTag>::Result>::Do(this->Data, sf);
  161. }
  162. else if(ext == "def")
  163. {
  164. DoAccept<IsSameTag<Tag, ModuleDefinitionFileTag>::Result>::Do(this->Data,
  165. sf);
  166. if(this->IsObjLib)
  167. {
  168. this->BadObjLibFiles.push_back(sf);
  169. }
  170. }
  171. else if(ext == "idl")
  172. {
  173. DoAccept<IsSameTag<Tag, IDLSourcesTag>::Result>::Do(this->Data, sf);
  174. if(this->IsObjLib)
  175. {
  176. this->BadObjLibFiles.push_back(sf);
  177. }
  178. }
  179. else if(ext == "resx")
  180. {
  181. DoAccept<IsSameTag<Tag, ResxTag>::Result>::Do(this->Data, sf);
  182. }
  183. else if(this->Header.find(sf->GetFullPath().c_str()))
  184. {
  185. DoAccept<IsSameTag<Tag, HeaderSourcesTag>::Result>::Do(this->Data, sf);
  186. }
  187. else if(this->GlobalGenerator->IgnoreFile(sf->GetExtension().c_str()))
  188. {
  189. DoAccept<IsSameTag<Tag, ExtraSourcesTag>::Result>::Do(this->Data, sf);
  190. }
  191. else
  192. {
  193. DoAccept<IsSameTag<Tag, ExtraSourcesTag>::Result>::Do(this->Data, sf);
  194. }
  195. }
  196. };
  197. //----------------------------------------------------------------------------
  198. cmGeneratorTarget::cmGeneratorTarget(cmTarget* t): Target(t),
  199. SourceFileFlagsConstructed(false)
  200. {
  201. this->Makefile = this->Target->GetMakefile();
  202. this->LocalGenerator = this->Makefile->GetLocalGenerator();
  203. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  204. }
  205. //----------------------------------------------------------------------------
  206. int cmGeneratorTarget::GetType() const
  207. {
  208. return this->Target->GetType();
  209. }
  210. //----------------------------------------------------------------------------
  211. std::string cmGeneratorTarget::GetName() const
  212. {
  213. return this->Target->GetName();
  214. }
  215. //----------------------------------------------------------------------------
  216. const char *cmGeneratorTarget::GetProperty(const std::string& prop) const
  217. {
  218. return this->Target->GetProperty(prop);
  219. }
  220. //----------------------------------------------------------------------------
  221. std::vector<cmSourceFile*> const*
  222. cmGeneratorTarget::GetSourceDepends(cmSourceFile const* sf) const
  223. {
  224. SourceEntriesType::const_iterator i = this->SourceEntries.find(sf);
  225. if(i != this->SourceEntries.end())
  226. {
  227. return &i->second.Depends;
  228. }
  229. return 0;
  230. }
  231. static void handleSystemIncludesDep(cmMakefile *mf, cmTarget const* depTgt,
  232. const std::string& config,
  233. cmTarget *headTarget,
  234. cmGeneratorExpressionDAGChecker *dagChecker,
  235. std::vector<std::string>& result,
  236. bool excludeImported)
  237. {
  238. if (const char* dirs =
  239. depTgt->GetProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES"))
  240. {
  241. cmGeneratorExpression ge;
  242. cmSystemTools::ExpandListArgument(ge.Parse(dirs)
  243. ->Evaluate(mf,
  244. config, false, headTarget,
  245. depTgt, dagChecker), result);
  246. }
  247. if (!depTgt->IsImported() || excludeImported)
  248. {
  249. return;
  250. }
  251. if (const char* dirs =
  252. depTgt->GetProperty("INTERFACE_INCLUDE_DIRECTORIES"))
  253. {
  254. cmGeneratorExpression ge;
  255. cmSystemTools::ExpandListArgument(ge.Parse(dirs)
  256. ->Evaluate(mf,
  257. config, false, headTarget,
  258. depTgt, dagChecker), result);
  259. }
  260. }
  261. #define IMPLEMENT_VISIT_IMPL(DATA, DATATYPE) \
  262. { \
  263. std::vector<cmSourceFile*> sourceFiles; \
  264. this->Target->GetSourceFiles(sourceFiles, config); \
  265. TagVisitor<DATA ## Tag DATATYPE> visitor(this->Target, data); \
  266. for(std::vector<cmSourceFile*>::const_iterator si = sourceFiles.begin(); \
  267. si != sourceFiles.end(); ++si) \
  268. { \
  269. visitor.Accept(*si); \
  270. } \
  271. } \
  272. #define IMPLEMENT_VISIT(DATA) \
  273. IMPLEMENT_VISIT_IMPL(DATA, EMPTY) \
  274. #define EMPTY
  275. #define COMMA ,
  276. //----------------------------------------------------------------------------
  277. void
  278. cmGeneratorTarget
  279. ::GetObjectSources(std::vector<cmSourceFile const*> &data,
  280. const std::string& config) const
  281. {
  282. IMPLEMENT_VISIT(ObjectSources);
  283. if (!this->Objects.empty())
  284. {
  285. return;
  286. }
  287. for(std::vector<cmSourceFile const*>::const_iterator it = data.begin();
  288. it != data.end(); ++it)
  289. {
  290. this->Objects[*it];
  291. }
  292. this->LocalGenerator->ComputeObjectFilenames(this->Objects, this);
  293. }
  294. void cmGeneratorTarget::ComputeObjectMapping()
  295. {
  296. if(!this->Objects.empty())
  297. {
  298. return;
  299. }
  300. std::vector<std::string> configs;
  301. this->Makefile->GetConfigurations(configs);
  302. if (configs.empty())
  303. {
  304. configs.push_back("");
  305. }
  306. for(std::vector<std::string>::const_iterator ci = configs.begin();
  307. ci != configs.end(); ++ci)
  308. {
  309. std::vector<cmSourceFile const*> sourceFiles;
  310. this->GetObjectSources(sourceFiles, *ci);
  311. }
  312. }
  313. //----------------------------------------------------------------------------
  314. const std::string& cmGeneratorTarget::GetObjectName(cmSourceFile const* file)
  315. {
  316. this->ComputeObjectMapping();
  317. return this->Objects[file];
  318. }
  319. //----------------------------------------------------------------------------
  320. void cmGeneratorTarget::AddExplicitObjectName(cmSourceFile const* sf)
  321. {
  322. this->ExplicitObjectName.insert(sf);
  323. }
  324. //----------------------------------------------------------------------------
  325. bool cmGeneratorTarget::HasExplicitObjectName(cmSourceFile const* file) const
  326. {
  327. const_cast<cmGeneratorTarget*>(this)->ComputeObjectMapping();
  328. std::set<cmSourceFile const*>::const_iterator it
  329. = this->ExplicitObjectName.find(file);
  330. return it != this->ExplicitObjectName.end();
  331. }
  332. //----------------------------------------------------------------------------
  333. void cmGeneratorTarget
  334. ::GetIDLSources(std::vector<cmSourceFile const*>& data,
  335. const std::string& config) const
  336. {
  337. IMPLEMENT_VISIT(IDLSources);
  338. }
  339. //----------------------------------------------------------------------------
  340. void
  341. cmGeneratorTarget
  342. ::GetHeaderSources(std::vector<cmSourceFile const*>& data,
  343. const std::string& config) const
  344. {
  345. IMPLEMENT_VISIT(HeaderSources);
  346. }
  347. //----------------------------------------------------------------------------
  348. void cmGeneratorTarget
  349. ::GetExtraSources(std::vector<cmSourceFile const*>& data,
  350. const std::string& config) const
  351. {
  352. IMPLEMENT_VISIT(ExtraSources);
  353. }
  354. //----------------------------------------------------------------------------
  355. void
  356. cmGeneratorTarget
  357. ::GetCustomCommands(std::vector<cmSourceFile const*>& data,
  358. const std::string& config) const
  359. {
  360. IMPLEMENT_VISIT(CustomCommands);
  361. }
  362. //----------------------------------------------------------------------------
  363. void
  364. cmGeneratorTarget
  365. ::GetExternalObjects(std::vector<cmSourceFile const*>& data,
  366. const std::string& config) const
  367. {
  368. IMPLEMENT_VISIT(ExternalObjects);
  369. }
  370. //----------------------------------------------------------------------------
  371. void
  372. cmGeneratorTarget::GetExpectedResxHeaders(std::set<std::string>& srcs,
  373. const std::string& config) const
  374. {
  375. ResxData data;
  376. IMPLEMENT_VISIT_IMPL(Resx, COMMA cmGeneratorTarget::ResxData)
  377. srcs = data.ExpectedResxHeaders;
  378. }
  379. //----------------------------------------------------------------------------
  380. void cmGeneratorTarget
  381. ::GetResxSources(std::vector<cmSourceFile const*>& srcs,
  382. const std::string& config) const
  383. {
  384. ResxData data;
  385. IMPLEMENT_VISIT_IMPL(Resx, COMMA cmGeneratorTarget::ResxData)
  386. srcs = data.ResxSources;
  387. }
  388. //----------------------------------------------------------------------------
  389. bool cmGeneratorTarget::IsSystemIncludeDirectory(const std::string& dir,
  390. const std::string& config) const
  391. {
  392. assert(this->GetType() != cmTarget::INTERFACE_LIBRARY);
  393. std::string config_upper;
  394. if(!config.empty())
  395. {
  396. config_upper = cmSystemTools::UpperCase(config);
  397. }
  398. typedef std::map<std::string, std::vector<std::string> > IncludeCacheType;
  399. IncludeCacheType::const_iterator iter =
  400. this->SystemIncludesCache.find(config_upper);
  401. if (iter == this->SystemIncludesCache.end())
  402. {
  403. cmGeneratorExpressionDAGChecker dagChecker(
  404. this->GetName(),
  405. "SYSTEM_INCLUDE_DIRECTORIES", 0, 0);
  406. bool excludeImported
  407. = this->Target->GetPropertyAsBool("NO_SYSTEM_FROM_IMPORTED");
  408. std::vector<std::string> result;
  409. for (std::set<std::string>::const_iterator
  410. it = this->Target->GetSystemIncludeDirectories().begin();
  411. it != this->Target->GetSystemIncludeDirectories().end(); ++it)
  412. {
  413. cmGeneratorExpression ge;
  414. cmSystemTools::ExpandListArgument(ge.Parse(*it)
  415. ->Evaluate(this->Makefile,
  416. config, false, this->Target,
  417. &dagChecker), result);
  418. }
  419. std::vector<cmTarget const*> const& deps =
  420. this->Target->GetLinkImplementationClosure(config);
  421. for(std::vector<cmTarget const*>::const_iterator
  422. li = deps.begin(), le = deps.end(); li != le; ++li)
  423. {
  424. handleSystemIncludesDep(this->Makefile, *li, config, this->Target,
  425. &dagChecker, result, excludeImported);
  426. }
  427. std::set<std::string> unique;
  428. for(std::vector<std::string>::iterator li = result.begin();
  429. li != result.end(); ++li)
  430. {
  431. cmSystemTools::ConvertToUnixSlashes(*li);
  432. unique.insert(*li);
  433. }
  434. result.clear();
  435. for(std::set<std::string>::iterator li = unique.begin();
  436. li != unique.end(); ++li)
  437. {
  438. result.push_back(*li);
  439. }
  440. IncludeCacheType::value_type entry(config_upper, result);
  441. iter = this->SystemIncludesCache.insert(entry).first;
  442. }
  443. std::string dirString = dir;
  444. return std::binary_search(iter->second.begin(), iter->second.end(),
  445. dirString);
  446. }
  447. //----------------------------------------------------------------------------
  448. bool cmGeneratorTarget::GetPropertyAsBool(const std::string& prop) const
  449. {
  450. return this->Target->GetPropertyAsBool(prop);
  451. }
  452. //----------------------------------------------------------------------------
  453. void cmGeneratorTarget::GetSourceFiles(std::vector<cmSourceFile*> &files,
  454. const std::string& config) const
  455. {
  456. this->Target->GetSourceFiles(files, config);
  457. }
  458. //----------------------------------------------------------------------------
  459. std::string
  460. cmGeneratorTarget::GetModuleDefinitionFile(const std::string& config) const
  461. {
  462. std::string data;
  463. IMPLEMENT_VISIT_IMPL(ModuleDefinitionFile, COMMA std::string)
  464. return data;
  465. }
  466. //----------------------------------------------------------------------------
  467. void
  468. cmGeneratorTarget::UseObjectLibraries(std::vector<std::string>& objs,
  469. const std::string &config) const
  470. {
  471. std::vector<cmSourceFile const*> objectFiles;
  472. this->GetExternalObjects(objectFiles, config);
  473. std::vector<cmTarget*> objectLibraries;
  474. std::set<cmTarget*> emitted;
  475. for(std::vector<cmSourceFile const*>::const_iterator
  476. it = objectFiles.begin(); it != objectFiles.end(); ++it)
  477. {
  478. std::string objLib = (*it)->GetObjectLibrary();
  479. if (cmTarget* tgt = this->Makefile->FindTargetToUse(objLib))
  480. {
  481. if (emitted.insert(tgt).second)
  482. {
  483. objectLibraries.push_back(tgt);
  484. }
  485. }
  486. }
  487. for(std::vector<cmTarget*>::const_iterator
  488. ti = objectLibraries.begin();
  489. ti != objectLibraries.end(); ++ti)
  490. {
  491. cmTarget* objLib = *ti;
  492. cmGeneratorTarget* ogt =
  493. this->GlobalGenerator->GetGeneratorTarget(objLib);
  494. std::vector<cmSourceFile const*> objectSources;
  495. ogt->GetObjectSources(objectSources, config);
  496. for(std::vector<cmSourceFile const*>::const_iterator
  497. si = objectSources.begin();
  498. si != objectSources.end(); ++si)
  499. {
  500. std::string obj = ogt->ObjectDirectory;
  501. obj += ogt->Objects[*si];
  502. objs.push_back(obj);
  503. }
  504. }
  505. }
  506. //----------------------------------------------------------------------------
  507. class cmTargetTraceDependencies
  508. {
  509. public:
  510. cmTargetTraceDependencies(cmGeneratorTarget* target);
  511. void Trace();
  512. private:
  513. cmTarget* Target;
  514. cmGeneratorTarget* GeneratorTarget;
  515. cmMakefile* Makefile;
  516. cmGlobalGenerator const* GlobalGenerator;
  517. typedef cmGeneratorTarget::SourceEntry SourceEntry;
  518. SourceEntry* CurrentEntry;
  519. std::queue<cmSourceFile*> SourceQueue;
  520. std::set<cmSourceFile*> SourcesQueued;
  521. typedef std::map<std::string, cmSourceFile*> NameMapType;
  522. NameMapType NameMap;
  523. std::vector<std::string> NewSources;
  524. void QueueSource(cmSourceFile* sf);
  525. void FollowName(std::string const& name);
  526. void FollowNames(std::vector<std::string> const& names);
  527. bool IsUtility(std::string const& dep);
  528. void CheckCustomCommand(cmCustomCommand const& cc);
  529. void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
  530. void FollowCommandDepends(cmCustomCommand const& cc,
  531. const std::string& config,
  532. std::set<std::string>& emitted);
  533. };
  534. //----------------------------------------------------------------------------
  535. cmTargetTraceDependencies
  536. ::cmTargetTraceDependencies(cmGeneratorTarget* target):
  537. Target(target->Target), GeneratorTarget(target)
  538. {
  539. // Convenience.
  540. this->Makefile = this->Target->GetMakefile();
  541. this->GlobalGenerator =
  542. this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
  543. this->CurrentEntry = 0;
  544. // Queue all the source files already specified for the target.
  545. if (this->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  546. {
  547. std::vector<std::string> configs;
  548. this->Makefile->GetConfigurations(configs);
  549. if (configs.empty())
  550. {
  551. configs.push_back("");
  552. }
  553. std::set<cmSourceFile*> emitted;
  554. for(std::vector<std::string>::const_iterator ci = configs.begin();
  555. ci != configs.end(); ++ci)
  556. {
  557. std::vector<cmSourceFile*> sources;
  558. this->Target->GetSourceFiles(sources, *ci);
  559. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  560. si != sources.end(); ++si)
  561. {
  562. cmSourceFile* sf = *si;
  563. if(emitted.insert(sf).second && this->SourcesQueued.insert(sf).second)
  564. {
  565. this->SourceQueue.push(sf);
  566. }
  567. }
  568. }
  569. }
  570. // Queue pre-build, pre-link, and post-build rule dependencies.
  571. this->CheckCustomCommands(this->Target->GetPreBuildCommands());
  572. this->CheckCustomCommands(this->Target->GetPreLinkCommands());
  573. this->CheckCustomCommands(this->Target->GetPostBuildCommands());
  574. }
  575. //----------------------------------------------------------------------------
  576. void cmTargetTraceDependencies::Trace()
  577. {
  578. // Process one dependency at a time until the queue is empty.
  579. while(!this->SourceQueue.empty())
  580. {
  581. // Get the next source from the queue.
  582. cmSourceFile* sf = this->SourceQueue.front();
  583. this->SourceQueue.pop();
  584. this->CurrentEntry = &this->GeneratorTarget->SourceEntries[sf];
  585. // Queue dependencies added explicitly by the user.
  586. if(const char* additionalDeps = sf->GetProperty("OBJECT_DEPENDS"))
  587. {
  588. std::vector<std::string> objDeps;
  589. cmSystemTools::ExpandListArgument(additionalDeps, objDeps);
  590. this->FollowNames(objDeps);
  591. }
  592. // Queue the source needed to generate this file, if any.
  593. this->FollowName(sf->GetFullPath());
  594. // Queue dependencies added programatically by commands.
  595. this->FollowNames(sf->GetDepends());
  596. // Queue custom command dependencies.
  597. if(cmCustomCommand const* cc = sf->GetCustomCommand())
  598. {
  599. this->CheckCustomCommand(*cc);
  600. }
  601. }
  602. this->CurrentEntry = 0;
  603. this->Target->AddTracedSources(this->NewSources);
  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 at the end.
  612. this->NewSources.push_back(sf->GetFullPath());
  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);
  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);
  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);
  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);
  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> configs;
  744. std::set<std::string> emitted;
  745. this->Makefile->GetConfigurations(configs);
  746. if (configs.empty())
  747. {
  748. configs.push_back("");
  749. }
  750. for(std::vector<std::string>::const_iterator ci = configs.begin();
  751. ci != configs.end(); ++ci)
  752. {
  753. this->FollowCommandDepends(cc, *ci, emitted);
  754. }
  755. }
  756. //----------------------------------------------------------------------------
  757. void cmTargetTraceDependencies::FollowCommandDepends(cmCustomCommand const& cc,
  758. const std::string& config,
  759. std::set<std::string>& emitted)
  760. {
  761. cmCustomCommandGenerator ccg(cc, config, this->Makefile);
  762. const std::vector<std::string>& depends = ccg.GetDepends();
  763. for(std::vector<std::string>::const_iterator di = depends.begin();
  764. di != depends.end(); ++di)
  765. {
  766. std::string const& dep = *di;
  767. if(emitted.insert(dep).second)
  768. {
  769. if(!this->IsUtility(dep))
  770. {
  771. // The dependency does not name a target and may be a file we
  772. // know how to generate. Queue it.
  773. this->FollowName(dep);
  774. }
  775. }
  776. }
  777. }
  778. //----------------------------------------------------------------------------
  779. void
  780. cmTargetTraceDependencies
  781. ::CheckCustomCommands(const std::vector<cmCustomCommand>& commands)
  782. {
  783. for(std::vector<cmCustomCommand>::const_iterator cli = commands.begin();
  784. cli != commands.end(); ++cli)
  785. {
  786. this->CheckCustomCommand(*cli);
  787. }
  788. }
  789. //----------------------------------------------------------------------------
  790. void cmGeneratorTarget::TraceDependencies()
  791. {
  792. // CMake-generated targets have no dependencies to trace. Normally tracing
  793. // would find nothing anyway, but when building CMake itself the "install"
  794. // target command ends up referencing the "cmake" target but we do not
  795. // really want the dependency because "install" depend on "all" anyway.
  796. if(this->GetType() == cmTarget::GLOBAL_TARGET)
  797. {
  798. return;
  799. }
  800. // Use a helper object to trace the dependencies.
  801. cmTargetTraceDependencies tracer(this);
  802. tracer.Trace();
  803. }
  804. //----------------------------------------------------------------------------
  805. void cmGeneratorTarget::GetAppleArchs(const std::string& config,
  806. std::vector<std::string>& archVec) const
  807. {
  808. const char* archs = 0;
  809. if(!config.empty())
  810. {
  811. std::string defVarName = "OSX_ARCHITECTURES_";
  812. defVarName += cmSystemTools::UpperCase(config);
  813. archs = this->Target->GetProperty(defVarName);
  814. }
  815. if(!archs)
  816. {
  817. archs = this->Target->GetProperty("OSX_ARCHITECTURES");
  818. }
  819. if(archs)
  820. {
  821. cmSystemTools::ExpandListArgument(std::string(archs), archVec);
  822. }
  823. }
  824. //----------------------------------------------------------------------------
  825. std::string
  826. cmGeneratorTarget::GetCreateRuleVariable(std::string const& lang,
  827. std::string const& config) const
  828. {
  829. switch(this->GetType())
  830. {
  831. case cmTarget::STATIC_LIBRARY:
  832. {
  833. std::string var = "CMAKE_" + lang + "_CREATE_STATIC_LIBRARY";
  834. if(this->Target->GetFeatureAsBool(
  835. "INTERPROCEDURAL_OPTIMIZATION", config))
  836. {
  837. std::string varIPO = var + "_IPO";
  838. if(this->Makefile->GetDefinition(varIPO))
  839. {
  840. return varIPO;
  841. }
  842. }
  843. return var;
  844. }
  845. case cmTarget::SHARED_LIBRARY:
  846. return "CMAKE_" + lang + "_CREATE_SHARED_LIBRARY";
  847. case cmTarget::MODULE_LIBRARY:
  848. return "CMAKE_" + lang + "_CREATE_SHARED_MODULE";
  849. case cmTarget::EXECUTABLE:
  850. return "CMAKE_" + lang + "_LINK_EXECUTABLE";
  851. default:
  852. break;
  853. }
  854. return "";
  855. }
  856. //----------------------------------------------------------------------------
  857. std::vector<std::string>
  858. cmGeneratorTarget::GetIncludeDirectories(const std::string& config) const
  859. {
  860. return this->Target->GetIncludeDirectories(config);
  861. }
  862. //----------------------------------------------------------------------------
  863. void cmGeneratorTarget::GenerateTargetManifest(
  864. const std::string& config) const
  865. {
  866. if (this->Target->IsImported())
  867. {
  868. return;
  869. }
  870. cmMakefile* mf = this->Target->GetMakefile();
  871. cmLocalGenerator* lg = mf->GetLocalGenerator();
  872. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  873. // Get the names.
  874. std::string name;
  875. std::string soName;
  876. std::string realName;
  877. std::string impName;
  878. std::string pdbName;
  879. if(this->GetType() == cmTarget::EXECUTABLE)
  880. {
  881. this->Target->GetExecutableNames(name, realName, impName, pdbName,
  882. config);
  883. }
  884. else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
  885. this->GetType() == cmTarget::SHARED_LIBRARY ||
  886. this->GetType() == cmTarget::MODULE_LIBRARY)
  887. {
  888. this->Target->GetLibraryNames(name, soName, realName, impName, pdbName,
  889. config);
  890. }
  891. else
  892. {
  893. return;
  894. }
  895. // Get the directory.
  896. std::string dir = this->Target->GetDirectory(config, false);
  897. // Add each name.
  898. std::string f;
  899. if(!name.empty())
  900. {
  901. f = dir;
  902. f += "/";
  903. f += name;
  904. gg->AddToManifest(config, f);
  905. }
  906. if(!soName.empty())
  907. {
  908. f = dir;
  909. f += "/";
  910. f += soName;
  911. gg->AddToManifest(config, f);
  912. }
  913. if(!realName.empty())
  914. {
  915. f = dir;
  916. f += "/";
  917. f += realName;
  918. gg->AddToManifest(config, f);
  919. }
  920. if(!pdbName.empty())
  921. {
  922. f = dir;
  923. f += "/";
  924. f += pdbName;
  925. gg->AddToManifest(config, f);
  926. }
  927. if(!impName.empty())
  928. {
  929. f = this->Target->GetDirectory(config, true);
  930. f += "/";
  931. f += impName;
  932. gg->AddToManifest(config, f);
  933. }
  934. }
  935. bool cmStrictTargetComparison::operator()(cmTarget const* t1,
  936. cmTarget const* t2) const
  937. {
  938. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  939. if (nameResult == 0)
  940. {
  941. return strcmp(t1->GetMakefile()->GetStartOutputDirectory(),
  942. t2->GetMakefile()->GetStartOutputDirectory()) < 0;
  943. }
  944. return nameResult < 0;
  945. }
  946. //----------------------------------------------------------------------------
  947. struct cmGeneratorTarget::SourceFileFlags
  948. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  949. {
  950. struct SourceFileFlags flags;
  951. this->ConstructSourceFileFlags();
  952. std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
  953. this->SourceFlagsMap.find(sf);
  954. if(si != this->SourceFlagsMap.end())
  955. {
  956. flags = si->second;
  957. }
  958. else
  959. {
  960. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  961. // were not listed in one of the other lists.
  962. if(const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION"))
  963. {
  964. flags.MacFolder = location;
  965. if(strcmp(location, "Resources") == 0)
  966. {
  967. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  968. }
  969. else
  970. {
  971. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  972. }
  973. }
  974. }
  975. return flags;
  976. }
  977. //----------------------------------------------------------------------------
  978. void cmGeneratorTarget::ConstructSourceFileFlags() const
  979. {
  980. if(this->SourceFileFlagsConstructed)
  981. {
  982. return;
  983. }
  984. this->SourceFileFlagsConstructed = true;
  985. // Process public headers to mark the source files.
  986. if(const char* files = this->Target->GetProperty("PUBLIC_HEADER"))
  987. {
  988. std::vector<std::string> relFiles;
  989. cmSystemTools::ExpandListArgument(files, relFiles);
  990. for(std::vector<std::string>::iterator it = relFiles.begin();
  991. it != relFiles.end(); ++it)
  992. {
  993. if(cmSourceFile* sf = this->Makefile->GetSource(*it))
  994. {
  995. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  996. flags.MacFolder = "Headers";
  997. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  998. }
  999. }
  1000. }
  1001. // Process private headers after public headers so that they take
  1002. // precedence if a file is listed in both.
  1003. if(const char* files = this->Target->GetProperty("PRIVATE_HEADER"))
  1004. {
  1005. std::vector<std::string> relFiles;
  1006. cmSystemTools::ExpandListArgument(files, relFiles);
  1007. for(std::vector<std::string>::iterator it = relFiles.begin();
  1008. it != relFiles.end(); ++it)
  1009. {
  1010. if(cmSourceFile* sf = this->Makefile->GetSource(*it))
  1011. {
  1012. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  1013. flags.MacFolder = "PrivateHeaders";
  1014. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  1015. }
  1016. }
  1017. }
  1018. // Mark sources listed as resources.
  1019. if(const char* files = this->Target->GetProperty("RESOURCE"))
  1020. {
  1021. std::vector<std::string> relFiles;
  1022. cmSystemTools::ExpandListArgument(files, relFiles);
  1023. for(std::vector<std::string>::iterator it = relFiles.begin();
  1024. it != relFiles.end(); ++it)
  1025. {
  1026. if(cmSourceFile* sf = this->Makefile->GetSource(*it))
  1027. {
  1028. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  1029. flags.MacFolder = "Resources";
  1030. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  1031. }
  1032. }
  1033. }
  1034. }