cmSetPropertyCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. #include "cmCacheManager.h"
  15. //----------------------------------------------------------------------------
  16. cmSetPropertyCommand::cmSetPropertyCommand()
  17. {
  18. this->AppendMode = 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
  58. {
  59. cmOStringStream e;
  60. e << "given invalid scope " << *arg << ". "
  61. << "Valid scopes are GLOBAL, DIRECTORY, TARGET, SOURCE, TEST, CACHE.";
  62. this->SetError(e.str().c_str());
  63. return false;
  64. }
  65. // Parse the rest of the arguments up to the values.
  66. enum Doing { DoingNone, DoingNames, DoingProperty, DoingValues };
  67. Doing doing = DoingNames;
  68. const char* sep = "";
  69. for(++arg; arg != args.end(); ++arg)
  70. {
  71. if(*arg == "PROPERTY")
  72. {
  73. doing = DoingProperty;
  74. }
  75. else if(*arg == "APPEND")
  76. {
  77. doing = DoingNone;
  78. this->AppendMode = true;
  79. }
  80. else if(doing == DoingNames)
  81. {
  82. this->Names.insert(*arg);
  83. }
  84. else if(doing == DoingProperty)
  85. {
  86. this->PropertyName = *arg;
  87. doing = DoingValues;
  88. }
  89. else if(doing == DoingValues)
  90. {
  91. this->PropertyValue += sep;
  92. sep = ";";
  93. this->PropertyValue += *arg;
  94. this->Remove = false;
  95. }
  96. else
  97. {
  98. cmOStringStream e;
  99. e << "given invalid argument \"" << *arg << "\".";
  100. this->SetError(e.str().c_str());
  101. return false;
  102. }
  103. }
  104. // Make sure a property name was found.
  105. if(this->PropertyName.empty())
  106. {
  107. this->SetError("not given a PROPERTY <name> argument.");
  108. return false;
  109. }
  110. // Dispatch property setting.
  111. switch(scope)
  112. {
  113. case cmProperty::GLOBAL: return this->HandleGlobalMode();
  114. case cmProperty::DIRECTORY: return this->HandleDirectoryMode();
  115. case cmProperty::TARGET: return this->HandleTargetMode();
  116. case cmProperty::SOURCE_FILE: return this->HandleSourceMode();
  117. case cmProperty::TEST: return this->HandleTestMode();
  118. case cmProperty::CACHE: return this->HandleCacheMode();
  119. case cmProperty::VARIABLE:
  120. case cmProperty::CACHED_VARIABLE:
  121. break; // should never happen
  122. }
  123. return true;
  124. }
  125. //----------------------------------------------------------------------------
  126. bool cmSetPropertyCommand::HandleGlobalMode()
  127. {
  128. if(!this->Names.empty())
  129. {
  130. this->SetError("given names for GLOBAL scope.");
  131. return false;
  132. }
  133. // Set or append the property.
  134. cmake* cm = this->Makefile->GetCMakeInstance();
  135. const char* name = this->PropertyName.c_str();
  136. const char *value = this->PropertyValue.c_str();
  137. if (this->Remove)
  138. {
  139. value = 0;
  140. }
  141. if(this->AppendMode)
  142. {
  143. cm->AppendProperty(name, value);
  144. }
  145. else
  146. {
  147. cm->SetProperty(name, value);
  148. }
  149. return true;
  150. }
  151. //----------------------------------------------------------------------------
  152. bool cmSetPropertyCommand::HandleDirectoryMode()
  153. {
  154. if(this->Names.size() > 1)
  155. {
  156. this->SetError("allows at most one name for DIRECTORY scope.");
  157. return false;
  158. }
  159. // Default to the current directory.
  160. cmMakefile* mf = this->Makefile;
  161. // Lookup the directory if given.
  162. if(!this->Names.empty())
  163. {
  164. // Construct the directory name. Interpret relative paths with
  165. // respect to the current directory.
  166. std::string dir = *this->Names.begin();
  167. if(!cmSystemTools::FileIsFullPath(dir.c_str()))
  168. {
  169. dir = this->Makefile->GetCurrentDirectory();
  170. dir += "/";
  171. dir += *this->Names.begin();
  172. }
  173. // The local generators are associated with collapsed paths.
  174. dir = cmSystemTools::CollapseFullPath(dir.c_str());
  175. // Lookup the generator.
  176. if(cmLocalGenerator* lg =
  177. (this->Makefile->GetLocalGenerator()
  178. ->GetGlobalGenerator()->FindLocalGenerator(dir.c_str())))
  179. {
  180. // Use the makefile for the directory found.
  181. mf = lg->GetMakefile();
  182. }
  183. else
  184. {
  185. // Could not find the directory.
  186. this->SetError
  187. ("DIRECTORY scope provided but requested directory was not found. "
  188. "This could be because the directory argument was invalid or, "
  189. "it is valid but has not been processed yet.");
  190. return false;
  191. }
  192. }
  193. // Set or append the property.
  194. const char* name = this->PropertyName.c_str();
  195. const char *value = this->PropertyValue.c_str();
  196. if (this->Remove)
  197. {
  198. value = 0;
  199. }
  200. if(this->AppendMode)
  201. {
  202. mf->AppendProperty(name, value);
  203. }
  204. else
  205. {
  206. mf->SetProperty(name, value);
  207. }
  208. return true;
  209. }
  210. //----------------------------------------------------------------------------
  211. bool cmSetPropertyCommand::HandleTargetMode()
  212. {
  213. for(std::set<cmStdString>::const_iterator ni = this->Names.begin();
  214. ni != this->Names.end(); ++ni)
  215. {
  216. if(cmTarget* target = this->Makefile->FindTargetToUse(ni->c_str()))
  217. {
  218. // Handle the current target.
  219. if(!this->HandleTarget(target))
  220. {
  221. return false;
  222. }
  223. }
  224. else
  225. {
  226. cmOStringStream e;
  227. e << "could not find TARGET " << *ni
  228. << ". Perhaps it has not yet been created.";
  229. this->SetError(e.str().c_str());
  230. return false;
  231. }
  232. }
  233. return true;
  234. }
  235. //----------------------------------------------------------------------------
  236. bool cmSetPropertyCommand::HandleTarget(cmTarget* target)
  237. {
  238. // Set or append the property.
  239. const char* name = this->PropertyName.c_str();
  240. const char *value = this->PropertyValue.c_str();
  241. if (this->Remove)
  242. {
  243. value = 0;
  244. }
  245. if(this->AppendMode)
  246. {
  247. target->AppendProperty(name, value);
  248. }
  249. else
  250. {
  251. target->SetProperty(name, value);
  252. }
  253. // Check the resulting value.
  254. target->CheckProperty(name, this->Makefile);
  255. return true;
  256. }
  257. //----------------------------------------------------------------------------
  258. bool cmSetPropertyCommand::HandleSourceMode()
  259. {
  260. for(std::set<cmStdString>::const_iterator ni = this->Names.begin();
  261. ni != this->Names.end(); ++ni)
  262. {
  263. // Get the source file.
  264. if(cmSourceFile* sf = this->Makefile->GetOrCreateSource(ni->c_str()))
  265. {
  266. if(!this->HandleSource(sf))
  267. {
  268. return false;
  269. }
  270. }
  271. else
  272. {
  273. cmOStringStream e;
  274. e << "given SOURCE name that could not be found or created: " << *ni;
  275. this->SetError(e.str().c_str());
  276. return false;
  277. }
  278. }
  279. return true;
  280. }
  281. //----------------------------------------------------------------------------
  282. bool cmSetPropertyCommand::HandleSource(cmSourceFile* sf)
  283. {
  284. // Set or append the property.
  285. const char* name = this->PropertyName.c_str();
  286. const char *value = this->PropertyValue.c_str();
  287. if (this->Remove)
  288. {
  289. value = 0;
  290. }
  291. if(this->AppendMode)
  292. {
  293. sf->AppendProperty(name, value);
  294. }
  295. else
  296. {
  297. sf->SetProperty(name, value);
  298. }
  299. return true;
  300. }
  301. //----------------------------------------------------------------------------
  302. bool cmSetPropertyCommand::HandleTestMode()
  303. {
  304. // Look for tests with all names given.
  305. std::set<cmStdString>::iterator next;
  306. for(std::set<cmStdString>::iterator ni = this->Names.begin();
  307. ni != this->Names.end(); ni = next)
  308. {
  309. next = ni;
  310. ++next;
  311. if(cmTest* test = this->Makefile->GetTest(ni->c_str()))
  312. {
  313. if(this->HandleTest(test))
  314. {
  315. this->Names.erase(ni);
  316. }
  317. else
  318. {
  319. return false;
  320. }
  321. }
  322. }
  323. // Names that are still left were not found.
  324. if(!this->Names.empty())
  325. {
  326. cmOStringStream e;
  327. e << "given TEST names that do not exist:\n";
  328. for(std::set<cmStdString>::const_iterator ni = this->Names.begin();
  329. ni != this->Names.end(); ++ni)
  330. {
  331. e << " " << *ni << "\n";
  332. }
  333. this->SetError(e.str().c_str());
  334. return false;
  335. }
  336. return true;
  337. }
  338. //----------------------------------------------------------------------------
  339. bool cmSetPropertyCommand::HandleTest(cmTest* test)
  340. {
  341. // Set or append the property.
  342. const char* name = this->PropertyName.c_str();
  343. const char *value = this->PropertyValue.c_str();
  344. if (this->Remove)
  345. {
  346. value = 0;
  347. }
  348. if(this->AppendMode)
  349. {
  350. test->AppendProperty(name, value);
  351. }
  352. else
  353. {
  354. test->SetProperty(name, value);
  355. }
  356. return true;
  357. }
  358. //----------------------------------------------------------------------------
  359. bool cmSetPropertyCommand::HandleCacheMode()
  360. {
  361. if(this->PropertyName == "ADVANCED")
  362. {
  363. if(!this->Remove &&
  364. !cmSystemTools::IsOn(this->PropertyValue.c_str()) &&
  365. !cmSystemTools::IsOff(this->PropertyValue.c_str()))
  366. {
  367. cmOStringStream e;
  368. e << "given non-boolean value \"" << this->PropertyValue
  369. << "\" for CACHE property \"ADVANCED\". ";
  370. this->SetError(e.str().c_str());
  371. return false;
  372. }
  373. }
  374. else if(this->PropertyName == "TYPE")
  375. {
  376. if(!cmCacheManager::IsType(this->PropertyValue.c_str()))
  377. {
  378. cmOStringStream e;
  379. e << "given invalid CACHE entry TYPE \"" << this->PropertyValue << "\"";
  380. this->SetError(e.str().c_str());
  381. return false;
  382. }
  383. }
  384. else if(this->PropertyName != "HELPSTRING" &&
  385. this->PropertyName != "STRINGS" &&
  386. this->PropertyName != "VALUE")
  387. {
  388. cmOStringStream e;
  389. e << "given invalid CACHE property " << this->PropertyName << ". "
  390. << "Settable CACHE properties are: "
  391. << "ADVANCED, HELPSTRING, STRINGS, TYPE, and VALUE.";
  392. this->SetError(e.str().c_str());
  393. return false;
  394. }
  395. for(std::set<cmStdString>::const_iterator ni = this->Names.begin();
  396. ni != this->Names.end(); ++ni)
  397. {
  398. // Get the source file.
  399. cmMakefile* mf = this->GetMakefile();
  400. cmake* cm = mf->GetCMakeInstance();
  401. cmCacheManager::CacheIterator it =
  402. cm->GetCacheManager()->GetCacheIterator(ni->c_str());
  403. if(!it.IsAtEnd())
  404. {
  405. if(!this->HandleCacheEntry(it))
  406. {
  407. return false;
  408. }
  409. }
  410. else
  411. {
  412. cmOStringStream e;
  413. e << "could not find CACHE variable " << *ni
  414. << ". Perhaps it has not yet been created.";
  415. this->SetError(e.str().c_str());
  416. return false;
  417. }
  418. }
  419. return true;
  420. }
  421. //----------------------------------------------------------------------------
  422. bool cmSetPropertyCommand::HandleCacheEntry(cmCacheManager::CacheIterator& it)
  423. {
  424. // Set or append the property.
  425. const char* name = this->PropertyName.c_str();
  426. const char* value = this->PropertyValue.c_str();
  427. if (this->Remove)
  428. {
  429. value = 0;
  430. }
  431. if(this->AppendMode)
  432. {
  433. it.AppendProperty(name, value);
  434. }
  435. else
  436. {
  437. it.SetProperty(name, value);
  438. }
  439. return true;
  440. }