cmSetPropertyCommand.cxx 14 KB

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