cmGeneratorTarget.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. //----------------------------------------------------------------------------
  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. //----------------------------------------------------------------------------
  28. int cmGeneratorTarget::GetType() const
  29. {
  30. return this->Target->GetType();
  31. }
  32. //----------------------------------------------------------------------------
  33. const char *cmGeneratorTarget::GetName() const
  34. {
  35. return this->Target->GetName();
  36. }
  37. //----------------------------------------------------------------------------
  38. const char *cmGeneratorTarget::GetProperty(const char *prop)
  39. {
  40. return this->Target->GetProperty(prop);
  41. }
  42. //----------------------------------------------------------------------------
  43. bool cmGeneratorTarget::GetPropertyAsBool(const char *prop)
  44. {
  45. return this->Target->GetPropertyAsBool(prop);
  46. }
  47. //----------------------------------------------------------------------------
  48. std::vector<cmSourceFile*> const& cmGeneratorTarget::GetSourceFiles()
  49. {
  50. return this->Target->GetSourceFiles();
  51. }
  52. //----------------------------------------------------------------------------
  53. void cmGeneratorTarget::ClassifySources()
  54. {
  55. cmsys::RegularExpression header(CM_HEADER_REGEX);
  56. bool isObjLib = this->Target->GetType() == cmTarget::OBJECT_LIBRARY;
  57. std::vector<cmSourceFile*> badObjLib;
  58. std::vector<cmSourceFile*> const& sources = this->Target->GetSourceFiles();
  59. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  60. si != sources.end(); ++si)
  61. {
  62. cmSourceFile* sf = *si;
  63. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  64. if(sf->GetCustomCommand())
  65. {
  66. this->CustomCommands.push_back(sf);
  67. }
  68. else if(sf->GetPropertyAsBool("HEADER_FILE_ONLY"))
  69. {
  70. this->HeaderSources.push_back(sf);
  71. }
  72. else if(sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
  73. {
  74. this->ExternalObjects.push_back(sf);
  75. if(isObjLib) { badObjLib.push_back(sf); }
  76. }
  77. else if(sf->GetLanguage())
  78. {
  79. this->ObjectSources.push_back(sf);
  80. }
  81. else if(ext == "def")
  82. {
  83. this->ModuleDefinitionFile = sf->GetFullPath();
  84. if(isObjLib) { badObjLib.push_back(sf); }
  85. }
  86. else if(ext == "idl")
  87. {
  88. this->IDLSources.push_back(sf);
  89. if(isObjLib) { badObjLib.push_back(sf); }
  90. }
  91. else if(header.find(sf->GetFullPath().c_str()))
  92. {
  93. this->HeaderSources.push_back(sf);
  94. }
  95. else if(this->GlobalGenerator->IgnoreFile(sf->GetExtension().c_str()))
  96. {
  97. // We only get here if a source file is not an external object
  98. // and has an extension that is listed as an ignored file type.
  99. // No message or diagnosis should be given.
  100. this->ExtraSources.push_back(sf);
  101. }
  102. else
  103. {
  104. this->ExtraSources.push_back(sf);
  105. if(isObjLib && ext != "txt")
  106. {
  107. badObjLib.push_back(sf);
  108. }
  109. }
  110. }
  111. if(!badObjLib.empty())
  112. {
  113. cmOStringStream e;
  114. e << "OBJECT library \"" << this->Target->GetName() << "\" contains:\n";
  115. for(std::vector<cmSourceFile*>::iterator i = badObjLib.begin();
  116. i != badObjLib.end(); ++i)
  117. {
  118. e << " " << (*i)->GetLocation().GetName() << "\n";
  119. }
  120. e << "but may contain only headers and sources that compile.";
  121. this->GlobalGenerator->GetCMakeInstance()
  122. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  123. this->Target->GetBacktrace());
  124. }
  125. }
  126. //----------------------------------------------------------------------------
  127. void cmGeneratorTarget::LookupObjectLibraries()
  128. {
  129. std::vector<std::string> const& objLibs =
  130. this->Target->GetObjectLibraries();
  131. for(std::vector<std::string>::const_iterator oli = objLibs.begin();
  132. oli != objLibs.end(); ++oli)
  133. {
  134. std::string const& objLibName = *oli;
  135. if(cmTarget* objLib = this->Makefile->FindTargetToUse(objLibName.c_str()))
  136. {
  137. if(objLib->GetType() == cmTarget::OBJECT_LIBRARY)
  138. {
  139. if(this->Target->GetType() != cmTarget::EXECUTABLE &&
  140. this->Target->GetType() != cmTarget::STATIC_LIBRARY &&
  141. this->Target->GetType() != cmTarget::SHARED_LIBRARY &&
  142. this->Target->GetType() != cmTarget::MODULE_LIBRARY)
  143. {
  144. this->GlobalGenerator->GetCMakeInstance()
  145. ->IssueMessage(cmake::FATAL_ERROR,
  146. "Only executables and non-OBJECT libraries may "
  147. "reference target objects.",
  148. this->Target->GetBacktrace());
  149. return;
  150. }
  151. this->Target->AddUtility(objLib->GetName());
  152. this->ObjectLibraries.push_back(objLib);
  153. }
  154. else
  155. {
  156. cmOStringStream e;
  157. e << "Objects of target \"" << objLibName
  158. << "\" referenced but is not an OBJECT library.";
  159. this->GlobalGenerator->GetCMakeInstance()
  160. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  161. this->Target->GetBacktrace());
  162. return;
  163. }
  164. }
  165. else
  166. {
  167. cmOStringStream e;
  168. e << "Objects of target \"" << objLibName
  169. << "\" referenced but no such target exists.";
  170. this->GlobalGenerator->GetCMakeInstance()
  171. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  172. this->Target->GetBacktrace());
  173. return;
  174. }
  175. }
  176. }
  177. //----------------------------------------------------------------------------
  178. void cmGeneratorTarget::UseObjectLibraries(std::vector<std::string>& objs)
  179. {
  180. for(std::vector<cmTarget*>::const_iterator
  181. ti = this->ObjectLibraries.begin();
  182. ti != this->ObjectLibraries.end(); ++ti)
  183. {
  184. cmTarget* objLib = *ti;
  185. cmGeneratorTarget* ogt =
  186. this->GlobalGenerator->GetGeneratorTarget(objLib);
  187. for(std::vector<cmSourceFile*>::const_iterator
  188. si = ogt->ObjectSources.begin();
  189. si != ogt->ObjectSources.end(); ++si)
  190. {
  191. std::string obj = ogt->ObjectDirectory;
  192. obj += ogt->Objects[*si];
  193. objs.push_back(obj);
  194. }
  195. }
  196. }
  197. //----------------------------------------------------------------------------
  198. void cmGeneratorTarget::GetAppleArchs(const char* config,
  199. std::vector<std::string>& archVec)
  200. {
  201. const char* archs = 0;
  202. if(config && *config)
  203. {
  204. std::string defVarName = "OSX_ARCHITECTURES_";
  205. defVarName += cmSystemTools::UpperCase(config);
  206. archs = this->Target->GetProperty(defVarName.c_str());
  207. }
  208. if(!archs)
  209. {
  210. archs = this->Target->GetProperty("OSX_ARCHITECTURES");
  211. }
  212. if(archs)
  213. {
  214. cmSystemTools::ExpandListArgument(std::string(archs), archVec);
  215. }
  216. }
  217. //----------------------------------------------------------------------------
  218. const char* cmGeneratorTarget::GetCreateRuleVariable()
  219. {
  220. switch(this->GetType())
  221. {
  222. case cmTarget::STATIC_LIBRARY:
  223. return "_CREATE_STATIC_LIBRARY";
  224. case cmTarget::SHARED_LIBRARY:
  225. return "_CREATE_SHARED_LIBRARY";
  226. case cmTarget::MODULE_LIBRARY:
  227. return "_CREATE_SHARED_MODULE";
  228. case cmTarget::EXECUTABLE:
  229. return "_LINK_EXECUTABLE";
  230. default:
  231. break;
  232. }
  233. return "";
  234. }
  235. //----------------------------------------------------------------------------
  236. std::vector<std::string> cmGeneratorTarget::GetIncludeDirectories(
  237. const char *config)
  238. {
  239. return this->Target->GetIncludeDirectories(config);
  240. }