cmFileCommand.cxx 43 KB

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