Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmInstallFilesCommand.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 "cmInstallFilesCommand.h"
13
14 #include "cmInstallFilesGenerator.h"
15
16 // cmExecutableCommand
17 bool cmInstallFilesCommand
18 ::InitialPass(std::vector<std::string> const& argsIn, cmExecutionStatus &)
19 {
20   if(argsIn.size() < 2)
21     {
22     this->SetError("called with incorrect number of arguments");
23     return false;
24     }
25
26   // Enable the install target.
27   this->Makefile->GetLocalGenerator()
28     ->GetGlobalGenerator()->EnableInstallTarget();
29
30   std::vector<std::string> args;
31   this->Makefile->ExpandSourceListArguments(argsIn, args, 2);
32
33   this->Destination = args[0];
34
35   if((args.size() > 1) && (args[1] == "FILES"))
36     {
37     this->IsFilesForm = true;
38     for(std::vector<std::string>::const_iterator s = args.begin()+2;
39         s != args.end(); ++s)
40       {
41       // Find the source location for each file listed.
42       std::string f = this->FindInstallSource(s->c_str());
43       this->Files.push_back(f);
44       }
45     this->CreateInstallGenerator();
46     }
47   else
48     {
49     this->IsFilesForm = false;
50     std::vector<std::string>::const_iterator s = args.begin();
51     for (++s;s != args.end(); ++s)
52       {
53       this->FinalArgs.push_back(*s);
54       }
55     }
56
57   this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
58                       ->AddInstallComponent(this->Makefile->GetSafeDefinition(
59                                       "CMAKE_INSTALL_DEFAULT_COMPONENT_NAME"));
60
61   return true;
62 }
63
64 void cmInstallFilesCommand::FinalPass()
65 {
66   // No final pass for "FILES" form of arguments.
67   if(this->IsFilesForm)
68     {
69     return;
70     }
71
72   std::string testf;
73   std::string ext = this->FinalArgs[0];
74
75   // two different options
76   if (this->FinalArgs.size() > 1)
77     {
78     // now put the files into the list
79     std::vector<std::string>::iterator s = this->FinalArgs.begin();
80     ++s;
81     // for each argument, get the files
82     for (;s != this->FinalArgs.end(); ++s)
83       {
84       // replace any variables
85       std::string temps = *s;
86       if (cmSystemTools::GetFilenamePath(temps).size() > 0)
87         {
88           testf = cmSystemTools::GetFilenamePath(temps) + "/" +
89             cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
90         }
91       else
92         {
93           testf = cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
94         }
95
96       // add to the result
97       this->Files.push_back(this->FindInstallSource(testf.c_str()));
98       }
99     }
100   else     // reg exp list
101     {
102     std::vector<std::string> files;
103     std::string regex = this->FinalArgs[0].c_str();
104     cmSystemTools::Glob(this->Makefile->GetCurrentDirectory(),
105                         regex.c_str(), files);
106
107     std::vector<std::string>::iterator s = files.begin();
108     // for each argument, get the files
109     for (;s != files.end(); ++s)
110       {
111       this->Files.push_back(this->FindInstallSource(s->c_str()));
112       }
113     }
114
115   this->CreateInstallGenerator();
116 }
117
118 void cmInstallFilesCommand::CreateInstallGenerator() const
119 {
120   // Construct the destination.  This command always installs under
121   // the prefix.  We skip the leading slash given by the user.
122   std::string destination = this->Destination.substr(1);
123   cmSystemTools::ConvertToUnixSlashes(destination);
124   if(destination.empty())
125     {
126     destination = ".";
127     }
128
129   // Use a file install generator.
130   const char* no_permissions = "";
131   const char* no_rename = "";
132   std::string no_component = this->Makefile->GetSafeDefinition(
133                                        "CMAKE_INSTALL_DEFAULT_COMPONENT_NAME");
134   std::vector<std::string> no_configurations;
135   this->Makefile->AddInstallGenerator(
136     new cmInstallFilesGenerator(this->Files,
137                                 destination.c_str(), false,
138                                 no_permissions, no_configurations,
139                                 no_component.c_str(), no_rename));
140 }
141
142
143 /**
144  * Find a file in the build or source tree for installation given a
145  * relative path from the CMakeLists.txt file.  This will favor files
146  * present in the build tree.  If a full path is given, it is just
147  * returned.
148  */
149 std::string cmInstallFilesCommand::FindInstallSource(const char* name) const
150 {
151   if(cmSystemTools::FileIsFullPath(name))
152     {
153     // This is a full path.
154     return name;
155     }
156
157   // This is a relative path.
158   std::string tb = this->Makefile->GetCurrentOutputDirectory();
159   tb += "/";
160   tb += name;
161   std::string ts = this->Makefile->GetCurrentDirectory();
162   ts += "/";
163   ts += name;
164
165   if(cmSystemTools::FileExists(tb.c_str()))
166     {
167     // The file exists in the binary tree.  Use it.
168     return tb;
169     }
170   else if(cmSystemTools::FileExists(ts.c_str()))
171     {
172     // The file exists in the source tree.  Use it.
173     return ts;
174     }
175   else
176     {
177     // The file doesn't exist.  Assume it will be present in the
178     // binary tree when the install occurs.
179     return tb;
180     }
181 }