cmFileCommand.cxx 84 KB

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