cmGetPropertyCommand.cxx 17 KB

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