cmFileCommand.cxx 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373
  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. bool explicitFollowSymlinks = false;
  624. cmPolicies::PolicyStatus status =
  625. this->Makefile->GetPolicyStatus(cmPolicies::CMP0009);
  626. if(recurse)
  627. {
  628. switch(status)
  629. {
  630. case cmPolicies::NEW:
  631. g.RecurseThroughSymlinksOff();
  632. break;
  633. case cmPolicies::OLD:
  634. case cmPolicies::WARN:
  635. case cmPolicies::REQUIRED_IF_USED:
  636. case cmPolicies::REQUIRED_ALWAYS:
  637. g.RecurseThroughSymlinksOn();
  638. break;
  639. }
  640. }
  641. std::string output = "";
  642. bool first = true;
  643. for ( ; i != args.end(); ++i )
  644. {
  645. if ( recurse && (*i == "FOLLOW_SYMLINKS") )
  646. {
  647. explicitFollowSymlinks = true;
  648. g.RecurseThroughSymlinksOn();
  649. ++i;
  650. if ( i == args.end() )
  651. {
  652. this->SetError(
  653. "GLOB_RECURSE requires a glob expression after FOLLOW_SYMLINKS");
  654. return false;
  655. }
  656. }
  657. if ( *i == "RELATIVE" )
  658. {
  659. ++i; // skip RELATIVE
  660. if ( i == args.end() )
  661. {
  662. this->SetError("GLOB requires a directory after the RELATIVE tag");
  663. return false;
  664. }
  665. g.SetRelative(i->c_str());
  666. ++i;
  667. if(i == args.end())
  668. {
  669. this->SetError("GLOB requires a glob expression after the directory");
  670. return false;
  671. }
  672. }
  673. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  674. {
  675. std::string expr = this->Makefile->GetCurrentDirectory();
  676. // Handle script mode
  677. if ( expr.size() > 0 )
  678. {
  679. expr += "/" + *i;
  680. g.FindFiles(expr);
  681. }
  682. else
  683. {
  684. g.FindFiles(*i);
  685. }
  686. }
  687. else
  688. {
  689. g.FindFiles(*i);
  690. }
  691. std::vector<std::string>::size_type cc;
  692. std::vector<std::string>& files = g.GetFiles();
  693. for ( cc = 0; cc < files.size(); cc ++ )
  694. {
  695. if ( !first )
  696. {
  697. output += ";";
  698. }
  699. output += files[cc];
  700. first = false;
  701. }
  702. }
  703. if(recurse && !explicitFollowSymlinks)
  704. {
  705. switch (status)
  706. {
  707. case cmPolicies::NEW:
  708. // Correct behavior, yay!
  709. break;
  710. case cmPolicies::OLD:
  711. // Probably not really the expected behavior, but the author explicitly
  712. // asked for the old behavior... no warning.
  713. case cmPolicies::WARN:
  714. // Possibly unexpected old behavior *and* we actually traversed
  715. // symlinks without being explicitly asked to: warn the author.
  716. if(g.GetFollowedSymlinkCount() != 0)
  717. {
  718. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING,
  719. this->Makefile->GetPolicies()->
  720. GetPolicyWarning(cmPolicies::CMP0009));
  721. }
  722. break;
  723. case cmPolicies::REQUIRED_IF_USED:
  724. case cmPolicies::REQUIRED_ALWAYS:
  725. this->SetError("policy CMP0009 error");
  726. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  727. this->Makefile->GetPolicies()->
  728. GetRequiredPolicyError(cmPolicies::CMP0009));
  729. return false;
  730. }
  731. }
  732. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  733. return true;
  734. }
  735. //----------------------------------------------------------------------------
  736. bool cmFileCommand::HandleMakeDirectoryCommand(
  737. std::vector<std::string> const& args)
  738. {
  739. if(args.size() < 2 )
  740. {
  741. this->SetError("called with incorrect number of arguments");
  742. return false;
  743. }
  744. std::vector<std::string>::const_iterator i = args.begin();
  745. i++; // Get rid of subcommand
  746. std::string expr;
  747. for ( ; i != args.end(); ++i )
  748. {
  749. const std::string* cdir = &(*i);
  750. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  751. {
  752. expr = this->Makefile->GetCurrentDirectory();
  753. expr += "/" + *i;
  754. cdir = &expr;
  755. }
  756. if ( !this->Makefile->CanIWriteThisFile(cdir->c_str()) )
  757. {
  758. std::string e = "attempted to create a directory: " + *cdir
  759. + " into a source directory.";
  760. this->SetError(e.c_str());
  761. cmSystemTools::SetFatalErrorOccured();
  762. return false;
  763. }
  764. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  765. {
  766. std::string error = "problem creating directory: " + *cdir;
  767. this->SetError(error.c_str());
  768. return false;
  769. }
  770. }
  771. return true;
  772. }
  773. //----------------------------------------------------------------------------
  774. // File installation helper class.
  775. struct cmFileInstaller
  776. {
  777. // Methods to actually install files.
  778. bool InstallFile(const char* fromFile, const char* toFile, bool always);
  779. bool InstallDirectory(const char* source, const char* destination,
  780. bool always);
  781. // All instances need the file command and makefile using them.
  782. cmFileInstaller(cmFileCommand* fc, cmMakefile* mf):
  783. FileCommand(fc), Makefile(mf), DestDirLength(0), MatchlessFiles(true)
  784. {
  785. // Get the current manifest.
  786. this->Manifest =
  787. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  788. }
  789. ~cmFileInstaller()
  790. {
  791. // Save the updated install manifest.
  792. this->Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  793. this->Manifest.c_str());
  794. }
  795. private:
  796. cmFileCommand* FileCommand;
  797. cmMakefile* Makefile;
  798. cmFileTimeComparison FileTimes;
  799. public:
  800. // The length of the destdir setting.
  801. int DestDirLength;
  802. // Whether to install a file not matching any expression.
  803. bool MatchlessFiles;
  804. // The current file manifest (semicolon separated list).
  805. std::string Manifest;
  806. // Permissions for files and directories installed by this object.
  807. mode_t FilePermissions;
  808. mode_t DirPermissions;
  809. // Properties set by pattern and regex match rules.
  810. struct MatchProperties
  811. {
  812. bool Exclude;
  813. mode_t Permissions;
  814. MatchProperties(): Exclude(false), Permissions(0) {}
  815. };
  816. struct MatchRule
  817. {
  818. cmsys::RegularExpression Regex;
  819. MatchProperties Properties;
  820. std::string RegexString;
  821. MatchRule(std::string const& regex):
  822. Regex(regex.c_str()), RegexString(regex) {}
  823. };
  824. std::vector<MatchRule> MatchRules;
  825. // Get the properties from rules matching this input file.
  826. MatchProperties CollectMatchProperties(const char* file,
  827. bool isDirectory)
  828. {
  829. // Match rules are case-insensitive on some platforms.
  830. #if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
  831. std::string lower = cmSystemTools::LowerCase(file);
  832. file = lower.c_str();
  833. #endif
  834. // Collect properties from all matching rules.
  835. bool matched = false;
  836. MatchProperties result;
  837. for(std::vector<MatchRule>::iterator mr = this->MatchRules.begin();
  838. mr != this->MatchRules.end(); ++mr)
  839. {
  840. if(mr->Regex.find(file))
  841. {
  842. matched = true;
  843. result.Exclude |= mr->Properties.Exclude;
  844. result.Permissions |= mr->Properties.Permissions;
  845. }
  846. }
  847. if(!matched && !this->MatchlessFiles && !isDirectory)
  848. {
  849. result.Exclude = true;
  850. }
  851. return result;
  852. }
  853. // Append a file to the installation manifest.
  854. void ManifestAppend(std::string const& file)
  855. {
  856. this->Manifest += ";";
  857. this->Manifest += file.substr(this->DestDirLength);
  858. }
  859. // Translate an argument to a permissions bit.
  860. bool CheckPermissions(std::string const& arg, mode_t& permissions)
  861. {
  862. if(arg == "OWNER_READ") { permissions |= mode_owner_read; }
  863. else if(arg == "OWNER_WRITE") { permissions |= mode_owner_write; }
  864. else if(arg == "OWNER_EXECUTE") { permissions |= mode_owner_execute; }
  865. else if(arg == "GROUP_READ") { permissions |= mode_group_read; }
  866. else if(arg == "GROUP_WRITE") { permissions |= mode_group_write; }
  867. else if(arg == "GROUP_EXECUTE") { permissions |= mode_group_execute; }
  868. else if(arg == "WORLD_READ") { permissions |= mode_world_read; }
  869. else if(arg == "WORLD_WRITE") { permissions |= mode_world_write; }
  870. else if(arg == "WORLD_EXECUTE") { permissions |= mode_world_execute; }
  871. else if(arg == "SETUID") { permissions |= mode_setuid; }
  872. else if(arg == "SETGID") { permissions |= mode_setgid; }
  873. else
  874. {
  875. cmOStringStream e;
  876. e << "INSTALL given invalid permission \"" << arg << "\".";
  877. this->FileCommand->SetError(e.str().c_str());
  878. return false;
  879. }
  880. return true;
  881. }
  882. private:
  883. bool InstallSymlink(const char* fromFile, const char* toFile, bool always);
  884. };
  885. //----------------------------------------------------------------------------
  886. bool cmFileInstaller::InstallSymlink(const char* fromFile, const char* toFile,
  887. bool always)
  888. {
  889. // Read the original symlink.
  890. std::string symlinkTarget;
  891. if(!cmSystemTools::ReadSymlink(fromFile, symlinkTarget))
  892. {
  893. cmOStringStream e;
  894. e << "INSTALL cannot read symlink \"" << fromFile
  895. << "\" to duplicate at \"" << toFile << "\".";
  896. this->FileCommand->SetError(e.str().c_str());
  897. return false;
  898. }
  899. // Compare the symlink value to that at the destination if not
  900. // always installing.
  901. bool copy = true;
  902. if(!always)
  903. {
  904. std::string oldSymlinkTarget;
  905. if(cmSystemTools::ReadSymlink(toFile, oldSymlinkTarget))
  906. {
  907. if(symlinkTarget == oldSymlinkTarget)
  908. {
  909. copy = false;
  910. }
  911. }
  912. }
  913. // Inform the user about this file installation.
  914. std::string message = (copy? "Installing: " : "Up-to-date: ");
  915. message += toFile;
  916. this->Makefile->DisplayStatus(message.c_str(), -1);
  917. if(copy)
  918. {
  919. // Remove the destination file so we can always create the symlink.
  920. cmSystemTools::RemoveFile(toFile);
  921. // Create the symlink.
  922. if(!cmSystemTools::CreateSymlink(symlinkTarget.c_str(), toFile))
  923. {
  924. cmOStringStream e;
  925. e << "INSTALL cannot duplicate symlink \"" << fromFile
  926. << "\" at \"" << toFile << "\".";
  927. this->FileCommand->SetError(e.str().c_str());
  928. return false;
  929. }
  930. }
  931. // Add the file to the manifest.
  932. this->ManifestAppend(toFile);
  933. return true;
  934. }
  935. //----------------------------------------------------------------------------
  936. bool cmFileInstaller::InstallFile(const char* fromFile, const char* toFile,
  937. bool always)
  938. {
  939. // Collect any properties matching this file name.
  940. MatchProperties match_properties =
  941. this->CollectMatchProperties(fromFile, false);
  942. // Skip the file if it is excluded.
  943. if(match_properties.Exclude)
  944. {
  945. return true;
  946. }
  947. // Short-circuit for symbolic links.
  948. if(cmSystemTools::FileIsSymlink(fromFile))
  949. {
  950. return this->InstallSymlink(fromFile, toFile, always);
  951. }
  952. // Determine whether we will copy the file.
  953. bool copy = true;
  954. if(!always)
  955. {
  956. // If both files exist with the same time do not copy.
  957. if(!this->FileTimes.FileTimesDiffer(fromFile, toFile))
  958. {
  959. copy = false;
  960. }
  961. }
  962. // Inform the user about this file installation.
  963. std::string message = (copy? "Installing: " : "Up-to-date: ");
  964. message += toFile;
  965. this->Makefile->DisplayStatus(message.c_str(), -1);
  966. // Copy the file.
  967. if(copy && !cmSystemTools::CopyAFile(fromFile, toFile, true, false))
  968. {
  969. cmOStringStream e;
  970. e << "INSTALL cannot copy file \"" << fromFile
  971. << "\" to \"" << toFile << "\".";
  972. this->FileCommand->SetError(e.str().c_str());
  973. return false;
  974. }
  975. // Add the file to the manifest.
  976. this->ManifestAppend(toFile);
  977. // Set the file modification time of the destination file.
  978. if(copy && !always)
  979. {
  980. if (!cmSystemTools::CopyFileTime(fromFile, toFile))
  981. {
  982. cmOStringStream e;
  983. e << "Problem setting modification time on file \"" << toFile << "\"";
  984. this->FileCommand->SetError(e.str().c_str());
  985. return false;
  986. }
  987. }
  988. // Set permissions of the destination file.
  989. mode_t permissions = (match_properties.Permissions?
  990. match_properties.Permissions : this->FilePermissions);
  991. if(!permissions)
  992. {
  993. // No permissions were explicitly provided but the user requested
  994. // that the source file permissions be used.
  995. cmSystemTools::GetPermissions(fromFile, permissions);
  996. }
  997. if(permissions && !cmSystemTools::SetPermissions(toFile, permissions))
  998. {
  999. cmOStringStream e;
  1000. e << "Problem setting permissions on file \"" << toFile << "\"";
  1001. this->FileCommand->SetError(e.str().c_str());
  1002. return false;
  1003. }
  1004. return true;
  1005. }
  1006. //----------------------------------------------------------------------------
  1007. bool cmFileInstaller::InstallDirectory(const char* source,
  1008. const char* destination,
  1009. bool always)
  1010. {
  1011. // Collect any properties matching this directory name.
  1012. MatchProperties match_properties =
  1013. this->CollectMatchProperties(source, true);
  1014. // Skip the directory if it is excluded.
  1015. if(match_properties.Exclude)
  1016. {
  1017. return true;
  1018. }
  1019. // Short-circuit for symbolic links.
  1020. if(cmSystemTools::FileIsSymlink(source))
  1021. {
  1022. return this->InstallSymlink(source, destination, always);
  1023. }
  1024. // Inform the user about this directory installation.
  1025. std::string message = "Installing: ";
  1026. message += destination;
  1027. this->Makefile->DisplayStatus(message.c_str(), -1);
  1028. // Make sure the destination directory exists.
  1029. if(!cmSystemTools::MakeDirectory(destination))
  1030. {
  1031. return false;
  1032. }
  1033. // Compute the requested permissions for the destination directory.
  1034. mode_t permissions = (match_properties.Permissions?
  1035. match_properties.Permissions : this->DirPermissions);
  1036. if(!permissions)
  1037. {
  1038. // No permissions were explicitly provided but the user requested
  1039. // that the source directory permissions be used.
  1040. cmSystemTools::GetPermissions(source, permissions);
  1041. }
  1042. // Compute the set of permissions required on this directory to
  1043. // recursively install files and subdirectories safely.
  1044. mode_t required_permissions =
  1045. mode_owner_read | mode_owner_write | mode_owner_execute;
  1046. // If the required permissions are specified it is safe to set the
  1047. // final permissions now. Otherwise we must add the required
  1048. // permissions temporarily during file installation.
  1049. mode_t permissions_before = 0;
  1050. mode_t permissions_after = 0;
  1051. if(permissions & required_permissions)
  1052. {
  1053. permissions_before = permissions;
  1054. }
  1055. else
  1056. {
  1057. permissions_before = permissions | required_permissions;
  1058. permissions_after = permissions;
  1059. }
  1060. // Set the required permissions of the destination directory.
  1061. if(permissions_before &&
  1062. !cmSystemTools::SetPermissions(destination, permissions_before))
  1063. {
  1064. cmOStringStream e;
  1065. e << "Problem setting permissions on directory \""
  1066. << destination << "\"";
  1067. this->FileCommand->SetError(e.str().c_str());
  1068. return false;
  1069. }
  1070. // Load the directory contents to traverse it recursively.
  1071. cmsys::Directory dir;
  1072. if(source && *source)
  1073. {
  1074. dir.Load(source);
  1075. }
  1076. unsigned long numFiles = static_cast<unsigned long>(dir.GetNumberOfFiles());
  1077. for(unsigned long fileNum = 0; fileNum < numFiles; ++fileNum)
  1078. {
  1079. if(!(strcmp(dir.GetFile(fileNum), ".") == 0 ||
  1080. strcmp(dir.GetFile(fileNum), "..") == 0))
  1081. {
  1082. cmsys_stl::string fromPath = source;
  1083. fromPath += "/";
  1084. fromPath += dir.GetFile(fileNum);
  1085. if(cmSystemTools::FileIsDirectory(fromPath.c_str()))
  1086. {
  1087. cmsys_stl::string toDir = destination;
  1088. toDir += "/";
  1089. toDir += dir.GetFile(fileNum);
  1090. if(!this->InstallDirectory(fromPath.c_str(), toDir.c_str(), always))
  1091. {
  1092. return false;
  1093. }
  1094. }
  1095. else
  1096. {
  1097. // Install this file.
  1098. std::string toFile = destination;
  1099. toFile += "/";
  1100. toFile += dir.GetFile(fileNum);
  1101. if(!this->InstallFile(fromPath.c_str(), toFile.c_str(), always))
  1102. {
  1103. return false;
  1104. }
  1105. }
  1106. }
  1107. }
  1108. // Set the requested permissions of the destination directory.
  1109. if(permissions_after &&
  1110. !cmSystemTools::SetPermissions(destination, permissions_after))
  1111. {
  1112. cmOStringStream e;
  1113. e << "Problem setting permissions on directory \"" << destination << "\"";
  1114. this->FileCommand->SetError(e.str().c_str());
  1115. return false;
  1116. }
  1117. return true;
  1118. }
  1119. //----------------------------------------------------------------------------
  1120. void cmFileCommand::HandleInstallPermissions(cmFileInstaller& installer,
  1121. mode_t& permissions_file,
  1122. mode_t& permissions_dir,
  1123. int itype,
  1124. bool use_given_permissions_file,
  1125. bool use_given_permissions_dir,
  1126. bool use_source_permissions) const
  1127. {
  1128. // Choose a default for shared library permissions.
  1129. bool install_so_no_exe = this->Makefile->IsOn("CMAKE_INSTALL_SO_NO_EXE");
  1130. // If file permissions were not specified set default permissions
  1131. // for this target type.
  1132. if(!use_given_permissions_file && !use_source_permissions)
  1133. {
  1134. switch(itype)
  1135. {
  1136. case cmTarget::SHARED_LIBRARY:
  1137. case cmTarget::MODULE_LIBRARY:
  1138. if(install_so_no_exe)
  1139. {
  1140. // Use read/write permissions.
  1141. permissions_file = 0;
  1142. permissions_file |= mode_owner_read;
  1143. permissions_file |= mode_owner_write;
  1144. permissions_file |= mode_group_read;
  1145. permissions_file |= mode_world_read;
  1146. break;
  1147. }
  1148. case cmTarget::EXECUTABLE:
  1149. case cmTarget::INSTALL_PROGRAMS:
  1150. // Use read/write/executable permissions.
  1151. permissions_file = 0;
  1152. permissions_file |= mode_owner_read;
  1153. permissions_file |= mode_owner_write;
  1154. permissions_file |= mode_owner_execute;
  1155. permissions_file |= mode_group_read;
  1156. permissions_file |= mode_group_execute;
  1157. permissions_file |= mode_world_read;
  1158. permissions_file |= mode_world_execute;
  1159. break;
  1160. default:
  1161. // Use read/write permissions.
  1162. permissions_file = 0;
  1163. permissions_file |= mode_owner_read;
  1164. permissions_file |= mode_owner_write;
  1165. permissions_file |= mode_group_read;
  1166. permissions_file |= mode_world_read;
  1167. break;
  1168. }
  1169. }
  1170. // If directory permissions were not specified set default permissions.
  1171. if(!use_given_permissions_dir && !use_source_permissions)
  1172. {
  1173. // Use read/write/executable permissions.
  1174. permissions_dir = 0;
  1175. permissions_dir |= mode_owner_read;
  1176. permissions_dir |= mode_owner_write;
  1177. permissions_dir |= mode_owner_execute;
  1178. permissions_dir |= mode_group_read;
  1179. permissions_dir |= mode_group_execute;
  1180. permissions_dir |= mode_world_read;
  1181. permissions_dir |= mode_world_execute;
  1182. }
  1183. // Set the installer permissions.
  1184. installer.FilePermissions = permissions_file;
  1185. installer.DirPermissions = permissions_dir;
  1186. }
  1187. //----------------------------------------------------------------------------
  1188. void cmFileCommand
  1189. ::GetTargetTypeFromString(const std::string& stype, int& itype) const
  1190. {
  1191. if ( stype == "EXECUTABLE" )
  1192. {
  1193. itype = cmTarget::EXECUTABLE;
  1194. }
  1195. else if ( stype == "PROGRAM" )
  1196. {
  1197. itype = cmTarget::INSTALL_PROGRAMS;
  1198. }
  1199. else if ( stype == "STATIC_LIBRARY" )
  1200. {
  1201. itype = cmTarget::STATIC_LIBRARY;
  1202. }
  1203. else if ( stype == "SHARED_LIBRARY" )
  1204. {
  1205. itype = cmTarget::SHARED_LIBRARY;
  1206. }
  1207. else if ( stype == "MODULE" )
  1208. {
  1209. itype = cmTarget::MODULE_LIBRARY;
  1210. }
  1211. else if ( stype == "DIRECTORY" )
  1212. {
  1213. itype = cmTarget::INSTALL_DIRECTORY;
  1214. }
  1215. }
  1216. //----------------------------------------------------------------------------
  1217. bool cmFileCommand::HandleInstallDestination(cmFileInstaller& installer,
  1218. std::string& destination)
  1219. {
  1220. // allow for / to be a valid destination
  1221. if ( destination.size() < 2 && destination != "/" )
  1222. {
  1223. this->SetError("called with inapropriate arguments. "
  1224. "No DESTINATION provided or .");
  1225. return false;
  1226. }
  1227. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  1228. if ( destdir && *destdir )
  1229. {
  1230. std::string sdestdir = destdir;
  1231. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  1232. char ch1 = destination[0];
  1233. char ch2 = destination[1];
  1234. char ch3 = 0;
  1235. if ( destination.size() > 2 )
  1236. {
  1237. ch3 = destination[2];
  1238. }
  1239. int skip = 0;
  1240. if ( ch1 != '/' )
  1241. {
  1242. int relative = 0;
  1243. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  1244. ch2 == ':' )
  1245. {
  1246. // Assume windows
  1247. // let's do some destdir magic:
  1248. skip = 2;
  1249. if ( ch3 != '/' )
  1250. {
  1251. relative = 1;
  1252. }
  1253. }
  1254. else
  1255. {
  1256. relative = 1;
  1257. }
  1258. if ( relative )
  1259. {
  1260. // This is relative path on unix or windows. Since we are doing
  1261. // destdir, this case does not make sense.
  1262. this->SetError("called with relative DESTINATION. This "
  1263. "does not make sense when using DESTDIR. Specify "
  1264. "absolute path or remove DESTDIR environment variable.");
  1265. return false;
  1266. }
  1267. }
  1268. else
  1269. {
  1270. if ( ch2 == '/' )
  1271. {
  1272. // looks like a network path.
  1273. std::string message = "called with network path DESTINATION. This "
  1274. "does not make sense when using DESTDIR. Specify local "
  1275. "absolute path or remove DESTDIR environment variable."
  1276. "\nDESTINATION=\n";
  1277. message += destination;
  1278. this->SetError(message.c_str());
  1279. return false;
  1280. }
  1281. }
  1282. destination = sdestdir + (destination.c_str() + skip);
  1283. installer.DestDirLength = int(sdestdir.size());
  1284. }
  1285. if ( !cmSystemTools::FileExists(destination.c_str()) )
  1286. {
  1287. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  1288. {
  1289. std::string errstring = "cannot create directory: " + destination +
  1290. ". Maybe need administrative privileges.";
  1291. this->SetError(errstring.c_str());
  1292. return false;
  1293. }
  1294. }
  1295. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  1296. {
  1297. std::string errstring = "INSTALL destination: " + destination +
  1298. " is not a directory.";
  1299. this->SetError(errstring.c_str());
  1300. return false;
  1301. }
  1302. return true;
  1303. }
  1304. //----------------------------------------------------------------------------
  1305. bool
  1306. cmFileCommand::HandleRPathChangeCommand(std::vector<std::string> const& args)
  1307. {
  1308. // Evaluate arguments.
  1309. const char* file = 0;
  1310. const char* oldRPath = 0;
  1311. const char* newRPath = 0;
  1312. enum Doing { DoingNone, DoingFile, DoingOld, DoingNew };
  1313. Doing doing = DoingNone;
  1314. for(unsigned int i=1; i < args.size(); ++i)
  1315. {
  1316. if(args[i] == "OLD_RPATH")
  1317. {
  1318. doing = DoingOld;
  1319. }
  1320. else if(args[i] == "NEW_RPATH")
  1321. {
  1322. doing = DoingNew;
  1323. }
  1324. else if(args[i] == "FILE")
  1325. {
  1326. doing = DoingFile;
  1327. }
  1328. else if(doing == DoingFile)
  1329. {
  1330. file = args[i].c_str();
  1331. doing = DoingNone;
  1332. }
  1333. else if(doing == DoingOld)
  1334. {
  1335. oldRPath = args[i].c_str();
  1336. doing = DoingNone;
  1337. }
  1338. else if(doing == DoingNew)
  1339. {
  1340. newRPath = args[i].c_str();
  1341. doing = DoingNone;
  1342. }
  1343. else
  1344. {
  1345. cmOStringStream e;
  1346. e << "RPATH_CHANGE given unknown argument " << args[i];
  1347. this->SetError(e.str().c_str());
  1348. return false;
  1349. }
  1350. }
  1351. if(!file)
  1352. {
  1353. this->SetError("RPATH_CHANGE not given FILE option.");
  1354. return false;
  1355. }
  1356. if(!oldRPath)
  1357. {
  1358. this->SetError("RPATH_CHANGE not given OLD_RPATH option.");
  1359. return false;
  1360. }
  1361. if(!newRPath)
  1362. {
  1363. this->SetError("RPATH_CHANGE not given NEW_RPATH option.");
  1364. return false;
  1365. }
  1366. if(!cmSystemTools::FileExists(file, true))
  1367. {
  1368. cmOStringStream e;
  1369. e << "RPATH_CHANGE given FILE \"" << file << "\" that does not exist.";
  1370. this->SetError(e.str().c_str());
  1371. return false;
  1372. }
  1373. bool success = true;
  1374. cmSystemToolsFileTime* ft = cmSystemTools::FileTimeNew();
  1375. bool have_ft = cmSystemTools::FileTimeGet(file, ft);
  1376. std::string emsg;
  1377. bool changed;
  1378. if(!cmSystemTools::ChangeRPath(file, oldRPath, newRPath, &emsg, &changed))
  1379. {
  1380. cmOStringStream e;
  1381. e << "RPATH_CHANGE could not write new RPATH:\n"
  1382. << " " << newRPath << "\n"
  1383. << "to the file:\n"
  1384. << " " << file << "\n"
  1385. << emsg;
  1386. this->SetError(e.str().c_str());
  1387. success = false;
  1388. }
  1389. if(success)
  1390. {
  1391. if(changed)
  1392. {
  1393. std::string message = "Set runtime path of \"";
  1394. message += file;
  1395. message += "\" to \"";
  1396. message += newRPath;
  1397. message += "\"";
  1398. this->Makefile->DisplayStatus(message.c_str(), -1);
  1399. }
  1400. if(have_ft)
  1401. {
  1402. cmSystemTools::FileTimeSet(file, ft);
  1403. }
  1404. }
  1405. cmSystemTools::FileTimeDelete(ft);
  1406. return success;
  1407. }
  1408. //----------------------------------------------------------------------------
  1409. bool
  1410. cmFileCommand::HandleRPathRemoveCommand(std::vector<std::string> const& args)
  1411. {
  1412. // Evaluate arguments.
  1413. const char* file = 0;
  1414. enum Doing { DoingNone, DoingFile };
  1415. Doing doing = DoingNone;
  1416. for(unsigned int i=1; i < args.size(); ++i)
  1417. {
  1418. if(args[i] == "FILE")
  1419. {
  1420. doing = DoingFile;
  1421. }
  1422. else if(doing == DoingFile)
  1423. {
  1424. file = args[i].c_str();
  1425. doing = DoingNone;
  1426. }
  1427. else
  1428. {
  1429. cmOStringStream e;
  1430. e << "RPATH_REMOVE given unknown argument " << args[i];
  1431. this->SetError(e.str().c_str());
  1432. return false;
  1433. }
  1434. }
  1435. if(!file)
  1436. {
  1437. this->SetError("RPATH_REMOVE not given FILE option.");
  1438. return false;
  1439. }
  1440. if(!cmSystemTools::FileExists(file, true))
  1441. {
  1442. cmOStringStream e;
  1443. e << "RPATH_REMOVE given FILE \"" << file << "\" that does not exist.";
  1444. this->SetError(e.str().c_str());
  1445. return false;
  1446. }
  1447. bool success = true;
  1448. cmSystemToolsFileTime* ft = cmSystemTools::FileTimeNew();
  1449. bool have_ft = cmSystemTools::FileTimeGet(file, ft);
  1450. std::string emsg;
  1451. bool removed;
  1452. if(!cmSystemTools::RemoveRPath(file, &emsg, &removed))
  1453. {
  1454. cmOStringStream e;
  1455. e << "RPATH_REMOVE could not remove RPATH from file:\n"
  1456. << " " << file << "\n"
  1457. << emsg;
  1458. this->SetError(e.str().c_str());
  1459. success = false;
  1460. }
  1461. if(success)
  1462. {
  1463. if(removed)
  1464. {
  1465. std::string message = "Removed runtime path from \"";
  1466. message += file;
  1467. message += "\"";
  1468. this->Makefile->DisplayStatus(message.c_str(), -1);
  1469. }
  1470. if(have_ft)
  1471. {
  1472. cmSystemTools::FileTimeSet(file, ft);
  1473. }
  1474. }
  1475. cmSystemTools::FileTimeDelete(ft);
  1476. return success;
  1477. }
  1478. //----------------------------------------------------------------------------
  1479. bool
  1480. cmFileCommand::HandleRPathCheckCommand(std::vector<std::string> const& args)
  1481. {
  1482. // Evaluate arguments.
  1483. const char* file = 0;
  1484. const char* rpath = 0;
  1485. enum Doing { DoingNone, DoingFile, DoingRPath };
  1486. Doing doing = DoingNone;
  1487. for(unsigned int i=1; i < args.size(); ++i)
  1488. {
  1489. if(args[i] == "RPATH")
  1490. {
  1491. doing = DoingRPath;
  1492. }
  1493. else if(args[i] == "FILE")
  1494. {
  1495. doing = DoingFile;
  1496. }
  1497. else if(doing == DoingFile)
  1498. {
  1499. file = args[i].c_str();
  1500. doing = DoingNone;
  1501. }
  1502. else if(doing == DoingRPath)
  1503. {
  1504. rpath = args[i].c_str();
  1505. doing = DoingNone;
  1506. }
  1507. else
  1508. {
  1509. cmOStringStream e;
  1510. e << "RPATH_CHECK given unknown argument " << args[i];
  1511. this->SetError(e.str().c_str());
  1512. return false;
  1513. }
  1514. }
  1515. if(!file)
  1516. {
  1517. this->SetError("RPATH_CHECK not given FILE option.");
  1518. return false;
  1519. }
  1520. if(!rpath)
  1521. {
  1522. this->SetError("RPATH_CHECK not given RPATH option.");
  1523. return false;
  1524. }
  1525. // If the file exists but does not have the desired RPath then
  1526. // delete it. This is used during installation to re-install a file
  1527. // if its RPath will change.
  1528. if(cmSystemTools::FileExists(file, true) &&
  1529. !cmSystemTools::CheckRPath(file, rpath))
  1530. {
  1531. cmSystemTools::RemoveFile(file);
  1532. }
  1533. return true;
  1534. }
  1535. //----------------------------------------------------------------------------
  1536. bool cmFileCommand::HandleInstallCommand(std::vector<std::string> const& args)
  1537. {
  1538. if ( args.size() < 6 )
  1539. {
  1540. this->SetError("called with incorrect number of arguments");
  1541. return false;
  1542. }
  1543. // Construct a file installer object.
  1544. cmFileInstaller installer(this, this->Makefile);
  1545. std::string rename = "";
  1546. std::string destination = "";
  1547. std::vector<std::string> files;
  1548. int itype = cmTarget::INSTALL_FILES;
  1549. std::map<cmStdString, const char*> properties;
  1550. bool optional = false;
  1551. bool result = this->ParseInstallArgs(args, installer, properties,
  1552. itype, rename, destination, files,
  1553. optional);
  1554. if (result == true)
  1555. {
  1556. result = this->DoInstall(installer,
  1557. itype, rename, destination, files, optional);
  1558. }
  1559. return result;
  1560. }
  1561. //----------------------------------------------------------------------------
  1562. bool cmFileCommand::ParseInstallArgs(std::vector<std::string> const& args,
  1563. cmFileInstaller& installer,
  1564. std::map<cmStdString, const char*>& properties,
  1565. int& itype,
  1566. std::string& rename,
  1567. std::string& destination,
  1568. std::vector<std::string>& files,
  1569. bool& optional)
  1570. {
  1571. std::string stype = "FILES";
  1572. bool doing_files = false;
  1573. bool doing_properties = false;
  1574. bool doing_permissions_file = false;
  1575. bool doing_permissions_dir = false;
  1576. bool doing_permissions_match = false;
  1577. bool use_given_permissions_file = false;
  1578. bool use_given_permissions_dir = false;
  1579. bool use_source_permissions = false;
  1580. mode_t permissions_file = 0;
  1581. mode_t permissions_dir = 0;
  1582. cmFileInstaller::MatchRule* current_match_rule = 0;
  1583. std::vector<std::string>::size_type i = 0;
  1584. i++; // Get rid of subcommand
  1585. for ( ; i != args.size(); ++i )
  1586. {
  1587. const std::string* cstr = &args[i];
  1588. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  1589. {
  1590. if(current_match_rule)
  1591. {
  1592. cmOStringStream e;
  1593. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1594. this->SetError(e.str().c_str());
  1595. return false;
  1596. }
  1597. i++;
  1598. destination = args[i];
  1599. doing_files = false;
  1600. doing_properties = false;
  1601. doing_permissions_file = false;
  1602. doing_permissions_dir = false;
  1603. }
  1604. else if ( *cstr == "TYPE" && i < args.size()-1 )
  1605. {
  1606. if(current_match_rule)
  1607. {
  1608. cmOStringStream e;
  1609. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1610. this->SetError(e.str().c_str());
  1611. return false;
  1612. }
  1613. i++;
  1614. stype = args[i];
  1615. if ( args[i+1] == "OPTIONAL" )
  1616. {
  1617. i++;
  1618. optional = true;
  1619. }
  1620. doing_properties = false;
  1621. doing_files = false;
  1622. doing_permissions_file = false;
  1623. doing_permissions_dir = false;
  1624. }
  1625. else if ( *cstr == "RENAME" && i < args.size()-1 )
  1626. {
  1627. if(current_match_rule)
  1628. {
  1629. cmOStringStream e;
  1630. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1631. this->SetError(e.str().c_str());
  1632. return false;
  1633. }
  1634. i++;
  1635. rename = args[i];
  1636. doing_properties = false;
  1637. doing_files = false;
  1638. doing_permissions_file = false;
  1639. doing_permissions_dir = false;
  1640. }
  1641. else if ( *cstr == "REGEX" && i < args.size()-1 )
  1642. {
  1643. i++;
  1644. installer.MatchRules.push_back(cmFileInstaller::MatchRule(args[i]));
  1645. current_match_rule = &*(installer.MatchRules.end()-1);
  1646. if(!current_match_rule->Regex.is_valid())
  1647. {
  1648. cmOStringStream e;
  1649. e << "INSTALL could not compile REGEX \"" << args[i] << "\".";
  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. }
  1658. else if ( *cstr == "EXCLUDE" )
  1659. {
  1660. // Add this property to the current match rule.
  1661. if(!current_match_rule)
  1662. {
  1663. cmOStringStream e;
  1664. e << "INSTALL does not allow \""
  1665. << *cstr << "\" before a REGEX is given.";
  1666. this->SetError(e.str().c_str());
  1667. return false;
  1668. }
  1669. current_match_rule->Properties.Exclude = true;
  1670. doing_permissions_match = true;
  1671. }
  1672. else if ( *cstr == "PROPERTIES" )
  1673. {
  1674. if(current_match_rule)
  1675. {
  1676. cmOStringStream e;
  1677. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1678. this->SetError(e.str().c_str());
  1679. return false;
  1680. }
  1681. doing_properties = true;
  1682. doing_files = false;
  1683. doing_permissions_file = false;
  1684. doing_permissions_dir = false;
  1685. }
  1686. else if ( *cstr == "PERMISSIONS" )
  1687. {
  1688. if(current_match_rule)
  1689. {
  1690. doing_permissions_match = true;
  1691. doing_permissions_file = false;
  1692. }
  1693. else
  1694. {
  1695. doing_permissions_match = false;
  1696. doing_permissions_file = true;
  1697. use_given_permissions_file = true;
  1698. }
  1699. doing_properties = false;
  1700. doing_files = false;
  1701. doing_permissions_dir = false;
  1702. }
  1703. else if ( *cstr == "DIR_PERMISSIONS" )
  1704. {
  1705. if(current_match_rule)
  1706. {
  1707. cmOStringStream e;
  1708. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1709. this->SetError(e.str().c_str());
  1710. return false;
  1711. }
  1712. use_given_permissions_dir = true;
  1713. doing_properties = false;
  1714. doing_files = false;
  1715. doing_permissions_file = false;
  1716. doing_permissions_dir = true;
  1717. }
  1718. else if ( *cstr == "USE_SOURCE_PERMISSIONS" )
  1719. {
  1720. if(current_match_rule)
  1721. {
  1722. cmOStringStream e;
  1723. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1724. this->SetError(e.str().c_str());
  1725. return false;
  1726. }
  1727. doing_properties = false;
  1728. doing_files = false;
  1729. doing_permissions_file = false;
  1730. doing_permissions_dir = false;
  1731. use_source_permissions = true;
  1732. }
  1733. else if ( *cstr == "FILES_MATCHING" )
  1734. {
  1735. if(current_match_rule)
  1736. {
  1737. cmOStringStream e;
  1738. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1739. this->SetError(e.str().c_str());
  1740. return false;
  1741. }
  1742. doing_properties = false;
  1743. doing_files = false;
  1744. doing_permissions_file = false;
  1745. doing_permissions_dir = false;
  1746. installer.MatchlessFiles = false;
  1747. }
  1748. else if ( *cstr == "COMPONENTS" )
  1749. {
  1750. cmOStringStream e;
  1751. e << "INSTALL called with old-style COMPONENTS argument. "
  1752. << "This script was generated with an older version of CMake. "
  1753. << "Re-run this cmake version on your build tree.";
  1754. this->SetError(e.str().c_str());
  1755. return false;
  1756. }
  1757. else if ( *cstr == "CONFIGURATIONS" )
  1758. {
  1759. cmOStringStream e;
  1760. e << "INSTALL called with old-style CONFIGURATIONS argument. "
  1761. << "This script was generated with an older version of CMake. "
  1762. << "Re-run this cmake version on your build tree.";
  1763. this->SetError(e.str().c_str());
  1764. return false;
  1765. }
  1766. else if ( *cstr == "FILES" && !doing_files)
  1767. {
  1768. if(current_match_rule)
  1769. {
  1770. cmOStringStream e;
  1771. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1772. this->SetError(e.str().c_str());
  1773. return false;
  1774. }
  1775. doing_files = true;
  1776. doing_properties = false;
  1777. doing_permissions_file = false;
  1778. doing_permissions_dir = false;
  1779. }
  1780. else if ( doing_properties && i < args.size()-1 )
  1781. {
  1782. properties[args[i]] = args[i+1].c_str();
  1783. i++;
  1784. }
  1785. else if ( doing_files )
  1786. {
  1787. files.push_back(*cstr);
  1788. }
  1789. else if(doing_permissions_file)
  1790. {
  1791. if(!installer.CheckPermissions(args[i], permissions_file))
  1792. {
  1793. return false;
  1794. }
  1795. }
  1796. else if(doing_permissions_dir)
  1797. {
  1798. if(!installer.CheckPermissions(args[i], permissions_dir))
  1799. {
  1800. return false;
  1801. }
  1802. }
  1803. else if(doing_permissions_match)
  1804. {
  1805. if(!installer.CheckPermissions(
  1806. args[i], current_match_rule->Properties.Permissions))
  1807. {
  1808. return false;
  1809. }
  1810. }
  1811. else
  1812. {
  1813. this->SetError("called with inappropriate arguments");
  1814. return false;
  1815. }
  1816. }
  1817. // now check and postprocess what has been parsed
  1818. if ( files.size() == 0 )
  1819. {
  1820. // nothing to do, no files were listed.
  1821. // if this is handled as error, INSTALL_FILES() creates an invalid
  1822. // cmake_install.cmake script with no FILES() arguments if no files were
  1823. // given to INSTALL_FILES(). This was accepted with CMake 2.4.x.
  1824. return true;
  1825. }
  1826. // Check rename form.
  1827. if(!rename.empty())
  1828. {
  1829. if(itype != cmTarget::INSTALL_FILES &&
  1830. itype != cmTarget::INSTALL_PROGRAMS)
  1831. {
  1832. this->SetError("INSTALL option RENAME may be used only with "
  1833. "FILES or PROGRAMS.");
  1834. return false;
  1835. }
  1836. if(files.size() > 1)
  1837. {
  1838. this->SetError("INSTALL option RENAME may be used only with "
  1839. "one file.");
  1840. return false;
  1841. }
  1842. }
  1843. if (this->HandleInstallDestination(installer, destination) == false)
  1844. {
  1845. return false;
  1846. }
  1847. if(properties.find("VERSION") != properties.end())
  1848. {
  1849. cmOStringStream e;
  1850. e << "INSTALL called with old-style VERSION property. "
  1851. << "This script was generated with an older version of CMake. "
  1852. << "Re-run this cmake version on your build tree.";
  1853. this->SetError(e.str().c_str());
  1854. return false;
  1855. }
  1856. if(properties.find("SOVERSION") != properties.end())
  1857. {
  1858. cmOStringStream e;
  1859. e << "INSTALL called with old-style SOVERSION property. "
  1860. << "This script was generated with an older version of CMake. "
  1861. << "Re-run this cmake version on your build tree.";
  1862. this->SetError(e.str().c_str());
  1863. return false;
  1864. }
  1865. this->GetTargetTypeFromString(stype, itype);
  1866. this->HandleInstallPermissions(installer,
  1867. permissions_file,
  1868. permissions_dir,
  1869. itype,
  1870. use_given_permissions_file,
  1871. use_given_permissions_dir,
  1872. use_source_permissions);
  1873. return true;
  1874. }
  1875. //----------------------------------------------------------------------------
  1876. bool cmFileCommand::DoInstall( cmFileInstaller& installer,
  1877. const int itype,
  1878. const std::string& rename,
  1879. const std::string& destination,
  1880. const std::vector<std::string>& files,
  1881. const bool optional)
  1882. {
  1883. typedef std::set<cmStdString>::const_iterator iter_type;
  1884. // Check whether files should be copied always or only if they have
  1885. // changed.
  1886. bool copy_always =
  1887. cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_INSTALL_ALWAYS"));
  1888. // Handle each file listed.
  1889. for (std::vector<std::string>::size_type i = 0; i < files.size(); i ++ )
  1890. {
  1891. // Split the input file into its directory and name components.
  1892. std::vector<std::string> fromPathComponents;
  1893. cmSystemTools::SplitPath(files[i].c_str(), fromPathComponents);
  1894. std::string fromName = *(fromPathComponents.end()-1);
  1895. std::string fromDir = cmSystemTools::JoinPath(fromPathComponents.begin(),
  1896. fromPathComponents.end()-1);
  1897. // Compute the full path to the destination file.
  1898. std::string toFile = destination;
  1899. std::string const& toName = rename.empty()? fromName : rename;
  1900. if(!toName.empty())
  1901. {
  1902. toFile += "/";
  1903. toFile += toName;
  1904. }
  1905. // Construct the full path to the source file. The file name may
  1906. // have been changed above.
  1907. std::string fromFile = fromDir;
  1908. if(!fromName.empty())
  1909. {
  1910. fromFile += "/";
  1911. fromFile += fromName;
  1912. }
  1913. std::string message;
  1914. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  1915. {
  1916. if(itype == cmTarget::INSTALL_DIRECTORY &&
  1917. (fromFile.empty() ||
  1918. cmSystemTools::FileIsDirectory(fromFile.c_str())))
  1919. {
  1920. // Try installing this directory.
  1921. if(!installer.InstallDirectory(fromFile.c_str(), toFile.c_str(),
  1922. copy_always))
  1923. {
  1924. return false;
  1925. }
  1926. }
  1927. else if(cmSystemTools::FileExists(fromFile.c_str()))
  1928. {
  1929. // Install this file.
  1930. if(!installer.InstallFile(fromFile.c_str(), toFile.c_str(),
  1931. copy_always))
  1932. {
  1933. return false;
  1934. }
  1935. }
  1936. else if(!optional)
  1937. {
  1938. // The input file does not exist and installation is not optional.
  1939. cmOStringStream e;
  1940. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  1941. this->SetError(e.str().c_str());
  1942. return false;
  1943. }
  1944. }
  1945. }
  1946. return true;
  1947. }
  1948. //----------------------------------------------------------------------------
  1949. bool cmFileCommand::HandleRelativePathCommand(
  1950. std::vector<std::string> const& args)
  1951. {
  1952. if(args.size() != 4 )
  1953. {
  1954. this->SetError("called with incorrect number of arguments");
  1955. return false;
  1956. }
  1957. const std::string& outVar = args[1];
  1958. const std::string& directoryName = args[2];
  1959. const std::string& fileName = args[3];
  1960. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  1961. {
  1962. std::string errstring =
  1963. "RelativePath must be passed a full path to the directory: "
  1964. + directoryName;
  1965. this->SetError(errstring.c_str());
  1966. return false;
  1967. }
  1968. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  1969. {
  1970. std::string errstring =
  1971. "RelativePath must be passed a full path to the file: "
  1972. + fileName;
  1973. this->SetError(errstring.c_str());
  1974. return false;
  1975. }
  1976. std::string res = cmSystemTools::RelativePath(directoryName.c_str(),
  1977. fileName.c_str());
  1978. this->Makefile->AddDefinition(outVar.c_str(),
  1979. res.c_str());
  1980. return true;
  1981. }
  1982. //----------------------------------------------------------------------------
  1983. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  1984. bool recurse)
  1985. {
  1986. std::string message;
  1987. std::vector<std::string>::const_iterator i = args.begin();
  1988. i++; // Get rid of subcommand
  1989. for(;i != args.end(); ++i)
  1990. {
  1991. if(cmSystemTools::FileIsDirectory(i->c_str()) && recurse)
  1992. {
  1993. cmSystemTools::RemoveADirectory(i->c_str());
  1994. }
  1995. else
  1996. {
  1997. cmSystemTools::RemoveFile(i->c_str());
  1998. }
  1999. }
  2000. return true;
  2001. }
  2002. //----------------------------------------------------------------------------
  2003. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  2004. const& args,
  2005. bool nativePath)
  2006. {
  2007. std::vector<std::string>::const_iterator i = args.begin();
  2008. if(args.size() != 3)
  2009. {
  2010. this->SetError("FILE(SYSTEM_PATH ENV result) must be called with "
  2011. "only three arguments.");
  2012. return false;
  2013. }
  2014. i++; // Get rid of subcommand
  2015. #if defined(_WIN32) && !defined(__CYGWIN__)
  2016. char pathSep = ';';
  2017. #else
  2018. char pathSep = ':';
  2019. #endif
  2020. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  2021. pathSep);
  2022. i++;
  2023. const char* var = i->c_str();
  2024. std::string value;
  2025. for(std::vector<cmsys::String>::iterator j = path.begin();
  2026. j != path.end(); ++j)
  2027. {
  2028. if(j != path.begin())
  2029. {
  2030. value += ";";
  2031. }
  2032. if(!nativePath)
  2033. {
  2034. cmSystemTools::ConvertToUnixSlashes(*j);
  2035. }
  2036. else
  2037. {
  2038. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  2039. // remove double quotes in the path
  2040. cmsys::String& s = *j;
  2041. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  2042. {
  2043. s = s.substr(1,s.size()-2);
  2044. }
  2045. }
  2046. value += *j;
  2047. }
  2048. this->Makefile->AddDefinition(var, value.c_str());
  2049. return true;
  2050. }
  2051. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2052. // Stuff for curl download
  2053. typedef std::vector<char> cmFileCommandVectorOfChar;
  2054. namespace{
  2055. size_t
  2056. cmFileCommandWriteMemoryCallback(void *ptr, size_t size, size_t nmemb,
  2057. void *data)
  2058. {
  2059. register int realsize = (int)(size * nmemb);
  2060. std::ofstream* fout = static_cast<std::ofstream*>(data);
  2061. const char* chPtr = static_cast<char*>(ptr);
  2062. fout->write(chPtr, realsize);
  2063. return realsize;
  2064. }
  2065. static size_t
  2066. cmFileCommandCurlDebugCallback(CURL *, curl_infotype, char *chPtr,
  2067. size_t size, void *data)
  2068. {
  2069. cmFileCommandVectorOfChar *vec
  2070. = static_cast<cmFileCommandVectorOfChar*>(data);
  2071. vec->insert(vec->end(), chPtr, chPtr + size);
  2072. return size;
  2073. }
  2074. }
  2075. #endif
  2076. bool
  2077. cmFileCommand::HandleDownloadCommand(std::vector<std::string>
  2078. const& args)
  2079. {
  2080. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2081. std::vector<std::string>::const_iterator i = args.begin();
  2082. if(args.size() < 3)
  2083. {
  2084. this->SetError("FILE(DOWNLOAD url file) must be called with "
  2085. "at least three arguments.");
  2086. return false;
  2087. }
  2088. i++; // Get rid of subcommand
  2089. std::string url = *i;
  2090. i++;
  2091. std::string file = *i;
  2092. i++;
  2093. double timeout = 0;
  2094. std::string verboseLog;
  2095. std::string statusVar;
  2096. while(i != args.end())
  2097. {
  2098. if(*i == "TIMEOUT")
  2099. {
  2100. i++;
  2101. if(i != args.end())
  2102. {
  2103. timeout = atof(i->c_str());
  2104. }
  2105. else
  2106. {
  2107. this->SetError("FILE(DOWNLOAD url file TIMEOUT time) missing "
  2108. "time for TIMEOUT.");
  2109. return false;
  2110. }
  2111. }
  2112. else if(*i == "LOG")
  2113. {
  2114. i++;
  2115. if( i == args.end())
  2116. {
  2117. this->SetError("FILE(DOWNLOAD url file LOG VAR) missing "
  2118. "VAR for LOG.");
  2119. return false;
  2120. }
  2121. verboseLog = *i;
  2122. }
  2123. else if(*i == "STATUS")
  2124. {
  2125. i++;
  2126. if( i == args.end())
  2127. {
  2128. this->SetError("FILE(DOWNLOAD url file STATUS VAR) missing "
  2129. "VAR for STATUS.");
  2130. return false;
  2131. }
  2132. statusVar = *i;
  2133. }
  2134. i++;
  2135. }
  2136. std::string dir = cmSystemTools::GetFilenamePath(file.c_str());
  2137. if(!cmSystemTools::FileExists(dir.c_str()) &&
  2138. !cmSystemTools::MakeDirectory(dir.c_str()))
  2139. {
  2140. std::string errstring = "FILE(DOWNLOAD ) error; cannot create directory: "
  2141. + dir + ". Maybe need administrative privileges.";
  2142. this->SetError(errstring.c_str());
  2143. return false;
  2144. }
  2145. std::ofstream fout(file.c_str(), std::ios::binary);
  2146. if(!fout)
  2147. {
  2148. this->SetError("FILE(DOWNLOAD url file TIMEOUT time) can not open "
  2149. "file for write.");
  2150. return false;
  2151. }
  2152. CURL *curl;
  2153. curl_global_init(CURL_GLOBAL_DEFAULT);
  2154. curl = curl_easy_init();
  2155. if(!curl)
  2156. {
  2157. this->SetError("FILE(DOWNLOAD ) error "
  2158. "initializing curl.");
  2159. return false;
  2160. }
  2161. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  2162. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  2163. cmFileCommandWriteMemoryCallback);
  2164. curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  2165. cmFileCommandCurlDebugCallback);
  2166. cmFileCommandVectorOfChar chunkDebug;
  2167. ::curl_easy_setopt(curl, CURLOPT_FILE, (void *)&fout);
  2168. ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void *)&chunkDebug);
  2169. if(verboseLog.size())
  2170. {
  2171. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  2172. }
  2173. if(timeout > 0)
  2174. {
  2175. curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout );
  2176. }
  2177. CURLcode res = curl_easy_perform(curl);
  2178. /* always cleanup */
  2179. curl_easy_cleanup(curl);
  2180. if(statusVar.size())
  2181. {
  2182. cmOStringStream result;
  2183. result << (int)res << ";\"" << curl_easy_strerror(res) << "\"";
  2184. this->Makefile->AddDefinition(statusVar.c_str(),
  2185. result.str().c_str());
  2186. }
  2187. curl_global_cleanup();
  2188. if(chunkDebug.size())
  2189. {
  2190. chunkDebug.push_back(0);
  2191. if(CURLE_OPERATION_TIMEOUTED == res)
  2192. {
  2193. std::string output = &*chunkDebug.begin();
  2194. if(verboseLog.size())
  2195. {
  2196. this->Makefile->AddDefinition(verboseLog.c_str(),
  2197. &*chunkDebug.begin());
  2198. }
  2199. }
  2200. this->Makefile->AddDefinition(verboseLog.c_str(),
  2201. &*chunkDebug.begin());
  2202. }
  2203. return true;
  2204. #else
  2205. this->SetError("FILE(DOWNLOAD ) "
  2206. "not supported in bootstrap cmake ");
  2207. return false;
  2208. #endif
  2209. }