cmFileCommand.cxx 87 KB

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