cmInstallGenerator.cxx 5.3 KB

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