cmFileCommand.cxx 28 KB

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