cmGetPropertyCommand.cxx 16 KB

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