Imported Upstream version 2.8.9
[platform/upstream/cmake.git] / Source / cmAddSubDirectoryCommand.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 "cmAddSubDirectoryCommand.h"
13
14 // cmAddSubDirectoryCommand
15 bool cmAddSubDirectoryCommand::InitialPass
16 (std::vector<std::string> const& args, cmExecutionStatus &)
17 {
18   if(args.size() < 1 )
19     {
20     this->SetError("called with incorrect number of arguments");
21     return false;
22     }
23   
24   // store the binpath
25   std::string srcArg = args[0];
26   std::string binArg;
27   
28   bool excludeFromAll = false;
29
30   // process the rest of the arguments looking for optional args
31   std::vector<std::string>::const_iterator i = args.begin();
32   ++i;
33   for(;i != args.end(); ++i)
34     {
35     if(*i == "EXCLUDE_FROM_ALL")
36       {
37       excludeFromAll = true;
38       continue;
39       }
40     else if (!binArg.size())
41       {
42       binArg = *i;
43       }
44     else
45       {
46       this->SetError("called with incorrect number of arguments");
47       return false;
48       }
49     }
50
51   // Compute the full path to the specified source directory.
52   // Interpret a relative path with respect to the current source directory.
53   std::string srcPath;
54   if(cmSystemTools::FileIsFullPath(srcArg.c_str()))
55     {
56     srcPath = srcArg;
57     }
58   else
59     {
60     srcPath = this->Makefile->GetCurrentDirectory();
61     srcPath += "/";
62     srcPath += srcArg;
63     }
64   if(!cmSystemTools::FileIsDirectory(srcPath.c_str()))
65     {
66     std::string error = "given source \"";
67     error += srcArg;
68     error += "\" which is not an existing directory.";
69     this->SetError(error.c_str());
70     return false;
71     }
72   srcPath = cmSystemTools::CollapseFullPath(srcPath.c_str());
73
74   // Compute the full path to the binary directory.
75   std::string binPath;
76   if(binArg.empty())
77     {
78     // No binary directory was specified.  If the source directory is
79     // not a subdirectory of the current directory then it is an
80     // error.
81     if(!cmSystemTools::FindLastString(srcPath.c_str(),
82                                       this->Makefile->GetCurrentDirectory()))
83       {
84       cmOStringStream e;
85       e << "not given a binary directory but the given source directory "
86         << "\"" << srcPath << "\" is not a subdirectory of \""
87         << this->Makefile->GetCurrentDirectory() << "\".  "
88         << "When specifying an out-of-tree source a binary directory "
89         << "must be explicitly specified.";
90       this->SetError(e.str().c_str());
91       return false;
92       }
93
94     // Remove the CurrentDirectory from the srcPath and replace it
95     // with the CurrentOutputDirectory.
96     binPath = srcPath;
97     cmSystemTools::ReplaceString(binPath,
98                                  this->Makefile->GetCurrentDirectory(),
99                                  this->Makefile->GetCurrentOutputDirectory());
100     }
101   else
102     {
103     // Use the binary directory specified.
104     // Interpret a relative path with respect to the current binary directory.
105     if(cmSystemTools::FileIsFullPath(binArg.c_str()))
106       {
107       binPath = binArg;
108       }
109     else
110       {
111       binPath = this->Makefile->GetCurrentOutputDirectory();
112       binPath += "/";
113       binPath += binArg;
114       }
115     }
116   binPath = cmSystemTools::CollapseFullPath(binPath.c_str());
117
118   // Add the subdirectory using the computed full paths.
119   this->Makefile->AddSubDirectory(srcPath.c_str(), binPath.c_str(),
120                                   excludeFromAll, false, true);
121
122   return true;
123 }