cmGetPropertyCommand.cxx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGetPropertyCommand.h"
  14. #include "cmake.h"
  15. #include "cmTest.h"
  16. #include "cmPropertyDefinition.h"
  17. //----------------------------------------------------------------------------
  18. cmGetPropertyCommand::cmGetPropertyCommand()
  19. {
  20. this->InfoType = OutValue;
  21. }
  22. //----------------------------------------------------------------------------
  23. bool cmGetPropertyCommand::InitialPass(std::vector<std::string> const& args)
  24. {
  25. if(args.size() < 3 )
  26. {
  27. this->SetError("called with incorrect number of arguments");
  28. return false;
  29. }
  30. // The cmake variable in which to store the result.
  31. this->Variable = args[0];
  32. // Get the scope from which to get the property.
  33. cmProperty::ScopeType scope;
  34. if(args[1] == "GLOBAL")
  35. {
  36. scope = cmProperty::GLOBAL;
  37. }
  38. else if(args[1] == "DIRECTORY")
  39. {
  40. scope = cmProperty::DIRECTORY;
  41. }
  42. else if(args[1] == "TARGET")
  43. {
  44. scope = cmProperty::TARGET;
  45. }
  46. else if(args[1] == "SOURCE")
  47. {
  48. scope = cmProperty::SOURCE_FILE;
  49. }
  50. else if(args[1] == "TEST")
  51. {
  52. scope = cmProperty::TEST;
  53. }
  54. else if(args[1] == "VARIABLE")
  55. {
  56. scope = cmProperty::VARIABLE;
  57. }
  58. else
  59. {
  60. cmOStringStream e;
  61. e << "given invalid scope " << args[1] << ". "
  62. << "Valid scopes are "
  63. << "GLOBAL, DIRECTORY, TARGET, SOURCE, TEST, VARIABLE.";
  64. this->SetError(e.str().c_str());
  65. return false;
  66. }
  67. // Parse remaining arguments.
  68. enum Doing { DoingNone, DoingName, DoingProperty, DoingType };
  69. Doing doing = DoingName;
  70. for(unsigned int i=2; i < args.size(); ++i)
  71. {
  72. if(args[i] == "PROPERTY")
  73. {
  74. doing = DoingProperty;
  75. }
  76. else if(args[i] == "BRIEF_DOCS")
  77. {
  78. doing = DoingNone;
  79. this->InfoType = OutBriefDoc;
  80. }
  81. else if(args[i] == "FULL_DOCS")
  82. {
  83. doing = DoingNone;
  84. this->InfoType = OutFullDoc;
  85. }
  86. else if(args[i] == "DEFINED")
  87. {
  88. doing = DoingNone;
  89. this->InfoType = OutDefined;
  90. }
  91. else if(doing == DoingName)
  92. {
  93. doing = DoingNone;
  94. this->Name = args[i];
  95. }
  96. else if(doing == DoingProperty)
  97. {
  98. doing = DoingNone;
  99. this->PropertyName = args[i];
  100. }
  101. else
  102. {
  103. cmOStringStream e;
  104. e << "given invalid argument \"" << args[i] << "\".";
  105. this->SetError(e.str().c_str());
  106. return false;
  107. }
  108. }
  109. // Make sure a property name was found.
  110. if(this->PropertyName.empty())
  111. {
  112. this->SetError("not given a PROPERTY <name> argument.");
  113. return false;
  114. }
  115. // Compute requested output.
  116. if(this->InfoType == OutBriefDoc)
  117. {
  118. // Lookup brief documentation.
  119. std::string output;
  120. if(cmPropertyDefinition* def =
  121. this->Makefile->GetCMakeInstance()->
  122. GetPropertyDefinition(this->PropertyName.c_str(), scope))
  123. {
  124. output = def->GetShortDescription();
  125. }
  126. else
  127. {
  128. output = "NOTFOUND";
  129. }
  130. this->Makefile->AddDefinition(this->Variable.c_str(), output.c_str());
  131. }
  132. else if(this->InfoType == OutFullDoc)
  133. {
  134. // Lookup full documentation.
  135. std::string output;
  136. if(cmPropertyDefinition* def =
  137. this->Makefile->GetCMakeInstance()->
  138. GetPropertyDefinition(this->PropertyName.c_str(), scope))
  139. {
  140. output = def->GetFullDescription();
  141. }
  142. else
  143. {
  144. output = "NOTFOUND";
  145. }
  146. this->Makefile->AddDefinition(this->Variable.c_str(), output.c_str());
  147. }
  148. else
  149. {
  150. // Dispatch property getting.
  151. switch(scope)
  152. {
  153. case cmProperty::GLOBAL: return this->HandleGlobalMode();
  154. case cmProperty::DIRECTORY: return this->HandleDirectoryMode();
  155. case cmProperty::TARGET: return this->HandleTargetMode();
  156. case cmProperty::SOURCE_FILE: return this->HandleSourceMode();
  157. case cmProperty::TEST: return this->HandleTestMode();
  158. case cmProperty::VARIABLE: return this->HandleVariableMode();
  159. case cmProperty::CACHED_VARIABLE:
  160. break; // should never happen
  161. }
  162. }
  163. return true;
  164. }
  165. //----------------------------------------------------------------------------
  166. bool cmGetPropertyCommand::StoreResult(const char* value)
  167. {
  168. if(this->InfoType == OutDefined)
  169. {
  170. this->Makefile->AddDefinition(this->Variable.c_str(), value? "1":"0");
  171. }
  172. else // if(this->InfoType == OutValue)
  173. {
  174. this->Makefile->AddDefinition(this->Variable.c_str(), value);
  175. }
  176. return true;
  177. }
  178. //----------------------------------------------------------------------------
  179. bool cmGetPropertyCommand::HandleGlobalMode()
  180. {
  181. if(!this->Name.empty())
  182. {
  183. this->SetError("given name for GLOBAL scope.");
  184. return false;
  185. }
  186. // Get the property.
  187. cmake* cm = this->Makefile->GetCMakeInstance();
  188. return this->StoreResult(cm->GetProperty(this->PropertyName.c_str()));
  189. }
  190. //----------------------------------------------------------------------------
  191. bool cmGetPropertyCommand::HandleDirectoryMode()
  192. {
  193. // Default to the current directory.
  194. cmMakefile* mf = this->Makefile;
  195. // Lookup the directory if given.
  196. if(!this->Name.empty())
  197. {
  198. // Construct the directory name. Interpret relative paths with
  199. // respect to the current directory.
  200. std::string dir = this->Name;
  201. if(!cmSystemTools::FileIsFullPath(dir.c_str()))
  202. {
  203. dir = this->Makefile->GetCurrentDirectory();
  204. dir += "/";
  205. dir += this->Name;
  206. }
  207. // The local generators are associated with collapsed paths.
  208. dir = cmSystemTools::CollapseFullPath(dir.c_str());
  209. // Lookup the generator.
  210. if(cmLocalGenerator* lg =
  211. (this->Makefile->GetLocalGenerator()
  212. ->GetGlobalGenerator()->FindLocalGenerator(dir.c_str())))
  213. {
  214. // Use the makefile for the directory found.
  215. mf = lg->GetMakefile();
  216. }
  217. else
  218. {
  219. // Could not find the directory.
  220. this->SetError
  221. ("DIRECTORY scope provided but requested directory was not found. "
  222. "This could be because the directory argument was invalid or, "
  223. "it is valid but has not been processed yet.");
  224. return false;
  225. }
  226. }
  227. // Get the property.
  228. return this->StoreResult(mf->GetProperty(this->PropertyName.c_str()));
  229. }
  230. //----------------------------------------------------------------------------
  231. bool cmGetPropertyCommand::HandleTargetMode()
  232. {
  233. if(this->Name.empty())
  234. {
  235. this->SetError("not given name for TARGET scope.");
  236. return false;
  237. }
  238. if(cmTarget* target =
  239. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  240. ->FindTarget(0, this->Name.c_str(), true))
  241. {
  242. return this->StoreResult(target->GetProperty(this->PropertyName.c_str()));
  243. }
  244. else
  245. {
  246. cmOStringStream e;
  247. e << "could not find TARGET " << this->Name
  248. << ". Perhaps it has not yet been created.";
  249. this->SetError(e.str().c_str());
  250. return false;
  251. }
  252. }
  253. //----------------------------------------------------------------------------
  254. bool cmGetPropertyCommand::HandleSourceMode()
  255. {
  256. if(this->Name.empty())
  257. {
  258. this->SetError("not given name for SOURCE scope.");
  259. return false;
  260. }
  261. // Get the source file.
  262. if(cmSourceFile* sf =
  263. this->Makefile->GetOrCreateSource(this->Name.c_str()))
  264. {
  265. return this->StoreResult(sf->GetProperty(this->PropertyName.c_str()));
  266. }
  267. else
  268. {
  269. cmOStringStream e;
  270. e << "given SOURCE name that could not be found or created: "
  271. << this->Name;
  272. this->SetError(e.str().c_str());
  273. return false;
  274. }
  275. }
  276. //----------------------------------------------------------------------------
  277. bool cmGetPropertyCommand::HandleTestMode()
  278. {
  279. if(this->Name.empty())
  280. {
  281. this->SetError("not given name for TEST scope.");
  282. return false;
  283. }
  284. // Loop over all tests looking for matching names.
  285. std::vector<cmTest*> const& tests = *this->Makefile->GetTests();
  286. for(std::vector<cmTest*>::const_iterator ti = tests.begin();
  287. ti != tests.end(); ++ti)
  288. {
  289. cmTest* test = *ti;
  290. if(test->GetName() == this->Name)
  291. {
  292. return this->StoreResult(test->GetProperty(this->PropertyName.c_str()));
  293. }
  294. }
  295. // If not found it is an error.
  296. cmOStringStream e;
  297. e << "given TEST name that does not exist: " << this->Name;
  298. this->SetError(e.str().c_str());
  299. return false;
  300. }
  301. //----------------------------------------------------------------------------
  302. bool cmGetPropertyCommand::HandleVariableMode()
  303. {
  304. if(!this->Name.empty())
  305. {
  306. this->SetError("given name for VARIABLE scope.");
  307. return false;
  308. }
  309. return this->StoreResult
  310. (this->Makefile->GetDefinition(this->PropertyName.c_str()));
  311. }