cmGetPropertyCommand.cxx 11 KB

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