cmGetPropertyCommand.cxx 10 KB

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