cmGeneratorTarget.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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::IsSystemIncludeDirectory(const char *dir,
  44. const char *config)
  45. {
  46. std::string config_upper;
  47. if(config && *config)
  48. {
  49. config_upper = cmSystemTools::UpperCase(config);
  50. }
  51. typedef std::map<std::string, std::vector<std::string> > IncludeCacheType;
  52. IncludeCacheType::iterator iter =
  53. this->SystemIncludesCache.find(config_upper);
  54. if (iter == this->SystemIncludesCache.end())
  55. {
  56. std::vector<std::string> result;
  57. for (std::set<cmStdString>::const_iterator
  58. it = this->Target->GetSystemIncludeDirectories().begin();
  59. it != this->Target->GetSystemIncludeDirectories().end(); ++it)
  60. {
  61. cmListFileBacktrace lfbt;
  62. cmGeneratorExpression ge(lfbt);
  63. cmGeneratorExpressionDAGChecker dagChecker(lfbt,
  64. this->GetName(),
  65. "INTERFACE_SYSTEM_INCLUDE_DIRECTORIES", 0, 0);
  66. cmSystemTools::ExpandListArgument(ge.Parse(*it)
  67. ->Evaluate(this->Makefile,
  68. config, false, this->Target,
  69. &dagChecker), result);
  70. }
  71. for(std::vector<std::string>::iterator li = result.begin();
  72. li != result.end(); ++li)
  73. {
  74. cmSystemTools::ConvertToUnixSlashes(*li);
  75. }
  76. IncludeCacheType::value_type entry(config_upper, result);
  77. iter = this->SystemIncludesCache.insert(entry).first;
  78. }
  79. if (std::find(iter->second.begin(),
  80. iter->second.end(), dir) != iter->second.end())
  81. {
  82. return true;
  83. }
  84. return false;
  85. }
  86. //----------------------------------------------------------------------------
  87. bool cmGeneratorTarget::GetPropertyAsBool(const char *prop)
  88. {
  89. return this->Target->GetPropertyAsBool(prop);
  90. }
  91. //----------------------------------------------------------------------------
  92. std::vector<cmSourceFile*> const& cmGeneratorTarget::GetSourceFiles()
  93. {
  94. return this->Target->GetSourceFiles();
  95. }
  96. //----------------------------------------------------------------------------
  97. void cmGeneratorTarget::ClassifySources()
  98. {
  99. cmsys::RegularExpression header(CM_HEADER_REGEX);
  100. bool isObjLib = this->Target->GetType() == cmTarget::OBJECT_LIBRARY;
  101. std::vector<cmSourceFile*> badObjLib;
  102. std::vector<cmSourceFile*> const& sources = this->Target->GetSourceFiles();
  103. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  104. si != sources.end(); ++si)
  105. {
  106. cmSourceFile* sf = *si;
  107. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  108. if(sf->GetCustomCommand())
  109. {
  110. this->CustomCommands.push_back(sf);
  111. }
  112. else if(sf->GetPropertyAsBool("HEADER_FILE_ONLY"))
  113. {
  114. this->HeaderSources.push_back(sf);
  115. }
  116. else if(sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
  117. {
  118. this->ExternalObjects.push_back(sf);
  119. if(isObjLib) { badObjLib.push_back(sf); }
  120. }
  121. else if(sf->GetLanguage())
  122. {
  123. this->ObjectSources.push_back(sf);
  124. }
  125. else if(ext == "def")
  126. {
  127. this->ModuleDefinitionFile = sf->GetFullPath();
  128. if(isObjLib) { badObjLib.push_back(sf); }
  129. }
  130. else if(ext == "idl")
  131. {
  132. this->IDLSources.push_back(sf);
  133. if(isObjLib) { badObjLib.push_back(sf); }
  134. }
  135. else if(ext == "resx")
  136. {
  137. // Build and save the name of the corresponding .h file
  138. // This relationship will be used later when building the project files.
  139. // Both names would have been auto generated from Visual Studio
  140. // where the user supplied the file name and Visual Studio
  141. // appended the suffix.
  142. std::string resx = sf->GetFullPath();
  143. std::string hFileName = resx.substr(0, resx.find_last_of(".")) + ".h";
  144. this->ExpectedResxHeaders.insert(hFileName);
  145. this->ResxSources.push_back(sf);
  146. }
  147. else if(header.find(sf->GetFullPath().c_str()))
  148. {
  149. this->HeaderSources.push_back(sf);
  150. }
  151. else if(this->GlobalGenerator->IgnoreFile(sf->GetExtension().c_str()))
  152. {
  153. // We only get here if a source file is not an external object
  154. // and has an extension that is listed as an ignored file type.
  155. // No message or diagnosis should be given.
  156. this->ExtraSources.push_back(sf);
  157. }
  158. else
  159. {
  160. this->ExtraSources.push_back(sf);
  161. if(isObjLib && ext != "txt")
  162. {
  163. badObjLib.push_back(sf);
  164. }
  165. }
  166. }
  167. if(!badObjLib.empty())
  168. {
  169. cmOStringStream e;
  170. e << "OBJECT library \"" << this->Target->GetName() << "\" contains:\n";
  171. for(std::vector<cmSourceFile*>::iterator i = badObjLib.begin();
  172. i != badObjLib.end(); ++i)
  173. {
  174. e << " " << (*i)->GetLocation().GetName() << "\n";
  175. }
  176. e << "but may contain only headers and sources that compile.";
  177. this->GlobalGenerator->GetCMakeInstance()
  178. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  179. this->Target->GetBacktrace());
  180. }
  181. }
  182. //----------------------------------------------------------------------------
  183. void cmGeneratorTarget::LookupObjectLibraries()
  184. {
  185. std::vector<std::string> const& objLibs =
  186. this->Target->GetObjectLibraries();
  187. for(std::vector<std::string>::const_iterator oli = objLibs.begin();
  188. oli != objLibs.end(); ++oli)
  189. {
  190. std::string const& objLibName = *oli;
  191. if(cmTarget* objLib = this->Makefile->FindTargetToUse(objLibName.c_str()))
  192. {
  193. if(objLib->GetType() == cmTarget::OBJECT_LIBRARY)
  194. {
  195. if(this->Target->GetType() != cmTarget::EXECUTABLE &&
  196. this->Target->GetType() != cmTarget::STATIC_LIBRARY &&
  197. this->Target->GetType() != cmTarget::SHARED_LIBRARY &&
  198. this->Target->GetType() != cmTarget::MODULE_LIBRARY)
  199. {
  200. this->GlobalGenerator->GetCMakeInstance()
  201. ->IssueMessage(cmake::FATAL_ERROR,
  202. "Only executables and non-OBJECT libraries may "
  203. "reference target objects.",
  204. this->Target->GetBacktrace());
  205. return;
  206. }
  207. this->Target->AddUtility(objLib->GetName());
  208. this->ObjectLibraries.push_back(objLib);
  209. }
  210. else
  211. {
  212. cmOStringStream e;
  213. e << "Objects of target \"" << objLibName
  214. << "\" referenced but is not an OBJECT library.";
  215. this->GlobalGenerator->GetCMakeInstance()
  216. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  217. this->Target->GetBacktrace());
  218. return;
  219. }
  220. }
  221. else
  222. {
  223. cmOStringStream e;
  224. e << "Objects of target \"" << objLibName
  225. << "\" referenced but no such target exists.";
  226. this->GlobalGenerator->GetCMakeInstance()
  227. ->IssueMessage(cmake::FATAL_ERROR, e.str(),
  228. this->Target->GetBacktrace());
  229. return;
  230. }
  231. }
  232. }
  233. //----------------------------------------------------------------------------
  234. void cmGeneratorTarget::UseObjectLibraries(std::vector<std::string>& objs)
  235. {
  236. for(std::vector<cmTarget*>::const_iterator
  237. ti = this->ObjectLibraries.begin();
  238. ti != this->ObjectLibraries.end(); ++ti)
  239. {
  240. cmTarget* objLib = *ti;
  241. cmGeneratorTarget* ogt =
  242. this->GlobalGenerator->GetGeneratorTarget(objLib);
  243. for(std::vector<cmSourceFile*>::const_iterator
  244. si = ogt->ObjectSources.begin();
  245. si != ogt->ObjectSources.end(); ++si)
  246. {
  247. std::string obj = ogt->ObjectDirectory;
  248. obj += ogt->Objects[*si];
  249. objs.push_back(obj);
  250. }
  251. }
  252. }
  253. //----------------------------------------------------------------------------
  254. void cmGeneratorTarget::GetAppleArchs(const char* config,
  255. std::vector<std::string>& archVec)
  256. {
  257. const char* archs = 0;
  258. if(config && *config)
  259. {
  260. std::string defVarName = "OSX_ARCHITECTURES_";
  261. defVarName += cmSystemTools::UpperCase(config);
  262. archs = this->Target->GetProperty(defVarName.c_str());
  263. }
  264. if(!archs)
  265. {
  266. archs = this->Target->GetProperty("OSX_ARCHITECTURES");
  267. }
  268. if(archs)
  269. {
  270. cmSystemTools::ExpandListArgument(std::string(archs), archVec);
  271. }
  272. }
  273. //----------------------------------------------------------------------------
  274. const char* cmGeneratorTarget::GetCreateRuleVariable()
  275. {
  276. switch(this->GetType())
  277. {
  278. case cmTarget::STATIC_LIBRARY:
  279. return "_CREATE_STATIC_LIBRARY";
  280. case cmTarget::SHARED_LIBRARY:
  281. return "_CREATE_SHARED_LIBRARY";
  282. case cmTarget::MODULE_LIBRARY:
  283. return "_CREATE_SHARED_MODULE";
  284. case cmTarget::EXECUTABLE:
  285. return "_LINK_EXECUTABLE";
  286. default:
  287. break;
  288. }
  289. return "";
  290. }
  291. //----------------------------------------------------------------------------
  292. std::vector<std::string> cmGeneratorTarget::GetIncludeDirectories(
  293. const char *config)
  294. {
  295. return this->Target->GetIncludeDirectories(config);
  296. }