cmFileCommand.cxx 61 KB

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