cmFileCommand.cxx 66 KB

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