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