cmSetPropertyCommand.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 "cmSetPropertyCommand.h"
  4. #include <set>
  5. #include <sstream>
  6. #include "cmExecutionStatus.h"
  7. #include "cmGlobalGenerator.h"
  8. #include "cmInstalledFile.h"
  9. #include "cmMakefile.h"
  10. #include "cmProperty.h"
  11. #include "cmRange.h"
  12. #include "cmSourceFile.h"
  13. #include "cmState.h"
  14. #include "cmStringAlgorithms.h"
  15. #include "cmSystemTools.h"
  16. #include "cmTarget.h"
  17. #include "cmTest.h"
  18. #include "cmake.h"
  19. namespace {
  20. bool HandleGlobalMode(cmExecutionStatus& status,
  21. const std::set<std::string>& names,
  22. const std::string& propertyName,
  23. const std::string& propertyValue, bool appendAsString,
  24. bool appendMode, bool remove);
  25. bool HandleDirectoryMode(cmExecutionStatus& status,
  26. const std::set<std::string>& names,
  27. const std::string& propertyName,
  28. const std::string& propertyValue, bool appendAsString,
  29. bool appendMode, bool remove);
  30. bool HandleTargetMode(cmExecutionStatus& status,
  31. const std::set<std::string>& names,
  32. const std::string& propertyName,
  33. const std::string& propertyValue, bool appendAsString,
  34. bool appendMode, bool remove);
  35. bool HandleTarget(cmTarget* target, cmMakefile& makefile,
  36. const std::string& propertyName,
  37. const std::string& propertyValue, bool appendAsString,
  38. bool appendMode, bool remove);
  39. bool HandleSourceMode(cmExecutionStatus& status,
  40. const std::set<std::string>& names,
  41. const std::string& propertyName,
  42. const std::string& propertyValue, bool appendAsString,
  43. bool appendMode, bool remove);
  44. bool HandleSource(cmSourceFile* sf, const std::string& propertyName,
  45. const std::string& propertyValue, bool appendAsString,
  46. bool appendMode, bool remove);
  47. bool HandleTestMode(cmExecutionStatus& status, std::set<std::string>& names,
  48. const std::string& propertyName,
  49. const std::string& propertyValue, bool appendAsString,
  50. bool appendMode, bool remove);
  51. bool HandleTest(cmTest* test, const std::string& propertyName,
  52. const std::string& propertyValue, bool appendAsString,
  53. bool appendMode, bool remove);
  54. bool HandleCacheMode(cmExecutionStatus& status,
  55. const std::set<std::string>& names,
  56. const std::string& propertyName,
  57. const std::string& propertyValue, bool appendAsString,
  58. bool appendMode, bool remove);
  59. bool HandleCacheEntry(std::string const& cacheKey, const cmMakefile& makefile,
  60. const std::string& propertyName,
  61. const std::string& propertyValue, bool appendAsString,
  62. bool appendMode, bool remove);
  63. bool HandleInstallMode(cmExecutionStatus& status,
  64. const std::set<std::string>& names,
  65. const std::string& propertyName,
  66. const std::string& propertyValue, bool appendAsString,
  67. bool appendMode, bool remove);
  68. bool HandleInstall(cmInstalledFile* file, cmMakefile& makefile,
  69. const std::string& propertyName,
  70. const std::string& propertyValue, bool appendAsString,
  71. bool appendMode, bool remove);
  72. }
  73. bool cmSetPropertyCommand(std::vector<std::string> const& args,
  74. cmExecutionStatus& status)
  75. {
  76. if (args.size() < 2) {
  77. status.SetError("called with incorrect number of arguments");
  78. return false;
  79. }
  80. // Get the scope on which to set the property.
  81. std::string const& scopeName = args.front();
  82. cmProperty::ScopeType scope;
  83. if (scopeName == "GLOBAL") {
  84. scope = cmProperty::GLOBAL;
  85. } else if (scopeName == "DIRECTORY") {
  86. scope = cmProperty::DIRECTORY;
  87. } else if (scopeName == "TARGET") {
  88. scope = cmProperty::TARGET;
  89. } else if (scopeName == "SOURCE") {
  90. scope = cmProperty::SOURCE_FILE;
  91. } else if (scopeName == "TEST") {
  92. scope = cmProperty::TEST;
  93. } else if (scopeName == "CACHE") {
  94. scope = cmProperty::CACHE;
  95. } else if (scopeName == "INSTALL") {
  96. scope = cmProperty::INSTALL;
  97. } else {
  98. status.SetError(cmStrCat("given invalid scope ", scopeName,
  99. ". "
  100. "Valid scopes are GLOBAL, DIRECTORY, "
  101. "TARGET, SOURCE, TEST, CACHE, INSTALL."));
  102. return false;
  103. }
  104. bool appendAsString = false;
  105. bool appendMode = false;
  106. bool remove = true;
  107. std::set<std::string> names;
  108. std::string propertyName;
  109. std::string propertyValue;
  110. // Parse the rest of the arguments up to the values.
  111. enum Doing
  112. {
  113. DoingNone,
  114. DoingNames,
  115. DoingProperty,
  116. DoingValues
  117. };
  118. Doing doing = DoingNames;
  119. const char* sep = "";
  120. for (std::string const& arg : cmMakeRange(args).advance(1)) {
  121. if (arg == "PROPERTY") {
  122. doing = DoingProperty;
  123. } else if (arg == "APPEND") {
  124. doing = DoingNone;
  125. appendMode = true;
  126. remove = false;
  127. appendAsString = false;
  128. } else if (arg == "APPEND_STRING") {
  129. doing = DoingNone;
  130. appendMode = true;
  131. remove = false;
  132. appendAsString = true;
  133. } else if (doing == DoingNames) {
  134. names.insert(arg);
  135. } else if (doing == DoingProperty) {
  136. propertyName = arg;
  137. doing = DoingValues;
  138. } else if (doing == DoingValues) {
  139. propertyValue += sep;
  140. sep = ";";
  141. propertyValue += arg;
  142. remove = false;
  143. } else {
  144. status.SetError(cmStrCat("given invalid argument \"", arg, "\"."));
  145. return false;
  146. }
  147. }
  148. // Make sure a property name was found.
  149. if (propertyName.empty()) {
  150. status.SetError("not given a PROPERTY <name> argument.");
  151. return false;
  152. }
  153. // Dispatch property setting.
  154. switch (scope) {
  155. case cmProperty::GLOBAL:
  156. return HandleGlobalMode(status, names, propertyName, propertyValue,
  157. appendAsString, appendMode, remove);
  158. case cmProperty::DIRECTORY:
  159. return HandleDirectoryMode(status, names, propertyName, propertyValue,
  160. appendAsString, appendMode, remove);
  161. case cmProperty::TARGET:
  162. return HandleTargetMode(status, names, propertyName, propertyValue,
  163. appendAsString, appendMode, remove);
  164. case cmProperty::SOURCE_FILE:
  165. return HandleSourceMode(status, names, propertyName, propertyValue,
  166. appendAsString, appendMode, remove);
  167. case cmProperty::TEST:
  168. return HandleTestMode(status, names, propertyName, propertyValue,
  169. appendAsString, appendMode, remove);
  170. case cmProperty::CACHE:
  171. return HandleCacheMode(status, names, propertyName, propertyValue,
  172. appendAsString, appendMode, remove);
  173. case cmProperty::INSTALL:
  174. return HandleInstallMode(status, names, propertyName, propertyValue,
  175. appendAsString, appendMode, remove);
  176. case cmProperty::VARIABLE:
  177. case cmProperty::CACHED_VARIABLE:
  178. break; // should never happen
  179. }
  180. return true;
  181. }
  182. namespace {
  183. bool HandleGlobalMode(cmExecutionStatus& status,
  184. const std::set<std::string>& names,
  185. const std::string& propertyName,
  186. const std::string& propertyValue, bool appendAsString,
  187. bool appendMode, bool remove)
  188. {
  189. if (!names.empty()) {
  190. status.SetError("given names for GLOBAL scope.");
  191. return false;
  192. }
  193. // Set or append the property.
  194. cmake* cm = status.GetMakefile().GetCMakeInstance();
  195. if (appendMode) {
  196. cm->AppendProperty(propertyName, propertyValue, appendAsString);
  197. } else {
  198. if (remove) {
  199. cm->SetProperty(propertyName, nullptr);
  200. } else {
  201. cm->SetProperty(propertyName, propertyValue.c_str());
  202. }
  203. }
  204. return true;
  205. }
  206. bool HandleDirectoryMode(cmExecutionStatus& status,
  207. const std::set<std::string>& names,
  208. const std::string& propertyName,
  209. const std::string& propertyValue, bool appendAsString,
  210. bool appendMode, bool remove)
  211. {
  212. if (names.size() > 1) {
  213. status.SetError("allows at most one name for DIRECTORY scope.");
  214. return false;
  215. }
  216. // Default to the current directory.
  217. cmMakefile* mf = &status.GetMakefile();
  218. // Lookup the directory if given.
  219. if (!names.empty()) {
  220. // Construct the directory name. Interpret relative paths with
  221. // respect to the current directory.
  222. std::string dir = *names.begin();
  223. if (!cmSystemTools::FileIsFullPath(dir)) {
  224. dir = cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/',
  225. *names.begin());
  226. }
  227. // The local generators are associated with collapsed paths.
  228. dir = cmSystemTools::CollapseFullPath(dir);
  229. mf = status.GetMakefile().GetGlobalGenerator()->FindMakefile(dir);
  230. if (!mf) {
  231. // Could not find the directory.
  232. status.SetError(
  233. "DIRECTORY scope provided but requested directory was not found. "
  234. "This could be because the directory argument was invalid or, "
  235. "it is valid but has not been processed yet.");
  236. return false;
  237. }
  238. }
  239. // Set or append the property.
  240. if (appendMode) {
  241. mf->AppendProperty(propertyName, propertyValue, appendAsString);
  242. } else {
  243. if (remove) {
  244. mf->SetProperty(propertyName, nullptr);
  245. } else {
  246. mf->SetProperty(propertyName, propertyValue.c_str());
  247. }
  248. }
  249. return true;
  250. }
  251. bool HandleTargetMode(cmExecutionStatus& status,
  252. const std::set<std::string>& names,
  253. const std::string& propertyName,
  254. const std::string& propertyValue, bool appendAsString,
  255. bool appendMode, bool remove)
  256. {
  257. for (std::string const& name : names) {
  258. if (status.GetMakefile().IsAlias(name)) {
  259. status.SetError("can not be used on an ALIAS target.");
  260. return false;
  261. }
  262. if (cmTarget* target = status.GetMakefile().FindTargetToUse(name)) {
  263. // Handle the current target.
  264. if (!HandleTarget(target, status.GetMakefile(), propertyName,
  265. propertyValue, appendAsString, appendMode, remove)) {
  266. return false;
  267. }
  268. } else {
  269. status.SetError(cmStrCat("could not find TARGET ", name,
  270. ". Perhaps it has not yet been created."));
  271. return false;
  272. }
  273. }
  274. return true;
  275. }
  276. bool HandleTarget(cmTarget* target, cmMakefile& makefile,
  277. const std::string& propertyName,
  278. const std::string& propertyValue, bool appendAsString,
  279. bool appendMode, bool remove)
  280. {
  281. // Set or append the property.
  282. if (appendMode) {
  283. target->AppendProperty(propertyName, propertyValue, appendAsString);
  284. } else {
  285. if (remove) {
  286. target->SetProperty(propertyName, nullptr);
  287. } else {
  288. target->SetProperty(propertyName, propertyValue.c_str());
  289. }
  290. }
  291. // Check the resulting value.
  292. target->CheckProperty(propertyName, &makefile);
  293. return true;
  294. }
  295. bool HandleSourceMode(cmExecutionStatus& status,
  296. const std::set<std::string>& names,
  297. const std::string& propertyName,
  298. const std::string& propertyValue, bool appendAsString,
  299. bool appendMode, bool remove)
  300. {
  301. for (std::string const& name : names) {
  302. // Get the source file.
  303. if (cmSourceFile* sf = status.GetMakefile().GetOrCreateSource(name)) {
  304. if (!HandleSource(sf, propertyName, propertyValue, appendAsString,
  305. appendMode, remove)) {
  306. return false;
  307. }
  308. } else {
  309. status.SetError(cmStrCat(
  310. "given SOURCE name that could not be found or created: ", name));
  311. return false;
  312. }
  313. }
  314. return true;
  315. }
  316. bool HandleSource(cmSourceFile* sf, const std::string& propertyName,
  317. const std::string& propertyValue, bool appendAsString,
  318. bool appendMode, bool remove)
  319. {
  320. // Set or append the property.
  321. if (appendMode) {
  322. sf->AppendProperty(propertyName, propertyValue, appendAsString);
  323. } else {
  324. if (remove) {
  325. sf->SetProperty(propertyName, nullptr);
  326. } else {
  327. sf->SetProperty(propertyName, propertyValue.c_str());
  328. }
  329. }
  330. return true;
  331. }
  332. bool HandleTestMode(cmExecutionStatus& status, std::set<std::string>& names,
  333. const std::string& propertyName,
  334. const std::string& propertyValue, bool appendAsString,
  335. bool appendMode, bool remove)
  336. {
  337. // Look for tests with all names given.
  338. std::set<std::string>::iterator next;
  339. for (auto ni = names.begin(); ni != names.end(); ni = next) {
  340. next = ni;
  341. ++next;
  342. if (cmTest* test = status.GetMakefile().GetTest(*ni)) {
  343. if (HandleTest(test, propertyName, propertyValue, appendAsString,
  344. appendMode, remove)) {
  345. names.erase(ni);
  346. } else {
  347. return false;
  348. }
  349. }
  350. }
  351. // Names that are still left were not found.
  352. if (!names.empty()) {
  353. std::ostringstream e;
  354. e << "given TEST names that do not exist:\n";
  355. for (std::string const& name : names) {
  356. e << " " << name << "\n";
  357. }
  358. status.SetError(e.str());
  359. return false;
  360. }
  361. return true;
  362. }
  363. bool HandleTest(cmTest* test, const std::string& propertyName,
  364. const std::string& propertyValue, bool appendAsString,
  365. bool appendMode, bool remove)
  366. {
  367. // Set or append the property.
  368. if (appendMode) {
  369. test->AppendProperty(propertyName, propertyValue, appendAsString);
  370. } else {
  371. if (remove) {
  372. test->SetProperty(propertyName, nullptr);
  373. } else {
  374. test->SetProperty(propertyName, propertyValue.c_str());
  375. }
  376. }
  377. return true;
  378. }
  379. bool HandleCacheMode(cmExecutionStatus& status,
  380. const std::set<std::string>& names,
  381. const std::string& propertyName,
  382. const std::string& propertyValue, bool appendAsString,
  383. bool appendMode, bool remove)
  384. {
  385. if (propertyName == "ADVANCED") {
  386. if (!remove && !cmIsOn(propertyValue) && !cmIsOff(propertyValue)) {
  387. status.SetError(cmStrCat("given non-boolean value \"", propertyValue,
  388. R"(" for CACHE property "ADVANCED". )"));
  389. return false;
  390. }
  391. } else if (propertyName == "TYPE") {
  392. if (!cmState::IsCacheEntryType(propertyValue)) {
  393. status.SetError(
  394. cmStrCat("given invalid CACHE entry TYPE \"", propertyValue, "\""));
  395. return false;
  396. }
  397. } else if (propertyName != "HELPSTRING" && propertyName != "STRINGS" &&
  398. propertyName != "VALUE") {
  399. status.SetError(
  400. cmStrCat("given invalid CACHE property ", propertyName,
  401. ". "
  402. "Settable CACHE properties are: "
  403. "ADVANCED, HELPSTRING, STRINGS, TYPE, and VALUE."));
  404. return false;
  405. }
  406. for (std::string const& name : names) {
  407. // Get the source file.
  408. cmake* cm = status.GetMakefile().GetCMakeInstance();
  409. const char* existingValue = cm->GetState()->GetCacheEntryValue(name);
  410. if (existingValue) {
  411. if (!HandleCacheEntry(name, status.GetMakefile(), propertyName,
  412. propertyValue, appendAsString, appendMode,
  413. remove)) {
  414. return false;
  415. }
  416. } else {
  417. status.SetError(cmStrCat("could not find CACHE variable ", name,
  418. ". Perhaps it has not yet been created."));
  419. return false;
  420. }
  421. }
  422. return true;
  423. }
  424. bool HandleCacheEntry(std::string const& cacheKey, const cmMakefile& makefile,
  425. const std::string& propertyName,
  426. const std::string& propertyValue, bool appendAsString,
  427. bool appendMode, bool remove)
  428. {
  429. // Set or append the property.
  430. const char* value = propertyValue.c_str();
  431. cmState* state = makefile.GetState();
  432. if (remove) {
  433. state->RemoveCacheEntryProperty(cacheKey, propertyName);
  434. }
  435. if (appendMode) {
  436. state->AppendCacheEntryProperty(cacheKey, propertyName, value,
  437. appendAsString);
  438. } else {
  439. state->SetCacheEntryProperty(cacheKey, propertyName, value);
  440. }
  441. return true;
  442. }
  443. bool HandleInstallMode(cmExecutionStatus& status,
  444. const std::set<std::string>& names,
  445. const std::string& propertyName,
  446. const std::string& propertyValue, bool appendAsString,
  447. bool appendMode, bool remove)
  448. {
  449. cmake* cm = status.GetMakefile().GetCMakeInstance();
  450. for (std::string const& name : names) {
  451. if (cmInstalledFile* file =
  452. cm->GetOrCreateInstalledFile(&status.GetMakefile(), name)) {
  453. if (!HandleInstall(file, status.GetMakefile(), propertyName,
  454. propertyValue, appendAsString, appendMode, remove)) {
  455. return false;
  456. }
  457. } else {
  458. status.SetError(cmStrCat(
  459. "given INSTALL name that could not be found or created: ", name));
  460. return false;
  461. }
  462. }
  463. return true;
  464. }
  465. bool HandleInstall(cmInstalledFile* file, cmMakefile& makefile,
  466. const std::string& propertyName,
  467. const std::string& propertyValue, bool appendAsString,
  468. bool appendMode, bool remove)
  469. {
  470. // Set or append the property.
  471. const char* value = propertyValue.c_str();
  472. if (remove) {
  473. file->RemoveProperty(propertyName);
  474. } else if (appendMode) {
  475. file->AppendProperty(&makefile, propertyName, value, appendAsString);
  476. } else {
  477. file->SetProperty(&makefile, propertyName, value);
  478. }
  479. return true;
  480. }
  481. }