source tree restructured a little (should i move *.[ch] to src/ ?)
[k8jam.git] / jam.c
blobb7148ce447277d7db36d0bd141a66fccf8293c33
1 /*
2 * /+\
3 * +\ Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * \+/
6 * This file is part of jam.
8 * License is hereby granted to use this software and distribute it
9 * freely, as long as this copyright notice is retained and modifications
10 * are clearly marked.
12 * ALL WARRANTIES ARE HEREBY DISCLAIMED.
16 * jam.c - make redux
18 * See Jam.html for usage information.
20 * These comments document the code.
22 * The top half of the code is structured such:
24 * jam
25 * / | \
26 * +---+ | \
27 * / | \
28 * jamgram option \
29 * / | \ \
30 * / | \ \
31 * / | \ |
32 * scan | compile make
33 * | | / | \ / | \
34 * | | / | \ / | \
35 * | | / | \ / | \
36 * jambase parse | rules search make1
37 * | | | \
38 * | | | \
39 * | | | \
40 * builtins timestamp command execute
41 * |
42 * |
43 * |
44 * filesys
47 * The support routines are called by all of the above, but themselves
48 * are layered thus:
50 * variable|expand
51 * / | | |
52 * / | | |
53 * / | | |
54 * lists | | pathsys
55 * \ | |
56 * \ | |
57 * \ | |
58 * newstr |
59 * \ |
60 * \ |
61 * \ |
62 * hash
64 * Roughly, the modules are:
66 * builtins.c - jam's built-in rules
67 * command.c - maintain lists of commands
68 * compile.c - compile parsed jam statements
69 * execunix.c - execute a shell script on UNIX
70 * execvms.c - execute a shell script, ala VMS
71 * expand.c - expand a buffer, given variable values
72 * file*.c - scan directories and archives on *
73 * hash.c - simple in-memory hashing routines
74 * headers.c - handle #includes in source files
75 * jambase.c - compilable copy of Jambase
76 * jamgram.y - jam grammar
77 * lists.c - maintain lists of strings
78 * make.c - bring a target up to date, once rules are in place
79 * make1.c - execute command to bring targets up to date
80 * newstr.c - string manipulation routines
81 * option.c - command line option processing
82 * parse.c - make and destroy parse trees as driven by the parser
83 * path*.c - manipulate file names on *
84 * hash.c - simple in-memory hashing routines
85 * regexp.c - Henry Spencer's regexp
86 * rules.c - access to RULEs, TARGETs, and ACTIONs
87 * scan.c - the jam yacc scanner
88 * search.c - find a target along $(SEARCH) or $(LOCATE)
89 * timestamp.c - get the timestamp of a file or archive member
90 * variable.c - handle jam multi-element variables
92 * 05/04/94 (seiwald) - async multiprocess (-j) support
93 * 02/08/95 (seiwald) - -n implies -d2.
94 * 02/22/95 (seiwald) - -v for version info.
95 * 09/11/00 (seiwald) - PATCHLEVEL folded into VERSION.
96 * 01/10/01 (seiwald) - pathsys.h split from filesys.h
97 * 01/21/02 (seiwald) - new -q to quit quickly on build failure
98 * 03/16/02 (seiwald) - support for -g (reorder builds by source time)
99 * 09/19/02 (seiwald) - new -d displays
100 * 10/22/02 (seiwald) - list_new() now does its own newstr()/copystr()
101 * 11/04/02 (seiwald) - const-ing for string literals
104 # include "jam.h"
105 # include "option.h"
106 # include "patchlevel.h"
108 /* These get various function declarations. */
110 # include "lists.h"
111 # include "parse.h"
112 # include "variable.h"
113 # include "compile.h"
114 # include "builtins.h"
115 # include "rules.h"
116 # include "newstr.h"
117 # include "scan.h"
118 # include "timestamp.h"
119 # include "make.h"
121 /* Macintosh is "special" */
123 # ifdef OS_MAC
124 # include <QuickDraw.h>
125 # endif
127 /* And UNIX for this */
129 # ifdef unix
130 # include <sys/utsname.h>
131 # endif
133 struct globs globs = {
134 0, /* noexec */
135 1, /* jobs */
136 0, /* quitquick */
137 0, /* newestfirst */
138 # ifdef OS_MAC
139 { 0 }, /* display - suppress actions output */
140 # else
141 { 0, 1 }, /* display actions */
142 # endif
143 0 /* output commands, not run them */
146 /* Symbols to be defined as true for use in Jambase */
148 static const char *othersyms[] = { OSMAJOR, OSMINOR, OSPLAT, JAMVERSYM, 0 } ;
150 /* Known for sure:
151 * mac needs arg_enviro
152 * OS2 needs extern environ
155 # ifdef OS_MAC
156 # define use_environ arg_environ
157 # ifdef MPW
158 QDGlobals qd;
159 # endif
160 # endif
162 # ifndef use_environ
163 # define use_environ environ
164 # if !defined( __WATCOM__ ) && !defined( OS_OS2 ) && !defined( OS_NT )
165 extern char **environ;
166 # endif
167 # endif
170 main( int argc, char **argv, char **arg_environ )
172 int n, num_targets;
173 const char *s;
174 struct option optv[N_OPTS];
175 char* targets[N_TARGETS];
176 const char *all = "all";
177 int anyhow = 0;
178 int status;
180 # ifdef OS_MAC
181 InitGraf(&qd.thePort);
182 # endif
184 argc--, argv++;
186 if( ( num_targets = getoptions( argc, argv, "d:j:f:gs:t:ano:qv", optv, targets ) ) < 0 )
188 printf( "\nusage: jam [ options ] targets...\n\n" );
190 printf( "-a Build all targets, even if they are current.\n" );
191 printf( "-dx Display (a)actions (c)causes (d)dependencies\n" );
192 printf( " (m)make tree (x)commands (0-9) debug levels.\n" );
193 printf( "-fx Read x instead of Jambase.\n" );
194 printf( "-g Build from newest sources first.\n" );
195 printf( "-jx Run up to x shell commands concurrently.\n" );
196 printf( "-n Don't actually execute the updating actions.\n" );
197 printf( "-ox Write the updating actions to file x.\n" );
198 printf( "-q Quit quickly as soon as a target fails.\n" );
199 printf( "-sx=y Set variable x=y, overriding environment.\n" );
200 printf( "-tx Rebuild x, even if it is up-to-date.\n" );
201 printf( "-v Print the version of jam and exit.\n\n" );
203 exit( EXITBAD );
206 /* Version info. */
208 if( ( s = getoptval( optv, 'v', 0 ) ) )
210 printf( "K-Jam %s. %s. ", VERSION, OSMINOR );
211 printf( "(C) 1993-2003 Christopher Seiwald, see www.freetype.org/jam/\n" );
212 printf( "changed by Ketmar, ketmar@ua.fm\n" );
214 return EXITOK;
217 /* Pick up interesting options */
219 if( ( s = getoptval( optv, 'n', 0 ) ) )
220 globs.noexec++, DEBUG_MAKE = DEBUG_MAKEQ = DEBUG_EXEC = 1;
222 if( ( s = getoptval( optv, 'q', 0 ) ) )
223 globs.quitquick = 1;
225 if( ( s = getoptval( optv, 'a', 0 ) ) )
226 anyhow++;
228 if( ( s = getoptval( optv, 'j', 0 ) ) )
229 globs.jobs = atoi( s );
231 if( ( s = getoptval( optv, 'g', 0 ) ) )
232 globs.newestfirst = 1;
234 /* Turn on/off debugging */
236 for( n = 0; (s = getoptval( optv, 'd', n )) != 0; n++ )
238 int i = atoi( s );
240 /* First -d, turn off defaults. */
242 if( !n )
243 DEBUG_MAKE = DEBUG_MAKEQ = DEBUG_EXEC = 0;
245 /* n turns on levels 1-n */
246 /* +n turns on level n */
247 /* c turns on named display c */
249 if( i < 0 || i >= DEBUG_MAX )
251 printf( "Invalid debug level '%s'.\n", s );
253 else if( *s == '+' )
255 globs.debug[i] = 1;
257 else if( i ) while( i )
259 globs.debug[i--] = 1;
261 else while( *s ) switch( *s++ )
263 case 'a': DEBUG_MAKE = DEBUG_MAKEQ = 1; break;
264 case 'c': DEBUG_CAUSES = 1; break;
265 case 'd': DEBUG_DEPENDS = 1; break;
266 case 'm': DEBUG_MAKEPROG = 1; break;
267 case 'x': DEBUG_EXEC = 1; break;
268 case '0': break;
269 default: printf( "Invalid debug flag '%c'.\n", s[-1] );
273 /* Set JAMDATE first */
276 char buf[ 128 ];
277 time_t clock;
278 time( &clock );
279 strcpy( buf, ctime( &clock ) );
281 /* Trim newline from date */
283 if( strlen( buf ) == 25 )
284 buf[ 24 ] = 0;
286 var_set( "JAMDATE", list_new( L0, buf, 0 ), VAR_SET );
289 /* And JAMUNAME */
290 # ifdef unix
292 struct utsname u;
294 if( uname( &u ) >= 0 )
296 LIST *l = L0;
297 l = list_new( l, u.machine, 0 );
298 l = list_new( l, u.version, 0 );
299 l = list_new( l, u.release, 0 );
300 l = list_new( l, u.nodename, 0 );
301 l = list_new( l, u.sysname, 0 );
302 var_set( "JAMUNAME", l, VAR_SET );
305 # endif /* unix */
308 * Jam defined variables OS, OSPLAT
311 var_defines( othersyms );
313 /* load up environment variables */
315 var_defines( (const char **)use_environ );
317 /* Load up variables set on command line. */
319 for( n = 0; (s = getoptval( optv, 's', n )) != 0; n++ )
321 const char *symv[2];
322 symv[0] = s;
323 symv[1] = 0;
324 var_defines( symv );
327 /* Initialize built-in rules */
329 load_builtins();
331 /* Parse ruleset */
333 for( n = 0; (s = getoptval( optv, 'f', n )) != 0; n++ )
334 parse_file( s );
336 if( !n )
337 parse_file( "+" );
339 status = yyanyerrors();
341 /* Manually touch -t targets */
343 for( n = 0; (s = getoptval( optv, 't', n )) != 0; n++ )
344 touchtarget( s );
346 /* If an output file is specified, set globs.cmdout to that */
348 if( (s = getoptval( optv, 'o', 0 )) != 0 )
350 if( !( globs.cmdout = fopen( s, "w" ) ) )
352 printf( "Failed to write to '%s'\n", s );
353 exit( EXITBAD );
355 globs.noexec++;
358 /* Add JAMCMDARGS
361 LIST* l = L0;
363 for ( n = 0; n < num_targets; n++ )
364 l = list_new( l, targets[n], 0 );
366 var_set( "JAMCMDARGS", l, VAR_SET );
369 /* Now make target */
371 if( !num_targets )
372 status |= make( 1, &all, anyhow );
373 else
374 status |= make( num_targets, (const char**)targets, anyhow );
376 /* Widely scattered cleanup */
378 var_done();
379 donerules();
380 donestamps();
381 donestr();
383 /* close cmdout */
385 if( globs.cmdout )
386 fclose( globs.cmdout );
388 return status ? EXITBAD : EXITOK;