cmGetPropertyCommand.cxx 9.4 KB

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