cmFileCommand.cxx 26 KB

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