cmGeneratorTarget.cxx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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(ext == "resx")
  92. {
  93. // Build and save the name of the corresponding .h 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 resx = sf->GetFullPath();
  99. std::string hFileName = resx.substr(0, resx.find_last_of(".")) + ".h";
  100. this->ExpectedResxHeaders.insert(hFileName);
  101. this->ResxSources.push_back(sf);
  102. }
  103. else if(header.find(sf->GetFullPath().c_str()))
  104. {
  105. this->HeaderSources.push_back(sf);
  106. }
  107. else if(this->GlobalGenerator->IgnoreFile(sf->GetExtension().c_str()))
  108. {
  109. // We only get here if a source file is not an external object
  110. // and has an extension that is listed as an ignored file type.
  111. // No message or diagnosis should be given.
  112. this->ExtraSources.push_back(sf);
  113. }
  114. else
  115. {
  116. this->ExtraSources.push_back(sf);
  117. if(isObjLib && ext != "txt")
  118. {
  119. badObjLib.push_back(sf);
  120. }
  121. }
  122. }
  123. if(!badObjLib.empty())
  124. {
  125. cmOStringStream e;
  126. e << "OBJECT library \"" << this->Target->GetName() << "\" contains:\n";
  127. for(std::vector<cmSourceFile*>::iterator i = badObjLib.begin();
  128. i != badObjLib.end(); ++i)
  129. {
  130. e << " " << (*i)->GetLocation().GetName() << "\n";
  131. }
  132. e << "but may contain only headers and sources that compile.";
  133. this->GlobalGenerator->GetCMakeInstance()
  134. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  135. this->Target->GetBacktrace());
  136. }
  137. }
  138. //----------------------------------------------------------------------------
  139. void cmGeneratorTarget::LookupObjectLibraries()
  140. {
  141. std::vector<std::string> const& objLibs =
  142. this->Target->GetObjectLibraries();
  143. for(std::vector<std::string>::const_iterator oli = objLibs.begin();
  144. oli != objLibs.end(); ++oli)
  145. {
  146. std::string const& objLibName = *oli;
  147. if(cmTarget* objLib = this->Makefile->FindTargetToUse(objLibName.c_str()))
  148. {
  149. if(objLib->GetType() == cmTarget::OBJECT_LIBRARY)
  150. {
  151. if(this->Target->GetType() != cmTarget::EXECUTABLE &&
  152. this->Target->GetType() != cmTarget::STATIC_LIBRARY &&
  153. this->Target->GetType() != cmTarget::SHARED_LIBRARY &&
  154. this->Target->GetType() != cmTarget::MODULE_LIBRARY)
  155. {
  156. this->GlobalGenerator->GetCMakeInstance()
  157. ->IssueMessage(cmake::FATAL_ERROR,
  158. "Only executables and non-OBJECT libraries may "
  159. "reference target objects.",
  160. this->Target->GetBacktrace());
  161. return;
  162. }
  163. this->Target->AddUtility(objLib->GetName());
  164. this->ObjectLibraries.push_back(objLib);
  165. }
  166. else
  167. {
  168. cmOStringStream e;
  169. e << "Objects of target \"" << objLibName
  170. << "\" referenced but is not an OBJECT library.";
  171. this->GlobalGenerator->GetCMakeInstance()
  172. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  173. this->Target->GetBacktrace());
  174. return;
  175. }
  176. }
  177. else
  178. {
  179. cmOStringStream e;
  180. e << "Objects of target \"" << objLibName
  181. << "\" referenced but no such target exists.";
  182. this->GlobalGenerator->GetCMakeInstance()
  183. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  184. this->Target->GetBacktrace());
  185. return;
  186. }
  187. }
  188. }
  189. //----------------------------------------------------------------------------
  190. void cmGeneratorTarget::UseObjectLibraries(std::vector<std::string>& objs)
  191. {
  192. for(std::vector<cmTarget*>::const_iterator
  193. ti = this->ObjectLibraries.begin();
  194. ti != this->ObjectLibraries.end(); ++ti)
  195. {
  196. cmTarget* objLib = *ti;
  197. cmGeneratorTarget* ogt =
  198. this->GlobalGenerator->GetGeneratorTarget(objLib);
  199. for(std::vector<cmSourceFile*>::const_iterator
  200. si = ogt->ObjectSources.begin();
  201. si != ogt->ObjectSources.end(); ++si)
  202. {
  203. std::string obj = ogt->ObjectDirectory;
  204. obj += ogt->Objects[*si];
  205. objs.push_back(obj);
  206. }
  207. }
  208. }
  209. //----------------------------------------------------------------------------
  210. void cmGeneratorTarget::GetAppleArchs(const char* config,
  211. std::vector<std::string>& archVec)
  212. {
  213. const char* archs = 0;
  214. if(config && *config)
  215. {
  216. std::string defVarName = "OSX_ARCHITECTURES_";
  217. defVarName += cmSystemTools::UpperCase(config);
  218. archs = this->Target->GetProperty(defVarName.c_str());
  219. }
  220. if(!archs)
  221. {
  222. archs = this->Target->GetProperty("OSX_ARCHITECTURES");
  223. }
  224. if(archs)
  225. {
  226. cmSystemTools::ExpandListArgument(std::string(archs), archVec);
  227. }
  228. }
  229. //----------------------------------------------------------------------------
  230. const char* cmGeneratorTarget::GetCreateRuleVariable()
  231. {
  232. switch(this->GetType())
  233. {
  234. case cmTarget::STATIC_LIBRARY:
  235. return "_CREATE_STATIC_LIBRARY";
  236. case cmTarget::SHARED_LIBRARY:
  237. return "_CREATE_SHARED_LIBRARY";
  238. case cmTarget::MODULE_LIBRARY:
  239. return "_CREATE_SHARED_MODULE";
  240. case cmTarget::EXECUTABLE:
  241. return "_LINK_EXECUTABLE";
  242. default:
  243. break;
  244. }
  245. return "";
  246. }
  247. //----------------------------------------------------------------------------
  248. std::vector<std::string> cmGeneratorTarget::GetIncludeDirectories(
  249. const char *config)
  250. {
  251. return this->Target->GetIncludeDirectories(config);
  252. }