cmFileCommand.cxx 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  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. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <cmsys/Directory.hxx>
  20. #include <cmsys/Glob.hxx>
  21. #include <cmsys/RegularExpression.hxx>
  22. // Table of permissions flags.
  23. #if defined(_WIN32) && !defined(__CYGWIN__)
  24. static mode_t mode_owner_read = S_IREAD;
  25. static mode_t mode_owner_write = S_IWRITE;
  26. static mode_t mode_owner_execute = S_IEXEC;
  27. static mode_t mode_group_read = 0;
  28. static mode_t mode_group_write = 0;
  29. static mode_t mode_group_execute = 0;
  30. static mode_t mode_world_read = 0;
  31. static mode_t mode_world_write = 0;
  32. static mode_t mode_world_execute = 0;
  33. static mode_t mode_setuid = 0;
  34. static mode_t mode_setgid = 0;
  35. #else
  36. static mode_t mode_owner_read = S_IRUSR;
  37. static mode_t mode_owner_write = S_IWUSR;
  38. static mode_t mode_owner_execute = S_IXUSR;
  39. static mode_t mode_group_read = S_IRGRP;
  40. static mode_t mode_group_write = S_IWGRP;
  41. static mode_t mode_group_execute = S_IXGRP;
  42. static mode_t mode_world_read = S_IROTH;
  43. static mode_t mode_world_write = S_IWOTH;
  44. static mode_t mode_world_execute = S_IXOTH;
  45. static mode_t mode_setuid = S_ISUID;
  46. static mode_t mode_setgid = S_ISGID;
  47. #endif
  48. // cmLibraryCommand
  49. bool cmFileCommand
  50. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  51. {
  52. if(args.size() < 2 )
  53. {
  54. this->SetError("must be called with at least two arguments.");
  55. return false;
  56. }
  57. std::string subCommand = args[0];
  58. if ( subCommand == "WRITE" )
  59. {
  60. return this->HandleWriteCommand(args, false);
  61. }
  62. else if ( subCommand == "APPEND" )
  63. {
  64. return this->HandleWriteCommand(args, true);
  65. }
  66. else if ( subCommand == "READ" )
  67. {
  68. return this->HandleReadCommand(args);
  69. }
  70. else if ( subCommand == "STRINGS" )
  71. {
  72. return this->HandleStringsCommand(args);
  73. }
  74. else if ( subCommand == "GLOB" )
  75. {
  76. return this->HandleGlobCommand(args, false);
  77. }
  78. else if ( subCommand == "GLOB_RECURSE" )
  79. {
  80. return this->HandleGlobCommand(args, true);
  81. }
  82. else if ( subCommand == "MAKE_DIRECTORY" )
  83. {
  84. return this->HandleMakeDirectoryCommand(args);
  85. }
  86. else if ( subCommand == "REMOVE" )
  87. {
  88. return this->HandleRemove(args, false);
  89. }
  90. else if ( subCommand == "REMOVE_RECURSE" )
  91. {
  92. return this->HandleRemove(args, true);
  93. }
  94. else if ( subCommand == "INSTALL" )
  95. {
  96. return this->HandleInstallCommand(args);
  97. }
  98. else if ( subCommand == "RELATIVE_PATH" )
  99. {
  100. return this->HandleRelativePathCommand(args);
  101. }
  102. else if ( subCommand == "TO_CMAKE_PATH" )
  103. {
  104. return this->HandleCMakePathCommand(args, false);
  105. }
  106. else if ( subCommand == "TO_NATIVE_PATH" )
  107. {
  108. return this->HandleCMakePathCommand(args, true);
  109. }
  110. std::string e = "does not recognize sub-command "+subCommand;
  111. this->SetError(e.c_str());
  112. return false;
  113. }
  114. //----------------------------------------------------------------------------
  115. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  116. bool append)
  117. {
  118. std::string message;
  119. std::vector<std::string>::const_iterator i = args.begin();
  120. i++; // Get rid of subcommand
  121. std::string fileName = *i;
  122. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  123. {
  124. fileName = this->Makefile->GetCurrentDirectory();
  125. fileName += "/" + *i;
  126. }
  127. i++;
  128. for(;i != args.end(); ++i)
  129. {
  130. message += *i;
  131. }
  132. if ( !this->Makefile->CanIWriteThisFile(fileName.c_str()) )
  133. {
  134. std::string e
  135. = "attempted to write a file: " + fileName +
  136. " into a source directory.";
  137. this->SetError(e.c_str());
  138. cmSystemTools::SetFatalErrorOccured();
  139. return false;
  140. }
  141. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  142. cmSystemTools::MakeDirectory(dir.c_str());
  143. mode_t mode =
  144. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  145. S_IREAD | S_IWRITE
  146. #elif defined( __BORLANDC__ )
  147. S_IRUSR | S_IWUSR
  148. #else
  149. S_IRUSR | S_IWUSR |
  150. S_IRGRP |
  151. S_IROTH
  152. #endif
  153. ;
  154. // Set permissions to writable
  155. if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
  156. {
  157. cmSystemTools::SetPermissions(fileName.c_str(),
  158. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  159. S_IREAD | S_IWRITE
  160. #else
  161. S_IRUSR | S_IWUSR
  162. #endif
  163. );
  164. }
  165. // If GetPermissions fails, pretend like it is ok. File open will fail if
  166. // the file is not writable
  167. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  168. if ( !file )
  169. {
  170. std::string error = "Internal CMake error when trying to open file: ";
  171. error += fileName.c_str();
  172. error += " for writing.";
  173. this->SetError(error.c_str());
  174. return false;
  175. }
  176. file << message;
  177. file.close();
  178. cmSystemTools::SetPermissions(fileName.c_str(), mode);
  179. this->Makefile->AddWrittenFile(fileName.c_str());
  180. return true;
  181. }
  182. //----------------------------------------------------------------------------
  183. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  184. {
  185. if ( args.size() < 3 )
  186. {
  187. this->SetError("READ must be called with at least two additional "
  188. "arguments");
  189. return false;
  190. }
  191. cmCommandArgumentsHelper argHelper;
  192. cmCommandArgumentGroup group;
  193. cmCAString readArg (&argHelper, "READ");
  194. cmCAString fileNameArg (&argHelper, 0);
  195. cmCAString resultArg (&argHelper, 0);
  196. cmCAString offsetArg (&argHelper, "OFFSET", &group);
  197. cmCAString limitArg (&argHelper, "LIMIT", &group);
  198. cmCAEnabler hexOutputArg (&argHelper, "HEX", &group);
  199. readArg.Follows(0);
  200. fileNameArg.Follows(&readArg);
  201. resultArg.Follows(&fileNameArg);
  202. group.Follows(&resultArg);
  203. argHelper.Parse(&args, 0);
  204. std::string fileName = fileNameArg.GetString();
  205. if ( !cmsys::SystemTools::FileIsFullPath(fileName.c_str()) )
  206. {
  207. fileName = this->Makefile->GetCurrentDirectory();
  208. fileName += "/" + fileNameArg.GetString();
  209. }
  210. std::string variable = resultArg.GetString();
  211. // Open the specified file.
  212. #if defined(_WIN32) || defined(__CYGWIN__)
  213. std::ifstream file(fileName.c_str(), std::ios::in |
  214. (hexOutputArg.IsEnabled() ? std::ios::binary : std::ios::in));
  215. #else
  216. std::ifstream file(fileName.c_str(), std::ios::in);
  217. #endif
  218. if ( !file )
  219. {
  220. std::string error = "Internal CMake error when trying to open file: ";
  221. error += fileName.c_str();
  222. error += " for reading.";
  223. this->SetError(error.c_str());
  224. return false;
  225. }
  226. // is there a limit?
  227. long sizeLimit = -1;
  228. if (limitArg.GetString().size() > 0)
  229. {
  230. sizeLimit = atoi(limitArg.GetCString());
  231. }
  232. // is there an offset?
  233. long offset = 0;
  234. if (offsetArg.GetString().size() > 0)
  235. {
  236. offset = atoi(offsetArg.GetCString());
  237. }
  238. file.seekg(offset);
  239. std::string output;
  240. if (hexOutputArg.IsEnabled())
  241. {
  242. // Convert part of the file into hex code
  243. int c;
  244. while((sizeLimit != 0) && (c = file.get(), file))
  245. {
  246. char hex[4];
  247. sprintf(hex, "%x", c&0xff);
  248. output += hex;
  249. if (sizeLimit > 0)
  250. {
  251. sizeLimit--;
  252. }
  253. }
  254. }
  255. else
  256. {
  257. std::string line;
  258. bool has_newline = false;
  259. while (sizeLimit != 0 &&
  260. cmSystemTools::GetLineFromStream(file, line, &has_newline,
  261. sizeLimit) )
  262. {
  263. if (sizeLimit > 0)
  264. {
  265. sizeLimit = sizeLimit - static_cast<long>(line.size());
  266. if (has_newline)
  267. {
  268. sizeLimit--;
  269. }
  270. if (sizeLimit < 0)
  271. {
  272. sizeLimit = 0;
  273. }
  274. }
  275. output += line;
  276. if ( has_newline )
  277. {
  278. output += "\n";
  279. }
  280. }
  281. }
  282. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  283. return true;
  284. }
  285. //----------------------------------------------------------------------------
  286. bool cmFileCommand::HandleStringsCommand(std::vector<std::string> const& args)
  287. {
  288. if(args.size() < 3)
  289. {
  290. this->SetError("STRINGS requires a file name and output variable");
  291. return false;
  292. }
  293. // Get the file to read.
  294. std::string fileName = args[1];
  295. if(!cmsys::SystemTools::FileIsFullPath(fileName.c_str()))
  296. {
  297. fileName = this->Makefile->GetCurrentDirectory();
  298. fileName += "/" + args[1];
  299. }
  300. // Get the variable in which to store the results.
  301. std::string outVar = args[2];
  302. // Parse the options.
  303. enum { arg_none,
  304. arg_limit_input,
  305. arg_limit_output,
  306. arg_limit_count,
  307. arg_length_minimum,
  308. arg_length_maximum,
  309. arg__maximum,
  310. arg_regex };
  311. unsigned int minlen = 0;
  312. unsigned int maxlen = 0;
  313. int limit_input = -1;
  314. int limit_output = -1;
  315. unsigned int limit_count = 0;
  316. cmsys::RegularExpression regex;
  317. bool have_regex = false;
  318. bool newline_consume = false;
  319. bool hex_conversion_enabled = true;
  320. int arg_mode = arg_none;
  321. for(unsigned int i=3; i < args.size(); ++i)
  322. {
  323. if(args[i] == "LIMIT_INPUT")
  324. {
  325. arg_mode = arg_limit_input;
  326. }
  327. else if(args[i] == "LIMIT_OUTPUT")
  328. {
  329. arg_mode = arg_limit_output;
  330. }
  331. else if(args[i] == "LIMIT_COUNT")
  332. {
  333. arg_mode = arg_limit_count;
  334. }
  335. else if(args[i] == "LENGTH_MINIMUM")
  336. {
  337. arg_mode = arg_length_minimum;
  338. }
  339. else if(args[i] == "LENGTH_MAXIMUM")
  340. {
  341. arg_mode = arg_length_maximum;
  342. }
  343. else if(args[i] == "REGEX")
  344. {
  345. arg_mode = arg_regex;
  346. }
  347. else if(args[i] == "NEWLINE_CONSUME")
  348. {
  349. newline_consume = true;
  350. arg_mode = arg_none;
  351. }
  352. else if(args[i] == "NO_HEX_CONVERSION")
  353. {
  354. hex_conversion_enabled = false;
  355. arg_mode = arg_none;
  356. }
  357. else if(arg_mode == arg_limit_input)
  358. {
  359. if(sscanf(args[i].c_str(), "%d", &limit_input) != 1 ||
  360. limit_input < 0)
  361. {
  362. cmOStringStream e;
  363. e << "STRINGS option LIMIT_INPUT value \""
  364. << args[i] << "\" is not an unsigned integer.";
  365. this->SetError(e.str().c_str());
  366. return false;
  367. }
  368. arg_mode = arg_none;
  369. }
  370. else if(arg_mode == arg_limit_output)
  371. {
  372. if(sscanf(args[i].c_str(), "%d", &limit_output) != 1 ||
  373. limit_output < 0)
  374. {
  375. cmOStringStream e;
  376. e << "STRINGS option LIMIT_OUTPUT value \""
  377. << args[i] << "\" is not an unsigned integer.";
  378. this->SetError(e.str().c_str());
  379. return false;
  380. }
  381. arg_mode = arg_none;
  382. }
  383. else if(arg_mode == arg_limit_count)
  384. {
  385. int count;
  386. if(sscanf(args[i].c_str(), "%d", &count) != 1 || count < 0)
  387. {
  388. cmOStringStream e;
  389. e << "STRINGS option LIMIT_COUNT value \""
  390. << args[i] << "\" is not an unsigned integer.";
  391. this->SetError(e.str().c_str());
  392. return false;
  393. }
  394. limit_count = count;
  395. arg_mode = arg_none;
  396. }
  397. else if(arg_mode == arg_length_minimum)
  398. {
  399. int len;
  400. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  401. {
  402. cmOStringStream e;
  403. e << "STRINGS option LENGTH_MINIMUM value \""
  404. << args[i] << "\" is not an unsigned integer.";
  405. this->SetError(e.str().c_str());
  406. return false;
  407. }
  408. minlen = len;
  409. arg_mode = arg_none;
  410. }
  411. else if(arg_mode == arg_length_maximum)
  412. {
  413. int len;
  414. if(sscanf(args[i].c_str(), "%d", &len) != 1 || len < 0)
  415. {
  416. cmOStringStream e;
  417. e << "STRINGS option LENGTH_MAXIMUM value \""
  418. << args[i] << "\" is not an unsigned integer.";
  419. this->SetError(e.str().c_str());
  420. return false;
  421. }
  422. maxlen = len;
  423. arg_mode = arg_none;
  424. }
  425. else if(arg_mode == arg_regex)
  426. {
  427. if(!regex.compile(args[i].c_str()))
  428. {
  429. cmOStringStream e;
  430. e << "STRINGS option REGEX value \""
  431. << args[i] << "\" could not be compiled.";
  432. this->SetError(e.str().c_str());
  433. return false;
  434. }
  435. have_regex = true;
  436. arg_mode = arg_none;
  437. }
  438. else
  439. {
  440. cmOStringStream e;
  441. e << "STRINGS given unknown argument \""
  442. << args[i] << "\"";
  443. this->SetError(e.str().c_str());
  444. return false;
  445. }
  446. }
  447. if (hex_conversion_enabled)
  448. {
  449. // TODO: should work without temp file, but just on a memory buffer
  450. std::string binaryFileName = this->Makefile->GetCurrentOutputDirectory();
  451. binaryFileName += cmake::GetCMakeFilesDirectory();
  452. binaryFileName += "/FileCommandStringsBinaryFile";
  453. if(cmHexFileConverter::TryConvert(fileName.c_str(),binaryFileName.c_str()))
  454. {
  455. fileName = binaryFileName;
  456. }
  457. }
  458. // Open the specified file.
  459. #if defined(_WIN32) || defined(__CYGWIN__)
  460. std::ifstream fin(fileName.c_str(), std::ios::in | std::ios::binary);
  461. #else
  462. std::ifstream fin(fileName.c_str(), std::ios::in);
  463. #endif
  464. if(!fin)
  465. {
  466. cmOStringStream e;
  467. e << "STRINGS file \"" << fileName << "\" cannot be read.";
  468. this->SetError(e.str().c_str());
  469. return false;
  470. }
  471. // Parse strings out of the file.
  472. int output_size = 0;
  473. std::vector<std::string> strings;
  474. std::string s;
  475. int c;
  476. while((!limit_count || strings.size() < limit_count) &&
  477. (limit_input < 0 || static_cast<int>(fin.tellg()) < limit_input) &&
  478. (c = fin.get(), fin))
  479. {
  480. if(c == '\0')
  481. {
  482. // A terminating null character has been found. Check if the
  483. // current string matches the requirements. Since it was
  484. // terminated by a null character, we require that the length be
  485. // at least one no matter what the user specified.
  486. if(s.length() >= minlen && s.length() >= 1 &&
  487. (!have_regex || regex.find(s.c_str())))
  488. {
  489. output_size += static_cast<int>(s.size()) + 1;
  490. if(limit_output >= 0 && output_size >= limit_output)
  491. {
  492. s = "";
  493. break;
  494. }
  495. strings.push_back(s);
  496. }
  497. // Reset the string to empty.
  498. s = "";
  499. }
  500. else if(c == '\n' && !newline_consume)
  501. {
  502. // The current line has been terminated. Check if the current
  503. // string matches the requirements. The length may now be as
  504. // low as zero since blank lines are allowed.
  505. if(s.length() >= minlen &&
  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 == '\r')
  520. {
  521. // Ignore CR character to make output always have UNIX newlines.
  522. }
  523. else if(c >= 0x20 && c < 0x7F || c == '\t' ||
  524. (c == '\n' && newline_consume))
  525. {
  526. // This is an ASCII character that may be part of a string.
  527. s += c;
  528. }
  529. else
  530. {
  531. // This is a non-string character. Reset the string to emtpy.
  532. s = "";
  533. }
  534. // Terminate a string if the maximum length is reached.
  535. if(maxlen > 0 && s.size() == maxlen)
  536. {
  537. if(s.length() >= minlen &&
  538. (!have_regex || regex.find(s.c_str())))
  539. {
  540. output_size += static_cast<int>(s.size()) + 1;
  541. if(limit_output >= 0 && output_size >= limit_output)
  542. {
  543. s = "";
  544. break;
  545. }
  546. strings.push_back(s);
  547. }
  548. s = "";
  549. }
  550. }
  551. // If there is a non-empty current string we have hit the end of the
  552. // input file or the input size limit. Check if the current string
  553. // matches the requirements.
  554. if((!limit_count || strings.size() < limit_count) &&
  555. !s.empty() && s.length() >= minlen &&
  556. (!have_regex || regex.find(s.c_str())))
  557. {
  558. output_size += static_cast<int>(s.size()) + 1;
  559. if(limit_output < 0 || output_size < limit_output)
  560. {
  561. strings.push_back(s);
  562. }
  563. }
  564. // Encode the result in a CMake list.
  565. const char* sep = "";
  566. std::string output;
  567. for(std::vector<std::string>::const_iterator si = strings.begin();
  568. si != strings.end(); ++si)
  569. {
  570. // Separate the strings in the output to make it a list.
  571. output += sep;
  572. sep = ";";
  573. // Store the string in the output, but escape semicolons to
  574. // make sure it is a list.
  575. std::string const& sr = *si;
  576. for(unsigned int i=0; i < sr.size(); ++i)
  577. {
  578. if(sr[i] == ';')
  579. {
  580. output += '\\';
  581. }
  582. output += sr[i];
  583. }
  584. }
  585. // Save the output in a makefile variable.
  586. this->Makefile->AddDefinition(outVar.c_str(), output.c_str());
  587. return true;
  588. }
  589. //----------------------------------------------------------------------------
  590. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  591. bool recurse)
  592. {
  593. if ( args.size() < 2 )
  594. {
  595. this->SetError("GLOB requires at least a variable name");
  596. return false;
  597. }
  598. std::vector<std::string>::const_iterator i = args.begin();
  599. i++; // Get rid of subcommand
  600. std::string variable = *i;
  601. i++;
  602. cmsys::Glob g;
  603. g.SetRecurse(recurse);
  604. std::string output = "";
  605. bool first = true;
  606. for ( ; i != args.end(); ++i )
  607. {
  608. if ( *i == "RELATIVE" )
  609. {
  610. ++i; // skip RELATIVE
  611. if ( i == args.end() )
  612. {
  613. this->SetError("GLOB requires a directory after the RELATIVE tag");
  614. return false;
  615. }
  616. g.SetRelative(i->c_str());
  617. ++i;
  618. if(i == args.end())
  619. {
  620. this->SetError("GLOB requires a glob expression after the directory");
  621. return false;
  622. }
  623. }
  624. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  625. {
  626. std::string expr = this->Makefile->GetCurrentDirectory();
  627. // Handle script mode
  628. if ( expr.size() > 0 )
  629. {
  630. expr += "/" + *i;
  631. g.FindFiles(expr);
  632. }
  633. else
  634. {
  635. g.FindFiles(*i);
  636. }
  637. }
  638. else
  639. {
  640. g.FindFiles(*i);
  641. }
  642. std::vector<std::string>::size_type cc;
  643. std::vector<std::string>& files = g.GetFiles();
  644. for ( cc = 0; cc < files.size(); cc ++ )
  645. {
  646. if ( !first )
  647. {
  648. output += ";";
  649. }
  650. output += files[cc];
  651. first = false;
  652. }
  653. }
  654. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  655. return true;
  656. }
  657. //----------------------------------------------------------------------------
  658. bool cmFileCommand::HandleMakeDirectoryCommand(
  659. std::vector<std::string> const& args)
  660. {
  661. if(args.size() < 2 )
  662. {
  663. this->SetError("called with incorrect number of arguments");
  664. return false;
  665. }
  666. std::vector<std::string>::const_iterator i = args.begin();
  667. i++; // Get rid of subcommand
  668. std::string expr;
  669. for ( ; i != args.end(); ++i )
  670. {
  671. const std::string* cdir = &(*i);
  672. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  673. {
  674. expr = this->Makefile->GetCurrentDirectory();
  675. expr += "/" + *i;
  676. cdir = &expr;
  677. }
  678. if ( !this->Makefile->CanIWriteThisFile(cdir->c_str()) )
  679. {
  680. std::string e = "attempted to create a directory: " + *cdir
  681. + " into a source directory.";
  682. this->SetError(e.c_str());
  683. cmSystemTools::SetFatalErrorOccured();
  684. return false;
  685. }
  686. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  687. {
  688. std::string error = "problem creating directory: " + *cdir;
  689. this->SetError(error.c_str());
  690. return false;
  691. }
  692. }
  693. return true;
  694. }
  695. //----------------------------------------------------------------------------
  696. // File installation helper class.
  697. struct cmFileInstaller
  698. {
  699. // Methods to actually install files.
  700. bool InstallFile(const char* fromFile, const char* toFile, bool always);
  701. bool InstallDirectory(const char* source, const char* destination,
  702. bool always);
  703. // All instances need the file command and makefile using them.
  704. cmFileInstaller(cmFileCommand* fc, cmMakefile* mf):
  705. FileCommand(fc), Makefile(mf), DestDirLength(0), MatchlessFiles(true)
  706. {
  707. // Get the current manifest.
  708. this->Manifest =
  709. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  710. }
  711. ~cmFileInstaller()
  712. {
  713. // Save the updated install manifest.
  714. this->Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  715. this->Manifest.c_str());
  716. }
  717. private:
  718. cmFileCommand* FileCommand;
  719. cmMakefile* Makefile;
  720. cmFileTimeComparison FileTimes;
  721. public:
  722. // The length of the destdir setting.
  723. int DestDirLength;
  724. // Whether to install a file not matching any expression.
  725. bool MatchlessFiles;
  726. // The current file manifest (semicolon separated list).
  727. std::string Manifest;
  728. // Permissions for files and directories installed by this object.
  729. mode_t FilePermissions;
  730. mode_t DirPermissions;
  731. // Properties set by pattern and regex match rules.
  732. struct MatchProperties
  733. {
  734. bool Exclude;
  735. mode_t Permissions;
  736. MatchProperties(): Exclude(false), Permissions(0) {}
  737. };
  738. struct MatchRule
  739. {
  740. cmsys::RegularExpression Regex;
  741. MatchProperties Properties;
  742. std::string RegexString;
  743. MatchRule(std::string const& regex):
  744. Regex(regex.c_str()), RegexString(regex) {}
  745. };
  746. std::vector<MatchRule> MatchRules;
  747. // Get the properties from rules matching this input file.
  748. MatchProperties CollectMatchProperties(const char* file,
  749. bool isDirectory)
  750. {
  751. // Match rules are case-insensitive on some platforms.
  752. #if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
  753. std::string lower = cmSystemTools::LowerCase(file);
  754. file = lower.c_str();
  755. #endif
  756. // Collect properties from all matching rules.
  757. bool matched = false;
  758. MatchProperties result;
  759. for(std::vector<MatchRule>::iterator mr = this->MatchRules.begin();
  760. mr != this->MatchRules.end(); ++mr)
  761. {
  762. if(mr->Regex.find(file))
  763. {
  764. matched = true;
  765. result.Exclude |= mr->Properties.Exclude;
  766. result.Permissions |= mr->Properties.Permissions;
  767. }
  768. }
  769. if(!matched && !this->MatchlessFiles && !isDirectory)
  770. {
  771. result.Exclude = true;
  772. }
  773. return result;
  774. }
  775. // Append a file to the installation manifest.
  776. void ManifestAppend(std::string const& file)
  777. {
  778. this->Manifest += ";";
  779. this->Manifest += file.substr(this->DestDirLength);
  780. }
  781. // Translate an argument to a permissions bit.
  782. bool CheckPermissions(std::string const& arg, mode_t& permissions)
  783. {
  784. if(arg == "OWNER_READ") { permissions |= mode_owner_read; }
  785. else if(arg == "OWNER_WRITE") { permissions |= mode_owner_write; }
  786. else if(arg == "OWNER_EXECUTE") { permissions |= mode_owner_execute; }
  787. else if(arg == "GROUP_READ") { permissions |= mode_group_read; }
  788. else if(arg == "GROUP_WRITE") { permissions |= mode_group_write; }
  789. else if(arg == "GROUP_EXECUTE") { permissions |= mode_group_execute; }
  790. else if(arg == "WORLD_READ") { permissions |= mode_world_read; }
  791. else if(arg == "WORLD_WRITE") { permissions |= mode_world_write; }
  792. else if(arg == "WORLD_EXECUTE") { permissions |= mode_world_execute; }
  793. else if(arg == "SETUID") { permissions |= mode_setuid; }
  794. else if(arg == "SETGID") { permissions |= mode_setgid; }
  795. else
  796. {
  797. cmOStringStream e;
  798. e << "INSTALL given invalid permission \"" << arg << "\".";
  799. this->FileCommand->SetError(e.str().c_str());
  800. return false;
  801. }
  802. return true;
  803. }
  804. private:
  805. bool InstallSymlink(const char* fromFile, const char* toFile, bool always);
  806. };
  807. //----------------------------------------------------------------------------
  808. bool cmFileInstaller::InstallSymlink(const char* fromFile, const char* toFile,
  809. bool always)
  810. {
  811. // Read the original symlink.
  812. std::string symlinkTarget;
  813. if(!cmSystemTools::ReadSymlink(fromFile, symlinkTarget))
  814. {
  815. cmOStringStream e;
  816. e << "INSTALL cannot read symlink \"" << fromFile
  817. << "\" to duplicate at \"" << toFile << "\".";
  818. this->FileCommand->SetError(e.str().c_str());
  819. return false;
  820. }
  821. // Compare the symlink value to that at the destination if not
  822. // always installing.
  823. bool copy = true;
  824. if(!always)
  825. {
  826. std::string oldSymlinkTarget;
  827. if(cmSystemTools::ReadSymlink(toFile, oldSymlinkTarget))
  828. {
  829. if(symlinkTarget == oldSymlinkTarget)
  830. {
  831. copy = false;
  832. }
  833. }
  834. }
  835. // Inform the user about this file installation.
  836. std::string message = (copy? "Installing: " : "Up-to-date: ");
  837. message += toFile;
  838. this->Makefile->DisplayStatus(message.c_str(), -1);
  839. if(copy)
  840. {
  841. // Remove the destination file so we can always create the symlink.
  842. cmSystemTools::RemoveFile(toFile);
  843. // Create the symlink.
  844. if(!cmSystemTools::CreateSymlink(symlinkTarget.c_str(), toFile))
  845. {
  846. cmOStringStream e;
  847. e << "INSTALL cannot duplicate symlink \"" << fromFile
  848. << "\" at \"" << toFile << "\".";
  849. this->FileCommand->SetError(e.str().c_str());
  850. return false;
  851. }
  852. }
  853. // Add the file to the manifest.
  854. this->ManifestAppend(toFile);
  855. return true;
  856. }
  857. //----------------------------------------------------------------------------
  858. bool cmFileInstaller::InstallFile(const char* fromFile, const char* toFile,
  859. bool always)
  860. {
  861. // Collect any properties matching this file name.
  862. MatchProperties match_properties =
  863. this->CollectMatchProperties(fromFile, false);
  864. // Skip the file if it is excluded.
  865. if(match_properties.Exclude)
  866. {
  867. return true;
  868. }
  869. // Short-circuit for symbolic links.
  870. if(cmSystemTools::FileIsSymlink(fromFile))
  871. {
  872. return this->InstallSymlink(fromFile, toFile, always);
  873. }
  874. // Determine whether we will copy the file.
  875. bool copy = true;
  876. if(!always)
  877. {
  878. // If both files exist with the same time do not copy.
  879. if(!this->FileTimes.FileTimesDiffer(fromFile, toFile))
  880. {
  881. copy = false;
  882. }
  883. }
  884. // Inform the user about this file installation.
  885. std::string message = (copy? "Installing: " : "Up-to-date: ");
  886. message += toFile;
  887. this->Makefile->DisplayStatus(message.c_str(), -1);
  888. // Copy the file.
  889. if(copy && !cmSystemTools::CopyAFile(fromFile, toFile, true))
  890. {
  891. cmOStringStream e;
  892. e << "INSTALL cannot copy file \"" << fromFile
  893. << "\" to \"" << toFile << "\".";
  894. this->FileCommand->SetError(e.str().c_str());
  895. return false;
  896. }
  897. // Add the file to the manifest.
  898. this->ManifestAppend(toFile);
  899. // Set the file modification time of the destination file.
  900. if(copy && !always)
  901. {
  902. cmSystemTools::CopyFileTime(fromFile, toFile);
  903. }
  904. // Set permissions of the destination file.
  905. mode_t permissions = (match_properties.Permissions?
  906. match_properties.Permissions : this->FilePermissions);
  907. if(!permissions)
  908. {
  909. // No permissions were explicitly provided but the user requested
  910. // that the source file permissions be used.
  911. cmSystemTools::GetPermissions(fromFile, permissions);
  912. }
  913. if(permissions && !cmSystemTools::SetPermissions(toFile, permissions))
  914. {
  915. cmOStringStream e;
  916. e << "Problem setting permissions on file \"" << toFile << "\"";
  917. this->FileCommand->SetError(e.str().c_str());
  918. return false;
  919. }
  920. return true;
  921. }
  922. //----------------------------------------------------------------------------
  923. bool cmFileInstaller::InstallDirectory(const char* source,
  924. const char* destination,
  925. bool always)
  926. {
  927. // Collect any properties matching this directory name.
  928. MatchProperties match_properties =
  929. this->CollectMatchProperties(source, true);
  930. // Skip the directory if it is excluded.
  931. if(match_properties.Exclude)
  932. {
  933. return true;
  934. }
  935. // Short-circuit for symbolic links.
  936. if(cmSystemTools::FileIsSymlink(source))
  937. {
  938. return this->InstallSymlink(source, destination, always);
  939. }
  940. // Inform the user about this directory installation.
  941. std::string message = "Installing: ";
  942. message += destination;
  943. this->Makefile->DisplayStatus(message.c_str(), -1);
  944. // Make sure the destination directory exists.
  945. if(!cmSystemTools::MakeDirectory(destination))
  946. {
  947. return false;
  948. }
  949. // Compute the requested permissions for the destination directory.
  950. mode_t permissions = (match_properties.Permissions?
  951. match_properties.Permissions : this->DirPermissions);
  952. if(!permissions)
  953. {
  954. // No permissions were explicitly provided but the user requested
  955. // that the source directory permissions be used.
  956. cmSystemTools::GetPermissions(source, permissions);
  957. }
  958. // Compute the set of permissions required on this directory to
  959. // recursively install files and subdirectories safely.
  960. mode_t required_permissions =
  961. mode_owner_read | mode_owner_write | mode_owner_execute;
  962. // If the required permissions are specified it is safe to set the
  963. // final permissions now. Otherwise we must add the required
  964. // permissions temporarily during file installation.
  965. mode_t permissions_before = 0;
  966. mode_t permissions_after = 0;
  967. if(permissions & required_permissions)
  968. {
  969. permissions_before = permissions;
  970. }
  971. else
  972. {
  973. permissions_before = permissions | required_permissions;
  974. permissions_after = permissions;
  975. }
  976. // Set the required permissions of the destination directory.
  977. if(permissions_before &&
  978. !cmSystemTools::SetPermissions(destination, permissions_before))
  979. {
  980. cmOStringStream e;
  981. e << "Problem setting permissions on directory \""
  982. << destination << "\"";
  983. this->FileCommand->SetError(e.str().c_str());
  984. return false;
  985. }
  986. // Load the directory contents to traverse it recursively.
  987. cmsys::Directory dir;
  988. if(source && *source)
  989. {
  990. dir.Load(source);
  991. }
  992. unsigned long numFiles = static_cast<unsigned long>(dir.GetNumberOfFiles());
  993. for(unsigned long fileNum = 0; fileNum < numFiles; ++fileNum)
  994. {
  995. if(!(strcmp(dir.GetFile(fileNum), ".") == 0 ||
  996. strcmp(dir.GetFile(fileNum), "..") == 0))
  997. {
  998. cmsys_stl::string fromPath = source;
  999. fromPath += "/";
  1000. fromPath += dir.GetFile(fileNum);
  1001. if(cmSystemTools::FileIsDirectory(fromPath.c_str()))
  1002. {
  1003. cmsys_stl::string toDir = destination;
  1004. toDir += "/";
  1005. toDir += dir.GetFile(fileNum);
  1006. if(!this->InstallDirectory(fromPath.c_str(), toDir.c_str(), always))
  1007. {
  1008. return false;
  1009. }
  1010. }
  1011. else
  1012. {
  1013. // Install this file.
  1014. std::string toFile = destination;
  1015. toFile += "/";
  1016. toFile += dir.GetFile(fileNum);
  1017. if(!this->InstallFile(fromPath.c_str(), toFile.c_str(), always))
  1018. {
  1019. return false;
  1020. }
  1021. }
  1022. }
  1023. }
  1024. // Set the requested permissions of the destination directory.
  1025. if(permissions_after &&
  1026. !cmSystemTools::SetPermissions(destination, permissions_after))
  1027. {
  1028. cmOStringStream e;
  1029. e << "Problem setting permissions on directory \"" << destination << "\"";
  1030. this->FileCommand->SetError(e.str().c_str());
  1031. return false;
  1032. }
  1033. return true;
  1034. }
  1035. //----------------------------------------------------------------------------
  1036. void cmFileCommand::HandleInstallPermissions(cmFileInstaller& installer,
  1037. mode_t& permissions_file,
  1038. mode_t& permissions_dir,
  1039. int itype,
  1040. bool use_given_permissions_file,
  1041. bool use_given_permissions_dir,
  1042. bool use_source_permissions) const
  1043. {
  1044. // Choose a default for shared library permissions.
  1045. bool install_so_no_exe = this->Makefile->IsOn("CMAKE_INSTALL_SO_NO_EXE");
  1046. // If file permissions were not specified set default permissions
  1047. // for this target type.
  1048. if(!use_given_permissions_file && !use_source_permissions)
  1049. {
  1050. switch(itype)
  1051. {
  1052. case cmTarget::SHARED_LIBRARY:
  1053. case cmTarget::MODULE_LIBRARY:
  1054. if(install_so_no_exe)
  1055. {
  1056. // Use read/write permissions.
  1057. permissions_file = 0;
  1058. permissions_file |= mode_owner_read;
  1059. permissions_file |= mode_owner_write;
  1060. permissions_file |= mode_group_read;
  1061. permissions_file |= mode_world_read;
  1062. break;
  1063. }
  1064. case cmTarget::EXECUTABLE:
  1065. case cmTarget::INSTALL_PROGRAMS:
  1066. // Use read/write/executable permissions.
  1067. permissions_file = 0;
  1068. permissions_file |= mode_owner_read;
  1069. permissions_file |= mode_owner_write;
  1070. permissions_file |= mode_owner_execute;
  1071. permissions_file |= mode_group_read;
  1072. permissions_file |= mode_group_execute;
  1073. permissions_file |= mode_world_read;
  1074. permissions_file |= mode_world_execute;
  1075. break;
  1076. default:
  1077. // Use read/write permissions.
  1078. permissions_file = 0;
  1079. permissions_file |= mode_owner_read;
  1080. permissions_file |= mode_owner_write;
  1081. permissions_file |= mode_group_read;
  1082. permissions_file |= mode_world_read;
  1083. break;
  1084. }
  1085. }
  1086. // If directory permissions were not specified set default permissions.
  1087. if(!use_given_permissions_dir && !use_source_permissions)
  1088. {
  1089. // Use read/write/executable permissions.
  1090. permissions_dir = 0;
  1091. permissions_dir |= mode_owner_read;
  1092. permissions_dir |= mode_owner_write;
  1093. permissions_dir |= mode_owner_execute;
  1094. permissions_dir |= mode_group_read;
  1095. permissions_dir |= mode_group_execute;
  1096. permissions_dir |= mode_world_read;
  1097. permissions_dir |= mode_world_execute;
  1098. }
  1099. // Set the installer permissions.
  1100. installer.FilePermissions = permissions_file;
  1101. installer.DirPermissions = permissions_dir;
  1102. }
  1103. //----------------------------------------------------------------------------
  1104. void cmFileCommand
  1105. ::GetTargetTypeFromString(const std::string& stype, int& itype) const
  1106. {
  1107. if ( stype == "EXECUTABLE" )
  1108. {
  1109. itype = cmTarget::EXECUTABLE;
  1110. }
  1111. else if ( stype == "PROGRAM" )
  1112. {
  1113. itype = cmTarget::INSTALL_PROGRAMS;
  1114. }
  1115. else if ( stype == "STATIC_LIBRARY" )
  1116. {
  1117. itype = cmTarget::STATIC_LIBRARY;
  1118. }
  1119. else if ( stype == "SHARED_LIBRARY" )
  1120. {
  1121. itype = cmTarget::SHARED_LIBRARY;
  1122. }
  1123. else if ( stype == "MODULE" )
  1124. {
  1125. itype = cmTarget::MODULE_LIBRARY;
  1126. }
  1127. else if ( stype == "DIRECTORY" )
  1128. {
  1129. itype = cmTarget::INSTALL_DIRECTORY;
  1130. }
  1131. }
  1132. //----------------------------------------------------------------------------
  1133. bool cmFileCommand::HandleInstallDestination(cmFileInstaller& installer,
  1134. std::string& destination)
  1135. {
  1136. if ( destination.size() < 2 )
  1137. {
  1138. this->SetError("called with inapropriate arguments. "
  1139. "No DESTINATION provided or .");
  1140. return false;
  1141. }
  1142. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  1143. if ( destdir && *destdir )
  1144. {
  1145. std::string sdestdir = destdir;
  1146. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  1147. char ch1 = destination[0];
  1148. char ch2 = destination[1];
  1149. char ch3 = 0;
  1150. if ( destination.size() > 2 )
  1151. {
  1152. ch3 = destination[2];
  1153. }
  1154. int skip = 0;
  1155. if ( ch1 != '/' )
  1156. {
  1157. int relative = 0;
  1158. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  1159. ch2 == ':' )
  1160. {
  1161. // Assume windows
  1162. // let's do some destdir magic:
  1163. skip = 2;
  1164. if ( ch3 != '/' )
  1165. {
  1166. relative = 1;
  1167. }
  1168. }
  1169. else
  1170. {
  1171. relative = 1;
  1172. }
  1173. if ( relative )
  1174. {
  1175. // This is relative path on unix or windows. Since we are doing
  1176. // destdir, this case does not make sense.
  1177. this->SetError("called with relative DESTINATION. This "
  1178. "does not make sense when using DESTDIR. Specify "
  1179. "absolute path or remove DESTDIR environment variable.");
  1180. return false;
  1181. }
  1182. }
  1183. else
  1184. {
  1185. if ( ch2 == '/' )
  1186. {
  1187. // looks like a network path.
  1188. this->SetError("called with network path DESTINATION. This "
  1189. "does not make sense when using DESTDIR. Specify local "
  1190. "absolute path or remove DESTDIR environment variable.");
  1191. return false;
  1192. }
  1193. }
  1194. destination = sdestdir + (destination.c_str() + skip);
  1195. installer.DestDirLength = int(sdestdir.size());
  1196. }
  1197. if ( !cmSystemTools::FileExists(destination.c_str()) )
  1198. {
  1199. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  1200. {
  1201. std::string errstring = "cannot create directory: " + destination +
  1202. ". Maybe need administrative privileges.";
  1203. this->SetError(errstring.c_str());
  1204. return false;
  1205. }
  1206. }
  1207. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  1208. {
  1209. std::string errstring = "INSTALL destination: " + destination +
  1210. " is not a directory.";
  1211. this->SetError(errstring.c_str());
  1212. return false;
  1213. }
  1214. return true;
  1215. }
  1216. //----------------------------------------------------------------------------
  1217. bool cmFileCommand::HandleInstallCommand(std::vector<std::string> const& args)
  1218. {
  1219. if ( args.size() < 6 )
  1220. {
  1221. this->SetError("called with incorrect number of arguments");
  1222. return false;
  1223. }
  1224. // Construct a file installer object.
  1225. cmFileInstaller installer(this, this->Makefile);
  1226. std::string rename = "";
  1227. std::string destination = "";
  1228. std::vector<std::string> files;
  1229. int itype = cmTarget::INSTALL_FILES;
  1230. std::map<cmStdString, const char*> properties;
  1231. bool optional = false;
  1232. bool result = this->ParseInstallArgs(args, installer, properties,
  1233. itype, rename, destination, files,
  1234. optional);
  1235. if (result == true)
  1236. {
  1237. result = this->DoInstall(installer,
  1238. itype, rename, destination, files, optional);
  1239. }
  1240. return result;
  1241. }
  1242. //----------------------------------------------------------------------------
  1243. bool cmFileCommand::ParseInstallArgs(std::vector<std::string> const& args,
  1244. cmFileInstaller& installer,
  1245. std::map<cmStdString, const char*>& properties,
  1246. int& itype,
  1247. std::string& rename,
  1248. std::string& destination,
  1249. std::vector<std::string>& files,
  1250. bool& optional)
  1251. {
  1252. std::string stype = "FILES";
  1253. bool doing_files = false;
  1254. bool doing_properties = false;
  1255. bool doing_permissions_file = false;
  1256. bool doing_permissions_dir = false;
  1257. bool doing_permissions_match = false;
  1258. bool use_given_permissions_file = false;
  1259. bool use_given_permissions_dir = false;
  1260. bool use_source_permissions = false;
  1261. mode_t permissions_file = 0;
  1262. mode_t permissions_dir = 0;
  1263. cmFileInstaller::MatchRule* current_match_rule = 0;
  1264. std::vector<std::string>::size_type i = 0;
  1265. i++; // Get rid of subcommand
  1266. for ( ; i != args.size(); ++i )
  1267. {
  1268. const std::string* cstr = &args[i];
  1269. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  1270. {
  1271. if(current_match_rule)
  1272. {
  1273. cmOStringStream e;
  1274. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1275. this->SetError(e.str().c_str());
  1276. return false;
  1277. }
  1278. i++;
  1279. destination = args[i];
  1280. doing_files = false;
  1281. doing_properties = false;
  1282. doing_permissions_file = false;
  1283. doing_permissions_dir = false;
  1284. }
  1285. else if ( *cstr == "TYPE" && i < args.size()-1 )
  1286. {
  1287. if(current_match_rule)
  1288. {
  1289. cmOStringStream e;
  1290. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1291. this->SetError(e.str().c_str());
  1292. return false;
  1293. }
  1294. i++;
  1295. stype = args[i];
  1296. if ( args[i+1] == "OPTIONAL" )
  1297. {
  1298. i++;
  1299. optional = true;
  1300. }
  1301. doing_properties = false;
  1302. doing_files = false;
  1303. doing_permissions_file = false;
  1304. doing_permissions_dir = false;
  1305. }
  1306. else if ( *cstr == "RENAME" && i < args.size()-1 )
  1307. {
  1308. if(current_match_rule)
  1309. {
  1310. cmOStringStream e;
  1311. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1312. this->SetError(e.str().c_str());
  1313. return false;
  1314. }
  1315. i++;
  1316. rename = args[i];
  1317. doing_properties = false;
  1318. doing_files = false;
  1319. doing_permissions_file = false;
  1320. doing_permissions_dir = false;
  1321. }
  1322. else if ( *cstr == "REGEX" && i < args.size()-1 )
  1323. {
  1324. i++;
  1325. installer.MatchRules.push_back(cmFileInstaller::MatchRule(args[i]));
  1326. current_match_rule = &*(installer.MatchRules.end()-1);
  1327. if(!current_match_rule->Regex.is_valid())
  1328. {
  1329. cmOStringStream e;
  1330. e << "INSTALL could not compile REGEX \"" << args[i] << "\".";
  1331. this->SetError(e.str().c_str());
  1332. return false;
  1333. }
  1334. doing_properties = false;
  1335. doing_files = false;
  1336. doing_permissions_file = false;
  1337. doing_permissions_dir = false;
  1338. }
  1339. else if ( *cstr == "EXCLUDE" )
  1340. {
  1341. // Add this property to the current match rule.
  1342. if(!current_match_rule)
  1343. {
  1344. cmOStringStream e;
  1345. e << "INSTALL does not allow \""
  1346. << *cstr << "\" before a REGEX is given.";
  1347. this->SetError(e.str().c_str());
  1348. return false;
  1349. }
  1350. current_match_rule->Properties.Exclude = true;
  1351. doing_permissions_match = true;
  1352. }
  1353. else if ( *cstr == "PROPERTIES" )
  1354. {
  1355. if(current_match_rule)
  1356. {
  1357. cmOStringStream e;
  1358. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1359. this->SetError(e.str().c_str());
  1360. return false;
  1361. }
  1362. doing_properties = true;
  1363. doing_files = false;
  1364. doing_permissions_file = false;
  1365. doing_permissions_dir = false;
  1366. }
  1367. else if ( *cstr == "PERMISSIONS" )
  1368. {
  1369. if(current_match_rule)
  1370. {
  1371. doing_permissions_match = true;
  1372. doing_permissions_file = false;
  1373. }
  1374. else
  1375. {
  1376. doing_permissions_match = false;
  1377. doing_permissions_file = true;
  1378. use_given_permissions_file = true;
  1379. }
  1380. doing_properties = false;
  1381. doing_files = false;
  1382. doing_permissions_dir = false;
  1383. }
  1384. else if ( *cstr == "DIR_PERMISSIONS" )
  1385. {
  1386. if(current_match_rule)
  1387. {
  1388. cmOStringStream e;
  1389. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1390. this->SetError(e.str().c_str());
  1391. return false;
  1392. }
  1393. use_given_permissions_dir = true;
  1394. doing_properties = false;
  1395. doing_files = false;
  1396. doing_permissions_file = false;
  1397. doing_permissions_dir = true;
  1398. }
  1399. else if ( *cstr == "USE_SOURCE_PERMISSIONS" )
  1400. {
  1401. if(current_match_rule)
  1402. {
  1403. cmOStringStream e;
  1404. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1405. this->SetError(e.str().c_str());
  1406. return false;
  1407. }
  1408. doing_properties = false;
  1409. doing_files = false;
  1410. doing_permissions_file = false;
  1411. doing_permissions_dir = false;
  1412. use_source_permissions = true;
  1413. }
  1414. else if ( *cstr == "FILES_MATCHING" )
  1415. {
  1416. if(current_match_rule)
  1417. {
  1418. cmOStringStream e;
  1419. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1420. this->SetError(e.str().c_str());
  1421. return false;
  1422. }
  1423. doing_properties = false;
  1424. doing_files = false;
  1425. doing_permissions_file = false;
  1426. doing_permissions_dir = false;
  1427. installer.MatchlessFiles = false;
  1428. }
  1429. else if ( *cstr == "COMPONENTS" )
  1430. {
  1431. cmOStringStream e;
  1432. e << "INSTALL called with old-style COMPONENTS argument. "
  1433. << "This script was generated with an older version of CMake. "
  1434. << "Re-run this cmake version on your build tree.";
  1435. this->SetError(e.str().c_str());
  1436. return false;
  1437. }
  1438. else if ( *cstr == "CONFIGURATIONS" )
  1439. {
  1440. cmOStringStream e;
  1441. e << "INSTALL called with old-style CONFIGURATIONS argument. "
  1442. << "This script was generated with an older version of CMake. "
  1443. << "Re-run this cmake version on your build tree.";
  1444. this->SetError(e.str().c_str());
  1445. return false;
  1446. }
  1447. else if ( *cstr == "FILES" && !doing_files)
  1448. {
  1449. if(current_match_rule)
  1450. {
  1451. cmOStringStream e;
  1452. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  1453. this->SetError(e.str().c_str());
  1454. return false;
  1455. }
  1456. doing_files = true;
  1457. doing_properties = false;
  1458. doing_permissions_file = false;
  1459. doing_permissions_dir = false;
  1460. }
  1461. else if ( doing_properties && i < args.size()-1 )
  1462. {
  1463. properties[args[i]] = args[i+1].c_str();
  1464. i++;
  1465. }
  1466. else if ( doing_files )
  1467. {
  1468. files.push_back(*cstr);
  1469. }
  1470. else if(doing_permissions_file)
  1471. {
  1472. if(!installer.CheckPermissions(args[i], permissions_file))
  1473. {
  1474. return false;
  1475. }
  1476. }
  1477. else if(doing_permissions_dir)
  1478. {
  1479. if(!installer.CheckPermissions(args[i], permissions_dir))
  1480. {
  1481. return false;
  1482. }
  1483. }
  1484. else if(doing_permissions_match)
  1485. {
  1486. if(!installer.CheckPermissions(
  1487. args[i], current_match_rule->Properties.Permissions))
  1488. {
  1489. return false;
  1490. }
  1491. }
  1492. else
  1493. {
  1494. this->SetError("called with inappropriate arguments");
  1495. return false;
  1496. }
  1497. }
  1498. // now check and postprocess what has been parsed
  1499. if ( files.size() == 0 )
  1500. {
  1501. // nothing to do, no files were listed.
  1502. // if this is handled as error, INSTALL_FILES() creates an invalid
  1503. // cmake_install.cmake script with no FILES() arguments if no files were
  1504. // given to INSTALL_FILES(). This was accepted with CMake 2.4.x.
  1505. return true;
  1506. }
  1507. // Check rename form.
  1508. if(!rename.empty())
  1509. {
  1510. if(itype != cmTarget::INSTALL_FILES &&
  1511. itype != cmTarget::INSTALL_PROGRAMS)
  1512. {
  1513. this->SetError("INSTALL option RENAME may be used only with "
  1514. "FILES or PROGRAMS.");
  1515. return false;
  1516. }
  1517. if(files.size() > 1)
  1518. {
  1519. this->SetError("INSTALL option RENAME may be used only with "
  1520. "one file.");
  1521. return false;
  1522. }
  1523. }
  1524. if (this->HandleInstallDestination(installer, destination) == false)
  1525. {
  1526. return false;
  1527. }
  1528. if(properties.find("VERSION") != properties.end())
  1529. {
  1530. cmOStringStream e;
  1531. e << "INSTALL called with old-style VERSION property. "
  1532. << "This script was generated with an older version of CMake. "
  1533. << "Re-run this cmake version on your build tree.";
  1534. this->SetError(e.str().c_str());
  1535. return false;
  1536. }
  1537. if(properties.find("SOVERSION") != properties.end())
  1538. {
  1539. cmOStringStream e;
  1540. e << "INSTALL called with old-style SOVERSION property. "
  1541. << "This script was generated with an older version of CMake. "
  1542. << "Re-run this cmake version on your build tree.";
  1543. this->SetError(e.str().c_str());
  1544. return false;
  1545. }
  1546. this->GetTargetTypeFromString(stype, itype);
  1547. this->HandleInstallPermissions(installer,
  1548. permissions_file,
  1549. permissions_dir,
  1550. itype,
  1551. use_given_permissions_file,
  1552. use_given_permissions_dir,
  1553. use_source_permissions);
  1554. return true;
  1555. }
  1556. //----------------------------------------------------------------------------
  1557. bool cmFileCommand::DoInstall( cmFileInstaller& installer,
  1558. const int itype,
  1559. const std::string& rename,
  1560. const std::string& destination,
  1561. const std::vector<std::string>& files,
  1562. const bool optional)
  1563. {
  1564. typedef std::set<cmStdString>::const_iterator iter_type;
  1565. // Check whether files should be copied always or only if they have
  1566. // changed.
  1567. bool copy_always =
  1568. cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_INSTALL_ALWAYS"));
  1569. // Handle each file listed.
  1570. for (std::vector<std::string>::size_type i = 0; i < files.size(); i ++ )
  1571. {
  1572. // Split the input file into its directory and name components.
  1573. std::vector<std::string> fromPathComponents;
  1574. cmSystemTools::SplitPath(files[i].c_str(), fromPathComponents);
  1575. std::string fromName = *(fromPathComponents.end()-1);
  1576. std::string fromDir = cmSystemTools::JoinPath(fromPathComponents.begin(),
  1577. fromPathComponents.end()-1);
  1578. // Compute the full path to the destination file.
  1579. std::string toFile = destination;
  1580. std::string const& toName = rename.empty()? fromName : rename;
  1581. if(!toName.empty())
  1582. {
  1583. toFile += "/";
  1584. toFile += toName;
  1585. }
  1586. // Construct the full path to the source file. The file name may
  1587. // have been changed above.
  1588. std::string fromFile = fromDir;
  1589. if(!fromName.empty())
  1590. {
  1591. fromFile += "/";
  1592. fromFile += fromName;
  1593. }
  1594. std::string message;
  1595. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  1596. {
  1597. if(itype == cmTarget::INSTALL_DIRECTORY &&
  1598. (fromFile.empty() ||
  1599. cmSystemTools::FileIsDirectory(fromFile.c_str())))
  1600. {
  1601. // Try installing this directory.
  1602. if(!installer.InstallDirectory(fromFile.c_str(), toFile.c_str(),
  1603. copy_always))
  1604. {
  1605. return false;
  1606. }
  1607. }
  1608. else if(cmSystemTools::FileExists(fromFile.c_str()))
  1609. {
  1610. // Install this file.
  1611. if(!installer.InstallFile(fromFile.c_str(), toFile.c_str(),
  1612. copy_always))
  1613. {
  1614. return false;
  1615. }
  1616. }
  1617. else if(!optional)
  1618. {
  1619. // The input file does not exist and installation is not optional.
  1620. cmOStringStream e;
  1621. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  1622. this->SetError(e.str().c_str());
  1623. return false;
  1624. }
  1625. }
  1626. }
  1627. return true;
  1628. }
  1629. //----------------------------------------------------------------------------
  1630. bool cmFileCommand::HandleRelativePathCommand(
  1631. std::vector<std::string> const& args)
  1632. {
  1633. if(args.size() != 4 )
  1634. {
  1635. this->SetError("called with incorrect number of arguments");
  1636. return false;
  1637. }
  1638. const std::string& outVar = args[1];
  1639. const std::string& directoryName = args[2];
  1640. const std::string& fileName = args[3];
  1641. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  1642. {
  1643. std::string errstring =
  1644. "RelativePath must be passed a full path to the directory: "
  1645. + directoryName;
  1646. this->SetError(errstring.c_str());
  1647. return false;
  1648. }
  1649. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  1650. {
  1651. std::string errstring =
  1652. "RelativePath must be passed a full path to the file: "
  1653. + fileName;
  1654. this->SetError(errstring.c_str());
  1655. return false;
  1656. }
  1657. std::string res = cmSystemTools::RelativePath(directoryName.c_str(),
  1658. fileName.c_str());
  1659. this->Makefile->AddDefinition(outVar.c_str(),
  1660. res.c_str());
  1661. return true;
  1662. }
  1663. //----------------------------------------------------------------------------
  1664. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  1665. bool recurse)
  1666. {
  1667. std::string message;
  1668. std::vector<std::string>::const_iterator i = args.begin();
  1669. i++; // Get rid of subcommand
  1670. for(;i != args.end(); ++i)
  1671. {
  1672. if(cmSystemTools::FileIsDirectory(i->c_str()) && recurse)
  1673. {
  1674. cmSystemTools::RemoveADirectory(i->c_str());
  1675. }
  1676. else
  1677. {
  1678. cmSystemTools::RemoveFile(i->c_str());
  1679. }
  1680. }
  1681. return true;
  1682. }
  1683. //----------------------------------------------------------------------------
  1684. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  1685. const& args,
  1686. bool nativePath)
  1687. {
  1688. std::vector<std::string>::const_iterator i = args.begin();
  1689. if(args.size() != 3)
  1690. {
  1691. this->SetError("FILE(SYSTEM_PATH ENV result) must be called with "
  1692. "only three arguments.");
  1693. return false;
  1694. }
  1695. i++; // Get rid of subcommand
  1696. #if defined(_WIN32) && !defined(__CYGWIN__)
  1697. char pathSep = ';';
  1698. #else
  1699. char pathSep = ':';
  1700. #endif
  1701. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  1702. pathSep);
  1703. i++;
  1704. const char* var = i->c_str();
  1705. std::string value;
  1706. for(std::vector<cmsys::String>::iterator j = path.begin();
  1707. j != path.end(); ++j)
  1708. {
  1709. if(j != path.begin())
  1710. {
  1711. value += ";";
  1712. }
  1713. if(!nativePath)
  1714. {
  1715. cmSystemTools::ConvertToUnixSlashes(*j);
  1716. }
  1717. else
  1718. {
  1719. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  1720. // remove double quotes in the path
  1721. cmsys::String& s = *j;
  1722. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  1723. {
  1724. s = s.substr(1,s.size()-2);
  1725. }
  1726. }
  1727. value += *j;
  1728. }
  1729. this->Makefile->AddDefinition(var, value.c_str());
  1730. return true;
  1731. }