1
0

cmFileCommand.cxx 91 KB

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