1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
#include "buildparser.h"
#include "context.h"
#include "ast.h"
#include "runner.h"
#include "target.h"
#include "viewplugger.h"
#include "cache.h"
#include <bu/optparser.h>
#include <bu/sio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
extern char **environ;
using namespace Bu;
class Options : public Bu::OptParser
{
public:
Options( int argc, char *argv[] ) :
sView("default"),
sAction("default"),
sConfig("default.bld"),
sCacheFile(".build_cache"),
bDot( false ),
bDebug( false ),
bAutoInclude( true ),
bAstDump( false ),
bEnviron( true ),
bCache( true ),
iInfoLevel( 0 )
{
bool bClean = false;
addHelpBanner("build mark 3\n");
Bu::FString sViews("Select a view from: ");
StrList lViews = ViewPlugger::getInstance().getPluginList();
for( StrList::iterator i = lViews.begin(); i; i++ )
{
if( i != lViews.begin() )
sViews += ", ";
sViews += *i;
}
addHelpBanner("The following options do things other than build:");
addOption( iInfoLevel, 'i', "info", "Display some basic info about the "
"loaded build config, including available targets.");
addHelpBanner("The following options control general execution:");
addOption( sView, 'v', "view", sViews );
addOption( sConfig, 'f', "file", "Select a different config file." );
addOption( bClean, 'c', "Shorthand for running action 'clean'. If an "
"action is specified, this will modify it to run 'clean-action'.");
addOption( slot(this, &Options::onChdir), 'C', "chdir",
"Change to directory before doing anything else.");
addOption( sCacheFile, "cache", "Select a different cache file.");
addOption( bCache, "no-cache", "Disable using the cache.");
addHelpBanner("\nThe following options control debugging:");
addOption( bEnviron, "no-env", "Do not import environment variables.");
addOption( bDot, "dot", "Generate a dot chart after execution." );
addOption( bDebug, "debug",
"Dump massive amounts of hard to read debugging data." );
addOption( bAstDump, "debug-ast",
"Display the raw AST that is computed from parsing the input. "
"You should probably never ever use this, it'll scare you."
);
setOverride( "no-env", "false" );
setOverride( "dot", "true" );
setOverride( "debug", "true" );
setOverride( "debug-ast", "true" );
setOverride( "info", "1" );
setOverride( 'c', "true" );
setOverride( "no-cache", "false" );
addHelpOption();
setNonOption( slot( this, &Options::onNonOption ) );
parse( argc, argv );
if( bClean )
{
if( sAction == "default" )
sAction = "clean";
else
sAction.prepend("clean-");
}
}
virtual ~Options()
{
}
int onChdir( StrArray sParams )
{
if( sParams.getSize() == 0 )
{
sio << "You must specify a directory name!" << sio.nl << sio.nl;
exit(2);
}
chdir( sParams[1].getStr() );
return 1;
}
int onNonOption( StrArray sParams )
{
sAction = sParams[0];
return 0;
}
Bu::FString sView;
Bu::FString sAction;
Bu::FString sConfig;
Bu::FString sCacheFile;
bool bDot;
bool bDebug;
bool bAutoInclude;
bool bAstDump;
bool bEnviron;
bool bCache;
int iInfoLevel;
};
int main( int argc, char *argv[] )
{
typedef Bu::List<Bu::FString> StrList;
StrList lShareList;
lShareList.append("/usr/share/build/").append("./share/");
Ast ast;
Context cnt;
BuildParser bp( ast );
for( StrList::iterator i = lShareList.begin(); i; i++ )
{
bp.addIncludePath( *i + "include");
}
Options opts( argc, argv );
try
{
cnt.setView( ViewPlugger::getInstance().instantiate( opts.sView ) );
}
catch( Bu::HashException &e )
{
sio << "Error: Invalid view specified, please choose from the "
"following choices:" << sio.nl << sio.nl << "\t";
StrList lViews = ViewPlugger::getInstance().getPluginList();
for( StrList::iterator i = lViews.begin(); i; i++ )
{
if( i != lViews.begin() )
sio << ", ";
sio << *i;
}
sio << sio.nl << sio.nl;
return 1;
}
if( opts.bCache )
{
Cache::getInstance().bind( opts.sCacheFile );
}
// Load up the environment as vars.
if( opts.bEnviron )
{
for( char **env = environ; *env; env++ )
{
int iSplit;
for( iSplit = 0; (*env)[iSplit] != '='; iSplit++ ) { }
cnt.addVariable(
FString( *env, iSplit ),
FString( *env+iSplit+1 )
);
}
}
if( opts.bAutoInclude )
{
DIR *d;
Bu::FString sAutoDir;
for( StrList::iterator i = lShareList.begin(); i; i++ )
{
sAutoDir = *i + "autoinclude";
d = opendir( sAutoDir.getStr() );
if( d )
break;
}
if( !d )
{
cnt.getView()->sysWarning(
"Could not find an autoinclude directory."
);
}
else
{
struct dirent *de;
while( (de = readdir( d )) )
{
if( de->d_name[0] == '.' || (de->d_type != DT_REG) )
continue;
//sio << "Auto-including: " << de->d_name << sio.nl;
bp.load( sAutoDir + "/" + de->d_name );
}
}
}
bp.load( opts.sConfig );
if( opts.bAstDump )
{
sio << ast << sio.nl << sio.nl;
return 0;
}
// sio << ast << sio.nl;
Runner r( ast, cnt );
r.initialize();
r.run();
switch( opts.iInfoLevel )
{
case 0:
// Do nothing
break;
case 1:
cnt.printBasicInfo();
return 0;
}
try
{
r.execAction( opts.sAction );
}
catch( std::exception &e )
{
cnt.getView()->sysError(e.what());
}
catch( ... )
{
cnt.getView()->sysError(
"Unknown error occured, this is probably bad..."
);
}
if( opts.bDot )
{
cnt.writeTargetDot();
}
if( opts.bDebug )
{
sio << "Final context:" << sio.nl << cnt << sio.nl << sio.nl;
}
return 0;
}
|