cmListCommand.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 "cmListCommand.h"
  14. #include <cmsys/RegularExpression.hxx>
  15. #include <cmsys/SystemTools.hxx>
  16. #include <stdlib.h> // required for atoi
  17. #include <ctype.h>
  18. //----------------------------------------------------------------------------
  19. bool cmListCommand
  20. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  21. {
  22. if(args.size() < 1)
  23. {
  24. this->SetError("must be called with at least one argument.");
  25. return false;
  26. }
  27. const std::string &subCommand = args[0];
  28. if(subCommand == "LENGTH")
  29. {
  30. return this->HandleLengthCommand(args);
  31. }
  32. if(subCommand == "GET")
  33. {
  34. return this->HandleGetCommand(args);
  35. }
  36. if(subCommand == "APPEND")
  37. {
  38. return this->HandleAppendCommand(args);
  39. }
  40. if(subCommand == "FIND")
  41. {
  42. return this->HandleFindCommand(args);
  43. }
  44. if(subCommand == "INSERT")
  45. {
  46. return this->HandleInsertCommand(args);
  47. }
  48. if(subCommand == "REMOVE_AT")
  49. {
  50. return this->HandleRemoveAtCommand(args);
  51. }
  52. if(subCommand == "REMOVE_ITEM")
  53. {
  54. return this->HandleRemoveItemCommand(args);
  55. }
  56. if(subCommand == "REMOVE_DUPLICATES")
  57. {
  58. return this->HandleRemoveDuplicatesCommand(args);
  59. }
  60. if(subCommand == "SORT")
  61. {
  62. return this->HandleSortCommand(args);
  63. }
  64. if(subCommand == "REVERSE")
  65. {
  66. return this->HandleReverseCommand(args);
  67. }
  68. std::string e = "does not recognize sub-command "+subCommand;
  69. this->SetError(e.c_str());
  70. return false;
  71. }
  72. //----------------------------------------------------------------------------
  73. bool cmListCommand::GetListString(std::string& listString, const char* var)
  74. {
  75. if ( !var )
  76. {
  77. return false;
  78. }
  79. // get the old value
  80. const char* cacheValue
  81. = this->Makefile->GetDefinition(var);
  82. if(!cacheValue)
  83. {
  84. return false;
  85. }
  86. listString = cacheValue;
  87. return true;
  88. }
  89. //----------------------------------------------------------------------------
  90. bool cmListCommand::GetList(std::vector<std::string>& list, const char* var)
  91. {
  92. std::string listString;
  93. if ( !this->GetListString(listString, var) )
  94. {
  95. return false;
  96. }
  97. // expand the variable
  98. cmSystemTools::ExpandListArgument(listString, list);
  99. return true;
  100. }
  101. //----------------------------------------------------------------------------
  102. bool cmListCommand::HandleLengthCommand(std::vector<std::string> const& args)
  103. {
  104. if(args.size() != 3)
  105. {
  106. this->SetError("sub-command LENGTH requires two arguments.");
  107. return false;
  108. }
  109. const std::string& listName = args[1];
  110. const std::string& variableName = args[args.size() - 1];
  111. std::vector<std::string> varArgsExpanded;
  112. // do not check the return value here
  113. // if the list var is not found varArgsExpanded will have size 0
  114. // and we will return 0
  115. this->GetList(varArgsExpanded, listName.c_str());
  116. size_t length = varArgsExpanded.size();
  117. char buffer[1024];
  118. sprintf(buffer, "%d", static_cast<int>(length));
  119. this->Makefile->AddDefinition(variableName.c_str(), buffer);
  120. return true;
  121. }
  122. //----------------------------------------------------------------------------
  123. bool cmListCommand::HandleGetCommand(std::vector<std::string> const& args)
  124. {
  125. if(args.size() < 4)
  126. {
  127. this->SetError("sub-command GET requires at least three arguments.");
  128. return false;
  129. }
  130. const std::string& listName = args[1];
  131. const std::string& variableName = args[args.size() - 1];
  132. // expand the variable
  133. std::vector<std::string> varArgsExpanded;
  134. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  135. {
  136. this->Makefile->AddDefinition(variableName.c_str(), "NOTFOUND");
  137. return true;
  138. }
  139. std::string value;
  140. size_t cc;
  141. for ( cc = 2; cc < args.size()-1; cc ++ )
  142. {
  143. int item = atoi(args[cc].c_str());
  144. if (value.size())
  145. {
  146. value += ";";
  147. }
  148. size_t nitem = varArgsExpanded.size();
  149. if ( item < 0 )
  150. {
  151. item = (int)nitem + item;
  152. }
  153. if ( item < 0 || nitem <= (size_t)item )
  154. {
  155. cmOStringStream str;
  156. str << "index: " << item << " out of range (-"
  157. << varArgsExpanded.size() << ", "
  158. << varArgsExpanded.size()-1 << ")";
  159. this->SetError(str.str().c_str());
  160. return false;
  161. }
  162. value += varArgsExpanded[item];
  163. }
  164. this->Makefile->AddDefinition(variableName.c_str(), value.c_str());
  165. return true;
  166. }
  167. //----------------------------------------------------------------------------
  168. bool cmListCommand::HandleAppendCommand(std::vector<std::string> const& args)
  169. {
  170. if(args.size() < 2)
  171. {
  172. this->SetError("sub-command APPEND requires at least one argument.");
  173. return false;
  174. }
  175. // Skip if nothing to append.
  176. if(args.size() < 3)
  177. {
  178. return true;
  179. }
  180. const std::string& listName = args[1];
  181. // expand the variable
  182. std::string listString;
  183. this->GetListString(listString, listName.c_str());
  184. size_t cc;
  185. for ( cc = 2; cc < args.size(); ++ cc )
  186. {
  187. if ( listString.size() )
  188. {
  189. listString += ";";
  190. }
  191. listString += args[cc];
  192. }
  193. this->Makefile->AddDefinition(listName.c_str(), listString.c_str());
  194. return true;
  195. }
  196. //----------------------------------------------------------------------------
  197. bool cmListCommand::HandleFindCommand(std::vector<std::string> const& args)
  198. {
  199. if(args.size() != 4)
  200. {
  201. this->SetError("sub-command FIND requires three arguments.");
  202. return false;
  203. }
  204. const std::string& listName = args[1];
  205. const std::string& variableName = args[args.size() - 1];
  206. // expand the variable
  207. std::vector<std::string> varArgsExpanded;
  208. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  209. {
  210. this->Makefile->AddDefinition(variableName.c_str(), "-1");
  211. return true;
  212. }
  213. std::vector<std::string>::iterator it;
  214. unsigned int index = 0;
  215. for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it )
  216. {
  217. if ( *it == args[2] )
  218. {
  219. char indexString[32];
  220. sprintf(indexString, "%d", index);
  221. this->Makefile->AddDefinition(variableName.c_str(), indexString);
  222. return true;
  223. }
  224. index++;
  225. }
  226. this->Makefile->AddDefinition(variableName.c_str(), "-1");
  227. return true;
  228. }
  229. //----------------------------------------------------------------------------
  230. bool cmListCommand::HandleInsertCommand(std::vector<std::string> const& args)
  231. {
  232. if(args.size() < 4)
  233. {
  234. this->SetError("sub-command INSERT requires at least three arguments.");
  235. return false;
  236. }
  237. const std::string& listName = args[1];
  238. // expand the variable
  239. int item = atoi(args[2].c_str());
  240. std::vector<std::string> varArgsExpanded;
  241. if ( !this->GetList(varArgsExpanded, listName.c_str()) && item != 0)
  242. {
  243. cmOStringStream str;
  244. str << "index: " << item << " out of range (0, 0)";
  245. this->SetError(str.str().c_str());
  246. return false;
  247. }
  248. if ( varArgsExpanded.size() != 0 )
  249. {
  250. size_t nitem = varArgsExpanded.size();
  251. if ( item < 0 )
  252. {
  253. item = (int)nitem + item;
  254. }
  255. if ( item < 0 || nitem <= (size_t)item )
  256. {
  257. cmOStringStream str;
  258. str << "index: " << item << " out of range (-"
  259. << varArgsExpanded.size() << ", "
  260. << (varArgsExpanded.size() == 0?0:(varArgsExpanded.size()-1)) << ")";
  261. this->SetError(str.str().c_str());
  262. return false;
  263. }
  264. }
  265. size_t cc;
  266. size_t cnt = 0;
  267. for ( cc = 3; cc < args.size(); ++ cc )
  268. {
  269. varArgsExpanded.insert(varArgsExpanded.begin()+item+cnt, args[cc]);
  270. cnt ++;
  271. }
  272. std::string value;
  273. for ( cc = 0; cc < varArgsExpanded.size(); cc ++ )
  274. {
  275. if (value.size())
  276. {
  277. value += ";";
  278. }
  279. value += varArgsExpanded[cc];
  280. }
  281. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  282. return true;
  283. }
  284. //----------------------------------------------------------------------------
  285. bool cmListCommand
  286. ::HandleRemoveItemCommand(std::vector<std::string> const& args)
  287. {
  288. if(args.size() < 3)
  289. {
  290. this->SetError("sub-command REMOVE_ITEM requires two or more arguments.");
  291. return false;
  292. }
  293. const std::string& listName = args[1];
  294. // expand the variable
  295. std::vector<std::string> varArgsExpanded;
  296. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  297. {
  298. this->SetError("sub-command REMOVE_ITEM requires list to be present.");
  299. return false;
  300. }
  301. size_t cc;
  302. for ( cc = 2; cc < args.size(); ++ cc )
  303. {
  304. size_t kk = 0;
  305. while ( kk < varArgsExpanded.size() )
  306. {
  307. if ( varArgsExpanded[kk] == args[cc] )
  308. {
  309. varArgsExpanded.erase(varArgsExpanded.begin()+kk);
  310. }
  311. else
  312. {
  313. kk ++;
  314. }
  315. }
  316. }
  317. std::string value;
  318. for ( cc = 0; cc < varArgsExpanded.size(); cc ++ )
  319. {
  320. if (value.size())
  321. {
  322. value += ";";
  323. }
  324. value += varArgsExpanded[cc];
  325. }
  326. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  327. return true;
  328. }
  329. //----------------------------------------------------------------------------
  330. bool cmListCommand
  331. ::HandleReverseCommand(std::vector<std::string> const& args)
  332. {
  333. if(args.size() < 2)
  334. {
  335. this->SetError("sub-command REVERSE requires a list as an argument.");
  336. return false;
  337. }
  338. const std::string& listName = args[1];
  339. // expand the variable
  340. std::vector<std::string> varArgsExpanded;
  341. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  342. {
  343. this->SetError("sub-command REVERSE requires list to be present.");
  344. return false;
  345. }
  346. std::string value;
  347. std::vector<std::string>::reverse_iterator it;
  348. for ( it = varArgsExpanded.rbegin(); it != varArgsExpanded.rend(); ++ it )
  349. {
  350. if (value.size())
  351. {
  352. value += ";";
  353. }
  354. value += it->c_str();
  355. }
  356. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  357. return true;
  358. }
  359. //----------------------------------------------------------------------------
  360. bool cmListCommand
  361. ::HandleRemoveDuplicatesCommand(std::vector<std::string> const& args)
  362. {
  363. if(args.size() < 2)
  364. {
  365. this->SetError(
  366. "sub-command REMOVE_DUPLICATES requires a list as an argument.");
  367. return false;
  368. }
  369. const std::string& listName = args[1];
  370. // expand the variable
  371. std::vector<std::string> varArgsExpanded;
  372. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  373. {
  374. this->SetError(
  375. "sub-command REMOVE_DUPLICATES requires list to be present.");
  376. return false;
  377. }
  378. std::string value;
  379. #if 0
  380. // Fast version, but does not keep the ordering
  381. std::set<std::string> unique(varArgsExpanded.begin(), varArgsExpanded.end());
  382. std::set<std::string>::iterator it;
  383. for ( it = unique.begin(); it != unique.end(); ++ it )
  384. {
  385. if (value.size())
  386. {
  387. value += ";";
  388. }
  389. value += it->c_str();
  390. }
  391. #else
  392. // Slower version, keep the ordering
  393. std::set<std::string> unique;
  394. std::vector<std::string>::iterator it;
  395. for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it )
  396. {
  397. if (unique.find(*it) != unique.end())
  398. {
  399. continue;
  400. }
  401. unique.insert(*it);
  402. if (value.size())
  403. {
  404. value += ";";
  405. }
  406. value += it->c_str();
  407. }
  408. #endif
  409. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  410. return true;
  411. }
  412. //----------------------------------------------------------------------------
  413. bool cmListCommand
  414. ::HandleSortCommand(std::vector<std::string> const& args)
  415. {
  416. if(args.size() < 2)
  417. {
  418. this->SetError("sub-command SORT requires a list as an argument.");
  419. return false;
  420. }
  421. const std::string& listName = args[1];
  422. // expand the variable
  423. std::vector<std::string> varArgsExpanded;
  424. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  425. {
  426. this->SetError("sub-command SORT requires list to be present.");
  427. return false;
  428. }
  429. std::sort(varArgsExpanded.begin(), varArgsExpanded.end());
  430. std::string value;
  431. std::vector<std::string>::iterator it;
  432. for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it )
  433. {
  434. if (value.size())
  435. {
  436. value += ";";
  437. }
  438. value += it->c_str();
  439. }
  440. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  441. return true;
  442. }
  443. //----------------------------------------------------------------------------
  444. bool cmListCommand::HandleRemoveAtCommand(
  445. std::vector<std::string> const& args)
  446. {
  447. if(args.size() < 3)
  448. {
  449. this->SetError("sub-command REMOVE_AT requires at least "
  450. "two arguments.");
  451. return false;
  452. }
  453. const std::string& listName = args[1];
  454. // expand the variable
  455. std::vector<std::string> varArgsExpanded;
  456. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  457. {
  458. this->SetError("sub-command REMOVE_AT requires list to be present.");
  459. return false;
  460. }
  461. size_t cc;
  462. std::vector<size_t> removed;
  463. for ( cc = 2; cc < args.size(); ++ cc )
  464. {
  465. int item = atoi(args[cc].c_str());
  466. size_t nitem = varArgsExpanded.size();
  467. if ( item < 0 )
  468. {
  469. item = (int)nitem + item;
  470. }
  471. if ( item < 0 || nitem <= (size_t)item )
  472. {
  473. cmOStringStream str;
  474. str << "index: " << item << " out of range (-"
  475. << varArgsExpanded.size() << ", "
  476. << varArgsExpanded.size()-1 << ")";
  477. this->SetError(str.str().c_str());
  478. return false;
  479. }
  480. removed.push_back(static_cast<size_t>(item));
  481. }
  482. std::string value;
  483. for ( cc = 0; cc < varArgsExpanded.size(); ++ cc )
  484. {
  485. size_t kk;
  486. bool found = false;
  487. for ( kk = 0; kk < removed.size(); ++ kk )
  488. {
  489. if ( cc == removed[kk] )
  490. {
  491. found = true;
  492. }
  493. }
  494. if ( !found )
  495. {
  496. if (value.size())
  497. {
  498. value += ";";
  499. }
  500. value += varArgsExpanded[cc];
  501. }
  502. }
  503. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  504. return true;
  505. }