cmSetPropertyCommand.cxx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmSetPropertyCommand.h"
  14. #include "cmSetTargetPropertiesCommand.h"
  15. #include "cmSetTestsPropertiesCommand.h"
  16. #include "cmSetSourceFilesPropertiesCommand.h"
  17. //----------------------------------------------------------------------------
  18. cmSetPropertyCommand::cmSetPropertyCommand()
  19. {
  20. this->AppendMode = false;
  21. this->Remove = true;
  22. }
  23. //----------------------------------------------------------------------------
  24. bool cmSetPropertyCommand
  25. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  26. {
  27. if(args.size() < 2 )
  28. {
  29. this->SetError("called with incorrect number of arguments");
  30. return false;
  31. }
  32. // Get the scope on which to set the property.
  33. std::vector<std::string>::const_iterator arg = args.begin();
  34. cmProperty::ScopeType scope;
  35. if(*arg == "GLOBAL")
  36. {
  37. scope = cmProperty::GLOBAL;
  38. }
  39. else if(*arg == "DIRECTORY")
  40. {
  41. scope = cmProperty::DIRECTORY;
  42. }
  43. else if(*arg == "TARGET")
  44. {
  45. scope = cmProperty::TARGET;
  46. }
  47. else if(*arg == "SOURCE")
  48. {
  49. scope = cmProperty::SOURCE_FILE;
  50. }
  51. else if(*arg == "TEST")
  52. {
  53. scope = cmProperty::TEST;
  54. }
  55. else
  56. {
  57. cmOStringStream e;
  58. e << "given invalid scope " << *arg << ". "
  59. << "Valid scopes are GLOBAL, DIRECTORY, TARGET, SOURCE, TEST.";
  60. this->SetError(e.str().c_str());
  61. return false;
  62. }
  63. // Parse the rest of the arguments up to the values.
  64. enum Doing { DoingNone, DoingNames, DoingProperty, DoingValues };
  65. Doing doing = DoingNames;
  66. const char* sep = "";
  67. for(++arg; arg != args.end(); ++arg)
  68. {
  69. if(*arg == "PROPERTY")
  70. {
  71. doing = DoingProperty;
  72. }
  73. else if(*arg == "APPEND")
  74. {
  75. doing = DoingNone;
  76. this->AppendMode = true;
  77. }
  78. else if(doing == DoingNames)
  79. {
  80. this->Names.insert(*arg);
  81. }
  82. else if(doing == DoingProperty)
  83. {
  84. this->PropertyName = *arg;
  85. doing = DoingValues;
  86. }
  87. else if(doing == DoingValues)
  88. {
  89. this->PropertyValue += sep;
  90. sep = ";";
  91. this->PropertyValue += *arg;
  92. this->Remove = false;
  93. }
  94. else
  95. {
  96. cmOStringStream e;
  97. e << "given invalid argument \"" << *arg << "\".";
  98. this->SetError(e.str().c_str());
  99. return false;
  100. }
  101. }
  102. // Make sure a property name was found.
  103. if(this->PropertyName.empty())
  104. {
  105. this->SetError("not given a PROPERTY <name> argument.");
  106. return false;
  107. }
  108. // Dispatch property setting.
  109. switch(scope)
  110. {
  111. case cmProperty::GLOBAL: return this->HandleGlobalMode();
  112. case cmProperty::DIRECTORY: return this->HandleDirectoryMode();
  113. case cmProperty::TARGET: return this->HandleTargetMode();
  114. case cmProperty::SOURCE_FILE: return this->HandleSourceMode();
  115. case cmProperty::TEST: return this->HandleTestMode();
  116. case cmProperty::VARIABLE:
  117. case cmProperty::CACHED_VARIABLE:
  118. break; // should never happen
  119. }
  120. return true;
  121. }
  122. //----------------------------------------------------------------------------
  123. bool cmSetPropertyCommand::HandleGlobalMode()
  124. {
  125. if(!this->Names.empty())
  126. {
  127. this->SetError("given names for GLOBAL scope.");
  128. return false;
  129. }
  130. // Set or append the property.
  131. cmake* cm = this->Makefile->GetCMakeInstance();
  132. const char* name = this->PropertyName.c_str();
  133. const char *value = this->PropertyValue.c_str();
  134. if (this->Remove)
  135. {
  136. value = 0;
  137. }
  138. if(this->AppendMode)
  139. {
  140. cm->AppendProperty(name, value);
  141. }
  142. else
  143. {
  144. cm->SetProperty(name, value);
  145. }
  146. return true;
  147. }
  148. //----------------------------------------------------------------------------
  149. bool cmSetPropertyCommand::HandleDirectoryMode()
  150. {
  151. if(this->Names.size() > 1)
  152. {
  153. this->SetError("allows at most one name for DIRECTORY scope.");
  154. return false;
  155. }
  156. // Default to the current directory.
  157. cmMakefile* mf = this->Makefile;
  158. // Lookup the directory if given.
  159. if(!this->Names.empty())
  160. {
  161. // Construct the directory name. Interpret relative paths with
  162. // respect to the current directory.
  163. std::string dir = *this->Names.begin();
  164. if(!cmSystemTools::FileIsFullPath(dir.c_str()))
  165. {
  166. dir = this->Makefile->GetCurrentDirectory();
  167. dir += "/";
  168. dir += *this->Names.begin();
  169. }
  170. // The local generators are associated with collapsed paths.
  171. dir = cmSystemTools::CollapseFullPath(dir.c_str());
  172. // Lookup the generator.
  173. if(cmLocalGenerator* lg =
  174. (this->Makefile->GetLocalGenerator()
  175. ->GetGlobalGenerator()->FindLocalGenerator(dir.c_str())))
  176. {
  177. // Use the makefile for the directory found.
  178. mf = lg->GetMakefile();
  179. }
  180. else
  181. {
  182. // Could not find the directory.
  183. this->SetError
  184. ("DIRECTORY scope provided but requested directory was not found. "
  185. "This could be because the directory argument was invalid or, "
  186. "it is valid but has not been processed yet.");
  187. return false;
  188. }
  189. }
  190. // Set or append the property.
  191. const char* name = this->PropertyName.c_str();
  192. const char *value = this->PropertyValue.c_str();
  193. if (this->Remove)
  194. {
  195. value = 0;
  196. }
  197. if(this->AppendMode)
  198. {
  199. mf->AppendProperty(name, value);
  200. }
  201. else
  202. {
  203. mf->SetProperty(name, value);
  204. }
  205. return true;
  206. }
  207. //----------------------------------------------------------------------------
  208. bool cmSetPropertyCommand::HandleTargetMode()
  209. {
  210. for(std::set<cmStdString>::const_iterator ni = this->Names.begin();
  211. ni != this->Names.end(); ++ni)
  212. {
  213. if(cmTarget* target = this->Makefile->FindTargetToUse(ni->c_str()))
  214. {
  215. // Handle the current target.
  216. if(!this->HandleTarget(target))
  217. {
  218. return false;
  219. }
  220. }
  221. else
  222. {
  223. cmOStringStream e;
  224. e << "could not find TARGET " << *ni
  225. << ". Perhaps it has not yet been created.";
  226. this->SetError(e.str().c_str());
  227. return false;
  228. }
  229. }
  230. return true;
  231. }
  232. //----------------------------------------------------------------------------
  233. bool cmSetPropertyCommand::HandleTarget(cmTarget* target)
  234. {
  235. // Set or append the property.
  236. const char* name = this->PropertyName.c_str();
  237. const char *value = this->PropertyValue.c_str();
  238. if (this->Remove)
  239. {
  240. value = 0;
  241. }
  242. if(this->AppendMode)
  243. {
  244. target->AppendProperty(name, value);
  245. }
  246. else
  247. {
  248. target->SetProperty(name, value);
  249. }
  250. // Check the resulting value.
  251. target->CheckProperty(name, this->Makefile);
  252. return true;
  253. }
  254. //----------------------------------------------------------------------------
  255. bool cmSetPropertyCommand::HandleSourceMode()
  256. {
  257. for(std::set<cmStdString>::const_iterator ni = this->Names.begin();
  258. ni != this->Names.end(); ++ni)
  259. {
  260. // Get the source file.
  261. if(cmSourceFile* sf = this->Makefile->GetOrCreateSource(ni->c_str()))
  262. {
  263. if(!this->HandleSource(sf))
  264. {
  265. return false;
  266. }
  267. }
  268. else
  269. {
  270. cmOStringStream e;
  271. e << "given SOURCE name that could not be found or created: " << *ni;
  272. this->SetError(e.str().c_str());
  273. return false;
  274. }
  275. }
  276. return true;
  277. }
  278. //----------------------------------------------------------------------------
  279. bool cmSetPropertyCommand::HandleSource(cmSourceFile* sf)
  280. {
  281. // Set or append the property.
  282. const char* name = this->PropertyName.c_str();
  283. const char *value = this->PropertyValue.c_str();
  284. if (this->Remove)
  285. {
  286. value = 0;
  287. }
  288. if(this->AppendMode)
  289. {
  290. sf->AppendProperty(name, value);
  291. }
  292. else
  293. {
  294. sf->SetProperty(name, value);
  295. }
  296. return true;
  297. }
  298. //----------------------------------------------------------------------------
  299. bool cmSetPropertyCommand::HandleTestMode()
  300. {
  301. // Loop over all tests looking for matching names.
  302. std::vector<cmTest*> const& tests = *this->Makefile->GetTests();
  303. for(std::vector<cmTest*>::const_iterator ti = tests.begin();
  304. ti != tests.end(); ++ti)
  305. {
  306. cmTest* test = *ti;
  307. std::set<cmStdString>::iterator ni =
  308. this->Names.find(test->GetName());
  309. if(ni != this->Names.end())
  310. {
  311. if(this->HandleTest(test))
  312. {
  313. this->Names.erase(ni);
  314. }
  315. else
  316. {
  317. return false;
  318. }
  319. }
  320. }
  321. // Names that are still left were not found.
  322. if(!this->Names.empty())
  323. {
  324. cmOStringStream e;
  325. e << "given TEST names that do not exist:\n";
  326. for(std::set<cmStdString>::const_iterator ni = this->Names.begin();
  327. ni != this->Names.end(); ++ni)
  328. {
  329. e << " " << *ni << "\n";
  330. }
  331. this->SetError(e.str().c_str());
  332. return false;
  333. }
  334. return true;
  335. }
  336. //----------------------------------------------------------------------------
  337. bool cmSetPropertyCommand::HandleTest(cmTest* test)
  338. {
  339. // Set or append the property.
  340. const char* name = this->PropertyName.c_str();
  341. const char *value = this->PropertyValue.c_str();
  342. if (this->Remove)
  343. {
  344. value = 0;
  345. }
  346. if(this->AppendMode)
  347. {
  348. test->AppendProperty(name, value);
  349. }
  350. else
  351. {
  352. test->SetProperty(name, value);
  353. }
  354. return true;
  355. }