cmFileCommand.cxx 64 KB

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