cmFileCommand.cxx 36 KB

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