cmFileCommand.cxx 90 KB

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