1
0

cmFileCommand.cxx 126 KB

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