cmGetPropertyCommand.cxx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. const char *value;
  158. if(this->Makefile->GetCMakeInstance()->
  159. GetPropertyDefinition(this->PropertyName.c_str(), scope))
  160. {
  161. this->Makefile->AddDefinition(this->Variable.c_str(), "1");
  162. }
  163. else
  164. {
  165. this->Makefile->AddDefinition(this->Variable.c_str(), "0");
  166. }
  167. }
  168. else
  169. {
  170. // Dispatch property getting.
  171. switch(scope)
  172. {
  173. case cmProperty::GLOBAL: return this->HandleGlobalMode();
  174. case cmProperty::DIRECTORY: return this->HandleDirectoryMode();
  175. case cmProperty::TARGET: return this->HandleTargetMode();
  176. case cmProperty::SOURCE_FILE: return this->HandleSourceMode();
  177. case cmProperty::TEST: return this->HandleTestMode();
  178. case cmProperty::VARIABLE: return this->HandleVariableMode();
  179. case cmProperty::CACHED_VARIABLE:
  180. break; // should never happen
  181. }
  182. }
  183. return true;
  184. }
  185. //----------------------------------------------------------------------------
  186. bool cmGetPropertyCommand::StoreResult(const char* value)
  187. {
  188. if(this->InfoType == OutSet)
  189. {
  190. this->Makefile->AddDefinition(this->Variable.c_str(), value? "1":"0");
  191. }
  192. else // if(this->InfoType == OutValue)
  193. {
  194. this->Makefile->AddDefinition(this->Variable.c_str(), value);
  195. }
  196. return true;
  197. }
  198. //----------------------------------------------------------------------------
  199. bool cmGetPropertyCommand::HandleGlobalMode()
  200. {
  201. if(!this->Name.empty())
  202. {
  203. this->SetError("given name for GLOBAL scope.");
  204. return false;
  205. }
  206. // Get the property.
  207. cmake* cm = this->Makefile->GetCMakeInstance();
  208. return this->StoreResult(cm->GetProperty(this->PropertyName.c_str()));
  209. }
  210. //----------------------------------------------------------------------------
  211. bool cmGetPropertyCommand::HandleDirectoryMode()
  212. {
  213. // Default to the current directory.
  214. cmMakefile* mf = this->Makefile;
  215. // Lookup the directory if given.
  216. if(!this->Name.empty())
  217. {
  218. // Construct the directory name. Interpret relative paths with
  219. // respect to the current directory.
  220. std::string dir = this->Name;
  221. if(!cmSystemTools::FileIsFullPath(dir.c_str()))
  222. {
  223. dir = this->Makefile->GetCurrentDirectory();
  224. dir += "/";
  225. dir += this->Name;
  226. }
  227. // The local generators are associated with collapsed paths.
  228. dir = cmSystemTools::CollapseFullPath(dir.c_str());
  229. // Lookup the generator.
  230. if(cmLocalGenerator* lg =
  231. (this->Makefile->GetLocalGenerator()
  232. ->GetGlobalGenerator()->FindLocalGenerator(dir.c_str())))
  233. {
  234. // Use the makefile for the directory found.
  235. mf = lg->GetMakefile();
  236. }
  237. else
  238. {
  239. // Could not find the directory.
  240. this->SetError
  241. ("DIRECTORY scope provided but requested directory was not found. "
  242. "This could be because the directory argument was invalid or, "
  243. "it is valid but has not been processed yet.");
  244. return false;
  245. }
  246. }
  247. // Get the property.
  248. return this->StoreResult(mf->GetProperty(this->PropertyName.c_str()));
  249. }
  250. //----------------------------------------------------------------------------
  251. bool cmGetPropertyCommand::HandleTargetMode()
  252. {
  253. if(this->Name.empty())
  254. {
  255. this->SetError("not given name for TARGET scope.");
  256. return false;
  257. }
  258. if(cmTarget* target = this->Makefile->FindTargetToUse(this->Name.c_str()))
  259. {
  260. return this->StoreResult(target->GetProperty(this->PropertyName.c_str()));
  261. }
  262. else
  263. {
  264. cmOStringStream e;
  265. e << "could not find TARGET " << this->Name
  266. << ". Perhaps it has not yet been created.";
  267. this->SetError(e.str().c_str());
  268. return false;
  269. }
  270. }
  271. //----------------------------------------------------------------------------
  272. bool cmGetPropertyCommand::HandleSourceMode()
  273. {
  274. if(this->Name.empty())
  275. {
  276. this->SetError("not given name for SOURCE scope.");
  277. return false;
  278. }
  279. // Get the source file.
  280. if(cmSourceFile* sf =
  281. this->Makefile->GetOrCreateSource(this->Name.c_str()))
  282. {
  283. return
  284. this->StoreResult(sf->GetPropertyForUser(this->PropertyName.c_str()));
  285. }
  286. else
  287. {
  288. cmOStringStream e;
  289. e << "given SOURCE name that could not be found or created: "
  290. << this->Name;
  291. this->SetError(e.str().c_str());
  292. return false;
  293. }
  294. }
  295. //----------------------------------------------------------------------------
  296. bool cmGetPropertyCommand::HandleTestMode()
  297. {
  298. if(this->Name.empty())
  299. {
  300. this->SetError("not given name for TEST scope.");
  301. return false;
  302. }
  303. // Loop over all tests looking for matching names.
  304. std::vector<cmTest*> const& tests = *this->Makefile->GetTests();
  305. for(std::vector<cmTest*>::const_iterator ti = tests.begin();
  306. ti != tests.end(); ++ti)
  307. {
  308. cmTest* test = *ti;
  309. if(test->GetName() == this->Name)
  310. {
  311. return this->StoreResult(test->GetProperty(this->PropertyName.c_str()));
  312. }
  313. }
  314. // If not found it is an error.
  315. cmOStringStream e;
  316. e << "given TEST name that does not exist: " << this->Name;
  317. this->SetError(e.str().c_str());
  318. return false;
  319. }
  320. //----------------------------------------------------------------------------
  321. bool cmGetPropertyCommand::HandleVariableMode()
  322. {
  323. if(!this->Name.empty())
  324. {
  325. this->SetError("given name for VARIABLE scope.");
  326. return false;
  327. }
  328. return this->StoreResult
  329. (this->Makefile->GetDefinition(this->PropertyName.c_str()));
  330. }