cmFileCommand.cxx 92 KB

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