cmFileCommand.cxx 68 KB

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