cmFileCommand.cxx 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244
  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. #if defined(CMAKE_BUILD_WITH_CMAKE)
  18. #include "cm_curl.h"
  19. #endif
  20. #undef GetCurrentDirectory
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <cmsys/auto_ptr.hxx>
  24. #include <cmsys/Directory.hxx>
  25. #include <cmsys/Glob.hxx>
  26. #include <cmsys/RegularExpression.hxx>
  27. // Table of permissions flags.
  28. #if defined(_WIN32) && !defined(__CYGWIN__)
  29. static mode_t mode_owner_read = S_IREAD;
  30. static mode_t mode_owner_write = S_IWRITE;
  31. static mode_t mode_owner_execute = S_IEXEC;
  32. static mode_t mode_group_read = 0;
  33. static mode_t mode_group_write = 0;
  34. static mode_t mode_group_execute = 0;
  35. static mode_t mode_world_read = 0;
  36. static mode_t mode_world_write = 0;
  37. static mode_t mode_world_execute = 0;
  38. static mode_t mode_setuid = 0;
  39. static mode_t mode_setgid = 0;
  40. #else
  41. static mode_t mode_owner_read = S_IRUSR;
  42. static mode_t mode_owner_write = S_IWUSR;
  43. static mode_t mode_owner_execute = S_IXUSR;
  44. static mode_t mode_group_read = S_IRGRP;
  45. static mode_t mode_group_write = S_IWGRP;
  46. static mode_t mode_group_execute = S_IXGRP;
  47. static mode_t mode_world_read = S_IROTH;
  48. static mode_t mode_world_write = S_IWOTH;
  49. static mode_t mode_world_execute = S_IXOTH;
  50. static mode_t mode_setuid = S_ISUID;
  51. static mode_t mode_setgid = S_ISGID;
  52. #endif
  53. // cmLibraryCommand
  54. bool cmFileCommand
  55. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  56. {
  57. if(args.size() < 2 )
  58. {
  59. this->SetError("must be called with at least two arguments.");
  60. return false;
  61. }
  62. std::string subCommand = args[0];
  63. if ( subCommand == "WRITE" )
  64. {
  65. return this->HandleWriteCommand(args, false);
  66. }
  67. else if ( subCommand == "APPEND" )
  68. {
  69. return this->HandleWriteCommand(args, true);
  70. }
  71. else if ( subCommand == "DOWNLOAD" )
  72. {
  73. return this->HandleDownloadCommand(args);
  74. }
  75. else if ( subCommand == "UPLOAD" )
  76. {
  77. return this->HandleUploadCommand(args);
  78. }
  79. else if ( subCommand == "READ" )
  80. {
  81. return this->HandleReadCommand(args);
  82. }
  83. else if ( subCommand == "MD5" ||
  84. subCommand == "SHA1" ||
  85. subCommand == "SHA224" ||
  86. subCommand == "SHA256" ||
  87. subCommand == "SHA384" ||
  88. subCommand == "SHA512" )
  89. {
  90. return this->HandleHashCommand(args);
  91. }
  92. else if ( subCommand == "STRINGS" )
  93. {
  94. return this->HandleStringsCommand(args);
  95. }
  96. else if ( subCommand == "GLOB" )
  97. {
  98. return this->HandleGlobCommand(args, false);
  99. }
  100. else if ( subCommand == "GLOB_RECURSE" )
  101. {
  102. return this->HandleGlobCommand(args, true);
  103. }
  104. else if ( subCommand == "MAKE_DIRECTORY" )
  105. {
  106. return this->HandleMakeDirectoryCommand(args);
  107. }
  108. else if ( subCommand == "RENAME" )
  109. {
  110. return this->HandleRename(args);
  111. }
  112. else if ( subCommand == "REMOVE" )
  113. {
  114. return this->HandleRemove(args, false);
  115. }
  116. else if ( subCommand == "REMOVE_RECURSE" )
  117. {
  118. return this->HandleRemove(args, true);
  119. }
  120. else if ( subCommand == "COPY" )
  121. {
  122. return this->HandleCopyCommand(args);
  123. }
  124. else if ( subCommand == "INSTALL" )
  125. {
  126. return this->HandleInstallCommand(args);
  127. }
  128. else if ( subCommand == "DIFFERENT" )
  129. {
  130. return this->HandleDifferentCommand(args);
  131. }
  132. else if ( subCommand == "RPATH_CHANGE" || subCommand == "CHRPATH" )
  133. {
  134. return this->HandleRPathChangeCommand(args);
  135. }
  136. else if ( subCommand == "RPATH_CHECK" )
  137. {
  138. return this->HandleRPathCheckCommand(args);
  139. }
  140. else if ( subCommand == "RPATH_REMOVE" )
  141. {
  142. return this->HandleRPathRemoveCommand(args);
  143. }
  144. else if ( subCommand == "RELATIVE_PATH" )
  145. {
  146. return this->HandleRelativePathCommand(args);
  147. }
  148. else if ( subCommand == "TO_CMAKE_PATH" )
  149. {
  150. return this->HandleCMakePathCommand(args, false);
  151. }
  152. else if ( subCommand == "TO_NATIVE_PATH" )
  153. {
  154. return this->HandleCMakePathCommand(args, true);
  155. }
  156. std::string e = "does not recognize sub-command "+subCommand;
  157. this->SetError(e.c_str());
  158. return false;
  159. }
  160. //----------------------------------------------------------------------------
  161. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  162. bool append)
  163. {
  164. std::string message;
  165. std::vector<std::string>::const_iterator i = args.begin();
  166. i++; // Get rid of subcommand
  167. std::string fileName = *i;
  168. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  169. {
  170. fileName = this->Makefile->GetCurrentDirectory();
  171. fileName += "/" + *i;
  172. }
  173. i++;
  174. for(;i != args.end(); ++i)
  175. {
  176. message += *i;
  177. }
  178. if ( !this->Makefile->CanIWriteThisFile(fileName.c_str()) )
  179. {
  180. std::string e
  181. = "attempted to write a file: " + fileName +
  182. " into a source directory.";
  183. this->SetError(e.c_str());
  184. cmSystemTools::SetFatalErrorOccured();
  185. return false;
  186. }
  187. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  188. cmSystemTools::MakeDirectory(dir.c_str());
  189. mode_t mode = 0;
  190. // Set permissions to writable
  191. if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
  192. {
  193. cmSystemTools::SetPermissions(fileName.c_str(),
  194. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  195. mode | S_IWRITE
  196. #elif defined( __BORLANDC__ )
  197. mode | S_IWUSR
  198. #else
  199. mode | S_IWUSR | S_IWGRP
  200. #endif
  201. );
  202. }
  203. // If GetPermissions fails, pretend like it is ok. File open will fail if
  204. // the file is not writable
  205. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  206. if ( !file )
  207. {
  208. std::string error = "Internal CMake error when trying to open file: ";
  209. error += fileName.c_str();
  210. error += " for writing.";
  211. this->SetError(error.c_str());
  212. return false;
  213. }
  214. file << message;
  215. file.close();
  216. if(mode)
  217. {
  218. cmSystemTools::SetPermissions(fileName.c_str(), mode);
  219. }
  220. return true;
  221. }
  222. //----------------------------------------------------------------------------
  223. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  224. {
  225. if ( args.size() < 3 )
  226. {
  227. this->SetError("READ must be called with at least two additional "
  228. "arguments");
  229. return false;
  230. }
  231. cmCommandArgumentsHelper argHelper;
  232. cmCommandArgumentGroup group;
  233. cmCAString readArg (&argHelper, "READ");
  234. cmCAString fileNameArg (&argHelper, 0);
  235. cmCAString resultArg (&argHelper, 0);
  236. cmCAString offsetArg (&argHelper, "OFFSET", &group);
  237. cmCAString limitArg (&argHelper, "LIMIT", &group);
  238. cmCAEnabler hexOutputArg (&argHelper, "HEX", &group);
  239. readArg.Follows(0);
  240. fileNameArg.Follows(&readArg);
  241. resultArg.Follows(&fileNameArg);
  242. group.Follows(&resultArg);
  243. argHelper.Parse(&args, 0);
  244. std::string fileName = fileNameArg.GetString();
  245. if ( !cmsys::SystemTools::FileIsFullPath(fileName.c_str()) )
  246. {
  247. fileName = this->Makefile->GetCurrentDirectory();
  248. fileName += "/" + fileNameArg.GetString();
  249. }
  250. std::string variable = resultArg.GetString();
  251. // Open the specified file.
  252. #if defined(_WIN32) || defined(__CYGWIN__)
  253. std::ifstream file(fileName.c_str(), std::ios::in |
  254. (hexOutputArg.IsEnabled() ? std::ios::binary : std::ios::in));
  255. #else
  256. std::ifstream file(fileName.c_str(), std::ios::in);
  257. #endif
  258. if ( !file )
  259. {
  260. std::string error = "Internal CMake error when trying to open file: ";
  261. error += fileName.c_str();
  262. error += " for reading.";
  263. this->SetError(error.c_str());
  264. return false;
  265. }
  266. // is there a limit?
  267. long sizeLimit = -1;
  268. if (limitArg.GetString().size() > 0)
  269. {
  270. sizeLimit = atoi(limitArg.GetCString());
  271. }
  272. // is there an offset?
  273. long offset = 0;
  274. if (offsetArg.GetString().size() > 0)
  275. {
  276. offset = atoi(offsetArg.GetCString());
  277. }
  278. file.seekg(offset, std::ios::beg); // explicit ios::beg for IBM VisualAge 6
  279. std::string output;
  280. if (hexOutputArg.IsEnabled())
  281. {
  282. // Convert part of the file into hex code
  283. char c;
  284. while((sizeLimit != 0) && (file.get(c)))
  285. {
  286. char hex[4];
  287. sprintf(hex, "%.2x", c&0xff);
  288. output += hex;
  289. if (sizeLimit > 0)
  290. {
  291. sizeLimit--;
  292. }
  293. }
  294. }
  295. else
  296. {
  297. std::string line;
  298. bool has_newline = false;
  299. while (sizeLimit != 0 &&
  300. cmSystemTools::GetLineFromStream(file, line, &has_newline,
  301. sizeLimit) )
  302. {
  303. if (sizeLimit > 0)
  304. {
  305. sizeLimit = sizeLimit - static_cast<long>(line.size());
  306. if (has_newline)
  307. {
  308. sizeLimit--;
  309. }
  310. if (sizeLimit < 0)
  311. {
  312. sizeLimit = 0;
  313. }
  314. }
  315. output += line;
  316. if ( has_newline )
  317. {
  318. output += "\n";
  319. }
  320. }
  321. }
  322. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  323. return true;
  324. }
  325. //----------------------------------------------------------------------------
  326. bool cmFileCommand::HandleHashCommand(std::vector<std::string> const& args)
  327. {
  328. #if defined(CMAKE_BUILD_WITH_CMAKE)
  329. if(args.size() != 3)
  330. {
  331. cmOStringStream e;
  332. e << args[0] << " requires a file name and output variable";
  333. this->SetError(e.str().c_str());
  334. return false;
  335. }
  336. cmsys::auto_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0].c_str()));
  337. if(hash.get())
  338. {
  339. std::string out = hash->HashFile(args[1].c_str());
  340. if(!out.empty())
  341. {
  342. this->Makefile->AddDefinition(args[2].c_str(), out.c_str());
  343. return true;
  344. }
  345. cmOStringStream e;
  346. e << args[0] << " failed to read file \"" << args[1] << "\": "
  347. << cmSystemTools::GetLastSystemError();
  348. this->SetError(e.str().c_str());
  349. }
  350. return false;
  351. #else
  352. cmOStringStream e;
  353. e << args[0] << " not available during bootstrap";
  354. this->SetError(e.str().c_str());
  355. return false;
  356. #endif
  357. }
  358. //----------------------------------------------------------------------------
  359. bool cmFileCommand::HandleStringsCommand(std::vector<std::string> const& args)
  360. {
  361. if(args.size() < 3)
  362. {
  363. this->SetError("STRINGS requires a file name and output variable");
  364. return false;
  365. }
  366. // Get the file to read.
  367. std::string fileName = args[1];
  368. if(!cmsys::SystemTools::FileIsFullPath(fileName.c_str()))
  369. {
  370. fileName = this->Makefile->GetCurrentDirectory();
  371. fileName += "/" + args[1];
  372. }
  373. // Get the variable in which to store the results.
  374. std::string outVar = args[2];
  375. // Parse the options.
  376. enum { arg_none,
  377. arg_limit_input,
  378. arg_limit_output,
  379. arg_limit_count,
  380. arg_length_minimum,
  381. arg_length_maximum,
  382. arg__maximum,
  383. arg_regex };
  384. unsigned int minlen = 0;
  385. unsigned int maxlen = 0;
  386. int limit_input = -1;
  387. int limit_output = -1;
  388. unsigned int limit_count = 0;
  389. cmsys::RegularExpression regex;
  390. bool have_regex = false;
  391. bool newline_consume = false;
  392. bool hex_conversion_enabled = true;
  393. int arg_mode = arg_none;
  394. for(unsigned int i=3; i < args.size(); ++i)
  395. {
  396. if(args[i] == "LIMIT_INPUT")
  397. {
  398. arg_mode = arg_limit_input;
  399. }
  400. else if(args[i] == "LIMIT_OUTPUT")
  401. {
  402. arg_mode = arg_limit_output;
  403. }
  404. else if(args[i] == "LIMIT_COUNT")
  405. {
  406. arg_mode = arg_limit_count;
  407. }
  408. else if(args[i] == "LENGTH_MINIMUM")
  409. {
  410. arg_mode = arg_length_minimum;
  411. }
  412. else if(args[i] == "LENGTH_MAXIMUM")
  413. {
  414. arg_mode = arg_length_maximum;
  415. }
  416. else if(args[i] == "REGEX")
  417. {
  418. arg_mode = arg_regex;
  419. }
  420. else if(args[i] == "NEWLINE_CONSUME")
  421. {
  422. newline_consume = true;
  423. arg_mode = arg_none;
  424. }
  425. else if(args[i] == "NO_HEX_CONVERSION")
  426. {
  427. hex_conversion_enabled = false;
  428. arg_mode = arg_none;
  429. }
  430. else if(arg_mode == arg_limit_input)
  431. {
  432. if(sscanf(args[i].c_str(), "%d", &limit_input) != 1 ||
  433. limit_input < 0)
  434. {
  435. cmOStringStream e;
  436. e << "STRINGS option LIMIT_INPUT value \""
  437. << args[i] << "\" is not an unsigned integer.";
  438. this->SetError(e.str().c_str());
  439. return false;
  440. }
  441. arg_mode = arg_none;
  442. }
  443. else if(arg_mode == arg_limit_output)
  444. {
  445. if(sscanf(args[i].c_str(), "%d", &limit_output) != 1 ||
  446. limit_output < 0)
  447. {
  448. cmOStringStream e;
  449. e << "STRINGS option LIMIT_OUTPUT value \""
  450. << args[i] << "\" is not an unsigned integer.";
  451. this->SetError(e.str().c_str());
  452. return false;
  453. }
  454. arg_mode = arg_none;
  455. }
  456. else if(arg_mode == arg_limit_count)
  457. {
  458. int count;
  459. if(sscanf(args[i].c_str(), "%d", &count) != 1 || count < 0)
  460. {
  461. cmOStringStream e;
  462. e << "STRINGS option LIMIT_COUNT value \""
  463. << args[i] << "\" is not an unsigned integer.";
  464. this->SetError(e.str().c_str());
  465. return false;
  466. }
  467. limit_count = count;
  468. arg_mode = arg_none;
  469. }
  470. else if(arg_mode == arg_length_minimum)
  471. {
  472. int len;
  473. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  474. {
  475. cmOStringStream e;
  476. e << "STRINGS option LENGTH_MINIMUM value \""
  477. << args[i] << "\" is not an unsigned integer.";
  478. this->SetError(e.str().c_str());
  479. return false;
  480. }
  481. minlen = len;
  482. arg_mode = arg_none;
  483. }
  484. else if(arg_mode == arg_length_maximum)
  485. {
  486. int len;
  487. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  488. {
  489. cmOStringStream e;
  490. e << "STRINGS option LENGTH_MAXIMUM value \""
  491. << args[i] << "\" is not an unsigned integer.";
  492. this->SetError(e.str().c_str());
  493. return false;
  494. }
  495. maxlen = len;
  496. arg_mode = arg_none;
  497. }
  498. else if(arg_mode == arg_regex)
  499. {
  500. if(!regex.compile(args[i].c_str()))
  501. {
  502. cmOStringStream e;
  503. e << "STRINGS option REGEX value \""
  504. << args[i] << "\" could not be compiled.";
  505. this->SetError(e.str().c_str());
  506. return false;
  507. }
  508. have_regex = true;
  509. arg_mode = arg_none;
  510. }
  511. else
  512. {
  513. cmOStringStream e;
  514. e << "STRINGS given unknown argument \""
  515. << args[i] << "\"";
  516. this->SetError(e.str().c_str());
  517. return false;
  518. }
  519. }
  520. if (hex_conversion_enabled)
  521. {
  522. // TODO: should work without temp file, but just on a memory buffer
  523. std::string binaryFileName = this->Makefile->GetCurrentOutputDirectory();
  524. binaryFileName += cmake::GetCMakeFilesDirectory();
  525. binaryFileName += "/FileCommandStringsBinaryFile";
  526. if(cmHexFileConverter::TryConvert(fileName.c_str(),binaryFileName.c_str()))
  527. {
  528. fileName = binaryFileName;
  529. }
  530. }
  531. // Open the specified file.
  532. #if defined(_WIN32) || defined(__CYGWIN__)
  533. std::ifstream fin(fileName.c_str(), std::ios::in | std::ios::binary);
  534. #else
  535. std::ifstream fin(fileName.c_str(), std::ios::in);
  536. #endif
  537. if(!fin)
  538. {
  539. cmOStringStream e;
  540. e << "STRINGS file \"" << fileName << "\" cannot be read.";
  541. this->SetError(e.str().c_str());
  542. return false;
  543. }
  544. // Parse strings out of the file.
  545. int output_size = 0;
  546. std::vector<std::string> strings;
  547. std::string s;
  548. int c;
  549. while((!limit_count || strings.size() < limit_count) &&
  550. (limit_input < 0 || static_cast<int>(fin.tellg()) < limit_input) &&
  551. (c = fin.get(), fin))
  552. {
  553. if(c == '\n' && !newline_consume)
  554. {
  555. // The current line has been terminated. Check if the current
  556. // string matches the requirements. The length may now be as
  557. // low as zero since blank lines are allowed.
  558. if(s.length() >= minlen &&
  559. (!have_regex || regex.find(s.c_str())))
  560. {
  561. output_size += static_cast<int>(s.size()) + 1;
  562. if(limit_output >= 0 && output_size >= limit_output)
  563. {
  564. s = "";
  565. break;
  566. }
  567. strings.push_back(s);
  568. }
  569. // Reset the string to empty.
  570. s = "";
  571. }
  572. else if(c == '\r')
  573. {
  574. // Ignore CR character to make output always have UNIX newlines.
  575. }
  576. else if((c >= 0x20 && c < 0x7F) || c == '\t' ||
  577. (c == '\n' && newline_consume))
  578. {
  579. // This is an ASCII character that may be part of a string.
  580. // Cast added to avoid compiler warning. Cast is ok because
  581. // c is guaranteed to fit in char by the above if...
  582. s += static_cast<char>(c);
  583. }
  584. else
  585. {
  586. // TODO: Support ENCODING option. See issue #10519.
  587. // A non-string character has been found. Check if the current
  588. // string matches the requirements. We require that the length
  589. // be at least one no matter what the user specified.
  590. if(s.length() >= minlen && s.length() >= 1 &&
  591. (!have_regex || regex.find(s.c_str())))
  592. {
  593. output_size += static_cast<int>(s.size()) + 1;
  594. if(limit_output >= 0 && output_size >= limit_output)
  595. {
  596. s = "";
  597. break;
  598. }
  599. strings.push_back(s);
  600. }
  601. // Reset the string to empty.
  602. s = "";
  603. }
  604. // Terminate a string if the maximum length is reached.
  605. if(maxlen > 0 && s.size() == maxlen)
  606. {
  607. if(s.length() >= minlen &&
  608. (!have_regex || regex.find(s.c_str())))
  609. {
  610. output_size += static_cast<int>(s.size()) + 1;
  611. if(limit_output >= 0 && output_size >= limit_output)
  612. {
  613. s = "";
  614. break;
  615. }
  616. strings.push_back(s);
  617. }
  618. s = "";
  619. }
  620. }
  621. // If there is a non-empty current string we have hit the end of the
  622. // input file or the input size limit. Check if the current string
  623. // matches the requirements.
  624. if((!limit_count || strings.size() < limit_count) &&
  625. !s.empty() && s.length() >= minlen &&
  626. (!have_regex || regex.find(s.c_str())))
  627. {
  628. output_size += static_cast<int>(s.size()) + 1;
  629. if(limit_output < 0 || output_size < limit_output)
  630. {
  631. strings.push_back(s);
  632. }
  633. }
  634. // Encode the result in a CMake list.
  635. const char* sep = "";
  636. std::string output;
  637. for(std::vector<std::string>::const_iterator si = strings.begin();
  638. si != strings.end(); ++si)
  639. {
  640. // Separate the strings in the output to make it a list.
  641. output += sep;
  642. sep = ";";
  643. // Store the string in the output, but escape semicolons to
  644. // make sure it is a list.
  645. std::string const& sr = *si;
  646. for(unsigned int i=0; i < sr.size(); ++i)
  647. {
  648. if(sr[i] == ';')
  649. {
  650. output += '\\';
  651. }
  652. output += sr[i];
  653. }
  654. }
  655. // Save the output in a makefile variable.
  656. this->Makefile->AddDefinition(outVar.c_str(), output.c_str());
  657. return true;
  658. }
  659. //----------------------------------------------------------------------------
  660. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  661. bool recurse)
  662. {
  663. if ( args.size() < 2 )
  664. {
  665. this->SetError("GLOB requires at least a variable name");
  666. return false;
  667. }
  668. std::vector<std::string>::const_iterator i = args.begin();
  669. i++; // Get rid of subcommand
  670. std::string variable = *i;
  671. i++;
  672. cmsys::Glob g;
  673. g.SetRecurse(recurse);
  674. bool explicitFollowSymlinks = false;
  675. cmPolicies::PolicyStatus status =
  676. this->Makefile->GetPolicyStatus(cmPolicies::CMP0009);
  677. if(recurse)
  678. {
  679. switch(status)
  680. {
  681. case cmPolicies::NEW:
  682. g.RecurseThroughSymlinksOff();
  683. break;
  684. case cmPolicies::OLD:
  685. case cmPolicies::WARN:
  686. case cmPolicies::REQUIRED_IF_USED:
  687. case cmPolicies::REQUIRED_ALWAYS:
  688. g.RecurseThroughSymlinksOn();
  689. break;
  690. }
  691. }
  692. std::string output = "";
  693. bool first = true;
  694. for ( ; i != args.end(); ++i )
  695. {
  696. if ( recurse && (*i == "FOLLOW_SYMLINKS") )
  697. {
  698. explicitFollowSymlinks = true;
  699. g.RecurseThroughSymlinksOn();
  700. ++i;
  701. if ( i == args.end() )
  702. {
  703. this->SetError(
  704. "GLOB_RECURSE requires a glob expression after FOLLOW_SYMLINKS");
  705. return false;
  706. }
  707. }
  708. if ( *i == "RELATIVE" )
  709. {
  710. ++i; // skip RELATIVE
  711. if ( i == args.end() )
  712. {
  713. this->SetError("GLOB requires a directory after the RELATIVE tag");
  714. return false;
  715. }
  716. g.SetRelative(i->c_str());
  717. ++i;
  718. if(i == args.end())
  719. {
  720. this->SetError("GLOB requires a glob expression after the directory");
  721. return false;
  722. }
  723. }
  724. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  725. {
  726. std::string expr = this->Makefile->GetCurrentDirectory();
  727. // Handle script mode
  728. if ( expr.size() > 0 )
  729. {
  730. expr += "/" + *i;
  731. g.FindFiles(expr);
  732. }
  733. else
  734. {
  735. g.FindFiles(*i);
  736. }
  737. }
  738. else
  739. {
  740. g.FindFiles(*i);
  741. }
  742. std::vector<std::string>::size_type cc;
  743. std::vector<std::string>& files = g.GetFiles();
  744. for ( cc = 0; cc < files.size(); cc ++ )
  745. {
  746. if ( !first )
  747. {
  748. output += ";";
  749. }
  750. output += files[cc];
  751. first = false;
  752. }
  753. }
  754. if(recurse && !explicitFollowSymlinks)
  755. {
  756. switch (status)
  757. {
  758. case cmPolicies::NEW:
  759. // Correct behavior, yay!
  760. break;
  761. case cmPolicies::OLD:
  762. // Probably not really the expected behavior, but the author explicitly
  763. // asked for the old behavior... no warning.
  764. case cmPolicies::WARN:
  765. // Possibly unexpected old behavior *and* we actually traversed
  766. // symlinks without being explicitly asked to: warn the author.
  767. if(g.GetFollowedSymlinkCount() != 0)
  768. {
  769. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING,
  770. this->Makefile->GetPolicies()->
  771. GetPolicyWarning(cmPolicies::CMP0009));
  772. }
  773. break;
  774. case cmPolicies::REQUIRED_IF_USED:
  775. case cmPolicies::REQUIRED_ALWAYS:
  776. this->SetError("policy CMP0009 error");
  777. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  778. this->Makefile->GetPolicies()->
  779. GetRequiredPolicyError(cmPolicies::CMP0009));
  780. return false;
  781. }
  782. }
  783. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  784. return true;
  785. }
  786. //----------------------------------------------------------------------------
  787. bool cmFileCommand::HandleMakeDirectoryCommand(
  788. std::vector<std::string> const& args)
  789. {
  790. if(args.size() < 2 )
  791. {
  792. this->SetError("called with incorrect number of arguments");
  793. return false;
  794. }
  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 uknown 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 inapropriate 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()) && recurse)
  2262. {
  2263. cmSystemTools::RemoveADirectory(fileName.c_str());
  2264. }
  2265. else
  2266. {
  2267. cmSystemTools::RemoveFile(fileName.c_str());
  2268. }
  2269. }
  2270. return true;
  2271. }
  2272. //----------------------------------------------------------------------------
  2273. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  2274. const& args,
  2275. bool nativePath)
  2276. {
  2277. std::vector<std::string>::const_iterator i = args.begin();
  2278. if(args.size() != 3)
  2279. {
  2280. this->SetError("FILE([TO_CMAKE_PATH|TO_NATIVE_PATH] path result) must be "
  2281. "called with exactly three arguments.");
  2282. return false;
  2283. }
  2284. i++; // Get rid of subcommand
  2285. #if defined(_WIN32) && !defined(__CYGWIN__)
  2286. char pathSep = ';';
  2287. #else
  2288. char pathSep = ':';
  2289. #endif
  2290. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  2291. pathSep);
  2292. i++;
  2293. const char* var = i->c_str();
  2294. std::string value;
  2295. for(std::vector<cmsys::String>::iterator j = path.begin();
  2296. j != path.end(); ++j)
  2297. {
  2298. if(j != path.begin())
  2299. {
  2300. value += ";";
  2301. }
  2302. if(!nativePath)
  2303. {
  2304. cmSystemTools::ConvertToUnixSlashes(*j);
  2305. }
  2306. else
  2307. {
  2308. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  2309. // remove double quotes in the path
  2310. cmsys::String& s = *j;
  2311. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  2312. {
  2313. s = s.substr(1,s.size()-2);
  2314. }
  2315. }
  2316. value += *j;
  2317. }
  2318. this->Makefile->AddDefinition(var, value.c_str());
  2319. return true;
  2320. }
  2321. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2322. // Stuff for curl download/upload
  2323. typedef std::vector<char> cmFileCommandVectorOfChar;
  2324. namespace {
  2325. size_t
  2326. cmWriteToFileCallback(void *ptr, size_t size, size_t nmemb,
  2327. void *data)
  2328. {
  2329. register int realsize = (int)(size * nmemb);
  2330. std::ofstream* fout = static_cast<std::ofstream*>(data);
  2331. const char* chPtr = static_cast<char*>(ptr);
  2332. fout->write(chPtr, realsize);
  2333. return realsize;
  2334. }
  2335. size_t
  2336. cmWriteToMemoryCallback(void *ptr, size_t size, size_t nmemb,
  2337. void *data)
  2338. {
  2339. register int realsize = (int)(size * nmemb);
  2340. cmFileCommandVectorOfChar *vec
  2341. = static_cast<cmFileCommandVectorOfChar*>(data);
  2342. const char* chPtr = static_cast<char*>(ptr);
  2343. vec->insert(vec->end(), chPtr, chPtr + realsize);
  2344. return realsize;
  2345. }
  2346. static size_t
  2347. cmFileCommandCurlDebugCallback(CURL *, curl_infotype, char *chPtr,
  2348. size_t size, void *data)
  2349. {
  2350. cmFileCommandVectorOfChar *vec
  2351. = static_cast<cmFileCommandVectorOfChar*>(data);
  2352. vec->insert(vec->end(), chPtr, chPtr + size);
  2353. return size;
  2354. }
  2355. class cURLProgressHelper
  2356. {
  2357. public:
  2358. cURLProgressHelper(cmFileCommand *fc, const char *text)
  2359. {
  2360. this->CurrentPercentage = -1;
  2361. this->FileCommand = fc;
  2362. this->Text = text;
  2363. }
  2364. bool UpdatePercentage(double value, double total, std::string &status)
  2365. {
  2366. int OldPercentage = this->CurrentPercentage;
  2367. if (total > 0.0)
  2368. {
  2369. this->CurrentPercentage = static_cast<int>(value/total*100.0 + 0.5);
  2370. }
  2371. bool updated = (OldPercentage != this->CurrentPercentage);
  2372. if (updated)
  2373. {
  2374. cmOStringStream oss;
  2375. oss << "[" << this->Text << " " << this->CurrentPercentage
  2376. << "% complete]";
  2377. status = oss.str();
  2378. }
  2379. return updated;
  2380. }
  2381. cmFileCommand *GetFileCommand()
  2382. {
  2383. return this->FileCommand;
  2384. }
  2385. private:
  2386. int CurrentPercentage;
  2387. cmFileCommand *FileCommand;
  2388. std::string Text;
  2389. };
  2390. static int
  2391. cmFileDownloadProgressCallback(void *clientp,
  2392. double dltotal, double dlnow,
  2393. double ultotal, double ulnow)
  2394. {
  2395. cURLProgressHelper *helper =
  2396. reinterpret_cast<cURLProgressHelper *>(clientp);
  2397. static_cast<void>(ultotal);
  2398. static_cast<void>(ulnow);
  2399. std::string status;
  2400. if (helper->UpdatePercentage(dlnow, dltotal, status))
  2401. {
  2402. cmFileCommand *fc = helper->GetFileCommand();
  2403. cmMakefile *mf = fc->GetMakefile();
  2404. mf->DisplayStatus(status.c_str(), -1);
  2405. }
  2406. return 0;
  2407. }
  2408. static int
  2409. cmFileUploadProgressCallback(void *clientp,
  2410. double dltotal, double dlnow,
  2411. double ultotal, double ulnow)
  2412. {
  2413. cURLProgressHelper *helper =
  2414. reinterpret_cast<cURLProgressHelper *>(clientp);
  2415. static_cast<void>(dltotal);
  2416. static_cast<void>(dlnow);
  2417. std::string status;
  2418. if (helper->UpdatePercentage(ulnow, ultotal, status))
  2419. {
  2420. cmFileCommand *fc = helper->GetFileCommand();
  2421. cmMakefile *mf = fc->GetMakefile();
  2422. mf->DisplayStatus(status.c_str(), -1);
  2423. }
  2424. return 0;
  2425. }
  2426. }
  2427. namespace {
  2428. class cURLEasyGuard
  2429. {
  2430. public:
  2431. cURLEasyGuard(CURL * easy)
  2432. : Easy(easy)
  2433. {}
  2434. ~cURLEasyGuard(void)
  2435. {
  2436. if (this->Easy)
  2437. {
  2438. ::curl_easy_cleanup(this->Easy);
  2439. }
  2440. }
  2441. inline void release(void)
  2442. {
  2443. this->Easy = 0;
  2444. return;
  2445. }
  2446. private:
  2447. ::CURL * Easy;
  2448. };
  2449. }
  2450. #endif
  2451. #define check_curl_result(result, errstr) \
  2452. if (result != CURLE_OK) \
  2453. { \
  2454. std::string e(errstr); \
  2455. e += ::curl_easy_strerror(result); \
  2456. this->SetError(e.c_str()); \
  2457. return false; \
  2458. }
  2459. bool
  2460. cmFileCommand::HandleDownloadCommand(std::vector<std::string> const& args)
  2461. {
  2462. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2463. std::vector<std::string>::const_iterator i = args.begin();
  2464. if(args.size() < 3)
  2465. {
  2466. this->SetError("DOWNLOAD must be called with at least three arguments.");
  2467. return false;
  2468. }
  2469. ++i; // Get rid of subcommand
  2470. std::string url = *i;
  2471. ++i;
  2472. std::string file = *i;
  2473. ++i;
  2474. long timeout = 0;
  2475. long inactivity_timeout = 0;
  2476. std::string verboseLog;
  2477. std::string statusVar;
  2478. bool tls_verify = this->Makefile->IsOn("CMAKE_TLS_VERIFY");
  2479. const char* cainfo = this->Makefile->GetDefinition("CMAKE_TLS_CAINFO");
  2480. std::string expectedHash;
  2481. std::string hashMatchMSG;
  2482. cmsys::auto_ptr<cmCryptoHash> hash;
  2483. bool showProgress = false;
  2484. while(i != args.end())
  2485. {
  2486. if(*i == "TIMEOUT")
  2487. {
  2488. ++i;
  2489. if(i != args.end())
  2490. {
  2491. timeout = atol(i->c_str());
  2492. }
  2493. else
  2494. {
  2495. this->SetError("DOWNLOAD missing time for TIMEOUT.");
  2496. return false;
  2497. }
  2498. }
  2499. else if(*i == "INACTIVITY_TIMEOUT")
  2500. {
  2501. ++i;
  2502. if(i != args.end())
  2503. {
  2504. inactivity_timeout = atol(i->c_str());
  2505. }
  2506. else
  2507. {
  2508. this->SetError("DOWNLOAD missing time for INACTIVITY_TIMEOUT.");
  2509. return false;
  2510. }
  2511. }
  2512. else if(*i == "LOG")
  2513. {
  2514. ++i;
  2515. if( i == args.end())
  2516. {
  2517. this->SetError("DOWNLOAD missing VAR for LOG.");
  2518. return false;
  2519. }
  2520. verboseLog = *i;
  2521. }
  2522. else if(*i == "STATUS")
  2523. {
  2524. ++i;
  2525. if( i == args.end())
  2526. {
  2527. this->SetError("DOWNLOAD missing VAR for STATUS.");
  2528. return false;
  2529. }
  2530. statusVar = *i;
  2531. }
  2532. else if(*i == "TLS_VERIFY")
  2533. {
  2534. ++i;
  2535. if(i != args.end())
  2536. {
  2537. tls_verify = cmSystemTools::IsOn(i->c_str());
  2538. }
  2539. else
  2540. {
  2541. this->SetError("TLS_VERIFY missing bool value.");
  2542. return false;
  2543. }
  2544. }
  2545. else if(*i == "TLS_CAINFO")
  2546. {
  2547. ++i;
  2548. if(i != args.end())
  2549. {
  2550. cainfo = i->c_str();
  2551. }
  2552. else
  2553. {
  2554. this->SetError("TLS_CAFILE missing file value.");
  2555. return false;
  2556. }
  2557. }
  2558. else if(*i == "EXPECTED_MD5")
  2559. {
  2560. ++i;
  2561. if( i == args.end())
  2562. {
  2563. this->SetError("DOWNLOAD missing sum value for EXPECTED_MD5.");
  2564. return false;
  2565. }
  2566. hash = cmsys::auto_ptr<cmCryptoHash>(cmCryptoHash::New("MD5"));
  2567. hashMatchMSG = "MD5 sum";
  2568. expectedHash = cmSystemTools::LowerCase(*i);
  2569. }
  2570. else if(*i == "SHOW_PROGRESS")
  2571. {
  2572. showProgress = true;
  2573. }
  2574. else if(*i == "EXPECTED_HASH")
  2575. {
  2576. ++i;
  2577. if(i != args.end())
  2578. {
  2579. hash = cmsys::auto_ptr<cmCryptoHash>(cmCryptoHash::New(i->c_str()));
  2580. if(!hash.get())
  2581. {
  2582. std::string err = "DOWNLOAD bad SHA type: ";
  2583. err += *i;
  2584. this->SetError(err.c_str());
  2585. return false;
  2586. }
  2587. hashMatchMSG = *i;
  2588. hashMatchMSG += " hash";
  2589. ++i;
  2590. }
  2591. if(i != args.end())
  2592. {
  2593. expectedHash = cmSystemTools::LowerCase(*i);
  2594. }
  2595. else
  2596. {
  2597. this->SetError("DOWNLOAD missing time for EXPECTED_HASH.");
  2598. return false;
  2599. }
  2600. }
  2601. ++i;
  2602. }
  2603. // If file exists already, and caller specified an expected md5 or sha,
  2604. // and the existing file already has the expected hash, then simply
  2605. // return.
  2606. //
  2607. if(cmSystemTools::FileExists(file.c_str()) && hash.get())
  2608. {
  2609. std::string msg;
  2610. std::string actualHash = hash->HashFile(file.c_str());
  2611. if(actualHash == expectedHash)
  2612. {
  2613. msg = "returning early; file already exists with expected ";
  2614. msg += hashMatchMSG;
  2615. msg += "\"";
  2616. if(statusVar.size())
  2617. {
  2618. cmOStringStream result;
  2619. result << (int)0 << ";\"" << msg;
  2620. this->Makefile->AddDefinition(statusVar.c_str(),
  2621. result.str().c_str());
  2622. }
  2623. return true;
  2624. }
  2625. }
  2626. // Make sure parent directory exists so we can write to the file
  2627. // as we receive downloaded bits from curl...
  2628. //
  2629. std::string dir = cmSystemTools::GetFilenamePath(file.c_str());
  2630. if(!cmSystemTools::FileExists(dir.c_str()) &&
  2631. !cmSystemTools::MakeDirectory(dir.c_str()))
  2632. {
  2633. std::string errstring = "DOWNLOAD error: cannot create directory '"
  2634. + dir + "' - Specify file by full path name and verify that you "
  2635. "have directory creation and file write privileges.";
  2636. this->SetError(errstring.c_str());
  2637. return false;
  2638. }
  2639. std::ofstream fout(file.c_str(), std::ios::binary);
  2640. if(!fout)
  2641. {
  2642. this->SetError("DOWNLOAD cannot open file for write.");
  2643. return false;
  2644. }
  2645. ::CURL *curl;
  2646. ::curl_global_init(CURL_GLOBAL_DEFAULT);
  2647. curl = ::curl_easy_init();
  2648. if(!curl)
  2649. {
  2650. this->SetError("DOWNLOAD error initializing curl.");
  2651. return false;
  2652. }
  2653. cURLEasyGuard g_curl(curl);
  2654. ::CURLcode res = ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  2655. check_curl_result(res, "DOWNLOAD cannot set url: ");
  2656. // enable HTTP ERROR parsing
  2657. res = ::curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  2658. check_curl_result(res, "DOWNLOAD cannot set http failure option: ");
  2659. res = ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  2660. cmWriteToFileCallback);
  2661. check_curl_result(res, "DOWNLOAD cannot set write function: ");
  2662. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  2663. cmFileCommandCurlDebugCallback);
  2664. check_curl_result(res, "DOWNLOAD cannot set debug function: ");
  2665. // check to see if TLS verification is requested
  2666. if(tls_verify)
  2667. {
  2668. res = ::curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
  2669. check_curl_result(res, "Unable to set TLS/SSL Verify on: ");
  2670. }
  2671. else
  2672. {
  2673. res = ::curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  2674. check_curl_result(res, "Unable to set TLS/SSL Verify off: ");
  2675. }
  2676. // check to see if a CAINFO file has been specified
  2677. // command arg comes first
  2678. if(cainfo && *cainfo)
  2679. {
  2680. res = ::curl_easy_setopt(curl, CURLOPT_CAINFO, cainfo);
  2681. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  2682. }
  2683. cmFileCommandVectorOfChar chunkDebug;
  2684. res = ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&fout);
  2685. check_curl_result(res, "DOWNLOAD cannot set write data: ");
  2686. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void *)&chunkDebug);
  2687. check_curl_result(res, "DOWNLOAD cannot set debug data: ");
  2688. res = ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  2689. check_curl_result(res, "DOWNLOAD cannot set follow-redirect option: ");
  2690. if(verboseLog.size())
  2691. {
  2692. res = ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  2693. check_curl_result(res, "DOWNLOAD cannot set verbose: ");
  2694. }
  2695. if(timeout > 0)
  2696. {
  2697. res = ::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout );
  2698. check_curl_result(res, "DOWNLOAD cannot set timeout: ");
  2699. }
  2700. if(inactivity_timeout > 0)
  2701. {
  2702. // Give up if there is no progress for a long time.
  2703. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  2704. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, inactivity_timeout);
  2705. }
  2706. // Need the progress helper's scope to last through the duration of
  2707. // the curl_easy_perform call... so this object is declared at function
  2708. // scope intentionally, rather than inside the "if(showProgress)"
  2709. // block...
  2710. //
  2711. cURLProgressHelper helper(this, "download");
  2712. if(showProgress)
  2713. {
  2714. res = ::curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
  2715. check_curl_result(res, "DOWNLOAD cannot set noprogress value: ");
  2716. res = ::curl_easy_setopt(curl,
  2717. CURLOPT_PROGRESSFUNCTION, cmFileDownloadProgressCallback);
  2718. check_curl_result(res, "DOWNLOAD cannot set progress function: ");
  2719. res = ::curl_easy_setopt(curl,
  2720. CURLOPT_PROGRESSDATA, reinterpret_cast<void*>(&helper));
  2721. check_curl_result(res, "DOWNLOAD cannot set progress data: ");
  2722. }
  2723. res = ::curl_easy_perform(curl);
  2724. /* always cleanup */
  2725. g_curl.release();
  2726. ::curl_easy_cleanup(curl);
  2727. if(statusVar.size())
  2728. {
  2729. cmOStringStream result;
  2730. result << (int)res << ";\"" << ::curl_easy_strerror(res) << "\"";
  2731. this->Makefile->AddDefinition(statusVar.c_str(),
  2732. result.str().c_str());
  2733. }
  2734. ::curl_global_cleanup();
  2735. // Explicitly flush/close so we can measure the md5 accurately.
  2736. //
  2737. fout.flush();
  2738. fout.close();
  2739. // Verify MD5 sum if requested:
  2740. //
  2741. if (hash.get())
  2742. {
  2743. std::string actualHash = hash->HashFile(file.c_str());
  2744. if (actualHash.size() == 0)
  2745. {
  2746. this->SetError("DOWNLOAD cannot compute hash on downloaded file");
  2747. return false;
  2748. }
  2749. if (expectedHash != actualHash)
  2750. {
  2751. cmOStringStream oss;
  2752. oss << "DOWNLOAD HASH mismatch" << std::endl
  2753. << " for file: [" << file << "]" << std::endl
  2754. << " expected hash: [" << expectedHash << "]" << std::endl
  2755. << " actual hash: [" << actualHash << "]" << std::endl
  2756. ;
  2757. this->SetError(oss.str().c_str());
  2758. return false;
  2759. }
  2760. }
  2761. if(chunkDebug.size())
  2762. {
  2763. chunkDebug.push_back(0);
  2764. if(CURLE_OPERATION_TIMEOUTED == res)
  2765. {
  2766. std::string output = &*chunkDebug.begin();
  2767. if(verboseLog.size())
  2768. {
  2769. this->Makefile->AddDefinition(verboseLog.c_str(),
  2770. &*chunkDebug.begin());
  2771. }
  2772. }
  2773. this->Makefile->AddDefinition(verboseLog.c_str(),
  2774. &*chunkDebug.begin());
  2775. }
  2776. return true;
  2777. #else
  2778. this->SetError("DOWNLOAD not supported by bootstrap cmake.");
  2779. return false;
  2780. #endif
  2781. }
  2782. bool
  2783. cmFileCommand::HandleUploadCommand(std::vector<std::string> const& args)
  2784. {
  2785. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2786. if(args.size() < 3)
  2787. {
  2788. this->SetError("UPLOAD must be called with at least three arguments.");
  2789. return false;
  2790. }
  2791. std::vector<std::string>::const_iterator i = args.begin();
  2792. ++i;
  2793. std::string filename = *i;
  2794. ++i;
  2795. std::string url = *i;
  2796. ++i;
  2797. long timeout = 0;
  2798. long inactivity_timeout = 0;
  2799. std::string logVar;
  2800. std::string statusVar;
  2801. bool showProgress = false;
  2802. while(i != args.end())
  2803. {
  2804. if(*i == "TIMEOUT")
  2805. {
  2806. ++i;
  2807. if(i != args.end())
  2808. {
  2809. timeout = atol(i->c_str());
  2810. }
  2811. else
  2812. {
  2813. this->SetError("UPLOAD missing time for TIMEOUT.");
  2814. return false;
  2815. }
  2816. }
  2817. else if(*i == "INACTIVITY_TIMEOUT")
  2818. {
  2819. ++i;
  2820. if(i != args.end())
  2821. {
  2822. inactivity_timeout = atol(i->c_str());
  2823. }
  2824. else
  2825. {
  2826. this->SetError("UPLOAD missing time for INACTIVITY_TIMEOUT.");
  2827. return false;
  2828. }
  2829. }
  2830. else if(*i == "LOG")
  2831. {
  2832. ++i;
  2833. if( i == args.end())
  2834. {
  2835. this->SetError("UPLOAD missing VAR for LOG.");
  2836. return false;
  2837. }
  2838. logVar = *i;
  2839. }
  2840. else if(*i == "STATUS")
  2841. {
  2842. ++i;
  2843. if( i == args.end())
  2844. {
  2845. this->SetError("UPLOAD missing VAR for STATUS.");
  2846. return false;
  2847. }
  2848. statusVar = *i;
  2849. }
  2850. else if(*i == "SHOW_PROGRESS")
  2851. {
  2852. showProgress = true;
  2853. }
  2854. ++i;
  2855. }
  2856. // Open file for reading:
  2857. //
  2858. FILE *fin = fopen(filename.c_str(), "rb");
  2859. if(!fin)
  2860. {
  2861. std::string errStr = "UPLOAD cannot open file '";
  2862. errStr += filename + "' for reading.";
  2863. this->SetError(errStr.c_str());
  2864. return false;
  2865. }
  2866. struct stat st;
  2867. if(::stat(filename.c_str(), &st))
  2868. {
  2869. std::string errStr = "UPLOAD cannot stat file '";
  2870. errStr += filename + "'.";
  2871. this->SetError(errStr.c_str());
  2872. fclose(fin);
  2873. return false;
  2874. }
  2875. ::CURL *curl;
  2876. ::curl_global_init(CURL_GLOBAL_DEFAULT);
  2877. curl = ::curl_easy_init();
  2878. if(!curl)
  2879. {
  2880. this->SetError("UPLOAD error initializing curl.");
  2881. fclose(fin);
  2882. return false;
  2883. }
  2884. cURLEasyGuard g_curl(curl);
  2885. // enable HTTP ERROR parsing
  2886. ::CURLcode res = ::curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  2887. // enable uploading
  2888. res = ::curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
  2889. check_curl_result(res, "UPLOAD cannot set upload flag: ");
  2890. res = ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  2891. check_curl_result(res, "UPLOAD cannot set url: ");
  2892. res = ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  2893. cmWriteToMemoryCallback);
  2894. check_curl_result(res, "UPLOAD cannot set write function: ");
  2895. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  2896. cmFileCommandCurlDebugCallback);
  2897. check_curl_result(res, "UPLOAD cannot set debug function: ");
  2898. cmFileCommandVectorOfChar chunkResponse;
  2899. cmFileCommandVectorOfChar chunkDebug;
  2900. res = ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunkResponse);
  2901. check_curl_result(res, "UPLOAD cannot set write data: ");
  2902. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void *)&chunkDebug);
  2903. check_curl_result(res, "UPLOAD cannot set debug data: ");
  2904. res = ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  2905. check_curl_result(res, "UPLOAD cannot set follow-redirect option: ");
  2906. if(logVar.size())
  2907. {
  2908. res = ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  2909. check_curl_result(res, "UPLOAD cannot set verbose: ");
  2910. }
  2911. if(timeout > 0)
  2912. {
  2913. res = ::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout );
  2914. check_curl_result(res, "UPLOAD cannot set timeout: ");
  2915. }
  2916. if(inactivity_timeout > 0)
  2917. {
  2918. // Give up if there is no progress for a long time.
  2919. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
  2920. ::curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, inactivity_timeout);
  2921. }
  2922. // Need the progress helper's scope to last through the duration of
  2923. // the curl_easy_perform call... so this object is declared at function
  2924. // scope intentionally, rather than inside the "if(showProgress)"
  2925. // block...
  2926. //
  2927. cURLProgressHelper helper(this, "upload");
  2928. if(showProgress)
  2929. {
  2930. res = ::curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
  2931. check_curl_result(res, "UPLOAD cannot set noprogress value: ");
  2932. res = ::curl_easy_setopt(curl,
  2933. CURLOPT_PROGRESSFUNCTION, cmFileUploadProgressCallback);
  2934. check_curl_result(res, "UPLOAD cannot set progress function: ");
  2935. res = ::curl_easy_setopt(curl,
  2936. CURLOPT_PROGRESSDATA, reinterpret_cast<void*>(&helper));
  2937. check_curl_result(res, "UPLOAD cannot set progress data: ");
  2938. }
  2939. // now specify which file to upload
  2940. res = ::curl_easy_setopt(curl, CURLOPT_INFILE, fin);
  2941. check_curl_result(res, "UPLOAD cannot set input file: ");
  2942. // and give the size of the upload (optional)
  2943. res = ::curl_easy_setopt(curl,
  2944. CURLOPT_INFILESIZE, static_cast<long>(st.st_size));
  2945. check_curl_result(res, "UPLOAD cannot set input file size: ");
  2946. res = ::curl_easy_perform(curl);
  2947. /* always cleanup */
  2948. g_curl.release();
  2949. ::curl_easy_cleanup(curl);
  2950. if(statusVar.size())
  2951. {
  2952. cmOStringStream result;
  2953. result << (int)res << ";\"" << ::curl_easy_strerror(res) << "\"";
  2954. this->Makefile->AddDefinition(statusVar.c_str(),
  2955. result.str().c_str());
  2956. }
  2957. ::curl_global_cleanup();
  2958. fclose(fin);
  2959. fin = NULL;
  2960. if(logVar.size())
  2961. {
  2962. std::string log;
  2963. if(chunkResponse.size())
  2964. {
  2965. chunkResponse.push_back(0);
  2966. log += "Response:\n";
  2967. log += &*chunkResponse.begin();
  2968. log += "\n";
  2969. }
  2970. if(chunkDebug.size())
  2971. {
  2972. chunkDebug.push_back(0);
  2973. log += "Debug:\n";
  2974. log += &*chunkDebug.begin();
  2975. log += "\n";
  2976. }
  2977. this->Makefile->AddDefinition(logVar.c_str(), log.c_str());
  2978. }
  2979. return true;
  2980. #else
  2981. this->SetError("UPLOAD not supported by bootstrap cmake.");
  2982. return false;
  2983. #endif
  2984. }