cmFileCommand.cxx 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmFileCommand.h"
  11. #include "cmCryptoHash.h"
  12. #include "cmake.h"
  13. #include "cmHexFileConverter.h"
  14. #include "cmInstallType.h"
  15. #include "cmFileTimeComparison.h"
  16. #include "cmCryptoHash.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/auto_ptr.hxx>
  24. #include <cmsys/Directory.hxx>
  25. #include <cmsys/Glob.hxx>
  26. #include <cmsys/RegularExpression.hxx>
  27. // Table of permissions flags.
  28. #if defined(_WIN32) && !defined(__CYGWIN__)
  29. static mode_t mode_owner_read = S_IREAD;
  30. static mode_t mode_owner_write = S_IWRITE;
  31. static mode_t mode_owner_execute = S_IEXEC;
  32. static mode_t mode_group_read = 0;
  33. static mode_t mode_group_write = 0;
  34. static mode_t mode_group_execute = 0;
  35. static mode_t mode_world_read = 0;
  36. static mode_t mode_world_write = 0;
  37. static mode_t mode_world_execute = 0;
  38. static mode_t mode_setuid = 0;
  39. static mode_t mode_setgid = 0;
  40. #else
  41. static mode_t mode_owner_read = S_IRUSR;
  42. static mode_t mode_owner_write = S_IWUSR;
  43. static mode_t mode_owner_execute = S_IXUSR;
  44. static mode_t mode_group_read = S_IRGRP;
  45. static mode_t mode_group_write = S_IWGRP;
  46. static mode_t mode_group_execute = S_IXGRP;
  47. static mode_t mode_world_read = S_IROTH;
  48. static mode_t mode_world_write = S_IWOTH;
  49. static mode_t mode_world_execute = S_IXOTH;
  50. static mode_t mode_setuid = S_ISUID;
  51. static mode_t mode_setgid = S_ISGID;
  52. #endif
  53. // cmLibraryCommand
  54. bool cmFileCommand
  55. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  56. {
  57. if(args.size() < 2 )
  58. {
  59. this->SetError("must be called with at least two arguments.");
  60. return false;
  61. }
  62. std::string subCommand = args[0];
  63. if ( subCommand == "WRITE" )
  64. {
  65. return this->HandleWriteCommand(args, false);
  66. }
  67. else if ( subCommand == "APPEND" )
  68. {
  69. return this->HandleWriteCommand(args, true);
  70. }
  71. else if ( subCommand == "DOWNLOAD" )
  72. {
  73. return this->HandleDownloadCommand(args);
  74. }
  75. else if ( subCommand == "UPLOAD" )
  76. {
  77. return this->HandleUploadCommand(args);
  78. }
  79. else if ( subCommand == "READ" )
  80. {
  81. return this->HandleReadCommand(args);
  82. }
  83. else if ( subCommand == "MD5" ||
  84. subCommand == "SHA1" ||
  85. subCommand == "SHA224" ||
  86. subCommand == "SHA256" ||
  87. subCommand == "SHA384" ||
  88. subCommand == "SHA512" )
  89. {
  90. return this->HandleHashCommand(args);
  91. }
  92. else if ( subCommand == "STRINGS" )
  93. {
  94. return this->HandleStringsCommand(args);
  95. }
  96. else if ( subCommand == "GLOB" )
  97. {
  98. return this->HandleGlobCommand(args, false);
  99. }
  100. else if ( subCommand == "GLOB_RECURSE" )
  101. {
  102. return this->HandleGlobCommand(args, true);
  103. }
  104. else if ( subCommand == "MAKE_DIRECTORY" )
  105. {
  106. return this->HandleMakeDirectoryCommand(args);
  107. }
  108. else if ( subCommand == "RENAME" )
  109. {
  110. return this->HandleRename(args);
  111. }
  112. else if ( subCommand == "REMOVE" )
  113. {
  114. return this->HandleRemove(args, false);
  115. }
  116. else if ( subCommand == "REMOVE_RECURSE" )
  117. {
  118. return this->HandleRemove(args, true);
  119. }
  120. else if ( subCommand == "COPY" )
  121. {
  122. return this->HandleCopyCommand(args);
  123. }
  124. else if ( subCommand == "INSTALL" )
  125. {
  126. return this->HandleInstallCommand(args);
  127. }
  128. else if ( subCommand == "DIFFERENT" )
  129. {
  130. return this->HandleDifferentCommand(args);
  131. }
  132. else if ( subCommand == "RPATH_CHANGE" || subCommand == "CHRPATH" )
  133. {
  134. return this->HandleRPathChangeCommand(args);
  135. }
  136. else if ( subCommand == "RPATH_CHECK" )
  137. {
  138. return this->HandleRPathCheckCommand(args);
  139. }
  140. else if ( subCommand == "RPATH_REMOVE" )
  141. {
  142. return this->HandleRPathRemoveCommand(args);
  143. }
  144. else if ( subCommand == "RELATIVE_PATH" )
  145. {
  146. return this->HandleRelativePathCommand(args);
  147. }
  148. else if ( subCommand == "TO_CMAKE_PATH" )
  149. {
  150. return this->HandleCMakePathCommand(args, false);
  151. }
  152. else if ( subCommand == "TO_NATIVE_PATH" )
  153. {
  154. return this->HandleCMakePathCommand(args, true);
  155. }
  156. std::string e = "does not recognize sub-command "+subCommand;
  157. this->SetError(e.c_str());
  158. return false;
  159. }
  160. //----------------------------------------------------------------------------
  161. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  162. bool append)
  163. {
  164. std::string message;
  165. std::vector<std::string>::const_iterator i = args.begin();
  166. i++; // Get rid of subcommand
  167. std::string fileName = *i;
  168. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  169. {
  170. fileName = this->Makefile->GetCurrentDirectory();
  171. fileName += "/" + *i;
  172. }
  173. i++;
  174. for(;i != args.end(); ++i)
  175. {
  176. message += *i;
  177. }
  178. if ( !this->Makefile->CanIWriteThisFile(fileName.c_str()) )
  179. {
  180. std::string e
  181. = "attempted to write a file: " + fileName +
  182. " into a source directory.";
  183. this->SetError(e.c_str());
  184. cmSystemTools::SetFatalErrorOccured();
  185. return false;
  186. }
  187. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  188. cmSystemTools::MakeDirectory(dir.c_str());
  189. mode_t mode = 0;
  190. // Set permissions to writable
  191. if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
  192. {
  193. cmSystemTools::SetPermissions(fileName.c_str(),
  194. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  195. mode | S_IWRITE
  196. #elif defined( __BORLANDC__ )
  197. mode | S_IWUSR
  198. #else
  199. mode | S_IWUSR | S_IWGRP
  200. #endif
  201. );
  202. }
  203. // If GetPermissions fails, pretend like it is ok. File open will fail if
  204. // the file is not writable
  205. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  206. if ( !file )
  207. {
  208. std::string error = "Internal CMake error when trying to open file: ";
  209. error += fileName.c_str();
  210. error += " for writing.";
  211. this->SetError(error.c_str());
  212. return false;
  213. }
  214. file << message;
  215. file.close();
  216. if(mode)
  217. {
  218. cmSystemTools::SetPermissions(fileName.c_str(), mode);
  219. }
  220. return true;
  221. }
  222. //----------------------------------------------------------------------------
  223. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  224. {
  225. if ( args.size() < 3 )
  226. {
  227. this->SetError("READ must be called with at least two additional "
  228. "arguments");
  229. return false;
  230. }
  231. cmCommandArgumentsHelper argHelper;
  232. cmCommandArgumentGroup group;
  233. cmCAString readArg (&argHelper, "READ");
  234. cmCAString fileNameArg (&argHelper, 0);
  235. cmCAString resultArg (&argHelper, 0);
  236. cmCAString offsetArg (&argHelper, "OFFSET", &group);
  237. cmCAString limitArg (&argHelper, "LIMIT", &group);
  238. cmCAEnabler hexOutputArg (&argHelper, "HEX", &group);
  239. readArg.Follows(0);
  240. fileNameArg.Follows(&readArg);
  241. resultArg.Follows(&fileNameArg);
  242. group.Follows(&resultArg);
  243. argHelper.Parse(&args, 0);
  244. std::string fileName = fileNameArg.GetString();
  245. if ( !cmsys::SystemTools::FileIsFullPath(fileName.c_str()) )
  246. {
  247. fileName = this->Makefile->GetCurrentDirectory();
  248. fileName += "/" + fileNameArg.GetString();
  249. }
  250. std::string variable = resultArg.GetString();
  251. // Open the specified file.
  252. #if defined(_WIN32) || defined(__CYGWIN__)
  253. std::ifstream file(fileName.c_str(), std::ios::in |
  254. (hexOutputArg.IsEnabled() ? std::ios::binary : std::ios::in));
  255. #else
  256. std::ifstream file(fileName.c_str(), std::ios::in);
  257. #endif
  258. if ( !file )
  259. {
  260. std::string error = "Internal CMake error when trying to open file: ";
  261. error += fileName.c_str();
  262. error += " for reading.";
  263. this->SetError(error.c_str());
  264. return false;
  265. }
  266. // is there a limit?
  267. long sizeLimit = -1;
  268. if (limitArg.GetString().size() > 0)
  269. {
  270. sizeLimit = atoi(limitArg.GetCString());
  271. }
  272. // is there an offset?
  273. long offset = 0;
  274. if (offsetArg.GetString().size() > 0)
  275. {
  276. offset = atoi(offsetArg.GetCString());
  277. }
  278. file.seekg(offset, std::ios::beg); // explicit ios::beg for IBM VisualAge 6
  279. std::string output;
  280. if (hexOutputArg.IsEnabled())
  281. {
  282. // Convert part of the file into hex code
  283. char c;
  284. while((sizeLimit != 0) && (file.get(c)))
  285. {
  286. char hex[4];
  287. sprintf(hex, "%.2x", c&0xff);
  288. output += hex;
  289. if (sizeLimit > 0)
  290. {
  291. sizeLimit--;
  292. }
  293. }
  294. }
  295. else
  296. {
  297. std::string line;
  298. bool has_newline = false;
  299. while (sizeLimit != 0 &&
  300. cmSystemTools::GetLineFromStream(file, line, &has_newline,
  301. sizeLimit) )
  302. {
  303. if (sizeLimit > 0)
  304. {
  305. sizeLimit = sizeLimit - static_cast<long>(line.size());
  306. if (has_newline)
  307. {
  308. sizeLimit--;
  309. }
  310. if (sizeLimit < 0)
  311. {
  312. sizeLimit = 0;
  313. }
  314. }
  315. output += line;
  316. if ( has_newline )
  317. {
  318. output += "\n";
  319. }
  320. }
  321. }
  322. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  323. return true;
  324. }
  325. //----------------------------------------------------------------------------
  326. bool cmFileCommand::HandleHashCommand(std::vector<std::string> const& args)
  327. {
  328. #if defined(CMAKE_BUILD_WITH_CMAKE)
  329. if(args.size() != 3)
  330. {
  331. cmOStringStream e;
  332. e << args[0] << " requires a file name and output variable";
  333. this->SetError(e.str().c_str());
  334. return false;
  335. }
  336. cmsys::auto_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0].c_str()));
  337. if(hash.get())
  338. {
  339. std::string out = hash->HashFile(args[1].c_str());
  340. if(!out.empty())
  341. {
  342. this->Makefile->AddDefinition(args[2].c_str(), out.c_str());
  343. return true;
  344. }
  345. cmOStringStream e;
  346. e << args[0] << " failed to read file \"" << args[1] << "\": "
  347. << cmSystemTools::GetLastSystemError();
  348. this->SetError(e.str().c_str());
  349. }
  350. return false;
  351. #else
  352. cmOStringStream e;
  353. e << args[0] << " not available during bootstrap";
  354. this->SetError(e.str().c_str());
  355. return false;
  356. #endif
  357. }
  358. //----------------------------------------------------------------------------
  359. bool cmFileCommand::HandleStringsCommand(std::vector<std::string> const& args)
  360. {
  361. if(args.size() < 3)
  362. {
  363. this->SetError("STRINGS requires a file name and output variable");
  364. return false;
  365. }
  366. // Get the file to read.
  367. std::string fileName = args[1];
  368. if(!cmsys::SystemTools::FileIsFullPath(fileName.c_str()))
  369. {
  370. fileName = this->Makefile->GetCurrentDirectory();
  371. fileName += "/" + args[1];
  372. }
  373. // Get the variable in which to store the results.
  374. std::string outVar = args[2];
  375. // Parse the options.
  376. enum { arg_none,
  377. arg_limit_input,
  378. arg_limit_output,
  379. arg_limit_count,
  380. arg_length_minimum,
  381. arg_length_maximum,
  382. arg__maximum,
  383. arg_regex };
  384. unsigned int minlen = 0;
  385. unsigned int maxlen = 0;
  386. int limit_input = -1;
  387. int limit_output = -1;
  388. unsigned int limit_count = 0;
  389. cmsys::RegularExpression regex;
  390. bool have_regex = false;
  391. bool newline_consume = false;
  392. bool hex_conversion_enabled = true;
  393. int arg_mode = arg_none;
  394. for(unsigned int i=3; i < args.size(); ++i)
  395. {
  396. if(args[i] == "LIMIT_INPUT")
  397. {
  398. arg_mode = arg_limit_input;
  399. }
  400. else if(args[i] == "LIMIT_OUTPUT")
  401. {
  402. arg_mode = arg_limit_output;
  403. }
  404. else if(args[i] == "LIMIT_COUNT")
  405. {
  406. arg_mode = arg_limit_count;
  407. }
  408. else if(args[i] == "LENGTH_MINIMUM")
  409. {
  410. arg_mode = arg_length_minimum;
  411. }
  412. else if(args[i] == "LENGTH_MAXIMUM")
  413. {
  414. arg_mode = arg_length_maximum;
  415. }
  416. else if(args[i] == "REGEX")
  417. {
  418. arg_mode = arg_regex;
  419. }
  420. else if(args[i] == "NEWLINE_CONSUME")
  421. {
  422. newline_consume = true;
  423. arg_mode = arg_none;
  424. }
  425. else if(args[i] == "NO_HEX_CONVERSION")
  426. {
  427. hex_conversion_enabled = false;
  428. arg_mode = arg_none;
  429. }
  430. else if(arg_mode == arg_limit_input)
  431. {
  432. if(sscanf(args[i].c_str(), "%d", &limit_input) != 1 ||
  433. limit_input < 0)
  434. {
  435. cmOStringStream e;
  436. e << "STRINGS option LIMIT_INPUT value \""
  437. << args[i] << "\" is not an unsigned integer.";
  438. this->SetError(e.str().c_str());
  439. return false;
  440. }
  441. arg_mode = arg_none;
  442. }
  443. else if(arg_mode == arg_limit_output)
  444. {
  445. if(sscanf(args[i].c_str(), "%d", &limit_output) != 1 ||
  446. limit_output < 0)
  447. {
  448. cmOStringStream e;
  449. e << "STRINGS option LIMIT_OUTPUT value \""
  450. << args[i] << "\" is not an unsigned integer.";
  451. this->SetError(e.str().c_str());
  452. return false;
  453. }
  454. arg_mode = arg_none;
  455. }
  456. else if(arg_mode == arg_limit_count)
  457. {
  458. int count;
  459. if(sscanf(args[i].c_str(), "%d", &count) != 1 || count < 0)
  460. {
  461. cmOStringStream e;
  462. e << "STRINGS option LIMIT_COUNT value \""
  463. << args[i] << "\" is not an unsigned integer.";
  464. this->SetError(e.str().c_str());
  465. return false;
  466. }
  467. limit_count = count;
  468. arg_mode = arg_none;
  469. }
  470. else if(arg_mode == arg_length_minimum)
  471. {
  472. int len;
  473. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  474. {
  475. cmOStringStream e;
  476. e << "STRINGS option LENGTH_MINIMUM value \""
  477. << args[i] << "\" is not an unsigned integer.";
  478. this->SetError(e.str().c_str());
  479. return false;
  480. }
  481. minlen = len;
  482. arg_mode = arg_none;
  483. }
  484. else if(arg_mode == arg_length_maximum)
  485. {
  486. int len;
  487. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  488. {
  489. cmOStringStream e;
  490. e << "STRINGS option LENGTH_MAXIMUM value \""
  491. << args[i] << "\" is not an unsigned integer.";
  492. this->SetError(e.str().c_str());
  493. return false;
  494. }
  495. maxlen = len;
  496. arg_mode = arg_none;
  497. }
  498. else if(arg_mode == arg_regex)
  499. {
  500. if(!regex.compile(args[i].c_str()))
  501. {
  502. cmOStringStream e;
  503. e << "STRINGS option REGEX value \""
  504. << args[i] << "\" could not be compiled.";
  505. this->SetError(e.str().c_str());
  506. return false;
  507. }
  508. have_regex = true;
  509. arg_mode = arg_none;
  510. }
  511. else
  512. {
  513. cmOStringStream e;
  514. e << "STRINGS given unknown argument \""
  515. << args[i] << "\"";
  516. this->SetError(e.str().c_str());
  517. return false;
  518. }
  519. }
  520. if (hex_conversion_enabled)
  521. {
  522. // TODO: should work without temp file, but just on a memory buffer
  523. std::string binaryFileName = this->Makefile->GetCurrentOutputDirectory();
  524. binaryFileName += cmake::GetCMakeFilesDirectory();
  525. binaryFileName += "/FileCommandStringsBinaryFile";
  526. if(cmHexFileConverter::TryConvert(fileName.c_str(),binaryFileName.c_str()))
  527. {
  528. fileName = binaryFileName;
  529. }
  530. }
  531. // Open the specified file.
  532. #if defined(_WIN32) || defined(__CYGWIN__)
  533. std::ifstream fin(fileName.c_str(), std::ios::in | std::ios::binary);
  534. #else
  535. std::ifstream fin(fileName.c_str(), std::ios::in);
  536. #endif
  537. if(!fin)
  538. {
  539. cmOStringStream e;
  540. e << "STRINGS file \"" << fileName << "\" cannot be read.";
  541. this->SetError(e.str().c_str());
  542. return false;
  543. }
  544. // Parse strings out of the file.
  545. int output_size = 0;
  546. std::vector<std::string> strings;
  547. std::string s;
  548. int c;
  549. while((!limit_count || strings.size() < limit_count) &&
  550. (limit_input < 0 || static_cast<int>(fin.tellg()) < limit_input) &&
  551. (c = fin.get(), fin))
  552. {
  553. if(c == '\n' && !newline_consume)
  554. {
  555. // The current line has been terminated. Check if the current
  556. // string matches the requirements. The length may now be as
  557. // low as zero since blank lines are allowed.
  558. if(s.length() >= minlen &&
  559. (!have_regex || regex.find(s.c_str())))
  560. {
  561. output_size += static_cast<int>(s.size()) + 1;
  562. if(limit_output >= 0 && output_size >= limit_output)
  563. {
  564. s = "";
  565. break;
  566. }
  567. strings.push_back(s);
  568. }
  569. // Reset the string to empty.
  570. s = "";
  571. }
  572. else if(c == '\r')
  573. {
  574. // Ignore CR character to make output always have UNIX newlines.
  575. }
  576. else if((c >= 0x20 && c < 0x7F) || c == '\t' ||
  577. (c == '\n' && newline_consume))
  578. {
  579. // This is an ASCII character that may be part of a string.
  580. // Cast added to avoid compiler warning. Cast is ok because
  581. // c is guaranteed to fit in char by the above if...
  582. s += static_cast<char>(c);
  583. }
  584. else
  585. {
  586. // TODO: Support ENCODING option. See issue #10519.
  587. // A non-string character has been found. Check if the current
  588. // string matches the requirements. We require that the length
  589. // be at least one no matter what the user specified.
  590. if(s.length() >= minlen && s.length() >= 1 &&
  591. (!have_regex || regex.find(s.c_str())))
  592. {
  593. output_size += static_cast<int>(s.size()) + 1;
  594. if(limit_output >= 0 && output_size >= limit_output)
  595. {
  596. s = "";
  597. break;
  598. }
  599. strings.push_back(s);
  600. }
  601. // Reset the string to empty.
  602. s = "";
  603. }
  604. // Terminate a string if the maximum length is reached.
  605. if(maxlen > 0 && s.size() == maxlen)
  606. {
  607. if(s.length() >= minlen &&
  608. (!have_regex || regex.find(s.c_str())))
  609. {
  610. output_size += static_cast<int>(s.size()) + 1;
  611. if(limit_output >= 0 && output_size >= limit_output)
  612. {
  613. s = "";
  614. break;
  615. }
  616. strings.push_back(s);
  617. }
  618. s = "";
  619. }
  620. }
  621. // If there is a non-empty current string we have hit the end of the
  622. // input file or the input size limit. Check if the current string
  623. // matches the requirements.
  624. if((!limit_count || strings.size() < limit_count) &&
  625. !s.empty() && s.length() >= minlen &&
  626. (!have_regex || regex.find(s.c_str())))
  627. {
  628. output_size += static_cast<int>(s.size()) + 1;
  629. if(limit_output < 0 || output_size < limit_output)
  630. {
  631. strings.push_back(s);
  632. }
  633. }
  634. // Encode the result in a CMake list.
  635. const char* sep = "";
  636. std::string output;
  637. for(std::vector<std::string>::const_iterator si = strings.begin();
  638. si != strings.end(); ++si)
  639. {
  640. // Separate the strings in the output to make it a list.
  641. output += sep;
  642. sep = ";";
  643. // Store the string in the output, but escape semicolons to
  644. // make sure it is a list.
  645. std::string const& sr = *si;
  646. for(unsigned int i=0; i < sr.size(); ++i)
  647. {
  648. if(sr[i] == ';')
  649. {
  650. output += '\\';
  651. }
  652. output += sr[i];
  653. }
  654. }
  655. // Save the output in a makefile variable.
  656. this->Makefile->AddDefinition(outVar.c_str(), output.c_str());
  657. return true;
  658. }
  659. //----------------------------------------------------------------------------
  660. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  661. bool recurse)
  662. {
  663. if ( args.size() < 2 )
  664. {
  665. this->SetError("GLOB requires at least a variable name");
  666. return false;
  667. }
  668. std::vector<std::string>::const_iterator i = args.begin();
  669. i++; // Get rid of subcommand
  670. std::string variable = *i;
  671. i++;
  672. cmsys::Glob g;
  673. g.SetRecurse(recurse);
  674. bool explicitFollowSymlinks = false;
  675. cmPolicies::PolicyStatus status =
  676. this->Makefile->GetPolicyStatus(cmPolicies::CMP0009);
  677. if(recurse)
  678. {
  679. switch(status)
  680. {
  681. case cmPolicies::NEW:
  682. g.RecurseThroughSymlinksOff();
  683. break;
  684. case cmPolicies::OLD:
  685. case cmPolicies::WARN:
  686. case cmPolicies::REQUIRED_IF_USED:
  687. case cmPolicies::REQUIRED_ALWAYS:
  688. g.RecurseThroughSymlinksOn();
  689. break;
  690. }
  691. }
  692. std::string output = "";
  693. bool first = true;
  694. for ( ; i != args.end(); ++i )
  695. {
  696. if ( recurse && (*i == "FOLLOW_SYMLINKS") )
  697. {
  698. explicitFollowSymlinks = true;
  699. g.RecurseThroughSymlinksOn();
  700. ++i;
  701. if ( i == args.end() )
  702. {
  703. this->SetError(
  704. "GLOB_RECURSE requires a glob expression after FOLLOW_SYMLINKS");
  705. return false;
  706. }
  707. }
  708. if ( *i == "RELATIVE" )
  709. {
  710. ++i; // skip RELATIVE
  711. if ( i == args.end() )
  712. {
  713. this->SetError("GLOB requires a directory after the RELATIVE tag");
  714. return false;
  715. }
  716. g.SetRelative(i->c_str());
  717. ++i;
  718. if(i == args.end())
  719. {
  720. this->SetError("GLOB requires a glob expression after the directory");
  721. return false;
  722. }
  723. }
  724. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  725. {
  726. std::string expr = this->Makefile->GetCurrentDirectory();
  727. // Handle script mode
  728. if ( expr.size() > 0 )
  729. {
  730. expr += "/" + *i;
  731. g.FindFiles(expr);
  732. }
  733. else
  734. {
  735. g.FindFiles(*i);
  736. }
  737. }
  738. else
  739. {
  740. g.FindFiles(*i);
  741. }
  742. std::vector<std::string>::size_type cc;
  743. std::vector<std::string>& files = g.GetFiles();
  744. for ( cc = 0; cc < files.size(); cc ++ )
  745. {
  746. if ( !first )
  747. {
  748. output += ";";
  749. }
  750. output += files[cc];
  751. first = false;
  752. }
  753. }
  754. if(recurse && !explicitFollowSymlinks)
  755. {
  756. switch (status)
  757. {
  758. case cmPolicies::NEW:
  759. // Correct behavior, yay!
  760. break;
  761. case cmPolicies::OLD:
  762. // Probably not really the expected behavior, but the author explicitly
  763. // asked for the old behavior... no warning.
  764. case cmPolicies::WARN:
  765. // Possibly unexpected old behavior *and* we actually traversed
  766. // symlinks without being explicitly asked to: warn the author.
  767. if(g.GetFollowedSymlinkCount() != 0)
  768. {
  769. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING,
  770. this->Makefile->GetPolicies()->
  771. GetPolicyWarning(cmPolicies::CMP0009));
  772. }
  773. break;
  774. case cmPolicies::REQUIRED_IF_USED:
  775. case cmPolicies::REQUIRED_ALWAYS:
  776. this->SetError("policy CMP0009 error");
  777. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  778. this->Makefile->GetPolicies()->
  779. GetRequiredPolicyError(cmPolicies::CMP0009));
  780. return false;
  781. }
  782. }
  783. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  784. return true;
  785. }
  786. //----------------------------------------------------------------------------
  787. bool cmFileCommand::HandleMakeDirectoryCommand(
  788. std::vector<std::string> const& args)
  789. {
  790. if(args.size() < 2 )
  791. {
  792. this->SetError("called with incorrect number of arguments");
  793. return false;
  794. }
  795. std::vector<std::string>::const_iterator i = args.begin();
  796. i++; // Get rid of subcommand
  797. std::string expr;
  798. for ( ; i != args.end(); ++i )
  799. {
  800. const std::string* cdir = &(*i);
  801. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  802. {
  803. expr = this->Makefile->GetCurrentDirectory();
  804. expr += "/" + *i;
  805. cdir = &expr;
  806. }
  807. if ( !this->Makefile->CanIWriteThisFile(cdir->c_str()) )
  808. {
  809. std::string e = "attempted to create a directory: " + *cdir
  810. + " into a source directory.";
  811. this->SetError(e.c_str());
  812. cmSystemTools::SetFatalErrorOccured();
  813. return false;
  814. }
  815. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  816. {
  817. std::string error = "problem creating directory: " + *cdir;
  818. this->SetError(error.c_str());
  819. return false;
  820. }
  821. }
  822. return true;
  823. }
  824. //----------------------------------------------------------------------------
  825. bool
  826. cmFileCommand::HandleDifferentCommand(std::vector<std::string> const& args)
  827. {
  828. /*
  829. FILE(DIFFERENT <variable> FILES <lhs> <rhs>)
  830. */
  831. // Evaluate arguments.
  832. const char* file_lhs = 0;
  833. const char* file_rhs = 0;
  834. const char* var = 0;
  835. enum Doing { DoingNone, DoingVar, DoingFileLHS, DoingFileRHS };
  836. Doing doing = DoingVar;
  837. for(unsigned int i=1; i < args.size(); ++i)
  838. {
  839. if(args[i] == "FILES")
  840. {
  841. doing = DoingFileLHS;
  842. }
  843. else if(doing == DoingVar)
  844. {
  845. var = args[i].c_str();
  846. doing = DoingNone;
  847. }
  848. else if(doing == DoingFileLHS)
  849. {
  850. file_lhs = args[i].c_str();
  851. doing = DoingFileRHS;
  852. }
  853. else if(doing == DoingFileRHS)
  854. {
  855. file_rhs = args[i].c_str();
  856. doing = DoingNone;
  857. }
  858. else
  859. {
  860. cmOStringStream e;
  861. e << "DIFFERENT given unknown argument " << args[i];
  862. this->SetError(e.str().c_str());
  863. return false;
  864. }
  865. }
  866. if(!var)
  867. {
  868. this->SetError("DIFFERENT not given result variable name.");
  869. return false;
  870. }
  871. if(!file_lhs || !file_rhs)
  872. {
  873. this->SetError("DIFFERENT not given FILES option with two file names.");
  874. return false;
  875. }
  876. // Compare the files.
  877. const char* result =
  878. cmSystemTools::FilesDiffer(file_lhs, file_rhs)? "1" : "0";
  879. this->Makefile->AddDefinition(var, result);
  880. return true;
  881. }
  882. //----------------------------------------------------------------------------
  883. // File installation helper class.
  884. struct cmFileCopier
  885. {
  886. cmFileCopier(cmFileCommand* command, const char* name = "COPY"):
  887. FileCommand(command),
  888. Makefile(command->GetMakefile()),
  889. Name(name),
  890. Always(false),
  891. MatchlessFiles(true),
  892. FilePermissions(0),
  893. DirPermissions(0),
  894. CurrentMatchRule(0),
  895. UseGivenPermissionsFile(false),
  896. UseGivenPermissionsDir(false),
  897. UseSourcePermissions(true),
  898. Doing(DoingNone)
  899. {
  900. }
  901. virtual ~cmFileCopier() {}
  902. bool Run(std::vector<std::string> const& args);
  903. protected:
  904. cmFileCommand* FileCommand;
  905. cmMakefile* Makefile;
  906. const char* Name;
  907. bool Always;
  908. cmFileTimeComparison FileTimes;
  909. // Whether to install a file not matching any expression.
  910. bool MatchlessFiles;
  911. // Permissions for files and directories installed by this object.
  912. mode_t FilePermissions;
  913. mode_t DirPermissions;
  914. // Properties set by pattern and regex match rules.
  915. struct MatchProperties
  916. {
  917. bool Exclude;
  918. mode_t Permissions;
  919. MatchProperties(): Exclude(false), Permissions(0) {}
  920. };
  921. struct MatchRule;
  922. friend struct MatchRule;
  923. struct MatchRule
  924. {
  925. cmsys::RegularExpression Regex;
  926. MatchProperties Properties;
  927. std::string RegexString;
  928. MatchRule(std::string const& regex):
  929. Regex(regex.c_str()), RegexString(regex) {}
  930. };
  931. std::vector<MatchRule> MatchRules;
  932. // Get the properties from rules matching this input file.
  933. MatchProperties CollectMatchProperties(const char* file)
  934. {
  935. // Match rules are case-insensitive on some platforms.
  936. #if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
  937. std::string lower = cmSystemTools::LowerCase(file);
  938. const char* file_to_match = lower.c_str();
  939. #else
  940. const char* file_to_match = file;
  941. #endif
  942. // Collect properties from all matching rules.
  943. bool matched = false;
  944. MatchProperties result;
  945. for(std::vector<MatchRule>::iterator mr = this->MatchRules.begin();
  946. mr != this->MatchRules.end(); ++mr)
  947. {
  948. if(mr->Regex.find(file_to_match))
  949. {
  950. matched = true;
  951. result.Exclude |= mr->Properties.Exclude;
  952. result.Permissions |= mr->Properties.Permissions;
  953. }
  954. }
  955. if(!matched && !this->MatchlessFiles)
  956. {
  957. result.Exclude = !cmSystemTools::FileIsDirectory(file);
  958. }
  959. return result;
  960. }
  961. bool SetPermissions(const char* toFile, mode_t permissions)
  962. {
  963. if(permissions && !cmSystemTools::SetPermissions(toFile, permissions))
  964. {
  965. cmOStringStream e;
  966. e << this->Name << " cannot set permissions on \"" << toFile << "\"";
  967. this->FileCommand->SetError(e.str().c_str());
  968. return false;
  969. }
  970. return true;
  971. }
  972. // Translate an argument to a permissions bit.
  973. bool CheckPermissions(std::string const& arg, mode_t& permissions)
  974. {
  975. if(arg == "OWNER_READ") { permissions |= mode_owner_read; }
  976. else if(arg == "OWNER_WRITE") { permissions |= mode_owner_write; }
  977. else if(arg == "OWNER_EXECUTE") { permissions |= mode_owner_execute; }
  978. else if(arg == "GROUP_READ") { permissions |= mode_group_read; }
  979. else if(arg == "GROUP_WRITE") { permissions |= mode_group_write; }
  980. else if(arg == "GROUP_EXECUTE") { permissions |= mode_group_execute; }
  981. else if(arg == "WORLD_READ") { permissions |= mode_world_read; }
  982. else if(arg == "WORLD_WRITE") { permissions |= mode_world_write; }
  983. else if(arg == "WORLD_EXECUTE") { permissions |= mode_world_execute; }
  984. else if(arg == "SETUID") { permissions |= mode_setuid; }
  985. else if(arg == "SETGID") { permissions |= mode_setgid; }
  986. else
  987. {
  988. cmOStringStream e;
  989. e << this->Name << " given invalid permission \"" << arg << "\".";
  990. this->FileCommand->SetError(e.str().c_str());
  991. return false;
  992. }
  993. return true;
  994. }
  995. bool InstallSymlink(const char* fromFile, const char* toFile);
  996. bool InstallFile(const char* fromFile, const char* toFile,
  997. MatchProperties const& match_properties);
  998. bool InstallDirectory(const char* source, const char* destination,
  999. MatchProperties const& match_properties);
  1000. virtual bool Install(const char* fromFile, const char* toFile);
  1001. virtual std::string const& ToName(std::string const& fromName)
  1002. { return fromName; }
  1003. enum Type
  1004. {
  1005. TypeFile,
  1006. TypeDir,
  1007. TypeLink
  1008. };
  1009. virtual void ReportCopy(const char*, Type, bool) {}
  1010. virtual bool ReportMissing(const char* fromFile)
  1011. {
  1012. // The input file does not exist and installation is not optional.
  1013. cmOStringStream e;
  1014. e << this->Name << " cannot find \"" << fromFile << "\".";
  1015. this->FileCommand->SetError(e.str().c_str());
  1016. return false;
  1017. }
  1018. MatchRule* CurrentMatchRule;
  1019. bool UseGivenPermissionsFile;
  1020. bool UseGivenPermissionsDir;
  1021. bool UseSourcePermissions;
  1022. std::string Destination;
  1023. std::vector<std::string> Files;
  1024. int Doing;
  1025. virtual bool Parse(std::vector<std::string> const& args);
  1026. enum
  1027. {
  1028. DoingNone,
  1029. DoingError,
  1030. DoingDestination,
  1031. DoingFiles,
  1032. DoingPattern,
  1033. DoingRegex,
  1034. DoingPermissionsFile,
  1035. DoingPermissionsDir,
  1036. DoingPermissionsMatch,
  1037. DoingLast1
  1038. };
  1039. virtual bool CheckKeyword(std::string const& arg);
  1040. virtual bool CheckValue(std::string const& arg);
  1041. void NotBeforeMatch(std::string const& arg)
  1042. {
  1043. cmOStringStream e;
  1044. e << "option " << arg << " may not appear before PATTERN or REGEX.";
  1045. this->FileCommand->SetError(e.str().c_str());
  1046. this->Doing = DoingError;
  1047. }
  1048. void NotAfterMatch(std::string const& arg)
  1049. {
  1050. cmOStringStream e;
  1051. e << "option " << arg << " may not appear after PATTERN or REGEX.";
  1052. this->FileCommand->SetError(e.str().c_str());
  1053. this->Doing = DoingError;
  1054. }
  1055. virtual void DefaultFilePermissions()
  1056. {
  1057. // Use read/write permissions.
  1058. this->FilePermissions = 0;
  1059. this->FilePermissions |= mode_owner_read;
  1060. this->FilePermissions |= mode_owner_write;
  1061. this->FilePermissions |= mode_group_read;
  1062. this->FilePermissions |= mode_world_read;
  1063. }
  1064. virtual void DefaultDirectoryPermissions()
  1065. {
  1066. // Use read/write/executable permissions.
  1067. this->DirPermissions = 0;
  1068. this->DirPermissions |= mode_owner_read;
  1069. this->DirPermissions |= mode_owner_write;
  1070. this->DirPermissions |= mode_owner_execute;
  1071. this->DirPermissions |= mode_group_read;
  1072. this->DirPermissions |= mode_group_execute;
  1073. this->DirPermissions |= mode_world_read;
  1074. this->DirPermissions |= mode_world_execute;
  1075. }
  1076. };
  1077. //----------------------------------------------------------------------------
  1078. bool cmFileCopier::Parse(std::vector<std::string> const& args)
  1079. {
  1080. this->Doing = DoingFiles;
  1081. for(unsigned int i=1; i < args.size(); ++i)
  1082. {
  1083. // Check this argument.
  1084. if(!this->CheckKeyword(args[i]) &&
  1085. !this->CheckValue(args[i]))
  1086. {
  1087. cmOStringStream e;
  1088. e << "called with unknown argument \"" << args[i] << "\".";
  1089. this->FileCommand->SetError(e.str().c_str());
  1090. return false;
  1091. }
  1092. // Quit if an argument is invalid.
  1093. if(this->Doing == DoingError)
  1094. {
  1095. return false;
  1096. }
  1097. }
  1098. // Require a destination.
  1099. if(this->Destination.empty())
  1100. {
  1101. cmOStringStream e;
  1102. e << this->Name << " given no DESTINATION";
  1103. this->FileCommand->SetError(e.str().c_str());
  1104. return false;
  1105. }
  1106. // If file permissions were not specified set default permissions.
  1107. if(!this->UseGivenPermissionsFile && !this->UseSourcePermissions)
  1108. {
  1109. this->DefaultFilePermissions();
  1110. }
  1111. // If directory permissions were not specified set default permissions.
  1112. if(!this->UseGivenPermissionsDir && !this->UseSourcePermissions)
  1113. {
  1114. this->DefaultDirectoryPermissions();
  1115. }
  1116. return true;
  1117. }
  1118. //----------------------------------------------------------------------------
  1119. bool cmFileCopier::CheckKeyword(std::string const& arg)
  1120. {
  1121. if(arg == "DESTINATION")
  1122. {
  1123. if(this->CurrentMatchRule)
  1124. {
  1125. this->NotAfterMatch(arg);
  1126. }
  1127. else
  1128. {
  1129. this->Doing = DoingDestination;
  1130. }
  1131. }
  1132. else if(arg == "PATTERN")
  1133. {
  1134. this->Doing = DoingPattern;
  1135. }
  1136. else if(arg == "REGEX")
  1137. {
  1138. this->Doing = DoingRegex;
  1139. }
  1140. else if(arg == "EXCLUDE")
  1141. {
  1142. // Add this property to the current match rule.
  1143. if(this->CurrentMatchRule)
  1144. {
  1145. this->CurrentMatchRule->Properties.Exclude = true;
  1146. this->Doing = DoingNone;
  1147. }
  1148. else
  1149. {
  1150. this->NotBeforeMatch(arg);
  1151. }
  1152. }
  1153. else if(arg == "PERMISSIONS")
  1154. {
  1155. if(this->CurrentMatchRule)
  1156. {
  1157. this->Doing = DoingPermissionsMatch;
  1158. }
  1159. else
  1160. {
  1161. this->NotBeforeMatch(arg);
  1162. }
  1163. }
  1164. else if(arg == "FILE_PERMISSIONS")
  1165. {
  1166. if(this->CurrentMatchRule)
  1167. {
  1168. this->NotAfterMatch(arg);
  1169. }
  1170. else
  1171. {
  1172. this->Doing = DoingPermissionsFile;
  1173. this->UseGivenPermissionsFile = true;
  1174. }
  1175. }
  1176. else if(arg == "DIRECTORY_PERMISSIONS")
  1177. {
  1178. if(this->CurrentMatchRule)
  1179. {
  1180. this->NotAfterMatch(arg);
  1181. }
  1182. else
  1183. {
  1184. this->Doing = DoingPermissionsDir;
  1185. this->UseGivenPermissionsDir = true;
  1186. }
  1187. }
  1188. else if(arg == "USE_SOURCE_PERMISSIONS")
  1189. {
  1190. if(this->CurrentMatchRule)
  1191. {
  1192. this->NotAfterMatch(arg);
  1193. }
  1194. else
  1195. {
  1196. this->Doing = DoingNone;
  1197. this->UseSourcePermissions = true;
  1198. }
  1199. }
  1200. else if(arg == "NO_SOURCE_PERMISSIONS")
  1201. {
  1202. if(this->CurrentMatchRule)
  1203. {
  1204. this->NotAfterMatch(arg);
  1205. }
  1206. else
  1207. {
  1208. this->Doing = DoingNone;
  1209. this->UseSourcePermissions = false;
  1210. }
  1211. }
  1212. else if(arg == "FILES_MATCHING")
  1213. {
  1214. if(this->CurrentMatchRule)
  1215. {
  1216. this->NotAfterMatch(arg);
  1217. }
  1218. else
  1219. {
  1220. this->Doing = DoingNone;
  1221. this->MatchlessFiles = false;
  1222. }
  1223. }
  1224. else
  1225. {
  1226. return false;
  1227. }
  1228. return true;
  1229. }
  1230. //----------------------------------------------------------------------------
  1231. bool cmFileCopier::CheckValue(std::string const& arg)
  1232. {
  1233. switch(this->Doing)
  1234. {
  1235. case DoingFiles:
  1236. if(arg.empty() || cmSystemTools::FileIsFullPath(arg.c_str()))
  1237. {
  1238. this->Files.push_back(arg);
  1239. }
  1240. else
  1241. {
  1242. std::string file = this->Makefile->GetCurrentDirectory();
  1243. file += "/" + arg;
  1244. this->Files.push_back(file);
  1245. }
  1246. break;
  1247. case DoingDestination:
  1248. if(arg.empty() || cmSystemTools::FileIsFullPath(arg.c_str()))
  1249. {
  1250. this->Destination = arg;
  1251. }
  1252. else
  1253. {
  1254. this->Destination = this->Makefile->GetCurrentOutputDirectory();
  1255. this->Destination += "/" + arg;
  1256. }
  1257. this->Doing = DoingNone;
  1258. break;
  1259. case DoingPattern:
  1260. {
  1261. // Convert the pattern to a regular expression. Require a
  1262. // leading slash and trailing end-of-string in the matched
  1263. // string to make sure the pattern matches only whole file
  1264. // names.
  1265. std::string regex = "/";
  1266. regex += cmsys::Glob::PatternToRegex(arg, false);
  1267. regex += "$";
  1268. this->MatchRules.push_back(MatchRule(regex));
  1269. this->CurrentMatchRule = &*(this->MatchRules.end()-1);
  1270. if(this->CurrentMatchRule->Regex.is_valid())
  1271. {
  1272. this->Doing = DoingNone;
  1273. }
  1274. else
  1275. {
  1276. cmOStringStream e;
  1277. e << "could not compile PATTERN \"" << arg << "\".";
  1278. this->FileCommand->SetError(e.str().c_str());
  1279. this->Doing = DoingError;
  1280. }
  1281. }
  1282. break;
  1283. case DoingRegex:
  1284. this->MatchRules.push_back(MatchRule(arg));
  1285. this->CurrentMatchRule = &*(this->MatchRules.end()-1);
  1286. if(this->CurrentMatchRule->Regex.is_valid())
  1287. {
  1288. this->Doing = DoingNone;
  1289. }
  1290. else
  1291. {
  1292. cmOStringStream e;
  1293. e << "could not compile REGEX \"" << arg << "\".";
  1294. this->FileCommand->SetError(e.str().c_str());
  1295. this->Doing = DoingError;
  1296. }
  1297. break;
  1298. case DoingPermissionsFile:
  1299. if(!this->CheckPermissions(arg, this->FilePermissions))
  1300. {
  1301. this->Doing = DoingError;
  1302. }
  1303. break;
  1304. case DoingPermissionsDir:
  1305. if(!this->CheckPermissions(arg, this->DirPermissions))
  1306. {
  1307. this->Doing = DoingError;
  1308. }
  1309. break;
  1310. case DoingPermissionsMatch:
  1311. if(!this->CheckPermissions(
  1312. arg, this->CurrentMatchRule->Properties.Permissions))
  1313. {
  1314. this->Doing = DoingError;
  1315. }
  1316. break;
  1317. default:
  1318. return false;
  1319. }
  1320. return true;
  1321. }
  1322. //----------------------------------------------------------------------------
  1323. bool cmFileCopier::Run(std::vector<std::string> const& args)
  1324. {
  1325. if(!this->Parse(args))
  1326. {
  1327. return false;
  1328. }
  1329. std::vector<std::string> const& files = this->Files;
  1330. for(std::vector<std::string>::size_type i = 0; i < files.size(); ++i)
  1331. {
  1332. // Split the input file into its directory and name components.
  1333. std::vector<std::string> fromPathComponents;
  1334. cmSystemTools::SplitPath(files[i].c_str(), fromPathComponents);
  1335. std::string fromName = *(fromPathComponents.end()-1);
  1336. std::string fromDir = cmSystemTools::JoinPath(fromPathComponents.begin(),
  1337. fromPathComponents.end()-1);
  1338. // Compute the full path to the destination file.
  1339. std::string toFile = this->Destination;
  1340. std::string const& toName = this->ToName(fromName);
  1341. if(!toName.empty())
  1342. {
  1343. toFile += "/";
  1344. toFile += toName;
  1345. }
  1346. // Construct the full path to the source file. The file name may
  1347. // have been changed above.
  1348. std::string fromFile = fromDir;
  1349. if(!fromName.empty())
  1350. {
  1351. fromFile += "/";
  1352. fromFile += fromName;
  1353. }
  1354. if(!this->Install(fromFile.c_str(), toFile.c_str()))
  1355. {
  1356. return false;
  1357. }
  1358. }
  1359. return true;
  1360. }
  1361. //----------------------------------------------------------------------------
  1362. bool cmFileCopier::Install(const char* fromFile, const char* toFile)
  1363. {
  1364. if(!*fromFile)
  1365. {
  1366. cmOStringStream e;
  1367. e << "INSTALL encountered an empty string input file name.";
  1368. this->FileCommand->SetError(e.str().c_str());
  1369. return false;
  1370. }
  1371. // Collect any properties matching this file name.
  1372. MatchProperties match_properties = this->CollectMatchProperties(fromFile);
  1373. // Skip the file if it is excluded.
  1374. if(match_properties.Exclude)
  1375. {
  1376. return true;
  1377. }
  1378. if(cmSystemTools::SameFile(fromFile, toFile))
  1379. {
  1380. return true;
  1381. }
  1382. else if(cmSystemTools::FileIsSymlink(fromFile))
  1383. {
  1384. return this->InstallSymlink(fromFile, toFile);
  1385. }
  1386. else if(cmSystemTools::FileIsDirectory(fromFile))
  1387. {
  1388. return this->InstallDirectory(fromFile, toFile, match_properties);
  1389. }
  1390. else if(cmSystemTools::FileExists(fromFile))
  1391. {
  1392. return this->InstallFile(fromFile, toFile, match_properties);
  1393. }
  1394. return this->ReportMissing(fromFile);
  1395. }
  1396. //----------------------------------------------------------------------------
  1397. bool cmFileCopier::InstallSymlink(const char* fromFile, const char* toFile)
  1398. {
  1399. // Read the original symlink.
  1400. std::string symlinkTarget;
  1401. if(!cmSystemTools::ReadSymlink(fromFile, symlinkTarget))
  1402. {
  1403. cmOStringStream e;
  1404. e << this->Name << " cannot read symlink \"" << fromFile
  1405. << "\" to duplicate at \"" << toFile << "\".";
  1406. this->FileCommand->SetError(e.str().c_str());
  1407. return false;
  1408. }
  1409. // Compare the symlink value to that at the destination if not
  1410. // always installing.
  1411. bool copy = true;
  1412. if(!this->Always)
  1413. {
  1414. std::string oldSymlinkTarget;
  1415. if(cmSystemTools::ReadSymlink(toFile, oldSymlinkTarget))
  1416. {
  1417. if(symlinkTarget == oldSymlinkTarget)
  1418. {
  1419. copy = false;
  1420. }
  1421. }
  1422. }
  1423. // Inform the user about this file installation.
  1424. this->ReportCopy(toFile, TypeLink, copy);
  1425. if(copy)
  1426. {
  1427. // Remove the destination file so we can always create the symlink.
  1428. cmSystemTools::RemoveFile(toFile);
  1429. // Create the symlink.
  1430. if(!cmSystemTools::CreateSymlink(symlinkTarget.c_str(), toFile))
  1431. {
  1432. cmOStringStream e;
  1433. e << this->Name << " cannot duplicate symlink \"" << fromFile
  1434. << "\" at \"" << toFile << "\".";
  1435. this->FileCommand->SetError(e.str().c_str());
  1436. return false;
  1437. }
  1438. }
  1439. return true;
  1440. }
  1441. //----------------------------------------------------------------------------
  1442. bool cmFileCopier::InstallFile(const char* fromFile, const char* toFile,
  1443. MatchProperties const& match_properties)
  1444. {
  1445. // Determine whether we will copy the file.
  1446. bool copy = true;
  1447. if(!this->Always)
  1448. {
  1449. // If both files exist with the same time do not copy.
  1450. if(!this->FileTimes.FileTimesDiffer(fromFile, toFile))
  1451. {
  1452. copy = false;
  1453. }
  1454. }
  1455. // Inform the user about this file installation.
  1456. this->ReportCopy(toFile, TypeFile, copy);
  1457. // Copy the file.
  1458. if(copy && !cmSystemTools::CopyAFile(fromFile, toFile, true))
  1459. {
  1460. cmOStringStream e;
  1461. e << this->Name << " cannot copy file \"" << fromFile
  1462. << "\" to \"" << toFile << "\".";
  1463. this->FileCommand->SetError(e.str().c_str());
  1464. return false;
  1465. }
  1466. // Set the file modification time of the destination file.
  1467. if(copy && !this->Always)
  1468. {
  1469. // Add write permission so we can set the file time.
  1470. // Permissions are set unconditionally below anyway.
  1471. mode_t perm = 0;
  1472. if(cmSystemTools::GetPermissions(toFile, perm))
  1473. {
  1474. cmSystemTools::SetPermissions(toFile, perm | mode_owner_write);
  1475. }
  1476. if (!cmSystemTools::CopyFileTime(fromFile, toFile))
  1477. {
  1478. cmOStringStream e;
  1479. e << this->Name << " cannot set modification time on \""
  1480. << toFile << "\"";
  1481. this->FileCommand->SetError(e.str().c_str());
  1482. return false;
  1483. }
  1484. }
  1485. // Set permissions of the destination file.
  1486. mode_t permissions = (match_properties.Permissions?
  1487. match_properties.Permissions : this->FilePermissions);
  1488. if(!permissions)
  1489. {
  1490. // No permissions were explicitly provided but the user requested
  1491. // that the source file permissions be used.
  1492. cmSystemTools::GetPermissions(fromFile, permissions);
  1493. }
  1494. return this->SetPermissions(toFile, permissions);
  1495. }
  1496. //----------------------------------------------------------------------------
  1497. bool cmFileCopier::InstallDirectory(const char* source,
  1498. const char* destination,
  1499. MatchProperties const& match_properties)
  1500. {
  1501. // Inform the user about this directory installation.
  1502. this->ReportCopy(destination, TypeDir, true);
  1503. // Make sure the destination directory exists.
  1504. if(!cmSystemTools::MakeDirectory(destination))
  1505. {
  1506. cmOStringStream e;
  1507. e << this->Name << " cannot make directory \"" << destination << "\": "
  1508. << cmSystemTools::GetLastSystemError();
  1509. this->FileCommand->SetError(e.str().c_str());
  1510. return false;
  1511. }
  1512. // Compute the requested permissions for the destination directory.
  1513. mode_t permissions = (match_properties.Permissions?
  1514. match_properties.Permissions : this->DirPermissions);
  1515. if(!permissions)
  1516. {
  1517. // No permissions were explicitly provided but the user requested
  1518. // that the source directory permissions be used.
  1519. cmSystemTools::GetPermissions(source, permissions);
  1520. }
  1521. // Compute the set of permissions required on this directory to
  1522. // recursively install files and subdirectories safely.
  1523. mode_t required_permissions =
  1524. mode_owner_read | mode_owner_write | mode_owner_execute;
  1525. // If the required permissions are specified it is safe to set the
  1526. // final permissions now. Otherwise we must add the required
  1527. // permissions temporarily during file installation.
  1528. mode_t permissions_before = 0;
  1529. mode_t permissions_after = 0;
  1530. if((permissions & required_permissions) == required_permissions)
  1531. {
  1532. permissions_before = permissions;
  1533. }
  1534. else
  1535. {
  1536. permissions_before = permissions | required_permissions;
  1537. permissions_after = permissions;
  1538. }
  1539. // Set the required permissions of the destination directory.
  1540. if(!this->SetPermissions(destination, permissions_before))
  1541. {
  1542. return false;
  1543. }
  1544. // Load the directory contents to traverse it recursively.
  1545. cmsys::Directory dir;
  1546. if(source && *source)
  1547. {
  1548. dir.Load(source);
  1549. }
  1550. unsigned long numFiles = static_cast<unsigned long>(dir.GetNumberOfFiles());
  1551. for(unsigned long fileNum = 0; fileNum < numFiles; ++fileNum)
  1552. {
  1553. if(!(strcmp(dir.GetFile(fileNum), ".") == 0 ||
  1554. strcmp(dir.GetFile(fileNum), "..") == 0))
  1555. {
  1556. cmsys_stl::string fromPath = source;
  1557. fromPath += "/";
  1558. fromPath += dir.GetFile(fileNum);
  1559. std::string toPath = destination;
  1560. toPath += "/";
  1561. toPath += dir.GetFile(fileNum);
  1562. if(!this->Install(fromPath.c_str(), toPath.c_str()))
  1563. {
  1564. return false;
  1565. }
  1566. }
  1567. }
  1568. // Set the requested permissions of the destination directory.
  1569. return this->SetPermissions(destination, permissions_after);
  1570. }
  1571. //----------------------------------------------------------------------------
  1572. bool cmFileCommand::HandleCopyCommand(std::vector<std::string> const& args)
  1573. {
  1574. cmFileCopier copier(this);
  1575. return copier.Run(args);
  1576. }
  1577. //----------------------------------------------------------------------------
  1578. struct cmFileInstaller: public cmFileCopier
  1579. {
  1580. cmFileInstaller(cmFileCommand* command):
  1581. cmFileCopier(command, "INSTALL"),
  1582. InstallType(cmInstallType_FILES),
  1583. Optional(false),
  1584. DestDirLength(0)
  1585. {
  1586. // Installation does not use source permissions by default.
  1587. this->UseSourcePermissions = false;
  1588. // Check whether to copy files always or only if they have changed.
  1589. this->Always =
  1590. cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_INSTALL_ALWAYS"));
  1591. // Get the current manifest.
  1592. this->Manifest =
  1593. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  1594. }
  1595. ~cmFileInstaller()
  1596. {
  1597. // Save the updated install manifest.
  1598. this->Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  1599. this->Manifest.c_str());
  1600. }
  1601. protected:
  1602. cmInstallType InstallType;
  1603. bool Optional;
  1604. int DestDirLength;
  1605. std::string Rename;
  1606. std::string Manifest;
  1607. void ManifestAppend(std::string const& file)
  1608. {
  1609. this->Manifest += ";";
  1610. this->Manifest += file.substr(this->DestDirLength);
  1611. }
  1612. virtual std::string const& ToName(std::string const& fromName)
  1613. { return this->Rename.empty()? fromName : this->Rename; }
  1614. virtual void ReportCopy(const char* toFile, Type type, bool copy)
  1615. {
  1616. std::string message = (copy? "Installing: " : "Up-to-date: ");
  1617. message += toFile;
  1618. this->Makefile->DisplayStatus(message.c_str(), -1);
  1619. if(type != TypeDir)
  1620. {
  1621. // Add the file to the manifest.
  1622. this->ManifestAppend(toFile);
  1623. }
  1624. }
  1625. virtual bool ReportMissing(const char* fromFile)
  1626. {
  1627. return (this->Optional ||
  1628. this->cmFileCopier::ReportMissing(fromFile));
  1629. }
  1630. virtual bool Install(const char* fromFile, const char* toFile)
  1631. {
  1632. // Support installing from empty source to make a directory.
  1633. if(this->InstallType == cmInstallType_DIRECTORY && !*fromFile)
  1634. {
  1635. return this->InstallDirectory(fromFile, toFile, MatchProperties());
  1636. }
  1637. return this->cmFileCopier::Install(fromFile, toFile);
  1638. }
  1639. virtual bool Parse(std::vector<std::string> const& args);
  1640. enum
  1641. {
  1642. DoingType = DoingLast1,
  1643. DoingRename,
  1644. DoingLast2
  1645. };
  1646. virtual bool CheckKeyword(std::string const& arg);
  1647. virtual bool CheckValue(std::string const& arg);
  1648. virtual void DefaultFilePermissions()
  1649. {
  1650. this->cmFileCopier::DefaultFilePermissions();
  1651. // Add execute permissions based on the target type.
  1652. switch(this->InstallType)
  1653. {
  1654. case cmInstallType_SHARED_LIBRARY:
  1655. case cmInstallType_MODULE_LIBRARY:
  1656. if(this->Makefile->IsOn("CMAKE_INSTALL_SO_NO_EXE"))
  1657. {
  1658. break;
  1659. }
  1660. case cmInstallType_EXECUTABLE:
  1661. case cmInstallType_PROGRAMS:
  1662. this->FilePermissions |= mode_owner_execute;
  1663. this->FilePermissions |= mode_group_execute;
  1664. this->FilePermissions |= mode_world_execute;
  1665. break;
  1666. default: break;
  1667. }
  1668. }
  1669. bool GetTargetTypeFromString(const std::string& stype);
  1670. bool HandleInstallDestination();
  1671. };
  1672. //----------------------------------------------------------------------------
  1673. bool cmFileInstaller::Parse(std::vector<std::string> const& args)
  1674. {
  1675. if(!this->cmFileCopier::Parse(args))
  1676. {
  1677. return false;
  1678. }
  1679. if(!this->Rename.empty())
  1680. {
  1681. if(this->InstallType != cmInstallType_FILES &&
  1682. this->InstallType != cmInstallType_PROGRAMS)
  1683. {
  1684. this->FileCommand->SetError("INSTALL option RENAME may be used "
  1685. "only with FILES or PROGRAMS.");
  1686. return false;
  1687. }
  1688. if(this->Files.size() > 1)
  1689. {
  1690. this->FileCommand->SetError("INSTALL option RENAME may be used "
  1691. "only with one file.");
  1692. return false;
  1693. }
  1694. }
  1695. if(!this->HandleInstallDestination())
  1696. {
  1697. return false;
  1698. }
  1699. return true;
  1700. }
  1701. //----------------------------------------------------------------------------
  1702. bool cmFileInstaller::CheckKeyword(std::string const& arg)
  1703. {
  1704. if(arg == "TYPE")
  1705. {
  1706. if(this->CurrentMatchRule)
  1707. {
  1708. this->NotAfterMatch(arg);
  1709. }
  1710. else
  1711. {
  1712. this->Doing = DoingType;
  1713. }
  1714. }
  1715. else if(arg == "FILES")
  1716. {
  1717. if(this->CurrentMatchRule)
  1718. {
  1719. this->NotAfterMatch(arg);
  1720. }
  1721. else
  1722. {
  1723. this->Doing = DoingFiles;
  1724. }
  1725. }
  1726. else if(arg == "RENAME")
  1727. {
  1728. if(this->CurrentMatchRule)
  1729. {
  1730. this->NotAfterMatch(arg);
  1731. }
  1732. else
  1733. {
  1734. this->Doing = DoingRename;
  1735. }
  1736. }
  1737. else if(arg == "OPTIONAL")
  1738. {
  1739. if(this->CurrentMatchRule)
  1740. {
  1741. this->NotAfterMatch(arg);
  1742. }
  1743. else
  1744. {
  1745. this->Doing = DoingNone;
  1746. this->Optional = true;
  1747. }
  1748. }
  1749. else if(arg == "PERMISSIONS")
  1750. {
  1751. if(this->CurrentMatchRule)
  1752. {
  1753. this->Doing = DoingPermissionsMatch;
  1754. }
  1755. else
  1756. {
  1757. // file(INSTALL) aliases PERMISSIONS to FILE_PERMISSIONS
  1758. this->Doing = DoingPermissionsFile;
  1759. this->UseGivenPermissionsFile = true;
  1760. }
  1761. }
  1762. else if(arg == "DIR_PERMISSIONS")
  1763. {
  1764. if(this->CurrentMatchRule)
  1765. {
  1766. this->NotAfterMatch(arg);
  1767. }
  1768. else
  1769. {
  1770. // file(INSTALL) aliases DIR_PERMISSIONS to DIRECTORY_PERMISSIONS
  1771. this->Doing = DoingPermissionsDir;
  1772. this->UseGivenPermissionsDir = true;
  1773. }
  1774. }
  1775. else if(arg == "COMPONENTS" || arg == "CONFIGURATIONS" ||
  1776. arg == "PROPERTIES")
  1777. {
  1778. cmOStringStream e;
  1779. e << "INSTALL called with old-style " << arg << " argument. "
  1780. << "This script was generated with an older version of CMake. "
  1781. << "Re-run this cmake version on your build tree.";
  1782. this->FileCommand->SetError(e.str().c_str());
  1783. this->Doing = DoingError;
  1784. }
  1785. else
  1786. {
  1787. return this->cmFileCopier::CheckKeyword(arg);
  1788. }
  1789. return true;
  1790. }
  1791. //----------------------------------------------------------------------------
  1792. bool cmFileInstaller::CheckValue(std::string const& arg)
  1793. {
  1794. switch(this->Doing)
  1795. {
  1796. case DoingType:
  1797. if(!this->GetTargetTypeFromString(arg))
  1798. {
  1799. this->Doing = DoingError;
  1800. }
  1801. break;
  1802. case DoingRename:
  1803. this->Rename = arg;
  1804. break;
  1805. default:
  1806. return this->cmFileCopier::CheckValue(arg);
  1807. }
  1808. return true;
  1809. }
  1810. //----------------------------------------------------------------------------
  1811. bool cmFileInstaller
  1812. ::GetTargetTypeFromString(const std::string& stype)
  1813. {
  1814. if ( stype == "EXECUTABLE" )
  1815. {
  1816. this->InstallType = cmInstallType_EXECUTABLE;
  1817. }
  1818. else if ( stype == "FILE" )
  1819. {
  1820. this->InstallType = cmInstallType_FILES;
  1821. }
  1822. else if ( stype == "PROGRAM" )
  1823. {
  1824. this->InstallType = cmInstallType_PROGRAMS;
  1825. }
  1826. else if ( stype == "STATIC_LIBRARY" )
  1827. {
  1828. this->InstallType = cmInstallType_STATIC_LIBRARY;
  1829. }
  1830. else if ( stype == "SHARED_LIBRARY" )
  1831. {
  1832. this->InstallType = cmInstallType_SHARED_LIBRARY;
  1833. }
  1834. else if ( stype == "MODULE" )
  1835. {
  1836. this->InstallType = cmInstallType_MODULE_LIBRARY;
  1837. }
  1838. else if ( stype == "DIRECTORY" )
  1839. {
  1840. this->InstallType = cmInstallType_DIRECTORY;
  1841. }
  1842. else
  1843. {
  1844. cmOStringStream e;
  1845. e << "Option TYPE given uknown value \"" << stype << "\".";
  1846. this->FileCommand->SetError(e.str().c_str());
  1847. return false;
  1848. }
  1849. return true;
  1850. }
  1851. //----------------------------------------------------------------------------
  1852. bool cmFileInstaller::HandleInstallDestination()
  1853. {
  1854. std::string& destination = this->Destination;
  1855. // allow for / to be a valid destination
  1856. if ( destination.size() < 2 && destination != "/" )
  1857. {
  1858. this->FileCommand->SetError("called with inapropriate arguments. "
  1859. "No DESTINATION provided or .");
  1860. return false;
  1861. }
  1862. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  1863. if ( destdir && *destdir )
  1864. {
  1865. std::string sdestdir = destdir;
  1866. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  1867. char ch1 = destination[0];
  1868. char ch2 = destination[1];
  1869. char ch3 = 0;
  1870. if ( destination.size() > 2 )
  1871. {
  1872. ch3 = destination[2];
  1873. }
  1874. int skip = 0;
  1875. if ( ch1 != '/' )
  1876. {
  1877. int relative = 0;
  1878. if (((ch1 >= 'a' && ch1 <= 'z') || (ch1 >= 'A' && ch1 <= 'Z')) &&
  1879. ch2 == ':' )
  1880. {
  1881. // Assume windows
  1882. // let's do some destdir magic:
  1883. skip = 2;
  1884. if ( ch3 != '/' )
  1885. {
  1886. relative = 1;
  1887. }
  1888. }
  1889. else
  1890. {
  1891. relative = 1;
  1892. }
  1893. if ( relative )
  1894. {
  1895. // This is relative path on unix or windows. Since we are doing
  1896. // destdir, this case does not make sense.
  1897. this->FileCommand->SetError(
  1898. "called with relative DESTINATION. This "
  1899. "does not make sense when using DESTDIR. Specify "
  1900. "absolute path or remove DESTDIR environment variable.");
  1901. return false;
  1902. }
  1903. }
  1904. else
  1905. {
  1906. if ( ch2 == '/' )
  1907. {
  1908. // looks like a network path.
  1909. std::string message = "called with network path DESTINATION. This "
  1910. "does not make sense when using DESTDIR. Specify local "
  1911. "absolute path or remove DESTDIR environment variable."
  1912. "\nDESTINATION=\n";
  1913. message += destination;
  1914. this->FileCommand->SetError(message.c_str());
  1915. return false;
  1916. }
  1917. }
  1918. destination = sdestdir + (destination.c_str() + skip);
  1919. this->DestDirLength = int(sdestdir.size());
  1920. }
  1921. if ( !cmSystemTools::FileExists(destination.c_str()) )
  1922. {
  1923. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  1924. {
  1925. std::string errstring = "cannot create directory: " + destination +
  1926. ". Maybe need administrative privileges.";
  1927. this->FileCommand->SetError(errstring.c_str());
  1928. return false;
  1929. }
  1930. }
  1931. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  1932. {
  1933. std::string errstring = "INSTALL destination: " + destination +
  1934. " is not a directory.";
  1935. this->FileCommand->SetError(errstring.c_str());
  1936. return false;
  1937. }
  1938. return true;
  1939. }
  1940. //----------------------------------------------------------------------------
  1941. bool
  1942. cmFileCommand::HandleRPathChangeCommand(std::vector<std::string> const& args)
  1943. {
  1944. // Evaluate arguments.
  1945. const char* file = 0;
  1946. const char* oldRPath = 0;
  1947. const char* newRPath = 0;
  1948. enum Doing { DoingNone, DoingFile, DoingOld, DoingNew };
  1949. Doing doing = DoingNone;
  1950. for(unsigned int i=1; i < args.size(); ++i)
  1951. {
  1952. if(args[i] == "OLD_RPATH")
  1953. {
  1954. doing = DoingOld;
  1955. }
  1956. else if(args[i] == "NEW_RPATH")
  1957. {
  1958. doing = DoingNew;
  1959. }
  1960. else if(args[i] == "FILE")
  1961. {
  1962. doing = DoingFile;
  1963. }
  1964. else if(doing == DoingFile)
  1965. {
  1966. file = args[i].c_str();
  1967. doing = DoingNone;
  1968. }
  1969. else if(doing == DoingOld)
  1970. {
  1971. oldRPath = args[i].c_str();
  1972. doing = DoingNone;
  1973. }
  1974. else if(doing == DoingNew)
  1975. {
  1976. newRPath = args[i].c_str();
  1977. doing = DoingNone;
  1978. }
  1979. else
  1980. {
  1981. cmOStringStream e;
  1982. e << "RPATH_CHANGE given unknown argument " << args[i];
  1983. this->SetError(e.str().c_str());
  1984. return false;
  1985. }
  1986. }
  1987. if(!file)
  1988. {
  1989. this->SetError("RPATH_CHANGE not given FILE option.");
  1990. return false;
  1991. }
  1992. if(!oldRPath)
  1993. {
  1994. this->SetError("RPATH_CHANGE not given OLD_RPATH option.");
  1995. return false;
  1996. }
  1997. if(!newRPath)
  1998. {
  1999. this->SetError("RPATH_CHANGE not given NEW_RPATH option.");
  2000. return false;
  2001. }
  2002. if(!cmSystemTools::FileExists(file, true))
  2003. {
  2004. cmOStringStream e;
  2005. e << "RPATH_CHANGE given FILE \"" << file << "\" that does not exist.";
  2006. this->SetError(e.str().c_str());
  2007. return false;
  2008. }
  2009. bool success = true;
  2010. cmSystemToolsFileTime* ft = cmSystemTools::FileTimeNew();
  2011. bool have_ft = cmSystemTools::FileTimeGet(file, ft);
  2012. std::string emsg;
  2013. bool changed;
  2014. if(!cmSystemTools::ChangeRPath(file, oldRPath, newRPath, &emsg, &changed))
  2015. {
  2016. cmOStringStream e;
  2017. e << "RPATH_CHANGE could not write new RPATH:\n"
  2018. << " " << newRPath << "\n"
  2019. << "to the file:\n"
  2020. << " " << file << "\n"
  2021. << emsg;
  2022. this->SetError(e.str().c_str());
  2023. success = false;
  2024. }
  2025. if(success)
  2026. {
  2027. if(changed)
  2028. {
  2029. std::string message = "Set runtime path of \"";
  2030. message += file;
  2031. message += "\" to \"";
  2032. message += newRPath;
  2033. message += "\"";
  2034. this->Makefile->DisplayStatus(message.c_str(), -1);
  2035. }
  2036. if(have_ft)
  2037. {
  2038. cmSystemTools::FileTimeSet(file, ft);
  2039. }
  2040. }
  2041. cmSystemTools::FileTimeDelete(ft);
  2042. return success;
  2043. }
  2044. //----------------------------------------------------------------------------
  2045. bool
  2046. cmFileCommand::HandleRPathRemoveCommand(std::vector<std::string> const& args)
  2047. {
  2048. // Evaluate arguments.
  2049. const char* file = 0;
  2050. enum Doing { DoingNone, DoingFile };
  2051. Doing doing = DoingNone;
  2052. for(unsigned int i=1; i < args.size(); ++i)
  2053. {
  2054. if(args[i] == "FILE")
  2055. {
  2056. doing = DoingFile;
  2057. }
  2058. else if(doing == DoingFile)
  2059. {
  2060. file = args[i].c_str();
  2061. doing = DoingNone;
  2062. }
  2063. else
  2064. {
  2065. cmOStringStream e;
  2066. e << "RPATH_REMOVE given unknown argument " << args[i];
  2067. this->SetError(e.str().c_str());
  2068. return false;
  2069. }
  2070. }
  2071. if(!file)
  2072. {
  2073. this->SetError("RPATH_REMOVE not given FILE option.");
  2074. return false;
  2075. }
  2076. if(!cmSystemTools::FileExists(file, true))
  2077. {
  2078. cmOStringStream e;
  2079. e << "RPATH_REMOVE given FILE \"" << file << "\" that does not exist.";
  2080. this->SetError(e.str().c_str());
  2081. return false;
  2082. }
  2083. bool success = true;
  2084. cmSystemToolsFileTime* ft = cmSystemTools::FileTimeNew();
  2085. bool have_ft = cmSystemTools::FileTimeGet(file, ft);
  2086. std::string emsg;
  2087. bool removed;
  2088. if(!cmSystemTools::RemoveRPath(file, &emsg, &removed))
  2089. {
  2090. cmOStringStream e;
  2091. e << "RPATH_REMOVE could not remove RPATH from file:\n"
  2092. << " " << file << "\n"
  2093. << emsg;
  2094. this->SetError(e.str().c_str());
  2095. success = false;
  2096. }
  2097. if(success)
  2098. {
  2099. if(removed)
  2100. {
  2101. std::string message = "Removed runtime path from \"";
  2102. message += file;
  2103. message += "\"";
  2104. this->Makefile->DisplayStatus(message.c_str(), -1);
  2105. }
  2106. if(have_ft)
  2107. {
  2108. cmSystemTools::FileTimeSet(file, ft);
  2109. }
  2110. }
  2111. cmSystemTools::FileTimeDelete(ft);
  2112. return success;
  2113. }
  2114. //----------------------------------------------------------------------------
  2115. bool
  2116. cmFileCommand::HandleRPathCheckCommand(std::vector<std::string> const& args)
  2117. {
  2118. // Evaluate arguments.
  2119. const char* file = 0;
  2120. const char* rpath = 0;
  2121. enum Doing { DoingNone, DoingFile, DoingRPath };
  2122. Doing doing = DoingNone;
  2123. for(unsigned int i=1; i < args.size(); ++i)
  2124. {
  2125. if(args[i] == "RPATH")
  2126. {
  2127. doing = DoingRPath;
  2128. }
  2129. else if(args[i] == "FILE")
  2130. {
  2131. doing = DoingFile;
  2132. }
  2133. else if(doing == DoingFile)
  2134. {
  2135. file = args[i].c_str();
  2136. doing = DoingNone;
  2137. }
  2138. else if(doing == DoingRPath)
  2139. {
  2140. rpath = args[i].c_str();
  2141. doing = DoingNone;
  2142. }
  2143. else
  2144. {
  2145. cmOStringStream e;
  2146. e << "RPATH_CHECK given unknown argument " << args[i];
  2147. this->SetError(e.str().c_str());
  2148. return false;
  2149. }
  2150. }
  2151. if(!file)
  2152. {
  2153. this->SetError("RPATH_CHECK not given FILE option.");
  2154. return false;
  2155. }
  2156. if(!rpath)
  2157. {
  2158. this->SetError("RPATH_CHECK not given RPATH option.");
  2159. return false;
  2160. }
  2161. // If the file exists but does not have the desired RPath then
  2162. // delete it. This is used during installation to re-install a file
  2163. // if its RPath will change.
  2164. if(cmSystemTools::FileExists(file, true) &&
  2165. !cmSystemTools::CheckRPath(file, rpath))
  2166. {
  2167. cmSystemTools::RemoveFile(file);
  2168. }
  2169. return true;
  2170. }
  2171. //----------------------------------------------------------------------------
  2172. bool cmFileCommand::HandleInstallCommand(std::vector<std::string> const& args)
  2173. {
  2174. cmFileInstaller installer(this);
  2175. return installer.Run(args);
  2176. }
  2177. //----------------------------------------------------------------------------
  2178. bool cmFileCommand::HandleRelativePathCommand(
  2179. std::vector<std::string> const& args)
  2180. {
  2181. if(args.size() != 4 )
  2182. {
  2183. this->SetError("RELATIVE_PATH called with incorrect number of arguments");
  2184. return false;
  2185. }
  2186. const std::string& outVar = args[1];
  2187. const std::string& directoryName = args[2];
  2188. const std::string& fileName = args[3];
  2189. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  2190. {
  2191. std::string errstring =
  2192. "RELATIVE_PATH must be passed a full path to the directory: "
  2193. + directoryName;
  2194. this->SetError(errstring.c_str());
  2195. return false;
  2196. }
  2197. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  2198. {
  2199. std::string errstring =
  2200. "RELATIVE_PATH must be passed a full path to the file: "
  2201. + fileName;
  2202. this->SetError(errstring.c_str());
  2203. return false;
  2204. }
  2205. std::string res = cmSystemTools::RelativePath(directoryName.c_str(),
  2206. fileName.c_str());
  2207. this->Makefile->AddDefinition(outVar.c_str(),
  2208. res.c_str());
  2209. return true;
  2210. }
  2211. //----------------------------------------------------------------------------
  2212. bool cmFileCommand::HandleRename(std::vector<std::string> const& args)
  2213. {
  2214. if(args.size() != 3)
  2215. {
  2216. this->SetError("RENAME given incorrect number of arguments.");
  2217. return false;
  2218. }
  2219. // Compute full path for old and new names.
  2220. std::string oldname = args[1];
  2221. if(!cmsys::SystemTools::FileIsFullPath(oldname.c_str()))
  2222. {
  2223. oldname = this->Makefile->GetCurrentDirectory();
  2224. oldname += "/" + args[1];
  2225. }
  2226. std::string newname = args[2];
  2227. if(!cmsys::SystemTools::FileIsFullPath(newname.c_str()))
  2228. {
  2229. newname = this->Makefile->GetCurrentDirectory();
  2230. newname += "/" + args[2];
  2231. }
  2232. if(!cmSystemTools::RenameFile(oldname.c_str(), newname.c_str()))
  2233. {
  2234. std::string err = cmSystemTools::GetLastSystemError();
  2235. cmOStringStream e;
  2236. e << "RENAME failed to rename\n"
  2237. << " " << oldname << "\n"
  2238. << "to\n"
  2239. << " " << newname << "\n"
  2240. << "because: " << err << "\n";
  2241. this->SetError(e.str().c_str());
  2242. return false;
  2243. }
  2244. return true;
  2245. }
  2246. //----------------------------------------------------------------------------
  2247. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  2248. bool recurse)
  2249. {
  2250. std::string message;
  2251. std::vector<std::string>::const_iterator i = args.begin();
  2252. i++; // Get rid of subcommand
  2253. for(;i != args.end(); ++i)
  2254. {
  2255. std::string fileName = *i;
  2256. if(!cmsys::SystemTools::FileIsFullPath(fileName.c_str()))
  2257. {
  2258. fileName = this->Makefile->GetCurrentDirectory();
  2259. fileName += "/" + *i;
  2260. }
  2261. if(cmSystemTools::FileIsDirectory(fileName.c_str()) && recurse)
  2262. {
  2263. cmSystemTools::RemoveADirectory(fileName.c_str());
  2264. }
  2265. else
  2266. {
  2267. cmSystemTools::RemoveFile(fileName.c_str());
  2268. }
  2269. }
  2270. return true;
  2271. }
  2272. //----------------------------------------------------------------------------
  2273. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  2274. const& args,
  2275. bool nativePath)
  2276. {
  2277. std::vector<std::string>::const_iterator i = args.begin();
  2278. if(args.size() != 3)
  2279. {
  2280. this->SetError("FILE([TO_CMAKE_PATH|TO_NATIVE_PATH] path result) must be "
  2281. "called with exactly three arguments.");
  2282. return false;
  2283. }
  2284. i++; // Get rid of subcommand
  2285. #if defined(_WIN32) && !defined(__CYGWIN__)
  2286. char pathSep = ';';
  2287. #else
  2288. char pathSep = ':';
  2289. #endif
  2290. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  2291. pathSep);
  2292. i++;
  2293. const char* var = i->c_str();
  2294. std::string value;
  2295. for(std::vector<cmsys::String>::iterator j = path.begin();
  2296. j != path.end(); ++j)
  2297. {
  2298. if(j != path.begin())
  2299. {
  2300. value += ";";
  2301. }
  2302. if(!nativePath)
  2303. {
  2304. cmSystemTools::ConvertToUnixSlashes(*j);
  2305. }
  2306. else
  2307. {
  2308. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  2309. // remove double quotes in the path
  2310. cmsys::String& s = *j;
  2311. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  2312. {
  2313. s = s.substr(1,s.size()-2);
  2314. }
  2315. }
  2316. value += *j;
  2317. }
  2318. this->Makefile->AddDefinition(var, value.c_str());
  2319. return true;
  2320. }
  2321. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2322. // Stuff for curl download/upload
  2323. typedef std::vector<char> cmFileCommandVectorOfChar;
  2324. namespace {
  2325. size_t
  2326. cmWriteToFileCallback(void *ptr, size_t size, size_t nmemb,
  2327. void *data)
  2328. {
  2329. register int realsize = (int)(size * nmemb);
  2330. std::ofstream* fout = static_cast<std::ofstream*>(data);
  2331. const char* chPtr = static_cast<char*>(ptr);
  2332. fout->write(chPtr, realsize);
  2333. return realsize;
  2334. }
  2335. size_t
  2336. cmWriteToMemoryCallback(void *ptr, size_t size, size_t nmemb,
  2337. void *data)
  2338. {
  2339. register int realsize = (int)(size * nmemb);
  2340. cmFileCommandVectorOfChar *vec
  2341. = static_cast<cmFileCommandVectorOfChar*>(data);
  2342. const char* chPtr = static_cast<char*>(ptr);
  2343. vec->insert(vec->end(), chPtr, chPtr + realsize);
  2344. return realsize;
  2345. }
  2346. static size_t
  2347. cmFileCommandCurlDebugCallback(CURL *, curl_infotype, char *chPtr,
  2348. size_t size, void *data)
  2349. {
  2350. cmFileCommandVectorOfChar *vec
  2351. = static_cast<cmFileCommandVectorOfChar*>(data);
  2352. vec->insert(vec->end(), chPtr, chPtr + size);
  2353. return size;
  2354. }
  2355. class cURLProgressHelper
  2356. {
  2357. public:
  2358. cURLProgressHelper(cmFileCommand *fc, const char *text)
  2359. {
  2360. this->CurrentPercentage = -1;
  2361. this->FileCommand = fc;
  2362. this->Text = text;
  2363. }
  2364. bool UpdatePercentage(double value, double total, std::string &status)
  2365. {
  2366. int OldPercentage = this->CurrentPercentage;
  2367. if (total > 0.0)
  2368. {
  2369. this->CurrentPercentage = static_cast<int>(value/total*100.0 + 0.5);
  2370. }
  2371. bool updated = (OldPercentage != this->CurrentPercentage);
  2372. if (updated)
  2373. {
  2374. cmOStringStream oss;
  2375. oss << "[" << this->Text << " " << this->CurrentPercentage
  2376. << "% complete]";
  2377. status = oss.str();
  2378. }
  2379. return updated;
  2380. }
  2381. cmFileCommand *GetFileCommand()
  2382. {
  2383. return this->FileCommand;
  2384. }
  2385. private:
  2386. int CurrentPercentage;
  2387. cmFileCommand *FileCommand;
  2388. std::string Text;
  2389. };
  2390. static int
  2391. cmFileDownloadProgressCallback(void *clientp,
  2392. double dltotal, double dlnow,
  2393. double ultotal, double ulnow)
  2394. {
  2395. cURLProgressHelper *helper =
  2396. reinterpret_cast<cURLProgressHelper *>(clientp);
  2397. static_cast<void>(ultotal);
  2398. static_cast<void>(ulnow);
  2399. std::string status;
  2400. if (helper->UpdatePercentage(dlnow, dltotal, status))
  2401. {
  2402. cmFileCommand *fc = helper->GetFileCommand();
  2403. cmMakefile *mf = fc->GetMakefile();
  2404. mf->DisplayStatus(status.c_str(), -1);
  2405. }
  2406. return 0;
  2407. }
  2408. static int
  2409. cmFileUploadProgressCallback(void *clientp,
  2410. double dltotal, double dlnow,
  2411. double ultotal, double ulnow)
  2412. {
  2413. cURLProgressHelper *helper =
  2414. reinterpret_cast<cURLProgressHelper *>(clientp);
  2415. static_cast<void>(dltotal);
  2416. static_cast<void>(dlnow);
  2417. std::string status;
  2418. if (helper->UpdatePercentage(ulnow, ultotal, status))
  2419. {
  2420. cmFileCommand *fc = helper->GetFileCommand();
  2421. cmMakefile *mf = fc->GetMakefile();
  2422. mf->DisplayStatus(status.c_str(), -1);
  2423. }
  2424. return 0;
  2425. }
  2426. }
  2427. namespace {
  2428. class cURLEasyGuard
  2429. {
  2430. public:
  2431. cURLEasyGuard(CURL * easy)
  2432. : Easy(easy)
  2433. {}
  2434. ~cURLEasyGuard(void)
  2435. {
  2436. if (this->Easy)
  2437. {
  2438. ::curl_easy_cleanup(this->Easy);
  2439. }
  2440. }
  2441. inline void release(void)
  2442. {
  2443. this->Easy = 0;
  2444. return;
  2445. }
  2446. private:
  2447. ::CURL * Easy;
  2448. };
  2449. }
  2450. #endif
  2451. #define check_curl_result(result, errstr) \
  2452. if (result != CURLE_OK) \
  2453. { \
  2454. std::string e(errstr); \
  2455. e += ::curl_easy_strerror(result); \
  2456. this->SetError(e.c_str()); \
  2457. return false; \
  2458. }
  2459. bool
  2460. cmFileCommand::HandleDownloadCommand(std::vector<std::string> const& args)
  2461. {
  2462. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2463. std::vector<std::string>::const_iterator i = args.begin();
  2464. if(args.size() < 3)
  2465. {
  2466. this->SetError("DOWNLOAD must be called with at least three arguments.");
  2467. return false;
  2468. }
  2469. ++i; // Get rid of subcommand
  2470. std::string url = *i;
  2471. ++i;
  2472. std::string file = *i;
  2473. ++i;
  2474. long timeout = 0;
  2475. long inactivity_timeout = 0;
  2476. std::string verboseLog;
  2477. std::string statusVar;
  2478. std::string caFile;
  2479. bool checkSSL = false;
  2480. bool verifySSL = false;
  2481. std::string expectedHash;
  2482. std::string hashMatchMSG;
  2483. cmsys::auto_ptr<cmCryptoHash> hash;
  2484. bool showProgress = false;
  2485. while(i != args.end())
  2486. {
  2487. if(*i == "TIMEOUT")
  2488. {
  2489. ++i;
  2490. if(i != args.end())
  2491. {
  2492. timeout = atol(i->c_str());
  2493. }
  2494. else
  2495. {
  2496. this->SetError("DOWNLOAD missing time for TIMEOUT.");
  2497. return false;
  2498. }
  2499. }
  2500. else if(*i == "INACTIVITY_TIMEOUT")
  2501. {
  2502. ++i;
  2503. if(i != args.end())
  2504. {
  2505. inactivity_timeout = atol(i->c_str());
  2506. }
  2507. else
  2508. {
  2509. this->SetError("DOWNLOAD missing time for INACTIVITY_TIMEOUT.");
  2510. return false;
  2511. }
  2512. }
  2513. else if(*i == "LOG")
  2514. {
  2515. ++i;
  2516. if( i == args.end())
  2517. {
  2518. this->SetError("DOWNLOAD missing VAR for LOG.");
  2519. return false;
  2520. }
  2521. verboseLog = *i;
  2522. }
  2523. else if(*i == "STATUS")
  2524. {
  2525. ++i;
  2526. if( i == args.end())
  2527. {
  2528. this->SetError("DOWNLOAD missing VAR for STATUS.");
  2529. return false;
  2530. }
  2531. statusVar = *i;
  2532. }
  2533. else if(*i == "SSL_VERIFY")
  2534. {
  2535. ++i;
  2536. if(i != args.end())
  2537. {
  2538. verifySSL = cmSystemTools::IsOn(i->c_str());
  2539. checkSSL = true;
  2540. }
  2541. else
  2542. {
  2543. this->SetError("SSL_VERIFY missing bool value.");
  2544. return false;
  2545. }
  2546. }
  2547. else if(*i == "SSL_CAINFO_FILE")
  2548. {
  2549. ++i;
  2550. if(i != args.end())
  2551. {
  2552. caFile = *i;
  2553. }
  2554. else
  2555. {
  2556. this->SetError("SSL_CAFILE missing file value.");
  2557. return false;
  2558. }
  2559. }
  2560. else if(*i == "EXPECTED_MD5")
  2561. {
  2562. ++i;
  2563. if( i == args.end())
  2564. {
  2565. this->SetError("DOWNLOAD missing sum value for EXPECTED_MD5.");
  2566. return false;
  2567. }
  2568. hash = cmsys::auto_ptr<cmCryptoHash>(cmCryptoHash::New("MD5"));
  2569. hashMatchMSG = "MD5 sum";
  2570. expectedHash = cmSystemTools::LowerCase(*i);
  2571. }
  2572. else if(*i == "SHOW_PROGRESS")
  2573. {
  2574. showProgress = true;
  2575. }
  2576. else if(*i == "EXPECTED_HASH")
  2577. {
  2578. ++i;
  2579. if(i != args.end())
  2580. {
  2581. hash = cmsys::auto_ptr<cmCryptoHash>(cmCryptoHash::New(i->c_str()));
  2582. if(!hash.get())
  2583. {
  2584. std::string err = "DOWNLOAD bad SHA type: ";
  2585. err += *i;
  2586. this->SetError(err.c_str());
  2587. return false;
  2588. }
  2589. hashMatchMSG = *i;
  2590. hashMatchMSG += " hash";
  2591. ++i;
  2592. }
  2593. if(i != args.end())
  2594. {
  2595. expectedHash = cmSystemTools::LowerCase(*i);
  2596. }
  2597. else
  2598. {
  2599. this->SetError("DOWNLOAD missing time for EXPECTED_HASH.");
  2600. return false;
  2601. }
  2602. }
  2603. ++i;
  2604. }
  2605. // If file exists already, and caller specified an expected md5 or sha,
  2606. // and the existing file already has the expected hash, then simply
  2607. // return.
  2608. //
  2609. if(cmSystemTools::FileExists(file.c_str()) && hash.get())
  2610. {
  2611. std::string msg;
  2612. std::string actualHash = hash->HashFile(file.c_str());
  2613. if(actualHash == expectedHash)
  2614. {
  2615. msg = "returning early; file already exists with expected ";
  2616. msg += hashMatchMSG;
  2617. msg += "\"";
  2618. if(statusVar.size())
  2619. {
  2620. cmOStringStream result;
  2621. result << (int)0 << ";\"" << msg;
  2622. this->Makefile->AddDefinition(statusVar.c_str(),
  2623. result.str().c_str());
  2624. }
  2625. return true;
  2626. }
  2627. }
  2628. // Make sure parent directory exists so we can write to the file
  2629. // as we receive downloaded bits from curl...
  2630. //
  2631. std::string dir = cmSystemTools::GetFilenamePath(file.c_str());
  2632. if(!cmSystemTools::FileExists(dir.c_str()) &&
  2633. !cmSystemTools::MakeDirectory(dir.c_str()))
  2634. {
  2635. std::string errstring = "DOWNLOAD error: cannot create directory '"
  2636. + dir + "' - Specify file by full path name and verify that you "
  2637. "have directory creation and file write privileges.";
  2638. this->SetError(errstring.c_str());
  2639. return false;
  2640. }
  2641. std::ofstream fout(file.c_str(), std::ios::binary);
  2642. if(!fout)
  2643. {
  2644. this->SetError("DOWNLOAD cannot open file for write.");
  2645. return false;
  2646. }
  2647. ::CURL *curl;
  2648. ::curl_global_init(CURL_GLOBAL_DEFAULT);
  2649. curl = ::curl_easy_init();
  2650. if(!curl)
  2651. {
  2652. this->SetError("DOWNLOAD error initializing curl.");
  2653. return false;
  2654. }
  2655. cURLEasyGuard g_curl(curl);
  2656. ::CURLcode res = ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  2657. check_curl_result(res, "DOWNLOAD cannot set url: ");
  2658. // enable HTTP ERROR parsing
  2659. res = ::curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  2660. check_curl_result(res, "DOWNLOAD cannot set http failure option: ");
  2661. res = ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  2662. cmWriteToFileCallback);
  2663. check_curl_result(res, "DOWNLOAD cannot set write function: ");
  2664. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  2665. cmFileCommandCurlDebugCallback);
  2666. check_curl_result(res, "DOWNLOAD cannot set debug function: ");
  2667. // check to see if SSL verification is requested
  2668. const char* verifyValue =
  2669. this->Makefile->GetDefinition("CMAKE_CURLOPT_SSL_VERIFYPEER");
  2670. // if there is a cmake variable or if the command has SSL_VERIFY requested
  2671. if(verifyValue || checkSSL)
  2672. {
  2673. // the args to the command come first
  2674. bool verify = verifySSL;
  2675. if(!verify && verifyValue)
  2676. {
  2677. verify = cmSystemTools::IsOn(verifyValue);
  2678. }
  2679. if(verify)
  2680. {
  2681. res = ::curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
  2682. check_curl_result(res, "Unable to set SSL Verify on: ");
  2683. }
  2684. else
  2685. {
  2686. res = ::curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  2687. check_curl_result(res, "Unable to set SSL Verify off: ");
  2688. }
  2689. }
  2690. // check to see if a CAINFO file has been specified
  2691. const char* cainfo =
  2692. this->Makefile->GetDefinition("CMAKE_CURLOPT_CAINFO_FILE");
  2693. // command arg comes first
  2694. if(caFile.size())
  2695. {
  2696. cainfo = caFile.c_str();
  2697. }
  2698. if(cainfo)
  2699. {
  2700. res = ::curl_easy_setopt(curl, CURLOPT_CAINFO, cainfo);
  2701. check_curl_result(res, "Unable to set SSL Verify CAINFO: ");
  2702. }
  2703. cmFileCommandVectorOfChar chunkDebug;
  2704. res = ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&fout);
  2705. check_curl_result(res, "DOWNLOAD cannot set write data: ");
  2706. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void *)&chunkDebug);
  2707. check_curl_result(res, "DOWNLOAD cannot set debug data: ");
  2708. res = ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  2709. check_curl_result(res, "DOWNLOAD cannot set follow-redirect option: ");
  2710. if(verboseLog.size())
  2711. {
  2712. res = ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  2713. check_curl_result(res, "DOWNLOAD cannot set verbose: ");
  2714. }
  2715. if(timeout > 0)
  2716. {
  2717. res = ::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout );
  2718. check_curl_result(res, "DOWNLOAD cannot set timeout: ");
  2719. }
  2720. if(inactivity_timeout > 0)
  2721. {
  2722. // Give up if there is no progress for a long time.
  2723. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  2724. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, inactivity_timeout);
  2725. }
  2726. // Need the progress helper's scope to last through the duration of
  2727. // the curl_easy_perform call... so this object is declared at function
  2728. // scope intentionally, rather than inside the "if(showProgress)"
  2729. // block...
  2730. //
  2731. cURLProgressHelper helper(this, "download");
  2732. if(showProgress)
  2733. {
  2734. res = ::curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
  2735. check_curl_result(res, "DOWNLOAD cannot set noprogress value: ");
  2736. res = ::curl_easy_setopt(curl,
  2737. CURLOPT_PROGRESSFUNCTION, cmFileDownloadProgressCallback);
  2738. check_curl_result(res, "DOWNLOAD cannot set progress function: ");
  2739. res = ::curl_easy_setopt(curl,
  2740. CURLOPT_PROGRESSDATA, reinterpret_cast<void*>(&helper));
  2741. check_curl_result(res, "DOWNLOAD cannot set progress data: ");
  2742. }
  2743. res = ::curl_easy_perform(curl);
  2744. /* always cleanup */
  2745. g_curl.release();
  2746. ::curl_easy_cleanup(curl);
  2747. if(statusVar.size())
  2748. {
  2749. cmOStringStream result;
  2750. result << (int)res << ";\"" << ::curl_easy_strerror(res) << "\"";
  2751. this->Makefile->AddDefinition(statusVar.c_str(),
  2752. result.str().c_str());
  2753. }
  2754. ::curl_global_cleanup();
  2755. // Explicitly flush/close so we can measure the md5 accurately.
  2756. //
  2757. fout.flush();
  2758. fout.close();
  2759. // Verify MD5 sum if requested:
  2760. //
  2761. if (hash.get())
  2762. {
  2763. std::string actualHash = hash->HashFile(file.c_str());
  2764. if (actualHash.size() == 0)
  2765. {
  2766. this->SetError("DOWNLOAD cannot compute hash on downloaded file");
  2767. return false;
  2768. }
  2769. if (expectedHash != actualHash)
  2770. {
  2771. cmOStringStream oss;
  2772. oss << "DOWNLOAD HASH mismatch" << std::endl
  2773. << " for file: [" << file << "]" << std::endl
  2774. << " expected hash: [" << expectedHash << "]" << std::endl
  2775. << " actual hash: [" << actualHash << "]" << std::endl
  2776. ;
  2777. this->SetError(oss.str().c_str());
  2778. return false;
  2779. }
  2780. }
  2781. if(chunkDebug.size())
  2782. {
  2783. chunkDebug.push_back(0);
  2784. if(CURLE_OPERATION_TIMEOUTED == res)
  2785. {
  2786. std::string output = &*chunkDebug.begin();
  2787. if(verboseLog.size())
  2788. {
  2789. this->Makefile->AddDefinition(verboseLog.c_str(),
  2790. &*chunkDebug.begin());
  2791. }
  2792. }
  2793. this->Makefile->AddDefinition(verboseLog.c_str(),
  2794. &*chunkDebug.begin());
  2795. }
  2796. return true;
  2797. #else
  2798. this->SetError("DOWNLOAD not supported by bootstrap cmake.");
  2799. return false;
  2800. #endif
  2801. }
  2802. bool
  2803. cmFileCommand::HandleUploadCommand(std::vector<std::string> const& args)
  2804. {
  2805. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2806. if(args.size() < 3)
  2807. {
  2808. this->SetError("UPLOAD must be called with at least three arguments.");
  2809. return false;
  2810. }
  2811. std::vector<std::string>::const_iterator i = args.begin();
  2812. ++i;
  2813. std::string filename = *i;
  2814. ++i;
  2815. std::string url = *i;
  2816. ++i;
  2817. long timeout = 0;
  2818. long inactivity_timeout = 0;
  2819. std::string logVar;
  2820. std::string statusVar;
  2821. bool showProgress = false;
  2822. while(i != args.end())
  2823. {
  2824. if(*i == "TIMEOUT")
  2825. {
  2826. ++i;
  2827. if(i != args.end())
  2828. {
  2829. timeout = atol(i->c_str());
  2830. }
  2831. else
  2832. {
  2833. this->SetError("UPLOAD missing time for TIMEOUT.");
  2834. return false;
  2835. }
  2836. }
  2837. else if(*i == "INACTIVITY_TIMEOUT")
  2838. {
  2839. ++i;
  2840. if(i != args.end())
  2841. {
  2842. inactivity_timeout = atol(i->c_str());
  2843. }
  2844. else
  2845. {
  2846. this->SetError("UPLOAD missing time for INACTIVITY_TIMEOUT.");
  2847. return false;
  2848. }
  2849. }
  2850. else if(*i == "LOG")
  2851. {
  2852. ++i;
  2853. if( i == args.end())
  2854. {
  2855. this->SetError("UPLOAD missing VAR for LOG.");
  2856. return false;
  2857. }
  2858. logVar = *i;
  2859. }
  2860. else if(*i == "STATUS")
  2861. {
  2862. ++i;
  2863. if( i == args.end())
  2864. {
  2865. this->SetError("UPLOAD missing VAR for STATUS.");
  2866. return false;
  2867. }
  2868. statusVar = *i;
  2869. }
  2870. else if(*i == "SHOW_PROGRESS")
  2871. {
  2872. showProgress = true;
  2873. }
  2874. ++i;
  2875. }
  2876. // Open file for reading:
  2877. //
  2878. FILE *fin = fopen(filename.c_str(), "rb");
  2879. if(!fin)
  2880. {
  2881. std::string errStr = "UPLOAD cannot open file '";
  2882. errStr += filename + "' for reading.";
  2883. this->SetError(errStr.c_str());
  2884. return false;
  2885. }
  2886. struct stat st;
  2887. if(::stat(filename.c_str(), &st))
  2888. {
  2889. std::string errStr = "UPLOAD cannot stat file '";
  2890. errStr += filename + "'.";
  2891. this->SetError(errStr.c_str());
  2892. fclose(fin);
  2893. return false;
  2894. }
  2895. ::CURL *curl;
  2896. ::curl_global_init(CURL_GLOBAL_DEFAULT);
  2897. curl = ::curl_easy_init();
  2898. if(!curl)
  2899. {
  2900. this->SetError("UPLOAD error initializing curl.");
  2901. fclose(fin);
  2902. return false;
  2903. }
  2904. cURLEasyGuard g_curl(curl);
  2905. // enable HTTP ERROR parsing
  2906. ::CURLcode res = ::curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  2907. // enable uploading
  2908. res = ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
  2909. check_curl_result(res, "UPLOAD cannot set upload flag: ");
  2910. res = ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  2911. check_curl_result(res, "UPLOAD cannot set url: ");
  2912. res = ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  2913. cmWriteToMemoryCallback);
  2914. check_curl_result(res, "UPLOAD cannot set write function: ");
  2915. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  2916. cmFileCommandCurlDebugCallback);
  2917. check_curl_result(res, "UPLOAD cannot set debug function: ");
  2918. cmFileCommandVectorOfChar chunkResponse;
  2919. cmFileCommandVectorOfChar chunkDebug;
  2920. res = ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunkResponse);
  2921. check_curl_result(res, "UPLOAD cannot set write data: ");
  2922. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void *)&chunkDebug);
  2923. check_curl_result(res, "UPLOAD cannot set debug data: ");
  2924. res = ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  2925. check_curl_result(res, "UPLOAD cannot set follow-redirect option: ");
  2926. if(logVar.size())
  2927. {
  2928. res = ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  2929. check_curl_result(res, "UPLOAD cannot set verbose: ");
  2930. }
  2931. if(timeout > 0)
  2932. {
  2933. res = ::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout );
  2934. check_curl_result(res, "UPLOAD cannot set timeout: ");
  2935. }
  2936. if(inactivity_timeout > 0)
  2937. {
  2938. // Give up if there is no progress for a long time.
  2939. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  2940. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, inactivity_timeout);
  2941. }
  2942. // Need the progress helper's scope to last through the duration of
  2943. // the curl_easy_perform call... so this object is declared at function
  2944. // scope intentionally, rather than inside the "if(showProgress)"
  2945. // block...
  2946. //
  2947. cURLProgressHelper helper(this, "upload");
  2948. if(showProgress)
  2949. {
  2950. res = ::curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
  2951. check_curl_result(res, "UPLOAD cannot set noprogress value: ");
  2952. res = ::curl_easy_setopt(curl,
  2953. CURLOPT_PROGRESSFUNCTION, cmFileUploadProgressCallback);
  2954. check_curl_result(res, "UPLOAD cannot set progress function: ");
  2955. res = ::curl_easy_setopt(curl,
  2956. CURLOPT_PROGRESSDATA, reinterpret_cast<void*>(&helper));
  2957. check_curl_result(res, "UPLOAD cannot set progress data: ");
  2958. }
  2959. // now specify which file to upload
  2960. res = ::curl_easy_setopt(curl, CURLOPT_INFILE, fin);
  2961. check_curl_result(res, "UPLOAD cannot set input file: ");
  2962. // and give the size of the upload (optional)
  2963. res = ::curl_easy_setopt(curl,
  2964. CURLOPT_INFILESIZE, static_cast<long>(st.st_size));
  2965. check_curl_result(res, "UPLOAD cannot set input file size: ");
  2966. res = ::curl_easy_perform(curl);
  2967. /* always cleanup */
  2968. g_curl.release();
  2969. ::curl_easy_cleanup(curl);
  2970. if(statusVar.size())
  2971. {
  2972. cmOStringStream result;
  2973. result << (int)res << ";\"" << ::curl_easy_strerror(res) << "\"";
  2974. this->Makefile->AddDefinition(statusVar.c_str(),
  2975. result.str().c_str());
  2976. }
  2977. ::curl_global_cleanup();
  2978. fclose(fin);
  2979. fin = NULL;
  2980. if(logVar.size())
  2981. {
  2982. std::string log;
  2983. if(chunkResponse.size())
  2984. {
  2985. chunkResponse.push_back(0);
  2986. log += "Response:\n";
  2987. log += &*chunkResponse.begin();
  2988. log += "\n";
  2989. }
  2990. if(chunkDebug.size())
  2991. {
  2992. chunkDebug.push_back(0);
  2993. log += "Debug:\n";
  2994. log += &*chunkDebug.begin();
  2995. log += "\n";
  2996. }
  2997. this->Makefile->AddDefinition(logVar.c_str(), log.c_str());
  2998. }
  2999. return true;
  3000. #else
  3001. this->SetError("UPLOAD not supported by bootstrap cmake.");
  3002. return false;
  3003. #endif
  3004. }