cmFileCommand.cxx 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903
  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. DoingSelf24
  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. if(this->Makefile->IsOn("CMAKE_INSTALL_SELF_2_4"))
  1727. {
  1728. // When CMake 2.4 builds this CMake version we need to support
  1729. // the install scripts it generates since it asks this CMake
  1730. // to install itself using the rules it generated.
  1731. this->Doing = DoingSelf24;
  1732. }
  1733. else
  1734. {
  1735. cmOStringStream e;
  1736. e << "INSTALL called with old-style " << arg << " argument. "
  1737. << "This script was generated with an older version of CMake. "
  1738. << "Re-run this cmake version on your build tree.";
  1739. this->FileCommand->SetError(e.str().c_str());
  1740. this->Doing = DoingError;
  1741. }
  1742. }
  1743. else
  1744. {
  1745. return this->cmFileCopier::CheckKeyword(arg);
  1746. }
  1747. return true;
  1748. }
  1749. //----------------------------------------------------------------------------
  1750. bool cmFileInstaller::CheckValue(std::string const& arg)
  1751. {
  1752. switch(this->Doing)
  1753. {
  1754. case DoingType:
  1755. if(!this->GetTargetTypeFromString(arg))
  1756. {
  1757. this->Doing = DoingError;
  1758. }
  1759. break;
  1760. case DoingRename:
  1761. this->Rename = arg;
  1762. break;
  1763. case DoingSelf24:
  1764. // Ignore these arguments for compatibility. This should be
  1765. // reached only when CMake 2.4 is installing the current
  1766. // CMake. It can be removed when CMake 2.6 or higher is
  1767. // required to build CMake.
  1768. break;
  1769. default:
  1770. return this->cmFileCopier::CheckValue(arg);
  1771. }
  1772. return true;
  1773. }
  1774. //----------------------------------------------------------------------------
  1775. bool cmFileInstaller
  1776. ::GetTargetTypeFromString(const std::string& stype)
  1777. {
  1778. if ( stype == "EXECUTABLE" )
  1779. {
  1780. this->InstallType = cmTarget::EXECUTABLE;
  1781. }
  1782. else if ( stype == "FILE" )
  1783. {
  1784. this->InstallType = cmTarget::INSTALL_FILES;
  1785. }
  1786. else if ( stype == "PROGRAM" )
  1787. {
  1788. this->InstallType = cmTarget::INSTALL_PROGRAMS;
  1789. }
  1790. else if ( stype == "STATIC_LIBRARY" )
  1791. {
  1792. this->InstallType = cmTarget::STATIC_LIBRARY;
  1793. }
  1794. else if ( stype == "SHARED_LIBRARY" )
  1795. {
  1796. this->InstallType = cmTarget::SHARED_LIBRARY;
  1797. }
  1798. else if ( stype == "MODULE" )
  1799. {
  1800. this->InstallType = cmTarget::MODULE_LIBRARY;
  1801. }
  1802. else if ( stype == "DIRECTORY" )
  1803. {
  1804. this->InstallType = cmTarget::INSTALL_DIRECTORY;
  1805. }
  1806. else
  1807. {
  1808. cmOStringStream e;
  1809. e << "Option TYPE given uknown value \"" << stype << "\".";
  1810. this->FileCommand->SetError(e.str().c_str());
  1811. return false;
  1812. }
  1813. return true;
  1814. }
  1815. //----------------------------------------------------------------------------
  1816. bool cmFileInstaller::HandleInstallDestination()
  1817. {
  1818. std::string& destination = this->Destination;
  1819. // allow for / to be a valid destination
  1820. if ( destination.size() < 2 && destination != "/" )
  1821. {
  1822. this->FileCommand->SetError("called with inapropriate arguments. "
  1823. "No DESTINATION provided or .");
  1824. return false;
  1825. }
  1826. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  1827. if ( destdir && *destdir )
  1828. {
  1829. std::string sdestdir = destdir;
  1830. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  1831. char ch1 = destination[0];
  1832. char ch2 = destination[1];
  1833. char ch3 = 0;
  1834. if ( destination.size() > 2 )
  1835. {
  1836. ch3 = destination[2];
  1837. }
  1838. int skip = 0;
  1839. if ( ch1 != '/' )
  1840. {
  1841. int relative = 0;
  1842. if (((ch1 >= 'a' && ch1 <= 'z') || (ch1 >= 'A' && ch1 <= 'Z')) &&
  1843. ch2 == ':' )
  1844. {
  1845. // Assume windows
  1846. // let's do some destdir magic:
  1847. skip = 2;
  1848. if ( ch3 != '/' )
  1849. {
  1850. relative = 1;
  1851. }
  1852. }
  1853. else
  1854. {
  1855. relative = 1;
  1856. }
  1857. if ( relative )
  1858. {
  1859. // This is relative path on unix or windows. Since we are doing
  1860. // destdir, this case does not make sense.
  1861. this->FileCommand->SetError(
  1862. "called with relative DESTINATION. This "
  1863. "does not make sense when using DESTDIR. Specify "
  1864. "absolute path or remove DESTDIR environment variable.");
  1865. return false;
  1866. }
  1867. }
  1868. else
  1869. {
  1870. if ( ch2 == '/' )
  1871. {
  1872. // looks like a network path.
  1873. std::string message = "called with network path DESTINATION. This "
  1874. "does not make sense when using DESTDIR. Specify local "
  1875. "absolute path or remove DESTDIR environment variable."
  1876. "\nDESTINATION=\n";
  1877. message += destination;
  1878. this->FileCommand->SetError(message.c_str());
  1879. return false;
  1880. }
  1881. }
  1882. destination = sdestdir + (destination.c_str() + skip);
  1883. this->DestDirLength = int(sdestdir.size());
  1884. }
  1885. if ( !cmSystemTools::FileExists(destination.c_str()) )
  1886. {
  1887. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  1888. {
  1889. std::string errstring = "cannot create directory: " + destination +
  1890. ". Maybe need administrative privileges.";
  1891. this->FileCommand->SetError(errstring.c_str());
  1892. return false;
  1893. }
  1894. }
  1895. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  1896. {
  1897. std::string errstring = "INSTALL destination: " + destination +
  1898. " is not a directory.";
  1899. this->FileCommand->SetError(errstring.c_str());
  1900. return false;
  1901. }
  1902. return true;
  1903. }
  1904. //----------------------------------------------------------------------------
  1905. bool
  1906. cmFileCommand::HandleRPathChangeCommand(std::vector<std::string> const& args)
  1907. {
  1908. // Evaluate arguments.
  1909. const char* file = 0;
  1910. const char* oldRPath = 0;
  1911. const char* newRPath = 0;
  1912. enum Doing { DoingNone, DoingFile, DoingOld, DoingNew };
  1913. Doing doing = DoingNone;
  1914. for(unsigned int i=1; i < args.size(); ++i)
  1915. {
  1916. if(args[i] == "OLD_RPATH")
  1917. {
  1918. doing = DoingOld;
  1919. }
  1920. else if(args[i] == "NEW_RPATH")
  1921. {
  1922. doing = DoingNew;
  1923. }
  1924. else if(args[i] == "FILE")
  1925. {
  1926. doing = DoingFile;
  1927. }
  1928. else if(doing == DoingFile)
  1929. {
  1930. file = args[i].c_str();
  1931. doing = DoingNone;
  1932. }
  1933. else if(doing == DoingOld)
  1934. {
  1935. oldRPath = args[i].c_str();
  1936. doing = DoingNone;
  1937. }
  1938. else if(doing == DoingNew)
  1939. {
  1940. newRPath = args[i].c_str();
  1941. doing = DoingNone;
  1942. }
  1943. else
  1944. {
  1945. cmOStringStream e;
  1946. e << "RPATH_CHANGE given unknown argument " << args[i];
  1947. this->SetError(e.str().c_str());
  1948. return false;
  1949. }
  1950. }
  1951. if(!file)
  1952. {
  1953. this->SetError("RPATH_CHANGE not given FILE option.");
  1954. return false;
  1955. }
  1956. if(!oldRPath)
  1957. {
  1958. this->SetError("RPATH_CHANGE not given OLD_RPATH option.");
  1959. return false;
  1960. }
  1961. if(!newRPath)
  1962. {
  1963. this->SetError("RPATH_CHANGE not given NEW_RPATH option.");
  1964. return false;
  1965. }
  1966. if(!cmSystemTools::FileExists(file, true))
  1967. {
  1968. cmOStringStream e;
  1969. e << "RPATH_CHANGE given FILE \"" << file << "\" that does not exist.";
  1970. this->SetError(e.str().c_str());
  1971. return false;
  1972. }
  1973. bool success = true;
  1974. cmSystemToolsFileTime* ft = cmSystemTools::FileTimeNew();
  1975. bool have_ft = cmSystemTools::FileTimeGet(file, ft);
  1976. std::string emsg;
  1977. bool changed;
  1978. if(!cmSystemTools::ChangeRPath(file, oldRPath, newRPath, &emsg, &changed))
  1979. {
  1980. cmOStringStream e;
  1981. e << "RPATH_CHANGE could not write new RPATH:\n"
  1982. << " " << newRPath << "\n"
  1983. << "to the file:\n"
  1984. << " " << file << "\n"
  1985. << emsg;
  1986. this->SetError(e.str().c_str());
  1987. success = false;
  1988. }
  1989. if(success)
  1990. {
  1991. if(changed)
  1992. {
  1993. std::string message = "Set runtime path of \"";
  1994. message += file;
  1995. message += "\" to \"";
  1996. message += newRPath;
  1997. message += "\"";
  1998. this->Makefile->DisplayStatus(message.c_str(), -1);
  1999. }
  2000. if(have_ft)
  2001. {
  2002. cmSystemTools::FileTimeSet(file, ft);
  2003. }
  2004. }
  2005. cmSystemTools::FileTimeDelete(ft);
  2006. return success;
  2007. }
  2008. //----------------------------------------------------------------------------
  2009. bool
  2010. cmFileCommand::HandleRPathRemoveCommand(std::vector<std::string> const& args)
  2011. {
  2012. // Evaluate arguments.
  2013. const char* file = 0;
  2014. enum Doing { DoingNone, DoingFile };
  2015. Doing doing = DoingNone;
  2016. for(unsigned int i=1; i < args.size(); ++i)
  2017. {
  2018. if(args[i] == "FILE")
  2019. {
  2020. doing = DoingFile;
  2021. }
  2022. else if(doing == DoingFile)
  2023. {
  2024. file = args[i].c_str();
  2025. doing = DoingNone;
  2026. }
  2027. else
  2028. {
  2029. cmOStringStream e;
  2030. e << "RPATH_REMOVE given unknown argument " << args[i];
  2031. this->SetError(e.str().c_str());
  2032. return false;
  2033. }
  2034. }
  2035. if(!file)
  2036. {
  2037. this->SetError("RPATH_REMOVE not given FILE option.");
  2038. return false;
  2039. }
  2040. if(!cmSystemTools::FileExists(file, true))
  2041. {
  2042. cmOStringStream e;
  2043. e << "RPATH_REMOVE given FILE \"" << file << "\" that does not exist.";
  2044. this->SetError(e.str().c_str());
  2045. return false;
  2046. }
  2047. bool success = true;
  2048. cmSystemToolsFileTime* ft = cmSystemTools::FileTimeNew();
  2049. bool have_ft = cmSystemTools::FileTimeGet(file, ft);
  2050. std::string emsg;
  2051. bool removed;
  2052. if(!cmSystemTools::RemoveRPath(file, &emsg, &removed))
  2053. {
  2054. cmOStringStream e;
  2055. e << "RPATH_REMOVE could not remove RPATH from file:\n"
  2056. << " " << file << "\n"
  2057. << emsg;
  2058. this->SetError(e.str().c_str());
  2059. success = false;
  2060. }
  2061. if(success)
  2062. {
  2063. if(removed)
  2064. {
  2065. std::string message = "Removed runtime path from \"";
  2066. message += file;
  2067. message += "\"";
  2068. this->Makefile->DisplayStatus(message.c_str(), -1);
  2069. }
  2070. if(have_ft)
  2071. {
  2072. cmSystemTools::FileTimeSet(file, ft);
  2073. }
  2074. }
  2075. cmSystemTools::FileTimeDelete(ft);
  2076. return success;
  2077. }
  2078. //----------------------------------------------------------------------------
  2079. bool
  2080. cmFileCommand::HandleRPathCheckCommand(std::vector<std::string> const& args)
  2081. {
  2082. // Evaluate arguments.
  2083. const char* file = 0;
  2084. const char* rpath = 0;
  2085. enum Doing { DoingNone, DoingFile, DoingRPath };
  2086. Doing doing = DoingNone;
  2087. for(unsigned int i=1; i < args.size(); ++i)
  2088. {
  2089. if(args[i] == "RPATH")
  2090. {
  2091. doing = DoingRPath;
  2092. }
  2093. else if(args[i] == "FILE")
  2094. {
  2095. doing = DoingFile;
  2096. }
  2097. else if(doing == DoingFile)
  2098. {
  2099. file = args[i].c_str();
  2100. doing = DoingNone;
  2101. }
  2102. else if(doing == DoingRPath)
  2103. {
  2104. rpath = args[i].c_str();
  2105. doing = DoingNone;
  2106. }
  2107. else
  2108. {
  2109. cmOStringStream e;
  2110. e << "RPATH_CHECK given unknown argument " << args[i];
  2111. this->SetError(e.str().c_str());
  2112. return false;
  2113. }
  2114. }
  2115. if(!file)
  2116. {
  2117. this->SetError("RPATH_CHECK not given FILE option.");
  2118. return false;
  2119. }
  2120. if(!rpath)
  2121. {
  2122. this->SetError("RPATH_CHECK not given RPATH option.");
  2123. return false;
  2124. }
  2125. // If the file exists but does not have the desired RPath then
  2126. // delete it. This is used during installation to re-install a file
  2127. // if its RPath will change.
  2128. if(cmSystemTools::FileExists(file, true) &&
  2129. !cmSystemTools::CheckRPath(file, rpath))
  2130. {
  2131. cmSystemTools::RemoveFile(file);
  2132. }
  2133. return true;
  2134. }
  2135. //----------------------------------------------------------------------------
  2136. bool cmFileCommand::HandleInstallCommand(std::vector<std::string> const& args)
  2137. {
  2138. cmFileInstaller installer(this);
  2139. return installer.Run(args);
  2140. }
  2141. //----------------------------------------------------------------------------
  2142. bool cmFileCommand::HandleRelativePathCommand(
  2143. std::vector<std::string> const& args)
  2144. {
  2145. if(args.size() != 4 )
  2146. {
  2147. this->SetError("RELATIVE_PATH called with incorrect number of arguments");
  2148. return false;
  2149. }
  2150. const std::string& outVar = args[1];
  2151. const std::string& directoryName = args[2];
  2152. const std::string& fileName = args[3];
  2153. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  2154. {
  2155. std::string errstring =
  2156. "RELATIVE_PATH must be passed a full path to the directory: "
  2157. + directoryName;
  2158. this->SetError(errstring.c_str());
  2159. return false;
  2160. }
  2161. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  2162. {
  2163. std::string errstring =
  2164. "RELATIVE_PATH must be passed a full path to the file: "
  2165. + fileName;
  2166. this->SetError(errstring.c_str());
  2167. return false;
  2168. }
  2169. std::string res = cmSystemTools::RelativePath(directoryName.c_str(),
  2170. fileName.c_str());
  2171. this->Makefile->AddDefinition(outVar.c_str(),
  2172. res.c_str());
  2173. return true;
  2174. }
  2175. //----------------------------------------------------------------------------
  2176. bool cmFileCommand::HandleRename(std::vector<std::string> const& args)
  2177. {
  2178. if(args.size() != 3)
  2179. {
  2180. this->SetError("RENAME given incorrect number of arguments.");
  2181. return false;
  2182. }
  2183. // Compute full path for old and new names.
  2184. std::string oldname = args[1];
  2185. if(!cmsys::SystemTools::FileIsFullPath(oldname.c_str()))
  2186. {
  2187. oldname = this->Makefile->GetCurrentDirectory();
  2188. oldname += "/" + args[1];
  2189. }
  2190. std::string newname = args[2];
  2191. if(!cmsys::SystemTools::FileIsFullPath(newname.c_str()))
  2192. {
  2193. newname = this->Makefile->GetCurrentDirectory();
  2194. newname += "/" + args[2];
  2195. }
  2196. if(!cmSystemTools::RenameFile(oldname.c_str(), newname.c_str()))
  2197. {
  2198. std::string err = cmSystemTools::GetLastSystemError();
  2199. cmOStringStream e;
  2200. e << "RENAME failed to rename\n"
  2201. << " " << oldname << "\n"
  2202. << "to\n"
  2203. << " " << newname << "\n"
  2204. << "because: " << err << "\n";
  2205. this->SetError(e.str().c_str());
  2206. return false;
  2207. }
  2208. return true;
  2209. }
  2210. //----------------------------------------------------------------------------
  2211. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  2212. bool recurse)
  2213. {
  2214. std::string message;
  2215. std::vector<std::string>::const_iterator i = args.begin();
  2216. i++; // Get rid of subcommand
  2217. for(;i != args.end(); ++i)
  2218. {
  2219. std::string fileName = *i;
  2220. if(!cmsys::SystemTools::FileIsFullPath(fileName.c_str()))
  2221. {
  2222. fileName = this->Makefile->GetCurrentDirectory();
  2223. fileName += "/" + *i;
  2224. }
  2225. if(cmSystemTools::FileIsDirectory(fileName.c_str()) && recurse)
  2226. {
  2227. cmSystemTools::RemoveADirectory(fileName.c_str());
  2228. }
  2229. else
  2230. {
  2231. cmSystemTools::RemoveFile(fileName.c_str());
  2232. }
  2233. }
  2234. return true;
  2235. }
  2236. //----------------------------------------------------------------------------
  2237. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  2238. const& args,
  2239. bool nativePath)
  2240. {
  2241. std::vector<std::string>::const_iterator i = args.begin();
  2242. if(args.size() != 3)
  2243. {
  2244. this->SetError("FILE([TO_CMAKE_PATH|TO_NATIVE_PATH] path result) must be "
  2245. "called with exactly three arguments.");
  2246. return false;
  2247. }
  2248. i++; // Get rid of subcommand
  2249. #if defined(_WIN32) && !defined(__CYGWIN__)
  2250. char pathSep = ';';
  2251. #else
  2252. char pathSep = ':';
  2253. #endif
  2254. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  2255. pathSep);
  2256. i++;
  2257. const char* var = i->c_str();
  2258. std::string value;
  2259. for(std::vector<cmsys::String>::iterator j = path.begin();
  2260. j != path.end(); ++j)
  2261. {
  2262. if(j != path.begin())
  2263. {
  2264. value += ";";
  2265. }
  2266. if(!nativePath)
  2267. {
  2268. cmSystemTools::ConvertToUnixSlashes(*j);
  2269. }
  2270. else
  2271. {
  2272. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  2273. // remove double quotes in the path
  2274. cmsys::String& s = *j;
  2275. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  2276. {
  2277. s = s.substr(1,s.size()-2);
  2278. }
  2279. }
  2280. value += *j;
  2281. }
  2282. this->Makefile->AddDefinition(var, value.c_str());
  2283. return true;
  2284. }
  2285. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2286. // Stuff for curl download
  2287. typedef std::vector<char> cmFileCommandVectorOfChar;
  2288. namespace{
  2289. size_t
  2290. cmFileCommandWriteMemoryCallback(void *ptr, size_t size, size_t nmemb,
  2291. void *data)
  2292. {
  2293. register int realsize = (int)(size * nmemb);
  2294. std::ofstream* fout = static_cast<std::ofstream*>(data);
  2295. const char* chPtr = static_cast<char*>(ptr);
  2296. fout->write(chPtr, realsize);
  2297. return realsize;
  2298. }
  2299. static size_t
  2300. cmFileCommandCurlDebugCallback(CURL *, curl_infotype, char *chPtr,
  2301. size_t size, void *data)
  2302. {
  2303. cmFileCommandVectorOfChar *vec
  2304. = static_cast<cmFileCommandVectorOfChar*>(data);
  2305. vec->insert(vec->end(), chPtr, chPtr + size);
  2306. return size;
  2307. }
  2308. class cURLProgressHelper
  2309. {
  2310. public:
  2311. cURLProgressHelper(cmFileCommand *fc)
  2312. {
  2313. this->CurrentPercentage = -1;
  2314. this->FileCommand = fc;
  2315. }
  2316. bool UpdatePercentage(double value, double total, std::string &status)
  2317. {
  2318. int OldPercentage = this->CurrentPercentage;
  2319. if (0.0 == total)
  2320. {
  2321. this->CurrentPercentage = 100;
  2322. }
  2323. else
  2324. {
  2325. this->CurrentPercentage = static_cast<int>(value/total*100.0 + 0.5);
  2326. }
  2327. bool updated = (OldPercentage != this->CurrentPercentage);
  2328. if (updated)
  2329. {
  2330. cmOStringStream oss;
  2331. oss << "[download " << this->CurrentPercentage << "% complete]";
  2332. status = oss.str();
  2333. }
  2334. return updated;
  2335. }
  2336. cmFileCommand *GetFileCommand()
  2337. {
  2338. return this->FileCommand;
  2339. }
  2340. private:
  2341. int CurrentPercentage;
  2342. cmFileCommand *FileCommand;
  2343. };
  2344. static int
  2345. cmFileCommandCurlProgressCallback(void *clientp,
  2346. double dltotal, double dlnow,
  2347. double ultotal, double ulnow)
  2348. {
  2349. cURLProgressHelper *helper =
  2350. reinterpret_cast<cURLProgressHelper *>(clientp);
  2351. static_cast<void>(ultotal);
  2352. static_cast<void>(ulnow);
  2353. std::string status;
  2354. if (helper->UpdatePercentage(dlnow, dltotal, status))
  2355. {
  2356. cmFileCommand *fc = helper->GetFileCommand();
  2357. cmMakefile *mf = fc->GetMakefile();
  2358. mf->DisplayStatus(status.c_str(), -1);
  2359. }
  2360. return 0;
  2361. }
  2362. }
  2363. #endif
  2364. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2365. namespace {
  2366. class cURLEasyGuard
  2367. {
  2368. public:
  2369. cURLEasyGuard(CURL * easy)
  2370. : Easy(easy)
  2371. {}
  2372. ~cURLEasyGuard(void)
  2373. {
  2374. if (this->Easy)
  2375. {
  2376. ::curl_easy_cleanup(this->Easy);
  2377. }
  2378. }
  2379. inline void release(void)
  2380. {
  2381. this->Easy = 0;
  2382. return;
  2383. }
  2384. private:
  2385. ::CURL * Easy;
  2386. };
  2387. }
  2388. #endif
  2389. bool
  2390. cmFileCommand::HandleDownloadCommand(std::vector<std::string>
  2391. const& args)
  2392. {
  2393. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2394. std::vector<std::string>::const_iterator i = args.begin();
  2395. if(args.size() < 3)
  2396. {
  2397. this->SetError("DOWNLOAD must be called with at least three arguments.");
  2398. return false;
  2399. }
  2400. ++i; // Get rid of subcommand
  2401. std::string url = *i;
  2402. ++i;
  2403. std::string file = *i;
  2404. ++i;
  2405. long timeout = 0;
  2406. std::string verboseLog;
  2407. std::string statusVar;
  2408. std::string expectedMD5sum;
  2409. bool showProgress = false;
  2410. while(i != args.end())
  2411. {
  2412. if(*i == "TIMEOUT")
  2413. {
  2414. ++i;
  2415. if(i != args.end())
  2416. {
  2417. timeout = atol(i->c_str());
  2418. }
  2419. else
  2420. {
  2421. this->SetError("DOWNLOAD missing time for TIMEOUT.");
  2422. return false;
  2423. }
  2424. }
  2425. else if(*i == "LOG")
  2426. {
  2427. ++i;
  2428. if( i == args.end())
  2429. {
  2430. this->SetError("DOWNLOAD missing VAR for LOG.");
  2431. return false;
  2432. }
  2433. verboseLog = *i;
  2434. }
  2435. else if(*i == "STATUS")
  2436. {
  2437. ++i;
  2438. if( i == args.end())
  2439. {
  2440. this->SetError("DOWNLOAD missing VAR for STATUS.");
  2441. return false;
  2442. }
  2443. statusVar = *i;
  2444. }
  2445. else if(*i == "EXPECTED_MD5")
  2446. {
  2447. ++i;
  2448. if( i == args.end())
  2449. {
  2450. this->SetError("DOWNLOAD missing sum value for EXPECTED_MD5.");
  2451. return false;
  2452. }
  2453. expectedMD5sum = cmSystemTools::LowerCase(*i);
  2454. }
  2455. else if(*i == "SHOW_PROGRESS")
  2456. {
  2457. showProgress = true;
  2458. }
  2459. ++i;
  2460. }
  2461. // If file exists already, and caller specified an expected md5 sum,
  2462. // and the existing file already has the expected md5 sum, then simply
  2463. // return.
  2464. //
  2465. if(cmSystemTools::FileExists(file.c_str()) &&
  2466. !expectedMD5sum.empty())
  2467. {
  2468. char computedMD5[32];
  2469. if (!cmSystemTools::ComputeFileMD5(file.c_str(), computedMD5))
  2470. {
  2471. this->SetError("DOWNLOAD cannot compute MD5 sum on pre-existing file");
  2472. return false;
  2473. }
  2474. std::string actualMD5sum = cmSystemTools::LowerCase(
  2475. std::string(computedMD5, 32));
  2476. if (expectedMD5sum == actualMD5sum)
  2477. {
  2478. if(statusVar.size())
  2479. {
  2480. cmOStringStream result;
  2481. result << (int)0 << ";\""
  2482. "returning early: file already exists with expected MD5 sum\"";
  2483. this->Makefile->AddDefinition(statusVar.c_str(),
  2484. result.str().c_str());
  2485. }
  2486. return true;
  2487. }
  2488. }
  2489. // Make sure parent directory exists so we can write to the file
  2490. // as we receive downloaded bits from curl...
  2491. //
  2492. std::string dir = cmSystemTools::GetFilenamePath(file.c_str());
  2493. if(!cmSystemTools::FileExists(dir.c_str()) &&
  2494. !cmSystemTools::MakeDirectory(dir.c_str()))
  2495. {
  2496. std::string errstring = "DOWNLOAD error: cannot create directory '"
  2497. + dir + "' - Specify file by full path name and verify that you "
  2498. "have directory creation and file write privileges.";
  2499. this->SetError(errstring.c_str());
  2500. return false;
  2501. }
  2502. std::ofstream fout(file.c_str(), std::ios::binary);
  2503. if(!fout)
  2504. {
  2505. this->SetError("DOWNLOAD cannot open file for write.");
  2506. return false;
  2507. }
  2508. ::CURL *curl;
  2509. ::curl_global_init(CURL_GLOBAL_DEFAULT);
  2510. curl = ::curl_easy_init();
  2511. if(!curl)
  2512. {
  2513. this->SetError("DOWNLOAD error initializing curl.");
  2514. return false;
  2515. }
  2516. cURLEasyGuard g_curl(curl);
  2517. ::CURLcode res = ::curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  2518. if (res != CURLE_OK)
  2519. {
  2520. std::string e = "DOWNLOAD cannot set url: ";
  2521. e += ::curl_easy_strerror(res);
  2522. this->SetError(e.c_str());
  2523. return false;
  2524. }
  2525. res = ::curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
  2526. cmFileCommandWriteMemoryCallback);
  2527. if (res != CURLE_OK)
  2528. {
  2529. std::string e = "DOWNLOAD cannot set write function: ";
  2530. e += ::curl_easy_strerror(res);
  2531. this->SetError(e.c_str());
  2532. return false;
  2533. }
  2534. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION,
  2535. cmFileCommandCurlDebugCallback);
  2536. if (res != CURLE_OK)
  2537. {
  2538. std::string e = "DOWNLOAD cannot set debug function: ";
  2539. e += ::curl_easy_strerror(res);
  2540. this->SetError(e.c_str());
  2541. return false;
  2542. }
  2543. cmFileCommandVectorOfChar chunkDebug;
  2544. res = ::curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&fout);
  2545. if (res != CURLE_OK)
  2546. {
  2547. std::string e = "DOWNLOAD cannot set write data: ";
  2548. e += ::curl_easy_strerror(res);
  2549. this->SetError(e.c_str());
  2550. return false;
  2551. }
  2552. res = ::curl_easy_setopt(curl, CURLOPT_DEBUGDATA, (void *)&chunkDebug);
  2553. if (res != CURLE_OK)
  2554. {
  2555. std::string e = "DOWNLOAD cannot set debug data: ";
  2556. e += ::curl_easy_strerror(res);
  2557. this->SetError(e.c_str());
  2558. return false;
  2559. }
  2560. res = ::curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  2561. if (res != CURLE_OK)
  2562. {
  2563. std::string e = "DOWNLOAD cannot set follow-redirect option: ";
  2564. e += ::curl_easy_strerror(res);
  2565. this->SetError(e.c_str());
  2566. return false;
  2567. }
  2568. if(verboseLog.size())
  2569. {
  2570. res = ::curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  2571. if (res != CURLE_OK)
  2572. {
  2573. std::string e = "DOWNLOAD cannot set verbose: ";
  2574. e += ::curl_easy_strerror(res);
  2575. this->SetError(e.c_str());
  2576. return false;
  2577. }
  2578. }
  2579. if(timeout > 0)
  2580. {
  2581. res = ::curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout );
  2582. if (res != CURLE_OK)
  2583. {
  2584. std::string e = "DOWNLOAD cannot set timeout: ";
  2585. e += ::curl_easy_strerror(res);
  2586. this->SetError(e.c_str());
  2587. return false;
  2588. }
  2589. }
  2590. // Need the progress helper's scope to last through the duration of
  2591. // the curl_easy_perform call... so this object is declared at function
  2592. // scope intentionally, rather than inside the "if(showProgress)"
  2593. // block...
  2594. //
  2595. cURLProgressHelper helper(this);
  2596. if(showProgress)
  2597. {
  2598. res = ::curl_easy_setopt(curl,
  2599. CURLOPT_NOPROGRESS, 0);
  2600. if (res != CURLE_OK)
  2601. {
  2602. std::string e = "DOWNLOAD cannot set noprogress value: ";
  2603. e += ::curl_easy_strerror(res);
  2604. this->SetError(e.c_str());
  2605. return false;
  2606. }
  2607. res = ::curl_easy_setopt(curl,
  2608. CURLOPT_PROGRESSFUNCTION, cmFileCommandCurlProgressCallback);
  2609. if (res != CURLE_OK)
  2610. {
  2611. std::string e = "DOWNLOAD cannot set progress function: ";
  2612. e += ::curl_easy_strerror(res);
  2613. this->SetError(e.c_str());
  2614. return false;
  2615. }
  2616. res = ::curl_easy_setopt(curl,
  2617. CURLOPT_PROGRESSDATA, reinterpret_cast<void*>(&helper));
  2618. if (res != CURLE_OK)
  2619. {
  2620. std::string e = "DOWNLOAD cannot set progress data: ";
  2621. e += ::curl_easy_strerror(res);
  2622. this->SetError(e.c_str());
  2623. return false;
  2624. }
  2625. }
  2626. res = ::curl_easy_perform(curl);
  2627. /* always cleanup */
  2628. g_curl.release();
  2629. ::curl_easy_cleanup(curl);
  2630. if(statusVar.size())
  2631. {
  2632. cmOStringStream result;
  2633. result << (int)res << ";\"" << ::curl_easy_strerror(res) << "\"";
  2634. this->Makefile->AddDefinition(statusVar.c_str(),
  2635. result.str().c_str());
  2636. }
  2637. ::curl_global_cleanup();
  2638. // Explicitly flush/close so we can measure the md5 accurately.
  2639. //
  2640. fout.flush();
  2641. fout.close();
  2642. // Verify MD5 sum if requested:
  2643. //
  2644. if (!expectedMD5sum.empty())
  2645. {
  2646. char computedMD5[32];
  2647. if (!cmSystemTools::ComputeFileMD5(file.c_str(), computedMD5))
  2648. {
  2649. this->SetError("DOWNLOAD cannot compute MD5 sum on downloaded file");
  2650. return false;
  2651. }
  2652. std::string actualMD5sum = cmSystemTools::LowerCase(
  2653. std::string(computedMD5, 32));
  2654. if (expectedMD5sum != actualMD5sum)
  2655. {
  2656. cmOStringStream oss;
  2657. oss << "DOWNLOAD MD5 mismatch" << std::endl
  2658. << " for file: [" << file << "]" << std::endl
  2659. << " expected MD5 sum: [" << expectedMD5sum << "]" << std::endl
  2660. << " actual MD5 sum: [" << actualMD5sum << "]" << std::endl
  2661. ;
  2662. this->SetError(oss.str().c_str());
  2663. return false;
  2664. }
  2665. }
  2666. if(chunkDebug.size())
  2667. {
  2668. chunkDebug.push_back(0);
  2669. if(CURLE_OPERATION_TIMEOUTED == res)
  2670. {
  2671. std::string output = &*chunkDebug.begin();
  2672. if(verboseLog.size())
  2673. {
  2674. this->Makefile->AddDefinition(verboseLog.c_str(),
  2675. &*chunkDebug.begin());
  2676. }
  2677. }
  2678. this->Makefile->AddDefinition(verboseLog.c_str(),
  2679. &*chunkDebug.begin());
  2680. }
  2681. return true;
  2682. #else
  2683. this->SetError("DOWNLOAD not supported by bootstrap cmake.");
  2684. return false;
  2685. #endif
  2686. }