cmFileCommand.cxx 89 KB

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