cmInstallGenerator.cxx 6.8 KB

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