cmGeneratorTarget.cxx 10 KB

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