cmInstallGenerator.cxx 5.9 KB

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