cmFileCommand.cxx 89 KB

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