cmFileCommand.cxx 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. }
  215. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  216. {
  217. std::string expr = this->Makefile->GetCurrentDirectory();
  218. // Handle script mode
  219. if ( expr.size() > 0 )
  220. {
  221. expr += "/" + *i;
  222. g.FindFiles(expr);
  223. }
  224. else
  225. {
  226. g.FindFiles(*i);
  227. }
  228. }
  229. else
  230. {
  231. g.FindFiles(*i);
  232. }
  233. std::vector<std::string>::size_type cc;
  234. std::vector<std::string>& files = g.GetFiles();
  235. for ( cc = 0; cc < files.size(); cc ++ )
  236. {
  237. if ( !first )
  238. {
  239. output += ";";
  240. }
  241. output += files[cc];
  242. first = false;
  243. }
  244. }
  245. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  246. return true;
  247. }
  248. //----------------------------------------------------------------------------
  249. bool cmFileCommand::HandleMakeDirectoryCommand(
  250. std::vector<std::string> const& args)
  251. {
  252. if(args.size() < 2 )
  253. {
  254. this->SetError("called with incorrect number of arguments");
  255. return false;
  256. }
  257. std::vector<std::string>::const_iterator i = args.begin();
  258. i++; // Get rid of subcommand
  259. std::string expr;
  260. for ( ; i != args.end(); ++i )
  261. {
  262. const std::string* cdir = &(*i);
  263. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  264. {
  265. expr = this->Makefile->GetCurrentDirectory();
  266. expr += "/" + *i;
  267. cdir = &expr;
  268. }
  269. if ( !this->Makefile->CanIWriteThisFile(cdir->c_str()) )
  270. {
  271. std::string e = "attempted to create a directory: " + *cdir
  272. + " into a source directory.";
  273. this->SetError(e.c_str());
  274. cmSystemTools::SetFatalErrorOccured();
  275. return false;
  276. }
  277. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  278. {
  279. std::string error = "problem creating directory: " + *cdir;
  280. this->SetError(error.c_str());
  281. return false;
  282. }
  283. }
  284. return true;
  285. }
  286. //----------------------------------------------------------------------------
  287. bool cmFileCommand::HandleInstallCommand(
  288. std::vector<std::string> const& args)
  289. {
  290. if ( args.size() < 6 )
  291. {
  292. this->SetError("called with incorrect number of arguments");
  293. return false;
  294. }
  295. std::string rename = "";
  296. std::string destination = "";
  297. std::string stype = "FILES";
  298. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  299. std::set<cmStdString> components;
  300. std::set<cmStdString> configurations;
  301. std::vector<std::string> files;
  302. int itype = cmTarget::INSTALL_FILES;
  303. std::vector<std::string>::size_type i = 0;
  304. i++; // Get rid of subcommand
  305. std::map<cmStdString, const char*> properties;
  306. // Build a table of permissions flags.
  307. #if defined(_WIN32) && !defined(__CYGWIN__)
  308. mode_t mode_owner_read = S_IREAD;
  309. mode_t mode_owner_write = S_IWRITE;
  310. mode_t mode_owner_execute = S_IEXEC;
  311. mode_t mode_group_read = 0;
  312. mode_t mode_group_write = 0;
  313. mode_t mode_group_execute = 0;
  314. mode_t mode_world_read = 0;
  315. mode_t mode_world_write = 0;
  316. mode_t mode_world_execute = 0;
  317. mode_t mode_setuid = 0;
  318. mode_t mode_setgid = 0;
  319. #else
  320. mode_t mode_owner_read = S_IRUSR;
  321. mode_t mode_owner_write = S_IWUSR;
  322. mode_t mode_owner_execute = S_IXUSR;
  323. mode_t mode_group_read = S_IRGRP;
  324. mode_t mode_group_write = S_IWGRP;
  325. mode_t mode_group_execute = S_IXGRP;
  326. mode_t mode_world_read = S_IROTH;
  327. mode_t mode_world_write = S_IWOTH;
  328. mode_t mode_world_execute = S_IXOTH;
  329. mode_t mode_setuid = S_ISUID;
  330. mode_t mode_setgid = S_ISGID;
  331. #endif
  332. bool in_files = false;
  333. bool in_properties = false;
  334. bool in_permissions = false;
  335. bool in_components = false;
  336. bool in_configurations = false;
  337. bool use_given_permissions = false;
  338. mode_t permissions = 0;
  339. bool optional = false;
  340. for ( ; i != args.size(); ++i )
  341. {
  342. const std::string* cstr = &args[i];
  343. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  344. {
  345. i++;
  346. destination = args[i];
  347. in_files = false;
  348. in_properties = false;
  349. in_permissions = false;
  350. in_components = false;
  351. in_configurations = false;
  352. }
  353. else if ( *cstr == "TYPE" && i < args.size()-1 )
  354. {
  355. i++;
  356. stype = args[i];
  357. if ( args[i+1] == "OPTIONAL" )
  358. {
  359. i++;
  360. optional = true;
  361. }
  362. in_properties = false;
  363. in_files = false;
  364. in_permissions = false;
  365. in_components = false;
  366. in_configurations = false;
  367. }
  368. else if ( *cstr == "RENAME" && i < args.size()-1 )
  369. {
  370. i++;
  371. rename = args[i];
  372. in_properties = false;
  373. in_files = false;
  374. in_permissions = false;
  375. in_components = false;
  376. in_configurations = false;
  377. }
  378. else if ( *cstr == "PROPERTIES" )
  379. {
  380. in_properties = true;
  381. in_files = false;
  382. in_permissions = false;
  383. in_components = false;
  384. in_configurations = false;
  385. }
  386. else if ( *cstr == "PERMISSIONS" )
  387. {
  388. use_given_permissions = true;
  389. in_properties = false;
  390. in_files = false;
  391. in_permissions = true;
  392. in_components = false;
  393. in_configurations = false;
  394. }
  395. else if ( *cstr == "COMPONENTS" )
  396. {
  397. in_properties = false;
  398. in_files = false;
  399. in_permissions = false;
  400. in_components = true;
  401. in_configurations = false;
  402. }
  403. else if ( *cstr == "CONFIGURATIONS" )
  404. {
  405. in_properties = false;
  406. in_files = false;
  407. in_permissions = false;
  408. in_components = false;
  409. in_configurations = true;
  410. }
  411. else if ( *cstr == "FILES" && !in_files)
  412. {
  413. in_files = true;
  414. in_properties = false;
  415. in_permissions = false;
  416. in_components = false;
  417. in_configurations = false;
  418. }
  419. else if ( in_properties && i < args.size()-1 )
  420. {
  421. properties[args[i]] = args[i+1].c_str();
  422. i++;
  423. }
  424. else if ( in_files )
  425. {
  426. files.push_back(*cstr);
  427. }
  428. else if ( in_components )
  429. {
  430. components.insert(*cstr);
  431. }
  432. else if ( in_configurations )
  433. {
  434. configurations.insert(cmSystemTools::UpperCase(*cstr));
  435. }
  436. else if(in_permissions && args[i] == "OWNER_READ")
  437. {
  438. permissions |= mode_owner_read;
  439. }
  440. else if(in_permissions && args[i] == "OWNER_WRITE")
  441. {
  442. permissions |= mode_owner_write;
  443. }
  444. else if(in_permissions && args[i] == "OWNER_EXECUTE")
  445. {
  446. permissions |= mode_owner_execute;
  447. }
  448. else if(in_permissions && args[i] == "GROUP_READ")
  449. {
  450. permissions |= mode_group_read;
  451. }
  452. else if(in_permissions && args[i] == "GROUP_WRITE")
  453. {
  454. permissions |= mode_group_write;
  455. }
  456. else if(in_permissions && args[i] == "GROUP_EXECUTE")
  457. {
  458. permissions |= mode_group_execute;
  459. }
  460. else if(in_permissions && args[i] == "WORLD_READ")
  461. {
  462. permissions |= mode_world_read;
  463. }
  464. else if(in_permissions && args[i] == "WORLD_WRITE")
  465. {
  466. permissions |= mode_world_write;
  467. }
  468. else if(in_permissions && args[i] == "WORLD_EXECUTE")
  469. {
  470. permissions |= mode_world_execute;
  471. }
  472. else if(in_permissions && args[i] == "SETUID")
  473. {
  474. permissions |= mode_setuid;
  475. }
  476. else if(in_permissions && args[i] == "SETGID")
  477. {
  478. permissions |= mode_setgid;
  479. }
  480. else
  481. {
  482. this->SetError("called with inappropriate arguments");
  483. return false;
  484. }
  485. }
  486. if ( destination.size() < 2 )
  487. {
  488. this->SetError("called with inapropriate arguments. "
  489. "No DESTINATION provided or .");
  490. return false;
  491. }
  492. // Check for component-specific installation.
  493. const char* cmake_install_component =
  494. this->Makefile->GetDefinition("CMAKE_INSTALL_COMPONENT");
  495. if(cmake_install_component && *cmake_install_component)
  496. {
  497. // This install rule applies only if it is associated with the
  498. // current component.
  499. if(components.find(cmake_install_component) == components.end())
  500. {
  501. return true;
  502. }
  503. }
  504. // Check for configuration-specific installation.
  505. if(!configurations.empty())
  506. {
  507. std::string cmake_install_configuration =
  508. cmSystemTools::UpperCase(
  509. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_CONFIG_NAME"));
  510. if(cmake_install_configuration.empty())
  511. {
  512. // No configuration specified for installation but this install
  513. // rule is configuration-specific. Skip it.
  514. return true;
  515. }
  516. else if(configurations.find(cmake_install_configuration) ==
  517. configurations.end())
  518. {
  519. // This rule is specific to a configuration not being installed.
  520. return true;
  521. }
  522. }
  523. int destDirLength = 0;
  524. if ( destdir && *destdir )
  525. {
  526. std::string sdestdir = destdir;
  527. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  528. char ch1 = destination[0];
  529. char ch2 = destination[1];
  530. char ch3 = 0;
  531. if ( destination.size() > 2 )
  532. {
  533. ch3 = destination[2];
  534. }
  535. int skip = 0;
  536. if ( ch1 != '/' )
  537. {
  538. int relative = 0;
  539. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  540. ch2 == ':' )
  541. {
  542. // Assume windows
  543. // let's do some destdir magic:
  544. skip = 2;
  545. if ( ch3 != '/' )
  546. {
  547. relative = 1;
  548. }
  549. }
  550. else
  551. {
  552. relative = 1;
  553. }
  554. if ( relative )
  555. {
  556. // This is relative path on unix or windows. Since we are doing
  557. // destdir, this case does not make sense.
  558. this->SetError("called with relative DESTINATION. This "
  559. "does not make sense when using DESTDIR. Specify "
  560. "absolute path or remove DESTDIR environment variable.");
  561. return false;
  562. }
  563. }
  564. else
  565. {
  566. if ( ch2 == '/' )
  567. {
  568. // looks like a network path.
  569. this->SetError("called with network path DESTINATION. This "
  570. "does not make sense when using DESTDIR. Specify local "
  571. "absolute path or remove DESTDIR environment variable.");
  572. return false;
  573. }
  574. }
  575. destination = sdestdir + (destination.c_str() + skip);
  576. destDirLength = int(sdestdir.size());
  577. }
  578. if ( files.size() == 0 )
  579. {
  580. this->SetError(
  581. "called with inapropriate arguments. No FILES provided.");
  582. return false;
  583. }
  584. if ( stype == "EXECUTABLE" )
  585. {
  586. itype = cmTarget::EXECUTABLE;
  587. }
  588. else if ( stype == "PROGRAM" )
  589. {
  590. itype = cmTarget::INSTALL_PROGRAMS;
  591. }
  592. else if ( stype == "STATIC_LIBRARY" )
  593. {
  594. itype = cmTarget::STATIC_LIBRARY;
  595. }
  596. else if ( stype == "SHARED_LIBRARY" )
  597. {
  598. itype = cmTarget::SHARED_LIBRARY;
  599. }
  600. else if ( stype == "MODULE" )
  601. {
  602. itype = cmTarget::MODULE_LIBRARY;
  603. }
  604. else if ( stype == "DIRECTORY" )
  605. {
  606. itype = cmTarget::INSTALL_DIRECTORY;
  607. }
  608. if ( !cmSystemTools::FileExists(destination.c_str()) )
  609. {
  610. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  611. {
  612. std::string errstring = "cannot create directory: " + destination +
  613. ". Maybe need administrative privileges.";
  614. this->SetError(errstring.c_str());
  615. return false;
  616. }
  617. }
  618. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  619. {
  620. std::string errstring = "INSTALL destination: " + destination +
  621. " is not a directory.";
  622. this->SetError(errstring.c_str());
  623. return false;
  624. }
  625. // Check rename form.
  626. if(!rename.empty())
  627. {
  628. if(itype != cmTarget::INSTALL_FILES)
  629. {
  630. this->SetError("INSTALL option RENAME may be used only with FILES.");
  631. return false;
  632. }
  633. if(files.size() > 1)
  634. {
  635. this->SetError("INSTALL option RENAME may be used only with one file.");
  636. return false;
  637. }
  638. }
  639. // If permissions were not specified set default permissions for
  640. // this target type.
  641. if(!use_given_permissions)
  642. {
  643. switch(itype)
  644. {
  645. case cmTarget::SHARED_LIBRARY:
  646. case cmTarget::MODULE_LIBRARY:
  647. #if defined(__linux__)
  648. // Use read/write permissions.
  649. permissions = 0;
  650. permissions |= mode_owner_read;
  651. permissions |= mode_owner_write;
  652. permissions |= mode_group_read;
  653. permissions |= mode_world_read;
  654. break;
  655. #endif
  656. case cmTarget::EXECUTABLE:
  657. case cmTarget::INSTALL_PROGRAMS:
  658. // Use read/write/executable permissions.
  659. permissions = 0;
  660. permissions |= mode_owner_read;
  661. permissions |= mode_owner_write;
  662. permissions |= mode_owner_execute;
  663. permissions |= mode_group_read;
  664. permissions |= mode_group_execute;
  665. permissions |= mode_world_read;
  666. permissions |= mode_world_execute;
  667. break;
  668. default:
  669. // Use read/write permissions.
  670. permissions = 0;
  671. permissions |= mode_owner_read;
  672. permissions |= mode_owner_write;
  673. permissions |= mode_group_read;
  674. permissions |= mode_world_read;
  675. break;
  676. }
  677. }
  678. // Get the current manifest.
  679. const char* manifest_files =
  680. this->Makefile->GetDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  681. std::string smanifest_files;
  682. if ( manifest_files )
  683. {
  684. smanifest_files = manifest_files;
  685. }
  686. // Check whether files should be copied always or only if they have
  687. // changed.
  688. bool copy_always =
  689. cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_INSTALL_ALWAYS"));
  690. // Handle each file listed.
  691. for ( i = 0; i < files.size(); i ++ )
  692. {
  693. // Split the input file into its directory and name components.
  694. std::string fromDir = cmSystemTools::GetFilenamePath(files[i]);
  695. std::string fromName = cmSystemTools::GetFilenameName(files[i]);
  696. // Compute the full path to the destination file.
  697. std::string toFile = destination;
  698. toFile += "/";
  699. toFile += rename.empty()? fromName : rename;
  700. // Handle type-specific installation details.
  701. switch(itype)
  702. {
  703. case cmTarget::MODULE_LIBRARY:
  704. case cmTarget::STATIC_LIBRARY:
  705. case cmTarget::SHARED_LIBRARY:
  706. {
  707. // Handle shared library versioning
  708. const char* lib_version = 0;
  709. const char* lib_soversion = 0;
  710. if ( properties.find("VERSION") != properties.end() )
  711. {
  712. lib_version = properties["VERSION"];
  713. }
  714. if ( properties.find("SOVERSION") != properties.end() )
  715. {
  716. lib_soversion = properties["SOVERSION"];
  717. }
  718. if ( !lib_version && lib_soversion )
  719. {
  720. lib_version = lib_soversion;
  721. }
  722. if ( !lib_soversion && lib_version )
  723. {
  724. lib_soversion = lib_version;
  725. }
  726. if ( lib_version && lib_soversion )
  727. {
  728. std::string libname = toFile;
  729. std::string soname = toFile;
  730. std::string soname_nopath = fromName;
  731. this->ComputeVersionedName(soname, lib_soversion);
  732. this->ComputeVersionedName(soname_nopath, lib_soversion);
  733. this->ComputeVersionedName(fromName, lib_version);
  734. this->ComputeVersionedName(toFile, lib_version);
  735. cmSystemTools::RemoveFile(soname.c_str());
  736. cmSystemTools::RemoveFile(libname.c_str());
  737. if (!cmSystemTools::CreateSymlink(soname_nopath.c_str(),
  738. libname.c_str()) )
  739. {
  740. std::string errstring = "error when creating symlink from: "
  741. + libname + " to " + soname_nopath;
  742. this->SetError(errstring.c_str());
  743. return false;
  744. }
  745. smanifest_files += ";";
  746. smanifest_files += libname.substr(destDirLength);;
  747. if ( toFile != soname )
  748. {
  749. if ( !cmSystemTools::CreateSymlink(fromName.c_str(),
  750. soname.c_str()) )
  751. {
  752. std::string errstring = "error when creating symlink from: "
  753. + soname + " to " + fromName;
  754. this->SetError(errstring.c_str());
  755. return false;
  756. }
  757. smanifest_files += ";";
  758. smanifest_files += soname.substr(destDirLength);
  759. }
  760. }
  761. }
  762. break;
  763. case cmTarget::EXECUTABLE:
  764. {
  765. // Handle executable versioning
  766. const char* exe_version = 0;
  767. if ( properties.find("VERSION") != properties.end() )
  768. {
  769. exe_version = properties["VERSION"];
  770. }
  771. if ( exe_version )
  772. {
  773. std::string exename = toFile;
  774. std::string exename_nopath = fromName;
  775. exename_nopath += "-";
  776. exename_nopath += exe_version;
  777. fromName += "-";
  778. fromName += exe_version;
  779. toFile += "-";
  780. toFile += exe_version;
  781. cmSystemTools::RemoveFile(exename.c_str());
  782. if (!cmSystemTools::CreateSymlink(exename_nopath.c_str(),
  783. exename.c_str()) )
  784. {
  785. std::string errstring = "error when creating symlink from: "
  786. + exename + " to " + exename_nopath;
  787. this->SetError(errstring.c_str());
  788. return false;
  789. }
  790. smanifest_files += ";";
  791. smanifest_files += exename.substr(destDirLength);
  792. }
  793. }
  794. break;
  795. }
  796. // Construct the full path to the source file. The file name may
  797. // have been changed above.
  798. std::string fromFile = fromDir;
  799. fromFile += "/";
  800. fromFile += fromName;
  801. std::string message;
  802. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  803. {
  804. if(itype == cmTarget::INSTALL_DIRECTORY &&
  805. cmSystemTools::FileIsDirectory(fromFile.c_str()))
  806. {
  807. // We will install this file. Display the information.
  808. message = "Installing ";
  809. message += toFile.c_str();
  810. this->Makefile->DisplayStatus(message.c_str(), -1);
  811. if(!cmSystemTools::CopyADirectory(fromFile.c_str(), toFile.c_str(),
  812. copy_always))
  813. {
  814. cmOStringStream e;
  815. e << "INSTALL cannot copy directory \"" << fromFile
  816. << "\" to \"" << toFile + "\".";
  817. this->SetError(e.str().c_str());
  818. return false;
  819. }
  820. }
  821. else if(cmSystemTools::FileExists(fromFile.c_str()))
  822. {
  823. // We will install this file. Display the information.
  824. message = "Installing ";
  825. message += toFile.c_str();
  826. this->Makefile->DisplayStatus(message.c_str(), -1);
  827. // Copy the file.
  828. if(!cmSystemTools::CopyAFile(fromFile.c_str(), toFile.c_str(),
  829. copy_always))
  830. {
  831. cmOStringStream e;
  832. e << "INSTALL cannot copy file \"" << fromFile
  833. << "\" to \"" << toFile + "\".";
  834. this->SetError(e.str().c_str());
  835. return false;
  836. }
  837. // Perform post-installation processing on the file depending
  838. // on its type.
  839. #if defined(__APPLE_CC__)
  840. // Static libraries need ranlib on this platform.
  841. if(itype == cmTarget::STATIC_LIBRARY)
  842. {
  843. std::string ranlib = "ranlib ";
  844. ranlib += cmSystemTools::ConvertToOutputPath(toFile.c_str());
  845. if(!cmSystemTools::RunSingleCommand(ranlib.c_str()))
  846. {
  847. std::string err = "ranlib failed: ";
  848. err += ranlib;
  849. this->SetError(err.c_str());
  850. return false;
  851. }
  852. }
  853. #endif
  854. // Set permissions of the destination file.
  855. if(!cmSystemTools::SetPermissions(toFile.c_str(), permissions))
  856. {
  857. cmOStringStream e;
  858. e << "Problem setting permissions on file \""
  859. << toFile.c_str() << "\"";
  860. this->SetError(e.str().c_str());
  861. return false;
  862. }
  863. // Add the file to the manifest.
  864. smanifest_files += ";";
  865. smanifest_files += toFile.substr(destDirLength);
  866. }
  867. else if(!optional)
  868. {
  869. // The input file does not exist and installation is not optional.
  870. cmOStringStream e;
  871. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  872. this->SetError(e.str().c_str());
  873. return false;
  874. }
  875. }
  876. }
  877. // Save the updated install manifest.
  878. this->Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  879. smanifest_files.c_str());
  880. return true;
  881. }
  882. //----------------------------------------------------------------------------
  883. void cmFileCommand::ComputeVersionedName(std::string& name,
  884. const char* version)
  885. {
  886. #if defined(__APPLE__)
  887. std::string ext;
  888. kwsys_stl::string::size_type dot_pos = name.rfind(".");
  889. if(dot_pos != name.npos)
  890. {
  891. ext = name.substr(dot_pos, name.npos);
  892. name = name.substr(0, dot_pos);
  893. }
  894. #endif
  895. name += ".";
  896. name += version;
  897. #if defined(__APPLE__)
  898. name += ext;
  899. #endif
  900. }
  901. //----------------------------------------------------------------------------
  902. bool cmFileCommand::HandleRelativePathCommand(
  903. std::vector<std::string> const& args)
  904. {
  905. if(args.size() != 4 )
  906. {
  907. this->SetError("called with incorrect number of arguments");
  908. return false;
  909. }
  910. const std::string& outVar = args[1];
  911. const std::string& directoryName = args[2];
  912. const std::string& fileName = args[3];
  913. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  914. {
  915. std::string errstring =
  916. "RelativePath must be passed a full path to the directory: "
  917. + directoryName;
  918. this->SetError(errstring.c_str());
  919. return false;
  920. }
  921. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  922. {
  923. std::string errstring =
  924. "RelativePath must be passed a full path to the file: "
  925. + fileName;
  926. this->SetError(errstring.c_str());
  927. return false;
  928. }
  929. std::string res = cmSystemTools::RelativePath(directoryName.c_str(),
  930. fileName.c_str());
  931. this->Makefile->AddDefinition(outVar.c_str(),
  932. res.c_str());
  933. return true;
  934. }
  935. //----------------------------------------------------------------------------
  936. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  937. bool recurse)
  938. {
  939. std::string message;
  940. std::vector<std::string>::const_iterator i = args.begin();
  941. i++; // Get rid of subcommand
  942. for(;i != args.end(); ++i)
  943. {
  944. if(cmSystemTools::FileIsDirectory(i->c_str()) && recurse)
  945. {
  946. cmSystemTools::RemoveADirectory(i->c_str());
  947. }
  948. else
  949. {
  950. cmSystemTools::RemoveFile(i->c_str());
  951. }
  952. }
  953. return true;
  954. }
  955. //----------------------------------------------------------------------------
  956. bool cmFileCommand::HandleCMakePathCommand(std::vector<std::string>
  957. const& args,
  958. bool nativePath)
  959. {
  960. std::vector<std::string>::const_iterator i = args.begin();
  961. if(args.size() != 3)
  962. {
  963. this->SetError("FILE(SYSTEM_PATH ENV result) must be called with "
  964. "only three arguments.");
  965. return false;
  966. }
  967. i++; // Get rid of subcommand
  968. #if defined(_WIN32) && !defined(__CYGWIN__)
  969. char pathSep = ';';
  970. #else
  971. char pathSep = ':';
  972. #endif
  973. std::vector<cmsys::String> path = cmSystemTools::SplitString(i->c_str(),
  974. pathSep);
  975. i++;
  976. const char* var = i->c_str();
  977. std::string value;
  978. for(std::vector<cmsys::String>::iterator j = path.begin();
  979. j != path.end(); ++j)
  980. {
  981. if(j != path.begin())
  982. {
  983. value += ";";
  984. }
  985. if(!nativePath)
  986. {
  987. cmSystemTools::ConvertToUnixSlashes(*j);
  988. }
  989. else
  990. {
  991. *j = cmSystemTools::ConvertToOutputPath(j->c_str());
  992. // remove double quotes in the path
  993. cmsys::String& s = *j;
  994. if(s.size() > 1 && s[0] == '\"' && s[s.size()-1] == '\"')
  995. {
  996. s = s.substr(1,s.size()-2);
  997. }
  998. }
  999. value += *j;
  1000. }
  1001. this->Makefile->AddDefinition(var, value.c_str());
  1002. return true;
  1003. }