cmInstallGenerator.cxx 5.6 KB

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