cmFileCommand.cxx 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmFileCommand.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cctype>
  7. #include <cmath>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <map>
  11. #include <set>
  12. #include <sstream>
  13. #include <utility>
  14. #include <vector>
  15. #include <cm/memory>
  16. #include <cm/optional>
  17. #include <cm/string_view>
  18. #include <cmext/algorithm>
  19. #include <cmext/string_view>
  20. #include <cm3p/kwiml/int.h>
  21. #include "cmsys/FStream.hxx"
  22. #include "cmsys/Glob.hxx"
  23. #include "cmsys/RegularExpression.hxx"
  24. #include "cm_sys_stat.h"
  25. #include "cmArgumentParser.h"
  26. #include "cmArgumentParserTypes.h"
  27. #include "cmCMakePath.h"
  28. #include "cmCryptoHash.h"
  29. #include "cmELF.h"
  30. #include "cmExecutionStatus.h"
  31. #include "cmFSPermissions.h"
  32. #include "cmFileCommand_ReadMacho.h"
  33. #include "cmFileCopier.h"
  34. #include "cmFileInstaller.h"
  35. #include "cmFileLockPool.h"
  36. #include "cmFileTimes.h"
  37. #include "cmGeneratedFileStream.h"
  38. #include "cmGeneratorExpression.h"
  39. #include "cmGlobCacheEntry.h"
  40. #include "cmGlobalGenerator.h"
  41. #include "cmHexFileConverter.h"
  42. #include "cmList.h"
  43. #include "cmListFileCache.h"
  44. #include "cmMakefile.h"
  45. #include "cmMessageType.h"
  46. #include "cmNewLineStyle.h"
  47. #include "cmPolicies.h"
  48. #include "cmRange.h"
  49. #include "cmRuntimeDependencyArchive.h"
  50. #include "cmState.h"
  51. #include "cmStringAlgorithms.h"
  52. #include "cmSubcommandTable.h"
  53. #include "cmSystemTools.h"
  54. #include "cmTimestamp.h"
  55. #include "cmValue.h"
  56. #include "cmWorkingDirectory.h"
  57. #include "cmake.h"
  58. #if !defined(CMAKE_BOOTSTRAP)
  59. # include <cm3p/curl/curl.h>
  60. # include "cmCurl.h"
  61. # include "cmFileLockResult.h"
  62. #endif
  63. namespace {
  64. bool HandleWriteImpl(std::vector<std::string> const& args, bool append,
  65. cmExecutionStatus& status)
  66. {
  67. if (args.size() < 2) {
  68. status.SetError(cmStrCat(
  69. args[0], " must be called with at least one additional argument."));
  70. return false;
  71. }
  72. auto i = args.begin();
  73. i++; // Get rid of subcommand
  74. std::string fileName = *i;
  75. if (!cmsys::SystemTools::FileIsFullPath(*i)) {
  76. fileName =
  77. cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/', *i);
  78. }
  79. i++;
  80. if (!status.GetMakefile().CanIWriteThisFile(fileName)) {
  81. std::string e =
  82. "attempted to write a file: " + fileName + " into a source directory.";
  83. status.SetError(e);
  84. cmSystemTools::SetFatalErrorOccurred();
  85. return false;
  86. }
  87. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  88. cmSystemTools::MakeDirectory(dir);
  89. mode_t mode = 0;
  90. bool writable = false;
  91. // Set permissions to writable
  92. if (cmSystemTools::GetPermissions(fileName, mode)) {
  93. #if defined(_MSC_VER) || defined(__MINGW32__)
  94. writable = (mode & S_IWRITE) != 0;
  95. mode_t newMode = mode | S_IWRITE;
  96. #else
  97. writable = mode & S_IWUSR;
  98. mode_t newMode = mode | S_IWUSR | S_IWGRP;
  99. #endif
  100. if (!writable) {
  101. cmSystemTools::SetPermissions(fileName, newMode);
  102. }
  103. }
  104. // If GetPermissions fails, pretend like it is ok. File open will fail if
  105. // the file is not writable
  106. cmsys::ofstream file(fileName.c_str(),
  107. append ? std::ios::app : std::ios::out);
  108. if (!file) {
  109. std::string error =
  110. cmStrCat("failed to open for writing (",
  111. cmSystemTools::GetLastSystemError(), "):\n ", fileName);
  112. status.SetError(error);
  113. return false;
  114. }
  115. std::string message = cmJoin(cmMakeRange(i, args.end()), std::string());
  116. file << message;
  117. if (!file) {
  118. std::string error =
  119. cmStrCat("write failed (", cmSystemTools::GetLastSystemError(), "):\n ",
  120. fileName);
  121. status.SetError(error);
  122. return false;
  123. }
  124. file.close();
  125. if (mode && !writable) {
  126. cmSystemTools::SetPermissions(fileName, mode);
  127. }
  128. return true;
  129. }
  130. bool HandleWriteCommand(std::vector<std::string> const& args,
  131. cmExecutionStatus& status)
  132. {
  133. return HandleWriteImpl(args, false, status);
  134. }
  135. bool HandleAppendCommand(std::vector<std::string> const& args,
  136. cmExecutionStatus& status)
  137. {
  138. return HandleWriteImpl(args, true, status);
  139. }
  140. bool HandleReadCommand(std::vector<std::string> const& args,
  141. cmExecutionStatus& status)
  142. {
  143. if (args.size() < 3) {
  144. status.SetError("READ must be called with at least two additional "
  145. "arguments");
  146. return false;
  147. }
  148. std::string const& fileNameArg = args[1];
  149. std::string const& variable = args[2];
  150. struct Arguments
  151. {
  152. std::string Offset;
  153. std::string Limit;
  154. bool Hex = false;
  155. };
  156. static auto const parser = cmArgumentParser<Arguments>{}
  157. .Bind("OFFSET"_s, &Arguments::Offset)
  158. .Bind("LIMIT"_s, &Arguments::Limit)
  159. .Bind("HEX"_s, &Arguments::Hex);
  160. Arguments const arguments = parser.Parse(cmMakeRange(args).advance(3),
  161. /*unparsedArguments=*/nullptr);
  162. std::string fileName = fileNameArg;
  163. if (!cmsys::SystemTools::FileIsFullPath(fileName)) {
  164. fileName = cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/',
  165. fileNameArg);
  166. }
  167. // Open the specified file.
  168. #if defined(_WIN32) || defined(__CYGWIN__)
  169. cmsys::ifstream file(fileName.c_str(),
  170. arguments.Hex ? (std::ios::binary | std::ios::in)
  171. : std::ios::in);
  172. #else
  173. cmsys::ifstream file(fileName.c_str());
  174. #endif
  175. if (!file) {
  176. std::string error =
  177. cmStrCat("failed to open for reading (",
  178. cmSystemTools::GetLastSystemError(), "):\n ", fileName);
  179. status.SetError(error);
  180. return false;
  181. }
  182. // is there a limit?
  183. std::string::size_type sizeLimit = std::string::npos;
  184. if (!arguments.Limit.empty()) {
  185. unsigned long long limit;
  186. if (cmStrToULongLong(arguments.Limit, &limit)) {
  187. sizeLimit = static_cast<std::string::size_type>(limit);
  188. }
  189. }
  190. // is there an offset?
  191. cmsys::ifstream::off_type offset = 0;
  192. if (!arguments.Offset.empty()) {
  193. long long off;
  194. if (cmStrToLongLong(arguments.Offset, &off)) {
  195. offset = static_cast<cmsys::ifstream::off_type>(off);
  196. }
  197. }
  198. file.seekg(offset, std::ios::beg); // explicit ios::beg for IBM VisualAge 6
  199. std::string output;
  200. if (arguments.Hex) {
  201. // Convert part of the file into hex code
  202. char c;
  203. while ((sizeLimit > 0) && (file.get(c))) {
  204. char hex[4];
  205. snprintf(hex, sizeof(hex), "%.2x", c & 0xff);
  206. output += hex;
  207. sizeLimit--;
  208. }
  209. } else {
  210. std::string line;
  211. bool has_newline = false;
  212. while (
  213. sizeLimit > 0 &&
  214. cmSystemTools::GetLineFromStream(file, line, &has_newline, sizeLimit)) {
  215. sizeLimit = sizeLimit - line.size();
  216. if (has_newline && sizeLimit > 0) {
  217. sizeLimit--;
  218. }
  219. output += line;
  220. if (has_newline) {
  221. output += "\n";
  222. }
  223. }
  224. }
  225. status.GetMakefile().AddDefinition(variable, output);
  226. return true;
  227. }
  228. bool HandleHashCommand(std::vector<std::string> const& args,
  229. cmExecutionStatus& status)
  230. {
  231. #if !defined(CMAKE_BOOTSTRAP)
  232. if (args.size() != 3) {
  233. status.SetError(
  234. cmStrCat(args[0], " requires a file name and output variable"));
  235. return false;
  236. }
  237. std::unique_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0]));
  238. if (hash) {
  239. std::string out = hash->HashFile(args[1]);
  240. if (!out.empty()) {
  241. status.GetMakefile().AddDefinition(args[2], out);
  242. return true;
  243. }
  244. status.SetError(cmStrCat(args[0], " failed to read file \"", args[1],
  245. "\": ", cmSystemTools::GetLastSystemError()));
  246. }
  247. return false;
  248. #else
  249. status.SetError(cmStrCat(args[0], " not available during bootstrap"));
  250. return false;
  251. #endif
  252. }
  253. bool HandleStringsCommand(std::vector<std::string> const& args,
  254. cmExecutionStatus& status)
  255. {
  256. if (args.size() < 3) {
  257. status.SetError("STRINGS requires a file name and output variable");
  258. return false;
  259. }
  260. // Get the file to read.
  261. std::string fileName = args[1];
  262. if (!cmsys::SystemTools::FileIsFullPath(fileName)) {
  263. fileName =
  264. cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/', args[1]);
  265. }
  266. // Get the variable in which to store the results.
  267. std::string const& outVar = args[2];
  268. // Parse the options.
  269. enum
  270. {
  271. arg_none,
  272. arg_limit_input,
  273. arg_limit_output,
  274. arg_limit_count,
  275. arg_length_minimum,
  276. arg_length_maximum,
  277. arg_maximum,
  278. arg_regex,
  279. arg_encoding
  280. };
  281. unsigned int minlen = 0;
  282. unsigned int maxlen = 0;
  283. int limit_input = -1;
  284. int limit_output = -1;
  285. unsigned int limit_count = 0;
  286. cmsys::RegularExpression regex;
  287. bool have_regex = false;
  288. bool store_regex = true;
  289. bool newline_consume = false;
  290. bool hex_conversion_enabled = true;
  291. enum
  292. {
  293. encoding_none = cmsys::FStream::BOM_None,
  294. encoding_utf8 = cmsys::FStream::BOM_UTF8,
  295. encoding_utf16le = cmsys::FStream::BOM_UTF16LE,
  296. encoding_utf16be = cmsys::FStream::BOM_UTF16BE,
  297. encoding_utf32le = cmsys::FStream::BOM_UTF32LE,
  298. encoding_utf32be = cmsys::FStream::BOM_UTF32BE
  299. };
  300. int encoding = encoding_none;
  301. int arg_mode = arg_none;
  302. for (unsigned int i = 3; i < args.size(); ++i) {
  303. if (args[i] == "LIMIT_INPUT") {
  304. arg_mode = arg_limit_input;
  305. } else if (args[i] == "LIMIT_OUTPUT") {
  306. arg_mode = arg_limit_output;
  307. } else if (args[i] == "LIMIT_COUNT") {
  308. arg_mode = arg_limit_count;
  309. } else if (args[i] == "LENGTH_MINIMUM") {
  310. arg_mode = arg_length_minimum;
  311. } else if (args[i] == "LENGTH_MAXIMUM") {
  312. arg_mode = arg_length_maximum;
  313. } else if (args[i] == "REGEX") {
  314. arg_mode = arg_regex;
  315. } else if (args[i] == "NEWLINE_CONSUME") {
  316. newline_consume = true;
  317. arg_mode = arg_none;
  318. } else if (args[i] == "NO_HEX_CONVERSION") {
  319. hex_conversion_enabled = false;
  320. arg_mode = arg_none;
  321. } else if (args[i] == "ENCODING") {
  322. arg_mode = arg_encoding;
  323. } else if (arg_mode == arg_limit_input) {
  324. if (sscanf(args[i].c_str(), "%d", &limit_input) != 1 ||
  325. limit_input < 0) {
  326. status.SetError(cmStrCat("STRINGS option LIMIT_INPUT value \"",
  327. args[i], "\" is not an unsigned integer."));
  328. return false;
  329. }
  330. arg_mode = arg_none;
  331. } else if (arg_mode == arg_limit_output) {
  332. if (sscanf(args[i].c_str(), "%d", &limit_output) != 1 ||
  333. limit_output < 0) {
  334. status.SetError(cmStrCat("STRINGS option LIMIT_OUTPUT value \"",
  335. args[i], "\" is not an unsigned integer."));
  336. return false;
  337. }
  338. arg_mode = arg_none;
  339. } else if (arg_mode == arg_limit_count) {
  340. int count;
  341. if (sscanf(args[i].c_str(), "%d", &count) != 1 || count < 0) {
  342. status.SetError(cmStrCat("STRINGS option LIMIT_COUNT value \"",
  343. args[i], "\" is not an unsigned integer."));
  344. return false;
  345. }
  346. limit_count = count;
  347. arg_mode = arg_none;
  348. } else if (arg_mode == arg_length_minimum) {
  349. int len;
  350. if (sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0) {
  351. status.SetError(cmStrCat("STRINGS option LENGTH_MINIMUM value \"",
  352. args[i], "\" is not an unsigned integer."));
  353. return false;
  354. }
  355. minlen = len;
  356. arg_mode = arg_none;
  357. } else if (arg_mode == arg_length_maximum) {
  358. int len;
  359. if (sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0) {
  360. status.SetError(cmStrCat("STRINGS option LENGTH_MAXIMUM value \"",
  361. args[i], "\" is not an unsigned integer."));
  362. return false;
  363. }
  364. maxlen = len;
  365. arg_mode = arg_none;
  366. } else if (arg_mode == arg_regex) {
  367. if (!regex.compile(args[i])) {
  368. status.SetError(cmStrCat("STRINGS option REGEX value \"", args[i],
  369. "\" could not be compiled."));
  370. return false;
  371. }
  372. have_regex = true;
  373. switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0159)) {
  374. case cmPolicies::REQUIRED_IF_USED:
  375. case cmPolicies::REQUIRED_ALWAYS:
  376. case cmPolicies::NEW:
  377. // store_regex = true
  378. break;
  379. case cmPolicies::WARN:
  380. if (status.GetMakefile().PolicyOptionalWarningEnabled(
  381. "CMAKE_POLICY_WARNING_CMP0159")) {
  382. status.GetMakefile().IssueMessage(
  383. MessageType::AUTHOR_WARNING,
  384. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0159), '\n',
  385. "For compatibility, CMake is leaving CMAKE_MATCH_<n> "
  386. "unchanged."));
  387. }
  388. CM_FALLTHROUGH;
  389. case cmPolicies::OLD:
  390. store_regex = false;
  391. break;
  392. }
  393. arg_mode = arg_none;
  394. } else if (arg_mode == arg_encoding) {
  395. if (args[i] == "UTF-8") {
  396. encoding = encoding_utf8;
  397. } else if (args[i] == "UTF-16LE") {
  398. encoding = encoding_utf16le;
  399. } else if (args[i] == "UTF-16BE") {
  400. encoding = encoding_utf16be;
  401. } else if (args[i] == "UTF-32LE") {
  402. encoding = encoding_utf32le;
  403. } else if (args[i] == "UTF-32BE") {
  404. encoding = encoding_utf32be;
  405. } else {
  406. status.SetError(cmStrCat("STRINGS option ENCODING \"", args[i],
  407. "\" not recognized."));
  408. return false;
  409. }
  410. arg_mode = arg_none;
  411. } else {
  412. status.SetError(
  413. cmStrCat("STRINGS given unknown argument \"", args[i], "\""));
  414. return false;
  415. }
  416. }
  417. if (hex_conversion_enabled) {
  418. // TODO: should work without temp file, but just on a memory buffer
  419. std::string binaryFileName =
  420. cmStrCat(status.GetMakefile().GetCurrentBinaryDirectory(),
  421. "/CMakeFiles/FileCommandStringsBinaryFile");
  422. if (cmHexFileConverter::TryConvert(fileName, binaryFileName)) {
  423. fileName = binaryFileName;
  424. }
  425. }
  426. // Open the specified file.
  427. #if defined(_WIN32) || defined(__CYGWIN__)
  428. cmsys::ifstream fin(fileName.c_str(), std::ios::in | std::ios::binary);
  429. #else
  430. cmsys::ifstream fin(fileName.c_str());
  431. #endif
  432. if (!fin) {
  433. status.SetError(
  434. cmStrCat("STRINGS file \"", fileName, "\" cannot be read."));
  435. return false;
  436. }
  437. // If BOM is found and encoding was not specified, use the BOM
  438. int bom_found = cmsys::FStream::ReadBOM(fin);
  439. if (encoding == encoding_none && bom_found != cmsys::FStream::BOM_None) {
  440. encoding = bom_found;
  441. }
  442. unsigned int bytes_rem = 0;
  443. if (encoding == encoding_utf16le || encoding == encoding_utf16be) {
  444. bytes_rem = 1;
  445. }
  446. if (encoding == encoding_utf32le || encoding == encoding_utf32be) {
  447. bytes_rem = 3;
  448. }
  449. // Parse strings out of the file.
  450. int output_size = 0;
  451. std::vector<std::string> strings;
  452. std::string s;
  453. while ((!limit_count || strings.size() < limit_count) &&
  454. (limit_input < 0 || static_cast<int>(fin.tellg()) < limit_input) &&
  455. fin) {
  456. std::string current_str;
  457. int c = fin.get();
  458. for (unsigned int i = 0; i < bytes_rem; ++i) {
  459. int c1 = fin.get();
  460. if (!fin) {
  461. fin.putback(static_cast<char>(c1));
  462. break;
  463. }
  464. c = (c << 8) | c1;
  465. }
  466. if (encoding == encoding_utf16le) {
  467. c = ((c & 0xFF) << 8) | ((c & 0xFF00) >> 8);
  468. } else if (encoding == encoding_utf32le) {
  469. c = (((c & 0xFF) << 24) | ((c & 0xFF00) << 8) | ((c & 0xFF0000) >> 8) |
  470. ((c & 0xFF000000) >> 24));
  471. }
  472. if (c == '\r') {
  473. // Ignore CR character to make output always have UNIX newlines.
  474. continue;
  475. }
  476. if (c >= 0 && c <= 0xFF &&
  477. (isprint(c) || c == '\t' || (c == '\n' && newline_consume))) {
  478. // This is an ASCII character that may be part of a string.
  479. // Cast added to avoid compiler warning. Cast is ok because
  480. // c is guaranteed to fit in char by the above if...
  481. current_str += static_cast<char>(c);
  482. } else if (encoding == encoding_utf8) {
  483. // Check for UTF-8 encoded string (up to 4 octets)
  484. static const unsigned char utf8_check_table[3][2] = {
  485. { 0xE0, 0xC0 },
  486. { 0xF0, 0xE0 },
  487. { 0xF8, 0xF0 },
  488. };
  489. // how many octets are there?
  490. unsigned int num_utf8_bytes = 0;
  491. for (unsigned int j = 0; num_utf8_bytes == 0 && j < 3; j++) {
  492. if ((c & utf8_check_table[j][0]) == utf8_check_table[j][1]) {
  493. num_utf8_bytes = j + 2;
  494. }
  495. }
  496. // get subsequent octets and check that they are valid
  497. for (unsigned int j = 0; j < num_utf8_bytes; j++) {
  498. if (j != 0) {
  499. c = fin.get();
  500. if (!fin || (c & 0xC0) != 0x80) {
  501. fin.putback(static_cast<char>(c));
  502. break;
  503. }
  504. }
  505. current_str += static_cast<char>(c);
  506. }
  507. // if this was an invalid utf8 sequence, discard the data, and put
  508. // back subsequent characters
  509. if ((current_str.length() != num_utf8_bytes)) {
  510. for (unsigned int j = 0; j < current_str.size() - 1; j++) {
  511. fin.putback(current_str[current_str.size() - 1 - j]);
  512. }
  513. current_str.clear();
  514. }
  515. }
  516. if (c == '\n' && !newline_consume) {
  517. // The current line has been terminated. Check if the current
  518. // string matches the requirements. The length may now be as
  519. // low as zero since blank lines are allowed.
  520. if (s.length() >= minlen && (!have_regex || regex.find(s))) {
  521. if (store_regex) {
  522. status.GetMakefile().ClearMatches();
  523. status.GetMakefile().StoreMatches(regex);
  524. }
  525. output_size += static_cast<int>(s.size()) + 1;
  526. if (limit_output >= 0 && output_size >= limit_output) {
  527. s.clear();
  528. break;
  529. }
  530. strings.push_back(s);
  531. }
  532. // Reset the string to empty.
  533. s.clear();
  534. } else if (current_str.empty()) {
  535. // A non-string character has been found. Check if the current
  536. // string matches the requirements. We require that the length
  537. // be at least one no matter what the user specified.
  538. if (s.length() >= minlen && !s.empty() &&
  539. (!have_regex || regex.find(s))) {
  540. if (store_regex) {
  541. status.GetMakefile().ClearMatches();
  542. status.GetMakefile().StoreMatches(regex);
  543. }
  544. output_size += static_cast<int>(s.size()) + 1;
  545. if (limit_output >= 0 && output_size >= limit_output) {
  546. s.clear();
  547. break;
  548. }
  549. strings.push_back(s);
  550. }
  551. // Reset the string to empty.
  552. s.clear();
  553. } else {
  554. s += current_str;
  555. }
  556. if (maxlen > 0 && s.size() == maxlen) {
  557. // Terminate a string if the maximum length is reached.
  558. if (s.length() >= minlen && (!have_regex || regex.find(s))) {
  559. if (store_regex) {
  560. status.GetMakefile().ClearMatches();
  561. status.GetMakefile().StoreMatches(regex);
  562. }
  563. output_size += static_cast<int>(s.size()) + 1;
  564. if (limit_output >= 0 && output_size >= limit_output) {
  565. s.clear();
  566. break;
  567. }
  568. strings.push_back(s);
  569. }
  570. s.clear();
  571. }
  572. }
  573. // If there is a non-empty current string we have hit the end of the
  574. // input file or the input size limit. Check if the current string
  575. // matches the requirements.
  576. if ((!limit_count || strings.size() < limit_count) && !s.empty() &&
  577. s.length() >= minlen && (!have_regex || regex.find(s))) {
  578. if (store_regex) {
  579. status.GetMakefile().ClearMatches();
  580. status.GetMakefile().StoreMatches(regex);
  581. }
  582. output_size += static_cast<int>(s.size()) + 1;
  583. if (limit_output < 0 || output_size < limit_output) {
  584. strings.push_back(s);
  585. }
  586. }
  587. // Encode the result in a CMake list.
  588. const char* sep = "";
  589. std::string output;
  590. for (std::string const& sr : strings) {
  591. // Separate the strings in the output to make it a list.
  592. output += sep;
  593. sep = ";";
  594. // Store the string in the output, but escape semicolons to
  595. // make sure it is a list.
  596. for (char i : sr) {
  597. if (i == ';') {
  598. output += '\\';
  599. }
  600. output += i;
  601. }
  602. }
  603. // Save the output in a makefile variable.
  604. status.GetMakefile().AddDefinition(outVar, output);
  605. return true;
  606. }
  607. bool HandleGlobImpl(std::vector<std::string> const& args, bool recurse,
  608. cmExecutionStatus& status)
  609. {
  610. if (args.size() < 2) {
  611. status.SetError(cmStrCat(
  612. args[0], " must be called with at least one additional argument."));
  613. return false;
  614. }
  615. auto i = args.begin();
  616. i++; // Get rid of subcommand
  617. std::string variable = *i;
  618. i++;
  619. cmsys::Glob g;
  620. g.SetRecurse(recurse);
  621. bool explicitFollowSymlinks = false;
  622. cmPolicies::PolicyStatus policyStatus =
  623. status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0009);
  624. if (recurse) {
  625. switch (policyStatus) {
  626. case cmPolicies::REQUIRED_IF_USED:
  627. case cmPolicies::REQUIRED_ALWAYS:
  628. case cmPolicies::NEW:
  629. g.RecurseThroughSymlinksOff();
  630. break;
  631. case cmPolicies::WARN:
  632. CM_FALLTHROUGH;
  633. case cmPolicies::OLD:
  634. g.RecurseThroughSymlinksOn();
  635. break;
  636. }
  637. }
  638. cmake* cm = status.GetMakefile().GetCMakeInstance();
  639. std::vector<std::string> files;
  640. bool configureDepends = false;
  641. bool warnConfigureLate = false;
  642. bool warnFollowedSymlinks = false;
  643. const cmake::WorkingMode workingMode = cm->GetWorkingMode();
  644. while (i != args.end()) {
  645. if (*i == "LIST_DIRECTORIES") {
  646. ++i; // skip LIST_DIRECTORIES
  647. if (i != args.end()) {
  648. if (cmIsOn(*i)) {
  649. g.SetListDirs(true);
  650. g.SetRecurseListDirs(true);
  651. } else if (cmIsOff(*i)) {
  652. g.SetListDirs(false);
  653. g.SetRecurseListDirs(false);
  654. } else {
  655. status.SetError("LIST_DIRECTORIES missing bool value.");
  656. return false;
  657. }
  658. ++i;
  659. } else {
  660. status.SetError("LIST_DIRECTORIES missing bool value.");
  661. return false;
  662. }
  663. } else if (*i == "FOLLOW_SYMLINKS") {
  664. ++i; // skip FOLLOW_SYMLINKS
  665. if (recurse) {
  666. explicitFollowSymlinks = true;
  667. g.RecurseThroughSymlinksOn();
  668. if (i == args.end()) {
  669. status.SetError(
  670. "GLOB_RECURSE requires a glob expression after FOLLOW_SYMLINKS.");
  671. return false;
  672. }
  673. }
  674. } else if (*i == "RELATIVE") {
  675. ++i; // skip RELATIVE
  676. if (i == args.end()) {
  677. status.SetError("GLOB requires a directory after the RELATIVE tag.");
  678. return false;
  679. }
  680. g.SetRelative(i->c_str());
  681. ++i;
  682. if (i == args.end()) {
  683. status.SetError(
  684. "GLOB requires a glob expression after the directory.");
  685. return false;
  686. }
  687. } else if (*i == "CONFIGURE_DEPENDS") {
  688. // Generated build system depends on glob results
  689. if (!configureDepends && warnConfigureLate) {
  690. status.GetMakefile().IssueMessage(
  691. MessageType::AUTHOR_WARNING,
  692. "CONFIGURE_DEPENDS flag was given after a glob expression was "
  693. "already evaluated.");
  694. }
  695. if (workingMode != cmake::NORMAL_MODE) {
  696. status.GetMakefile().IssueMessage(
  697. MessageType::FATAL_ERROR,
  698. "CONFIGURE_DEPENDS is invalid for script and find package modes.");
  699. return false;
  700. }
  701. configureDepends = true;
  702. ++i;
  703. if (i == args.end()) {
  704. status.SetError(
  705. "GLOB requires a glob expression after CONFIGURE_DEPENDS.");
  706. return false;
  707. }
  708. } else {
  709. std::string expr = *i;
  710. if (!cmsys::SystemTools::FileIsFullPath(*i)) {
  711. expr = status.GetMakefile().GetCurrentSourceDirectory();
  712. // Handle script mode
  713. if (!expr.empty()) {
  714. expr += "/" + *i;
  715. } else {
  716. expr = *i;
  717. }
  718. }
  719. cmsys::Glob::GlobMessages globMessages;
  720. g.FindFiles(expr, &globMessages);
  721. if (!globMessages.empty()) {
  722. bool shouldExit = false;
  723. for (cmsys::Glob::Message const& globMessage : globMessages) {
  724. if (globMessage.type == cmsys::Glob::cyclicRecursion) {
  725. status.GetMakefile().IssueMessage(
  726. MessageType::AUTHOR_WARNING,
  727. "Cyclic recursion detected while globbing for '" + *i + "':\n" +
  728. globMessage.content);
  729. } else if (globMessage.type == cmsys::Glob::error) {
  730. status.GetMakefile().IssueMessage(
  731. MessageType::FATAL_ERROR,
  732. "Error has occurred while globbing for '" + *i + "' - " +
  733. globMessage.content);
  734. shouldExit = true;
  735. } else if (cm->GetDebugOutput() || cm->GetTrace()) {
  736. status.GetMakefile().IssueMessage(
  737. MessageType::LOG,
  738. cmStrCat("Globbing for\n ", *i, "\nEncountered an error:\n ",
  739. globMessage.content));
  740. }
  741. }
  742. if (shouldExit) {
  743. return false;
  744. }
  745. }
  746. if (recurse && !explicitFollowSymlinks &&
  747. g.GetFollowedSymlinkCount() != 0) {
  748. warnFollowedSymlinks = true;
  749. }
  750. std::vector<std::string>& foundFiles = g.GetFiles();
  751. cm::append(files, foundFiles);
  752. if (configureDepends) {
  753. std::sort(foundFiles.begin(), foundFiles.end());
  754. foundFiles.erase(std::unique(foundFiles.begin(), foundFiles.end()),
  755. foundFiles.end());
  756. auto entry = cmGlobCacheEntry{
  757. recurse,
  758. (recurse ? g.GetRecurseListDirs() : g.GetListDirs()),
  759. (recurse ? g.GetRecurseThroughSymlinks() : false),
  760. (g.GetRelative() ? g.GetRelative() : ""),
  761. expr,
  762. foundFiles
  763. };
  764. cm->AddGlobCacheEntry(entry, variable,
  765. status.GetMakefile().GetBacktrace());
  766. } else {
  767. warnConfigureLate = true;
  768. }
  769. ++i;
  770. }
  771. }
  772. switch (policyStatus) {
  773. case cmPolicies::REQUIRED_IF_USED:
  774. case cmPolicies::REQUIRED_ALWAYS:
  775. case cmPolicies::NEW:
  776. // Correct behavior, yay!
  777. break;
  778. case cmPolicies::OLD:
  779. // Probably not really the expected behavior, but the author explicitly
  780. // asked for the old behavior... no warning.
  781. case cmPolicies::WARN:
  782. // Possibly unexpected old behavior *and* we actually traversed
  783. // symlinks without being explicitly asked to: warn the author.
  784. if (warnFollowedSymlinks) {
  785. status.GetMakefile().IssueMessage(
  786. MessageType::AUTHOR_WARNING,
  787. cmPolicies::GetPolicyWarning(cmPolicies::CMP0009));
  788. }
  789. break;
  790. }
  791. std::sort(files.begin(), files.end());
  792. files.erase(std::unique(files.begin(), files.end()), files.end());
  793. status.GetMakefile().AddDefinition(variable, cmList::to_string(files));
  794. return true;
  795. }
  796. bool HandleGlobCommand(std::vector<std::string> const& args,
  797. cmExecutionStatus& status)
  798. {
  799. return HandleGlobImpl(args, false, status);
  800. }
  801. bool HandleGlobRecurseCommand(std::vector<std::string> const& args,
  802. cmExecutionStatus& status)
  803. {
  804. return HandleGlobImpl(args, true, status);
  805. }
  806. bool HandleMakeDirectoryCommand(std::vector<std::string> const& args,
  807. cmExecutionStatus& status)
  808. {
  809. // Projects might pass a dynamically generated list of directories, and it
  810. // could be an empty list. We should not assume there is at least one.
  811. std::string expr;
  812. for (std::string const& arg :
  813. cmMakeRange(args).advance(1)) // Get rid of subcommand
  814. {
  815. const std::string* cdir = &arg;
  816. if (!cmsys::SystemTools::FileIsFullPath(arg)) {
  817. expr =
  818. cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/', arg);
  819. cdir = &expr;
  820. }
  821. if (!status.GetMakefile().CanIWriteThisFile(*cdir)) {
  822. std::string e = "attempted to create a directory: " + *cdir +
  823. " into a source directory.";
  824. status.SetError(e);
  825. cmSystemTools::SetFatalErrorOccurred();
  826. return false;
  827. }
  828. cmsys::Status mkdirStatus = cmSystemTools::MakeDirectory(*cdir);
  829. if (!mkdirStatus) {
  830. std::string error = cmStrCat("failed to create directory:\n ", *cdir,
  831. "\nbecause: ", mkdirStatus.GetString());
  832. status.SetError(error);
  833. return false;
  834. }
  835. }
  836. return true;
  837. }
  838. bool HandleTouchImpl(std::vector<std::string> const& args, bool create,
  839. cmExecutionStatus& status)
  840. {
  841. // Projects might pass a dynamically generated list of files, and it
  842. // could be an empty list. We should not assume there is at least one.
  843. for (std::string const& arg :
  844. cmMakeRange(args).advance(1)) // Get rid of subcommand
  845. {
  846. std::string tfile = arg;
  847. if (!cmsys::SystemTools::FileIsFullPath(tfile)) {
  848. tfile =
  849. cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/', arg);
  850. }
  851. if (!status.GetMakefile().CanIWriteThisFile(tfile)) {
  852. std::string e =
  853. "attempted to touch a file: " + tfile + " in a source directory.";
  854. status.SetError(e);
  855. cmSystemTools::SetFatalErrorOccurred();
  856. return false;
  857. }
  858. if (!cmSystemTools::Touch(tfile, create)) {
  859. std::string error = "problem touching file: " + tfile;
  860. status.SetError(error);
  861. return false;
  862. }
  863. }
  864. return true;
  865. }
  866. bool HandleTouchCommand(std::vector<std::string> const& args,
  867. cmExecutionStatus& status)
  868. {
  869. return HandleTouchImpl(args, true, status);
  870. }
  871. bool HandleTouchNocreateCommand(std::vector<std::string> const& args,
  872. cmExecutionStatus& status)
  873. {
  874. return HandleTouchImpl(args, false, status);
  875. }
  876. bool HandleDifferentCommand(std::vector<std::string> const& args,
  877. cmExecutionStatus& status)
  878. {
  879. /*
  880. FILE(DIFFERENT <variable> FILES <lhs> <rhs>)
  881. */
  882. // Evaluate arguments.
  883. const char* file_lhs = nullptr;
  884. const char* file_rhs = nullptr;
  885. const char* var = nullptr;
  886. enum Doing
  887. {
  888. DoingNone,
  889. DoingVar,
  890. DoingFileLHS,
  891. DoingFileRHS
  892. };
  893. Doing doing = DoingVar;
  894. for (unsigned int i = 1; i < args.size(); ++i) {
  895. if (args[i] == "FILES") {
  896. doing = DoingFileLHS;
  897. } else if (doing == DoingVar) {
  898. var = args[i].c_str();
  899. doing = DoingNone;
  900. } else if (doing == DoingFileLHS) {
  901. file_lhs = args[i].c_str();
  902. doing = DoingFileRHS;
  903. } else if (doing == DoingFileRHS) {
  904. file_rhs = args[i].c_str();
  905. doing = DoingNone;
  906. } else {
  907. status.SetError(cmStrCat("DIFFERENT given unknown argument ", args[i]));
  908. return false;
  909. }
  910. }
  911. if (!var) {
  912. status.SetError("DIFFERENT not given result variable name.");
  913. return false;
  914. }
  915. if (!file_lhs || !file_rhs) {
  916. status.SetError("DIFFERENT not given FILES option with two file names.");
  917. return false;
  918. }
  919. // Compare the files.
  920. const char* result =
  921. cmSystemTools::FilesDiffer(file_lhs, file_rhs) ? "1" : "0";
  922. status.GetMakefile().AddDefinition(var, result);
  923. return true;
  924. }
  925. bool HandleCopyCommand(std::vector<std::string> const& args,
  926. cmExecutionStatus& status)
  927. {
  928. cmFileCopier copier(status);
  929. return copier.Run(args);
  930. }
  931. bool HandleRPathChangeCommand(std::vector<std::string> const& args,
  932. cmExecutionStatus& status)
  933. {
  934. // Evaluate arguments.
  935. std::string file;
  936. cm::optional<std::string> oldRPath;
  937. cm::optional<std::string> newRPath;
  938. bool removeEnvironmentRPath = false;
  939. cmArgumentParser<void> parser;
  940. std::vector<std::string> unknownArgs;
  941. parser.Bind("FILE"_s, file)
  942. .Bind("OLD_RPATH"_s, oldRPath)
  943. .Bind("NEW_RPATH"_s, newRPath)
  944. .Bind("INSTALL_REMOVE_ENVIRONMENT_RPATH"_s, removeEnvironmentRPath);
  945. ArgumentParser::ParseResult parseResult =
  946. parser.Parse(cmMakeRange(args).advance(1), &unknownArgs);
  947. if (!unknownArgs.empty()) {
  948. status.SetError(
  949. cmStrCat("RPATH_CHANGE given unknown argument ", unknownArgs.front()));
  950. return false;
  951. }
  952. if (parseResult.MaybeReportError(status.GetMakefile())) {
  953. return true;
  954. }
  955. if (file.empty()) {
  956. status.SetError("RPATH_CHANGE not given FILE option.");
  957. return false;
  958. }
  959. if (!oldRPath) {
  960. status.SetError("RPATH_CHANGE not given OLD_RPATH option.");
  961. return false;
  962. }
  963. if (!newRPath) {
  964. status.SetError("RPATH_CHANGE not given NEW_RPATH option.");
  965. return false;
  966. }
  967. if (!cmSystemTools::FileExists(file, true)) {
  968. status.SetError(
  969. cmStrCat("RPATH_CHANGE given FILE \"", file, "\" that does not exist."));
  970. return false;
  971. }
  972. bool success = true;
  973. cmFileTimes const ft(file);
  974. std::string emsg;
  975. bool changed;
  976. if (!cmSystemTools::ChangeRPath(file, *oldRPath, *newRPath,
  977. removeEnvironmentRPath, &emsg, &changed)) {
  978. status.SetError(cmStrCat("RPATH_CHANGE could not write new RPATH:\n ",
  979. *newRPath, "\nto the file:\n ", file, "\n",
  980. emsg));
  981. success = false;
  982. }
  983. if (success) {
  984. if (changed) {
  985. std::string message =
  986. cmStrCat("Set non-toolchain portion of runtime path of \"", file,
  987. "\" to \"", *newRPath, '"');
  988. status.GetMakefile().DisplayStatus(message, -1);
  989. }
  990. ft.Store(file);
  991. }
  992. return success;
  993. }
  994. bool HandleRPathSetCommand(std::vector<std::string> const& args,
  995. cmExecutionStatus& status)
  996. {
  997. // Evaluate arguments.
  998. std::string file;
  999. cm::optional<std::string> newRPath;
  1000. cmArgumentParser<void> parser;
  1001. std::vector<std::string> unknownArgs;
  1002. parser.Bind("FILE"_s, file).Bind("NEW_RPATH"_s, newRPath);
  1003. ArgumentParser::ParseResult parseResult =
  1004. parser.Parse(cmMakeRange(args).advance(1), &unknownArgs);
  1005. if (!unknownArgs.empty()) {
  1006. status.SetError(cmStrCat("RPATH_SET given unrecognized argument \"",
  1007. unknownArgs.front(), "\"."));
  1008. return false;
  1009. }
  1010. if (parseResult.MaybeReportError(status.GetMakefile())) {
  1011. return true;
  1012. }
  1013. if (file.empty()) {
  1014. status.SetError("RPATH_SET not given FILE option.");
  1015. return false;
  1016. }
  1017. if (!newRPath) {
  1018. status.SetError("RPATH_SET not given NEW_RPATH option.");
  1019. return false;
  1020. }
  1021. if (!cmSystemTools::FileExists(file, true)) {
  1022. status.SetError(
  1023. cmStrCat("RPATH_SET given FILE \"", file, "\" that does not exist."));
  1024. return false;
  1025. }
  1026. bool success = true;
  1027. cmFileTimes const ft(file);
  1028. std::string emsg;
  1029. bool changed;
  1030. if (!cmSystemTools::SetRPath(file, *newRPath, &emsg, &changed)) {
  1031. status.SetError(cmStrCat("RPATH_SET could not write new RPATH:\n ",
  1032. *newRPath, "\nto the file:\n ", file, "\n",
  1033. emsg));
  1034. success = false;
  1035. }
  1036. if (success) {
  1037. if (changed) {
  1038. std::string message =
  1039. cmStrCat("Set non-toolchain portion of runtime path of \"", file,
  1040. "\" to \"", *newRPath, '"');
  1041. status.GetMakefile().DisplayStatus(message, -1);
  1042. }
  1043. ft.Store(file);
  1044. }
  1045. return success;
  1046. }
  1047. bool HandleRPathRemoveCommand(std::vector<std::string> const& args,
  1048. cmExecutionStatus& status)
  1049. {
  1050. // Evaluate arguments.
  1051. std::string file;
  1052. cmArgumentParser<void> parser;
  1053. std::vector<std::string> unknownArgs;
  1054. parser.Bind("FILE"_s, file);
  1055. ArgumentParser::ParseResult parseResult =
  1056. parser.Parse(cmMakeRange(args).advance(1), &unknownArgs);
  1057. if (!unknownArgs.empty()) {
  1058. status.SetError(
  1059. cmStrCat("RPATH_REMOVE given unknown argument ", unknownArgs.front()));
  1060. return false;
  1061. }
  1062. if (parseResult.MaybeReportError(status.GetMakefile())) {
  1063. return true;
  1064. }
  1065. if (file.empty()) {
  1066. status.SetError("RPATH_REMOVE not given FILE option.");
  1067. return false;
  1068. }
  1069. if (!cmSystemTools::FileExists(file, true)) {
  1070. status.SetError(
  1071. cmStrCat("RPATH_REMOVE given FILE \"", file, "\" that does not exist."));
  1072. return false;
  1073. }
  1074. bool success = true;
  1075. cmFileTimes const ft(file);
  1076. std::string emsg;
  1077. bool removed;
  1078. if (!cmSystemTools::RemoveRPath(file, &emsg, &removed)) {
  1079. status.SetError(
  1080. cmStrCat("RPATH_REMOVE could not remove RPATH from file: \n ", file,
  1081. "\n", emsg));
  1082. success = false;
  1083. }
  1084. if (success) {
  1085. if (removed) {
  1086. std::string message =
  1087. cmStrCat("Removed runtime path from \"", file, '"');
  1088. status.GetMakefile().DisplayStatus(message, -1);
  1089. }
  1090. ft.Store(file);
  1091. }
  1092. return success;
  1093. }
  1094. bool HandleRPathCheckCommand(std::vector<std::string> const& args,
  1095. cmExecutionStatus& status)
  1096. {
  1097. // Evaluate arguments.
  1098. std::string file;
  1099. cm::optional<std::string> rpath;
  1100. cmArgumentParser<void> parser;
  1101. std::vector<std::string> unknownArgs;
  1102. parser.Bind("FILE"_s, file).Bind("RPATH"_s, rpath);
  1103. ArgumentParser::ParseResult parseResult =
  1104. parser.Parse(cmMakeRange(args).advance(1), &unknownArgs);
  1105. if (!unknownArgs.empty()) {
  1106. status.SetError(
  1107. cmStrCat("RPATH_CHECK given unknown argument ", unknownArgs.front()));
  1108. return false;
  1109. }
  1110. if (parseResult.MaybeReportError(status.GetMakefile())) {
  1111. return true;
  1112. }
  1113. if (file.empty()) {
  1114. status.SetError("RPATH_CHECK not given FILE option.");
  1115. return false;
  1116. }
  1117. if (!rpath) {
  1118. status.SetError("RPATH_CHECK not given RPATH option.");
  1119. return false;
  1120. }
  1121. // If the file exists but does not have the desired RPath then
  1122. // delete it. This is used during installation to re-install a file
  1123. // if its RPath will change.
  1124. if (cmSystemTools::FileExists(file, true) &&
  1125. !cmSystemTools::CheckRPath(file, *rpath)) {
  1126. cmSystemTools::RemoveFile(file);
  1127. }
  1128. return true;
  1129. }
  1130. bool HandleReadElfCommand(std::vector<std::string> const& args,
  1131. cmExecutionStatus& status)
  1132. {
  1133. if (args.size() < 4) {
  1134. status.SetError("READ_ELF must be called with at least three additional "
  1135. "arguments.");
  1136. return false;
  1137. }
  1138. std::string const& fileNameArg = args[1];
  1139. struct Arguments
  1140. {
  1141. std::string RPath;
  1142. std::string RunPath;
  1143. std::string Error;
  1144. };
  1145. static auto const parser = cmArgumentParser<Arguments>{}
  1146. .Bind("RPATH"_s, &Arguments::RPath)
  1147. .Bind("RUNPATH"_s, &Arguments::RunPath)
  1148. .Bind("CAPTURE_ERROR"_s, &Arguments::Error);
  1149. Arguments const arguments = parser.Parse(cmMakeRange(args).advance(2),
  1150. /*unparsedArguments=*/nullptr);
  1151. if (!cmSystemTools::FileExists(fileNameArg, true)) {
  1152. status.SetError(cmStrCat("READ_ELF given FILE \"", fileNameArg,
  1153. "\" that does not exist."));
  1154. return false;
  1155. }
  1156. cmELF elf(fileNameArg.c_str());
  1157. if (!elf) {
  1158. if (arguments.Error.empty()) {
  1159. status.SetError(cmStrCat("READ_ELF given FILE:\n ", fileNameArg,
  1160. "\nthat is not a valid ELF file."));
  1161. return false;
  1162. }
  1163. status.GetMakefile().AddDefinition(arguments.Error,
  1164. "not a valid ELF file");
  1165. return true;
  1166. }
  1167. if (!arguments.RPath.empty()) {
  1168. if (cmELF::StringEntry const* se_rpath = elf.GetRPath()) {
  1169. std::string rpath(se_rpath->Value);
  1170. std::replace(rpath.begin(), rpath.end(), ':', ';');
  1171. status.GetMakefile().AddDefinition(arguments.RPath, rpath);
  1172. }
  1173. }
  1174. if (!arguments.RunPath.empty()) {
  1175. if (cmELF::StringEntry const* se_runpath = elf.GetRunPath()) {
  1176. std::string runpath(se_runpath->Value);
  1177. std::replace(runpath.begin(), runpath.end(), ':', ';');
  1178. status.GetMakefile().AddDefinition(arguments.RunPath, runpath);
  1179. }
  1180. }
  1181. return true;
  1182. }
  1183. bool HandleInstallCommand(std::vector<std::string> const& args,
  1184. cmExecutionStatus& status)
  1185. {
  1186. cmFileInstaller installer(status);
  1187. return installer.Run(args);
  1188. }
  1189. bool HandleRealPathCommand(std::vector<std::string> const& args,
  1190. cmExecutionStatus& status)
  1191. {
  1192. if (args.size() < 3) {
  1193. status.SetError("REAL_PATH requires a path and an output variable");
  1194. return false;
  1195. }
  1196. struct Arguments : public ArgumentParser::ParseResult
  1197. {
  1198. cm::optional<std::string> BaseDirectory;
  1199. bool ExpandTilde = false;
  1200. };
  1201. static auto const parser =
  1202. cmArgumentParser<Arguments>{}
  1203. .Bind("BASE_DIRECTORY"_s, &Arguments::BaseDirectory)
  1204. .Bind("EXPAND_TILDE"_s, &Arguments::ExpandTilde);
  1205. std::vector<std::string> unparsedArguments;
  1206. auto arguments =
  1207. parser.Parse(cmMakeRange(args).advance(3), &unparsedArguments);
  1208. if (!unparsedArguments.empty()) {
  1209. status.SetError("REAL_PATH called with unexpected arguments");
  1210. return false;
  1211. }
  1212. if (arguments.MaybeReportError(status.GetMakefile())) {
  1213. return true;
  1214. }
  1215. if (!arguments.BaseDirectory) {
  1216. arguments.BaseDirectory = status.GetMakefile().GetCurrentSourceDirectory();
  1217. }
  1218. auto input = args[1];
  1219. if (arguments.ExpandTilde && !input.empty()) {
  1220. if (input[0] == '~' && (input.length() == 1 || input[1] == '/')) {
  1221. std::string home;
  1222. if (
  1223. #if defined(_WIN32) && !defined(__CYGWIN__)
  1224. cmSystemTools::GetEnv("USERPROFILE", home) ||
  1225. #endif
  1226. cmSystemTools::GetEnv("HOME", home)) {
  1227. input.replace(0, 1, home);
  1228. }
  1229. }
  1230. }
  1231. bool warnAbout152 = false;
  1232. bool use152New = true;
  1233. cmPolicies::PolicyStatus policyStatus =
  1234. status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0152);
  1235. switch (policyStatus) {
  1236. case cmPolicies::REQUIRED_IF_USED:
  1237. case cmPolicies::REQUIRED_ALWAYS:
  1238. case cmPolicies::NEW:
  1239. break;
  1240. case cmPolicies::WARN:
  1241. use152New = false;
  1242. warnAbout152 = true;
  1243. break;
  1244. case cmPolicies::OLD:
  1245. use152New = false;
  1246. warnAbout152 = false;
  1247. break;
  1248. }
  1249. auto computeNewPath = [=](std::string const& in, std::string& result) {
  1250. auto path = cmCMakePath{ in };
  1251. if (path.IsRelative()) {
  1252. auto basePath = cmCMakePath{ *arguments.BaseDirectory };
  1253. path = basePath.Append(path);
  1254. }
  1255. result = cmSystemTools::GetActualCaseForPath(
  1256. cmSystemTools::GetRealPath(path.String()));
  1257. };
  1258. std::string realPath;
  1259. if (use152New) {
  1260. computeNewPath(input, realPath);
  1261. } else {
  1262. std::string oldPolicyPath =
  1263. cmSystemTools::CollapseFullPath(input, *arguments.BaseDirectory);
  1264. oldPolicyPath = cmSystemTools::GetRealPath(oldPolicyPath);
  1265. if (warnAbout152) {
  1266. computeNewPath(input, realPath);
  1267. if (oldPolicyPath != realPath) {
  1268. status.GetMakefile().IssueMessage(
  1269. MessageType::AUTHOR_WARNING,
  1270. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0152),
  1271. "\n"
  1272. "From input path:\n ",
  1273. input, "\nthe policy OLD behavior produces path:\n ",
  1274. oldPolicyPath,
  1275. "\nbut the policy NEW behavior produces path:\n ",
  1276. realPath,
  1277. "\nSince the policy is not set, CMake is using the OLD "
  1278. "behavior for compatibility."));
  1279. }
  1280. }
  1281. realPath = oldPolicyPath;
  1282. }
  1283. status.GetMakefile().AddDefinition(args[2], realPath);
  1284. return true;
  1285. }
  1286. bool HandleRelativePathCommand(std::vector<std::string> const& args,
  1287. cmExecutionStatus& status)
  1288. {
  1289. if (args.size() != 4) {
  1290. status.SetError("RELATIVE_PATH called with incorrect number of arguments");
  1291. return false;
  1292. }
  1293. const std::string& outVar = args[1];
  1294. const std::string& directoryName = args[2];
  1295. const std::string& fileName = args[3];
  1296. if (!cmSystemTools::FileIsFullPath(directoryName)) {
  1297. std::string errstring =
  1298. "RELATIVE_PATH must be passed a full path to the directory: " +
  1299. directoryName;
  1300. status.SetError(errstring);
  1301. return false;
  1302. }
  1303. if (!cmSystemTools::FileIsFullPath(fileName)) {
  1304. std::string errstring =
  1305. "RELATIVE_PATH must be passed a full path to the file: " + fileName;
  1306. status.SetError(errstring);
  1307. return false;
  1308. }
  1309. std::string res = cmSystemTools::RelativePath(directoryName, fileName);
  1310. status.GetMakefile().AddDefinition(outVar, res);
  1311. return true;
  1312. }
  1313. bool HandleRename(std::vector<std::string> const& args,
  1314. cmExecutionStatus& status)
  1315. {
  1316. if (args.size() < 3) {
  1317. status.SetError("RENAME must be called with at least two additional "
  1318. "arguments");
  1319. return false;
  1320. }
  1321. // Compute full path for old and new names.
  1322. std::string oldname = args[1];
  1323. if (!cmsys::SystemTools::FileIsFullPath(oldname)) {
  1324. oldname =
  1325. cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/', args[1]);
  1326. }
  1327. std::string newname = args[2];
  1328. if (!cmsys::SystemTools::FileIsFullPath(newname)) {
  1329. newname =
  1330. cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/', args[2]);
  1331. }
  1332. struct Arguments
  1333. {
  1334. bool NoReplace = false;
  1335. std::string Result;
  1336. };
  1337. static auto const parser = cmArgumentParser<Arguments>{}
  1338. .Bind("NO_REPLACE"_s, &Arguments::NoReplace)
  1339. .Bind("RESULT"_s, &Arguments::Result);
  1340. std::vector<std::string> unconsumedArgs;
  1341. Arguments const arguments =
  1342. parser.Parse(cmMakeRange(args).advance(3), &unconsumedArgs);
  1343. if (!unconsumedArgs.empty()) {
  1344. status.SetError("RENAME unknown argument:\n " + unconsumedArgs.front());
  1345. return false;
  1346. }
  1347. std::string err;
  1348. switch (cmSystemTools::RenameFile(oldname, newname,
  1349. arguments.NoReplace
  1350. ? cmSystemTools::Replace::No
  1351. : cmSystemTools::Replace::Yes,
  1352. &err)) {
  1353. case cmSystemTools::RenameResult::Success:
  1354. if (!arguments.Result.empty()) {
  1355. status.GetMakefile().AddDefinition(arguments.Result, "0");
  1356. }
  1357. return true;
  1358. case cmSystemTools::RenameResult::NoReplace:
  1359. if (!arguments.Result.empty()) {
  1360. err = "NO_REPLACE";
  1361. } else {
  1362. err = "path not replaced";
  1363. }
  1364. CM_FALLTHROUGH;
  1365. case cmSystemTools::RenameResult::Failure:
  1366. if (!arguments.Result.empty()) {
  1367. status.GetMakefile().AddDefinition(arguments.Result, err);
  1368. return true;
  1369. }
  1370. break;
  1371. }
  1372. status.SetError(cmStrCat("RENAME failed to rename\n ", oldname, "\nto\n ",
  1373. newname, "\nbecause: ", err, "\n"));
  1374. return false;
  1375. }
  1376. bool HandleCopyFile(std::vector<std::string> const& args,
  1377. cmExecutionStatus& status)
  1378. {
  1379. if (args.size() < 3) {
  1380. status.SetError("COPY_FILE must be called with at least two additional "
  1381. "arguments");
  1382. return false;
  1383. }
  1384. // Compute full path for old and new names.
  1385. std::string oldname = args[1];
  1386. if (!cmsys::SystemTools::FileIsFullPath(oldname)) {
  1387. oldname =
  1388. cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/', args[1]);
  1389. }
  1390. std::string newname = args[2];
  1391. if (!cmsys::SystemTools::FileIsFullPath(newname)) {
  1392. newname =
  1393. cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/', args[2]);
  1394. }
  1395. struct Arguments
  1396. {
  1397. bool InputMayBeRecent = false;
  1398. bool OnlyIfDifferent = false;
  1399. std::string Result;
  1400. };
  1401. static auto const parser =
  1402. cmArgumentParser<Arguments>{}
  1403. .Bind("INPUT_MAY_BE_RECENT"_s, &Arguments::InputMayBeRecent)
  1404. .Bind("ONLY_IF_DIFFERENT"_s, &Arguments::OnlyIfDifferent)
  1405. .Bind("RESULT"_s, &Arguments::Result);
  1406. std::vector<std::string> unconsumedArgs;
  1407. Arguments const arguments =
  1408. parser.Parse(cmMakeRange(args).advance(3), &unconsumedArgs);
  1409. if (!unconsumedArgs.empty()) {
  1410. status.SetError("COPY_FILE unknown argument:\n " +
  1411. unconsumedArgs.front());
  1412. return false;
  1413. }
  1414. bool result = true;
  1415. if (cmsys::SystemTools::FileIsDirectory(oldname)) {
  1416. if (!arguments.Result.empty()) {
  1417. status.GetMakefile().AddDefinition(arguments.Result,
  1418. "cannot copy a directory");
  1419. } else {
  1420. status.SetError(
  1421. cmStrCat("COPY_FILE cannot copy a directory\n ", oldname));
  1422. result = false;
  1423. }
  1424. return result;
  1425. }
  1426. if (cmsys::SystemTools::FileIsDirectory(newname)) {
  1427. if (!arguments.Result.empty()) {
  1428. status.GetMakefile().AddDefinition(arguments.Result,
  1429. "cannot copy to a directory");
  1430. } else {
  1431. status.SetError(
  1432. cmStrCat("COPY_FILE cannot copy to a directory\n ", newname));
  1433. result = false;
  1434. }
  1435. return result;
  1436. }
  1437. cmSystemTools::CopyWhen when;
  1438. if (arguments.OnlyIfDifferent) {
  1439. when = cmSystemTools::CopyWhen::OnlyIfDifferent;
  1440. } else {
  1441. when = cmSystemTools::CopyWhen::Always;
  1442. }
  1443. cmSystemTools::CopyInputRecent const inputRecent = arguments.InputMayBeRecent
  1444. ? cmSystemTools::CopyInputRecent::Yes
  1445. : cmSystemTools::CopyInputRecent::No;
  1446. std::string err;
  1447. if (cmSystemTools::CopySingleFile(oldname, newname, when, inputRecent,
  1448. &err) ==
  1449. cmSystemTools::CopyResult::Success) {
  1450. if (!arguments.Result.empty()) {
  1451. status.GetMakefile().AddDefinition(arguments.Result, "0");
  1452. }
  1453. } else {
  1454. if (!arguments.Result.empty()) {
  1455. status.GetMakefile().AddDefinition(arguments.Result, err);
  1456. } else {
  1457. status.SetError(cmStrCat("COPY_FILE failed to copy\n ", oldname,
  1458. "\nto\n ", newname, "\nbecause: ", err, "\n"));
  1459. result = false;
  1460. }
  1461. }
  1462. return result;
  1463. }
  1464. bool HandleRemoveImpl(std::vector<std::string> const& args, bool recurse,
  1465. cmExecutionStatus& status)
  1466. {
  1467. for (std::string const& arg :
  1468. cmMakeRange(args).advance(1)) // Get rid of subcommand
  1469. {
  1470. std::string fileName = arg;
  1471. if (fileName.empty()) {
  1472. std::string const r = recurse ? "REMOVE_RECURSE" : "REMOVE";
  1473. status.GetMakefile().IssueMessage(
  1474. MessageType::AUTHOR_WARNING, "Ignoring empty file name in " + r + ".");
  1475. continue;
  1476. }
  1477. if (!cmsys::SystemTools::FileIsFullPath(fileName)) {
  1478. fileName =
  1479. cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/', arg);
  1480. }
  1481. if (cmSystemTools::FileIsDirectory(fileName) &&
  1482. !cmSystemTools::FileIsSymlink(fileName) && recurse) {
  1483. cmSystemTools::RepeatedRemoveDirectory(fileName);
  1484. } else {
  1485. cmSystemTools::RemoveFile(fileName);
  1486. }
  1487. }
  1488. return true;
  1489. }
  1490. bool HandleRemove(std::vector<std::string> const& args,
  1491. cmExecutionStatus& status)
  1492. {
  1493. return HandleRemoveImpl(args, false, status);
  1494. }
  1495. bool HandleRemoveRecurse(std::vector<std::string> const& args,
  1496. cmExecutionStatus& status)
  1497. {
  1498. return HandleRemoveImpl(args, true, status);
  1499. }
  1500. std::string ToNativePath(const std::string& path)
  1501. {
  1502. const auto& outPath = cmSystemTools::ConvertToOutputPath(path);
  1503. if (outPath.size() > 1 && outPath.front() == '\"' &&
  1504. outPath.back() == '\"') {
  1505. return outPath.substr(1, outPath.size() - 2);
  1506. }
  1507. return outPath;
  1508. }
  1509. std::string ToCMakePath(const std::string& path)
  1510. {
  1511. auto temp = path;
  1512. cmSystemTools::ConvertToUnixSlashes(temp);
  1513. return temp;
  1514. }
  1515. bool HandlePathCommand(std::vector<std::string> const& args,
  1516. std::string (*convert)(std::string const&),
  1517. cmExecutionStatus& status)
  1518. {
  1519. if (args.size() != 3) {
  1520. status.SetError("FILE([TO_CMAKE_PATH|TO_NATIVE_PATH] path result) must be "
  1521. "called with exactly three arguments.");
  1522. return false;
  1523. }
  1524. #if defined(_WIN32) && !defined(__CYGWIN__)
  1525. char pathSep = ';';
  1526. #else
  1527. char pathSep = ':';
  1528. #endif
  1529. std::vector<std::string> path = cmSystemTools::SplitString(args[1], pathSep);
  1530. std::string value = cmList::to_string(cmMakeRange(path).transform(convert));
  1531. status.GetMakefile().AddDefinition(args[2], value);
  1532. return true;
  1533. }
  1534. bool HandleCMakePathCommand(std::vector<std::string> const& args,
  1535. cmExecutionStatus& status)
  1536. {
  1537. return HandlePathCommand(args, ToCMakePath, status);
  1538. }
  1539. bool HandleNativePathCommand(std::vector<std::string> const& args,
  1540. cmExecutionStatus& status)
  1541. {
  1542. return HandlePathCommand(args, ToNativePath, status);
  1543. }
  1544. #if !defined(CMAKE_BOOTSTRAP)
  1545. // Stuff for curl download/upload
  1546. using cmFileCommandVectorOfChar = std::vector<char>;
  1547. size_t cmWriteToFileCallback(void* ptr, size_t size, size_t nmemb, void* data)
  1548. {
  1549. int realsize = static_cast<int>(size * nmemb);
  1550. cmsys::ofstream* fout = static_cast<cmsys::ofstream*>(data);
  1551. if (fout) {
  1552. const char* chPtr = static_cast<char*>(ptr);
  1553. fout->write(chPtr, realsize);
  1554. }
  1555. return realsize;
  1556. }
  1557. size_t cmWriteToMemoryCallback(void* ptr, size_t size, size_t nmemb,
  1558. void* data)
  1559. {
  1560. int realsize = static_cast<int>(size * nmemb);
  1561. const char* chPtr = static_cast<char*>(ptr);
  1562. cm::append(*static_cast<cmFileCommandVectorOfChar*>(data), chPtr,
  1563. chPtr + realsize);
  1564. return realsize;
  1565. }
  1566. int cmFileCommandCurlDebugCallback(CURL*, curl_infotype type, char* chPtr,
  1567. size_t size, void* data)
  1568. {
  1569. cmFileCommandVectorOfChar& vec =
  1570. *static_cast<cmFileCommandVectorOfChar*>(data);
  1571. switch (type) {
  1572. case CURLINFO_TEXT:
  1573. case CURLINFO_HEADER_IN:
  1574. case CURLINFO_HEADER_OUT:
  1575. cm::append(vec, chPtr, chPtr + size);
  1576. break;
  1577. case CURLINFO_DATA_IN:
  1578. case CURLINFO_DATA_OUT:
  1579. case CURLINFO_SSL_DATA_IN:
  1580. case CURLINFO_SSL_DATA_OUT: {
  1581. char buf[128];
  1582. int n =
  1583. snprintf(buf, sizeof(buf), "[%" KWIML_INT_PRIu64 " bytes data]\n",
  1584. static_cast<KWIML_INT_uint64_t>(size));
  1585. if (n > 0) {
  1586. cm::append(vec, buf, buf + n);
  1587. }
  1588. } break;
  1589. default:
  1590. break;
  1591. }
  1592. return 0;
  1593. }
  1594. # if defined(LIBCURL_VERSION_NUM) && LIBCURL_VERSION_NUM >= 0x072000
  1595. const CURLoption CM_CURLOPT_XFERINFOFUNCTION = CURLOPT_XFERINFOFUNCTION;
  1596. using cm_curl_off_t = curl_off_t;
  1597. # else
  1598. const CURLoption CM_CURLOPT_XFERINFOFUNCTION = CURLOPT_PROGRESSFUNCTION;
  1599. using cm_curl_off_t = double;
  1600. # endif
  1601. class cURLProgressHelper
  1602. {
  1603. public:
  1604. cURLProgressHelper(cmMakefile* mf, const char* text)
  1605. : Makefile(mf)
  1606. , Text(text)
  1607. {
  1608. }
  1609. bool UpdatePercentage(cm_curl_off_t value, cm_curl_off_t total,
  1610. std::string& status)
  1611. {
  1612. long OldPercentage = this->CurrentPercentage;
  1613. if (total > 0) {
  1614. this->CurrentPercentage = std::lround(
  1615. static_cast<double>(value) / static_cast<double>(total) * 100.0);
  1616. if (this->CurrentPercentage > 100) {
  1617. // Avoid extra progress reports for unexpected data beyond total.
  1618. this->CurrentPercentage = 100;
  1619. }
  1620. }
  1621. bool updated = (OldPercentage != this->CurrentPercentage);
  1622. if (updated) {
  1623. status =
  1624. cmStrCat("[", this->Text, " ", this->CurrentPercentage, "% complete]");
  1625. }
  1626. return updated;
  1627. }
  1628. cmMakefile* GetMakefile() { return this->Makefile; }
  1629. private:
  1630. long CurrentPercentage = -1;
  1631. cmMakefile* Makefile;
  1632. std::string Text;
  1633. };
  1634. int cmFileDownloadProgressCallback(void* clientp, cm_curl_off_t dltotal,
  1635. cm_curl_off_t dlnow, cm_curl_off_t ultotal,
  1636. cm_curl_off_t ulnow)
  1637. {
  1638. cURLProgressHelper* helper = reinterpret_cast<cURLProgressHelper*>(clientp);
  1639. static_cast<void>(ultotal);
  1640. static_cast<void>(ulnow);
  1641. std::string status;
  1642. if (helper->UpdatePercentage(dlnow, dltotal, status)) {
  1643. cmMakefile* mf = helper->GetMakefile();
  1644. mf->DisplayStatus(status, -1);
  1645. }
  1646. return 0;
  1647. }
  1648. int cmFileUploadProgressCallback(void* clientp, cm_curl_off_t dltotal,
  1649. cm_curl_off_t dlnow, cm_curl_off_t ultotal,
  1650. cm_curl_off_t ulnow)
  1651. {
  1652. cURLProgressHelper* helper = reinterpret_cast<cURLProgressHelper*>(clientp);
  1653. static_cast<void>(dltotal);
  1654. static_cast<void>(dlnow);
  1655. std::string status;
  1656. if (helper->UpdatePercentage(ulnow, ultotal, status)) {
  1657. cmMakefile* mf = helper->GetMakefile();
  1658. mf->DisplayStatus(status, -1);
  1659. }
  1660. return 0;
  1661. }
  1662. class cURLEasyGuard
  1663. {
  1664. public:
  1665. cURLEasyGuard(CURL* easy)
  1666. : Easy(easy)
  1667. {
  1668. }
  1669. ~cURLEasyGuard()
  1670. {
  1671. if (this->Easy) {
  1672. ::curl_easy_cleanup(this->Easy);
  1673. }
  1674. }
  1675. cURLEasyGuard(const cURLEasyGuard&) = delete;
  1676. cURLEasyGuard& operator=(const cURLEasyGuard&) = delete;
  1677. void release() { this->Easy = nullptr; }
  1678. private:
  1679. ::CURL* Easy;
  1680. };
  1681. #endif
  1682. #define check_curl_result(result, errstr) \
  1683. do { \
  1684. if (result != CURLE_OK) { \
  1685. std::string e(errstr); \
  1686. e += ::curl_easy_strerror(result); \
  1687. status.SetError(e); \
  1688. return false; \
  1689. } \
  1690. } while (false)
  1691. bool HandleDownloadCommand(std::vector<std::string> const& args,
  1692. cmExecutionStatus& status)
  1693. {
  1694. #if !defined(CMAKE_BOOTSTRAP)
  1695. auto i = args.begin();
  1696. if (args.size() < 2) {
  1697. status.SetError("DOWNLOAD must be called with at least two arguments.");
  1698. return false;
  1699. }
  1700. ++i; // Get rid of subcommand
  1701. std::string url = *i;
  1702. ++i;
  1703. std::string file;
  1704. long timeout = 0;
  1705. long inactivity_timeout = 0;
  1706. std::string logVar;
  1707. std::string statusVar;
  1708. cm::optional<std::string> tls_version;
  1709. cm::optional<bool> tls_verify;
  1710. cmValue cainfo = status.GetMakefile().GetDefinition("CMAKE_TLS_CAINFO");
  1711. std::string netrc_level =
  1712. status.GetMakefile().GetSafeDefinition("CMAKE_NETRC");
  1713. std::string netrc_file =
  1714. status.GetMakefile().GetSafeDefinition("CMAKE_NETRC_FILE");
  1715. std::string expectedHash;
  1716. std::string hashMatchMSG;
  1717. std::unique_ptr<cmCryptoHash> hash;
  1718. bool showProgress = false;
  1719. std::string userpwd;
  1720. std::vector<std::string> curl_headers;
  1721. std::vector<std::pair<std::string, cm::optional<std::string>>> curl_ranges;
  1722. while (i != args.end()) {
  1723. if (*i == "TIMEOUT") {
  1724. ++i;
  1725. if (i != args.end()) {
  1726. timeout = atol(i->c_str());
  1727. } else {
  1728. status.SetError("DOWNLOAD missing time for TIMEOUT.");
  1729. return false;
  1730. }
  1731. } else if (*i == "INACTIVITY_TIMEOUT") {
  1732. ++i;
  1733. if (i != args.end()) {
  1734. inactivity_timeout = atol(i->c_str());
  1735. } else {
  1736. status.SetError("DOWNLOAD missing time for INACTIVITY_TIMEOUT.");
  1737. return false;
  1738. }
  1739. } else if (*i == "LOG") {
  1740. ++i;
  1741. if (i == args.end()) {
  1742. status.SetError("DOWNLOAD missing VAR for LOG.");
  1743. return false;
  1744. }
  1745. logVar = *i;
  1746. } else if (*i == "STATUS") {
  1747. ++i;
  1748. if (i == args.end()) {
  1749. status.SetError("DOWNLOAD missing VAR for STATUS.");
  1750. return false;
  1751. }
  1752. statusVar = *i;
  1753. } else if (*i == "TLS_VERSION") {
  1754. ++i;
  1755. if (i != args.end()) {
  1756. tls_version = *i;
  1757. } else {
  1758. status.SetError("DOWNLOAD missing value for TLS_VERSION.");
  1759. return false;
  1760. }
  1761. } else if (*i == "TLS_VERIFY") {
  1762. ++i;
  1763. if (i != args.end()) {
  1764. tls_verify = cmIsOn(*i);
  1765. } else {
  1766. status.SetError("DOWNLOAD missing bool value for TLS_VERIFY.");
  1767. return false;
  1768. }
  1769. } else if (*i == "TLS_CAINFO") {
  1770. ++i;
  1771. if (i != args.end()) {
  1772. cainfo = cmValue(*i);
  1773. } else {
  1774. status.SetError("DOWNLOAD missing file value for TLS_CAINFO.");
  1775. return false;
  1776. }
  1777. } else if (*i == "NETRC_FILE") {
  1778. ++i;
  1779. if (i != args.end()) {
  1780. netrc_file = *i;
  1781. } else {
  1782. status.SetError("DOWNLOAD missing file value for NETRC_FILE.");
  1783. return false;
  1784. }
  1785. } else if (*i == "NETRC") {
  1786. ++i;
  1787. if (i != args.end()) {
  1788. netrc_level = *i;
  1789. } else {
  1790. status.SetError("DOWNLOAD missing level value for NETRC.");
  1791. return false;
  1792. }
  1793. } else if (*i == "EXPECTED_MD5") {
  1794. ++i;
  1795. if (i == args.end()) {
  1796. status.SetError("DOWNLOAD missing sum value for EXPECTED_MD5.");
  1797. return false;
  1798. }
  1799. hash = cm::make_unique<cmCryptoHash>(cmCryptoHash::AlgoMD5);
  1800. hashMatchMSG = "MD5 sum";
  1801. expectedHash = cmSystemTools::LowerCase(*i);
  1802. } else if (*i == "SHOW_PROGRESS") {
  1803. showProgress = true;
  1804. } else if (*i == "EXPECTED_HASH") {
  1805. ++i;
  1806. if (i == args.end()) {
  1807. status.SetError("DOWNLOAD missing ALGO=value for EXPECTED_HASH.");
  1808. return false;
  1809. }
  1810. std::string::size_type pos = i->find("=");
  1811. if (pos == std::string::npos) {
  1812. std::string err =
  1813. cmStrCat("DOWNLOAD EXPECTED_HASH expects ALGO=value but got: ", *i);
  1814. status.SetError(err);
  1815. return false;
  1816. }
  1817. std::string algo = i->substr(0, pos);
  1818. expectedHash = cmSystemTools::LowerCase(i->substr(pos + 1));
  1819. hash = cmCryptoHash::New(algo);
  1820. if (!hash) {
  1821. std::string err =
  1822. cmStrCat("DOWNLOAD EXPECTED_HASH given unknown ALGO: ", algo);
  1823. status.SetError(err);
  1824. return false;
  1825. }
  1826. hashMatchMSG = algo + " hash";
  1827. } else if (*i == "USERPWD") {
  1828. ++i;
  1829. if (i == args.end()) {
  1830. status.SetError("DOWNLOAD missing string for USERPWD.");
  1831. return false;
  1832. }
  1833. userpwd = *i;
  1834. } else if (*i == "HTTPHEADER") {
  1835. ++i;
  1836. if (i == args.end()) {
  1837. status.SetError("DOWNLOAD missing string for HTTPHEADER.");
  1838. return false;
  1839. }
  1840. curl_headers.push_back(*i);
  1841. } else if (*i == "RANGE_START") {
  1842. ++i;
  1843. if (i == args.end()) {
  1844. status.SetError("DOWNLOAD missing value for RANGE_START.");
  1845. return false;
  1846. }
  1847. curl_ranges.emplace_back(*i, cm::nullopt);
  1848. } else if (*i == "RANGE_END") {
  1849. ++i;
  1850. if (curl_ranges.empty()) {
  1851. curl_ranges.emplace_back("0", *i);
  1852. } else {
  1853. auto& last_range = curl_ranges.back();
  1854. if (!last_range.second.has_value()) {
  1855. last_range.second = *i;
  1856. } else {
  1857. status.SetError("Multiple RANGE_END values is provided without "
  1858. "the corresponding RANGE_START.");
  1859. return false;
  1860. }
  1861. }
  1862. } else if (file.empty()) {
  1863. file = *i;
  1864. } else {
  1865. // Do not return error for compatibility reason.
  1866. std::string err = cmStrCat("Unexpected argument: ", *i);
  1867. status.GetMakefile().IssueMessage(MessageType::AUTHOR_WARNING, err);
  1868. }
  1869. ++i;
  1870. }
  1871. if (!tls_verify) {
  1872. if (cmValue v = status.GetMakefile().GetDefinition("CMAKE_TLS_VERIFY")) {
  1873. tls_verify = v.IsOn();
  1874. }
  1875. }
  1876. if (!tls_verify) {
  1877. if (cm::optional<std::string> v =
  1878. cmSystemTools::GetEnvVar("CMAKE_TLS_VERIFY")) {
  1879. tls_verify = cmIsOn(*v);
  1880. }
  1881. }
  1882. if (!tls_version) {
  1883. if (cmValue v = status.GetMakefile().GetDefinition("CMAKE_TLS_VERSION")) {
  1884. tls_version = *v;
  1885. }
  1886. }
  1887. if (!tls_version) {
  1888. if (cm::optional<std::string> v =
  1889. cmSystemTools::GetEnvVar("CMAKE_TLS_VERSION")) {
  1890. tls_version = std::move(v);
  1891. }
  1892. }
  1893. // Can't calculate hash if we don't save the file.
  1894. // TODO Incrementally calculate hash in the write callback as the file is
  1895. // being downloaded so this check can be relaxed.
  1896. if (file.empty() && hash) {
  1897. status.SetError("DOWNLOAD cannot calculate hash if file is not saved.");
  1898. return false;
  1899. }
  1900. // If file exists already, and caller specified an expected md5 or sha,
  1901. // and the existing file already has the expected hash, then simply
  1902. // return.
  1903. //
  1904. if (!file.empty() && cmSystemTools::FileExists(file) && hash.get()) {
  1905. std::string msg;
  1906. std::string actualHash = hash->HashFile(file);
  1907. if (actualHash == expectedHash) {
  1908. msg = cmStrCat("skipping download as file already exists with expected ",
  1909. hashMatchMSG, '"');
  1910. if (!statusVar.empty()) {
  1911. status.GetMakefile().AddDefinition(statusVar, cmStrCat(0, ";\"", msg));
  1912. }
  1913. return true;
  1914. }
  1915. }
  1916. // Make sure parent directory exists so we can write to the file
  1917. // as we receive downloaded bits from curl...
  1918. //
  1919. if (!file.empty()) {
  1920. std::string dir = cmSystemTools::GetFilenamePath(file);
  1921. if (!dir.empty() && !cmSystemTools::FileExists(dir) &&
  1922. !cmSystemTools::MakeDirectory(dir)) {
  1923. std::string errstring = "DOWNLOAD error: cannot create directory '" +
  1924. dir +
  1925. "' - Specify file by full path name and verify that you "
  1926. "have directory creation and file write privileges.";
  1927. status.SetError(errstring);
  1928. return false;
  1929. }
  1930. }
  1931. cmsys::ofstream fout;
  1932. if (!file.empty()) {
  1933. fout.open(file.c_str(), std::ios::binary);
  1934. if (!fout) {
  1935. status.SetError("DOWNLOAD cannot open file for write.");
  1936. return false;
  1937. }
  1938. }
  1939. url = cmCurlFixFileURL(url);
  1940. ::CURL* curl;
  1941. cmCurlInitOnce();
  1942. ::curl_global_init(CURL_GLOBAL_DEFAULT);
  1943. curl = cm_curl_easy_init();
  1944. if (!curl) {
  1945. status.SetError("DOWNLOAD error initializing curl.");
  1946. return false;
  1947. }
  1948. cURLEasyGuard g_curl(curl);
  1949. ::CURLcode res = ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  1950. check_curl_result(res, "DOWNLOAD cannot set url: ");
  1951. // enable HTTP ERROR parsing
  1952. res = ::curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  1953. check_curl_result(res, "DOWNLOAD cannot set http failure option: ");
  1954. res = ::curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/" LIBCURL_VERSION);
  1955. check_curl_result(res, "DOWNLOAD cannot set user agent option: ");
  1956. res = ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cmWriteToFileCallback);
  1957. check_curl_result(res, "DOWNLOAD cannot set write function: ");
  1958. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  1959. cmFileCommandCurlDebugCallback);
  1960. check_curl_result(res, "DOWNLOAD cannot set debug function: ");
  1961. if (tls_version) {
  1962. if (cm::optional<int> v = cmCurlParseTLSVersion(*tls_version)) {
  1963. res = ::curl_easy_setopt(curl, CURLOPT_SSLVERSION, *v);
  1964. check_curl_result(
  1965. res,
  1966. cmStrCat("DOWNLOAD cannot set TLS/SSL version ", *tls_version, ": "));
  1967. } else {
  1968. status.SetError(
  1969. cmStrCat("DOWNLOAD given unknown TLS/SSL version ", *tls_version));
  1970. return false;
  1971. }
  1972. }
  1973. // check to see if TLS verification is requested
  1974. if (tls_verify && *tls_verify) {
  1975. res = ::curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
  1976. check_curl_result(res, "DOWNLOAD cannot set TLS/SSL Verify on: ");
  1977. } else {
  1978. res = ::curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  1979. check_curl_result(res, "DOWNLOAD cannot set TLS/SSL Verify off: ");
  1980. }
  1981. for (const auto& range : curl_ranges) {
  1982. std::string curl_range = range.first + '-' +
  1983. (range.second.has_value() ? range.second.value() : "");
  1984. res = ::curl_easy_setopt(curl, CURLOPT_RANGE, curl_range.c_str());
  1985. check_curl_result(res, "DOWNLOAD cannot set range: ");
  1986. }
  1987. // check to see if a CAINFO file has been specified
  1988. // command arg comes first
  1989. std::string const& cainfo_err = cmCurlSetCAInfo(curl, cainfo);
  1990. if (!cainfo_err.empty()) {
  1991. status.SetError(cainfo_err);
  1992. return false;
  1993. }
  1994. // check to see if netrc parameters have been specified
  1995. // local command args takes precedence over CMAKE_NETRC*
  1996. netrc_level = cmSystemTools::UpperCase(netrc_level);
  1997. std::string const& netrc_option_err =
  1998. cmCurlSetNETRCOption(curl, netrc_level, netrc_file);
  1999. if (!netrc_option_err.empty()) {
  2000. status.SetError(netrc_option_err);
  2001. return false;
  2002. }
  2003. cmFileCommandVectorOfChar chunkDebug;
  2004. res = ::curl_easy_setopt(curl, CURLOPT_WRITEDATA,
  2005. file.empty() ? nullptr : &fout);
  2006. check_curl_result(res, "DOWNLOAD cannot set write data: ");
  2007. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &chunkDebug);
  2008. check_curl_result(res, "DOWNLOAD cannot set debug data: ");
  2009. res = ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  2010. check_curl_result(res, "DOWNLOAD cannot set follow-redirect option: ");
  2011. if (!logVar.empty()) {
  2012. res = ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  2013. check_curl_result(res, "DOWNLOAD cannot set verbose: ");
  2014. }
  2015. if (timeout > 0) {
  2016. res = ::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
  2017. check_curl_result(res, "DOWNLOAD cannot set timeout: ");
  2018. }
  2019. if (inactivity_timeout > 0) {
  2020. // Give up if there is no progress for a long time.
  2021. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  2022. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, inactivity_timeout);
  2023. }
  2024. // Need the progress helper's scope to last through the duration of
  2025. // the curl_easy_perform call... so this object is declared at function
  2026. // scope intentionally, rather than inside the "if(showProgress)"
  2027. // block...
  2028. //
  2029. cURLProgressHelper helper(&status.GetMakefile(), "download");
  2030. if (showProgress) {
  2031. res = ::curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
  2032. check_curl_result(res, "DOWNLOAD cannot set noprogress value: ");
  2033. res = ::curl_easy_setopt(curl, CM_CURLOPT_XFERINFOFUNCTION,
  2034. cmFileDownloadProgressCallback);
  2035. check_curl_result(res, "DOWNLOAD cannot set progress function: ");
  2036. res = ::curl_easy_setopt(curl, CURLOPT_PROGRESSDATA,
  2037. reinterpret_cast<void*>(&helper));
  2038. check_curl_result(res, "DOWNLOAD cannot set progress data: ");
  2039. }
  2040. if (!userpwd.empty()) {
  2041. res = ::curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd.c_str());
  2042. check_curl_result(res, "DOWNLOAD cannot set user password: ");
  2043. }
  2044. struct curl_slist* headers = nullptr;
  2045. for (std::string const& h : curl_headers) {
  2046. headers = ::curl_slist_append(headers, h.c_str());
  2047. }
  2048. ::curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  2049. res = ::curl_easy_perform(curl);
  2050. ::curl_slist_free_all(headers);
  2051. /* always cleanup */
  2052. g_curl.release();
  2053. ::curl_easy_cleanup(curl);
  2054. if (!statusVar.empty()) {
  2055. status.GetMakefile().AddDefinition(
  2056. statusVar,
  2057. cmStrCat(static_cast<int>(res), ";\"", ::curl_easy_strerror(res), "\""));
  2058. }
  2059. ::curl_global_cleanup();
  2060. // Ensure requested curl logs are returned (especially in case of failure)
  2061. //
  2062. if (!logVar.empty()) {
  2063. chunkDebug.push_back(0);
  2064. status.GetMakefile().AddDefinition(logVar, chunkDebug.data());
  2065. }
  2066. // Explicitly flush/close so we can measure the md5 accurately.
  2067. //
  2068. if (!file.empty()) {
  2069. fout.flush();
  2070. fout.close();
  2071. }
  2072. // Verify MD5 sum if requested:
  2073. //
  2074. if (hash) {
  2075. if (res != CURLE_OK) {
  2076. status.SetError(cmStrCat(
  2077. "DOWNLOAD cannot compute hash on failed download\n"
  2078. " status: [",
  2079. static_cast<int>(res), ";\"", ::curl_easy_strerror(res), "\"]"));
  2080. return false;
  2081. }
  2082. std::string actualHash = hash->HashFile(file);
  2083. if (actualHash.empty()) {
  2084. status.SetError("DOWNLOAD cannot compute hash on downloaded file");
  2085. return false;
  2086. }
  2087. if (expectedHash != actualHash) {
  2088. if (!statusVar.empty() && res == 0) {
  2089. status.GetMakefile().AddDefinition(statusVar,
  2090. "1;HASH mismatch: "
  2091. "expected: " +
  2092. expectedHash +
  2093. " actual: " + actualHash);
  2094. }
  2095. status.SetError(cmStrCat("DOWNLOAD HASH mismatch\n"
  2096. " for file: [",
  2097. file,
  2098. "]\n"
  2099. " expected hash: [",
  2100. expectedHash,
  2101. "]\n"
  2102. " actual hash: [",
  2103. actualHash, "]\n"));
  2104. return false;
  2105. }
  2106. }
  2107. return true;
  2108. #else
  2109. status.SetError("DOWNLOAD not supported by bootstrap cmake.");
  2110. return false;
  2111. #endif
  2112. }
  2113. bool HandleUploadCommand(std::vector<std::string> const& args,
  2114. cmExecutionStatus& status)
  2115. {
  2116. #if !defined(CMAKE_BOOTSTRAP)
  2117. if (args.size() < 3) {
  2118. status.SetError("UPLOAD must be called with at least three arguments.");
  2119. return false;
  2120. }
  2121. auto i = args.begin();
  2122. ++i;
  2123. std::string filename = *i;
  2124. ++i;
  2125. std::string url = *i;
  2126. ++i;
  2127. long timeout = 0;
  2128. long inactivity_timeout = 0;
  2129. std::string logVar;
  2130. std::string statusVar;
  2131. bool showProgress = false;
  2132. cm::optional<std::string> tls_version;
  2133. cm::optional<bool> tls_verify;
  2134. cmValue cainfo = status.GetMakefile().GetDefinition("CMAKE_TLS_CAINFO");
  2135. std::string userpwd;
  2136. std::string netrc_level =
  2137. status.GetMakefile().GetSafeDefinition("CMAKE_NETRC");
  2138. std::string netrc_file =
  2139. status.GetMakefile().GetSafeDefinition("CMAKE_NETRC_FILE");
  2140. std::vector<std::string> curl_headers;
  2141. while (i != args.end()) {
  2142. if (*i == "TIMEOUT") {
  2143. ++i;
  2144. if (i != args.end()) {
  2145. timeout = atol(i->c_str());
  2146. } else {
  2147. status.SetError("UPLOAD missing time for TIMEOUT.");
  2148. return false;
  2149. }
  2150. } else if (*i == "INACTIVITY_TIMEOUT") {
  2151. ++i;
  2152. if (i != args.end()) {
  2153. inactivity_timeout = atol(i->c_str());
  2154. } else {
  2155. status.SetError("UPLOAD missing time for INACTIVITY_TIMEOUT.");
  2156. return false;
  2157. }
  2158. } else if (*i == "LOG") {
  2159. ++i;
  2160. if (i == args.end()) {
  2161. status.SetError("UPLOAD missing VAR for LOG.");
  2162. return false;
  2163. }
  2164. logVar = *i;
  2165. } else if (*i == "STATUS") {
  2166. ++i;
  2167. if (i == args.end()) {
  2168. status.SetError("UPLOAD missing VAR for STATUS.");
  2169. return false;
  2170. }
  2171. statusVar = *i;
  2172. } else if (*i == "SHOW_PROGRESS") {
  2173. showProgress = true;
  2174. } else if (*i == "TLS_VERSION") {
  2175. ++i;
  2176. if (i != args.end()) {
  2177. tls_version = *i;
  2178. } else {
  2179. status.SetError("UPLOAD missing value for TLS_VERSION.");
  2180. return false;
  2181. }
  2182. } else if (*i == "TLS_VERIFY") {
  2183. ++i;
  2184. if (i != args.end()) {
  2185. tls_verify = cmIsOn(*i);
  2186. } else {
  2187. status.SetError("UPLOAD missing bool value for TLS_VERIFY.");
  2188. return false;
  2189. }
  2190. } else if (*i == "TLS_CAINFO") {
  2191. ++i;
  2192. if (i != args.end()) {
  2193. cainfo = cmValue(*i);
  2194. } else {
  2195. status.SetError("UPLOAD missing file value for TLS_CAINFO.");
  2196. return false;
  2197. }
  2198. } else if (*i == "NETRC_FILE") {
  2199. ++i;
  2200. if (i != args.end()) {
  2201. netrc_file = *i;
  2202. } else {
  2203. status.SetError("UPLOAD missing file value for NETRC_FILE.");
  2204. return false;
  2205. }
  2206. } else if (*i == "NETRC") {
  2207. ++i;
  2208. if (i != args.end()) {
  2209. netrc_level = *i;
  2210. } else {
  2211. status.SetError("UPLOAD missing level value for NETRC.");
  2212. return false;
  2213. }
  2214. } else if (*i == "USERPWD") {
  2215. ++i;
  2216. if (i == args.end()) {
  2217. status.SetError("UPLOAD missing string for USERPWD.");
  2218. return false;
  2219. }
  2220. userpwd = *i;
  2221. } else if (*i == "HTTPHEADER") {
  2222. ++i;
  2223. if (i == args.end()) {
  2224. status.SetError("UPLOAD missing string for HTTPHEADER.");
  2225. return false;
  2226. }
  2227. curl_headers.push_back(*i);
  2228. } else {
  2229. // Do not return error for compatibility reason.
  2230. std::string err = cmStrCat("Unexpected argument: ", *i);
  2231. status.GetMakefile().IssueMessage(MessageType::AUTHOR_WARNING, err);
  2232. }
  2233. ++i;
  2234. }
  2235. if (!tls_verify) {
  2236. if (cmValue v = status.GetMakefile().GetDefinition("CMAKE_TLS_VERIFY")) {
  2237. tls_verify = v.IsOn();
  2238. }
  2239. }
  2240. if (!tls_verify) {
  2241. if (cm::optional<std::string> v =
  2242. cmSystemTools::GetEnvVar("CMAKE_TLS_VERIFY")) {
  2243. tls_verify = cmIsOn(*v);
  2244. }
  2245. }
  2246. if (!tls_version) {
  2247. if (cmValue v = status.GetMakefile().GetDefinition("CMAKE_TLS_VERSION")) {
  2248. tls_version = *v;
  2249. }
  2250. }
  2251. if (!tls_version) {
  2252. if (cm::optional<std::string> v =
  2253. cmSystemTools::GetEnvVar("CMAKE_TLS_VERSION")) {
  2254. tls_version = std::move(v);
  2255. }
  2256. }
  2257. // Open file for reading:
  2258. //
  2259. FILE* fin = cmsys::SystemTools::Fopen(filename, "rb");
  2260. if (!fin) {
  2261. std::string errStr =
  2262. cmStrCat("UPLOAD cannot open file '", filename, "' for reading.");
  2263. status.SetError(errStr);
  2264. return false;
  2265. }
  2266. unsigned long file_size = cmsys::SystemTools::FileLength(filename);
  2267. url = cmCurlFixFileURL(url);
  2268. ::CURL* curl;
  2269. cmCurlInitOnce();
  2270. ::curl_global_init(CURL_GLOBAL_DEFAULT);
  2271. curl = cm_curl_easy_init();
  2272. if (!curl) {
  2273. status.SetError("UPLOAD error initializing curl.");
  2274. fclose(fin);
  2275. return false;
  2276. }
  2277. cURLEasyGuard g_curl(curl);
  2278. // enable HTTP ERROR parsing
  2279. ::CURLcode res = ::curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  2280. check_curl_result(res, "UPLOAD cannot set fail on error flag: ");
  2281. // enable uploading
  2282. res = ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
  2283. check_curl_result(res, "UPLOAD cannot set upload flag: ");
  2284. res = ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  2285. check_curl_result(res, "UPLOAD cannot set url: ");
  2286. res =
  2287. ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cmWriteToMemoryCallback);
  2288. check_curl_result(res, "UPLOAD cannot set write function: ");
  2289. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  2290. cmFileCommandCurlDebugCallback);
  2291. check_curl_result(res, "UPLOAD cannot set debug function: ");
  2292. if (tls_version) {
  2293. if (cm::optional<int> v = cmCurlParseTLSVersion(*tls_version)) {
  2294. res = ::curl_easy_setopt(curl, CURLOPT_SSLVERSION, *v);
  2295. check_curl_result(
  2296. res,
  2297. cmStrCat("UPLOAD cannot set TLS/SSL version ", *tls_version, ": "));
  2298. } else {
  2299. status.SetError(
  2300. cmStrCat("UPLOAD given unknown TLS/SSL version ", *tls_version));
  2301. return false;
  2302. }
  2303. }
  2304. // check to see if TLS verification is requested
  2305. if (tls_verify && *tls_verify) {
  2306. res = ::curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
  2307. check_curl_result(res, "UPLOAD cannot set TLS/SSL Verify on: ");
  2308. } else {
  2309. res = ::curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  2310. check_curl_result(res, "UPLOAD cannot set TLS/SSL Verify off: ");
  2311. }
  2312. // check to see if a CAINFO file has been specified
  2313. // command arg comes first
  2314. std::string const& cainfo_err = cmCurlSetCAInfo(curl, cainfo);
  2315. if (!cainfo_err.empty()) {
  2316. status.SetError(cainfo_err);
  2317. return false;
  2318. }
  2319. cmFileCommandVectorOfChar chunkResponse;
  2320. cmFileCommandVectorOfChar chunkDebug;
  2321. res = ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, &chunkResponse);
  2322. check_curl_result(res, "UPLOAD cannot set write data: ");
  2323. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &chunkDebug);
  2324. check_curl_result(res, "UPLOAD cannot set debug data: ");
  2325. res = ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  2326. check_curl_result(res, "UPLOAD cannot set follow-redirect option: ");
  2327. if (!logVar.empty()) {
  2328. res = ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  2329. check_curl_result(res, "UPLOAD cannot set verbose: ");
  2330. }
  2331. if (timeout > 0) {
  2332. res = ::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
  2333. check_curl_result(res, "UPLOAD cannot set timeout: ");
  2334. }
  2335. if (inactivity_timeout > 0) {
  2336. // Give up if there is no progress for a long time.
  2337. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  2338. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, inactivity_timeout);
  2339. }
  2340. // Need the progress helper's scope to last through the duration of
  2341. // the curl_easy_perform call... so this object is declared at function
  2342. // scope intentionally, rather than inside the "if(showProgress)"
  2343. // block...
  2344. //
  2345. cURLProgressHelper helper(&status.GetMakefile(), "upload");
  2346. if (showProgress) {
  2347. res = ::curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
  2348. check_curl_result(res, "UPLOAD cannot set noprogress value: ");
  2349. res = ::curl_easy_setopt(curl, CM_CURLOPT_XFERINFOFUNCTION,
  2350. cmFileUploadProgressCallback);
  2351. check_curl_result(res, "UPLOAD cannot set progress function: ");
  2352. res = ::curl_easy_setopt(curl, CURLOPT_PROGRESSDATA,
  2353. reinterpret_cast<void*>(&helper));
  2354. check_curl_result(res, "UPLOAD cannot set progress data: ");
  2355. }
  2356. // now specify which file to upload
  2357. res = ::curl_easy_setopt(curl, CURLOPT_INFILE, fin);
  2358. check_curl_result(res, "UPLOAD cannot set input file: ");
  2359. // and give the size of the upload (optional)
  2360. res =
  2361. ::curl_easy_setopt(curl, CURLOPT_INFILESIZE, static_cast<long>(file_size));
  2362. check_curl_result(res, "UPLOAD cannot set input file size: ");
  2363. if (!userpwd.empty()) {
  2364. res = ::curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd.c_str());
  2365. check_curl_result(res, "UPLOAD cannot set user password: ");
  2366. }
  2367. // check to see if netrc parameters have been specified
  2368. // local command args takes precedence over CMAKE_NETRC*
  2369. netrc_level = cmSystemTools::UpperCase(netrc_level);
  2370. std::string const& netrc_option_err =
  2371. cmCurlSetNETRCOption(curl, netrc_level, netrc_file);
  2372. if (!netrc_option_err.empty()) {
  2373. status.SetError(netrc_option_err);
  2374. return false;
  2375. }
  2376. struct curl_slist* headers = nullptr;
  2377. for (std::string const& h : curl_headers) {
  2378. headers = ::curl_slist_append(headers, h.c_str());
  2379. }
  2380. ::curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  2381. res = ::curl_easy_perform(curl);
  2382. ::curl_slist_free_all(headers);
  2383. /* always cleanup */
  2384. g_curl.release();
  2385. ::curl_easy_cleanup(curl);
  2386. if (!statusVar.empty()) {
  2387. status.GetMakefile().AddDefinition(
  2388. statusVar,
  2389. cmStrCat(static_cast<int>(res), ";\"", ::curl_easy_strerror(res), "\""));
  2390. }
  2391. ::curl_global_cleanup();
  2392. fclose(fin);
  2393. fin = nullptr;
  2394. if (!logVar.empty()) {
  2395. std::string log;
  2396. if (!chunkResponse.empty()) {
  2397. chunkResponse.push_back(0);
  2398. log += "Response:\n";
  2399. log += chunkResponse.data();
  2400. log += "\n";
  2401. }
  2402. if (!chunkDebug.empty()) {
  2403. chunkDebug.push_back(0);
  2404. log += "Debug:\n";
  2405. log += chunkDebug.data();
  2406. log += "\n";
  2407. }
  2408. status.GetMakefile().AddDefinition(logVar, log);
  2409. }
  2410. return true;
  2411. #else
  2412. status.SetError("UPLOAD not supported by bootstrap cmake.");
  2413. return false;
  2414. #endif
  2415. }
  2416. void AddEvaluationFile(const std::string& inputName,
  2417. const std::string& targetName,
  2418. const std::string& outputExpr,
  2419. const std::string& condition, bool inputIsContent,
  2420. const std::string& newLineCharacter, mode_t permissions,
  2421. cmExecutionStatus& status)
  2422. {
  2423. cmListFileBacktrace lfbt = status.GetMakefile().GetBacktrace();
  2424. cmGeneratorExpression outputGe(*status.GetMakefile().GetCMakeInstance(),
  2425. lfbt);
  2426. std::unique_ptr<cmCompiledGeneratorExpression> outputCge =
  2427. outputGe.Parse(outputExpr);
  2428. cmGeneratorExpression conditionGe(*status.GetMakefile().GetCMakeInstance(),
  2429. lfbt);
  2430. std::unique_ptr<cmCompiledGeneratorExpression> conditionCge =
  2431. conditionGe.Parse(condition);
  2432. status.GetMakefile().AddEvaluationFile(
  2433. inputName, targetName, std::move(outputCge), std::move(conditionCge),
  2434. newLineCharacter, permissions, inputIsContent);
  2435. }
  2436. bool HandleGenerateCommand(std::vector<std::string> const& args,
  2437. cmExecutionStatus& status)
  2438. {
  2439. if (args.size() < 5) {
  2440. status.SetError("Incorrect arguments to GENERATE subcommand.");
  2441. return false;
  2442. }
  2443. struct Arguments : public ArgumentParser::ParseResult
  2444. {
  2445. cm::optional<std::string> Output;
  2446. cm::optional<std::string> Input;
  2447. cm::optional<std::string> Content;
  2448. cm::optional<std::string> Condition;
  2449. cm::optional<std::string> Target;
  2450. cm::optional<std::string> NewLineStyle;
  2451. bool NoSourcePermissions = false;
  2452. bool UseSourcePermissions = false;
  2453. ArgumentParser::NonEmpty<std::vector<std::string>> FilePermissions;
  2454. std::vector<cm::string_view> ParsedKeywords;
  2455. };
  2456. static auto const parser =
  2457. cmArgumentParser<Arguments>{}
  2458. .Bind("OUTPUT"_s, &Arguments::Output)
  2459. .Bind("INPUT"_s, &Arguments::Input)
  2460. .Bind("CONTENT"_s, &Arguments::Content)
  2461. .Bind("CONDITION"_s, &Arguments::Condition)
  2462. .Bind("TARGET"_s, &Arguments::Target)
  2463. .Bind("NO_SOURCE_PERMISSIONS"_s, &Arguments::NoSourcePermissions)
  2464. .Bind("USE_SOURCE_PERMISSIONS"_s, &Arguments::UseSourcePermissions)
  2465. .Bind("FILE_PERMISSIONS"_s, &Arguments::FilePermissions)
  2466. .Bind("NEWLINE_STYLE"_s, &Arguments::NewLineStyle)
  2467. .BindParsedKeywords(&Arguments::ParsedKeywords);
  2468. std::vector<std::string> unparsedArguments;
  2469. Arguments const arguments =
  2470. parser.Parse(cmMakeRange(args).advance(1), &unparsedArguments);
  2471. if (arguments.MaybeReportError(status.GetMakefile())) {
  2472. return true;
  2473. }
  2474. if (!unparsedArguments.empty()) {
  2475. status.SetError("Unknown argument to GENERATE subcommand.");
  2476. return false;
  2477. }
  2478. if (!arguments.Output || arguments.ParsedKeywords[0] != "OUTPUT"_s) {
  2479. status.SetError("GENERATE requires OUTPUT as first option.");
  2480. return false;
  2481. }
  2482. std::string const& output = *arguments.Output;
  2483. if (!arguments.Input && !arguments.Content) {
  2484. status.SetError("GENERATE requires INPUT or CONTENT option.");
  2485. return false;
  2486. }
  2487. const bool inputIsContent = arguments.ParsedKeywords[1] == "CONTENT"_s;
  2488. if (!inputIsContent && arguments.ParsedKeywords[1] != "INPUT") {
  2489. status.SetError("Unknown argument to GENERATE subcommand.");
  2490. return false;
  2491. }
  2492. std::string const& input =
  2493. inputIsContent ? *arguments.Content : *arguments.Input;
  2494. if (arguments.Condition && arguments.Condition->empty()) {
  2495. status.SetError("CONDITION of sub-command GENERATE must not be empty "
  2496. "if specified.");
  2497. return false;
  2498. }
  2499. std::string const& condition =
  2500. arguments.Condition ? *arguments.Condition : std::string();
  2501. if (arguments.Target && arguments.Target->empty()) {
  2502. status.SetError("TARGET of sub-command GENERATE must not be empty "
  2503. "if specified.");
  2504. return false;
  2505. }
  2506. std::string const& target =
  2507. arguments.Target ? *arguments.Target : std::string();
  2508. cmNewLineStyle newLineStyle;
  2509. if (arguments.NewLineStyle) {
  2510. std::string errorMessage;
  2511. if (!newLineStyle.ReadFromArguments(args, errorMessage)) {
  2512. status.SetError(cmStrCat("GENERATE ", errorMessage));
  2513. return false;
  2514. }
  2515. }
  2516. if (arguments.NoSourcePermissions && arguments.UseSourcePermissions) {
  2517. status.SetError("given both NO_SOURCE_PERMISSIONS and "
  2518. "USE_SOURCE_PERMISSIONS. Only one option allowed.");
  2519. return false;
  2520. }
  2521. if (!arguments.FilePermissions.empty()) {
  2522. if (arguments.NoSourcePermissions) {
  2523. status.SetError("given both NO_SOURCE_PERMISSIONS and "
  2524. "FILE_PERMISSIONS. Only one option allowed.");
  2525. return false;
  2526. }
  2527. if (arguments.UseSourcePermissions) {
  2528. status.SetError("given both USE_SOURCE_PERMISSIONS and "
  2529. "FILE_PERMISSIONS. Only one option allowed.");
  2530. return false;
  2531. }
  2532. }
  2533. if (arguments.UseSourcePermissions) {
  2534. if (inputIsContent) {
  2535. status.SetError("given USE_SOURCE_PERMISSIONS without a file INPUT.");
  2536. return false;
  2537. }
  2538. }
  2539. mode_t permissions = 0;
  2540. if (arguments.NoSourcePermissions) {
  2541. permissions |= cmFSPermissions::mode_owner_read;
  2542. permissions |= cmFSPermissions::mode_owner_write;
  2543. permissions |= cmFSPermissions::mode_group_read;
  2544. permissions |= cmFSPermissions::mode_world_read;
  2545. }
  2546. if (!arguments.FilePermissions.empty()) {
  2547. std::vector<std::string> invalidOptions;
  2548. for (auto const& e : arguments.FilePermissions) {
  2549. if (!cmFSPermissions::stringToModeT(e, permissions)) {
  2550. invalidOptions.push_back(e);
  2551. }
  2552. }
  2553. if (!invalidOptions.empty()) {
  2554. std::ostringstream oss;
  2555. oss << "given invalid permission ";
  2556. for (auto i = 0u; i < invalidOptions.size(); i++) {
  2557. if (i == 0u) {
  2558. oss << "\"" << invalidOptions[i] << "\"";
  2559. } else {
  2560. oss << ",\"" << invalidOptions[i] << "\"";
  2561. }
  2562. }
  2563. oss << ".";
  2564. status.SetError(oss.str());
  2565. return false;
  2566. }
  2567. }
  2568. AddEvaluationFile(input, target, output, condition, inputIsContent,
  2569. newLineStyle.GetCharacters(), permissions, status);
  2570. return true;
  2571. }
  2572. bool HandleLockCommand(std::vector<std::string> const& args,
  2573. cmExecutionStatus& status)
  2574. {
  2575. #if !defined(CMAKE_BOOTSTRAP)
  2576. // Default values
  2577. bool directory = false;
  2578. bool release = false;
  2579. enum Guard
  2580. {
  2581. GUARD_FUNCTION,
  2582. GUARD_FILE,
  2583. GUARD_PROCESS
  2584. };
  2585. Guard guard = GUARD_PROCESS;
  2586. std::string resultVariable;
  2587. unsigned long timeout = static_cast<unsigned long>(-1);
  2588. // Parse arguments
  2589. if (args.size() < 2) {
  2590. status.GetMakefile().IssueMessage(
  2591. MessageType::FATAL_ERROR,
  2592. "sub-command LOCK requires at least two arguments.");
  2593. return false;
  2594. }
  2595. std::string path = args[1];
  2596. for (unsigned i = 2; i < args.size(); ++i) {
  2597. if (args[i] == "DIRECTORY") {
  2598. directory = true;
  2599. } else if (args[i] == "RELEASE") {
  2600. release = true;
  2601. } else if (args[i] == "GUARD") {
  2602. ++i;
  2603. const char* merr = "expected FUNCTION, FILE or PROCESS after GUARD";
  2604. if (i >= args.size()) {
  2605. status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR, merr);
  2606. return false;
  2607. }
  2608. if (args[i] == "FUNCTION") {
  2609. guard = GUARD_FUNCTION;
  2610. } else if (args[i] == "FILE") {
  2611. guard = GUARD_FILE;
  2612. } else if (args[i] == "PROCESS") {
  2613. guard = GUARD_PROCESS;
  2614. } else {
  2615. status.GetMakefile().IssueMessage(
  2616. MessageType::FATAL_ERROR,
  2617. cmStrCat(merr, ", but got:\n \"", args[i], "\"."));
  2618. return false;
  2619. }
  2620. } else if (args[i] == "RESULT_VARIABLE") {
  2621. ++i;
  2622. if (i >= args.size()) {
  2623. status.GetMakefile().IssueMessage(
  2624. MessageType::FATAL_ERROR,
  2625. "expected variable name after RESULT_VARIABLE");
  2626. return false;
  2627. }
  2628. resultVariable = args[i];
  2629. } else if (args[i] == "TIMEOUT") {
  2630. ++i;
  2631. if (i >= args.size()) {
  2632. status.GetMakefile().IssueMessage(
  2633. MessageType::FATAL_ERROR, "expected timeout value after TIMEOUT");
  2634. return false;
  2635. }
  2636. long scanned;
  2637. if (!cmStrToLong(args[i], &scanned) || scanned < 0) {
  2638. status.GetMakefile().IssueMessage(
  2639. MessageType::FATAL_ERROR,
  2640. cmStrCat("TIMEOUT value \"", args[i],
  2641. "\" is not an unsigned integer."));
  2642. return false;
  2643. }
  2644. timeout = static_cast<unsigned long>(scanned);
  2645. } else {
  2646. status.GetMakefile().IssueMessage(
  2647. MessageType::FATAL_ERROR,
  2648. cmStrCat("expected DIRECTORY, RELEASE, GUARD, RESULT_VARIABLE or ",
  2649. "TIMEOUT\nbut got: \"", args[i], "\"."));
  2650. return false;
  2651. }
  2652. }
  2653. if (directory) {
  2654. path += "/cmake.lock";
  2655. }
  2656. // Unify path (remove '//', '/../', ...)
  2657. path = cmSystemTools::CollapseFullPath(
  2658. path, status.GetMakefile().GetCurrentSourceDirectory());
  2659. // Create file and directories if needed
  2660. std::string parentDir = cmSystemTools::GetParentDirectory(path);
  2661. if (!cmSystemTools::MakeDirectory(parentDir)) {
  2662. status.GetMakefile().IssueMessage(
  2663. MessageType::FATAL_ERROR,
  2664. cmStrCat("directory\n \"", parentDir,
  2665. "\"\ncreation failed (check permissions)."));
  2666. cmSystemTools::SetFatalErrorOccurred();
  2667. return false;
  2668. }
  2669. FILE* file = cmsys::SystemTools::Fopen(path, "w");
  2670. if (!file) {
  2671. status.GetMakefile().IssueMessage(
  2672. MessageType::FATAL_ERROR,
  2673. cmStrCat("file\n \"", path,
  2674. "\"\ncreation failed (check permissions)."));
  2675. cmSystemTools::SetFatalErrorOccurred();
  2676. return false;
  2677. }
  2678. fclose(file);
  2679. // Actual lock/unlock
  2680. cmFileLockPool& lockPool =
  2681. status.GetMakefile().GetGlobalGenerator()->GetFileLockPool();
  2682. cmFileLockResult fileLockResult(cmFileLockResult::MakeOk());
  2683. if (release) {
  2684. fileLockResult = lockPool.Release(path);
  2685. } else {
  2686. switch (guard) {
  2687. case GUARD_FUNCTION:
  2688. fileLockResult = lockPool.LockFunctionScope(path, timeout);
  2689. break;
  2690. case GUARD_FILE:
  2691. fileLockResult = lockPool.LockFileScope(path, timeout);
  2692. break;
  2693. case GUARD_PROCESS:
  2694. fileLockResult = lockPool.LockProcessScope(path, timeout);
  2695. break;
  2696. default:
  2697. cmSystemTools::SetFatalErrorOccurred();
  2698. return false;
  2699. }
  2700. }
  2701. const std::string result = fileLockResult.GetOutputMessage();
  2702. if (resultVariable.empty() && !fileLockResult.IsOk()) {
  2703. status.GetMakefile().IssueMessage(
  2704. MessageType::FATAL_ERROR,
  2705. cmStrCat("error locking file\n \"", path, "\"\n", result, "."));
  2706. cmSystemTools::SetFatalErrorOccurred();
  2707. return false;
  2708. }
  2709. if (!resultVariable.empty()) {
  2710. status.GetMakefile().AddDefinition(resultVariable, result);
  2711. }
  2712. return true;
  2713. #else
  2714. static_cast<void>(args);
  2715. status.SetError("sub-command LOCK not implemented in bootstrap cmake");
  2716. return false;
  2717. #endif
  2718. }
  2719. bool HandleTimestampCommand(std::vector<std::string> const& args,
  2720. cmExecutionStatus& status)
  2721. {
  2722. if (args.size() < 3) {
  2723. status.SetError("sub-command TIMESTAMP requires at least two arguments.");
  2724. return false;
  2725. }
  2726. if (args.size() > 5) {
  2727. status.SetError("sub-command TIMESTAMP takes at most four arguments.");
  2728. return false;
  2729. }
  2730. unsigned int argsIndex = 1;
  2731. std::string filename = args[argsIndex++];
  2732. if (!cmsys::SystemTools::FileIsFullPath(filename)) {
  2733. filename = cmStrCat(status.GetMakefile().GetCurrentSourceDirectory(), '/',
  2734. filename);
  2735. }
  2736. const std::string& outputVariable = args[argsIndex++];
  2737. std::string formatString;
  2738. if (args.size() > argsIndex && args[argsIndex] != "UTC") {
  2739. formatString = args[argsIndex++];
  2740. }
  2741. bool utcFlag = false;
  2742. if (args.size() > argsIndex) {
  2743. if (args[argsIndex] == "UTC") {
  2744. utcFlag = true;
  2745. } else {
  2746. std::string e = " TIMESTAMP sub-command does not recognize option " +
  2747. args[argsIndex] + ".";
  2748. status.SetError(e);
  2749. return false;
  2750. }
  2751. }
  2752. cmTimestamp timestamp;
  2753. std::string result =
  2754. timestamp.FileModificationTime(filename.c_str(), formatString, utcFlag);
  2755. status.GetMakefile().AddDefinition(outputVariable, result);
  2756. return true;
  2757. }
  2758. bool HandleSizeCommand(std::vector<std::string> const& args,
  2759. cmExecutionStatus& status)
  2760. {
  2761. if (args.size() != 3) {
  2762. status.SetError(
  2763. cmStrCat(args[0], " requires a file name and output variable"));
  2764. return false;
  2765. }
  2766. unsigned int argsIndex = 1;
  2767. const std::string& filename = args[argsIndex++];
  2768. const std::string& outputVariable = args[argsIndex++];
  2769. if (!cmSystemTools::FileExists(filename, true)) {
  2770. status.SetError(
  2771. cmStrCat("SIZE requested of path that is not readable:\n ", filename));
  2772. return false;
  2773. }
  2774. status.GetMakefile().AddDefinition(
  2775. outputVariable, std::to_string(cmSystemTools::FileLength(filename)));
  2776. return true;
  2777. }
  2778. bool HandleReadSymlinkCommand(std::vector<std::string> const& args,
  2779. cmExecutionStatus& status)
  2780. {
  2781. if (args.size() != 3) {
  2782. status.SetError(
  2783. cmStrCat(args[0], " requires a file name and output variable"));
  2784. return false;
  2785. }
  2786. const std::string& filename = args[1];
  2787. const std::string& outputVariable = args[2];
  2788. std::string result;
  2789. if (!cmSystemTools::ReadSymlink(filename, result)) {
  2790. status.SetError(cmStrCat(
  2791. "READ_SYMLINK requested of path that is not a symlink:\n ", filename));
  2792. return false;
  2793. }
  2794. status.GetMakefile().AddDefinition(outputVariable, result);
  2795. return true;
  2796. }
  2797. bool HandleCreateLinkCommand(std::vector<std::string> const& args,
  2798. cmExecutionStatus& status)
  2799. {
  2800. if (args.size() < 3) {
  2801. status.SetError("CREATE_LINK must be called with at least two additional "
  2802. "arguments");
  2803. return false;
  2804. }
  2805. std::string const& fileName = args[1];
  2806. std::string const& newFileName = args[2];
  2807. struct Arguments
  2808. {
  2809. std::string Result;
  2810. bool CopyOnError = false;
  2811. bool Symbolic = false;
  2812. };
  2813. static auto const parser =
  2814. cmArgumentParser<Arguments>{}
  2815. .Bind("RESULT"_s, &Arguments::Result)
  2816. .Bind("COPY_ON_ERROR"_s, &Arguments::CopyOnError)
  2817. .Bind("SYMBOLIC"_s, &Arguments::Symbolic);
  2818. std::vector<std::string> unconsumedArgs;
  2819. Arguments const arguments =
  2820. parser.Parse(cmMakeRange(args).advance(3), &unconsumedArgs);
  2821. if (!unconsumedArgs.empty()) {
  2822. status.SetError("unknown argument: \"" + unconsumedArgs.front() + '\"');
  2823. return false;
  2824. }
  2825. // The system error message generated in the operation.
  2826. std::string result;
  2827. // Check if the paths are distinct.
  2828. if (fileName == newFileName) {
  2829. result = "CREATE_LINK cannot use same file and newfile";
  2830. if (!arguments.Result.empty()) {
  2831. status.GetMakefile().AddDefinition(arguments.Result, result);
  2832. return true;
  2833. }
  2834. status.SetError(result);
  2835. return false;
  2836. }
  2837. // Hard link requires original file to exist.
  2838. if (!arguments.Symbolic && !cmSystemTools::FileExists(fileName)) {
  2839. result = "Cannot hard link \'" + fileName + "\' as it does not exist.";
  2840. if (!arguments.Result.empty()) {
  2841. status.GetMakefile().AddDefinition(arguments.Result, result);
  2842. return true;
  2843. }
  2844. status.SetError(result);
  2845. return false;
  2846. }
  2847. // Check if the new file already exists and remove it.
  2848. if (cmSystemTools::PathExists(newFileName) &&
  2849. !cmSystemTools::RemoveFile(newFileName)) {
  2850. auto err = cmStrCat("Failed to create link '", newFileName,
  2851. "' because existing path cannot be removed: ",
  2852. cmSystemTools::GetLastSystemError(), '\n');
  2853. if (!arguments.Result.empty()) {
  2854. status.GetMakefile().AddDefinition(arguments.Result, err);
  2855. return true;
  2856. }
  2857. status.SetError(err);
  2858. return false;
  2859. }
  2860. // Whether the operation completed successfully.
  2861. bool completed = false;
  2862. // Check if the command requires a symbolic link.
  2863. if (arguments.Symbolic) {
  2864. cmsys::Status linked =
  2865. cmSystemTools::CreateSymlinkQuietly(fileName, newFileName);
  2866. if (linked) {
  2867. completed = true;
  2868. } else {
  2869. result = cmStrCat("failed to create symbolic link '", newFileName,
  2870. "': ", linked.GetString());
  2871. }
  2872. } else {
  2873. cmsys::Status linked =
  2874. cmSystemTools::CreateLinkQuietly(fileName, newFileName);
  2875. if (linked) {
  2876. completed = true;
  2877. } else {
  2878. result = cmStrCat("failed to create link '", newFileName,
  2879. "': ", linked.GetString());
  2880. }
  2881. }
  2882. // Check if copy-on-error is enabled in the arguments.
  2883. if (!completed && arguments.CopyOnError) {
  2884. cmsys::Status copied =
  2885. cmsys::SystemTools::CopyFileAlways(fileName, newFileName);
  2886. if (copied) {
  2887. completed = true;
  2888. } else {
  2889. result = "Copy failed: " + copied.GetString();
  2890. }
  2891. }
  2892. // Check if the operation was successful.
  2893. if (completed) {
  2894. result = "0";
  2895. } else if (arguments.Result.empty()) {
  2896. // The operation failed and the result is not reported in a variable.
  2897. status.SetError(result);
  2898. return false;
  2899. }
  2900. if (!arguments.Result.empty()) {
  2901. status.GetMakefile().AddDefinition(arguments.Result, result);
  2902. }
  2903. return true;
  2904. }
  2905. bool HandleGetRuntimeDependenciesCommand(std::vector<std::string> const& args,
  2906. cmExecutionStatus& status)
  2907. {
  2908. std::string platform =
  2909. status.GetMakefile().GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
  2910. if (!cmRuntimeDependencyArchive::PlatformSupportsRuntimeDependencies(
  2911. platform)) {
  2912. status.SetError(
  2913. cmStrCat("GET_RUNTIME_DEPENDENCIES is not supported on system \"",
  2914. platform, "\""));
  2915. cmSystemTools::SetFatalErrorOccurred();
  2916. return false;
  2917. }
  2918. if (status.GetMakefile().GetState()->GetMode() == cmState::Project) {
  2919. status.GetMakefile().IssueMessage(
  2920. MessageType::AUTHOR_WARNING,
  2921. "You have used file(GET_RUNTIME_DEPENDENCIES)"
  2922. " in project mode. This is probably not what "
  2923. "you intended to do. Instead, please consider"
  2924. " using it in an install(CODE) or "
  2925. "install(SCRIPT) command. For example:"
  2926. "\n install(CODE [["
  2927. "\n file(GET_RUNTIME_DEPENDENCIES"
  2928. "\n # ..."
  2929. "\n )"
  2930. "\n ]])");
  2931. }
  2932. struct Arguments : public ArgumentParser::ParseResult
  2933. {
  2934. std::string ResolvedDependenciesVar;
  2935. std::string UnresolvedDependenciesVar;
  2936. std::string ConflictingDependenciesPrefix;
  2937. std::string RPathPrefix;
  2938. std::string BundleExecutable;
  2939. ArgumentParser::MaybeEmpty<std::vector<std::string>> Executables;
  2940. ArgumentParser::MaybeEmpty<std::vector<std::string>> Libraries;
  2941. ArgumentParser::MaybeEmpty<std::vector<std::string>> Directories;
  2942. ArgumentParser::MaybeEmpty<std::vector<std::string>> Modules;
  2943. ArgumentParser::MaybeEmpty<std::vector<std::string>> PreIncludeRegexes;
  2944. ArgumentParser::MaybeEmpty<std::vector<std::string>> PreExcludeRegexes;
  2945. ArgumentParser::MaybeEmpty<std::vector<std::string>> PostIncludeRegexes;
  2946. ArgumentParser::MaybeEmpty<std::vector<std::string>> PostExcludeRegexes;
  2947. ArgumentParser::MaybeEmpty<std::vector<std::string>> PostIncludeFiles;
  2948. ArgumentParser::MaybeEmpty<std::vector<std::string>> PostExcludeFiles;
  2949. ArgumentParser::MaybeEmpty<std::vector<std::string>>
  2950. PostExcludeFilesStrict;
  2951. };
  2952. static auto const parser =
  2953. cmArgumentParser<Arguments>{}
  2954. .Bind("RESOLVED_DEPENDENCIES_VAR"_s, &Arguments::ResolvedDependenciesVar)
  2955. .Bind("UNRESOLVED_DEPENDENCIES_VAR"_s,
  2956. &Arguments::UnresolvedDependenciesVar)
  2957. .Bind("CONFLICTING_DEPENDENCIES_PREFIX"_s,
  2958. &Arguments::ConflictingDependenciesPrefix)
  2959. .Bind("RPATH_PREFIX"_s, &Arguments::RPathPrefix)
  2960. .Bind("BUNDLE_EXECUTABLE"_s, &Arguments::BundleExecutable)
  2961. .Bind("EXECUTABLES"_s, &Arguments::Executables)
  2962. .Bind("LIBRARIES"_s, &Arguments::Libraries)
  2963. .Bind("MODULES"_s, &Arguments::Modules)
  2964. .Bind("DIRECTORIES"_s, &Arguments::Directories)
  2965. .Bind("PRE_INCLUDE_REGEXES"_s, &Arguments::PreIncludeRegexes)
  2966. .Bind("PRE_EXCLUDE_REGEXES"_s, &Arguments::PreExcludeRegexes)
  2967. .Bind("POST_INCLUDE_REGEXES"_s, &Arguments::PostIncludeRegexes)
  2968. .Bind("POST_EXCLUDE_REGEXES"_s, &Arguments::PostExcludeRegexes)
  2969. .Bind("POST_INCLUDE_FILES"_s, &Arguments::PostIncludeFiles)
  2970. .Bind("POST_EXCLUDE_FILES"_s, &Arguments::PostExcludeFiles)
  2971. .Bind("POST_EXCLUDE_FILES_STRICT"_s, &Arguments::PostExcludeFilesStrict);
  2972. std::vector<std::string> unrecognizedArguments;
  2973. auto parsedArgs =
  2974. parser.Parse(cmMakeRange(args).advance(1), &unrecognizedArguments);
  2975. auto argIt = unrecognizedArguments.begin();
  2976. if (argIt != unrecognizedArguments.end()) {
  2977. status.SetError(cmStrCat("Unrecognized argument: \"", *argIt, "\""));
  2978. cmSystemTools::SetFatalErrorOccurred();
  2979. return false;
  2980. }
  2981. if (parsedArgs.MaybeReportError(status.GetMakefile())) {
  2982. cmSystemTools::SetFatalErrorOccurred();
  2983. return true;
  2984. }
  2985. cmRuntimeDependencyArchive archive(
  2986. status, parsedArgs.Directories, parsedArgs.BundleExecutable,
  2987. parsedArgs.PreIncludeRegexes, parsedArgs.PreExcludeRegexes,
  2988. parsedArgs.PostIncludeRegexes, parsedArgs.PostExcludeRegexes,
  2989. std::move(parsedArgs.PostIncludeFiles),
  2990. std::move(parsedArgs.PostExcludeFiles),
  2991. std::move(parsedArgs.PostExcludeFilesStrict));
  2992. if (!archive.Prepare()) {
  2993. cmSystemTools::SetFatalErrorOccurred();
  2994. return false;
  2995. }
  2996. if (!archive.GetRuntimeDependencies(
  2997. parsedArgs.Executables, parsedArgs.Libraries, parsedArgs.Modules)) {
  2998. cmSystemTools::SetFatalErrorOccurred();
  2999. return false;
  3000. }
  3001. std::vector<std::string> deps;
  3002. std::vector<std::string> unresolvedDeps;
  3003. std::vector<std::string> conflictingDeps;
  3004. for (auto const& val : archive.GetResolvedPaths()) {
  3005. bool unique = true;
  3006. auto it = val.second.begin();
  3007. assert(it != val.second.end());
  3008. auto const& firstPath = *it;
  3009. while (++it != val.second.end()) {
  3010. if (!cmSystemTools::SameFile(firstPath, *it)) {
  3011. unique = false;
  3012. break;
  3013. }
  3014. }
  3015. if (unique) {
  3016. deps.push_back(firstPath);
  3017. if (!parsedArgs.RPathPrefix.empty()) {
  3018. status.GetMakefile().AddDefinition(
  3019. parsedArgs.RPathPrefix + "_" + firstPath,
  3020. cmList::to_string(archive.GetRPaths().at(firstPath)));
  3021. }
  3022. } else if (!parsedArgs.ConflictingDependenciesPrefix.empty()) {
  3023. conflictingDeps.push_back(val.first);
  3024. std::vector<std::string> paths;
  3025. paths.insert(paths.begin(), val.second.begin(), val.second.end());
  3026. std::string varName =
  3027. parsedArgs.ConflictingDependenciesPrefix + "_" + val.first;
  3028. std::string pathsStr = cmList::to_string(paths);
  3029. status.GetMakefile().AddDefinition(varName, pathsStr);
  3030. } else {
  3031. std::ostringstream e;
  3032. e << "Multiple conflicting paths found for " << val.first << ":";
  3033. for (auto const& path : val.second) {
  3034. e << "\n " << path;
  3035. }
  3036. status.SetError(e.str());
  3037. cmSystemTools::SetFatalErrorOccurred();
  3038. return false;
  3039. }
  3040. }
  3041. if (!archive.GetUnresolvedPaths().empty()) {
  3042. if (!parsedArgs.UnresolvedDependenciesVar.empty()) {
  3043. unresolvedDeps.insert(unresolvedDeps.begin(),
  3044. archive.GetUnresolvedPaths().begin(),
  3045. archive.GetUnresolvedPaths().end());
  3046. } else {
  3047. std::ostringstream e;
  3048. e << "Could not resolve runtime dependencies:";
  3049. for (auto const& path : archive.GetUnresolvedPaths()) {
  3050. e << "\n " << path;
  3051. }
  3052. status.SetError(e.str());
  3053. cmSystemTools::SetFatalErrorOccurred();
  3054. return false;
  3055. }
  3056. }
  3057. if (!parsedArgs.ResolvedDependenciesVar.empty()) {
  3058. std::string val = cmList::to_string(deps);
  3059. status.GetMakefile().AddDefinition(parsedArgs.ResolvedDependenciesVar,
  3060. val);
  3061. }
  3062. if (!parsedArgs.UnresolvedDependenciesVar.empty()) {
  3063. std::string val = cmList::to_string(unresolvedDeps);
  3064. status.GetMakefile().AddDefinition(parsedArgs.UnresolvedDependenciesVar,
  3065. val);
  3066. }
  3067. if (!parsedArgs.ConflictingDependenciesPrefix.empty()) {
  3068. std::string val = cmList::to_string(conflictingDeps);
  3069. status.GetMakefile().AddDefinition(
  3070. parsedArgs.ConflictingDependenciesPrefix + "_FILENAMES", val);
  3071. }
  3072. return true;
  3073. }
  3074. bool HandleConfigureCommand(std::vector<std::string> const& args,
  3075. cmExecutionStatus& status)
  3076. {
  3077. struct Arguments : public ArgumentParser::ParseResult
  3078. {
  3079. cm::optional<std::string> Output;
  3080. cm::optional<std::string> Content;
  3081. bool EscapeQuotes = false;
  3082. bool AtOnly = false;
  3083. // "NEWLINE_STYLE" requires one value, but we use a custom check below.
  3084. ArgumentParser::Maybe<std::string> NewlineStyle;
  3085. };
  3086. static auto const parser =
  3087. cmArgumentParser<Arguments>{}
  3088. .Bind("OUTPUT"_s, &Arguments::Output)
  3089. .Bind("CONTENT"_s, &Arguments::Content)
  3090. .Bind("ESCAPE_QUOTES"_s, &Arguments::EscapeQuotes)
  3091. .Bind("@ONLY"_s, &Arguments::AtOnly)
  3092. .Bind("NEWLINE_STYLE"_s, &Arguments::NewlineStyle);
  3093. std::vector<std::string> unrecognizedArguments;
  3094. auto parsedArgs =
  3095. parser.Parse(cmMakeRange(args).advance(1), &unrecognizedArguments);
  3096. auto argIt = unrecognizedArguments.begin();
  3097. if (argIt != unrecognizedArguments.end()) {
  3098. status.SetError(
  3099. cmStrCat("CONFIGURE Unrecognized argument: \"", *argIt, "\""));
  3100. cmSystemTools::SetFatalErrorOccurred();
  3101. return false;
  3102. }
  3103. if (parsedArgs.MaybeReportError(status.GetMakefile())) {
  3104. cmSystemTools::SetFatalErrorOccurred();
  3105. return true;
  3106. }
  3107. if (!parsedArgs.Output) {
  3108. status.SetError("CONFIGURE OUTPUT option is mandatory.");
  3109. cmSystemTools::SetFatalErrorOccurred();
  3110. return false;
  3111. }
  3112. if (!parsedArgs.Content) {
  3113. status.SetError("CONFIGURE CONTENT option is mandatory.");
  3114. cmSystemTools::SetFatalErrorOccurred();
  3115. return false;
  3116. }
  3117. std::string errorMessage;
  3118. cmNewLineStyle newLineStyle;
  3119. if (!newLineStyle.ReadFromArguments(args, errorMessage)) {
  3120. status.SetError(cmStrCat("CONFIGURE ", errorMessage));
  3121. return false;
  3122. }
  3123. // Check for generator expressions
  3124. std::string outputFile = cmSystemTools::CollapseFullPath(
  3125. *parsedArgs.Output, status.GetMakefile().GetCurrentBinaryDirectory());
  3126. std::string::size_type pos = outputFile.find_first_of("<>");
  3127. if (pos != std::string::npos) {
  3128. status.SetError(cmStrCat("CONFIGURE called with OUTPUT containing a \"",
  3129. outputFile[pos],
  3130. "\". This character is not allowed."));
  3131. return false;
  3132. }
  3133. cmMakefile& makeFile = status.GetMakefile();
  3134. if (!makeFile.CanIWriteThisFile(outputFile)) {
  3135. cmSystemTools::Error("Attempt to write file: " + outputFile +
  3136. " into a source directory.");
  3137. return false;
  3138. }
  3139. cmSystemTools::ConvertToUnixSlashes(outputFile);
  3140. // Re-generate if non-temporary outputs are missing.
  3141. // when we finalize the configuration we will remove all
  3142. // output files that now don't exist.
  3143. makeFile.AddCMakeOutputFile(outputFile);
  3144. // Create output directory
  3145. const std::string::size_type slashPos = outputFile.rfind('/');
  3146. if (slashPos != std::string::npos) {
  3147. const std::string path = outputFile.substr(0, slashPos);
  3148. cmSystemTools::MakeDirectory(path);
  3149. }
  3150. std::string newLineCharacters = "\n";
  3151. bool open_with_binary_flag = false;
  3152. if (newLineStyle.IsValid()) {
  3153. newLineCharacters = newLineStyle.GetCharacters();
  3154. open_with_binary_flag = true;
  3155. }
  3156. cmGeneratedFileStream fout;
  3157. fout.Open(outputFile, false, open_with_binary_flag);
  3158. if (!fout) {
  3159. cmSystemTools::Error("Could not open file for write in copy operation " +
  3160. outputFile);
  3161. cmSystemTools::ReportLastSystemError("");
  3162. return false;
  3163. }
  3164. fout.SetCopyIfDifferent(true);
  3165. // copy input to output and expand variables from input at the same time
  3166. std::stringstream sin(*parsedArgs.Content, std::ios::in);
  3167. std::string inLine;
  3168. std::string outLine;
  3169. bool hasNewLine = false;
  3170. while (cmSystemTools::GetLineFromStream(sin, inLine, &hasNewLine)) {
  3171. outLine.clear();
  3172. makeFile.ConfigureString(inLine, outLine, parsedArgs.AtOnly,
  3173. parsedArgs.EscapeQuotes);
  3174. fout << outLine;
  3175. if (hasNewLine || newLineStyle.IsValid()) {
  3176. fout << newLineCharacters;
  3177. }
  3178. }
  3179. // close file before attempting to copy
  3180. fout.close();
  3181. return true;
  3182. }
  3183. bool HandleArchiveCreateCommand(std::vector<std::string> const& args,
  3184. cmExecutionStatus& status)
  3185. {
  3186. struct Arguments : public ArgumentParser::ParseResult
  3187. {
  3188. std::string Output;
  3189. std::string Format;
  3190. std::string Compression;
  3191. std::string CompressionLevel;
  3192. // "MTIME" should require one value, but it has long been accidentally
  3193. // accepted without one and treated as if an empty value were given.
  3194. // Fixing this would require a policy.
  3195. ArgumentParser::Maybe<std::string> MTime;
  3196. bool Verbose = false;
  3197. // "PATHS" requires at least one value, but use a custom check below.
  3198. ArgumentParser::MaybeEmpty<std::vector<std::string>> Paths;
  3199. };
  3200. static auto const parser =
  3201. cmArgumentParser<Arguments>{}
  3202. .Bind("OUTPUT"_s, &Arguments::Output)
  3203. .Bind("FORMAT"_s, &Arguments::Format)
  3204. .Bind("COMPRESSION"_s, &Arguments::Compression)
  3205. .Bind("COMPRESSION_LEVEL"_s, &Arguments::CompressionLevel)
  3206. .Bind("MTIME"_s, &Arguments::MTime)
  3207. .Bind("VERBOSE"_s, &Arguments::Verbose)
  3208. .Bind("PATHS"_s, &Arguments::Paths);
  3209. std::vector<std::string> unrecognizedArguments;
  3210. auto parsedArgs =
  3211. parser.Parse(cmMakeRange(args).advance(1), &unrecognizedArguments);
  3212. auto argIt = unrecognizedArguments.begin();
  3213. if (argIt != unrecognizedArguments.end()) {
  3214. status.SetError(cmStrCat("Unrecognized argument: \"", *argIt, "\""));
  3215. cmSystemTools::SetFatalErrorOccurred();
  3216. return false;
  3217. }
  3218. if (parsedArgs.MaybeReportError(status.GetMakefile())) {
  3219. cmSystemTools::SetFatalErrorOccurred();
  3220. return true;
  3221. }
  3222. const char* knownFormats[] = {
  3223. "7zip", "gnutar", "pax", "paxr", "raw", "zip"
  3224. };
  3225. if (!parsedArgs.Format.empty() &&
  3226. !cm::contains(knownFormats, parsedArgs.Format)) {
  3227. status.SetError(
  3228. cmStrCat("archive format ", parsedArgs.Format, " not supported"));
  3229. cmSystemTools::SetFatalErrorOccurred();
  3230. return false;
  3231. }
  3232. const char* zipFileFormats[] = { "7zip", "zip" };
  3233. if (!parsedArgs.Compression.empty() &&
  3234. cm::contains(zipFileFormats, parsedArgs.Format)) {
  3235. status.SetError(cmStrCat("archive format ", parsedArgs.Format,
  3236. " does not support COMPRESSION arguments"));
  3237. cmSystemTools::SetFatalErrorOccurred();
  3238. return false;
  3239. }
  3240. static std::map<std::string, cmSystemTools::cmTarCompression>
  3241. compressionTypeMap = { { "None", cmSystemTools::TarCompressNone },
  3242. { "BZip2", cmSystemTools::TarCompressBZip2 },
  3243. { "GZip", cmSystemTools::TarCompressGZip },
  3244. { "XZ", cmSystemTools::TarCompressXZ },
  3245. { "Zstd", cmSystemTools::TarCompressZstd } };
  3246. cmSystemTools::cmTarCompression compress = cmSystemTools::TarCompressNone;
  3247. auto typeIt = compressionTypeMap.find(parsedArgs.Compression);
  3248. if (typeIt != compressionTypeMap.end()) {
  3249. compress = typeIt->second;
  3250. } else if (!parsedArgs.Compression.empty()) {
  3251. status.SetError(cmStrCat("compression type ", parsedArgs.Compression,
  3252. " is not supported"));
  3253. cmSystemTools::SetFatalErrorOccurred();
  3254. return false;
  3255. }
  3256. int compressionLevel = 0;
  3257. int minCompressionLevel = 0;
  3258. int maxCompressionLevel = 9;
  3259. if (compress == cmSystemTools::TarCompressZstd) {
  3260. maxCompressionLevel = 19;
  3261. }
  3262. if (!parsedArgs.CompressionLevel.empty()) {
  3263. if (parsedArgs.CompressionLevel.size() != 1 &&
  3264. !std::isdigit(parsedArgs.CompressionLevel[0])) {
  3265. status.SetError(
  3266. cmStrCat("compression level ", parsedArgs.CompressionLevel, " for ",
  3267. parsedArgs.Compression, " should be in range ",
  3268. minCompressionLevel, " to ", maxCompressionLevel));
  3269. cmSystemTools::SetFatalErrorOccurred();
  3270. return false;
  3271. }
  3272. compressionLevel = std::stoi(parsedArgs.CompressionLevel);
  3273. if (compressionLevel < minCompressionLevel ||
  3274. compressionLevel > maxCompressionLevel) {
  3275. status.SetError(
  3276. cmStrCat("compression level ", parsedArgs.CompressionLevel, " for ",
  3277. parsedArgs.Compression, " should be in range ",
  3278. minCompressionLevel, " to ", maxCompressionLevel));
  3279. cmSystemTools::SetFatalErrorOccurred();
  3280. return false;
  3281. }
  3282. if (compress == cmSystemTools::TarCompressNone) {
  3283. status.SetError(cmStrCat("compression level is not supported for "
  3284. "compression \"None\"",
  3285. parsedArgs.Compression));
  3286. cmSystemTools::SetFatalErrorOccurred();
  3287. return false;
  3288. }
  3289. }
  3290. if (parsedArgs.Paths.empty()) {
  3291. status.SetError("ARCHIVE_CREATE requires a non-empty list of PATHS");
  3292. cmSystemTools::SetFatalErrorOccurred();
  3293. return false;
  3294. }
  3295. if (!cmSystemTools::CreateTar(parsedArgs.Output, parsedArgs.Paths, compress,
  3296. parsedArgs.Verbose, parsedArgs.MTime,
  3297. parsedArgs.Format, compressionLevel)) {
  3298. status.SetError(cmStrCat("failed to compress: ", parsedArgs.Output));
  3299. cmSystemTools::SetFatalErrorOccurred();
  3300. return false;
  3301. }
  3302. return true;
  3303. }
  3304. bool HandleArchiveExtractCommand(std::vector<std::string> const& args,
  3305. cmExecutionStatus& status)
  3306. {
  3307. struct Arguments : public ArgumentParser::ParseResult
  3308. {
  3309. std::string Input;
  3310. bool Verbose = false;
  3311. bool ListOnly = false;
  3312. std::string Destination;
  3313. ArgumentParser::MaybeEmpty<std::vector<std::string>> Patterns;
  3314. bool Touch = false;
  3315. };
  3316. static auto const parser = cmArgumentParser<Arguments>{}
  3317. .Bind("INPUT"_s, &Arguments::Input)
  3318. .Bind("VERBOSE"_s, &Arguments::Verbose)
  3319. .Bind("LIST_ONLY"_s, &Arguments::ListOnly)
  3320. .Bind("DESTINATION"_s, &Arguments::Destination)
  3321. .Bind("PATTERNS"_s, &Arguments::Patterns)
  3322. .Bind("TOUCH"_s, &Arguments::Touch);
  3323. std::vector<std::string> unrecognizedArguments;
  3324. auto parsedArgs =
  3325. parser.Parse(cmMakeRange(args).advance(1), &unrecognizedArguments);
  3326. auto argIt = unrecognizedArguments.begin();
  3327. if (argIt != unrecognizedArguments.end()) {
  3328. status.SetError(cmStrCat("Unrecognized argument: \"", *argIt, "\""));
  3329. cmSystemTools::SetFatalErrorOccurred();
  3330. return false;
  3331. }
  3332. if (parsedArgs.MaybeReportError(status.GetMakefile())) {
  3333. cmSystemTools::SetFatalErrorOccurred();
  3334. return true;
  3335. }
  3336. std::string inFile = parsedArgs.Input;
  3337. if (parsedArgs.ListOnly) {
  3338. if (!cmSystemTools::ListTar(inFile, parsedArgs.Patterns,
  3339. parsedArgs.Verbose)) {
  3340. status.SetError(cmStrCat("failed to list: ", inFile));
  3341. cmSystemTools::SetFatalErrorOccurred();
  3342. return false;
  3343. }
  3344. } else {
  3345. std::string destDir = status.GetMakefile().GetCurrentBinaryDirectory();
  3346. if (!parsedArgs.Destination.empty()) {
  3347. if (cmSystemTools::FileIsFullPath(parsedArgs.Destination)) {
  3348. destDir = parsedArgs.Destination;
  3349. } else {
  3350. destDir = cmStrCat(destDir, "/", parsedArgs.Destination);
  3351. }
  3352. if (!cmSystemTools::MakeDirectory(destDir)) {
  3353. status.SetError(cmStrCat("failed to create directory: ", destDir));
  3354. cmSystemTools::SetFatalErrorOccurred();
  3355. return false;
  3356. }
  3357. if (!cmSystemTools::FileIsFullPath(inFile)) {
  3358. inFile =
  3359. cmStrCat(cmSystemTools::GetCurrentWorkingDirectory(), "/", inFile);
  3360. }
  3361. }
  3362. cmWorkingDirectory workdir(destDir);
  3363. if (workdir.Failed()) {
  3364. status.SetError(
  3365. cmStrCat("failed to change working directory to: ", destDir));
  3366. cmSystemTools::SetFatalErrorOccurred();
  3367. return false;
  3368. }
  3369. if (!cmSystemTools::ExtractTar(
  3370. inFile, parsedArgs.Patterns,
  3371. parsedArgs.Touch ? cmSystemTools::cmTarExtractTimestamps::No
  3372. : cmSystemTools::cmTarExtractTimestamps::Yes,
  3373. parsedArgs.Verbose)) {
  3374. status.SetError(cmStrCat("failed to extract: ", inFile));
  3375. cmSystemTools::SetFatalErrorOccurred();
  3376. return false;
  3377. }
  3378. }
  3379. return true;
  3380. }
  3381. bool ValidateAndConvertPermissions(
  3382. cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> const&
  3383. permissions,
  3384. mode_t& perms, cmExecutionStatus& status)
  3385. {
  3386. if (!permissions) {
  3387. return true;
  3388. }
  3389. for (const auto& i : *permissions) {
  3390. if (!cmFSPermissions::stringToModeT(i, perms)) {
  3391. status.SetError(i + " is an invalid permission specifier");
  3392. cmSystemTools::SetFatalErrorOccurred();
  3393. return false;
  3394. }
  3395. }
  3396. return true;
  3397. }
  3398. bool SetPermissions(const std::string& filename, const mode_t& perms,
  3399. cmExecutionStatus& status)
  3400. {
  3401. if (!cmSystemTools::SetPermissions(filename, perms)) {
  3402. status.SetError("Failed to set permissions for " + filename);
  3403. cmSystemTools::SetFatalErrorOccurred();
  3404. return false;
  3405. }
  3406. return true;
  3407. }
  3408. bool HandleChmodCommandImpl(std::vector<std::string> const& args, bool recurse,
  3409. cmExecutionStatus& status)
  3410. {
  3411. mode_t perms = 0;
  3412. mode_t fperms = 0;
  3413. mode_t dperms = 0;
  3414. cmsys::Glob globber;
  3415. globber.SetRecurse(recurse);
  3416. globber.SetRecurseListDirs(recurse);
  3417. struct Arguments : public ArgumentParser::ParseResult
  3418. {
  3419. cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>>
  3420. Permissions;
  3421. cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>>
  3422. FilePermissions;
  3423. cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>>
  3424. DirectoryPermissions;
  3425. };
  3426. static auto const parser =
  3427. cmArgumentParser<Arguments>{}
  3428. .Bind("PERMISSIONS"_s, &Arguments::Permissions)
  3429. .Bind("FILE_PERMISSIONS"_s, &Arguments::FilePermissions)
  3430. .Bind("DIRECTORY_PERMISSIONS"_s, &Arguments::DirectoryPermissions);
  3431. std::vector<std::string> pathEntries;
  3432. Arguments parsedArgs =
  3433. parser.Parse(cmMakeRange(args).advance(1), &pathEntries);
  3434. // check validity of arguments
  3435. if (!parsedArgs.Permissions && !parsedArgs.FilePermissions &&
  3436. !parsedArgs.DirectoryPermissions) // no permissions given
  3437. {
  3438. status.SetError("No permissions given");
  3439. cmSystemTools::SetFatalErrorOccurred();
  3440. return false;
  3441. }
  3442. if (parsedArgs.Permissions && parsedArgs.FilePermissions &&
  3443. parsedArgs.DirectoryPermissions) // all keywords are used
  3444. {
  3445. status.SetError("Remove either PERMISSIONS or FILE_PERMISSIONS or "
  3446. "DIRECTORY_PERMISSIONS from the invocation");
  3447. cmSystemTools::SetFatalErrorOccurred();
  3448. return false;
  3449. }
  3450. if (parsedArgs.MaybeReportError(status.GetMakefile())) {
  3451. cmSystemTools::SetFatalErrorOccurred();
  3452. return true;
  3453. }
  3454. // validate permissions
  3455. bool validatePermissions =
  3456. ValidateAndConvertPermissions(parsedArgs.Permissions, perms, status) &&
  3457. ValidateAndConvertPermissions(parsedArgs.FilePermissions, fperms,
  3458. status) &&
  3459. ValidateAndConvertPermissions(parsedArgs.DirectoryPermissions, dperms,
  3460. status);
  3461. if (!validatePermissions) {
  3462. return false;
  3463. }
  3464. std::vector<std::string> allPathEntries;
  3465. if (recurse) {
  3466. std::vector<std::string> tempPathEntries;
  3467. for (const auto& i : pathEntries) {
  3468. if (cmSystemTools::FileIsDirectory(i)) {
  3469. globber.FindFiles(i + "/*");
  3470. tempPathEntries = globber.GetFiles();
  3471. allPathEntries.insert(allPathEntries.end(), tempPathEntries.begin(),
  3472. tempPathEntries.end());
  3473. allPathEntries.emplace_back(i);
  3474. } else {
  3475. allPathEntries.emplace_back(i); // We validate path entries below
  3476. }
  3477. }
  3478. } else {
  3479. allPathEntries = std::move(pathEntries);
  3480. }
  3481. // chmod
  3482. for (const auto& i : allPathEntries) {
  3483. if (!(cmSystemTools::FileExists(i) || cmSystemTools::FileIsDirectory(i))) {
  3484. status.SetError(cmStrCat("does not exist:\n ", i));
  3485. cmSystemTools::SetFatalErrorOccurred();
  3486. return false;
  3487. }
  3488. if (cmSystemTools::FileExists(i, true)) {
  3489. bool success = true;
  3490. const mode_t& filePermissions =
  3491. parsedArgs.FilePermissions ? fperms : perms;
  3492. if (filePermissions) {
  3493. success = SetPermissions(i, filePermissions, status);
  3494. }
  3495. if (!success) {
  3496. return false;
  3497. }
  3498. }
  3499. else if (cmSystemTools::FileIsDirectory(i)) {
  3500. bool success = true;
  3501. const mode_t& directoryPermissions =
  3502. parsedArgs.DirectoryPermissions ? dperms : perms;
  3503. if (directoryPermissions) {
  3504. success = SetPermissions(i, directoryPermissions, status);
  3505. }
  3506. if (!success) {
  3507. return false;
  3508. }
  3509. }
  3510. }
  3511. return true;
  3512. }
  3513. bool HandleChmodCommand(std::vector<std::string> const& args,
  3514. cmExecutionStatus& status)
  3515. {
  3516. return HandleChmodCommandImpl(args, false, status);
  3517. }
  3518. bool HandleChmodRecurseCommand(std::vector<std::string> const& args,
  3519. cmExecutionStatus& status)
  3520. {
  3521. return HandleChmodCommandImpl(args, true, status);
  3522. }
  3523. } // namespace
  3524. bool cmFileCommand(std::vector<std::string> const& args,
  3525. cmExecutionStatus& status)
  3526. {
  3527. if (args.empty()) {
  3528. status.SetError(
  3529. "given no arguments, but it requires at least a sub-command.");
  3530. return false;
  3531. }
  3532. static cmSubcommandTable const subcommand{
  3533. { "WRITE"_s, HandleWriteCommand },
  3534. { "APPEND"_s, HandleAppendCommand },
  3535. { "DOWNLOAD"_s, HandleDownloadCommand },
  3536. { "UPLOAD"_s, HandleUploadCommand },
  3537. { "READ"_s, HandleReadCommand },
  3538. { "MD5"_s, HandleHashCommand },
  3539. { "SHA1"_s, HandleHashCommand },
  3540. { "SHA224"_s, HandleHashCommand },
  3541. { "SHA256"_s, HandleHashCommand },
  3542. { "SHA384"_s, HandleHashCommand },
  3543. { "SHA512"_s, HandleHashCommand },
  3544. { "SHA3_224"_s, HandleHashCommand },
  3545. { "SHA3_256"_s, HandleHashCommand },
  3546. { "SHA3_384"_s, HandleHashCommand },
  3547. { "SHA3_512"_s, HandleHashCommand },
  3548. { "STRINGS"_s, HandleStringsCommand },
  3549. { "GLOB"_s, HandleGlobCommand },
  3550. { "GLOB_RECURSE"_s, HandleGlobRecurseCommand },
  3551. { "MAKE_DIRECTORY"_s, HandleMakeDirectoryCommand },
  3552. { "RENAME"_s, HandleRename },
  3553. { "COPY_FILE"_s, HandleCopyFile },
  3554. { "REMOVE"_s, HandleRemove },
  3555. { "REMOVE_RECURSE"_s, HandleRemoveRecurse },
  3556. { "COPY"_s, HandleCopyCommand },
  3557. { "INSTALL"_s, HandleInstallCommand },
  3558. { "DIFFERENT"_s, HandleDifferentCommand },
  3559. { "RPATH_CHANGE"_s, HandleRPathChangeCommand },
  3560. { "CHRPATH"_s, HandleRPathChangeCommand },
  3561. { "RPATH_SET"_s, HandleRPathSetCommand },
  3562. { "RPATH_CHECK"_s, HandleRPathCheckCommand },
  3563. { "RPATH_REMOVE"_s, HandleRPathRemoveCommand },
  3564. { "READ_ELF"_s, HandleReadElfCommand },
  3565. { "READ_MACHO"_s, HandleReadMachoCommand },
  3566. { "REAL_PATH"_s, HandleRealPathCommand },
  3567. { "RELATIVE_PATH"_s, HandleRelativePathCommand },
  3568. { "TO_CMAKE_PATH"_s, HandleCMakePathCommand },
  3569. { "TO_NATIVE_PATH"_s, HandleNativePathCommand },
  3570. { "TOUCH"_s, HandleTouchCommand },
  3571. { "TOUCH_NOCREATE"_s, HandleTouchNocreateCommand },
  3572. { "TIMESTAMP"_s, HandleTimestampCommand },
  3573. { "GENERATE"_s, HandleGenerateCommand },
  3574. { "LOCK"_s, HandleLockCommand },
  3575. { "SIZE"_s, HandleSizeCommand },
  3576. { "READ_SYMLINK"_s, HandleReadSymlinkCommand },
  3577. { "CREATE_LINK"_s, HandleCreateLinkCommand },
  3578. { "GET_RUNTIME_DEPENDENCIES"_s, HandleGetRuntimeDependenciesCommand },
  3579. { "CONFIGURE"_s, HandleConfigureCommand },
  3580. { "ARCHIVE_CREATE"_s, HandleArchiveCreateCommand },
  3581. { "ARCHIVE_EXTRACT"_s, HandleArchiveExtractCommand },
  3582. { "CHMOD"_s, HandleChmodCommand },
  3583. { "CHMOD_RECURSE"_s, HandleChmodRecurseCommand },
  3584. };
  3585. return subcommand(args[0], args, status);
  3586. }