cmInstallGenerator.cxx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "cmSystemTools.h"
  12. //----------------------------------------------------------------------------
  13. cmInstallGenerator
  14. ::cmInstallGenerator(const char* destination,
  15. std::vector<std::string> const& configurations,
  16. const char* component,
  17. MessageLevel message):
  18. cmScriptGenerator("CMAKE_INSTALL_CONFIG_NAME", configurations),
  19. Destination(destination? destination:""),
  20. Component(component? component:""),
  21. Message(message)
  22. {
  23. }
  24. //----------------------------------------------------------------------------
  25. cmInstallGenerator
  26. ::~cmInstallGenerator()
  27. {
  28. }
  29. //----------------------------------------------------------------------------
  30. void cmInstallGenerator
  31. ::AddInstallRule(
  32. std::ostream& os,
  33. cmInstallType type,
  34. std::vector<std::string> const& files,
  35. bool optional /* = false */,
  36. const char* permissions_file /* = 0 */,
  37. const char* permissions_dir /* = 0 */,
  38. const char* rename /* = 0 */,
  39. const char* literal_args /* = 0 */,
  40. Indent const& indent
  41. )
  42. {
  43. // Use the FILE command to install the file.
  44. std::string stype;
  45. switch(type)
  46. {
  47. case cmInstallType_DIRECTORY: stype = "DIRECTORY"; break;
  48. case cmInstallType_PROGRAMS: stype = "PROGRAM"; break;
  49. case cmInstallType_EXECUTABLE: stype = "EXECUTABLE"; break;
  50. case cmInstallType_STATIC_LIBRARY: stype = "STATIC_LIBRARY"; break;
  51. case cmInstallType_SHARED_LIBRARY: stype = "SHARED_LIBRARY"; break;
  52. case cmInstallType_MODULE_LIBRARY: stype = "MODULE"; break;
  53. case cmInstallType_FILES: stype = "FILE"; break;
  54. }
  55. os << indent;
  56. std::string dest = this->GetInstallDestination();
  57. if (cmSystemTools::FileIsFullPath(dest.c_str()))
  58. {
  59. os << "list(APPEND CMAKE_ABSOLUTE_DESTINATION_FILES\n";
  60. os << indent << " \"";
  61. for(std::vector<std::string>::const_iterator fi = files.begin();
  62. fi != files.end(); ++fi)
  63. {
  64. if (fi!=files.begin())
  65. {
  66. os << ";";
  67. }
  68. os << dest << "/";
  69. if (rename && *rename)
  70. {
  71. os << rename;
  72. }
  73. else
  74. {
  75. os << cmSystemTools::GetFilenameName(*fi);
  76. }
  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. os << "file(INSTALL DESTINATION \"" << dest << "\" TYPE " << stype;
  90. if(optional)
  91. {
  92. os << " OPTIONAL";
  93. }
  94. if(permissions_file && *permissions_file)
  95. {
  96. os << " PERMISSIONS" << permissions_file;
  97. }
  98. if(permissions_dir && *permissions_dir)
  99. {
  100. os << " DIR_PERMISSIONS" << permissions_dir;
  101. }
  102. if(rename && *rename)
  103. {
  104. os << " RENAME \"" << rename << "\"";
  105. }
  106. os << " FILES";
  107. if(files.size() == 1)
  108. {
  109. os << " \"" << files[0] << "\"";
  110. }
  111. else
  112. {
  113. for(std::vector<std::string>::const_iterator fi = files.begin();
  114. fi != files.end(); ++fi)
  115. {
  116. os << "\n" << indent << " \"" << *fi << "\"";
  117. }
  118. os << "\n" << indent << " ";
  119. if(!(literal_args && *literal_args))
  120. {
  121. os << " ";
  122. }
  123. }
  124. if(literal_args && *literal_args)
  125. {
  126. os << literal_args;
  127. }
  128. os << ")\n";
  129. }
  130. //----------------------------------------------------------------------------
  131. std::string
  132. cmInstallGenerator::CreateComponentTest(const char* component)
  133. {
  134. std::string result = "NOT CMAKE_INSTALL_COMPONENT OR "
  135. "\"${CMAKE_INSTALL_COMPONENT}\" STREQUAL \"";
  136. result += component;
  137. result += "\"";
  138. return result;
  139. }
  140. //----------------------------------------------------------------------------
  141. void cmInstallGenerator::GenerateScript(std::ostream& os)
  142. {
  143. // Track indentation.
  144. Indent indent;
  145. // Begin this block of installation.
  146. std::string component_test =
  147. this->CreateComponentTest(this->Component.c_str());
  148. os << indent << "if(" << component_test << ")\n";
  149. // Generate the script possibly with per-configuration code.
  150. this->GenerateScriptConfigs(os, indent.Next());
  151. // End this block of installation.
  152. os << indent << "endif()\n\n";
  153. }
  154. //----------------------------------------------------------------------------
  155. bool cmInstallGenerator::InstallsForConfig(const std::string& config)
  156. {
  157. return this->GeneratesForConfig(config);
  158. }
  159. //----------------------------------------------------------------------------
  160. std::string cmInstallGenerator::GetInstallDestination() const
  161. {
  162. std::string result;
  163. if(!this->Destination.empty() &&
  164. !cmSystemTools::FileIsFullPath(this->Destination.c_str()))
  165. {
  166. result = "${CMAKE_INSTALL_PREFIX}/";
  167. }
  168. result += this->Destination;
  169. return result;
  170. }