cmGeneratorTarget.cxx 38 KB

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