cmInstallCommand.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 "cmInstallCommand.h"
  14. #include "cmInstallFilesGenerator.h"
  15. #include "cmInstallScriptGenerator.h"
  16. #include "cmInstallTargetGenerator.h"
  17. // cmInstallCommand
  18. bool cmInstallCommand::InitialPass(std::vector<std::string> const& args)
  19. {
  20. // Allow calling with no arguments so that arguments may be built up
  21. // using a variable that may be left empty.
  22. if(args.empty())
  23. {
  24. return true;
  25. }
  26. // Switch among the command modes.
  27. if(args[0] == "SCRIPT")
  28. {
  29. return this->HandleScriptMode(args);
  30. }
  31. else if(args[0] == "CODE")
  32. {
  33. return this->HandleScriptMode(args);
  34. }
  35. else if(args[0] == "TARGETS")
  36. {
  37. return this->HandleTargetsMode(args);
  38. }
  39. else if(args[0] == "FILES")
  40. {
  41. return this->HandleFilesMode(args);
  42. }
  43. else if(args[0] == "PROGRAMS")
  44. {
  45. return this->HandleFilesMode(args);
  46. }
  47. // Unknown mode.
  48. cmStdString e = "called with unknown mode ";
  49. e += args[0];
  50. this->SetError(e.c_str());
  51. return false;
  52. }
  53. //----------------------------------------------------------------------------
  54. bool cmInstallCommand::HandleScriptMode(std::vector<std::string> const& args)
  55. {
  56. bool doing_script = false;
  57. bool doing_code = false;
  58. for(size_t i=0; i < args.size(); ++i)
  59. {
  60. if(args[i] == "SCRIPT")
  61. {
  62. doing_script = true;
  63. doing_code = false;
  64. }
  65. else if(args[i] == "CODE")
  66. {
  67. doing_script = false;
  68. doing_code = true;
  69. }
  70. else if(doing_script)
  71. {
  72. doing_script = false;
  73. std::string script = args[i];
  74. if(!cmSystemTools::FileIsFullPath(script.c_str()))
  75. {
  76. script = this->Makefile->GetCurrentDirectory();
  77. script += "/";
  78. script += args[i];
  79. }
  80. if(cmSystemTools::FileIsDirectory(script.c_str()))
  81. {
  82. this->SetError("given a directory as value of SCRIPT argument.");
  83. return false;
  84. }
  85. this->Makefile->AddInstallGenerator(
  86. new cmInstallScriptGenerator(script.c_str()));
  87. }
  88. else if(doing_code)
  89. {
  90. doing_code = false;
  91. std::string code = args[i];
  92. this->Makefile->AddInstallGenerator(
  93. new cmInstallScriptGenerator(code.c_str(), true));
  94. }
  95. }
  96. if(doing_script)
  97. {
  98. this->SetError("given no value for SCRIPT argument.");
  99. return false;
  100. }
  101. if(doing_code)
  102. {
  103. this->SetError("given no value for CODE argument.");
  104. return false;
  105. }
  106. return true;
  107. }
  108. //----------------------------------------------------------------------------
  109. bool cmInstallCommand::HandleTargetsMode(std::vector<std::string> const& args)
  110. {
  111. // This is the TARGETS mode.
  112. bool doing_targets = true;
  113. bool doing_destination = false;
  114. bool doing_permissions = false;
  115. bool doing_component = false;
  116. bool archive_settings = true;
  117. bool library_settings = true;
  118. bool runtime_settings = true;
  119. std::vector<cmTarget*> targets;
  120. const char* archive_destination = 0;
  121. const char* library_destination = 0;
  122. const char* runtime_destination = 0;
  123. std::string archive_permissions;
  124. std::string library_permissions;
  125. std::string runtime_permissions;
  126. std::string archive_component;
  127. std::string library_component;
  128. std::string runtime_component;
  129. for(unsigned int i=1; i < args.size(); ++i)
  130. {
  131. if(args[i] == "DESTINATION")
  132. {
  133. // Switch to setting the destination property.
  134. doing_targets = false;
  135. doing_destination = true;
  136. doing_permissions = false;
  137. doing_component = false;
  138. }
  139. else if(args[i] == "PERMISSIONS")
  140. {
  141. // Switch to setting the permissions property.
  142. doing_targets = false;
  143. doing_destination = false;
  144. doing_permissions = true;
  145. doing_component = false;
  146. }
  147. else if(args[i] == "COMPONENT")
  148. {
  149. // Switch to setting the component property.
  150. doing_targets = false;
  151. doing_destination = false;
  152. doing_permissions = false;
  153. doing_component = true;
  154. }
  155. else if(args[i] == "ARCHIVE")
  156. {
  157. // Switch to setting only archive properties.
  158. doing_targets = false;
  159. doing_destination = false;
  160. doing_permissions = false;
  161. doing_component = false;
  162. archive_settings = true;
  163. library_settings = false;
  164. runtime_settings = false;
  165. }
  166. else if(args[i] == "LIBRARY")
  167. {
  168. // Switch to setting only library properties.
  169. doing_targets = false;
  170. doing_destination = false;
  171. doing_permissions = false;
  172. doing_component = false;
  173. archive_settings = false;
  174. library_settings = true;
  175. runtime_settings = false;
  176. }
  177. else if(args[i] == "RUNTIME")
  178. {
  179. // Switch to setting only runtime properties.
  180. doing_targets = false;
  181. doing_destination = false;
  182. doing_permissions = false;
  183. doing_component = false;
  184. archive_settings = false;
  185. library_settings = false;
  186. runtime_settings = true;
  187. }
  188. else if(doing_targets)
  189. {
  190. // Lookup this target in the current directory.
  191. if(cmTarget* target = this->Makefile->FindTarget(args[i].c_str()))
  192. {
  193. // Found the target. Check its type.
  194. if(target->GetType() != cmTarget::EXECUTABLE &&
  195. target->GetType() != cmTarget::STATIC_LIBRARY &&
  196. target->GetType() != cmTarget::SHARED_LIBRARY &&
  197. target->GetType() != cmTarget::MODULE_LIBRARY)
  198. {
  199. cmOStringStream e;
  200. e << "TARGETS given target \"" << args[i]
  201. << "\" which is not an executable, library, or module.";
  202. this->SetError(e.str().c_str());
  203. return false;
  204. }
  205. // Store the target in the list to be installed.
  206. targets.push_back(target);
  207. }
  208. else
  209. {
  210. // Did not find the target.
  211. cmOStringStream e;
  212. e << "TARGETS given target \"" << args[i]
  213. << "\" which does not exist in this directory.";
  214. this->SetError(e.str().c_str());
  215. return false;
  216. }
  217. }
  218. else if(doing_destination)
  219. {
  220. // Set the destination in the active set(s) of properties.
  221. if(archive_settings)
  222. {
  223. archive_destination = args[i].c_str();
  224. }
  225. if(library_settings)
  226. {
  227. library_destination = args[i].c_str();
  228. }
  229. if(runtime_settings)
  230. {
  231. runtime_destination = args[i].c_str();
  232. }
  233. doing_destination = false;
  234. }
  235. else if(doing_component)
  236. {
  237. // Set the component in the active set(s) of properties.
  238. if(archive_settings)
  239. {
  240. archive_component = args[i];
  241. }
  242. if(library_settings)
  243. {
  244. library_component = args[i];
  245. }
  246. if(runtime_settings)
  247. {
  248. runtime_component = args[i];
  249. }
  250. doing_component = false;
  251. }
  252. else if(doing_permissions)
  253. {
  254. // Set the permissions in the active set(s) of properties.
  255. if(archive_settings)
  256. {
  257. // Check the requested permission.
  258. if(!this->CheckPermissions(args[i], archive_permissions))
  259. {
  260. cmOStringStream e;
  261. e << args[0] << " given invalid permission \""
  262. << args[i] << "\".";
  263. this->SetError(e.str().c_str());
  264. return false;
  265. }
  266. }
  267. if(library_settings)
  268. {
  269. // Check the requested permission.
  270. if(!this->CheckPermissions(args[i], library_permissions))
  271. {
  272. cmOStringStream e;
  273. e << args[0] << " given invalid permission \""
  274. << args[i] << "\".";
  275. this->SetError(e.str().c_str());
  276. return false;
  277. }
  278. }
  279. if(runtime_settings)
  280. {
  281. // Check the requested permission.
  282. if(!this->CheckPermissions(args[i], runtime_permissions))
  283. {
  284. cmOStringStream e;
  285. e << args[0] << " given invalid permission \""
  286. << args[i] << "\".";
  287. this->SetError(e.str().c_str());
  288. return false;
  289. }
  290. }
  291. }
  292. else
  293. {
  294. // Unknown argument.
  295. cmOStringStream e;
  296. e << "TARGETS given unknown argument \"" << args[i] << "\".";
  297. this->SetError(e.str().c_str());
  298. return false;
  299. }
  300. }
  301. // Check if there is something to do.
  302. if(targets.empty())
  303. {
  304. return true;
  305. }
  306. if(!archive_destination && !library_destination && !runtime_destination)
  307. {
  308. this->SetError("TARGETS given no DESTINATION!");
  309. return false;
  310. }
  311. // Compute destination paths.
  312. std::string archive_dest;
  313. std::string library_dest;
  314. std::string runtime_dest;
  315. this->ComputeDestination(archive_destination, archive_dest);
  316. this->ComputeDestination(library_destination, library_dest);
  317. this->ComputeDestination(runtime_destination, runtime_dest);
  318. // Generate install script code to install the given targets.
  319. for(std::vector<cmTarget*>::iterator ti = targets.begin();
  320. ti != targets.end(); ++ti)
  321. {
  322. // Handle each target type.
  323. cmTarget& target = *(*ti);
  324. switch(target.GetType())
  325. {
  326. case cmTarget::SHARED_LIBRARY:
  327. {
  328. // Shared libraries are handled differently on DLL and non-DLL
  329. // platforms. All windows platforms are DLL platforms
  330. // including cygwin. Currently no other platform is a DLL
  331. // platform.
  332. #if defined(_WIN32) || defined(__CYGWIN__)
  333. // This is a DLL platform.
  334. if(archive_destination)
  335. {
  336. // The import library uses the ARCHIVE properties.
  337. this->Makefile->AddInstallGenerator(
  338. new cmInstallTargetGenerator(target, archive_dest.c_str(), true,
  339. archive_permissions.c_str(),
  340. archive_component.c_str()));
  341. }
  342. if(runtime_destination)
  343. {
  344. // The DLL uses the RUNTIME properties.
  345. this->Makefile->AddInstallGenerator(
  346. new cmInstallTargetGenerator(target, runtime_dest.c_str(), false,
  347. runtime_permissions.c_str(),
  348. runtime_component.c_str()));
  349. }
  350. #else
  351. // This is a non-DLL platform.
  352. if(library_destination)
  353. {
  354. // The shared library uses the LIBRARY properties.
  355. this->Makefile->AddInstallGenerator(
  356. new cmInstallTargetGenerator(target, library_dest.c_str(), false,
  357. library_permissions.c_str(),
  358. library_component.c_str()));
  359. }
  360. else
  361. {
  362. cmOStringStream e;
  363. e << "TARGETS given no LIBRARY DESTINATION for shared library "
  364. "target \"" << target.GetName() << "\".";
  365. this->SetError(e.str().c_str());
  366. return false;
  367. }
  368. #endif
  369. }
  370. break;
  371. case cmTarget::STATIC_LIBRARY:
  372. {
  373. // Static libraries use ARCHIVE properties.
  374. if(archive_destination)
  375. {
  376. this->Makefile->AddInstallGenerator(
  377. new cmInstallTargetGenerator(target, archive_dest.c_str(), false,
  378. archive_permissions.c_str(),
  379. archive_component.c_str()));
  380. }
  381. else
  382. {
  383. cmOStringStream e;
  384. e << "TARGETS given no ARCHIVE DESTINATION for static library "
  385. "target \"" << target.GetName() << "\".";
  386. this->SetError(e.str().c_str());
  387. return false;
  388. }
  389. }
  390. break;
  391. case cmTarget::MODULE_LIBRARY:
  392. {
  393. // Modules use LIBRARY properties.
  394. if(library_destination)
  395. {
  396. this->Makefile->AddInstallGenerator(
  397. new cmInstallTargetGenerator(target, library_dest.c_str(), false,
  398. library_permissions.c_str(),
  399. library_component.c_str()));
  400. }
  401. else
  402. {
  403. cmOStringStream e;
  404. e << "TARGETS given no LIBRARY DESTINATION for module target \""
  405. << target.GetName() << "\".";
  406. this->SetError(e.str().c_str());
  407. return false;
  408. }
  409. }
  410. break;
  411. case cmTarget::EXECUTABLE:
  412. {
  413. // Executables use the RUNTIME properties.
  414. if(runtime_destination)
  415. {
  416. this->Makefile->AddInstallGenerator(
  417. new cmInstallTargetGenerator(target, runtime_dest.c_str(), false,
  418. runtime_permissions.c_str(),
  419. runtime_component.c_str()));
  420. }
  421. else
  422. {
  423. cmOStringStream e;
  424. e << "TARGETS given no RUNTIME DESTINATION for executable target \""
  425. << target.GetName() << "\".";
  426. this->SetError(e.str().c_str());
  427. return false;
  428. }
  429. }
  430. break;
  431. default:
  432. // This should never happen due to the above type check.
  433. // Ignore the case.
  434. break;
  435. }
  436. }
  437. // Tell the global generator about any installation component names
  438. // specified.
  439. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  440. ->AddInstallComponent(archive_component.c_str());
  441. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  442. ->AddInstallComponent(library_component.c_str());
  443. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  444. ->AddInstallComponent(runtime_component.c_str());
  445. return true;
  446. }
  447. //----------------------------------------------------------------------------
  448. bool cmInstallCommand::HandleFilesMode(std::vector<std::string> const& args)
  449. {
  450. // This is the FILES mode.
  451. bool programs = (args[0] == "PROGRAMS");
  452. bool doing_files = true;
  453. bool doing_destination = false;
  454. bool doing_permissions = false;
  455. bool doing_component = false;
  456. bool doing_rename = false;
  457. std::vector<std::string> files;
  458. const char* destination = 0;
  459. std::string rename;
  460. std::string permissions;
  461. std::string component;
  462. for(unsigned int i=1; i < args.size(); ++i)
  463. {
  464. if(args[i] == "DESTINATION")
  465. {
  466. // Switch to setting the destination property.
  467. doing_files = false;
  468. doing_destination = true;
  469. doing_permissions = false;
  470. doing_component = false;
  471. doing_rename = false;
  472. }
  473. else if(args[i] == "PERMISSIONS")
  474. {
  475. // Switch to setting the permissions property.
  476. doing_files = false;
  477. doing_destination = false;
  478. doing_permissions = true;
  479. doing_component = false;
  480. doing_rename = false;
  481. }
  482. else if(args[i] == "COMPONENT")
  483. {
  484. // Switch to setting the component property.
  485. doing_files = false;
  486. doing_destination = false;
  487. doing_permissions = false;
  488. doing_component = true;
  489. doing_rename = false;
  490. }
  491. else if(args[i] == "RENAME")
  492. {
  493. // Switch to setting the rename property.
  494. doing_files = false;
  495. doing_destination = false;
  496. doing_permissions = false;
  497. doing_component = false;
  498. doing_rename = true;
  499. }
  500. else if(doing_files)
  501. {
  502. // Convert this file to a full path.
  503. std::string file = args[i];
  504. if(!cmSystemTools::FileIsFullPath(file.c_str()))
  505. {
  506. file = this->Makefile->GetCurrentDirectory();
  507. file += "/";
  508. file += args[i];
  509. }
  510. // Make sure the file is not a directory.
  511. if(cmSystemTools::FileIsDirectory(file.c_str()))
  512. {
  513. cmOStringStream e;
  514. e << args[0] << " given directory \"" << args[i] << "\" to install.";
  515. this->SetError(e.str().c_str());
  516. return false;
  517. }
  518. // Store the file for installation.
  519. files.push_back(file);
  520. }
  521. else if(doing_destination)
  522. {
  523. destination = args[i].c_str();
  524. doing_destination = false;
  525. }
  526. else if(doing_component)
  527. {
  528. component = args[i];
  529. doing_component = false;
  530. }
  531. else if(doing_permissions)
  532. {
  533. // Check the requested permission.
  534. if(!this->CheckPermissions(args[i], permissions))
  535. {
  536. cmOStringStream e;
  537. e << args[0] << " given invalid permission \""
  538. << args[i] << "\".";
  539. this->SetError(e.str().c_str());
  540. return false;
  541. }
  542. }
  543. else if(doing_rename)
  544. {
  545. rename = args[i];
  546. doing_rename = false;
  547. }
  548. else
  549. {
  550. // Unknown argument.
  551. cmOStringStream e;
  552. e << args[0] << " given unknown argument \"" << args[i] << "\".";
  553. this->SetError(e.str().c_str());
  554. return false;
  555. }
  556. }
  557. // Check if there is something to do.
  558. if(files.empty())
  559. {
  560. return true;
  561. }
  562. if(!destination)
  563. {
  564. // A destination is required.
  565. cmOStringStream e;
  566. e << args[0] << " given no DESTINATION!";
  567. this->SetError(e.str().c_str());
  568. return false;
  569. }
  570. if(!rename.empty() && files.size() > 1)
  571. {
  572. // The rename option works only with one file.
  573. cmOStringStream e;
  574. e << args[0] << " given RENAME option with more than one file.";
  575. this->SetError(e.str().c_str());
  576. return false;
  577. }
  578. // Compute destination path.
  579. std::string dest;
  580. this->ComputeDestination(destination, dest);
  581. // Create the files install generator.
  582. this->Makefile->AddInstallGenerator(
  583. new cmInstallFilesGenerator(files, dest.c_str(), programs,
  584. permissions.c_str(), component.c_str(),
  585. rename.c_str()));
  586. // Tell the global generator about any installation component names
  587. // specified.
  588. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  589. ->AddInstallComponent(component.c_str());
  590. return true;
  591. }
  592. //----------------------------------------------------------------------------
  593. void cmInstallCommand::ComputeDestination(const char* destination,
  594. std::string& dest)
  595. {
  596. if(destination)
  597. {
  598. if(cmSystemTools::FileIsFullPath(destination))
  599. {
  600. // Full paths are absolute.
  601. dest = destination;
  602. }
  603. else
  604. {
  605. // Relative paths are treated with respect to the installation prefix.
  606. dest = "${CMAKE_INSTALL_PREFIX}/";
  607. dest += destination;
  608. }
  609. // Format the path nicely. Note this also removes trailing
  610. // slashes.
  611. cmSystemTools::ConvertToUnixSlashes(dest);
  612. }
  613. else
  614. {
  615. dest = "";
  616. }
  617. }
  618. //----------------------------------------------------------------------------
  619. bool cmInstallCommand::CheckPermissions(std::string const& arg,
  620. std::string& permissions)
  621. {
  622. // Table of valid permissions.
  623. const char* table[] =
  624. {
  625. "OWNER_READ", "OWNER_WRITE", "OWNER_EXECUTE",
  626. "GROUP_READ", "GROUP_WRITE", "GROUP_EXECUTE",
  627. "WORLD_READ", "WORLD_WRITE", "WORLD_EXECUTE",
  628. "SETUID", "SETGID", 0
  629. };
  630. // Check the permission against the table.
  631. for(const char** valid = table; *valid; ++valid)
  632. {
  633. if(arg == *valid)
  634. {
  635. // This is a valid permission.
  636. permissions += " ";
  637. permissions += arg;
  638. return true;
  639. }
  640. }
  641. // This is not a valid permission.
  642. return false;
  643. }