cmFileCommand.cxx 58 KB

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