cmGetPropertyCommand.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 "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 "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. cmMakefile& directory_makefile,
  48. bool source_file_paths_should_be_absolute);
  49. bool HandleTestMode(cmExecutionStatus& status, const std::string& name,
  50. OutType infoType, const std::string& variable,
  51. const std::string& propertyName);
  52. bool HandleVariableMode(cmExecutionStatus& status, const std::string& name,
  53. OutType infoType, const std::string& variable,
  54. const std::string& propertyName);
  55. bool HandleCacheMode(cmExecutionStatus& status, const std::string& name,
  56. OutType infoType, const std::string& variable,
  57. const std::string& propertyName);
  58. bool HandleInstallMode(cmExecutionStatus& status, const std::string& name,
  59. OutType infoType, const std::string& variable,
  60. const std::string& propertyName);
  61. }
  62. bool cmGetPropertyCommand(std::vector<std::string> const& args,
  63. cmExecutionStatus& status)
  64. {
  65. OutType infoType = OutValue;
  66. if (args.size() < 3) {
  67. status.SetError("called with incorrect number of arguments");
  68. return false;
  69. }
  70. // The cmake variable in which to store the result.
  71. const std::string variable = args[0];
  72. std::string name;
  73. std::string propertyName;
  74. std::vector<std::string> source_file_directories;
  75. std::vector<std::string> source_file_target_directories;
  76. bool source_file_directory_option_enabled = false;
  77. bool source_file_target_option_enabled = false;
  78. // Get the scope from which to get the property.
  79. cmProperty::ScopeType scope;
  80. if (args[1] == "GLOBAL") {
  81. scope = cmProperty::GLOBAL;
  82. } else if (args[1] == "DIRECTORY") {
  83. scope = cmProperty::DIRECTORY;
  84. } else if (args[1] == "TARGET") {
  85. scope = cmProperty::TARGET;
  86. } else if (args[1] == "SOURCE") {
  87. scope = cmProperty::SOURCE_FILE;
  88. } else if (args[1] == "TEST") {
  89. scope = cmProperty::TEST;
  90. } else if (args[1] == "VARIABLE") {
  91. scope = cmProperty::VARIABLE;
  92. } else if (args[1] == "CACHE") {
  93. scope = cmProperty::CACHE;
  94. } else if (args[1] == "INSTALL") {
  95. scope = cmProperty::INSTALL;
  96. } else {
  97. status.SetError(cmStrCat(
  98. "given invalid scope ", args[1],
  99. ". "
  100. "Valid scopes are "
  101. "GLOBAL, DIRECTORY, TARGET, SOURCE, TEST, VARIABLE, CACHE, INSTALL."));
  102. return false;
  103. }
  104. // Parse remaining arguments.
  105. enum Doing
  106. {
  107. DoingNone,
  108. DoingName,
  109. DoingProperty,
  110. DoingType,
  111. DoingSourceDirectory,
  112. DoingSourceTargetDirectory
  113. };
  114. Doing doing = DoingName;
  115. for (unsigned int i = 2; i < args.size(); ++i) {
  116. if (args[i] == "PROPERTY") {
  117. doing = DoingProperty;
  118. } else if (args[i] == "BRIEF_DOCS") {
  119. doing = DoingNone;
  120. infoType = OutBriefDoc;
  121. } else if (args[i] == "FULL_DOCS") {
  122. doing = DoingNone;
  123. infoType = OutFullDoc;
  124. } else if (args[i] == "SET") {
  125. doing = DoingNone;
  126. infoType = OutSet;
  127. } else if (args[i] == "DEFINED") {
  128. doing = DoingNone;
  129. infoType = OutDefined;
  130. } else if (doing == DoingName) {
  131. doing = DoingNone;
  132. name = args[i];
  133. } else if (doing == DoingNone && scope == cmProperty::SOURCE_FILE &&
  134. args[i] == "DIRECTORY") {
  135. doing = DoingSourceDirectory;
  136. source_file_directory_option_enabled = true;
  137. } else if (doing == DoingNone && scope == cmProperty::SOURCE_FILE &&
  138. args[i] == "TARGET_DIRECTORY") {
  139. doing = DoingSourceTargetDirectory;
  140. source_file_target_option_enabled = true;
  141. } else if (doing == DoingSourceDirectory) {
  142. source_file_directories.push_back(args[i]);
  143. doing = DoingNone;
  144. } else if (doing == DoingSourceTargetDirectory) {
  145. source_file_target_directories.push_back(args[i]);
  146. doing = DoingNone;
  147. } else if (doing == DoingProperty) {
  148. doing = DoingNone;
  149. propertyName = args[i];
  150. } else {
  151. status.SetError(cmStrCat("given invalid argument \"", args[i], "\"."));
  152. return false;
  153. }
  154. }
  155. // Make sure a property name was found.
  156. if (propertyName.empty()) {
  157. status.SetError("not given a PROPERTY <name> argument.");
  158. return false;
  159. }
  160. std::vector<cmMakefile*> source_file_directory_makefiles;
  161. bool file_scopes_handled =
  162. SetPropertyCommand::HandleAndValidateSourceFileDirectortoryScopes(
  163. status, source_file_directory_option_enabled,
  164. source_file_target_option_enabled, source_file_directories,
  165. source_file_target_directories, source_file_directory_makefiles);
  166. if (!file_scopes_handled) {
  167. return false;
  168. }
  169. // Compute requested output.
  170. if (infoType == OutBriefDoc) {
  171. // Lookup brief documentation.
  172. std::string output;
  173. if (cmPropertyDefinition const* def =
  174. status.GetMakefile().GetState()->GetPropertyDefinition(propertyName,
  175. scope)) {
  176. output = def->GetShortDescription();
  177. } else {
  178. output = "NOTFOUND";
  179. }
  180. status.GetMakefile().AddDefinition(variable, output);
  181. } else if (infoType == OutFullDoc) {
  182. // Lookup full documentation.
  183. std::string output;
  184. if (cmPropertyDefinition const* def =
  185. status.GetMakefile().GetState()->GetPropertyDefinition(propertyName,
  186. scope)) {
  187. output = def->GetFullDescription();
  188. } else {
  189. output = "NOTFOUND";
  190. }
  191. status.GetMakefile().AddDefinition(variable, output);
  192. } else if (infoType == OutDefined) {
  193. // Lookup if the property is defined
  194. if (status.GetMakefile().GetState()->GetPropertyDefinition(propertyName,
  195. scope)) {
  196. status.GetMakefile().AddDefinition(variable, "1");
  197. } else {
  198. status.GetMakefile().AddDefinition(variable, "0");
  199. }
  200. } else {
  201. // Dispatch property getting.
  202. cmMakefile& directory_scope_mf = *(source_file_directory_makefiles[0]);
  203. bool source_file_paths_should_be_absolute =
  204. source_file_directory_option_enabled ||
  205. source_file_target_option_enabled;
  206. switch (scope) {
  207. case cmProperty::GLOBAL:
  208. return HandleGlobalMode(status, name, infoType, variable,
  209. propertyName);
  210. case cmProperty::DIRECTORY:
  211. return HandleDirectoryMode(status, name, infoType, variable,
  212. propertyName);
  213. case cmProperty::TARGET:
  214. return HandleTargetMode(status, name, infoType, variable,
  215. propertyName);
  216. case cmProperty::SOURCE_FILE:
  217. return HandleSourceMode(status, name, infoType, variable, propertyName,
  218. directory_scope_mf,
  219. source_file_paths_should_be_absolute);
  220. case cmProperty::TEST:
  221. return HandleTestMode(status, name, infoType, variable, propertyName);
  222. case cmProperty::VARIABLE:
  223. return HandleVariableMode(status, name, infoType, variable,
  224. propertyName);
  225. case cmProperty::CACHE:
  226. return HandleCacheMode(status, name, infoType, variable, propertyName);
  227. case cmProperty::INSTALL:
  228. return HandleInstallMode(status, name, infoType, variable,
  229. propertyName);
  230. case cmProperty::CACHED_VARIABLE:
  231. break; // should never happen
  232. }
  233. }
  234. return true;
  235. }
  236. namespace {
  237. bool StoreResult(OutType infoType, cmMakefile& makefile,
  238. const std::string& variable, const char* value)
  239. {
  240. if (infoType == OutSet) {
  241. makefile.AddDefinition(variable, value ? "1" : "0");
  242. } else // if(infoType == OutValue)
  243. {
  244. if (value) {
  245. makefile.AddDefinition(variable, value);
  246. } else {
  247. makefile.RemoveDefinition(variable);
  248. }
  249. }
  250. return true;
  251. }
  252. bool HandleGlobalMode(cmExecutionStatus& status, const std::string& name,
  253. OutType infoType, const std::string& variable,
  254. const std::string& propertyName)
  255. {
  256. if (!name.empty()) {
  257. status.SetError("given name for GLOBAL scope.");
  258. return false;
  259. }
  260. // Get the property.
  261. cmake* cm = status.GetMakefile().GetCMakeInstance();
  262. cmProp p = cm->GetState()->GetGlobalProperty(propertyName);
  263. return StoreResult(infoType, status.GetMakefile(), variable,
  264. p ? p->c_str() : nullptr);
  265. }
  266. bool HandleDirectoryMode(cmExecutionStatus& status, const std::string& name,
  267. OutType infoType, const std::string& variable,
  268. const std::string& propertyName)
  269. {
  270. // Default to the current directory.
  271. cmMakefile* mf = &status.GetMakefile();
  272. // Lookup the directory if given.
  273. if (!name.empty()) {
  274. // Construct the directory name. Interpret relative paths with
  275. // respect to the current directory.
  276. std::string dir = cmSystemTools::CollapseFullPath(
  277. name, status.GetMakefile().GetCurrentSourceDirectory());
  278. // Lookup the generator.
  279. mf = status.GetMakefile().GetGlobalGenerator()->FindMakefile(dir);
  280. if (!mf) {
  281. // Could not find the directory.
  282. status.SetError(
  283. "DIRECTORY scope provided but requested directory was not found. "
  284. "This could be because the directory argument was invalid or, "
  285. "it is valid but has not been processed yet.");
  286. return false;
  287. }
  288. }
  289. if (propertyName == "DEFINITIONS") {
  290. switch (mf->GetPolicyStatus(cmPolicies::CMP0059)) {
  291. case cmPolicies::WARN:
  292. mf->IssueMessage(MessageType::AUTHOR_WARNING,
  293. cmPolicies::GetPolicyWarning(cmPolicies::CMP0059));
  294. CM_FALLTHROUGH;
  295. case cmPolicies::OLD:
  296. return StoreResult(infoType, status.GetMakefile(), variable,
  297. mf->GetDefineFlagsCMP0059());
  298. case cmPolicies::NEW:
  299. case cmPolicies::REQUIRED_ALWAYS:
  300. case cmPolicies::REQUIRED_IF_USED:
  301. break;
  302. }
  303. }
  304. // Get the property.
  305. cmProp p = mf->GetProperty(propertyName);
  306. return StoreResult(infoType, status.GetMakefile(), variable,
  307. p ? p->c_str() : nullptr);
  308. }
  309. bool HandleTargetMode(cmExecutionStatus& status, const std::string& name,
  310. OutType infoType, const std::string& variable,
  311. const std::string& propertyName)
  312. {
  313. if (name.empty()) {
  314. status.SetError("not given name for TARGET scope.");
  315. return false;
  316. }
  317. if (cmTarget* target = status.GetMakefile().FindTargetToUse(name)) {
  318. if (propertyName == "ALIASED_TARGET" || propertyName == "ALIAS_GLOBAL") {
  319. if (status.GetMakefile().IsAlias(name)) {
  320. if (propertyName == "ALIASED_TARGET") {
  321. return StoreResult(infoType, status.GetMakefile(), variable,
  322. target->GetName().c_str());
  323. }
  324. if (propertyName == "ALIAS_GLOBAL") {
  325. return StoreResult(
  326. infoType, status.GetMakefile(), variable,
  327. status.GetMakefile().GetGlobalGenerator()->IsAlias(name)
  328. ? "TRUE"
  329. : "FALSE");
  330. }
  331. }
  332. return StoreResult(infoType, status.GetMakefile(), variable, nullptr);
  333. }
  334. cmProp prop_cstr = nullptr;
  335. cmListFileBacktrace bt = status.GetMakefile().GetBacktrace();
  336. cmMessenger* messenger = status.GetMakefile().GetMessenger();
  337. prop_cstr = target->GetComputedProperty(propertyName, messenger, bt);
  338. if (!prop_cstr) {
  339. prop_cstr = target->GetProperty(propertyName);
  340. }
  341. return StoreResult(infoType, status.GetMakefile(), variable,
  342. prop_cstr ? prop_cstr->c_str() : nullptr);
  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. cmProp 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,
  414. value ? value->c_str() : nullptr);
  415. return true;
  416. }
  417. bool HandleInstallMode(cmExecutionStatus& status, const std::string& name,
  418. OutType infoType, const std::string& variable,
  419. const std::string& propertyName)
  420. {
  421. if (name.empty()) {
  422. status.SetError("not given name for INSTALL scope.");
  423. return false;
  424. }
  425. // Get the installed file.
  426. cmake* cm = status.GetMakefile().GetCMakeInstance();
  427. if (cmInstalledFile* file =
  428. cm->GetOrCreateInstalledFile(&status.GetMakefile(), name)) {
  429. std::string value;
  430. bool isSet = file->GetProperty(propertyName, value);
  431. return StoreResult(infoType, status.GetMakefile(), variable,
  432. isSet ? value.c_str() : nullptr);
  433. }
  434. status.SetError(
  435. cmStrCat("given INSTALL name that could not be found or created: ", name));
  436. return false;
  437. }
  438. }