Move some more autogenerated files to intermediate directory
[reactos.git] / reactos / tools / rbuild / directory.cpp
1 /*
2 * Copyright (C) 2005 Casper S. Hornstrup
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #include "pch.h"
19 #include <assert.h>
20
21 #include "rbuild.h"
22 #ifdef _MSC_VER
23 #include <errno.h>
24 #else
25 #include <dirent.h>
26 #endif//_MSC_VER
27
28 #ifdef WIN32
29 #define MKDIR(s) mkdir(s)
30 #else
31 #define MKDIR(s) mkdir(s, 0755)
32 #endif
33
34 using std::string;
35 using std::vector;
36
37 Directory::Directory ( const string& name_ )
38 : name(name_)
39 {
40 }
41
42 void
43 Directory::Add ( const char* subdir )
44 {
45 size_t i;
46 string s1 = string ( subdir );
47 if ( ( i = s1.find ( '$' ) ) != string::npos )
48 {
49 throw InvalidOperationException ( __FILE__,
50 __LINE__,
51 "No environment variables can be used here. Path was %s",
52 subdir );
53 }
54
55 const char* p = strpbrk ( subdir, "/\\" );
56 if ( !p )
57 p = subdir + strlen(subdir);
58 string s ( subdir, p-subdir );
59 if ( subdirs.find(s) == subdirs.end() )
60 subdirs[s] = new Directory(s);
61 if ( *p && *++p )
62 subdirs[s]->Add ( p );
63 }
64
65 bool
66 Directory::mkdir_p ( const char* path )
67 {
68 #ifndef _MSC_VER
69 DIR *directory;
70 directory = opendir ( path );
71 if ( directory != NULL )
72 {
73 closedir ( directory );
74 return false;
75 }
76 #endif//_MSC_VER
77
78 if ( MKDIR ( path ) != 0 )
79 {
80 #ifdef _MSC_VER
81 if ( errno == EEXIST )
82 return false;
83 #endif//_MSC_VER
84 throw AccessDeniedException ( string ( path ) );
85 }
86 return true;
87 }
88
89 bool
90 Directory::CreateDirectory ( string path )
91 {
92 size_t index = 0;
93 size_t nextIndex;
94 if ( isalpha ( path[0] ) && path[1] == ':' && path[2] == cSep )
95 {
96 nextIndex = path.find ( cSep, 3);
97 }
98 else
99 nextIndex = path.find ( cSep );
100
101 bool directoryWasCreated = false;
102 while ( nextIndex != string::npos )
103 {
104 nextIndex = path.find ( cSep, index + 1 );
105 directoryWasCreated = mkdir_p ( path.substr ( 0, nextIndex ).c_str () );
106 index = nextIndex;
107 }
108 return directoryWasCreated;
109 }
110
111 string
112 Directory::ReplaceVariable ( string name,
113 string value,
114 string path )
115 {
116 size_t i = path.find ( name );
117 if ( i != string::npos )
118 return path.replace ( i, name.length (), value );
119 else
120 return path;
121 }
122
123 void
124 Directory::ResolveVariablesInPath ( char* buf,
125 string path )
126 {
127 string s = ReplaceVariable ( "$(INTERMEDIATE)", Environment::GetIntermediatePath (), path );
128 s = ReplaceVariable ( "$(OUTPUT)", Environment::GetOutputPath (), s );
129 s = ReplaceVariable ( "$(INSTALL)", Environment::GetInstallPath (), s );
130 strcpy ( buf, s.c_str () );
131 }
132
133 void
134 Directory::GenerateTree ( const string& parent,
135 bool verbose )
136 {
137 string path;
138
139 if ( parent.size () > 0 )
140 {
141 char buf[256];
142
143 path = parent + sSep + name;
144 ResolveVariablesInPath ( buf, path );
145 if ( CreateDirectory ( buf ) && verbose )
146 printf ( "Created %s\n", buf );
147 }
148 else
149 path = name;
150
151 for ( directory_map::iterator i = subdirs.begin ();
152 i != subdirs.end ();
153 ++i )
154 {
155 i->second->GenerateTree ( path, verbose );
156 }
157 }
158
159 string
160 Directory::EscapeSpaces ( string path )
161 {
162 string newpath;
163 char* p = &path[0];
164 while ( *p != 0 )
165 {
166 if ( *p == ' ' )
167 newpath = newpath + "\\ ";
168 else
169 newpath = newpath + *p;
170 *p++;
171 }
172 return newpath;
173 }
174
175 void
176 Directory::CreateRule ( FILE* f,
177 const string& parent )
178 {
179 string path;
180
181 if ( parent.size() > 0 )
182 {
183 string escapedParent = EscapeSpaces ( parent );
184 fprintf ( f,
185 "%s%c%s: | %s\n",
186 escapedParent.c_str (),
187 cSep,
188 EscapeSpaces ( name ).c_str (),
189 escapedParent.c_str () );
190
191 fprintf ( f,
192 "\t$(ECHO_MKDIR)\n" );
193
194 fprintf ( f,
195 "\t${mkdir} $@\n" );
196
197 path = parent + sSep + name;
198 }
199 else
200 path = name;
201
202 for ( directory_map::iterator i = subdirs.begin();
203 i != subdirs.end();
204 ++i )
205 {
206 i->second->CreateRule ( f, path );
207 }
208 }