cmFileCommand.cxx 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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 <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <cmsys/Directory.hxx>
  17. #include <cmsys/Glob.hxx>
  18. #include <cmsys/RegularExpression.hxx>
  19. // Table of permissions flags.
  20. #if defined(_WIN32) && !defined(__CYGWIN__)
  21. static mode_t mode_owner_read = S_IREAD;
  22. static mode_t mode_owner_write = S_IWRITE;
  23. static mode_t mode_owner_execute = S_IEXEC;
  24. static mode_t mode_group_read = 0;
  25. static mode_t mode_group_write = 0;
  26. static mode_t mode_group_execute = 0;
  27. static mode_t mode_world_read = 0;
  28. static mode_t mode_world_write = 0;
  29. static mode_t mode_world_execute = 0;
  30. static mode_t mode_setuid = 0;
  31. static mode_t mode_setgid = 0;
  32. #else
  33. static mode_t mode_owner_read = S_IRUSR;
  34. static mode_t mode_owner_write = S_IWUSR;
  35. static mode_t mode_owner_execute = S_IXUSR;
  36. static mode_t mode_group_read = S_IRGRP;
  37. static mode_t mode_group_write = S_IWGRP;
  38. static mode_t mode_group_execute = S_IXGRP;
  39. static mode_t mode_world_read = S_IROTH;
  40. static mode_t mode_world_write = S_IWOTH;
  41. static mode_t mode_world_execute = S_IXOTH;
  42. static mode_t mode_setuid = S_ISUID;
  43. static mode_t mode_setgid = S_ISGID;
  44. #endif
  45. // cmLibraryCommand
  46. bool cmFileCommand::InitialPass(std::vector<std::string> const& args)
  47. {
  48. if(args.size() < 2 )
  49. {
  50. this->SetError("must be called with at least two arguments.");
  51. return false;
  52. }
  53. std::string subCommand = args[0];
  54. if ( subCommand == "WRITE" )
  55. {
  56. return this->HandleWriteCommand(args, false);
  57. }
  58. else if ( subCommand == "APPEND" )
  59. {
  60. return this->HandleWriteCommand(args, true);
  61. }
  62. else if ( subCommand == "READ" )
  63. {
  64. return this->HandleReadCommand(args);
  65. }
  66. else if ( subCommand == "GLOB" )
  67. {
  68. return this->HandleGlobCommand(args, false);
  69. }
  70. else if ( subCommand == "GLOB_RECURSE" )
  71. {
  72. return this->HandleGlobCommand(args, true);
  73. }
  74. else if ( subCommand == "MAKE_DIRECTORY" )
  75. {
  76. return this->HandleMakeDirectoryCommand(args);
  77. }
  78. else if ( subCommand == "REMOVE" )
  79. {
  80. return this->HandleRemove(args, false);
  81. }
  82. else if ( subCommand == "REMOVE_RECURSE" )
  83. {
  84. return this->HandleRemove(args, true);
  85. }
  86. else if ( subCommand == "INSTALL" )
  87. {
  88. return this->HandleInstallCommand(args);
  89. }
  90. else if ( subCommand == "RELATIVE_PATH" )
  91. {
  92. return this->HandleRelativePathCommand(args);
  93. }
  94. else if ( subCommand == "TO_CMAKE_PATH" )
  95. {
  96. return this->HandleCMakePathCommand(args, false);
  97. }
  98. else if ( subCommand == "TO_NATIVE_PATH" )
  99. {
  100. return this->HandleCMakePathCommand(args, true);
  101. }
  102. std::string e = "does not recognize sub-command "+subCommand;
  103. this->SetError(e.c_str());
  104. return false;
  105. }
  106. //----------------------------------------------------------------------------
  107. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  108. bool append)
  109. {
  110. std::string message;
  111. std::vector<std::string>::const_iterator i = args.begin();
  112. i++; // Get rid of subcommand
  113. std::string fileName = *i;
  114. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  115. {
  116. fileName = this->Makefile->GetCurrentDirectory();
  117. fileName += "/" + *i;
  118. }
  119. i++;
  120. for(;i != args.end(); ++i)
  121. {
  122. message += *i;
  123. }
  124. if ( !this->Makefile->CanIWriteThisFile(fileName.c_str()) )
  125. {
  126. std::string e
  127. = "attempted to write a file: " + fileName +
  128. " into a source directory.";
  129. this->SetError(e.c_str());
  130. cmSystemTools::SetFatalErrorOccured();
  131. return false;
  132. }
  133. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  134. cmSystemTools::MakeDirectory(dir.c_str());
  135. mode_t mode =
  136. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  137. S_IREAD | S_IWRITE
  138. #elif defined( __BORLANDC__ )
  139. S_IRUSR | S_IWUSR
  140. #else
  141. S_IRUSR | S_IWUSR |
  142. S_IRGRP |
  143. S_IROTH
  144. #endif
  145. ;
  146. // Set permissions to writable
  147. if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
  148. {
  149. cmSystemTools::SetPermissions(fileName.c_str(),
  150. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  151. S_IREAD | S_IWRITE
  152. #else
  153. S_IRUSR | S_IWUSR
  154. #endif
  155. );
  156. }
  157. // If GetPermissions fails, pretend like it is ok. File open will fail if
  158. // the file is not writable
  159. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  160. if ( !file )
  161. {
  162. std::string error = "Internal CMake error when trying to open file: ";
  163. error += fileName.c_str();
  164. error += " for writing.";
  165. this->SetError(error.c_str());
  166. return false;
  167. }
  168. file << message;
  169. file.close();
  170. cmSystemTools::SetPermissions(fileName.c_str(), mode);
  171. this->Makefile->AddWrittenFile(fileName.c_str());
  172. return true;
  173. }
  174. //----------------------------------------------------------------------------
  175. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  176. {
  177. if ( args.size() != 3 )
  178. {
  179. this->SetError("READ must be called with two additional arguments");
  180. return false;
  181. }
  182. std::string fileName = args[1];
  183. if ( !cmsys::SystemTools::FileIsFullPath(args[1].c_str()) )
  184. {
  185. fileName = this->Makefile->GetCurrentDirectory();
  186. fileName += "/" + args[1];
  187. }
  188. std::string variable = args[2];
  189. std::ifstream file(fileName.c_str(), std::ios::in);
  190. if ( !file )
  191. {
  192. std::string error = "Internal CMake error when trying to open file: ";
  193. error += fileName.c_str();
  194. error += " for reading.";
  195. this->SetError(error.c_str());
  196. return false;
  197. }
  198. std::string output;
  199. std::string line;
  200. bool has_newline = false;
  201. while ( cmSystemTools::GetLineFromStream(file, line, &has_newline) )
  202. {
  203. output += line;
  204. if ( has_newline )
  205. {
  206. output += "\n";
  207. }
  208. }
  209. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  210. return true;
  211. }
  212. //----------------------------------------------------------------------------
  213. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  214. bool recurse)
  215. {
  216. if ( args.size() < 2 )
  217. {
  218. this->SetError("GLOB requires at least a variable name");
  219. return false;
  220. }
  221. std::vector<std::string>::const_iterator i = args.begin();
  222. i++; // Get rid of subcommand
  223. std::string variable = *i;
  224. i++;
  225. cmsys::Glob g;
  226. g.SetRecurse(recurse);
  227. std::string output = "";
  228. bool first = true;
  229. for ( ; i != args.end(); ++i )
  230. {
  231. if ( *i == "RELATIVE" )
  232. {
  233. ++i; // skip RELATIVE
  234. if ( i == args.end() )
  235. {
  236. this->SetError("GLOB requires a directory after the RELATIVE tag");
  237. return false;
  238. }
  239. g.SetRelative(i->c_str());
  240. ++i;
  241. if(i == args.end())
  242. {
  243. this->SetError("GLOB requires a glob expression after the directory");
  244. return false;
  245. }
  246. }
  247. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  248. {
  249. std::string expr = this->Makefile->GetCurrentDirectory();
  250. // Handle script mode
  251. if ( expr.size() > 0 )
  252. {
  253. expr += "/" + *i;
  254. g.FindFiles(expr);
  255. }
  256. else
  257. {
  258. g.FindFiles(*i);
  259. }
  260. }
  261. else
  262. {
  263. g.FindFiles(*i);
  264. }
  265. std::vector<std::string>::size_type cc;
  266. std::vector<std::string>& files = g.GetFiles();
  267. for ( cc = 0; cc < files.size(); cc ++ )
  268. {
  269. if ( !first )
  270. {
  271. output += ";";
  272. }
  273. output += files[cc];
  274. first = false;
  275. }
  276. }
  277. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  278. return true;
  279. }
  280. //----------------------------------------------------------------------------
  281. bool cmFileCommand::HandleMakeDirectoryCommand(
  282. std::vector<std::string> const& args)
  283. {
  284. if(args.size() < 2 )
  285. {
  286. this->SetError("called with incorrect number of arguments");
  287. return false;
  288. }
  289. std::vector<std::string>::const_iterator i = args.begin();
  290. i++; // Get rid of subcommand
  291. std::string expr;
  292. for ( ; i != args.end(); ++i )
  293. {
  294. const std::string* cdir = &(*i);
  295. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  296. {
  297. expr = this->Makefile->GetCurrentDirectory();
  298. expr += "/" + *i;
  299. cdir = &expr;
  300. }
  301. if ( !this->Makefile->CanIWriteThisFile(cdir->c_str()) )
  302. {
  303. std::string e = "attempted to create a directory: " + *cdir
  304. + " into a source directory.";
  305. this->SetError(e.c_str());
  306. cmSystemTools::SetFatalErrorOccured();
  307. return false;
  308. }
  309. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  310. {
  311. std::string error = "problem creating directory: " + *cdir;
  312. this->SetError(error.c_str());
  313. return false;
  314. }
  315. }
  316. return true;
  317. }
  318. //----------------------------------------------------------------------------
  319. // File installation helper class.
  320. struct cmFileInstaller
  321. {
  322. // Methods to actually install files.
  323. bool InstallFile(const char* fromFile, const char* toFile, bool always);
  324. bool InstallDirectory(const char* source, const char* destination,
  325. bool always);
  326. // All instances need the file command and makefile using them.
  327. cmFileInstaller(cmFileCommand* fc, cmMakefile* mf):
  328. FileCommand(fc), Makefile(mf), DestDirLength(0)
  329. {
  330. // Get the current manifest.
  331. this->Manifest =
  332. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  333. }
  334. ~cmFileInstaller()
  335. {
  336. // Save the updated install manifest.
  337. this->Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  338. this->Manifest.c_str());
  339. }
  340. private:
  341. cmFileCommand* FileCommand;
  342. cmMakefile* Makefile;
  343. public:
  344. // The length of the destdir setting.
  345. int DestDirLength;
  346. // The current file manifest (semicolon separated list).
  347. std::string Manifest;
  348. // Permissions for files and directories installed by this object.
  349. mode_t FilePermissions;
  350. mode_t DirPermissions;
  351. // Properties set by pattern and regex match rules.
  352. struct MatchProperties
  353. {
  354. bool Exclude;
  355. mode_t Permissions;
  356. MatchProperties(): Exclude(false), Permissions(0) {}
  357. };
  358. struct MatchRule
  359. {
  360. cmsys::RegularExpression Regex;
  361. MatchProperties Properties;
  362. MatchRule(std::string const& regex): Regex(regex.c_str()) {}
  363. };
  364. std::vector<MatchRule> MatchRules;
  365. // Get the properties from rules matching this input file.
  366. MatchProperties CollectMatchProperties(const char* file)
  367. {
  368. // Match rules are case-insensitive on some platforms.
  369. #if defined(_WIN32) || defined(__APPLE__) || defined(__CYGWIN__)
  370. std::string lower = cmSystemTools::LowerCase(file);
  371. file = lower.c_str();
  372. #endif
  373. // Collect properties from all matching rules.
  374. MatchProperties result;
  375. for(std::vector<MatchRule>::iterator mr = this->MatchRules.begin();
  376. mr != this->MatchRules.end(); ++mr)
  377. {
  378. if(mr->Regex.find(file))
  379. {
  380. result.Exclude |= mr->Properties.Exclude;
  381. result.Permissions |= mr->Properties.Permissions;
  382. }
  383. }
  384. return result;
  385. }
  386. // Append a file to the installation manifest.
  387. void ManifestAppend(std::string const& file)
  388. {
  389. this->Manifest += ";";
  390. this->Manifest += file.substr(this->DestDirLength);
  391. }
  392. // Translate an argument to a permissions bit.
  393. bool CheckPermissions(std::string const& arg, mode_t& permissions)
  394. {
  395. if(arg == "OWNER_READ") { permissions |= mode_owner_read; }
  396. else if(arg == "OWNER_WRITE") { permissions |= mode_owner_write; }
  397. else if(arg == "OWNER_EXECUTE") { permissions |= mode_owner_execute; }
  398. else if(arg == "GROUP_READ") { permissions |= mode_group_read; }
  399. else if(arg == "GROUP_WRITE") { permissions |= mode_group_write; }
  400. else if(arg == "GROUP_EXECUTE") { permissions |= mode_group_execute; }
  401. else if(arg == "WORLD_READ") { permissions |= mode_world_read; }
  402. else if(arg == "WORLD_WRITE") { permissions |= mode_world_write; }
  403. else if(arg == "WORLD_EXECUTE") { permissions |= mode_world_execute; }
  404. else if(arg == "SETUID") { permissions |= mode_setuid; }
  405. else if(arg == "SETGID") { permissions |= mode_setgid; }
  406. else
  407. {
  408. cmOStringStream e;
  409. e << "INSTALL given invalid permission \"" << arg << "\".";
  410. this->FileCommand->SetError(e.str().c_str());
  411. return false;
  412. }
  413. return true;
  414. }
  415. };
  416. //----------------------------------------------------------------------------
  417. bool cmFileInstaller::InstallFile(const char* fromFile, const char* toFile,
  418. bool always)
  419. {
  420. // Collect any properties matching this file name.
  421. MatchProperties match_properties = this->CollectMatchProperties(fromFile);
  422. // Skip the file if it is excluded.
  423. if(match_properties.Exclude)
  424. {
  425. return true;
  426. }
  427. // Inform the user about this file installation.
  428. std::string message = "Installing ";
  429. message += toFile;
  430. this->Makefile->DisplayStatus(message.c_str(), -1);
  431. // Copy the file.
  432. if(!cmSystemTools::CopyAFile(fromFile, toFile, always))
  433. {
  434. cmOStringStream e;
  435. e << "INSTALL cannot copy file \"" << fromFile
  436. << "\" to \"" << toFile << "\".";
  437. this->FileCommand->SetError(e.str().c_str());
  438. return false;
  439. }
  440. // Add the file to the manifest.
  441. this->ManifestAppend(toFile);
  442. // Set permissions of the destination file.
  443. mode_t permissions = (match_properties.Permissions?
  444. match_properties.Permissions : this->FilePermissions);
  445. if(!permissions)
  446. {
  447. // No permissions were explicitly provided but the user requested
  448. // that the source file permissions be used.
  449. cmSystemTools::GetPermissions(fromFile, permissions);
  450. }
  451. if(permissions && !cmSystemTools::SetPermissions(toFile, permissions))
  452. {
  453. cmOStringStream e;
  454. e << "Problem setting permissions on file \"" << toFile << "\"";
  455. this->FileCommand->SetError(e.str().c_str());
  456. return false;
  457. }
  458. return true;
  459. }
  460. //----------------------------------------------------------------------------
  461. bool cmFileInstaller::InstallDirectory(const char* source,
  462. const char* destination,
  463. bool always)
  464. {
  465. // Collect any properties matching this directory name.
  466. MatchProperties match_properties = this->CollectMatchProperties(source);
  467. // Skip the directory if it is excluded.
  468. if(match_properties.Exclude)
  469. {
  470. return true;
  471. }
  472. // Make sure the destination directory exists.
  473. if(!cmSystemTools::MakeDirectory(destination))
  474. {
  475. return false;
  476. }
  477. // Compute the requested permissions for the destination directory.
  478. mode_t permissions = (match_properties.Permissions?
  479. match_properties.Permissions : this->DirPermissions);
  480. if(!permissions)
  481. {
  482. // No permissions were explicitly provided but the user requested
  483. // that the source directory permissions be used.
  484. cmSystemTools::GetPermissions(source, permissions);
  485. }
  486. // Compute the set of permissions required on this directory to
  487. // recursively install files and subdirectories safely.
  488. mode_t required_permissions =
  489. mode_owner_read | mode_owner_write | mode_owner_execute;
  490. // If the required permissions are specified it is safe to set the
  491. // final permissions now. Otherwise we must add the required
  492. // permissions temporarily during file installation.
  493. mode_t permissions_before = 0;
  494. mode_t permissions_after = 0;
  495. if(permissions & required_permissions)
  496. {
  497. permissions_before = permissions;
  498. }
  499. else
  500. {
  501. permissions_before = permissions | required_permissions;
  502. permissions_after = permissions;
  503. }
  504. // Set the required permissions of the destination directory.
  505. if(permissions_before &&
  506. !cmSystemTools::SetPermissions(destination, permissions_before))
  507. {
  508. cmOStringStream e;
  509. e << "Problem setting permissions on directory \""
  510. << destination << "\"";
  511. this->FileCommand->SetError(e.str().c_str());
  512. return false;
  513. }
  514. // Load the directory contents to traverse it recursively.
  515. cmsys::Directory dir;
  516. dir.Load(source);
  517. unsigned long numFiles = static_cast<unsigned long>(dir.GetNumberOfFiles());
  518. for(unsigned long fileNum = 0; fileNum < numFiles; ++fileNum)
  519. {
  520. if(!(strcmp(dir.GetFile(fileNum), ".") == 0 ||
  521. strcmp(dir.GetFile(fileNum), "..") == 0))
  522. {
  523. kwsys_stl::string fromPath = source;
  524. fromPath += "/";
  525. fromPath += dir.GetFile(fileNum);
  526. if(cmSystemTools::FileIsDirectory(fromPath.c_str()))
  527. {
  528. kwsys_stl::string toDir = destination;
  529. toDir += "/";
  530. toDir += dir.GetFile(fileNum);
  531. if(!this->InstallDirectory(fromPath.c_str(), toDir.c_str(), always))
  532. {
  533. return false;
  534. }
  535. }
  536. else
  537. {
  538. // Install this file.
  539. std::string toFile = destination;
  540. toFile += "/";
  541. toFile += dir.GetFile(fileNum);
  542. if(!this->InstallFile(fromPath.c_str(), toFile.c_str(), always))
  543. {
  544. return false;
  545. }
  546. }
  547. }
  548. }
  549. // Set the requested permissions of the destination directory.
  550. if(permissions_after &&
  551. !cmSystemTools::SetPermissions(destination, permissions_after))
  552. {
  553. cmOStringStream e;
  554. e << "Problem setting permissions on directory \"" << destination << "\"";
  555. this->FileCommand->SetError(e.str().c_str());
  556. return false;
  557. }
  558. return true;
  559. }
  560. //----------------------------------------------------------------------------
  561. bool cmFileCommand::HandleInstallCommand(
  562. std::vector<std::string> const& args)
  563. {
  564. if ( args.size() < 6 )
  565. {
  566. this->SetError("called with incorrect number of arguments");
  567. return false;
  568. }
  569. // Construct a file installer object.
  570. cmFileInstaller installer(this, this->Makefile);
  571. std::string rename = "";
  572. std::string destination = "";
  573. std::string stype = "FILES";
  574. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  575. std::set<cmStdString> components;
  576. std::set<cmStdString> configurations;
  577. std::vector<std::string> files;
  578. int itype = cmTarget::INSTALL_FILES;
  579. std::vector<std::string>::size_type i = 0;
  580. i++; // Get rid of subcommand
  581. std::map<cmStdString, const char*> properties;
  582. bool doing_files = false;
  583. bool doing_properties = false;
  584. bool doing_permissions_file = false;
  585. bool doing_permissions_dir = false;
  586. bool doing_permissions_match = false;
  587. bool doing_components = false;
  588. bool doing_configurations = false;
  589. bool use_given_permissions_file = false;
  590. bool use_given_permissions_dir = false;
  591. bool use_source_permissions = false;
  592. mode_t permissions_file = 0;
  593. mode_t permissions_dir = 0;
  594. bool optional = false;
  595. cmFileInstaller::MatchRule* current_match_rule = 0;
  596. for ( ; i != args.size(); ++i )
  597. {
  598. const std::string* cstr = &args[i];
  599. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  600. {
  601. if(current_match_rule)
  602. {
  603. cmOStringStream e;
  604. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  605. this->SetError(e.str().c_str());
  606. return false;
  607. }
  608. i++;
  609. destination = args[i];
  610. doing_files = false;
  611. doing_properties = false;
  612. doing_permissions_file = false;
  613. doing_permissions_dir = false;
  614. doing_components = false;
  615. doing_configurations = false;
  616. }
  617. else if ( *cstr == "TYPE" && i < args.size()-1 )
  618. {
  619. if(current_match_rule)
  620. {
  621. cmOStringStream e;
  622. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  623. this->SetError(e.str().c_str());
  624. return false;
  625. }
  626. i++;
  627. stype = args[i];
  628. if ( args[i+1] == "OPTIONAL" )
  629. {
  630. i++;
  631. optional = true;
  632. }
  633. doing_properties = false;
  634. doing_files = false;
  635. doing_permissions_file = false;
  636. doing_permissions_dir = false;
  637. doing_components = false;
  638. doing_configurations = false;
  639. }
  640. else if ( *cstr == "RENAME" && i < args.size()-1 )
  641. {
  642. if(current_match_rule)
  643. {
  644. cmOStringStream e;
  645. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  646. this->SetError(e.str().c_str());
  647. return false;
  648. }
  649. i++;
  650. rename = args[i];
  651. doing_properties = false;
  652. doing_files = false;
  653. doing_permissions_file = false;
  654. doing_permissions_dir = false;
  655. doing_components = false;
  656. doing_configurations = false;
  657. }
  658. else if ( *cstr == "REGEX" && i < args.size()-1 )
  659. {
  660. i++;
  661. installer.MatchRules.push_back(cmFileInstaller::MatchRule(args[i]));
  662. current_match_rule = &*(installer.MatchRules.end()-1);
  663. if(!current_match_rule->Regex.is_valid())
  664. {
  665. cmOStringStream e;
  666. e << "INSTALL could not compile REGEX \"" << args[i] << "\".";
  667. this->SetError(e.str().c_str());
  668. return false;
  669. }
  670. doing_properties = false;
  671. doing_files = false;
  672. doing_permissions_file = false;
  673. doing_permissions_dir = false;
  674. doing_components = false;
  675. doing_configurations = false;
  676. }
  677. else if ( *cstr == "EXCLUDE" )
  678. {
  679. // Add this property to the current match rule.
  680. if(!current_match_rule)
  681. {
  682. cmOStringStream e;
  683. e << "INSTALL does not allow \""
  684. << *cstr << "\" before a REGEX is given.";
  685. this->SetError(e.str().c_str());
  686. return false;
  687. }
  688. current_match_rule->Properties.Exclude = true;
  689. doing_permissions_match = true;
  690. }
  691. else if ( *cstr == "PROPERTIES" )
  692. {
  693. if(current_match_rule)
  694. {
  695. cmOStringStream e;
  696. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  697. this->SetError(e.str().c_str());
  698. return false;
  699. }
  700. doing_properties = true;
  701. doing_files = false;
  702. doing_permissions_file = false;
  703. doing_permissions_dir = false;
  704. doing_components = false;
  705. doing_configurations = false;
  706. }
  707. else if ( *cstr == "PERMISSIONS" )
  708. {
  709. if(current_match_rule)
  710. {
  711. doing_permissions_match = true;
  712. doing_permissions_file = false;
  713. }
  714. else
  715. {
  716. doing_permissions_match = false;
  717. doing_permissions_file = true;
  718. use_given_permissions_file = true;
  719. }
  720. doing_properties = false;
  721. doing_files = false;
  722. doing_permissions_dir = false;
  723. doing_components = false;
  724. doing_configurations = false;
  725. }
  726. else if ( *cstr == "DIR_PERMISSIONS" )
  727. {
  728. if(current_match_rule)
  729. {
  730. cmOStringStream e;
  731. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  732. this->SetError(e.str().c_str());
  733. return false;
  734. }
  735. use_given_permissions_dir = true;
  736. doing_properties = false;
  737. doing_files = false;
  738. doing_permissions_file = false;
  739. doing_permissions_dir = true;
  740. doing_components = false;
  741. doing_configurations = false;
  742. }
  743. else if ( *cstr == "USE_SOURCE_PERMISSIONS" )
  744. {
  745. if(current_match_rule)
  746. {
  747. cmOStringStream e;
  748. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  749. this->SetError(e.str().c_str());
  750. return false;
  751. }
  752. doing_properties = false;
  753. doing_files = false;
  754. doing_permissions_file = false;
  755. doing_permissions_dir = false;
  756. doing_components = false;
  757. doing_configurations = false;
  758. use_source_permissions = true;
  759. }
  760. else if ( *cstr == "COMPONENTS" )
  761. {
  762. if(current_match_rule)
  763. {
  764. cmOStringStream e;
  765. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  766. this->SetError(e.str().c_str());
  767. return false;
  768. }
  769. doing_properties = false;
  770. doing_files = false;
  771. doing_permissions_file = false;
  772. doing_permissions_dir = false;
  773. doing_components = true;
  774. doing_configurations = false;
  775. }
  776. else if ( *cstr == "CONFIGURATIONS" )
  777. {
  778. if(current_match_rule)
  779. {
  780. cmOStringStream e;
  781. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  782. this->SetError(e.str().c_str());
  783. return false;
  784. }
  785. doing_properties = false;
  786. doing_files = false;
  787. doing_permissions_file = false;
  788. doing_permissions_dir = false;
  789. doing_components = false;
  790. doing_configurations = true;
  791. }
  792. else if ( *cstr == "FILES" && !doing_files)
  793. {
  794. if(current_match_rule)
  795. {
  796. cmOStringStream e;
  797. e << "INSTALL does not allow \"" << *cstr << "\" after REGEX.";
  798. this->SetError(e.str().c_str());
  799. return false;
  800. }
  801. doing_files = true;
  802. doing_properties = false;
  803. doing_permissions_file = false;
  804. doing_permissions_dir = false;
  805. doing_components = false;
  806. doing_configurations = false;
  807. }
  808. else if ( doing_properties && i < args.size()-1 )
  809. {
  810. properties[args[i]] = args[i+1].c_str();
  811. i++;
  812. }
  813. else if ( doing_files )
  814. {
  815. files.push_back(*cstr);
  816. }
  817. else if ( doing_components )
  818. {
  819. components.insert(*cstr);
  820. }
  821. else if ( doing_configurations )
  822. {
  823. configurations.insert(cmSystemTools::UpperCase(*cstr));
  824. }
  825. else if(doing_permissions_file)
  826. {
  827. if(!installer.CheckPermissions(args[i], permissions_file))
  828. {
  829. return false;
  830. }
  831. }
  832. else if(doing_permissions_dir)
  833. {
  834. if(!installer.CheckPermissions(args[i], permissions_dir))
  835. {
  836. return false;
  837. }
  838. }
  839. else if(doing_permissions_match)
  840. {
  841. if(!installer.CheckPermissions(
  842. args[i], current_match_rule->Properties.Permissions))
  843. {
  844. return false;
  845. }
  846. }
  847. else
  848. {
  849. this->SetError("called with inappropriate arguments");
  850. return false;
  851. }
  852. }
  853. if ( destination.size() < 2 )
  854. {
  855. this->SetError("called with inapropriate arguments. "
  856. "No DESTINATION provided or .");
  857. return false;
  858. }
  859. // Check for component-specific installation.
  860. const char* cmake_install_component =
  861. this->Makefile->GetDefinition("CMAKE_INSTALL_COMPONENT");
  862. if(cmake_install_component && *cmake_install_component)
  863. {
  864. // This install rule applies only if it is associated with the
  865. // current component.
  866. if(components.find(cmake_install_component) == components.end())
  867. {
  868. return true;
  869. }
  870. }
  871. // Check for configuration-specific installation.
  872. if(!configurations.empty())
  873. {
  874. std::string cmake_install_configuration =
  875. cmSystemTools::UpperCase(
  876. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_CONFIG_NAME"));
  877. if(cmake_install_configuration.empty())
  878. {
  879. // No configuration specified for installation but this install
  880. // rule is configuration-specific. Skip it.
  881. return true;
  882. }
  883. else if(configurations.find(cmake_install_configuration) ==
  884. configurations.end())
  885. {
  886. // This rule is specific to a configuration not being installed.
  887. return true;
  888. }
  889. }
  890. if ( destdir && *destdir )
  891. {
  892. std::string sdestdir = destdir;
  893. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  894. char ch1 = destination[0];
  895. char ch2 = destination[1];
  896. char ch3 = 0;
  897. if ( destination.size() > 2 )
  898. {
  899. ch3 = destination[2];
  900. }
  901. int skip = 0;
  902. if ( ch1 != '/' )
  903. {
  904. int relative = 0;
  905. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  906. ch2 == ':' )
  907. {
  908. // Assume windows
  909. // let's do some destdir magic:
  910. skip = 2;
  911. if ( ch3 != '/' )
  912. {
  913. relative = 1;
  914. }
  915. }
  916. else
  917. {
  918. relative = 1;
  919. }
  920. if ( relative )
  921. {
  922. // This is relative path on unix or windows. Since we are doing
  923. // destdir, this case does not make sense.
  924. this->SetError("called with relative DESTINATION. This "
  925. "does not make sense when using DESTDIR. Specify "
  926. "absolute path or remove DESTDIR environment variable.");
  927. return false;
  928. }
  929. }
  930. else
  931. {
  932. if ( ch2 == '/' )
  933. {
  934. // looks like a network path.
  935. this->SetError("called with network path DESTINATION. This "
  936. "does not make sense when using DESTDIR. Specify local "
  937. "absolute path or remove DESTDIR environment variable.");
  938. return false;
  939. }
  940. }
  941. destination = sdestdir + (destination.c_str() + skip);
  942. installer.DestDirLength = int(sdestdir.size());
  943. }
  944. if ( files.size() == 0 )
  945. {
  946. this->SetError(
  947. "called with inapropriate arguments. No FILES provided.");
  948. return false;
  949. }
  950. if ( stype == "EXECUTABLE" )
  951. {
  952. itype = cmTarget::EXECUTABLE;
  953. }
  954. else if ( stype == "PROGRAM" )
  955. {
  956. itype = cmTarget::INSTALL_PROGRAMS;
  957. }
  958. else if ( stype == "STATIC_LIBRARY" )
  959. {
  960. itype = cmTarget::STATIC_LIBRARY;
  961. }
  962. else if ( stype == "SHARED_LIBRARY" )
  963. {
  964. itype = cmTarget::SHARED_LIBRARY;
  965. }
  966. else if ( stype == "MODULE" )
  967. {
  968. itype = cmTarget::MODULE_LIBRARY;
  969. }
  970. else if ( stype == "DIRECTORY" )
  971. {
  972. itype = cmTarget::INSTALL_DIRECTORY;
  973. }
  974. if ( !cmSystemTools::FileExists(destination.c_str()) )
  975. {
  976. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  977. {
  978. std::string errstring = "cannot create directory: " + destination +
  979. ". Maybe need administrative privileges.";
  980. this->SetError(errstring.c_str());
  981. return false;
  982. }
  983. }
  984. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  985. {
  986. std::string errstring = "INSTALL destination: " + destination +
  987. " is not a directory.";
  988. this->SetError(errstring.c_str());
  989. return false;
  990. }
  991. // Check rename form.
  992. if(!rename.empty())
  993. {
  994. if(itype != cmTarget::INSTALL_FILES &&
  995. itype != cmTarget::INSTALL_PROGRAMS)
  996. {
  997. this->SetError("INSTALL option RENAME may be used only with "
  998. "FILES or PROGRAMS.");
  999. return false;
  1000. }
  1001. if(files.size() > 1)
  1002. {
  1003. this->SetError("INSTALL option RENAME may be used only with one file.");
  1004. return false;
  1005. }
  1006. }
  1007. // If file permissions were not specified set default permissions
  1008. // for this target type.
  1009. if(!use_given_permissions_file && !use_source_permissions)
  1010. {
  1011. switch(itype)
  1012. {
  1013. case cmTarget::SHARED_LIBRARY:
  1014. case cmTarget::MODULE_LIBRARY:
  1015. #if defined(__linux__)
  1016. // Use read/write permissions.
  1017. permissions_file = 0;
  1018. permissions_file |= mode_owner_read;
  1019. permissions_file |= mode_owner_write;
  1020. permissions_file |= mode_group_read;
  1021. permissions_file |= mode_world_read;
  1022. break;
  1023. #endif
  1024. case cmTarget::EXECUTABLE:
  1025. case cmTarget::INSTALL_PROGRAMS:
  1026. // Use read/write/executable permissions.
  1027. permissions_file = 0;
  1028. permissions_file |= mode_owner_read;
  1029. permissions_file |= mode_owner_write;
  1030. permissions_file |= mode_owner_execute;
  1031. permissions_file |= mode_group_read;
  1032. permissions_file |= mode_group_execute;
  1033. permissions_file |= mode_world_read;
  1034. permissions_file |= mode_world_execute;
  1035. break;
  1036. default:
  1037. // Use read/write permissions.
  1038. permissions_file = 0;
  1039. permissions_file |= mode_owner_read;
  1040. permissions_file |= mode_owner_write;
  1041. permissions_file |= mode_group_read;
  1042. permissions_file |= mode_world_read;
  1043. break;
  1044. }
  1045. }
  1046. // If directory permissions were not specified set default permissions.
  1047. if(!use_given_permissions_dir && !use_source_permissions)
  1048. {
  1049. // Use read/write/executable permissions.
  1050. permissions_dir = 0;
  1051. permissions_dir |= mode_owner_read;
  1052. permissions_dir |= mode_owner_write;
  1053. permissions_dir |= mode_owner_execute;
  1054. permissions_dir |= mode_group_read;
  1055. permissions_dir |= mode_group_execute;
  1056. permissions_dir |= mode_world_read;
  1057. permissions_dir |= mode_world_execute;
  1058. }
  1059. // Set the installer permissions.
  1060. installer.FilePermissions = permissions_file;
  1061. installer.DirPermissions = permissions_dir;
  1062. // Check whether files should be copied always or only if they have
  1063. // changed.
  1064. bool copy_always =
  1065. cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_INSTALL_ALWAYS"));
  1066. // Handle each file listed.
  1067. for ( i = 0; i < files.size(); i ++ )
  1068. {
  1069. // Split the input file into its directory and name components.
  1070. std::vector<std::string> fromPathComponents;
  1071. cmSystemTools::SplitPath(files[i].c_str(), fromPathComponents);
  1072. std::string fromName = *(fromPathComponents.end()-1);
  1073. std::string fromDir = cmSystemTools::JoinPath(fromPathComponents.begin(),
  1074. fromPathComponents.end()-1);
  1075. // Compute the full path to the destination file.
  1076. std::string toFile = destination;
  1077. std::string const& toName = rename.empty()? fromName : rename;
  1078. if(!toName.empty())
  1079. {
  1080. toFile += "/";
  1081. toFile += toName;
  1082. }
  1083. // Handle type-specific installation details.
  1084. switch(itype)
  1085. {
  1086. case cmTarget::MODULE_LIBRARY:
  1087. case cmTarget::STATIC_LIBRARY:
  1088. case cmTarget::SHARED_LIBRARY:
  1089. {
  1090. // Handle shared library versioning
  1091. const char* lib_version = 0;
  1092. const char* lib_soversion = 0;
  1093. if ( properties.find("VERSION") != properties.end() )
  1094. {
  1095. lib_version = properties["VERSION"];
  1096. }
  1097. if ( properties.find("SOVERSION") != properties.end() )
  1098. {
  1099. lib_soversion = properties["SOVERSION"];
  1100. }
  1101. if ( !lib_version && lib_soversion )
  1102. {
  1103. lib_version = lib_soversion;
  1104. }
  1105. if ( !lib_soversion && lib_version )
  1106. {
  1107. lib_soversion = lib_version;
  1108. }
  1109. if ( lib_version && lib_soversion )
  1110. {
  1111. std::string libname = toFile;
  1112. std::string soname = toFile;
  1113. std::string soname_nopath = fromName;
  1114. this->ComputeVersionedName(soname, lib_soversion);
  1115. this->ComputeVersionedName(soname_nopath, lib_soversion);
  1116. this->ComputeVersionedName(fromName, lib_version);
  1117. this->ComputeVersionedName(toFile, lib_version);
  1118. cmSystemTools::RemoveFile(soname.c_str());
  1119. cmSystemTools::RemoveFile(libname.c_str());
  1120. if (!cmSystemTools::CreateSymlink(soname_nopath.c_str(),
  1121. libname.c_str()) )
  1122. {
  1123. std::string errstring = "error when creating symlink from: "
  1124. + libname + " to " + soname_nopath;
  1125. this->SetError(errstring.c_str());
  1126. return false;
  1127. }
  1128. installer.ManifestAppend(libname);
  1129. if ( toFile != soname )
  1130. {
  1131. if ( !cmSystemTools::CreateSymlink(fromName.c_str(),
  1132. soname.c_str()) )
  1133. {
  1134. std::string errstring = "error when creating symlink from: "
  1135. + soname + " to " + fromName;
  1136. this->SetError(errstring.c_str());
  1137. return false;
  1138. }
  1139. installer.ManifestAppend(soname);
  1140. }
  1141. }
  1142. }
  1143. break;
  1144. case cmTarget::EXECUTABLE:
  1145. {
  1146. // Handle executable versioning
  1147. const char* exe_version = 0;
  1148. if ( properties.find("VERSION") != properties.end() )
  1149. {
  1150. exe_version = properties["VERSION"];
  1151. }
  1152. if ( exe_version )
  1153. {
  1154. std::string exename = toFile;
  1155. std::string exename_nopath = fromName;
  1156. exename_nopath += "-";
  1157. exename_nopath += exe_version;
  1158. fromName += "-";
  1159. fromName += exe_version;
  1160. toFile += "-";
  1161. toFile += exe_version;
  1162. cmSystemTools::RemoveFile(exename.c_str());
  1163. if (!cmSystemTools::CreateSymlink(exename_nopath.c_str(),
  1164. exename.c_str()) )
  1165. {
  1166. std::string errstring = "error when creating symlink from: "
  1167. + exename + " to " + exename_nopath;
  1168. this->SetError(errstring.c_str());
  1169. return false;
  1170. }
  1171. installer.ManifestAppend(exename);
  1172. }
  1173. }
  1174. break;
  1175. }
  1176. // Construct the full path to the source file. The file name may
  1177. // have been changed above.
  1178. std::string fromFile = fromDir;
  1179. fromFile += "/";
  1180. fromFile += fromName;
  1181. std::string message;
  1182. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  1183. {
  1184. if(itype == cmTarget::INSTALL_DIRECTORY &&
  1185. cmSystemTools::FileIsDirectory(fromFile.c_str()))
  1186. {
  1187. // Try installing this directory.
  1188. if(!installer.InstallDirectory(fromFile.c_str(), toFile.c_str(),
  1189. copy_always))
  1190. {
  1191. return false;
  1192. }
  1193. }
  1194. else if(cmSystemTools::FileExists(fromFile.c_str()))
  1195. {
  1196. // Install this file.
  1197. if(!installer.InstallFile(fromFile.c_str(), toFile.c_str(),
  1198. copy_always))
  1199. {
  1200. return false;
  1201. }
  1202. // Perform post-installation processing on the file depending
  1203. // on its type.
  1204. #if defined(__APPLE_CC__)
  1205. // Static libraries need ranlib on this platform.
  1206. if(itype == cmTarget::STATIC_LIBRARY)
  1207. {
  1208. std::string ranlib = "ranlib ";
  1209. ranlib += cmSystemTools::ConvertToOutputPath(toFile.c_str());
  1210. if(!cmSystemTools::RunSingleCommand(ranlib.c_str()))
  1211. {
  1212. std::string err = "ranlib failed: ";
  1213. err += ranlib;
  1214. this->SetError(err.c_str());
  1215. return false;
  1216. }
  1217. }
  1218. #endif
  1219. }
  1220. else if(!optional)
  1221. {
  1222. // The input file does not exist and installation is not optional.
  1223. cmOStringStream e;
  1224. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  1225. this->SetError(e.str().c_str());
  1226. return false;
  1227. }
  1228. }
  1229. }
  1230. return true;
  1231. }
  1232. //----------------------------------------------------------------------------
  1233. void cmFileCommand::ComputeVersionedName(std::string& name,
  1234. const char* version)
  1235. {
  1236. #if defined(__APPLE__)
  1237. std::string ext;
  1238. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  1239. if(dot_pos != name.npos)
  1240. {
  1241. ext = name.substr(dot_pos, name.npos);
  1242. name = name.substr(0, dot_pos);
  1243. }
  1244. #endif
  1245. name += ".";
  1246. name += version;
  1247. #if defined(__APPLE__)
  1248. name += ext;
  1249. #endif
  1250. }
  1251. //----------------------------------------------------------------------------
  1252. bool cmFileCommand::HandleRelativePathCommand(
  1253. std::vector<std::string> const& args)
  1254. {
  1255. if(args.size() != 4 )
  1256. {
  1257. this->SetError("called with incorrect number of arguments");
  1258. return false;
  1259. }
  1260. const std::string& outVar = args[1];
  1261. const std::string& directoryName = args[2];
  1262. const std::string& fileName = args[3];
  1263. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  1264. {
  1265. std::string errstring =
  1266. "RelativePath must be passed a full path to the directory: "
  1267. + directoryName;
  1268. this->SetError(errstring.c_str());
  1269. return false;
  1270. }
  1271. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  1272. {
  1273. std::string errstring =
  1274. "RelativePath must be passed a full path to the file: "
  1275. + fileName;
  1276. this->SetError(errstring.c_str());
  1277. return false;
  1278. }
  1279. std::string res = cmSystemTools::RelativePath(directoryName.c_str(),
  1280. fileName.c_str());
  1281. this->Makefile->AddDefinition(outVar.c_str(),
  1282. res.c_str());
  1283. return true;
  1284. }
  1285. //----------------------------------------------------------------------------
  1286. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  1287. bool recurse)
  1288. {
  1289. std::string message;
  1290. std::vector<std::string>::const_iterator i = args.begin();
  1291. i++; // Get rid of subcommand
  1292. for(;i != args.end(); ++i)
  1293. {
  1294. if(cmSystemTools::FileIsDirectory(i->c_str()) && recurse)
  1295. {
  1296. cmSystemTools::RemoveADirectory(i->c_str());
  1297. }
  1298. else
  1299. {
  1300. cmSystemTools::RemoveFile(i->c_str());
  1301. }
  1302. }
  1303. return true;
  1304. }
  1305. //----------------------------------------------------------------------------
  1306. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  1307. const& args,
  1308. bool nativePath)
  1309. {
  1310. std::vector<std::string>::const_iterator i = args.begin();
  1311. if(args.size() != 3)
  1312. {
  1313. this->SetError("FILE(SYSTEM_PATH ENV result) must be called with "
  1314. "only three arguments.");
  1315. return false;
  1316. }
  1317. i++; // Get rid of subcommand
  1318. #if defined(_WIN32) && !defined(__CYGWIN__)
  1319. char pathSep = ';';
  1320. #else
  1321. char pathSep = ':';
  1322. #endif
  1323. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  1324. pathSep);
  1325. i++;
  1326. const char* var = i->c_str();
  1327. std::string value;
  1328. for(std::vector<cmsys::String>::iterator j = path.begin();
  1329. j != path.end(); ++j)
  1330. {
  1331. if(j != path.begin())
  1332. {
  1333. value += ";";
  1334. }
  1335. if(!nativePath)
  1336. {
  1337. cmSystemTools::ConvertToUnixSlashes(*j);
  1338. }
  1339. else
  1340. {
  1341. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  1342. // remove double quotes in the path
  1343. cmsys::String& s = *j;
  1344. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  1345. {
  1346. s = s.substr(1,s.size()-2);
  1347. }
  1348. }
  1349. value += *j;
  1350. }
  1351. this->Makefile->AddDefinition(var, value.c_str());
  1352. return true;
  1353. }