cmGeneratorTarget.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 "cmComputeLinkInformation.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmSourceFile.h"
  17. #include "cmGeneratorExpression.h"
  18. #include "cmGeneratorExpressionDAGChecker.h"
  19. #include <assert.h>
  20. //----------------------------------------------------------------------------
  21. cmGeneratorTarget::cmGeneratorTarget(cmTarget* t): Target(t)
  22. {
  23. this->Makefile = this->Target->GetMakefile();
  24. this->LocalGenerator = this->Makefile->GetLocalGenerator();
  25. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  26. this->ClassifySources();
  27. this->LookupObjectLibraries();
  28. }
  29. cmGeneratorTarget::~cmGeneratorTarget()
  30. {
  31. for(std::map<cmStdString, cmComputeLinkInformation*>::iterator i
  32. = LinkInformation.begin(); i != LinkInformation.end(); ++i)
  33. {
  34. delete i->second;
  35. }
  36. }
  37. //----------------------------------------------------------------------------
  38. int cmGeneratorTarget::GetType() const
  39. {
  40. return this->Target->GetType();
  41. }
  42. //----------------------------------------------------------------------------
  43. const char *cmGeneratorTarget::GetName() const
  44. {
  45. return this->Target->GetName();
  46. }
  47. //----------------------------------------------------------------------------
  48. const char *cmGeneratorTarget::GetProperty(const char *prop)
  49. {
  50. return this->Target->GetProperty(prop);
  51. }
  52. //----------------------------------------------------------------------------
  53. bool cmGeneratorTarget::GetPropertyAsBool(const char *prop)
  54. {
  55. return this->Target->GetPropertyAsBool(prop);
  56. }
  57. //----------------------------------------------------------------------------
  58. std::vector<cmSourceFile*> const& cmGeneratorTarget::GetSourceFiles()
  59. {
  60. return this->Target->GetSourceFiles();
  61. }
  62. //----------------------------------------------------------------------------
  63. void cmGeneratorTarget::ClassifySources()
  64. {
  65. cmsys::RegularExpression header(CM_HEADER_REGEX);
  66. bool isObjLib = this->Target->GetType() == cmTarget::OBJECT_LIBRARY;
  67. std::vector<cmSourceFile*> badObjLib;
  68. std::vector<cmSourceFile*> const& sources = this->Target->GetSourceFiles();
  69. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  70. si != sources.end(); ++si)
  71. {
  72. cmSourceFile* sf = *si;
  73. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  74. if(sf->GetCustomCommand())
  75. {
  76. this->CustomCommands.push_back(sf);
  77. }
  78. else if(sf->GetPropertyAsBool("HEADER_FILE_ONLY"))
  79. {
  80. this->HeaderSources.push_back(sf);
  81. }
  82. else if(sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
  83. {
  84. this->ExternalObjects.push_back(sf);
  85. if(isObjLib) { badObjLib.push_back(sf); }
  86. }
  87. else if(sf->GetLanguage())
  88. {
  89. this->ObjectSources.push_back(sf);
  90. }
  91. else if(ext == "def")
  92. {
  93. this->ModuleDefinitionFile = sf->GetFullPath();
  94. if(isObjLib) { badObjLib.push_back(sf); }
  95. }
  96. else if(ext == "idl")
  97. {
  98. this->IDLSources.push_back(sf);
  99. if(isObjLib) { badObjLib.push_back(sf); }
  100. }
  101. else if(header.find(sf->GetFullPath().c_str()))
  102. {
  103. this->HeaderSources.push_back(sf);
  104. }
  105. else if(this->GlobalGenerator->IgnoreFile(sf->GetExtension().c_str()))
  106. {
  107. // We only get here if a source file is not an external object
  108. // and has an extension that is listed as an ignored file type.
  109. // No message or diagnosis should be given.
  110. this->ExtraSources.push_back(sf);
  111. }
  112. else
  113. {
  114. this->ExtraSources.push_back(sf);
  115. if(isObjLib && ext != "txt")
  116. {
  117. badObjLib.push_back(sf);
  118. }
  119. }
  120. }
  121. if(!badObjLib.empty())
  122. {
  123. cmOStringStream e;
  124. e << "OBJECT library \"" << this->Target->GetName() << "\" contains:\n";
  125. for(std::vector<cmSourceFile*>::iterator i = badObjLib.begin();
  126. i != badObjLib.end(); ++i)
  127. {
  128. e << " " << (*i)->GetLocation().GetName() << "\n";
  129. }
  130. e << "but may contain only headers and sources that compile.";
  131. this->GlobalGenerator->GetCMakeInstance()
  132. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  133. this->Target->GetBacktrace());
  134. }
  135. }
  136. //----------------------------------------------------------------------------
  137. void cmGeneratorTarget::LookupObjectLibraries()
  138. {
  139. std::vector<std::string> const& objLibs =
  140. this->Target->GetObjectLibraries();
  141. for(std::vector<std::string>::const_iterator oli = objLibs.begin();
  142. oli != objLibs.end(); ++oli)
  143. {
  144. std::string const& objLibName = *oli;
  145. if(cmTarget* objLib = this->Makefile->FindTargetToUse(objLibName.c_str()))
  146. {
  147. if(objLib->GetType() == cmTarget::OBJECT_LIBRARY)
  148. {
  149. if(this->Target->GetType() != cmTarget::EXECUTABLE &&
  150. this->Target->GetType() != cmTarget::STATIC_LIBRARY &&
  151. this->Target->GetType() != cmTarget::SHARED_LIBRARY &&
  152. this->Target->GetType() != cmTarget::MODULE_LIBRARY)
  153. {
  154. this->GlobalGenerator->GetCMakeInstance()
  155. ->IssueMessage(cmake::FATAL_ERROR,
  156. "Only executables and non-OBJECT libraries may "
  157. "reference target objects.",
  158. this->Target->GetBacktrace());
  159. return;
  160. }
  161. this->Target->AddUtility(objLib->GetName());
  162. this->ObjectLibraries.push_back(objLib);
  163. }
  164. else
  165. {
  166. cmOStringStream e;
  167. e << "Objects of target \"" << objLibName
  168. << "\" referenced but is not an OBJECT library.";
  169. this->GlobalGenerator->GetCMakeInstance()
  170. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  171. this->Target->GetBacktrace());
  172. return;
  173. }
  174. }
  175. else
  176. {
  177. cmOStringStream e;
  178. e << "Objects of target \"" << objLibName
  179. << "\" referenced but no such target exists.";
  180. this->GlobalGenerator->GetCMakeInstance()
  181. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  182. this->Target->GetBacktrace());
  183. return;
  184. }
  185. }
  186. }
  187. //----------------------------------------------------------------------------
  188. void cmGeneratorTarget::UseObjectLibraries(std::vector<std::string>& objs)
  189. {
  190. for(std::vector<cmTarget*>::const_iterator
  191. ti = this->ObjectLibraries.begin();
  192. ti != this->ObjectLibraries.end(); ++ti)
  193. {
  194. cmTarget* objLib = *ti;
  195. cmGeneratorTarget* ogt =
  196. this->GlobalGenerator->GetGeneratorTarget(objLib);
  197. for(std::vector<cmSourceFile*>::const_iterator
  198. si = ogt->ObjectSources.begin();
  199. si != ogt->ObjectSources.end(); ++si)
  200. {
  201. std::string obj = ogt->ObjectDirectory;
  202. obj += ogt->Objects[*si];
  203. objs.push_back(obj);
  204. }
  205. }
  206. }
  207. //----------------------------------------------------------------------------
  208. cmComputeLinkInformation*
  209. cmGeneratorTarget::GetLinkInformation(const char* config)
  210. {
  211. // Lookup any existing information for this configuration.
  212. std::map<cmStdString, cmComputeLinkInformation*>::iterator
  213. i = this->LinkInformation.find(config?config:"");
  214. if(i == this->LinkInformation.end())
  215. {
  216. // Compute information for this configuration.
  217. cmComputeLinkInformation* info =
  218. new cmComputeLinkInformation(this->Target, config);
  219. if(!info || !info->Compute())
  220. {
  221. delete info;
  222. info = 0;
  223. }
  224. // Store the information for this configuration.
  225. std::map<cmStdString, cmComputeLinkInformation*>::value_type
  226. entry(config?config:"", info);
  227. i = this->LinkInformation.insert(entry).first;
  228. }
  229. return i->second;
  230. }
  231. //----------------------------------------------------------------------------
  232. void cmGeneratorTarget::GetAppleArchs(const char* config,
  233. std::vector<std::string>& archVec)
  234. {
  235. const char* archs = 0;
  236. if(config && *config)
  237. {
  238. std::string defVarName = "OSX_ARCHITECTURES_";
  239. defVarName += cmSystemTools::UpperCase(config);
  240. archs = this->Target->GetProperty(defVarName.c_str());
  241. }
  242. if(!archs)
  243. {
  244. archs = this->Target->GetProperty("OSX_ARCHITECTURES");
  245. }
  246. if(archs)
  247. {
  248. cmSystemTools::ExpandListArgument(std::string(archs), archVec);
  249. }
  250. }
  251. //----------------------------------------------------------------------------
  252. const char* cmGeneratorTarget::GetCreateRuleVariable()
  253. {
  254. switch(this->GetType())
  255. {
  256. case cmTarget::STATIC_LIBRARY:
  257. return "_CREATE_STATIC_LIBRARY";
  258. case cmTarget::SHARED_LIBRARY:
  259. return "_CREATE_SHARED_LIBRARY";
  260. case cmTarget::MODULE_LIBRARY:
  261. return "_CREATE_SHARED_MODULE";
  262. case cmTarget::EXECUTABLE:
  263. return "_LINK_EXECUTABLE";
  264. default:
  265. break;
  266. }
  267. return "";
  268. }
  269. //----------------------------------------------------------------------------
  270. std::vector<std::string> cmGeneratorTarget::GetIncludeDirectories(
  271. const char *config)
  272. {
  273. std::vector<std::string> includes;
  274. const char *prop = this->Target->GetProperty("INCLUDE_DIRECTORIES");
  275. if(!prop)
  276. {
  277. return includes;
  278. }
  279. cmListFileBacktrace lfbt;
  280. cmGeneratorExpression ge(lfbt);
  281. cmGeneratorExpressionDAGChecker dagChecker(lfbt,
  282. this->GetName(),
  283. "INCLUDE_DIRECTORIES", 0, 0);
  284. cmSystemTools::ExpandListArgument(ge.Parse(prop)
  285. .Evaluate(this->Makefile,
  286. config,
  287. false,
  288. this,
  289. &dagChecker),
  290. includes);
  291. std::set<std::string> uniqueIncludes;
  292. std::vector<std::string> orderedAndUniqueIncludes;
  293. for(std::vector<std::string>::const_iterator
  294. li = includes.begin(); li != includes.end(); ++li)
  295. {
  296. std::string inc = *li;
  297. if (!cmSystemTools::IsOff(inc.c_str()))
  298. {
  299. cmSystemTools::ConvertToUnixSlashes(inc);
  300. }
  301. if(uniqueIncludes.insert(inc).second)
  302. {
  303. orderedAndUniqueIncludes.push_back(inc);
  304. }
  305. }
  306. return orderedAndUniqueIncludes;
  307. }
  308. //----------------------------------------------------------------------------
  309. std::string cmGeneratorTarget::GetCompileDefinitions(const char *config)
  310. {
  311. std::string defPropName = "COMPILE_DEFINITIONS";
  312. if (config)
  313. {
  314. defPropName += "_" + cmSystemTools::UpperCase(config);
  315. }
  316. const char *prop = this->Target->GetProperty(defPropName.c_str());
  317. if (!prop)
  318. {
  319. return "";
  320. }
  321. cmListFileBacktrace lfbt;
  322. cmGeneratorExpression ge(lfbt);
  323. cmGeneratorExpressionDAGChecker dagChecker(lfbt,
  324. this->GetName(),
  325. defPropName, 0, 0);
  326. return ge.Parse(prop).Evaluate(this->Makefile,
  327. config,
  328. false,
  329. this,
  330. &dagChecker);
  331. }