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