cmFileCommand.cxx 64 KB

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