cmFileCommand.cxx 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  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 "cmFileCommand.h"
  14. #include "cmake.h"
  15. #include "cmHexFileConverter.h"
  16. #include "cmFileTimeComparison.h"
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <cmsys/Directory.hxx>
  20. #include <cmsys/Glob.hxx>
  21. #include <cmsys/RegularExpression.hxx>
  22. // Table of permissions flags.
  23. #if defined(_WIN32) && !defined(__CYGWIN__)
  24. static mode_t mode_owner_read = S_IREAD;
  25. static mode_t mode_owner_write = S_IWRITE;
  26. static mode_t mode_owner_execute = S_IEXEC;
  27. static mode_t mode_group_read = 0;
  28. static mode_t mode_group_write = 0;
  29. static mode_t mode_group_execute = 0;
  30. static mode_t mode_world_read = 0;
  31. static mode_t mode_world_write = 0;
  32. static mode_t mode_world_execute = 0;
  33. static mode_t mode_setuid = 0;
  34. static mode_t mode_setgid = 0;
  35. #else
  36. static mode_t mode_owner_read = S_IRUSR;
  37. static mode_t mode_owner_write = S_IWUSR;
  38. static mode_t mode_owner_execute = S_IXUSR;
  39. static mode_t mode_group_read = S_IRGRP;
  40. static mode_t mode_group_write = S_IWGRP;
  41. static mode_t mode_group_execute = S_IXGRP;
  42. static mode_t mode_world_read = S_IROTH;
  43. static mode_t mode_world_write = S_IWOTH;
  44. static mode_t mode_world_execute = S_IXOTH;
  45. static mode_t mode_setuid = S_ISUID;
  46. static mode_t mode_setgid = S_ISGID;
  47. #endif
  48. // cmLibraryCommand
  49. bool cmFileCommand::InitialPass(std::vector<std::string> const& args)
  50. {
  51. if(args.size() < 2 )
  52. {
  53. this->SetError("must be called with at least two arguments.");
  54. return false;
  55. }
  56. std::string subCommand = args[0];
  57. if ( subCommand == "WRITE" )
  58. {
  59. return this->HandleWriteCommand(args, false);
  60. }
  61. else if ( subCommand == "APPEND" )
  62. {
  63. return this->HandleWriteCommand(args, true);
  64. }
  65. else if ( subCommand == "READ" )
  66. {
  67. return this->HandleReadCommand(args);
  68. }
  69. else if ( subCommand == "STRINGS" )
  70. {
  71. return this->HandleStringsCommand(args);
  72. }
  73. else if ( subCommand == "GLOB" )
  74. {
  75. return this->HandleGlobCommand(args, false);
  76. }
  77. else if ( subCommand == "GLOB_RECURSE" )
  78. {
  79. return this->HandleGlobCommand(args, true);
  80. }
  81. else if ( subCommand == "MAKE_DIRECTORY" )
  82. {
  83. return this->HandleMakeDirectoryCommand(args);
  84. }
  85. else if ( subCommand == "REMOVE" )
  86. {
  87. return this->HandleRemove(args, false);
  88. }
  89. else if ( subCommand == "REMOVE_RECURSE" )
  90. {
  91. return this->HandleRemove(args, true);
  92. }
  93. else if ( subCommand == "INSTALL" )
  94. {
  95. return this->HandleInstallCommand(args);
  96. }
  97. else if ( subCommand == "RELATIVE_PATH" )
  98. {
  99. return this->HandleRelativePathCommand(args);
  100. }
  101. else if ( subCommand == "TO_CMAKE_PATH" )
  102. {
  103. return this->HandleCMakePathCommand(args, false);
  104. }
  105. else if ( subCommand == "TO_NATIVE_PATH" )
  106. {
  107. return this->HandleCMakePathCommand(args, true);
  108. }
  109. std::string e = "does not recognize sub-command "+subCommand;
  110. this->SetError(e.c_str());
  111. return false;
  112. }
  113. //----------------------------------------------------------------------------
  114. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  115. bool append)
  116. {
  117. std::string message;
  118. std::vector<std::string>::const_iterator i = args.begin();
  119. i++; // Get rid of subcommand
  120. std::string fileName = *i;
  121. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  122. {
  123. fileName = this->Makefile->GetCurrentDirectory();
  124. fileName += "/" + *i;
  125. }
  126. i++;
  127. for(;i != args.end(); ++i)
  128. {
  129. message += *i;
  130. }
  131. if ( !this->Makefile->CanIWriteThisFile(fileName.c_str()) )
  132. {
  133. std::string e
  134. = "attempted to write a file: " + fileName +
  135. " into a source directory.";
  136. this->SetError(e.c_str());
  137. cmSystemTools::SetFatalErrorOccured();
  138. return false;
  139. }
  140. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  141. cmSystemTools::MakeDirectory(dir.c_str());
  142. mode_t mode =
  143. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  144. S_IREAD | S_IWRITE
  145. #elif defined( __BORLANDC__ )
  146. S_IRUSR | S_IWUSR
  147. #else
  148. S_IRUSR | S_IWUSR |
  149. S_IRGRP |
  150. S_IROTH
  151. #endif
  152. ;
  153. // Set permissions to writable
  154. if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
  155. {
  156. cmSystemTools::SetPermissions(fileName.c_str(),
  157. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  158. S_IREAD | S_IWRITE
  159. #else
  160. S_IRUSR | S_IWUSR
  161. #endif
  162. );
  163. }
  164. // If GetPermissions fails, pretend like it is ok. File open will fail if
  165. // the file is not writable
  166. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  167. if ( !file )
  168. {
  169. std::string error = "Internal CMake error when trying to open file: ";
  170. error += fileName.c_str();
  171. error += " for writing.";
  172. this->SetError(error.c_str());
  173. return false;
  174. }
  175. file << message;
  176. file.close();
  177. cmSystemTools::SetPermissions(fileName.c_str(), mode);
  178. this->Makefile->AddWrittenFile(fileName.c_str());
  179. return true;
  180. }
  181. //----------------------------------------------------------------------------
  182. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  183. {
  184. if ( args.size() < 3 )
  185. {
  186. this->SetError("READ must be called with at least two additional "
  187. "arguments");
  188. return false;
  189. }
  190. std::string fileName = args[1];
  191. if ( !cmsys::SystemTools::FileIsFullPath(args[1].c_str()) )
  192. {
  193. fileName = this->Makefile->GetCurrentDirectory();
  194. fileName += "/" + args[1];
  195. }
  196. std::string variable = args[2];
  197. std::ifstream file(fileName.c_str(), std::ios::in);
  198. if ( !file )
  199. {
  200. std::string error = "Internal CMake error when trying to open file: ";
  201. error += fileName.c_str();
  202. error += " for reading.";
  203. this->SetError(error.c_str());
  204. return false;
  205. }
  206. // if there a limit?
  207. long sizeLimit = -1;
  208. if (args.size() >= 5 && args[3] == "LIMIT")
  209. {
  210. sizeLimit = atoi(args[4].c_str());
  211. }
  212. std::string output;
  213. std::string line;
  214. bool has_newline = false;
  215. while (sizeLimit != 0 &&
  216. cmSystemTools::GetLineFromStream(file, line, &has_newline,
  217. sizeLimit) )
  218. {
  219. if (sizeLimit > 0)
  220. {
  221. sizeLimit = sizeLimit - static_cast<long>(line.size());
  222. if (has_newline)
  223. {
  224. sizeLimit--;
  225. }
  226. if (sizeLimit < 0)
  227. {
  228. sizeLimit = 0;
  229. }
  230. }
  231. output += line;
  232. if ( has_newline )
  233. {
  234. output += "\n";
  235. }
  236. }
  237. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  238. return true;
  239. }
  240. //----------------------------------------------------------------------------
  241. bool cmFileCommand::HandleStringsCommand(std::vector<std::string> const& args)
  242. {
  243. if(args.size() < 3)
  244. {
  245. this->SetError("STRINGS requires a file name and output variable");
  246. return false;
  247. }
  248. // Get the file to read.
  249. std::string fileName = args[1];
  250. if(!cmsys::SystemTools::FileIsFullPath(fileName.c_str()))
  251. {
  252. fileName = this->Makefile->GetCurrentDirectory();
  253. fileName += "/" + args[1];
  254. }
  255. // Get the variable in which to store the results.
  256. std::string outVar = args[2];
  257. // Parse the options.
  258. enum { arg_none,
  259. arg_limit_input,
  260. arg_limit_output,
  261. arg_limit_count,
  262. arg_length_minimum,
  263. arg_length_maximum,
  264. arg__maximum,
  265. arg_regex };
  266. unsigned int minlen = 0;
  267. unsigned int maxlen = 0;
  268. int limit_input = -1;
  269. int limit_output = -1;
  270. unsigned int limit_count = 0;
  271. cmsys::RegularExpression regex;
  272. bool have_regex = false;
  273. bool newline_consume = false;
  274. bool hex_conversion_enabled = true;
  275. int arg_mode = arg_none;
  276. for(unsigned int i=3; i < args.size(); ++i)
  277. {
  278. if(args[i] == "LIMIT_INPUT")
  279. {
  280. arg_mode = arg_limit_input;
  281. }
  282. else if(args[i] == "LIMIT_OUTPUT")
  283. {
  284. arg_mode = arg_limit_output;
  285. }
  286. else if(args[i] == "LIMIT_COUNT")
  287. {
  288. arg_mode = arg_limit_count;
  289. }
  290. else if(args[i] == "LENGTH_MINIMUM")
  291. {
  292. arg_mode = arg_length_minimum;
  293. }
  294. else if(args[i] == "LENGTH_MAXIMUM")
  295. {
  296. arg_mode = arg_length_maximum;
  297. }
  298. else if(args[i] == "REGEX")
  299. {
  300. arg_mode = arg_regex;
  301. }
  302. else if(args[i] == "NEWLINE_CONSUME")
  303. {
  304. newline_consume = true;
  305. arg_mode = arg_none;
  306. }
  307. else if(args[i] == "NO_HEX_CONVERSION")
  308. {
  309. hex_conversion_enabled = false;
  310. arg_mode = arg_none;
  311. }
  312. else if(arg_mode == arg_limit_input)
  313. {
  314. if(sscanf(args[i].c_str(), "%d", &limit_input) != 1 ||
  315. limit_input < 0)
  316. {
  317. cmOStringStream e;
  318. e << "STRINGS option LIMIT_INPUT value \""
  319. << args[i] << "\" is not an unsigned integer.";
  320. this->SetError(e.str().c_str());
  321. return false;
  322. }
  323. arg_mode = arg_none;
  324. }
  325. else if(arg_mode == arg_limit_output)
  326. {
  327. if(sscanf(args[i].c_str(), "%d", &limit_output) != 1 ||
  328. limit_output < 0)
  329. {
  330. cmOStringStream e;
  331. e << "STRINGS option LIMIT_OUTPUT value \""
  332. << args[i] << "\" is not an unsigned integer.";
  333. this->SetError(e.str().c_str());
  334. return false;
  335. }
  336. arg_mode = arg_none;
  337. }
  338. else if(arg_mode == arg_limit_count)
  339. {
  340. int count;
  341. if(sscanf(args[i].c_str(), "%d", &count) != 1 || count < 0)
  342. {
  343. cmOStringStream e;
  344. e << "STRINGS option LIMIT_COUNT value \""
  345. << args[i] << "\" is not an unsigned integer.";
  346. this->SetError(e.str().c_str());
  347. return false;
  348. }
  349. limit_count = count;
  350. arg_mode = arg_none;
  351. }
  352. else if(arg_mode == arg_length_minimum)
  353. {
  354. int len;
  355. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  356. {
  357. cmOStringStream e;
  358. e << "STRINGS option LENGTH_MINIMUM value \""
  359. << args[i] << "\" is not an unsigned integer.";
  360. this->SetError(e.str().c_str());
  361. return false;
  362. }
  363. minlen = len;
  364. arg_mode = arg_none;
  365. }
  366. else if(arg_mode == arg_length_maximum)
  367. {
  368. int len;
  369. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  370. {
  371. cmOStringStream e;
  372. e << "STRINGS option LENGTH_MAXIMUM value \""
  373. << args[i] << "\" is not an unsigned integer.";
  374. this->SetError(e.str().c_str());
  375. return false;
  376. }
  377. maxlen = len;
  378. arg_mode = arg_none;
  379. }
  380. else if(arg_mode == arg_regex)
  381. {
  382. if(!regex.compile(args[i].c_str()))
  383. {
  384. cmOStringStream e;
  385. e << "STRINGS option REGEX value \""
  386. << args[i] << "\" could not be compiled.";
  387. this->SetError(e.str().c_str());
  388. return false;
  389. }
  390. have_regex = true;
  391. arg_mode = arg_none;
  392. }
  393. else
  394. {
  395. cmOStringStream e;
  396. e << "STRINGS given unknown argument \""
  397. << args[i] << "\"";
  398. this->SetError(e.str().c_str());
  399. return false;
  400. }
  401. }
  402. if (hex_conversion_enabled)
  403. {
  404. // TODO: should work without temp file, but just on a memory buffer
  405. std::string binaryFileName = this->Makefile->GetCurrentOutputDirectory();
  406. binaryFileName += cmake::GetCMakeFilesDirectory();
  407. binaryFileName += "/FileCommandStringsBinaryFile";
  408. if(cmHexFileConverter::TryConvert(fileName.c_str(),binaryFileName.c_str()))
  409. {
  410. fileName = binaryFileName;
  411. }
  412. }
  413. // Open the specified file.
  414. #if defined(_WIN32) || defined(__CYGWIN__)
  415. std::ifstream fin(fileName.c_str(), std::ios::in | std::ios::binary);
  416. #else
  417. std::ifstream fin(fileName.c_str(), std::ios::in);
  418. #endif
  419. if(!fin)
  420. {
  421. cmOStringStream e;
  422. e << "STRINGS file \"" << fileName << "\" cannot be read.";
  423. this->SetError(e.str().c_str());
  424. return false;
  425. }
  426. // Parse strings out of the file.
  427. int output_size = 0;
  428. std::vector<std::string> strings;
  429. std::string s;
  430. int c;
  431. while((!limit_count || strings.size() < limit_count) &&
  432. (limit_input < 0 || static_cast<int>(fin.tellg()) < limit_input) &&
  433. (c = fin.get(), fin))
  434. {
  435. if(c == '\0')
  436. {
  437. // A terminating null character has been found. Check if the
  438. // current string matches the requirements. Since it was
  439. // terminated by a null character, we require that the length be
  440. // at least one no matter what the user specified.
  441. if(s.length() >= minlen && s.length() >= 1 &&
  442. (!have_regex || regex.find(s.c_str())))
  443. {
  444. output_size += static_cast<int>(s.size()) + 1;
  445. if(limit_output >= 0 && output_size >= limit_output)
  446. {
  447. s = "";
  448. break;
  449. }
  450. strings.push_back(s);
  451. }
  452. // Reset the string to empty.
  453. s = "";
  454. }
  455. else if(c == '\n' && !newline_consume)
  456. {
  457. // The current line has been terminated. Check if the current
  458. // string matches the requirements. The length may now be as
  459. // low as zero since blank lines are allowed.
  460. if(s.length() >= minlen &&
  461. (!have_regex || regex.find(s.c_str())))
  462. {
  463. output_size += static_cast<int>(s.size()) + 1;
  464. if(limit_output >= 0 && output_size >= limit_output)
  465. {
  466. s = "";
  467. break;
  468. }
  469. strings.push_back(s);
  470. }
  471. // Reset the string to empty.
  472. s = "";
  473. }
  474. else if(c == '\r')
  475. {
  476. // Ignore CR character to make output always have UNIX newlines.
  477. }
  478. else if(c >= 0x20 && c < 0x7F || c == '\t' ||
  479. (c == '\n' && newline_consume))
  480. {
  481. // This is an ASCII character that may be part of a string.
  482. s += c;
  483. }
  484. else
  485. {
  486. // This is a non-string character. Reset the string to emtpy.
  487. s = "";
  488. }
  489. // Terminate a string if the maximum length is reached.
  490. if(maxlen > 0 && s.size() == maxlen)
  491. {
  492. if(s.length() >= minlen &&
  493. (!have_regex || regex.find(s.c_str())))
  494. {
  495. output_size += static_cast<int>(s.size()) + 1;
  496. if(limit_output >= 0 && output_size >= limit_output)
  497. {
  498. s = "";
  499. break;
  500. }
  501. strings.push_back(s);
  502. }
  503. s = "";
  504. }
  505. }
  506. // If there is a non-empty current string we have hit the end of the
  507. // input file or the input size limit. Check if the current string
  508. // matches the requirements.
  509. if((!limit_count || strings.size() < limit_count) &&
  510. !s.empty() && s.length() >= minlen &&
  511. (!have_regex || regex.find(s.c_str())))
  512. {
  513. output_size += static_cast<int>(s.size()) + 1;
  514. if(limit_output < 0 || output_size < limit_output)
  515. {
  516. strings.push_back(s);
  517. }
  518. }
  519. // Encode the result in a CMake list.
  520. const char* sep = "";
  521. std::string output;
  522. for(std::vector<std::string>::const_iterator si = strings.begin();
  523. si != strings.end(); ++si)
  524. {
  525. // Separate the strings in the output to make it a list.
  526. output += sep;
  527. sep = ";";
  528. // Store the string in the output, but escape semicolons to
  529. // make sure it is a list.
  530. std::string const& sr = *si;
  531. for(unsigned int i=0; i < sr.size(); ++i)
  532. {
  533. if(sr[i] == ';')
  534. {
  535. output += '\\';
  536. }
  537. output += sr[i];
  538. }
  539. }
  540. // Save the output in a makefile variable.
  541. this->Makefile->AddDefinition(outVar.c_str(), output.c_str());
  542. return true;
  543. }
  544. //----------------------------------------------------------------------------
  545. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  546. bool recurse)
  547. {
  548. if ( args.size() < 2 )
  549. {
  550. this->SetError("GLOB requires at least a variable name");
  551. return false;
  552. }
  553. std::vector<std::string>::const_iterator i = args.begin();
  554. i++; // Get rid of subcommand
  555. std::string variable = *i;
  556. i++;
  557. cmsys::Glob g;
  558. g.SetRecurse(recurse);
  559. std::string output = "";
  560. bool first = true;
  561. for ( ; i != args.end(); ++i )
  562. {
  563. if ( *i == "RELATIVE" )
  564. {
  565. ++i; // skip RELATIVE
  566. if ( i == args.end() )
  567. {
  568. this->SetError("GLOB requires a directory after the RELATIVE tag");
  569. return false;
  570. }
  571. g.SetRelative(i->c_str());
  572. ++i;
  573. if(i == args.end())
  574. {
  575. this->SetError("GLOB requires a glob expression after the directory");
  576. return false;
  577. }
  578. }
  579. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  580. {
  581. std::string expr = this->Makefile->GetCurrentDirectory();
  582. // Handle script mode
  583. if ( expr.size() > 0 )
  584. {
  585. expr += "/" + *i;
  586. g.FindFiles(expr);
  587. }
  588. else
  589. {
  590. g.FindFiles(*i);
  591. }
  592. }
  593. else
  594. {
  595. g.FindFiles(*i);
  596. }
  597. std::vector<std::string>::size_type cc;
  598. std::vector<std::string>& files = g.GetFiles();
  599. for ( cc = 0; cc < files.size(); cc ++ )
  600. {
  601. if ( !first )
  602. {
  603. output += ";";
  604. }
  605. output += files[cc];
  606. first = false;
  607. }
  608. }
  609. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  610. return true;
  611. }
  612. //----------------------------------------------------------------------------
  613. bool cmFileCommand::HandleMakeDirectoryCommand(
  614. std::vector<std::string> const& args)
  615. {
  616. if(args.size() < 2 )
  617. {
  618. this->SetError("called with incorrect number of arguments");
  619. return false;
  620. }
  621. std::vector<std::string>::const_iterator i = args.begin();
  622. i++; // Get rid of subcommand
  623. std::string expr;
  624. for ( ; i != args.end(); ++i )
  625. {
  626. const std::string* cdir = &(*i);
  627. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  628. {
  629. expr = this->Makefile->GetCurrentDirectory();
  630. expr += "/" + *i;
  631. cdir = &expr;
  632. }
  633. if ( !this->Makefile->CanIWriteThisFile(cdir->c_str()) )
  634. {
  635. std::string e = "attempted to create a directory: " + *cdir
  636. + " into a source directory.";
  637. this->SetError(e.c_str());
  638. cmSystemTools::SetFatalErrorOccured();
  639. return false;
  640. }
  641. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  642. {
  643. std::string error = "problem creating directory: " + *cdir;
  644. this->SetError(error.c_str());
  645. return false;
  646. }
  647. }
  648. return true;
  649. }
  650. //----------------------------------------------------------------------------
  651. // File installation helper class.
  652. struct cmFileInstaller
  653. {
  654. // Methods to actually install files.
  655. bool InstallFile(const char* fromFile, const char* toFile, bool always);
  656. bool InstallDirectory(const char* source, const char* destination,
  657. bool always);
  658. // All instances need the file command and makefile using them.
  659. cmFileInstaller(cmFileCommand* fc, cmMakefile* mf):
  660. FileCommand(fc), Makefile(mf), DestDirLength(0), MatchlessFiles(true)
  661. {
  662. // Get the current manifest.
  663. this->Manifest =
  664. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  665. }
  666. ~cmFileInstaller()
  667. {
  668. // Save the updated install manifest.
  669. this->Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  670. this->Manifest.c_str());
  671. }
  672. private:
  673. cmFileCommand* FileCommand;
  674. cmMakefile* Makefile;
  675. cmFileTimeComparison FileTimes;
  676. public:
  677. // The length of the destdir setting.
  678. int DestDirLength;
  679. // Whether to install a file not matching any expression.
  680. bool MatchlessFiles;
  681. // The current file manifest (semicolon separated list).
  682. std::string Manifest;
  683. // Permissions for files and directories installed by this object.
  684. mode_t FilePermissions;
  685. mode_t DirPermissions;
  686. // Properties set by pattern and regex match rules.
  687. struct MatchProperties
  688. {
  689. bool Exclude;
  690. mode_t Permissions;
  691. MatchProperties(): Exclude(false), Permissions(0) {}
  692. };
  693. struct MatchRule
  694. {
  695. cmsys::RegularExpression Regex;
  696. MatchProperties Properties;
  697. std::string RegexString;
  698. MatchRule(std::string const& regex):
  699. Regex(regex.c_str()), RegexString(regex) {}
  700. };
  701. std::vector<MatchRule> MatchRules;
  702. // Get the properties from rules matching this input file.
  703. MatchProperties CollectMatchProperties(const char* file,
  704. bool isDirectory)
  705. {
  706. // Match rules are case-insensitive on some platforms.
  707. #if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
  708. std::string lower = cmSystemTools::LowerCase(file);
  709. file = lower.c_str();
  710. #endif
  711. // Collect properties from all matching rules.
  712. bool matched = false;
  713. MatchProperties result;
  714. for(std::vector<MatchRule>::iterator mr = this->MatchRules.begin();
  715. mr != this->MatchRules.end(); ++mr)
  716. {
  717. if(mr->Regex.find(file))
  718. {
  719. matched = true;
  720. result.Exclude |= mr->Properties.Exclude;
  721. result.Permissions |= mr->Properties.Permissions;
  722. }
  723. }
  724. if(!matched && !this->MatchlessFiles && !isDirectory)
  725. {
  726. result.Exclude = true;
  727. }
  728. return result;
  729. }
  730. // Append a file to the installation manifest.
  731. void ManifestAppend(std::string const& file)
  732. {
  733. this->Manifest += ";";
  734. this->Manifest += file.substr(this->DestDirLength);
  735. }
  736. // Translate an argument to a permissions bit.
  737. bool CheckPermissions(std::string const& arg, mode_t& permissions)
  738. {
  739. if(arg == "OWNER_READ") { permissions |= mode_owner_read; }
  740. else if(arg == "OWNER_WRITE") { permissions |= mode_owner_write; }
  741. else if(arg == "OWNER_EXECUTE") { permissions |= mode_owner_execute; }
  742. else if(arg == "GROUP_READ") { permissions |= mode_group_read; }
  743. else if(arg == "GROUP_WRITE") { permissions |= mode_group_write; }
  744. else if(arg == "GROUP_EXECUTE") { permissions |= mode_group_execute; }
  745. else if(arg == "WORLD_READ") { permissions |= mode_world_read; }
  746. else if(arg == "WORLD_WRITE") { permissions |= mode_world_write; }
  747. else if(arg == "WORLD_EXECUTE") { permissions |= mode_world_execute; }
  748. else if(arg == "SETUID") { permissions |= mode_setuid; }
  749. else if(arg == "SETGID") { permissions |= mode_setgid; }
  750. else
  751. {
  752. cmOStringStream e;
  753. e << "INSTALL given invalid permission \"" << arg << "\".";
  754. this->FileCommand->SetError(e.str().c_str());
  755. return false;
  756. }
  757. return true;
  758. }
  759. private:
  760. bool InstallSymlink(const char* fromFile, const char* toFile, bool always);
  761. };
  762. //----------------------------------------------------------------------------
  763. bool cmFileInstaller::InstallSymlink(const char* fromFile, const char* toFile,
  764. bool always)
  765. {
  766. // Read the original symlink.
  767. std::string symlinkTarget;
  768. if(!cmSystemTools::ReadSymlink(fromFile, symlinkTarget))
  769. {
  770. cmOStringStream e;
  771. e << "INSTALL cannot read symlink \"" << fromFile
  772. << "\" to duplicate at \"" << toFile << "\".";
  773. this->FileCommand->SetError(e.str().c_str());
  774. return false;
  775. }
  776. // Compare the symlink value to that at the destination if not
  777. // always installing.
  778. bool copy = true;
  779. if(!always)
  780. {
  781. std::string oldSymlinkTarget;
  782. if(cmSystemTools::ReadSymlink(toFile, oldSymlinkTarget))
  783. {
  784. if(symlinkTarget == oldSymlinkTarget)
  785. {
  786. copy = false;
  787. }
  788. }
  789. }
  790. // Inform the user about this file installation.
  791. std::string message = (copy? "Installing: " : "Up-to-date: ");
  792. message += toFile;
  793. this->Makefile->DisplayStatus(message.c_str(), -1);
  794. if(copy)
  795. {
  796. // Remove the destination file so we can always create the symlink.
  797. cmSystemTools::RemoveFile(toFile);
  798. // Create the symlink.
  799. if(!cmSystemTools::CreateSymlink(symlinkTarget.c_str(), toFile))
  800. {
  801. cmOStringStream e;
  802. e << "INSTALL cannot duplicate symlink \"" << fromFile
  803. << "\" at \"" << toFile << "\".";
  804. this->FileCommand->SetError(e.str().c_str());
  805. return false;
  806. }
  807. }
  808. // Add the file to the manifest.
  809. this->ManifestAppend(toFile);
  810. return true;
  811. }
  812. //----------------------------------------------------------------------------
  813. bool cmFileInstaller::InstallFile(const char* fromFile, const char* toFile,
  814. bool always)
  815. {
  816. // Collect any properties matching this file name.
  817. MatchProperties match_properties =
  818. this->CollectMatchProperties(fromFile, false);
  819. // Skip the file if it is excluded.
  820. if(match_properties.Exclude)
  821. {
  822. return true;
  823. }
  824. // Short-circuit for symbolic links.
  825. if(cmSystemTools::FileIsSymlink(fromFile))
  826. {
  827. return this->InstallSymlink(fromFile, toFile, always);
  828. }
  829. // Determine whether we will copy the file.
  830. bool copy = true;
  831. if(!always)
  832. {
  833. // If both files exist and "fromFile" is not newer than "toFile"
  834. // do not copy.
  835. int timeResult;
  836. if(this->FileTimes.FileTimeCompare(fromFile, toFile, &timeResult) &&
  837. timeResult <= 0)
  838. {
  839. copy = false;
  840. }
  841. }
  842. // Inform the user about this file installation.
  843. std::string message = (copy? "Installing: " : "Up-to-date: ");
  844. message += toFile;
  845. this->Makefile->DisplayStatus(message.c_str(), -1);
  846. // Copy the file.
  847. if(copy && !cmSystemTools::CopyAFile(fromFile, toFile, true))
  848. {
  849. cmOStringStream e;
  850. e << "INSTALL cannot copy file \"" << fromFile
  851. << "\" to \"" << toFile << "\".";
  852. this->FileCommand->SetError(e.str().c_str());
  853. return false;
  854. }
  855. // Add the file to the manifest.
  856. this->ManifestAppend(toFile);
  857. // Set the file modification time of the destination file.
  858. if(copy && !always)
  859. {
  860. cmSystemTools::CopyFileTime(fromFile, toFile);
  861. }
  862. // Set permissions of the destination file.
  863. mode_t permissions = (match_properties.Permissions?
  864. match_properties.Permissions : this->FilePermissions);
  865. if(!permissions)
  866. {
  867. // No permissions were explicitly provided but the user requested
  868. // that the source file permissions be used.
  869. cmSystemTools::GetPermissions(fromFile, permissions);
  870. }
  871. if(permissions && !cmSystemTools::SetPermissions(toFile, permissions))
  872. {
  873. cmOStringStream e;
  874. e << "Problem setting permissions on file \"" << toFile << "\"";
  875. this->FileCommand->SetError(e.str().c_str());
  876. return false;
  877. }
  878. return true;
  879. }
  880. //----------------------------------------------------------------------------
  881. bool cmFileInstaller::InstallDirectory(const char* source,
  882. const char* destination,
  883. bool always)
  884. {
  885. // Collect any properties matching this directory name.
  886. MatchProperties match_properties =
  887. this->CollectMatchProperties(source, true);
  888. // Skip the directory if it is excluded.
  889. if(match_properties.Exclude)
  890. {
  891. return true;
  892. }
  893. // Short-circuit for symbolic links.
  894. if(cmSystemTools::FileIsSymlink(source))
  895. {
  896. return this->InstallSymlink(source, destination, always);
  897. }
  898. // Inform the user about this directory installation.
  899. std::string message = "Installing: ";
  900. message += destination;
  901. this->Makefile->DisplayStatus(message.c_str(), -1);
  902. // Make sure the destination directory exists.
  903. if(!cmSystemTools::MakeDirectory(destination))
  904. {
  905. return false;
  906. }
  907. // Compute the requested permissions for the destination directory.
  908. mode_t permissions = (match_properties.Permissions?
  909. match_properties.Permissions : this->DirPermissions);
  910. if(!permissions)
  911. {
  912. // No permissions were explicitly provided but the user requested
  913. // that the source directory permissions be used.
  914. cmSystemTools::GetPermissions(source, permissions);
  915. }
  916. // Compute the set of permissions required on this directory to
  917. // recursively install files and subdirectories safely.
  918. mode_t required_permissions =
  919. mode_owner_read | mode_owner_write | mode_owner_execute;
  920. // If the required permissions are specified it is safe to set the
  921. // final permissions now. Otherwise we must add the required
  922. // permissions temporarily during file installation.
  923. mode_t permissions_before = 0;
  924. mode_t permissions_after = 0;
  925. if(permissions & required_permissions)
  926. {
  927. permissions_before = permissions;
  928. }
  929. else
  930. {
  931. permissions_before = permissions | required_permissions;
  932. permissions_after = permissions;
  933. }
  934. // Set the required permissions of the destination directory.
  935. if(permissions_before &&
  936. !cmSystemTools::SetPermissions(destination, permissions_before))
  937. {
  938. cmOStringStream e;
  939. e << "Problem setting permissions on directory \""
  940. << destination << "\"";
  941. this->FileCommand->SetError(e.str().c_str());
  942. return false;
  943. }
  944. // Load the directory contents to traverse it recursively.
  945. cmsys::Directory dir;
  946. if(source && *source)
  947. {
  948. dir.Load(source);
  949. }
  950. unsigned long numFiles = static_cast<unsigned long>(dir.GetNumberOfFiles());
  951. for(unsigned long fileNum = 0; fileNum < numFiles; ++fileNum)
  952. {
  953. if(!(strcmp(dir.GetFile(fileNum), ".") == 0 ||
  954. strcmp(dir.GetFile(fileNum), "..") == 0))
  955. {
  956. cmsys_stl::string fromPath = source;
  957. fromPath += "/";
  958. fromPath += dir.GetFile(fileNum);
  959. if(cmSystemTools::FileIsDirectory(fromPath.c_str()))
  960. {
  961. cmsys_stl::string toDir = destination;
  962. toDir += "/";
  963. toDir += dir.GetFile(fileNum);
  964. if(!this->InstallDirectory(fromPath.c_str(), toDir.c_str(), always))
  965. {
  966. return false;
  967. }
  968. }
  969. else
  970. {
  971. // Install this file.
  972. std::string toFile = destination;
  973. toFile += "/";
  974. toFile += dir.GetFile(fileNum);
  975. if(!this->InstallFile(fromPath.c_str(), toFile.c_str(), always))
  976. {
  977. return false;
  978. }
  979. }
  980. }
  981. }
  982. // Set the requested permissions of the destination directory.
  983. if(permissions_after &&
  984. !cmSystemTools::SetPermissions(destination, permissions_after))
  985. {
  986. cmOStringStream e;
  987. e << "Problem setting permissions on directory \"" << destination << "\"";
  988. this->FileCommand->SetError(e.str().c_str());
  989. return false;
  990. }
  991. return true;
  992. }
  993. //----------------------------------------------------------------------------
  994. void cmFileCommand::HandleInstallPermissions(cmFileInstaller& installer,
  995. mode_t& permissions_file,
  996. mode_t& permissions_dir,
  997. int itype,
  998. bool use_given_permissions_file,
  999. bool use_given_permissions_dir,
  1000. bool use_source_permissions) const
  1001. {
  1002. // Choose a default for shared library permissions.
  1003. bool install_so_no_exe = this->Makefile->IsOn("CMAKE_INSTALL_SO_NO_EXE");
  1004. // If file permissions were not specified set default permissions
  1005. // for this target type.
  1006. if(!use_given_permissions_file && !use_source_permissions)
  1007. {
  1008. switch(itype)
  1009. {
  1010. case cmTarget::SHARED_LIBRARY:
  1011. case cmTarget::MODULE_LIBRARY:
  1012. if(install_so_no_exe)
  1013. {
  1014. // Use read/write permissions.
  1015. permissions_file = 0;
  1016. permissions_file |= mode_owner_read;
  1017. permissions_file |= mode_owner_write;
  1018. permissions_file |= mode_group_read;
  1019. permissions_file |= mode_world_read;
  1020. break;
  1021. }
  1022. case cmTarget::EXECUTABLE:
  1023. case cmTarget::INSTALL_PROGRAMS:
  1024. // Use read/write/executable permissions.
  1025. permissions_file = 0;
  1026. permissions_file |= mode_owner_read;
  1027. permissions_file |= mode_owner_write;
  1028. permissions_file |= mode_owner_execute;
  1029. permissions_file |= mode_group_read;
  1030. permissions_file |= mode_group_execute;
  1031. permissions_file |= mode_world_read;
  1032. permissions_file |= mode_world_execute;
  1033. break;
  1034. default:
  1035. // Use read/write permissions.
  1036. permissions_file = 0;
  1037. permissions_file |= mode_owner_read;
  1038. permissions_file |= mode_owner_write;
  1039. permissions_file |= mode_group_read;
  1040. permissions_file |= mode_world_read;
  1041. break;
  1042. }
  1043. }
  1044. // If directory permissions were not specified set default permissions.
  1045. if(!use_given_permissions_dir && !use_source_permissions)
  1046. {
  1047. // Use read/write/executable permissions.
  1048. permissions_dir = 0;
  1049. permissions_dir |= mode_owner_read;
  1050. permissions_dir |= mode_owner_write;
  1051. permissions_dir |= mode_owner_execute;
  1052. permissions_dir |= mode_group_read;
  1053. permissions_dir |= mode_group_execute;
  1054. permissions_dir |= mode_world_read;
  1055. permissions_dir |= mode_world_execute;
  1056. }
  1057. // Set the installer permissions.
  1058. installer.FilePermissions = permissions_file;
  1059. installer.DirPermissions = permissions_dir;
  1060. }
  1061. //----------------------------------------------------------------------------
  1062. void cmFileCommand
  1063. ::GetTargetTypeFromString(const std::string& stype, int& itype) const
  1064. {
  1065. if ( stype == "EXECUTABLE" )
  1066. {
  1067. itype = cmTarget::EXECUTABLE;
  1068. }
  1069. else if ( stype == "PROGRAM" )
  1070. {
  1071. itype = cmTarget::INSTALL_PROGRAMS;
  1072. }
  1073. else if ( stype == "STATIC_LIBRARY" )
  1074. {
  1075. itype = cmTarget::STATIC_LIBRARY;
  1076. }
  1077. else if ( stype == "SHARED_LIBRARY" )
  1078. {
  1079. itype = cmTarget::SHARED_LIBRARY;
  1080. }
  1081. else if ( stype == "MODULE" )
  1082. {
  1083. itype = cmTarget::MODULE_LIBRARY;
  1084. }
  1085. else if ( stype == "DIRECTORY" )
  1086. {
  1087. itype = cmTarget::INSTALL_DIRECTORY;
  1088. }
  1089. }
  1090. //----------------------------------------------------------------------------
  1091. bool cmFileCommand::HandleInstallDestination(cmFileInstaller& installer,
  1092. std::string& destination)
  1093. {
  1094. if ( destination.size() < 2 )
  1095. {
  1096. this->SetError("called with inapropriate arguments. "
  1097. "No DESTINATION provided or .");
  1098. return false;
  1099. }
  1100. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  1101. if ( destdir && *destdir )
  1102. {
  1103. std::string sdestdir = destdir;
  1104. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  1105. char ch1 = destination[0];
  1106. char ch2 = destination[1];
  1107. char ch3 = 0;
  1108. if ( destination.size() > 2 )
  1109. {
  1110. ch3 = destination[2];
  1111. }
  1112. int skip = 0;
  1113. if ( ch1 != '/' )
  1114. {
  1115. int relative = 0;
  1116. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  1117. ch2 == ':' )
  1118. {
  1119. // Assume windows
  1120. // let's do some destdir magic:
  1121. skip = 2;
  1122. if ( ch3 != '/' )
  1123. {
  1124. relative = 1;
  1125. }
  1126. }
  1127. else
  1128. {
  1129. relative = 1;
  1130. }
  1131. if ( relative )
  1132. {
  1133. // This is relative path on unix or windows. Since we are doing
  1134. // destdir, this case does not make sense.
  1135. this->SetError("called with relative DESTINATION. This "
  1136. "does not make sense when using DESTDIR. Specify "
  1137. "absolute path or remove DESTDIR environment variable.");
  1138. return false;
  1139. }
  1140. }
  1141. else
  1142. {
  1143. if ( ch2 == '/' )
  1144. {
  1145. // looks like a network path.
  1146. this->SetError("called with network path DESTINATION. This "
  1147. "does not make sense when using DESTDIR. Specify local "
  1148. "absolute path or remove DESTDIR environment variable.");
  1149. return false;
  1150. }
  1151. }
  1152. destination = sdestdir + (destination.c_str() + skip);
  1153. installer.DestDirLength = int(sdestdir.size());
  1154. }
  1155. if ( !cmSystemTools::FileExists(destination.c_str()) )
  1156. {
  1157. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  1158. {
  1159. std::string errstring = "cannot create directory: " + destination +
  1160. ". Maybe need administrative privileges.";
  1161. this->SetError(errstring.c_str());
  1162. return false;
  1163. }
  1164. }
  1165. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  1166. {
  1167. std::string errstring = "INSTALL destination: " + destination +
  1168. " is not a directory.";
  1169. this->SetError(errstring.c_str());
  1170. return false;
  1171. }
  1172. return true;
  1173. }
  1174. //----------------------------------------------------------------------------
  1175. bool cmFileCommand::HandleInstallCommand(std::vector<std::string> const& args)
  1176. {
  1177. if ( args.size() < 6 )
  1178. {
  1179. this->SetError("called with incorrect number of arguments");
  1180. return false;
  1181. }
  1182. // Construct a file installer object.
  1183. cmFileInstaller installer(this, this->Makefile);
  1184. std::string rename = "";
  1185. std::string destination = "";
  1186. std::vector<std::string> files;
  1187. int itype = cmTarget::INSTALL_FILES;
  1188. std::map<cmStdString, const char*> properties;
  1189. bool optional = false;
  1190. bool result = this->ParseInstallArgs(args, installer, properties,
  1191. itype, rename, destination, files,
  1192. optional);
  1193. if (result == true)
  1194. {
  1195. result = this->DoInstall(installer,
  1196. itype, rename, destination, files, optional);
  1197. }
  1198. return result;
  1199. }
  1200. //----------------------------------------------------------------------------
  1201. bool cmFileCommand::ParseInstallArgs(std::vector<std::string> const& args,
  1202. cmFileInstaller& installer,
  1203. std::map<cmStdString, const char*>& properties,
  1204. int& itype,
  1205. std::string& rename,
  1206. std::string& destination,
  1207. std::vector<std::string>& files,
  1208. bool& optional)
  1209. {
  1210. std::string stype = "FILES";
  1211. bool doing_files = false;
  1212. bool doing_properties = false;
  1213. bool doing_permissions_file = false;
  1214. bool doing_permissions_dir = false;
  1215. bool doing_permissions_match = false;
  1216. bool use_given_permissions_file = false;
  1217. bool use_given_permissions_dir = false;
  1218. bool use_source_permissions = false;
  1219. mode_t permissions_file = 0;
  1220. mode_t permissions_dir = 0;
  1221. cmFileInstaller::MatchRule* current_match_rule = 0;
  1222. std::vector<std::string>::size_type i = 0;
  1223. i++; // Get rid of subcommand
  1224. for ( ; i != args.size(); ++i )
  1225. {
  1226. const std::string* cstr = &args[i];
  1227. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  1228. {
  1229. if(current_match_rule)
  1230. {
  1231. cmOStringStream e;
  1232. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1233. this->SetError(e.str().c_str());
  1234. return false;
  1235. }
  1236. i++;
  1237. destination = args[i];
  1238. doing_files = false;
  1239. doing_properties = false;
  1240. doing_permissions_file = false;
  1241. doing_permissions_dir = false;
  1242. }
  1243. else if ( *cstr == "TYPE" && i < args.size()-1 )
  1244. {
  1245. if(current_match_rule)
  1246. {
  1247. cmOStringStream e;
  1248. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1249. this->SetError(e.str().c_str());
  1250. return false;
  1251. }
  1252. i++;
  1253. stype = args[i];
  1254. if ( args[i+1] == "OPTIONAL" )
  1255. {
  1256. i++;
  1257. optional = true;
  1258. }
  1259. doing_properties = false;
  1260. doing_files = false;
  1261. doing_permissions_file = false;
  1262. doing_permissions_dir = false;
  1263. }
  1264. else if ( *cstr == "RENAME" && i < args.size()-1 )
  1265. {
  1266. if(current_match_rule)
  1267. {
  1268. cmOStringStream e;
  1269. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1270. this->SetError(e.str().c_str());
  1271. return false;
  1272. }
  1273. i++;
  1274. rename = args[i];
  1275. doing_properties = false;
  1276. doing_files = false;
  1277. doing_permissions_file = false;
  1278. doing_permissions_dir = false;
  1279. }
  1280. else if ( *cstr == "REGEX" && i < args.size()-1 )
  1281. {
  1282. i++;
  1283. installer.MatchRules.push_back(cmFileInstaller::MatchRule(args[i]));
  1284. current_match_rule = &*(installer.MatchRules.end()-1);
  1285. if(!current_match_rule->Regex.is_valid())
  1286. {
  1287. cmOStringStream e;
  1288. e << "INSTALL could not compile REGEX \"" << args[i] << "\".";
  1289. this->SetError(e.str().c_str());
  1290. return false;
  1291. }
  1292. doing_properties = false;
  1293. doing_files = false;
  1294. doing_permissions_file = false;
  1295. doing_permissions_dir = false;
  1296. }
  1297. else if ( *cstr == "EXCLUDE" )
  1298. {
  1299. // Add this property to the current match rule.
  1300. if(!current_match_rule)
  1301. {
  1302. cmOStringStream e;
  1303. e << "INSTALL does not allow \""
  1304. << *cstr << "\" before a REGEX is given.";
  1305. this->SetError(e.str().c_str());
  1306. return false;
  1307. }
  1308. current_match_rule->Properties.Exclude = true;
  1309. doing_permissions_match = true;
  1310. }
  1311. else if ( *cstr == "PROPERTIES" )
  1312. {
  1313. if(current_match_rule)
  1314. {
  1315. cmOStringStream e;
  1316. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1317. this->SetError(e.str().c_str());
  1318. return false;
  1319. }
  1320. doing_properties = true;
  1321. doing_files = false;
  1322. doing_permissions_file = false;
  1323. doing_permissions_dir = false;
  1324. }
  1325. else if ( *cstr == "PERMISSIONS" )
  1326. {
  1327. if(current_match_rule)
  1328. {
  1329. doing_permissions_match = true;
  1330. doing_permissions_file = false;
  1331. }
  1332. else
  1333. {
  1334. doing_permissions_match = false;
  1335. doing_permissions_file = true;
  1336. use_given_permissions_file = true;
  1337. }
  1338. doing_properties = false;
  1339. doing_files = false;
  1340. doing_permissions_dir = false;
  1341. }
  1342. else if ( *cstr == "DIR_PERMISSIONS" )
  1343. {
  1344. if(current_match_rule)
  1345. {
  1346. cmOStringStream e;
  1347. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1348. this->SetError(e.str().c_str());
  1349. return false;
  1350. }
  1351. use_given_permissions_dir = true;
  1352. doing_properties = false;
  1353. doing_files = false;
  1354. doing_permissions_file = false;
  1355. doing_permissions_dir = true;
  1356. }
  1357. else if ( *cstr == "USE_SOURCE_PERMISSIONS" )
  1358. {
  1359. if(current_match_rule)
  1360. {
  1361. cmOStringStream e;
  1362. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1363. this->SetError(e.str().c_str());
  1364. return false;
  1365. }
  1366. doing_properties = false;
  1367. doing_files = false;
  1368. doing_permissions_file = false;
  1369. doing_permissions_dir = false;
  1370. use_source_permissions = true;
  1371. }
  1372. else if ( *cstr == "FILES_MATCHING" )
  1373. {
  1374. if(current_match_rule)
  1375. {
  1376. cmOStringStream e;
  1377. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1378. this->SetError(e.str().c_str());
  1379. return false;
  1380. }
  1381. doing_properties = false;
  1382. doing_files = false;
  1383. doing_permissions_file = false;
  1384. doing_permissions_dir = false;
  1385. installer.MatchlessFiles = false;
  1386. }
  1387. else if ( *cstr == "COMPONENTS" )
  1388. {
  1389. cmOStringStream e;
  1390. e << "INSTALL called with old-style COMPONENTS argument. "
  1391. << "This script was generated with an older version of CMake. "
  1392. << "Re-run this cmake version on your build tree.";
  1393. this->SetError(e.str().c_str());
  1394. return false;
  1395. }
  1396. else if ( *cstr == "CONFIGURATIONS" )
  1397. {
  1398. cmOStringStream e;
  1399. e << "INSTALL called with old-style CONFIGURATIONS argument. "
  1400. << "This script was generated with an older version of CMake. "
  1401. << "Re-run this cmake version on your build tree.";
  1402. this->SetError(e.str().c_str());
  1403. return false;
  1404. }
  1405. else if ( *cstr == "FILES" && !doing_files)
  1406. {
  1407. if(current_match_rule)
  1408. {
  1409. cmOStringStream e;
  1410. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1411. this->SetError(e.str().c_str());
  1412. return false;
  1413. }
  1414. doing_files = true;
  1415. doing_properties = false;
  1416. doing_permissions_file = false;
  1417. doing_permissions_dir = false;
  1418. }
  1419. else if ( doing_properties && i < args.size()-1 )
  1420. {
  1421. properties[args[i]] = args[i+1].c_str();
  1422. i++;
  1423. }
  1424. else if ( doing_files )
  1425. {
  1426. files.push_back(*cstr);
  1427. }
  1428. else if(doing_permissions_file)
  1429. {
  1430. if(!installer.CheckPermissions(args[i], permissions_file))
  1431. {
  1432. return false;
  1433. }
  1434. }
  1435. else if(doing_permissions_dir)
  1436. {
  1437. if(!installer.CheckPermissions(args[i], permissions_dir))
  1438. {
  1439. return false;
  1440. }
  1441. }
  1442. else if(doing_permissions_match)
  1443. {
  1444. if(!installer.CheckPermissions(
  1445. args[i], current_match_rule->Properties.Permissions))
  1446. {
  1447. return false;
  1448. }
  1449. }
  1450. else
  1451. {
  1452. this->SetError("called with inappropriate arguments");
  1453. return false;
  1454. }
  1455. }
  1456. // now check and postprocess what has been parsed
  1457. if ( files.size() == 0 )
  1458. {
  1459. this->SetError(
  1460. "called with inapropriate arguments. No FILES provided.");
  1461. return false;
  1462. }
  1463. // Check rename form.
  1464. if(!rename.empty())
  1465. {
  1466. if(itype != cmTarget::INSTALL_FILES &&
  1467. itype != cmTarget::INSTALL_PROGRAMS)
  1468. {
  1469. this->SetError("INSTALL option RENAME may be used only with "
  1470. "FILES or PROGRAMS.");
  1471. return false;
  1472. }
  1473. if(files.size() > 1)
  1474. {
  1475. this->SetError("INSTALL option RENAME may be used only with "
  1476. "one file.");
  1477. return false;
  1478. }
  1479. }
  1480. if (this->HandleInstallDestination(installer, destination) == false)
  1481. {
  1482. return false;
  1483. }
  1484. if(properties.find("VERSION") != properties.end())
  1485. {
  1486. cmOStringStream e;
  1487. e << "INSTALL called with old-style VERSION property. "
  1488. << "This script was generated with an older version of CMake. "
  1489. << "Re-run this cmake version on your build tree.";
  1490. this->SetError(e.str().c_str());
  1491. return false;
  1492. }
  1493. if(properties.find("SOVERSION") != properties.end())
  1494. {
  1495. cmOStringStream e;
  1496. e << "INSTALL called with old-style SOVERSION property. "
  1497. << "This script was generated with an older version of CMake. "
  1498. << "Re-run this cmake version on your build tree.";
  1499. this->SetError(e.str().c_str());
  1500. return false;
  1501. }
  1502. this->GetTargetTypeFromString(stype, itype);
  1503. this->HandleInstallPermissions(installer,
  1504. permissions_file,
  1505. permissions_dir,
  1506. itype,
  1507. use_given_permissions_file,
  1508. use_given_permissions_dir,
  1509. use_source_permissions);
  1510. return true;
  1511. }
  1512. //----------------------------------------------------------------------------
  1513. bool cmFileCommand::DoInstall( cmFileInstaller& installer,
  1514. const int itype,
  1515. const std::string& rename,
  1516. const std::string& destination,
  1517. const std::vector<std::string>& files,
  1518. const bool optional)
  1519. {
  1520. typedef std::set<cmStdString>::const_iterator iter_type;
  1521. // Check whether files should be copied always or only if they have
  1522. // changed.
  1523. bool copy_always =
  1524. cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_INSTALL_ALWAYS"));
  1525. // Handle each file listed.
  1526. for (std::vector<std::string>::size_type i = 0; i < files.size(); i ++ )
  1527. {
  1528. // Split the input file into its directory and name components.
  1529. std::vector<std::string> fromPathComponents;
  1530. cmSystemTools::SplitPath(files[i].c_str(), fromPathComponents);
  1531. std::string fromName = *(fromPathComponents.end()-1);
  1532. std::string fromDir = cmSystemTools::JoinPath(fromPathComponents.begin(),
  1533. fromPathComponents.end()-1);
  1534. // Compute the full path to the destination file.
  1535. std::string toFile = destination;
  1536. std::string const& toName = rename.empty()? fromName : rename;
  1537. if(!toName.empty())
  1538. {
  1539. toFile += "/";
  1540. toFile += toName;
  1541. }
  1542. // Construct the full path to the source file. The file name may
  1543. // have been changed above.
  1544. std::string fromFile = fromDir;
  1545. if(!fromName.empty())
  1546. {
  1547. fromFile += "/";
  1548. fromFile += fromName;
  1549. }
  1550. std::string message;
  1551. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  1552. {
  1553. if(itype == cmTarget::INSTALL_DIRECTORY &&
  1554. (fromFile.empty() ||
  1555. cmSystemTools::FileIsDirectory(fromFile.c_str())))
  1556. {
  1557. // Try installing this directory.
  1558. if(!installer.InstallDirectory(fromFile.c_str(), toFile.c_str(),
  1559. copy_always))
  1560. {
  1561. return false;
  1562. }
  1563. }
  1564. else if(cmSystemTools::FileExists(fromFile.c_str()))
  1565. {
  1566. // Install this file.
  1567. if(!installer.InstallFile(fromFile.c_str(), toFile.c_str(),
  1568. copy_always))
  1569. {
  1570. return false;
  1571. }
  1572. }
  1573. else if(!optional)
  1574. {
  1575. // The input file does not exist and installation is not optional.
  1576. cmOStringStream e;
  1577. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  1578. this->SetError(e.str().c_str());
  1579. return false;
  1580. }
  1581. }
  1582. }
  1583. return true;
  1584. }
  1585. //----------------------------------------------------------------------------
  1586. bool cmFileCommand::HandleRelativePathCommand(
  1587. std::vector<std::string> const& args)
  1588. {
  1589. if(args.size() != 4 )
  1590. {
  1591. this->SetError("called with incorrect number of arguments");
  1592. return false;
  1593. }
  1594. const std::string& outVar = args[1];
  1595. const std::string& directoryName = args[2];
  1596. const std::string& fileName = args[3];
  1597. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  1598. {
  1599. std::string errstring =
  1600. "RelativePath must be passed a full path to the directory: "
  1601. + directoryName;
  1602. this->SetError(errstring.c_str());
  1603. return false;
  1604. }
  1605. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  1606. {
  1607. std::string errstring =
  1608. "RelativePath must be passed a full path to the file: "
  1609. + fileName;
  1610. this->SetError(errstring.c_str());
  1611. return false;
  1612. }
  1613. std::string res = cmSystemTools::RelativePath(directoryName.c_str(),
  1614. fileName.c_str());
  1615. this->Makefile->AddDefinition(outVar.c_str(),
  1616. res.c_str());
  1617. return true;
  1618. }
  1619. //----------------------------------------------------------------------------
  1620. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  1621. bool recurse)
  1622. {
  1623. std::string message;
  1624. std::vector<std::string>::const_iterator i = args.begin();
  1625. i++; // Get rid of subcommand
  1626. for(;i != args.end(); ++i)
  1627. {
  1628. if(cmSystemTools::FileIsDirectory(i->c_str()) && recurse)
  1629. {
  1630. cmSystemTools::RemoveADirectory(i->c_str());
  1631. }
  1632. else
  1633. {
  1634. cmSystemTools::RemoveFile(i->c_str());
  1635. }
  1636. }
  1637. return true;
  1638. }
  1639. //----------------------------------------------------------------------------
  1640. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  1641. const& args,
  1642. bool nativePath)
  1643. {
  1644. std::vector<std::string>::const_iterator i = args.begin();
  1645. if(args.size() != 3)
  1646. {
  1647. this->SetError("FILE(SYSTEM_PATH ENV result) must be called with "
  1648. "only three arguments.");
  1649. return false;
  1650. }
  1651. i++; // Get rid of subcommand
  1652. #if defined(_WIN32) && !defined(__CYGWIN__)
  1653. char pathSep = ';';
  1654. #else
  1655. char pathSep = ':';
  1656. #endif
  1657. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  1658. pathSep);
  1659. i++;
  1660. const char* var = i->c_str();
  1661. std::string value;
  1662. for(std::vector<cmsys::String>::iterator j = path.begin();
  1663. j != path.end(); ++j)
  1664. {
  1665. if(j != path.begin())
  1666. {
  1667. value += ";";
  1668. }
  1669. if(!nativePath)
  1670. {
  1671. cmSystemTools::ConvertToUnixSlashes(*j);
  1672. }
  1673. else
  1674. {
  1675. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  1676. // remove double quotes in the path
  1677. cmsys::String& s = *j;
  1678. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  1679. {
  1680. s = s.substr(1,s.size()-2);
  1681. }
  1682. }
  1683. value += *j;
  1684. }
  1685. this->Makefile->AddDefinition(var, value.c_str());
  1686. return true;
  1687. }