1
0

cmInstallGenerator.cxx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 dest = this->ConvertToAbsoluteDestination(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. os << "file(INSTALL DESTINATION \"" << dest << "\" TYPE " << stype;
  91. if(optional)
  92. {
  93. os << " OPTIONAL";
  94. }
  95. switch(this->Message)
  96. {
  97. case MessageDefault: break;
  98. case MessageAlways: os << " MESSAGE_ALWAYS"; break;
  99. case MessageLazy: os << " MESSAGE_LAZY"; break;
  100. case MessageNever: os << " MESSAGE_NEVER"; break;
  101. }
  102. if(permissions_file && *permissions_file)
  103. {
  104. os << " PERMISSIONS" << permissions_file;
  105. }
  106. if(permissions_dir && *permissions_dir)
  107. {
  108. os << " DIR_PERMISSIONS" << permissions_dir;
  109. }
  110. if(rename && *rename)
  111. {
  112. os << " RENAME \"" << rename << "\"";
  113. }
  114. os << " FILES";
  115. if(files.size() == 1)
  116. {
  117. os << " \"" << files[0] << "\"";
  118. }
  119. else
  120. {
  121. for(std::vector<std::string>::const_iterator fi = files.begin();
  122. fi != files.end(); ++fi)
  123. {
  124. os << "\n" << indent << " \"" << *fi << "\"";
  125. }
  126. os << "\n" << indent << " ";
  127. if(!(literal_args && *literal_args))
  128. {
  129. os << " ";
  130. }
  131. }
  132. if(literal_args && *literal_args)
  133. {
  134. os << literal_args;
  135. }
  136. os << ")\n";
  137. }
  138. //----------------------------------------------------------------------------
  139. std::string
  140. cmInstallGenerator::CreateComponentTest(const char* component)
  141. {
  142. std::string result = "NOT CMAKE_INSTALL_COMPONENT OR "
  143. "\"${CMAKE_INSTALL_COMPONENT}\" STREQUAL \"";
  144. result += component;
  145. result += "\"";
  146. return result;
  147. }
  148. //----------------------------------------------------------------------------
  149. void cmInstallGenerator::GenerateScript(std::ostream& os)
  150. {
  151. // Track indentation.
  152. Indent indent;
  153. // Begin this block of installation.
  154. std::string component_test =
  155. this->CreateComponentTest(this->Component.c_str());
  156. os << indent << "if(" << component_test << ")\n";
  157. // Generate the script possibly with per-configuration code.
  158. this->GenerateScriptConfigs(os, indent.Next());
  159. // End this block of installation.
  160. os << indent << "endif()\n\n";
  161. }
  162. //----------------------------------------------------------------------------
  163. bool cmInstallGenerator::InstallsForConfig(const std::string& config)
  164. {
  165. return this->GeneratesForConfig(config);
  166. }
  167. //----------------------------------------------------------------------------
  168. std::string
  169. cmInstallGenerator::ConvertToAbsoluteDestination(std::string const& dest) const
  170. {
  171. std::string result;
  172. if(!dest.empty() &&
  173. !cmSystemTools::FileIsFullPath(dest.c_str()))
  174. {
  175. result = "${CMAKE_INSTALL_PREFIX}/";
  176. }
  177. result += dest;
  178. return result;
  179. }
  180. //----------------------------------------------------------------------------
  181. cmInstallGenerator::MessageLevel
  182. cmInstallGenerator::SelectMessageLevel(cmMakefile* mf, bool never)
  183. {
  184. if(never)
  185. {
  186. return MessageNever;
  187. }
  188. std::string m = mf->GetSafeDefinition("CMAKE_INSTALL_MESSAGE");
  189. if(m == "ALWAYS")
  190. {
  191. return MessageAlways;
  192. }
  193. if(m == "LAZY")
  194. {
  195. return MessageLazy;
  196. }
  197. if(m == "NEVER")
  198. {
  199. return MessageNever;
  200. }
  201. return MessageDefault;
  202. }