cmGetPropertyCommand.cxx 11 KB

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