Imported Upstream version 2.8.9
[platform/upstream/cmake.git] / Source / cmIncludeDirectoryCommand.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 "cmIncludeDirectoryCommand.h"
13
14 // cmIncludeDirectoryCommand
15 bool cmIncludeDirectoryCommand
16 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
17 {
18   if(args.size() < 1 )
19     {
20     return true;
21     }
22
23   std::vector<std::string>::const_iterator i = args.begin();
24
25   bool before = this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_BEFORE");
26   bool system = false;
27
28   if ((*i) == "BEFORE")
29     {
30     before = true;
31     ++i;
32     }
33   else if ((*i) == "AFTER")
34     {
35     before = false;
36     ++i;
37     }
38
39   for(; i != args.end(); ++i)
40     {
41     if(*i == "SYSTEM")
42       {
43       system = true;
44       continue;
45       }
46     if(i->size() == 0)
47       {
48       this->SetError("given empty-string as include directory.");
49       return false;
50       }
51
52     this->AddDirectory(i->c_str(),before,system);
53
54     }
55   return true;
56 }
57
58 // do a lot of cleanup on the arguments because this is one place where folks
59 // sometimes take the output of a program and pass it directly into this
60 // command not thinking that a single argument could be filled with spaces
61 // and newlines etc liek below:
62 //
63 // "   /foo/bar
64 //    /boo/hoo /dingle/berry "
65 //
66 // ideally that should be three separate arguments but when sucking the
67 // output from a program and passing it into a command the cleanup doesn't
68 // always happen
69 //
70 void cmIncludeDirectoryCommand::AddDirectory(const char *i, 
71                                              bool before, 
72                                              bool system)
73 {
74   // break apart any line feed arguments
75   std::string ret = i;
76   std::string::size_type pos = 0;
77   if((pos = ret.find('\n', pos)) != std::string::npos)
78     {
79     if (pos)
80       {
81       this->AddDirectory(ret.substr(0,pos).c_str(), before, system);
82       }
83     if (ret.size()-pos-1)
84       {
85       this->AddDirectory(ret.substr(pos+1,ret.size()-pos-1).c_str(),
86                          before, system);
87       }
88     return;
89     }
90
91   // remove any leading or trailing spaces and \r
92   std::string::size_type b = ret.find_first_not_of(" \r");
93   std::string::size_type e = ret.find_last_not_of(" \r");
94   if ((b!=ret.npos) && (e!=ret.npos))  
95     {
96     ret.assign(ret, b, 1+e-b);   // copy the remaining substring
97     }
98   else
99     {
100     return;         // if we get here, we had only whitespace in the string
101     }
102
103   if (!cmSystemTools::IsOff(ret.c_str()))
104     {
105     cmSystemTools::ConvertToUnixSlashes(ret);
106     if(!cmSystemTools::FileIsFullPath(ret.c_str()))
107       {
108       std::string tmp = this->Makefile->GetStartDirectory();
109       tmp += "/";
110       tmp += ret;
111       ret = tmp;
112       }
113     }
114   this->Makefile->AddIncludeDirectory(ret.c_str(), before);
115   if(system)
116     {
117     this->Makefile->AddSystemIncludeDirectory(ret.c_str());
118     }
119 }
120