cmGeneratorTarget.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. void cmGeneratorTarget::GenerateTargetManifest(const char* config)
  207. {
  208. cmMakefile* mf = this->Target->GetMakefile();
  209. cmLocalGenerator* lg = mf->GetLocalGenerator();
  210. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  211. // Get the names.
  212. std::string name;
  213. std::string soName;
  214. std::string realName;
  215. std::string impName;
  216. std::string pdbName;
  217. if(this->GetType() == cmTarget::EXECUTABLE)
  218. {
  219. this->Target->GetExecutableNames(name, realName, impName, pdbName,
  220. config);
  221. }
  222. else if(this->GetType() == cmTarget::STATIC_LIBRARY ||
  223. this->GetType() == cmTarget::SHARED_LIBRARY ||
  224. this->GetType() == cmTarget::MODULE_LIBRARY)
  225. {
  226. this->Target->GetLibraryNames(name, soName, realName, impName, pdbName,
  227. config);
  228. }
  229. else
  230. {
  231. return;
  232. }
  233. // Get the directory.
  234. std::string dir = this->Target->GetDirectory(config, false);
  235. // Add each name.
  236. std::string f;
  237. if(!name.empty())
  238. {
  239. f = dir;
  240. f += "/";
  241. f += name;
  242. gg->AddToManifest(config? config:"", f);
  243. }
  244. if(!soName.empty())
  245. {
  246. f = dir;
  247. f += "/";
  248. f += soName;
  249. gg->AddToManifest(config? config:"", f);
  250. }
  251. if(!realName.empty())
  252. {
  253. f = dir;
  254. f += "/";
  255. f += realName;
  256. gg->AddToManifest(config? config:"", f);
  257. }
  258. if(!pdbName.empty())
  259. {
  260. f = dir;
  261. f += "/";
  262. f += pdbName;
  263. gg->AddToManifest(config? config:"", f);
  264. }
  265. if(!impName.empty())
  266. {
  267. f = this->Target->GetDirectory(config, true);
  268. f += "/";
  269. f += impName;
  270. gg->AddToManifest(config? config:"", f);
  271. }
  272. }
  273. //----------------------------------------------------------------------------
  274. cmComputeLinkInformation*
  275. cmGeneratorTarget::GetLinkInformation(const char* config)
  276. {
  277. // Lookup any existing information for this configuration.
  278. std::map<cmStdString, cmComputeLinkInformation*>::iterator
  279. i = this->LinkInformation.find(config?config:"");
  280. if(i == this->LinkInformation.end())
  281. {
  282. // Compute information for this configuration.
  283. cmComputeLinkInformation* info =
  284. new cmComputeLinkInformation(this->Target, config);
  285. if(!info || !info->Compute())
  286. {
  287. delete info;
  288. info = 0;
  289. }
  290. // Store the information for this configuration.
  291. std::map<cmStdString, cmComputeLinkInformation*>::value_type
  292. entry(config?config:"", info);
  293. i = this->LinkInformation.insert(entry).first;
  294. }
  295. return i->second;
  296. }
  297. //----------------------------------------------------------------------------
  298. void cmGeneratorTarget::GetAppleArchs(const char* config,
  299. std::vector<std::string>& archVec)
  300. {
  301. const char* archs = 0;
  302. if(config && *config)
  303. {
  304. std::string defVarName = "OSX_ARCHITECTURES_";
  305. defVarName += cmSystemTools::UpperCase(config);
  306. archs = this->Target->GetProperty(defVarName.c_str());
  307. }
  308. if(!archs)
  309. {
  310. archs = this->Target->GetProperty("OSX_ARCHITECTURES");
  311. }
  312. if(archs)
  313. {
  314. cmSystemTools::ExpandListArgument(std::string(archs), archVec);
  315. }
  316. }