cmGetPropertyCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 const* def = this->Makefile->GetState()->
  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 const* def = this->Makefile->GetState()->
  151. GetPropertyDefinition(this->PropertyName, scope))
  152. {
  153. output = def->GetFullDescription();
  154. }
  155. else
  156. {
  157. output = "NOTFOUND";
  158. }
  159. this->Makefile->AddDefinition(this->Variable, output.c_str());
  160. }
  161. else if(this->InfoType == OutDefined)
  162. {
  163. // Lookup if the property is defined
  164. if(this->Makefile->GetState()->
  165. GetPropertyDefinition(this->PropertyName, scope))
  166. {
  167. this->Makefile->AddDefinition(this->Variable, "1");
  168. }
  169. else
  170. {
  171. this->Makefile->AddDefinition(this->Variable, "0");
  172. }
  173. }
  174. else
  175. {
  176. // Dispatch property getting.
  177. switch(scope)
  178. {
  179. case cmProperty::GLOBAL: return this->HandleGlobalMode();
  180. case cmProperty::DIRECTORY: return this->HandleDirectoryMode();
  181. case cmProperty::TARGET: return this->HandleTargetMode();
  182. case cmProperty::SOURCE_FILE: return this->HandleSourceMode();
  183. case cmProperty::TEST: return this->HandleTestMode();
  184. case cmProperty::VARIABLE: return this->HandleVariableMode();
  185. case cmProperty::CACHE: return this->HandleCacheMode();
  186. case cmProperty::INSTALL: return this->HandleInstallMode();
  187. case cmProperty::CACHED_VARIABLE:
  188. break; // should never happen
  189. }
  190. }
  191. return true;
  192. }
  193. //----------------------------------------------------------------------------
  194. bool cmGetPropertyCommand::StoreResult(const char* value)
  195. {
  196. if(this->InfoType == OutSet)
  197. {
  198. this->Makefile->AddDefinition(this->Variable, value? "1":"0");
  199. }
  200. else // if(this->InfoType == OutValue)
  201. {
  202. if(value)
  203. {
  204. this->Makefile->AddDefinition(this->Variable, value);
  205. }
  206. else
  207. {
  208. this->Makefile->RemoveDefinition(this->Variable);
  209. }
  210. }
  211. return true;
  212. }
  213. //----------------------------------------------------------------------------
  214. bool cmGetPropertyCommand::HandleGlobalMode()
  215. {
  216. if(!this->Name.empty())
  217. {
  218. this->SetError("given name for GLOBAL scope.");
  219. return false;
  220. }
  221. // Get the property.
  222. cmake* cm = this->Makefile->GetCMakeInstance();
  223. return this->StoreResult(cm->GetState()
  224. ->GetGlobalProperty(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->GetCurrentSourceDirectory();
  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. mf = this->Makefile->GetGlobalGenerator()->FindMakefile(dir);
  247. if (!mf)
  248. {
  249. // Could not find the directory.
  250. this->SetError
  251. ("DIRECTORY scope provided but requested directory was not found. "
  252. "This could be because the directory argument was invalid or, "
  253. "it is valid but has not been processed yet.");
  254. return false;
  255. }
  256. }
  257. if (this->PropertyName == "DEFINITIONS")
  258. {
  259. switch(mf->GetPolicyStatus(cmPolicies::CMP0059))
  260. {
  261. case cmPolicies::WARN:
  262. mf->IssueMessage(cmake::AUTHOR_WARNING,
  263. cmPolicies::GetPolicyWarning(cmPolicies::CMP0059));
  264. case cmPolicies::OLD:
  265. return this->StoreResult(mf->GetDefineFlagsCMP0059());
  266. case cmPolicies::NEW:
  267. case cmPolicies::REQUIRED_ALWAYS:
  268. case cmPolicies::REQUIRED_IF_USED:
  269. break;
  270. }
  271. }
  272. // Get the property.
  273. return this->StoreResult(mf->GetProperty(this->PropertyName));
  274. }
  275. //----------------------------------------------------------------------------
  276. bool cmGetPropertyCommand::HandleTargetMode()
  277. {
  278. if(this->Name.empty())
  279. {
  280. this->SetError("not given name for TARGET scope.");
  281. return false;
  282. }
  283. if(this->PropertyName == "ALIASED_TARGET")
  284. {
  285. if(this->Makefile->IsAlias(this->Name))
  286. {
  287. if(cmTarget* target =
  288. this->Makefile->FindTargetToUse(this->Name))
  289. {
  290. return this->StoreResult(target->GetName().c_str());
  291. }
  292. }
  293. return this->StoreResult((this->Variable + "-NOTFOUND").c_str());
  294. }
  295. if(cmTarget* target = this->Makefile->FindTargetToUse(this->Name))
  296. {
  297. return this->StoreResult(target->GetProperty(this->PropertyName,
  298. this->Makefile));
  299. }
  300. else
  301. {
  302. std::ostringstream e;
  303. e << "could not find TARGET " << this->Name
  304. << ". Perhaps it has not yet been created.";
  305. this->SetError(e.str());
  306. return false;
  307. }
  308. }
  309. //----------------------------------------------------------------------------
  310. bool cmGetPropertyCommand::HandleSourceMode()
  311. {
  312. if(this->Name.empty())
  313. {
  314. this->SetError("not given name for SOURCE scope.");
  315. return false;
  316. }
  317. // Get the source file.
  318. if(cmSourceFile* sf =
  319. this->Makefile->GetOrCreateSource(this->Name))
  320. {
  321. return
  322. this->StoreResult(sf->GetPropertyForUser(this->PropertyName));
  323. }
  324. else
  325. {
  326. std::ostringstream e;
  327. e << "given SOURCE name that could not be found or created: "
  328. << this->Name;
  329. this->SetError(e.str());
  330. return false;
  331. }
  332. }
  333. //----------------------------------------------------------------------------
  334. bool cmGetPropertyCommand::HandleTestMode()
  335. {
  336. if(this->Name.empty())
  337. {
  338. this->SetError("not given name for TEST scope.");
  339. return false;
  340. }
  341. // Loop over all tests looking for matching names.
  342. if(cmTest* test = this->Makefile->GetTest(this->Name))
  343. {
  344. return this->StoreResult(test->GetProperty(this->PropertyName));
  345. }
  346. // If not found it is an error.
  347. std::ostringstream e;
  348. e << "given TEST name that does not exist: " << this->Name;
  349. this->SetError(e.str());
  350. return false;
  351. }
  352. //----------------------------------------------------------------------------
  353. bool cmGetPropertyCommand::HandleVariableMode()
  354. {
  355. if(!this->Name.empty())
  356. {
  357. this->SetError("given name for VARIABLE scope.");
  358. return false;
  359. }
  360. return this->StoreResult
  361. (this->Makefile->GetDefinition(this->PropertyName));
  362. }
  363. //----------------------------------------------------------------------------
  364. bool cmGetPropertyCommand::HandleCacheMode()
  365. {
  366. if(this->Name.empty())
  367. {
  368. this->SetError("not given name for CACHE scope.");
  369. return false;
  370. }
  371. const char* value = 0;
  372. if(this->Makefile->GetState()->GetCacheEntryValue(this->Name))
  373. {
  374. value = this->Makefile->GetState()
  375. ->GetCacheEntryProperty(this->Name, this->PropertyName);
  376. }
  377. this->StoreResult(value);
  378. return true;
  379. }
  380. //----------------------------------------------------------------------------
  381. bool cmGetPropertyCommand::HandleInstallMode()
  382. {
  383. if(this->Name.empty())
  384. {
  385. this->SetError("not given name for INSTALL scope.");
  386. return false;
  387. }
  388. // Get the installed file.
  389. cmake* cm = this->Makefile->GetCMakeInstance();
  390. if(cmInstalledFile* file = cm->GetOrCreateInstalledFile(
  391. this->Makefile, this->Name))
  392. {
  393. std::string value;
  394. bool isSet = file->GetProperty(this->PropertyName, value);
  395. return this->StoreResult(isSet ? value.c_str() : 0);
  396. }
  397. else
  398. {
  399. std::ostringstream e;
  400. e << "given INSTALL name that could not be found or created: "
  401. << this->Name;
  402. this->SetError(e.str());
  403. return false;
  404. }
  405. }