cmGetPropertyCommand.cxx 10 KB

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