cmFileCommand.cxx 92 KB

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