cmFileCommand.cxx 69 KB

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