cmFileCommand.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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 == "SYSTEM_PATH" )
  68. {
  69. return this->HandleSystemPathCommand(args);
  70. }
  71. std::string e = "does not recognize sub-command "+subCommand;
  72. this->SetError(e.c_str());
  73. return false;
  74. }
  75. //----------------------------------------------------------------------------
  76. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  77. bool append)
  78. {
  79. std::string message;
  80. std::vector<std::string>::const_iterator i = args.begin();
  81. i++; // Get rid of subcommand
  82. std::string fileName = *i;
  83. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  84. {
  85. fileName = this->Makefile->GetCurrentDirectory();
  86. fileName += "/" + *i;
  87. }
  88. i++;
  89. for(;i != args.end(); ++i)
  90. {
  91. message += *i;
  92. }
  93. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  94. cmSystemTools::MakeDirectory(dir.c_str());
  95. mode_t mode =
  96. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  97. S_IREAD | S_IWRITE
  98. #elif defined( __BORLANDC__ )
  99. S_IRUSR | S_IWUSR
  100. #else
  101. S_IRUSR | S_IWUSR |
  102. S_IRGRP |
  103. S_IROTH
  104. #endif
  105. ;
  106. // Set permissions to writable
  107. if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
  108. {
  109. cmSystemTools::SetPermissions(fileName.c_str(),
  110. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  111. S_IREAD | S_IWRITE
  112. #else
  113. S_IRUSR | S_IWUSR
  114. #endif
  115. );
  116. }
  117. // If GetPermissions fails, pretend like it is ok. File open will fail if
  118. // the file is not writable
  119. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  120. if ( !file )
  121. {
  122. std::string error = "Internal CMake error when trying to open file: ";
  123. error += fileName.c_str();
  124. error += " for writing.";
  125. this->SetError(error.c_str());
  126. return false;
  127. }
  128. file << message;
  129. file.close();
  130. cmSystemTools::SetPermissions(fileName.c_str(), mode);
  131. this->Makefile->AddWrittenFile(fileName.c_str());
  132. return true;
  133. }
  134. //----------------------------------------------------------------------------
  135. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  136. {
  137. if ( args.size() != 3 )
  138. {
  139. this->SetError("READ must be called with two additional arguments");
  140. return false;
  141. }
  142. std::string fileName = args[1];
  143. if ( !cmsys::SystemTools::FileIsFullPath(args[1].c_str()) )
  144. {
  145. fileName = this->Makefile->GetCurrentDirectory();
  146. fileName += "/" + args[1];
  147. }
  148. std::string variable = args[2];
  149. std::ifstream file(fileName.c_str(), std::ios::in);
  150. if ( !file )
  151. {
  152. std::string error = "Internal CMake error when trying to open file: ";
  153. error += fileName.c_str();
  154. error += " for reading.";
  155. this->SetError(error.c_str());
  156. return false;
  157. }
  158. std::string output;
  159. std::string line;
  160. bool has_newline = false;
  161. while ( cmSystemTools::GetLineFromStream(file, line, &has_newline) )
  162. {
  163. output += line;
  164. if ( has_newline )
  165. {
  166. output += "\n";
  167. }
  168. }
  169. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  170. return true;
  171. }
  172. //----------------------------------------------------------------------------
  173. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  174. bool recurse)
  175. {
  176. if ( args.size() < 2 )
  177. {
  178. this->SetError("GLOB requires at least a variable name");
  179. return false;
  180. }
  181. std::vector<std::string>::const_iterator i = args.begin();
  182. i++; // Get rid of subcommand
  183. std::string variable = *i;
  184. i++;
  185. cmsys::Glob g;
  186. g.SetRecurse(recurse);
  187. std::string output = "";
  188. bool first = true;
  189. for ( ; i != args.end(); ++i )
  190. {
  191. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  192. {
  193. std::string expr = this->Makefile->GetCurrentDirectory();
  194. // Handle script mode
  195. if ( expr.size() > 0 )
  196. {
  197. expr += "/" + *i;
  198. g.FindFiles(expr);
  199. }
  200. else
  201. {
  202. g.FindFiles(*i);
  203. }
  204. }
  205. else
  206. {
  207. g.FindFiles(*i);
  208. }
  209. std::vector<std::string>::size_type cc;
  210. std::vector<std::string>& files = g.GetFiles();
  211. for ( cc = 0; cc < files.size(); cc ++ )
  212. {
  213. if ( !first )
  214. {
  215. output += ";";
  216. }
  217. output += files[cc];
  218. first = false;
  219. }
  220. }
  221. this->Makefile->AddDefinition(variable.c_str(), output.c_str());
  222. return true;
  223. }
  224. //----------------------------------------------------------------------------
  225. bool cmFileCommand::HandleMakeDirectoryCommand(
  226. std::vector<std::string> const& args)
  227. {
  228. if(args.size() < 2 )
  229. {
  230. this->SetError("called with incorrect number of arguments");
  231. return false;
  232. }
  233. std::vector<std::string>::const_iterator i = args.begin();
  234. i++; // Get rid of subcommand
  235. std::string expr;
  236. for ( ; i != args.end(); ++i )
  237. {
  238. const std::string* cdir = &(*i);
  239. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  240. {
  241. expr = this->Makefile->GetCurrentDirectory();
  242. expr += "/" + *i;
  243. cdir = &expr;
  244. }
  245. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  246. {
  247. std::string error = "problem creating directory: " + *cdir;
  248. this->SetError(error.c_str());
  249. return false;
  250. }
  251. }
  252. return true;
  253. }
  254. //----------------------------------------------------------------------------
  255. bool cmFileCommand::HandleInstallCommand(
  256. std::vector<std::string> const& args)
  257. {
  258. if ( args.size() < 6 )
  259. {
  260. this->SetError("called with incorrect number of arguments");
  261. return false;
  262. }
  263. std::string rename = "";
  264. std::string destination = "";
  265. std::string stype = "FILES";
  266. const char* build_type = this->Makefile->GetDefinition("BUILD_TYPE");
  267. if ( build_type && strcmp(build_type, ".") == 0 )
  268. {
  269. build_type = 0;
  270. }
  271. if ( build_type && strncmp(build_type, ".\\", 2) == 0 )
  272. {
  273. build_type += 2;
  274. }
  275. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  276. std::vector<std::string> files;
  277. int itype = cmTarget::INSTALL_FILES;
  278. std::vector<std::string>::size_type i = 0;
  279. i++; // Get rid of subcommand
  280. std::map<cmStdString, const char*> properties;
  281. // Build a table of permissions flags.
  282. #if defined(_WIN32) && !defined(__CYGWIN__)
  283. mode_t mode_owner_read = S_IREAD;
  284. mode_t mode_owner_write = S_IWRITE;
  285. mode_t mode_owner_execute = S_IEXEC;
  286. mode_t mode_group_read = 0;
  287. mode_t mode_group_write = 0;
  288. mode_t mode_group_execute = 0;
  289. mode_t mode_world_read = 0;
  290. mode_t mode_world_write = 0;
  291. mode_t mode_world_execute = 0;
  292. mode_t mode_setuid = 0;
  293. mode_t mode_setgid = 0;
  294. #else
  295. mode_t mode_owner_read = S_IRUSR;
  296. mode_t mode_owner_write = S_IWUSR;
  297. mode_t mode_owner_execute = S_IXUSR;
  298. mode_t mode_group_read = S_IRGRP;
  299. mode_t mode_group_write = S_IWGRP;
  300. mode_t mode_group_execute = S_IXGRP;
  301. mode_t mode_world_read = S_IROTH;
  302. mode_t mode_world_write = S_IWOTH;
  303. mode_t mode_world_execute = S_IXOTH;
  304. mode_t mode_setuid = S_ISUID;
  305. mode_t mode_setgid = S_ISGID;
  306. #endif
  307. bool in_files = false;
  308. bool in_properties = false;
  309. bool in_permissions = false;
  310. bool use_given_permissions = false;
  311. mode_t permissions = 0;
  312. bool optional = false;
  313. for ( ; i != args.size(); ++i )
  314. {
  315. const std::string* cstr = &args[i];
  316. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  317. {
  318. i++;
  319. destination = args[i];
  320. in_files = false;
  321. in_properties = false;
  322. in_permissions = false;
  323. }
  324. else if ( *cstr == "TYPE" && i < args.size()-1 )
  325. {
  326. i++;
  327. stype = args[i];
  328. if ( args[i+1] == "OPTIONAL" )
  329. {
  330. i++;
  331. optional = true;
  332. }
  333. in_properties = false;
  334. in_files = false;
  335. in_permissions = false;
  336. }
  337. else if ( *cstr == "RENAME" && i < args.size()-1 )
  338. {
  339. i++;
  340. rename = args[i];
  341. in_properties = false;
  342. in_files = false;
  343. in_permissions = false;
  344. }
  345. else if ( *cstr == "PROPERTIES" )
  346. {
  347. in_properties = true;
  348. in_files = false;
  349. in_permissions = false;
  350. }
  351. else if ( *cstr == "PERMISSIONS" )
  352. {
  353. use_given_permissions = true;
  354. in_properties = false;
  355. in_files = false;
  356. in_permissions = true;
  357. }
  358. else if ( *cstr == "FILES" && !in_files)
  359. {
  360. in_files = true;
  361. in_properties = false;
  362. in_permissions = false;
  363. }
  364. else if ( in_properties && i < args.size()-1 )
  365. {
  366. properties[args[i]] = args[i+1].c_str();
  367. i++;
  368. }
  369. else if ( in_files )
  370. {
  371. files.push_back(*cstr);
  372. }
  373. else if(in_permissions && args[i] == "OWNER_READ")
  374. {
  375. permissions |= mode_owner_read;
  376. }
  377. else if(in_permissions && args[i] == "OWNER_WRITE")
  378. {
  379. permissions |= mode_owner_write;
  380. }
  381. else if(in_permissions && args[i] == "OWNER_EXECUTE")
  382. {
  383. permissions |= mode_owner_execute;
  384. }
  385. else if(in_permissions && args[i] == "GROUP_READ")
  386. {
  387. permissions |= mode_group_read;
  388. }
  389. else if(in_permissions && args[i] == "GROUP_WRITE")
  390. {
  391. permissions |= mode_group_write;
  392. }
  393. else if(in_permissions && args[i] == "GROUP_EXECUTE")
  394. {
  395. permissions |= mode_group_execute;
  396. }
  397. else if(in_permissions && args[i] == "WORLD_READ")
  398. {
  399. permissions |= mode_world_read;
  400. }
  401. else if(in_permissions && args[i] == "WORLD_WRITE")
  402. {
  403. permissions |= mode_world_write;
  404. }
  405. else if(in_permissions && args[i] == "WORLD_EXECUTE")
  406. {
  407. permissions |= mode_world_execute;
  408. }
  409. else if(in_permissions && args[i] == "SETUID")
  410. {
  411. permissions |= mode_setuid;
  412. }
  413. else if(in_permissions && args[i] == "SETGID")
  414. {
  415. permissions |= mode_setgid;
  416. }
  417. else
  418. {
  419. this->SetError("called with inappropriate arguments");
  420. return false;
  421. }
  422. }
  423. if ( destination.size() < 2 )
  424. {
  425. this->SetError("called with inapropriate arguments. "
  426. "No DESTINATION provided or .");
  427. return false;
  428. }
  429. int destDirLength = 0;
  430. if ( destdir && *destdir )
  431. {
  432. std::string sdestdir = destdir;
  433. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  434. char ch1 = destination[0];
  435. char ch2 = destination[1];
  436. char ch3 = 0;
  437. if ( destination.size() > 2 )
  438. {
  439. ch3 = destination[2];
  440. }
  441. int skip = 0;
  442. if ( ch1 != '/' )
  443. {
  444. int relative = 0;
  445. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  446. ch2 == ':' )
  447. {
  448. // Assume windows
  449. // let's do some destdir magic:
  450. skip = 2;
  451. if ( ch3 != '/' )
  452. {
  453. relative = 1;
  454. }
  455. }
  456. else
  457. {
  458. relative = 1;
  459. }
  460. if ( relative )
  461. {
  462. // This is relative path on unix or windows. Since we are doing
  463. // destdir, this case does not make sense.
  464. this->SetError("called with relative DESTINATION. This "
  465. "does not make sense when using DESTDIR. Specify "
  466. "absolute path or remove DESTDIR environment variable.");
  467. return false;
  468. }
  469. }
  470. else
  471. {
  472. if ( ch2 == '/' )
  473. {
  474. // looks like a network path.
  475. this->SetError("called with network path DESTINATION. This "
  476. "does not make sense when using DESTDIR. Specify local "
  477. "absolute path or remove DESTDIR environment variable.");
  478. return false;
  479. }
  480. }
  481. destination = sdestdir + (destination.c_str() + skip);
  482. destDirLength = int(sdestdir.size());
  483. }
  484. if ( files.size() == 0 )
  485. {
  486. this->SetError(
  487. "called with inapropriate arguments. No FILES provided.");
  488. return false;
  489. }
  490. if ( stype == "EXECUTABLE" )
  491. {
  492. itype = cmTarget::EXECUTABLE;
  493. }
  494. else if ( stype == "PROGRAM" )
  495. {
  496. itype = cmTarget::INSTALL_PROGRAMS;
  497. }
  498. else if ( stype == "STATIC_LIBRARY" )
  499. {
  500. itype = cmTarget::STATIC_LIBRARY;
  501. }
  502. else if ( stype == "SHARED_LIBRARY" )
  503. {
  504. itype = cmTarget::SHARED_LIBRARY;
  505. }
  506. else if ( stype == "MODULE" )
  507. {
  508. itype = cmTarget::MODULE_LIBRARY;
  509. }
  510. if ( !cmSystemTools::FileExists(destination.c_str()) )
  511. {
  512. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  513. {
  514. std::string errstring = "cannot create directory: " + destination +
  515. ". Maybe need administrative privileges.";
  516. this->SetError(errstring.c_str());
  517. return false;
  518. }
  519. }
  520. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  521. {
  522. std::string errstring = "INSTALL destination: " + destination +
  523. " is not a directory.";
  524. this->SetError(errstring.c_str());
  525. return false;
  526. }
  527. // Check rename form.
  528. if(!rename.empty())
  529. {
  530. if(itype != cmTarget::INSTALL_FILES)
  531. {
  532. this->SetError("INSTALL option RENAME may be used only with FILES.");
  533. return false;
  534. }
  535. if(files.size() > 1)
  536. {
  537. this->SetError("INSTALL option RENAME may be used only with one file.");
  538. return false;
  539. }
  540. }
  541. // If permissions were not specified set default permissions for
  542. // this target type.
  543. bool use_source_permissions = false;
  544. if(!use_given_permissions)
  545. {
  546. switch(itype)
  547. {
  548. case cmTarget::SHARED_LIBRARY:
  549. case cmTarget::MODULE_LIBRARY:
  550. #if defined(__linux__)
  551. // Use read/write permissions.
  552. use_given_permissions = true;
  553. permissions = 0;
  554. permissions |= mode_owner_read;
  555. permissions |= mode_owner_write;
  556. permissions |= mode_group_read;
  557. permissions |= mode_world_read;
  558. break;
  559. #endif
  560. case cmTarget::EXECUTABLE:
  561. case cmTarget::INSTALL_PROGRAMS:
  562. // Use read/write/executable permissions.
  563. use_given_permissions = true;
  564. permissions = 0;
  565. permissions |= mode_owner_read;
  566. permissions |= mode_owner_write;
  567. permissions |= mode_owner_execute;
  568. permissions |= mode_group_read;
  569. permissions |= mode_group_execute;
  570. permissions |= mode_world_read;
  571. permissions |= mode_world_execute;
  572. break;
  573. default:
  574. // Use the permissions of the file being copied.
  575. use_source_permissions = true;
  576. break;
  577. }
  578. }
  579. // Get the current manifest.
  580. const char* manifest_files =
  581. this->Makefile->GetDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  582. std::string smanifest_files;
  583. if ( manifest_files )
  584. {
  585. smanifest_files = manifest_files;
  586. }
  587. // Handle each file listed.
  588. for ( i = 0; i < files.size(); i ++ )
  589. {
  590. // Split the input file into its directory and name components.
  591. std::string fromDir = cmSystemTools::GetFilenamePath(files[i]);
  592. std::string fromName = cmSystemTools::GetFilenameName(files[i]);
  593. // Compute the full path to the destination file.
  594. std::string toFile = destination;
  595. toFile += "/";
  596. toFile += rename.empty()? fromName : rename;
  597. // Handle type-specific installation details.
  598. switch(itype)
  599. {
  600. case cmTarget::MODULE_LIBRARY:
  601. case cmTarget::STATIC_LIBRARY:
  602. case cmTarget::SHARED_LIBRARY:
  603. {
  604. // Handle shared library versioning
  605. const char* lib_version = 0;
  606. const char* lib_soversion = 0;
  607. if ( properties.find("VERSION") != properties.end() )
  608. {
  609. lib_version = properties["VERSION"];
  610. }
  611. if ( properties.find("SOVERSION") != properties.end() )
  612. {
  613. lib_soversion = properties["SOVERSION"];
  614. }
  615. if ( !lib_version && lib_soversion )
  616. {
  617. lib_version = lib_soversion;
  618. }
  619. if ( !lib_soversion && lib_version )
  620. {
  621. lib_soversion = lib_version;
  622. }
  623. if ( lib_version && lib_soversion )
  624. {
  625. std::string libname = toFile;
  626. std::string soname = toFile;
  627. std::string soname_nopath = fromName;
  628. soname += ".";
  629. soname += lib_soversion;
  630. soname_nopath += ".";
  631. soname_nopath += lib_soversion;
  632. fromName += ".";
  633. fromName += lib_version;
  634. toFile += ".";
  635. toFile += lib_version;
  636. cmSystemTools::RemoveFile(soname.c_str());
  637. cmSystemTools::RemoveFile(libname.c_str());
  638. if (!cmSystemTools::CreateSymlink(soname_nopath.c_str(), libname.c_str()) )
  639. {
  640. std::string errstring = "error when creating symlink from: " + libname + " to " + soname_nopath;
  641. this->SetError(errstring.c_str());
  642. return false;
  643. }
  644. smanifest_files += ";";
  645. smanifest_files += libname.substr(destDirLength);;
  646. if ( toFile != soname )
  647. {
  648. if ( !cmSystemTools::CreateSymlink(fromName.c_str(), soname.c_str()) )
  649. {
  650. std::string errstring = "error when creating symlink from: " + soname + " to " + fromName;
  651. this->SetError(errstring.c_str());
  652. return false;
  653. }
  654. smanifest_files += ";";
  655. smanifest_files += soname.substr(destDirLength);
  656. }
  657. }
  658. }
  659. break;
  660. case cmTarget::EXECUTABLE:
  661. {
  662. // Handle executable versioning
  663. const char* exe_version = 0;
  664. if ( properties.find("VERSION") != properties.end() )
  665. {
  666. exe_version = properties["VERSION"];
  667. }
  668. if ( exe_version )
  669. {
  670. std::string exename = toFile;
  671. std::string exename_nopath = fromName;
  672. exename_nopath += "-";
  673. exename_nopath += exe_version;
  674. fromName += "-";
  675. fromName += exe_version;
  676. toFile += "-";
  677. toFile += exe_version;
  678. cmSystemTools::RemoveFile(exename.c_str());
  679. if (!cmSystemTools::CreateSymlink(exename_nopath.c_str(), exename.c_str()) )
  680. {
  681. std::string errstring = "error when creating symlink from: " + exename + " to " + exename_nopath;
  682. this->SetError(errstring.c_str());
  683. return false;
  684. }
  685. smanifest_files += ";";
  686. smanifest_files += exename.substr(destDirLength);
  687. }
  688. }
  689. break;
  690. }
  691. // Construct the full path to the source file. The file name may
  692. // have been changed above.
  693. std::string fromFile = fromDir;
  694. fromFile += "/";
  695. fromFile += fromName;
  696. std::string message;
  697. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  698. {
  699. if(cmSystemTools::FileExists(fromFile.c_str()))
  700. {
  701. // We will install this file. Display the information.
  702. message = "Installing ";
  703. message += toFile.c_str();
  704. this->Makefile->DisplayStatus(message.c_str(), -1);
  705. // If no permissions were already given use the permissions of
  706. // the file being copied.
  707. if(!use_given_permissions &&
  708. (!use_source_permissions ||
  709. !cmSystemTools::GetPermissions(fromFile.c_str(), permissions)))
  710. {
  711. // Set default permissions.
  712. permissions = 0;
  713. permissions |= mode_owner_read;
  714. permissions |= mode_owner_write;
  715. permissions |= mode_group_read;
  716. permissions |= mode_world_read;
  717. }
  718. // Remove the original file and try copying the new file.
  719. // TODO: This should be copy-if-different. Don't forget to
  720. // edit the destination file permissions, or compare files
  721. // first. This would need a new SystemTools::FilesDiffer that
  722. // does not read all of the files at once.
  723. cmSystemTools::RemoveFile(toFile.c_str());
  724. if(!cmSystemTools::CopyFileAlways(fromFile.c_str(), toFile.c_str()))
  725. {
  726. cmOStringStream e;
  727. e << "INSTALL cannot copy file \"" << fromFile
  728. << "\" to \"" << toFile + "\".";
  729. this->SetError(e.str().c_str());
  730. return false;
  731. }
  732. // Perform post-installation processing on the file depending
  733. // on its type.
  734. #if defined(__APPLE_CC__)
  735. // Static libraries need ranlib on this platform.
  736. if(itype == cmTarget::STATIC_LIBRARY)
  737. {
  738. std::string ranlib = "ranlib ";
  739. ranlib += cmSystemTools::ConvertToOutputPath(toFile.c_str());
  740. if(!cmSystemTools::RunSingleCommand(ranlib.c_str()))
  741. {
  742. std::string err = "ranlib failed: ";
  743. err += ranlib;
  744. this->SetError(err.c_str());
  745. return false;
  746. }
  747. }
  748. #endif
  749. // Set permissions of the destination file.
  750. if(!cmSystemTools::SetPermissions(toFile.c_str(), permissions))
  751. {
  752. cmOStringStream e;
  753. e << "Problem setting permissions on file \""
  754. << toFile.c_str() << "\"";
  755. this->SetError(e.str().c_str());
  756. return false;
  757. }
  758. // Add the file to the manifest.
  759. smanifest_files += ";";
  760. smanifest_files += toFile.substr(destDirLength);
  761. }
  762. else if(!optional)
  763. {
  764. // The input file does not exist and installation is not optional.
  765. cmOStringStream e;
  766. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  767. this->SetError(e.str().c_str());
  768. return false;
  769. }
  770. }
  771. }
  772. // Save the updated install manifest.
  773. this->Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  774. smanifest_files.c_str());
  775. return true;
  776. }
  777. //----------------------------------------------------------------------------
  778. bool cmFileCommand::HandleRelativePathCommand(
  779. std::vector<std::string> const& args)
  780. {
  781. if(args.size() != 4 )
  782. {
  783. this->SetError("called with incorrect number of arguments");
  784. return false;
  785. }
  786. const std::string& outVar = args[1];
  787. const std::string& directoryName = args[2];
  788. const std::string& fileName = args[3];
  789. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  790. {
  791. std::string errstring = "RelativePath must be passed a full path to the directory: " + directoryName;
  792. this->SetError(errstring.c_str());
  793. return false;
  794. }
  795. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  796. {
  797. std::string errstring = "RelativePath must be passed a full path to the directory: " + directoryName;
  798. this->SetError(errstring.c_str());
  799. return false;
  800. }
  801. std::string res = cmSystemTools::RelativePath(directoryName.c_str(), fileName.c_str());
  802. this->Makefile->AddDefinition(outVar.c_str(),
  803. res.c_str());
  804. return true;
  805. }
  806. //----------------------------------------------------------------------------
  807. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  808. bool recurse)
  809. {
  810. std::string message;
  811. std::vector<std::string>::const_iterator i = args.begin();
  812. i++; // Get rid of subcommand
  813. for(;i != args.end(); ++i)
  814. {
  815. if(cmSystemTools::FileIsDirectory(i->c_str()) && recurse)
  816. {
  817. cmSystemTools::RemoveADirectory(i->c_str());
  818. }
  819. else
  820. {
  821. cmSystemTools::RemoveFile(i->c_str());
  822. }
  823. }
  824. return true;
  825. }
  826. //----------------------------------------------------------------------------
  827. bool cmFileCommand::HandleSystemPathCommand(std::vector<std::string>
  828. const& args)
  829. {
  830. std::vector<std::string>::const_iterator i = args.begin();
  831. if(args.size() != 3)
  832. {
  833. this->SetError("FILE(SYSTEM_PATH ENV result) must be called with "
  834. "only three arguments.");
  835. return false;
  836. }
  837. i++; // Get rid of subcommand
  838. std::vector<std::string> path;
  839. cmSystemTools::GetPath(path, i->c_str());
  840. i++;
  841. const char* var = i->c_str();
  842. std::string value;
  843. for(std::vector<std::string>::iterator j = path.begin();
  844. j != path.end(); ++j)
  845. {
  846. value += *j;
  847. value += ";";
  848. }
  849. this->Makefile->AddDefinition(var, value.c_str());
  850. return true;
  851. }