cmGetPropertyCommand.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. return StoreResult(infoType, status.GetMakefile(), variable,
  227. cm->GetState()->GetGlobalProperty(propertyName));
  228. }
  229. bool HandleDirectoryMode(cmExecutionStatus& status, const std::string& name,
  230. OutType infoType, const std::string& variable,
  231. const std::string& propertyName)
  232. {
  233. // Default to the current directory.
  234. cmMakefile* mf = &status.GetMakefile();
  235. // Lookup the directory if given.
  236. if (!name.empty()) {
  237. // Construct the directory name. Interpret relative paths with
  238. // respect to the current directory.
  239. std::string dir = name;
  240. if (!cmSystemTools::FileIsFullPath(dir)) {
  241. dir =
  242. cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/', name);
  243. }
  244. // The local generators are associated with collapsed paths.
  245. dir = cmSystemTools::CollapseFullPath(dir);
  246. // Lookup the generator.
  247. mf = status.GetMakefile().GetGlobalGenerator()->FindMakefile(dir);
  248. if (!mf) {
  249. // Could not find the directory.
  250. status.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 (propertyName == "DEFINITIONS") {
  258. switch (mf->GetPolicyStatus(cmPolicies::CMP0059)) {
  259. case cmPolicies::WARN:
  260. mf->IssueMessage(MessageType::AUTHOR_WARNING,
  261. cmPolicies::GetPolicyWarning(cmPolicies::CMP0059));
  262. CM_FALLTHROUGH;
  263. case cmPolicies::OLD:
  264. return StoreResult(infoType, status.GetMakefile(), variable,
  265. 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 StoreResult(infoType, status.GetMakefile(), variable,
  274. mf->GetProperty(propertyName));
  275. }
  276. bool HandleTargetMode(cmExecutionStatus& status, const std::string& name,
  277. OutType infoType, const std::string& variable,
  278. const std::string& propertyName)
  279. {
  280. if (name.empty()) {
  281. status.SetError("not given name for TARGET scope.");
  282. return false;
  283. }
  284. if (cmTarget* target = status.GetMakefile().FindTargetToUse(name)) {
  285. if (propertyName == "ALIASED_TARGET") {
  286. if (status.GetMakefile().IsAlias(name)) {
  287. return StoreResult(infoType, status.GetMakefile(), variable,
  288. target->GetName().c_str());
  289. }
  290. return StoreResult(infoType, status.GetMakefile(), variable, nullptr);
  291. }
  292. const char* prop_cstr = nullptr;
  293. cmListFileBacktrace bt = status.GetMakefile().GetBacktrace();
  294. cmMessenger* messenger = status.GetMakefile().GetMessenger();
  295. if (cmTargetPropertyComputer::PassesWhitelist(
  296. target->GetType(), propertyName, messenger, bt)) {
  297. prop_cstr = target->GetComputedProperty(propertyName, messenger, bt);
  298. if (!prop_cstr) {
  299. prop_cstr = target->GetProperty(propertyName);
  300. }
  301. }
  302. return StoreResult(infoType, status.GetMakefile(), variable, prop_cstr);
  303. }
  304. status.SetError(cmStrCat("could not find TARGET ", name,
  305. ". Perhaps it has not yet been created."));
  306. return false;
  307. }
  308. bool HandleSourceMode(cmExecutionStatus& status, const std::string& name,
  309. OutType infoType, const std::string& variable,
  310. const std::string& propertyName)
  311. {
  312. if (name.empty()) {
  313. status.SetError("not given name for SOURCE scope.");
  314. return false;
  315. }
  316. // Get the source file.
  317. if (cmSourceFile* sf = status.GetMakefile().GetOrCreateSource(name)) {
  318. return StoreResult(infoType, status.GetMakefile(), variable,
  319. sf->GetPropertyForUser(propertyName));
  320. }
  321. status.SetError(
  322. cmStrCat("given SOURCE name that could not be found or created: ", name));
  323. return false;
  324. }
  325. bool HandleTestMode(cmExecutionStatus& status, const std::string& name,
  326. OutType infoType, const std::string& variable,
  327. const std::string& propertyName)
  328. {
  329. if (name.empty()) {
  330. status.SetError("not given name for TEST scope.");
  331. return false;
  332. }
  333. // Loop over all tests looking for matching names.
  334. if (cmTest* test = status.GetMakefile().GetTest(name)) {
  335. return StoreResult(infoType, status.GetMakefile(), variable,
  336. test->GetProperty(propertyName));
  337. }
  338. // If not found it is an error.
  339. status.SetError(cmStrCat("given TEST name that does not exist: ", name));
  340. return false;
  341. }
  342. bool HandleVariableMode(cmExecutionStatus& status, const std::string& name,
  343. OutType infoType, const std::string& variable,
  344. const std::string& propertyName)
  345. {
  346. if (!name.empty()) {
  347. status.SetError("given name for VARIABLE scope.");
  348. return false;
  349. }
  350. return StoreResult(infoType, status.GetMakefile(), variable,
  351. status.GetMakefile().GetDefinition(propertyName));
  352. }
  353. bool HandleCacheMode(cmExecutionStatus& status, const std::string& name,
  354. OutType infoType, const std::string& variable,
  355. const std::string& propertyName)
  356. {
  357. if (name.empty()) {
  358. status.SetError("not given name for CACHE scope.");
  359. return false;
  360. }
  361. cmProp value = nullptr;
  362. if (status.GetMakefile().GetState()->GetCacheEntryValue(name)) {
  363. value = status.GetMakefile().GetState()->GetCacheEntryProperty(
  364. name, propertyName);
  365. }
  366. StoreResult(infoType, status.GetMakefile(), variable,
  367. value ? value->c_str() : nullptr);
  368. return true;
  369. }
  370. bool HandleInstallMode(cmExecutionStatus& status, const std::string& name,
  371. OutType infoType, const std::string& variable,
  372. const std::string& propertyName)
  373. {
  374. if (name.empty()) {
  375. status.SetError("not given name for INSTALL scope.");
  376. return false;
  377. }
  378. // Get the installed file.
  379. cmake* cm = status.GetMakefile().GetCMakeInstance();
  380. if (cmInstalledFile* file =
  381. cm->GetOrCreateInstalledFile(&status.GetMakefile(), name)) {
  382. std::string value;
  383. bool isSet = file->GetProperty(propertyName, value);
  384. return StoreResult(infoType, status.GetMakefile(), variable,
  385. isSet ? value.c_str() : nullptr);
  386. }
  387. status.SetError(
  388. cmStrCat("given INSTALL name that could not be found or created: ", name));
  389. return false;
  390. }
  391. }