cmFileCommand.cxx 42 KB

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