cmGetPropertyCommand.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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
  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());
  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());
  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, scope))
  133. {
  134. output = def->GetShortDescription();
  135. }
  136. else
  137. {
  138. output = "NOTFOUND";
  139. }
  140. this->Makefile->AddDefinition(this->Variable, 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, scope))
  149. {
  150. output = def->GetFullDescription();
  151. }
  152. else
  153. {
  154. output = "NOTFOUND";
  155. }
  156. this->Makefile->AddDefinition(this->Variable, 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, scope))
  163. {
  164. this->Makefile->AddDefinition(this->Variable, "1");
  165. }
  166. else
  167. {
  168. this->Makefile->AddDefinition(this->Variable, "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, value? "1":"0");
  195. }
  196. else // if(this->InfoType == OutValue)
  197. {
  198. if(value)
  199. {
  200. this->Makefile->AddDefinition(this->Variable, value);
  201. }
  202. else
  203. {
  204. this->Makefile->RemoveDefinition(this->Variable);
  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));
  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)))
  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));
  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(this->PropertyName == "ALIASED_TARGET")
  270. {
  271. if(this->Makefile->IsAlias(this->Name))
  272. {
  273. if(cmTarget* target =
  274. this->Makefile->FindTargetToUse(this->Name))
  275. {
  276. return this->StoreResult(target->GetName().c_str());
  277. }
  278. }
  279. return this->StoreResult((this->Variable + "-NOTFOUND").c_str());
  280. }
  281. if(cmTarget* target = this->Makefile->FindTargetToUse(this->Name))
  282. {
  283. return this->StoreResult(target->GetProperty(this->PropertyName));
  284. }
  285. else
  286. {
  287. cmOStringStream e;
  288. e << "could not find TARGET " << this->Name
  289. << ". Perhaps it has not yet been created.";
  290. this->SetError(e.str());
  291. return false;
  292. }
  293. }
  294. //----------------------------------------------------------------------------
  295. bool cmGetPropertyCommand::HandleSourceMode()
  296. {
  297. if(this->Name.empty())
  298. {
  299. this->SetError("not given name for SOURCE scope.");
  300. return false;
  301. }
  302. // Get the source file.
  303. if(cmSourceFile* sf =
  304. this->Makefile->GetOrCreateSource(this->Name))
  305. {
  306. return
  307. this->StoreResult(sf->GetPropertyForUser(this->PropertyName));
  308. }
  309. else
  310. {
  311. cmOStringStream e;
  312. e << "given SOURCE name that could not be found or created: "
  313. << this->Name;
  314. this->SetError(e.str());
  315. return false;
  316. }
  317. }
  318. //----------------------------------------------------------------------------
  319. bool cmGetPropertyCommand::HandleTestMode()
  320. {
  321. if(this->Name.empty())
  322. {
  323. this->SetError("not given name for TEST scope.");
  324. return false;
  325. }
  326. // Loop over all tests looking for matching names.
  327. if(cmTest* test = this->Makefile->GetTest(this->Name))
  328. {
  329. return this->StoreResult(test->GetProperty(this->PropertyName));
  330. }
  331. // If not found it is an error.
  332. cmOStringStream e;
  333. e << "given TEST name that does not exist: " << this->Name;
  334. this->SetError(e.str());
  335. return false;
  336. }
  337. //----------------------------------------------------------------------------
  338. bool cmGetPropertyCommand::HandleVariableMode()
  339. {
  340. if(!this->Name.empty())
  341. {
  342. this->SetError("given name for VARIABLE scope.");
  343. return false;
  344. }
  345. return this->StoreResult
  346. (this->Makefile->GetDefinition(this->PropertyName));
  347. }
  348. //----------------------------------------------------------------------------
  349. bool cmGetPropertyCommand::HandleCacheMode()
  350. {
  351. if(this->Name.empty())
  352. {
  353. this->SetError("not given name for CACHE scope.");
  354. return false;
  355. }
  356. const char* value = 0;
  357. cmCacheManager::CacheIterator it =
  358. this->Makefile->GetCacheManager()->GetCacheIterator(this->Name.c_str());
  359. if(!it.IsAtEnd())
  360. {
  361. value = it.GetProperty(this->PropertyName);
  362. }
  363. this->StoreResult(value);
  364. return true;
  365. }