cmFileCommand.cxx 71 KB

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