cmGetPropertyCommand.cxx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGetPropertyCommand.h"
  14. #include "cmake.h"
  15. #include "cmTest.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
  60. {
  61. cmOStringStream e;
  62. e << "given invalid scope " << args[1] << ". "
  63. << "Valid scopes are "
  64. << "GLOBAL, DIRECTORY, TARGET, SOURCE, TEST, VARIABLE.";
  65. this->SetError(e.str().c_str());
  66. return false;
  67. }
  68. // Parse remaining arguments.
  69. enum Doing { DoingNone, DoingName, DoingProperty, DoingType };
  70. Doing doing = DoingName;
  71. for(unsigned int i=2; i < args.size(); ++i)
  72. {
  73. if(args[i] == "PROPERTY")
  74. {
  75. doing = DoingProperty;
  76. }
  77. else if(args[i] == "BRIEF_DOCS")
  78. {
  79. doing = DoingNone;
  80. this->InfoType = OutBriefDoc;
  81. }
  82. else if(args[i] == "FULL_DOCS")
  83. {
  84. doing = DoingNone;
  85. this->InfoType = OutFullDoc;
  86. }
  87. else if(args[i] == "DEFINED")
  88. {
  89. doing = DoingNone;
  90. this->InfoType = OutDefined;
  91. }
  92. else if(doing == DoingName)
  93. {
  94. doing = DoingNone;
  95. this->Name = args[i];
  96. }
  97. else if(doing == DoingProperty)
  98. {
  99. doing = DoingNone;
  100. this->PropertyName = args[i];
  101. }
  102. else
  103. {
  104. cmOStringStream e;
  105. e << "given invalid argument \"" << args[i] << "\".";
  106. this->SetError(e.str().c_str());
  107. return false;
  108. }
  109. }
  110. // Make sure a property name was found.
  111. if(this->PropertyName.empty())
  112. {
  113. this->SetError("not given a PROPERTY <name> argument.");
  114. return false;
  115. }
  116. // Compute requested output.
  117. if(this->InfoType == OutBriefDoc)
  118. {
  119. // Lookup brief documentation.
  120. std::string output;
  121. if(cmPropertyDefinition* def =
  122. this->Makefile->GetCMakeInstance()->
  123. GetPropertyDefinition(this->PropertyName.c_str(), scope))
  124. {
  125. output = def->GetShortDescription();
  126. }
  127. else
  128. {
  129. output = "NOTFOUND";
  130. }
  131. this->Makefile->AddDefinition(this->Variable.c_str(), output.c_str());
  132. }
  133. else if(this->InfoType == OutFullDoc)
  134. {
  135. // Lookup full documentation.
  136. std::string output;
  137. if(cmPropertyDefinition* def =
  138. this->Makefile->GetCMakeInstance()->
  139. GetPropertyDefinition(this->PropertyName.c_str(), scope))
  140. {
  141. output = def->GetFullDescription();
  142. }
  143. else
  144. {
  145. output = "NOTFOUND";
  146. }
  147. this->Makefile->AddDefinition(this->Variable.c_str(), output.c_str());
  148. }
  149. else
  150. {
  151. // Dispatch property getting.
  152. switch(scope)
  153. {
  154. case cmProperty::GLOBAL: return this->HandleGlobalMode();
  155. case cmProperty::DIRECTORY: return this->HandleDirectoryMode();
  156. case cmProperty::TARGET: return this->HandleTargetMode();
  157. case cmProperty::SOURCE_FILE: return this->HandleSourceMode();
  158. case cmProperty::TEST: return this->HandleTestMode();
  159. case cmProperty::VARIABLE: return this->HandleVariableMode();
  160. case cmProperty::CACHED_VARIABLE:
  161. break; // should never happen
  162. }
  163. }
  164. return true;
  165. }
  166. //----------------------------------------------------------------------------
  167. bool cmGetPropertyCommand::StoreResult(const char* value)
  168. {
  169. if(this->InfoType == OutDefined)
  170. {
  171. this->Makefile->AddDefinition(this->Variable.c_str(), value? "1":"0");
  172. }
  173. else // if(this->InfoType == OutValue)
  174. {
  175. this->Makefile->AddDefinition(this->Variable.c_str(), value);
  176. }
  177. return true;
  178. }
  179. //----------------------------------------------------------------------------
  180. bool cmGetPropertyCommand::HandleGlobalMode()
  181. {
  182. if(!this->Name.empty())
  183. {
  184. this->SetError("given name for GLOBAL scope.");
  185. return false;
  186. }
  187. // Get the property.
  188. cmake* cm = this->Makefile->GetCMakeInstance();
  189. return this->StoreResult(cm->GetProperty(this->PropertyName.c_str()));
  190. }
  191. //----------------------------------------------------------------------------
  192. bool cmGetPropertyCommand::HandleDirectoryMode()
  193. {
  194. // Default to the current directory.
  195. cmMakefile* mf = this->Makefile;
  196. // Lookup the directory if given.
  197. if(!this->Name.empty())
  198. {
  199. // Construct the directory name. Interpret relative paths with
  200. // respect to the current directory.
  201. std::string dir = this->Name;
  202. if(!cmSystemTools::FileIsFullPath(dir.c_str()))
  203. {
  204. dir = this->Makefile->GetCurrentDirectory();
  205. dir += "/";
  206. dir += this->Name;
  207. }
  208. // The local generators are associated with collapsed paths.
  209. dir = cmSystemTools::CollapseFullPath(dir.c_str());
  210. // Lookup the generator.
  211. if(cmLocalGenerator* lg =
  212. (this->Makefile->GetLocalGenerator()
  213. ->GetGlobalGenerator()->FindLocalGenerator(dir.c_str())))
  214. {
  215. // Use the makefile for the directory found.
  216. mf = lg->GetMakefile();
  217. }
  218. else
  219. {
  220. // Could not find the directory.
  221. this->SetError
  222. ("DIRECTORY scope provided but requested directory was not found. "
  223. "This could be because the directory argument was invalid or, "
  224. "it is valid but has not been processed yet.");
  225. return false;
  226. }
  227. }
  228. // Get the property.
  229. return this->StoreResult(mf->GetProperty(this->PropertyName.c_str()));
  230. }
  231. //----------------------------------------------------------------------------
  232. bool cmGetPropertyCommand::HandleTargetMode()
  233. {
  234. if(this->Name.empty())
  235. {
  236. this->SetError("not given name for TARGET scope.");
  237. return false;
  238. }
  239. if(cmTarget* target =
  240. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  241. ->FindTarget(0, this->Name.c_str(), true))
  242. {
  243. return this->StoreResult(target->GetProperty(this->PropertyName.c_str()));
  244. }
  245. else
  246. {
  247. cmOStringStream e;
  248. e << "could not find TARGET " << this->Name
  249. << ". Perhaps it has not yet been created.";
  250. this->SetError(e.str().c_str());
  251. return false;
  252. }
  253. }
  254. //----------------------------------------------------------------------------
  255. bool cmGetPropertyCommand::HandleSourceMode()
  256. {
  257. if(this->Name.empty())
  258. {
  259. this->SetError("not given name for SOURCE scope.");
  260. return false;
  261. }
  262. // Get the source file.
  263. if(cmSourceFile* sf =
  264. this->Makefile->GetOrCreateSource(this->Name.c_str()))
  265. {
  266. return this->StoreResult(sf->GetProperty(this->PropertyName.c_str()));
  267. }
  268. else
  269. {
  270. cmOStringStream e;
  271. e << "given SOURCE name that could not be found or created: "
  272. << this->Name;
  273. this->SetError(e.str().c_str());
  274. return false;
  275. }
  276. }
  277. //----------------------------------------------------------------------------
  278. bool cmGetPropertyCommand::HandleTestMode()
  279. {
  280. if(this->Name.empty())
  281. {
  282. this->SetError("not given name for TEST scope.");
  283. return false;
  284. }
  285. // Loop over all tests looking for matching names.
  286. std::vector<cmTest*> const& tests = *this->Makefile->GetTests();
  287. for(std::vector<cmTest*>::const_iterator ti = tests.begin();
  288. ti != tests.end(); ++ti)
  289. {
  290. cmTest* test = *ti;
  291. if(test->GetName() == this->Name)
  292. {
  293. return this->StoreResult(test->GetProperty(this->PropertyName.c_str()));
  294. }
  295. }
  296. // If not found it is an error.
  297. cmOStringStream e;
  298. e << "given TEST name that does not exist: " << this->Name;
  299. this->SetError(e.str().c_str());
  300. return false;
  301. }
  302. //----------------------------------------------------------------------------
  303. bool cmGetPropertyCommand::HandleVariableMode()
  304. {
  305. if(!this->Name.empty())
  306. {
  307. this->SetError("given name for VARIABLE scope.");
  308. return false;
  309. }
  310. return this->StoreResult
  311. (this->Makefile->GetDefinition(this->PropertyName.c_str()));
  312. }