cmFileCommand.cxx 69 KB

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