packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cmInstallGenerator.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "cmInstallGenerator.h"
13
14 #include "cmSystemTools.h"
15
16 //----------------------------------------------------------------------------
17 cmInstallGenerator
18 ::cmInstallGenerator(const char* destination,
19                      std::vector<std::string> const& configurations,
20                      const char* component):
21   cmScriptGenerator("CMAKE_INSTALL_CONFIG_NAME", configurations),
22   Destination(destination? destination:""),
23   Component(component? component:"")
24 {
25 }
26
27 //----------------------------------------------------------------------------
28 cmInstallGenerator
29 ::~cmInstallGenerator()
30 {
31 }
32
33 //----------------------------------------------------------------------------
34 void cmInstallGenerator
35 ::AddInstallRule(
36                  std::ostream& os,
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   std::string dest = this->GetInstallDestination();
61   if (cmSystemTools::FileIsFullPath(dest.c_str()))
62      {
63      os << "list(APPEND CMAKE_ABSOLUTE_DESTINATION_FILES\n";
64      os << indent << " \"";
65      for(std::vector<std::string>::const_iterator fi = files.begin();
66                fi != files.end(); ++fi)
67              {
68                if (fi!=files.begin())
69                  {
70                  os << ";";
71                  }
72                os << dest << "/";
73                if (rename && *rename)
74                  {
75                  os << rename;
76                  }
77                else
78                  {
79                  os << cmSystemTools::GetFilenameName(*fi);
80                  }
81              }
82      os << "\")\n";
83      os << indent << "IF (CMAKE_WARN_ON_ABSOLUTE_INSTALL_DESTINATION)\n";
84      os << indent << indent << "message(WARNING \"ABSOLUTE path INSTALL "
85         << "DESTINATION : ${CMAKE_ABSOLUTE_DESTINATION_FILES}\")\n";
86      os << indent << "ENDIF (CMAKE_WARN_ON_ABSOLUTE_INSTALL_DESTINATION)\n";
87
88      os << indent << "IF (CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION)\n";
89      os << indent << indent << "message(FATAL_ERROR \"ABSOLUTE path INSTALL "
90         << "DESTINATION forbidden (by caller): "
91         << "${CMAKE_ABSOLUTE_DESTINATION_FILES}\")\n";
92      os << indent << "ENDIF (CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION)\n";
93      }
94   os << "FILE(INSTALL DESTINATION \"" << dest << "\" TYPE " << stype.c_str();
95   if(optional)
96     {
97     os << " OPTIONAL";
98     }
99   if(permissions_file && *permissions_file)
100     {
101     os << " PERMISSIONS" << permissions_file;
102     }
103   if(permissions_dir && *permissions_dir)
104     {
105     os << " DIR_PERMISSIONS" << permissions_dir;
106     }
107   if(rename && *rename)
108     {
109     os << " RENAME \"" << rename << "\"";
110     }
111   os << " FILES";
112   if(files.size() == 1)
113     {
114     os << " \"" << files[0] << "\"";
115     }
116   else
117     {
118     for(std::vector<std::string>::const_iterator fi = files.begin();
119         fi != files.end(); ++fi)
120       {
121       os << "\n" << indent << "  \"" << *fi << "\"";
122       }
123     os << "\n" << indent << " ";
124     if(!(literal_args && *literal_args))
125       {
126       os << " ";
127       }
128     }
129   if(literal_args && *literal_args)
130     {
131     os << literal_args;
132     }
133   os << ")\n";
134 }
135
136 //----------------------------------------------------------------------------
137 std::string
138 cmInstallGenerator::CreateComponentTest(const char* component)
139 {
140   std::string result = "NOT CMAKE_INSTALL_COMPONENT OR "
141     "\"${CMAKE_INSTALL_COMPONENT}\" STREQUAL \"";
142   result += component;
143   result += "\"";
144   return result;
145 }
146
147 //----------------------------------------------------------------------------
148 void cmInstallGenerator::GenerateScript(std::ostream& os)
149 {
150   // Track indentation.
151   Indent indent;
152
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
158   // Generate the script possibly with per-configuration code.
159   this->GenerateScriptConfigs(os, indent.Next());
160
161   // End this block of installation.
162   os << indent << "ENDIF(" << component_test << ")\n\n";
163 }
164
165 //----------------------------------------------------------------------------
166 bool cmInstallGenerator::InstallsForConfig(const char* config)
167 {
168   return this->GeneratesForConfig(config);
169 }
170
171 //----------------------------------------------------------------------------
172 std::string cmInstallGenerator::GetInstallDestination() const
173 {
174   std::string result;
175   if(!this->Destination.empty() &&
176      !cmSystemTools::FileIsFullPath(this->Destination.c_str()))
177     {
178     result = "${CMAKE_INSTALL_PREFIX}/";
179     }
180   result += this->Destination;
181   return result;
182 }