cmGetPropertyCommand.cxx 14 KB

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