cmInstallGenerator.cxx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmInstallGenerator.h"
  4. #include "cmMakefile.h"
  5. #include "cmSystemTools.h"
  6. #include <ostream>
  7. cmInstallGenerator::cmInstallGenerator(
  8. const char* destination, std::vector<std::string> const& configurations,
  9. const char* component, MessageLevel message, bool exclude_from_all)
  10. : cmScriptGenerator("CMAKE_INSTALL_CONFIG_NAME", configurations)
  11. , Destination(destination ? destination : "")
  12. , Component(component ? component : "")
  13. , Message(message)
  14. , ExcludeFromAll(exclude_from_all)
  15. {
  16. }
  17. cmInstallGenerator::~cmInstallGenerator()
  18. {
  19. }
  20. bool cmInstallGenerator::HaveInstall()
  21. {
  22. return true;
  23. }
  24. void cmInstallGenerator::CheckCMP0082(bool& haveSubdirectoryInstall,
  25. bool& haveInstallAfterSubdirectory)
  26. {
  27. if (haveSubdirectoryInstall) {
  28. haveInstallAfterSubdirectory = true;
  29. }
  30. }
  31. void cmInstallGenerator::AddInstallRule(
  32. std::ostream& os, std::string const& dest, cmInstallType type,
  33. std::vector<std::string> const& files, bool optional /* = false */,
  34. const char* permissions_file /* = 0 */,
  35. const char* permissions_dir /* = 0 */, const char* rename /* = 0 */,
  36. const char* literal_args /* = 0 */, Indent indent)
  37. {
  38. // Use the FILE command to install the file.
  39. std::string stype;
  40. switch (type) {
  41. case cmInstallType_DIRECTORY:
  42. stype = "DIRECTORY";
  43. break;
  44. case cmInstallType_PROGRAMS:
  45. stype = "PROGRAM";
  46. break;
  47. case cmInstallType_EXECUTABLE:
  48. stype = "EXECUTABLE";
  49. break;
  50. case cmInstallType_STATIC_LIBRARY:
  51. stype = "STATIC_LIBRARY";
  52. break;
  53. case cmInstallType_SHARED_LIBRARY:
  54. stype = "SHARED_LIBRARY";
  55. break;
  56. case cmInstallType_MODULE_LIBRARY:
  57. stype = "MODULE";
  58. break;
  59. case cmInstallType_FILES:
  60. stype = "FILE";
  61. break;
  62. }
  63. os << indent;
  64. if (cmSystemTools::FileIsFullPath(dest)) {
  65. os << "list(APPEND CMAKE_ABSOLUTE_DESTINATION_FILES\n";
  66. os << indent << " \"";
  67. for (std::vector<std::string>::const_iterator fi = files.begin();
  68. fi != files.end(); ++fi) {
  69. if (fi != files.begin()) {
  70. os << ";";
  71. }
  72. os << dest << "/";
  73. if (rename && *rename) {
  74. os << rename;
  75. } else {
  76. os << cmSystemTools::GetFilenameName(*fi);
  77. }
  78. }
  79. os << "\")\n";
  80. os << indent << "if(CMAKE_WARN_ON_ABSOLUTE_INSTALL_DESTINATION)\n";
  81. os << indent << indent << "message(WARNING \"ABSOLUTE path INSTALL "
  82. << "DESTINATION : ${CMAKE_ABSOLUTE_DESTINATION_FILES}\")\n";
  83. os << indent << "endif()\n";
  84. os << indent << "if(CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION)\n";
  85. os << indent << indent << "message(FATAL_ERROR \"ABSOLUTE path INSTALL "
  86. << "DESTINATION forbidden (by caller): "
  87. << "${CMAKE_ABSOLUTE_DESTINATION_FILES}\")\n";
  88. os << indent << "endif()\n";
  89. }
  90. std::string absDest = this->ConvertToAbsoluteDestination(dest);
  91. os << "file(INSTALL DESTINATION \"" << absDest << "\" TYPE " << stype;
  92. if (optional) {
  93. os << " OPTIONAL";
  94. }
  95. switch (this->Message) {
  96. case MessageDefault:
  97. break;
  98. case MessageAlways:
  99. os << " MESSAGE_ALWAYS";
  100. break;
  101. case MessageLazy:
  102. os << " MESSAGE_LAZY";
  103. break;
  104. case MessageNever:
  105. os << " MESSAGE_NEVER";
  106. break;
  107. }
  108. if (permissions_file && *permissions_file) {
  109. os << " PERMISSIONS" << permissions_file;
  110. }
  111. if (permissions_dir && *permissions_dir) {
  112. os << " DIR_PERMISSIONS" << permissions_dir;
  113. }
  114. if (rename && *rename) {
  115. os << " RENAME \"" << rename << "\"";
  116. }
  117. os << " FILES";
  118. if (files.size() == 1) {
  119. os << " \"" << files[0] << "\"";
  120. } else {
  121. for (std::string const& f : files) {
  122. os << "\n" << indent << " \"" << f << "\"";
  123. }
  124. os << "\n" << indent << " ";
  125. if (!(literal_args && *literal_args)) {
  126. os << " ";
  127. }
  128. }
  129. if (literal_args && *literal_args) {
  130. os << literal_args;
  131. }
  132. os << ")\n";
  133. }
  134. std::string cmInstallGenerator::CreateComponentTest(const char* component,
  135. bool exclude_from_all)
  136. {
  137. std::string result = "\"x${CMAKE_INSTALL_COMPONENT}x\" STREQUAL \"x";
  138. result += component;
  139. result += "x\"";
  140. if (!exclude_from_all) {
  141. result += " OR NOT CMAKE_INSTALL_COMPONENT";
  142. }
  143. return result;
  144. }
  145. void cmInstallGenerator::GenerateScript(std::ostream& os)
  146. {
  147. // Track indentation.
  148. Indent indent;
  149. // Begin this block of installation.
  150. std::string component_test =
  151. this->CreateComponentTest(this->Component.c_str(), this->ExcludeFromAll);
  152. os << indent << "if(" << component_test << ")\n";
  153. // Generate the script possibly with per-configuration code.
  154. this->GenerateScriptConfigs(os, indent.Next());
  155. // End this block of installation.
  156. os << indent << "endif()\n\n";
  157. }
  158. bool cmInstallGenerator::InstallsForConfig(const std::string& config)
  159. {
  160. return this->GeneratesForConfig(config);
  161. }
  162. std::string cmInstallGenerator::ConvertToAbsoluteDestination(
  163. std::string const& dest) const
  164. {
  165. std::string result;
  166. if (!dest.empty() && !cmSystemTools::FileIsFullPath(dest)) {
  167. result = "${CMAKE_INSTALL_PREFIX}/";
  168. }
  169. result += dest;
  170. return result;
  171. }
  172. cmInstallGenerator::MessageLevel cmInstallGenerator::SelectMessageLevel(
  173. cmMakefile* mf, bool never)
  174. {
  175. if (never) {
  176. return MessageNever;
  177. }
  178. std::string m = mf->GetSafeDefinition("CMAKE_INSTALL_MESSAGE");
  179. if (m == "ALWAYS") {
  180. return MessageAlways;
  181. }
  182. if (m == "LAZY") {
  183. return MessageLazy;
  184. }
  185. if (m == "NEVER") {
  186. return MessageNever;
  187. }
  188. return MessageDefault;
  189. }