cmFileCommand.cxx 29 KB

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