cmFileCommand.cxx 76 KB

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