cmGetPropertyCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmGetPropertyCommand.h"
  4. #include <sstream>
  5. #include "cmGlobalGenerator.h"
  6. #include "cmInstalledFile.h"
  7. #include "cmListFileCache.h"
  8. #include "cmMakefile.h"
  9. #include "cmPolicies.h"
  10. #include "cmProperty.h"
  11. #include "cmPropertyDefinition.h"
  12. #include "cmSourceFile.h"
  13. #include "cmState.h"
  14. #include "cmSystemTools.h"
  15. #include "cmTarget.h"
  16. #include "cmTargetPropertyComputer.h"
  17. #include "cmTest.h"
  18. #include "cmake.h"
  19. class cmExecutionStatus;
  20. class cmMessenger;
  21. cmGetPropertyCommand::cmGetPropertyCommand()
  22. {
  23. this->InfoType = OutValue;
  24. }
  25. bool cmGetPropertyCommand::InitialPass(std::vector<std::string> const& args,
  26. cmExecutionStatus&)
  27. {
  28. if (args.size() < 3) {
  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. scope = cmProperty::GLOBAL;
  38. } else if (args[1] == "DIRECTORY") {
  39. scope = cmProperty::DIRECTORY;
  40. } else if (args[1] == "TARGET") {
  41. scope = cmProperty::TARGET;
  42. } else if (args[1] == "SOURCE") {
  43. scope = cmProperty::SOURCE_FILE;
  44. } else if (args[1] == "TEST") {
  45. scope = cmProperty::TEST;
  46. } else if (args[1] == "VARIABLE") {
  47. scope = cmProperty::VARIABLE;
  48. } else if (args[1] == "CACHE") {
  49. scope = cmProperty::CACHE;
  50. } else if (args[1] == "INSTALL") {
  51. scope = cmProperty::INSTALL;
  52. } else {
  53. std::ostringstream e;
  54. e << "given invalid scope " << args[1] << ". "
  55. << "Valid scopes are "
  56. << "GLOBAL, DIRECTORY, TARGET, SOURCE, TEST, VARIABLE, CACHE, INSTALL.";
  57. this->SetError(e.str());
  58. return false;
  59. }
  60. // Parse remaining arguments.
  61. enum Doing
  62. {
  63. DoingNone,
  64. DoingName,
  65. DoingProperty,
  66. DoingType
  67. };
  68. Doing doing = DoingName;
  69. for (unsigned int i = 2; i < args.size(); ++i) {
  70. if (args[i] == "PROPERTY") {
  71. doing = DoingProperty;
  72. } else if (args[i] == "BRIEF_DOCS") {
  73. doing = DoingNone;
  74. this->InfoType = OutBriefDoc;
  75. } else if (args[i] == "FULL_DOCS") {
  76. doing = DoingNone;
  77. this->InfoType = OutFullDoc;
  78. } else if (args[i] == "SET") {
  79. doing = DoingNone;
  80. this->InfoType = OutSet;
  81. } else if (args[i] == "DEFINED") {
  82. doing = DoingNone;
  83. this->InfoType = OutDefined;
  84. } else if (doing == DoingName) {
  85. doing = DoingNone;
  86. this->Name = args[i];
  87. } else if (doing == DoingProperty) {
  88. doing = DoingNone;
  89. this->PropertyName = args[i];
  90. } else {
  91. std::ostringstream e;
  92. e << "given invalid argument \"" << args[i] << "\".";
  93. this->SetError(e.str());
  94. return false;
  95. }
  96. }
  97. // Make sure a property name was found.
  98. if (this->PropertyName.empty()) {
  99. this->SetError("not given a PROPERTY <name> argument.");
  100. return false;
  101. }
  102. // Compute requested output.
  103. if (this->InfoType == OutBriefDoc) {
  104. // Lookup brief documentation.
  105. std::string output;
  106. if (cmPropertyDefinition const* def =
  107. this->Makefile->GetState()->GetPropertyDefinition(this->PropertyName,
  108. scope)) {
  109. output = def->GetShortDescription();
  110. } else {
  111. output = "NOTFOUND";
  112. }
  113. this->Makefile->AddDefinition(this->Variable, output.c_str());
  114. } else if (this->InfoType == OutFullDoc) {
  115. // Lookup full documentation.
  116. std::string output;
  117. if (cmPropertyDefinition const* def =
  118. this->Makefile->GetState()->GetPropertyDefinition(this->PropertyName,
  119. scope)) {
  120. output = def->GetFullDescription();
  121. } else {
  122. output = "NOTFOUND";
  123. }
  124. this->Makefile->AddDefinition(this->Variable, output.c_str());
  125. } else if (this->InfoType == OutDefined) {
  126. // Lookup if the property is defined
  127. if (this->Makefile->GetState()->GetPropertyDefinition(this->PropertyName,
  128. scope)) {
  129. this->Makefile->AddDefinition(this->Variable, "1");
  130. } else {
  131. this->Makefile->AddDefinition(this->Variable, "0");
  132. }
  133. } else {
  134. // Dispatch property getting.
  135. switch (scope) {
  136. case cmProperty::GLOBAL:
  137. return this->HandleGlobalMode();
  138. case cmProperty::DIRECTORY:
  139. return this->HandleDirectoryMode();
  140. case cmProperty::TARGET:
  141. return this->HandleTargetMode();
  142. case cmProperty::SOURCE_FILE:
  143. return this->HandleSourceMode();
  144. case cmProperty::TEST:
  145. return this->HandleTestMode();
  146. case cmProperty::VARIABLE:
  147. return this->HandleVariableMode();
  148. case cmProperty::CACHE:
  149. return this->HandleCacheMode();
  150. case cmProperty::INSTALL:
  151. return this->HandleInstallMode();
  152. case cmProperty::CACHED_VARIABLE:
  153. break; // should never happen
  154. }
  155. }
  156. return true;
  157. }
  158. bool cmGetPropertyCommand::StoreResult(const char* value)
  159. {
  160. if (this->InfoType == OutSet) {
  161. this->Makefile->AddDefinition(this->Variable, value ? "1" : "0");
  162. } else // if(this->InfoType == OutValue)
  163. {
  164. if (value) {
  165. this->Makefile->AddDefinition(this->Variable, value);
  166. } else {
  167. this->Makefile->RemoveDefinition(this->Variable);
  168. }
  169. }
  170. return true;
  171. }
  172. bool cmGetPropertyCommand::HandleGlobalMode()
  173. {
  174. if (!this->Name.empty()) {
  175. this->SetError("given name for GLOBAL scope.");
  176. return false;
  177. }
  178. // Get the property.
  179. cmake* cm = this->Makefile->GetCMakeInstance();
  180. return this->StoreResult(
  181. cm->GetState()->GetGlobalProperty(this->PropertyName));
  182. }
  183. bool cmGetPropertyCommand::HandleDirectoryMode()
  184. {
  185. // Default to the current directory.
  186. cmMakefile* mf = this->Makefile;
  187. // Lookup the directory if given.
  188. if (!this->Name.empty()) {
  189. // Construct the directory name. Interpret relative paths with
  190. // respect to the current directory.
  191. std::string dir = this->Name;
  192. if (!cmSystemTools::FileIsFullPath(dir)) {
  193. dir = this->Makefile->GetCurrentSourceDirectory();
  194. dir += "/";
  195. dir += this->Name;
  196. }
  197. // The local generators are associated with collapsed paths.
  198. dir = cmSystemTools::CollapseFullPath(dir);
  199. // Lookup the generator.
  200. mf = this->Makefile->GetGlobalGenerator()->FindMakefile(dir);
  201. if (!mf) {
  202. // Could not find the directory.
  203. this->SetError(
  204. "DIRECTORY scope provided but requested directory was not found. "
  205. "This could be because the directory argument was invalid or, "
  206. "it is valid but has not been processed yet.");
  207. return false;
  208. }
  209. }
  210. if (this->PropertyName == "DEFINITIONS") {
  211. switch (mf->GetPolicyStatus(cmPolicies::CMP0059)) {
  212. case cmPolicies::WARN:
  213. mf->IssueMessage(cmake::AUTHOR_WARNING,
  214. cmPolicies::GetPolicyWarning(cmPolicies::CMP0059));
  215. CM_FALLTHROUGH;
  216. case cmPolicies::OLD:
  217. return this->StoreResult(mf->GetDefineFlagsCMP0059());
  218. case cmPolicies::NEW:
  219. case cmPolicies::REQUIRED_ALWAYS:
  220. case cmPolicies::REQUIRED_IF_USED:
  221. break;
  222. }
  223. }
  224. // Get the property.
  225. return this->StoreResult(mf->GetProperty(this->PropertyName));
  226. }
  227. bool cmGetPropertyCommand::HandleTargetMode()
  228. {
  229. if (this->Name.empty()) {
  230. this->SetError("not given name for TARGET scope.");
  231. return false;
  232. }
  233. if (cmTarget* target = this->Makefile->FindTargetToUse(this->Name)) {
  234. if (this->PropertyName == "ALIASED_TARGET") {
  235. if (this->Makefile->IsAlias(this->Name)) {
  236. return this->StoreResult(target->GetName().c_str());
  237. }
  238. return this->StoreResult(nullptr);
  239. }
  240. const char* prop_cstr = nullptr;
  241. cmListFileBacktrace bt = this->Makefile->GetBacktrace();
  242. cmMessenger* messenger = this->Makefile->GetMessenger();
  243. if (cmTargetPropertyComputer::PassesWhitelist(
  244. target->GetType(), this->PropertyName, messenger, bt)) {
  245. prop_cstr =
  246. target->GetComputedProperty(this->PropertyName, messenger, bt);
  247. if (!prop_cstr) {
  248. prop_cstr = target->GetProperty(this->PropertyName);
  249. }
  250. }
  251. return this->StoreResult(prop_cstr);
  252. }
  253. std::ostringstream e;
  254. e << "could not find TARGET " << this->Name
  255. << ". Perhaps it has not yet been created.";
  256. this->SetError(e.str());
  257. return false;
  258. }
  259. bool cmGetPropertyCommand::HandleSourceMode()
  260. {
  261. if (this->Name.empty()) {
  262. this->SetError("not given name for SOURCE scope.");
  263. return false;
  264. }
  265. // Get the source file.
  266. if (cmSourceFile* sf = this->Makefile->GetOrCreateSource(this->Name)) {
  267. return this->StoreResult(sf->GetPropertyForUser(this->PropertyName));
  268. }
  269. std::ostringstream e;
  270. e << "given SOURCE name that could not be found or created: " << this->Name;
  271. this->SetError(e.str());
  272. return false;
  273. }
  274. bool cmGetPropertyCommand::HandleTestMode()
  275. {
  276. if (this->Name.empty()) {
  277. this->SetError("not given name for TEST scope.");
  278. return false;
  279. }
  280. // Loop over all tests looking for matching names.
  281. if (cmTest* test = this->Makefile->GetTest(this->Name)) {
  282. return this->StoreResult(test->GetProperty(this->PropertyName));
  283. }
  284. // If not found it is an error.
  285. std::ostringstream e;
  286. e << "given TEST name that does not exist: " << this->Name;
  287. this->SetError(e.str());
  288. return false;
  289. }
  290. bool cmGetPropertyCommand::HandleVariableMode()
  291. {
  292. if (!this->Name.empty()) {
  293. this->SetError("given name for VARIABLE scope.");
  294. return false;
  295. }
  296. return this->StoreResult(this->Makefile->GetDefinition(this->PropertyName));
  297. }
  298. bool cmGetPropertyCommand::HandleCacheMode()
  299. {
  300. if (this->Name.empty()) {
  301. this->SetError("not given name for CACHE scope.");
  302. return false;
  303. }
  304. const char* value = nullptr;
  305. if (this->Makefile->GetState()->GetCacheEntryValue(this->Name)) {
  306. value = this->Makefile->GetState()->GetCacheEntryProperty(
  307. this->Name, this->PropertyName);
  308. }
  309. this->StoreResult(value);
  310. return true;
  311. }
  312. bool cmGetPropertyCommand::HandleInstallMode()
  313. {
  314. if (this->Name.empty()) {
  315. this->SetError("not given name for INSTALL scope.");
  316. return false;
  317. }
  318. // Get the installed file.
  319. cmake* cm = this->Makefile->GetCMakeInstance();
  320. if (cmInstalledFile* file =
  321. cm->GetOrCreateInstalledFile(this->Makefile, this->Name)) {
  322. std::string value;
  323. bool isSet = file->GetProperty(this->PropertyName, value);
  324. return this->StoreResult(isSet ? value.c_str() : nullptr);
  325. }
  326. std::ostringstream e;
  327. e << "given INSTALL name that could not be found or created: " << this->Name;
  328. this->SetError(e.str());
  329. return false;
  330. }