cmInstallGenerator.cxx 6.6 KB

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