3 * +\ Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
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
12 * ALL WARRANTIES ARE HEREBY DISCLAIMED.
17 * See Jam.html for usage information.
19 * These comments document the code.
21 * The top half of the code is structured such:
35 * jambase parse | rules search make1
39 * builtins timestamp command execute
46 * The support routines are called by all of the above, but themselves
63 * Roughly, the modules are:
65 * builtins.c - jam's built-in rules
66 * command.c - maintain lists of commands
67 * compile.c - compile parsed jam statements
68 * execcmd.c - execute a shell script on UNIX
69 * expand.c - expand a buffer, given variable values
70 * file*.c - scan directories and archives on *
71 * hash.c - simple in-memory hashing routines
72 * headers.c - handle #includes in source files
73 * jambase.c - compilable copy of Jambase
74 * jamgram.y - jam grammar
75 * lists.c - maintain lists of strings
76 * make.c - bring a target up to date, once rules are in place
77 * make1.c - execute command to bring targets up to date
78 * newstr.c - string manipulation routines
79 * option.c - command line option processing
80 * parse.c - make and destroy parse trees as driven by the parser
81 * path*.c - manipulate file names on *
82 * hash.c - simple in-memory hashing routines
83 * hsregexp.c - Henry Spencer's regexp
84 * rules.c - access to RULEs, TARGETs, and ACTIONs
85 * scan.c - the jam yacc scanner
86 * search.c - find a target along $(SEARCH) or $(LOCATE)
87 * timestamp.c - get the timestamp of a file or archive member
88 * variable.c - handle jam multi-element variables
90 * 05/04/94 (seiwald) - async multiprocess (-j) support
91 * 02/08/95 (seiwald) - -n implies -d2.
92 * 02/22/95 (seiwald) - -v for version info.
93 * 09/11/00 (seiwald) - PATCHLEVEL folded into VERSION.
94 * 01/10/01 (seiwald) - pathsys.h split from filesys.h
95 * 01/21/02 (seiwald) - new -q to quit quickly on build failure
96 * 03/16/02 (seiwald) - support for -g (reorder builds by source time)
97 * 09/19/02 (seiwald) - new -d displays
98 * 10/22/02 (seiwald) - list_new() now does its own newstr()/copystr()
99 * 11/04/02 (seiwald) - const-ing for string literals
103 #include "patchlevel.h"
105 /* These get various function declarations. */
108 #include "variable.h"
110 #include "builtins.h"
114 #include "timestamp.h"
118 /* And UNIX for this */
120 # include <sys/utsname.h>
124 struct globs globs
= {
129 { 0, 1 }, /* display actions */
130 0 /* output commands, not run them */
134 /* Symbols to be defined as true for use in Jambase */
135 static const char *othersyms
[] = { OSMAJOR
, OSMINOR
, OSPLAT
, JAMVERSYM
, 0 } ;
139 * OS2 needs extern environ
142 # define use_environ environ
143 # if !defined(__WATCOM__) && !defined(OS_OS2) && !defined(OS_NT)
144 extern char **environ
;
149 int main (int argc
, char **argv
, char **arg_environ
) {
152 struct option optv
[N_OPTS
];
153 char *targets
[N_TARGETS
];
154 const char *all
= "all";
160 if ((num_targets
= getoptions(argc
, argv
, "H:d:j:f:gs:t:ano:qv", optv
, targets
)) < 0) {
161 printf("usage: jam [ options ] targets...\n\n"
162 "-a Build all targets, even if they are current.\n"
163 "-dx Display (a)actions (c)causes (d)dependencies\n"
164 " (m)make tree (x)commands (0-9) debug levels.\n"
165 "-fx Read x instead of Jambase.\n"
166 "-g Build from newest sources first.\n"
167 "-jx Run up to x shell commands concurrently.\n"
168 "-n Don't actually execute the updating actions.\n"
169 "-ox Write the updating actions to file x.\n"
170 "-q Quit quickly as soon as a target fails.\n"
171 "-sx=y Set variable x=y, overriding environment.\n"
172 "-tx Rebuild x, even if it is up-to-date.\n"
173 "-v Print the version of jam and exit.\n"
174 "-Hx show header cache statistics (a)ll, (h)its, (s)tats.\n"
179 if ((s
= getoptval(optv
, 'v', 0))) {
180 printf("K8-Jam %s. %s. ", VERSION
, OSMINOR
);
182 "(C) 1993-2003 Christopher Seiwald, see http://www.freetype.org/jam/\n"
183 "changes by Ketmar // Vampire Avalon, psyc://ketmar.no-ip.org/~ketmar\n"
187 /* pick up interesting options */
188 if ((s
= getoptval(optv
, 'n', 0))) globs
.noexec
++, DEBUG_MAKE
= DEBUG_MAKEQ
= DEBUG_EXEC
= 1;
189 if ((s
= getoptval(optv
, 'q', 0))) globs
.quitquick
= 1;
190 if ((s
= getoptval(optv
, 'a', 0))) anyhow
++;
191 if ((s
= getoptval(optv
, 'j', 0))) globs
.jobs
= atoi(s
);
192 if ((s
= getoptval(optv
, 'g', 0))) globs
.newestfirst
= 1;
193 /* turn on/off debugging */
194 for (n
= 0; (s
= getoptval(optv
, 'd', n
)) != 0; ++n
) {
196 /* first -d, turn off defaults. */
197 if (!n
) DEBUG_MAKE
= DEBUG_MAKEQ
= DEBUG_EXEC
= 0;
198 /* n turns on levels 1-n */
199 /* +n turns on level n */
200 /* c turns on named display c */
201 if (i
< 0 || i
>= DEBUG_MAX
) {
202 printf( "Invalid debug level '%s'.\n", s
);
203 } else if (*s
== '+') {
206 while (i
) globs
.debug
[i
--] = 1;
210 case 'a': DEBUG_MAKE
= DEBUG_MAKEQ
= 1; break;
211 case 'c': DEBUG_CAUSES
= 1; break;
212 case 'd': DEBUG_DEPENDS
= 1; break;
213 case 'm': DEBUG_MAKEPROG
= 1; break;
214 case 'x': DEBUG_EXEC
= 1; break;
216 default: printf("Invalid debug flag '%c'.\n", s
[-1]);
221 /* header cache statistics */
222 for (n
= 0; (s
= getoptval(optv
, 'H', n
)) != 0; ++n
) {
226 case 'a': optShowHCacheStats
= optShowHCacheInfo
= 1; break;
227 case 'h': optShowHCacheInfo
= 1; break;
228 case 's': optShowHCacheStats
= 1; break;
232 if (wasOptH
&& (optShowHCacheStats
== 0 && optShowHCacheInfo
== 0)) optShowHCacheStats
= optShowHCacheInfo
= 1;
233 /* set JAMDATE first */
239 strcpy(buf
, ctime(&clock
));
240 /* trim newline from date */
242 while (p
>= buf
&& *((unsigned char *)p
) <= ' ') --p
;
244 var_set("JAMDATE", list_new(L0
, buf
, 0), VAR_SET
);
251 if (uname(&u
) >= 0) {
253 l
= list_new(l
, u
.machine
, 0);
254 l
= list_new(l
, u
.version
, 0);
255 l
= list_new(l
, u
.release
, 0);
256 l
= list_new(l
, u
.nodename
, 0);
257 l
= list_new(l
, u
.sysname
, 0);
258 var_set("JAMUNAME", l
, VAR_SET
);
262 /* Jam defined variables OS, OSPLAT */
263 var_defines(othersyms
);
264 /* load up environment variables */
265 var_defines((const char **)use_environ
);
266 /* load up variables set on command line. */
267 for (n
= 0; (s
= getoptval(optv
, 's', n
)) != 0; ++n
) {
275 LIST
*l0
= L0
, *l1
= L0
;
277 if (num_targets
< 1) {
278 l0
= list_new(l0
, "all", 0);
279 l1
= list_new(l1
, "all", 0);
281 for (n
= 0; n
< num_targets
; ++n
) {
282 /*printf("target %i: %s\n", n, targets[n]);*/
283 l0
= list_new(l0
, targets
[n
], 0);
284 l1
= list_new(l1
, targets
[n
], 0);
287 var_set("JAMCMDARGS", l0
, VAR_SET
);
288 var_set("JAM_TARGETS", l1
, VAR_SET
); /* k8 */
290 /* initialize built-in rules */
293 for (n
= 0; (s
= getoptval(optv
, 'f', n
)) != 0; ++n
) parse_file(s
);
294 if (!n
) parse_file("::Jambase");
295 status
= yyanyerrors();
296 /* manually touch -t targets */
297 for (n
= 0; (s
= getoptval(optv
, 't', n
)) != 0; ++n
) touchtarget(s
);
298 /* if an output file is specified, set globs.cmdout to that */
299 if ((s
= getoptval(optv
, 'o', 0)) != 0) {
300 if (!(globs
.cmdout
= fopen(s
, "w"))) {
301 printf("Failed to write to '%s'\n", s
);
306 #ifndef NO_OPT_JAM_TARGETS_VARIABLE_EXT
307 /* ported from Haiku */
308 /* get value of variable JAM_TARGETS and build the targets */
310 LIST
*l
= var_get("JAM_TARGETS");
311 int targetCount
= list_length(l
);
314 if (targetCount
== 0) {
315 /* no targets: nothing to do */
316 printf("No targets. Nothing to do.\n");
319 if (targetCount
>= N_TARGETS
) {
320 printf("ERROR: Too many targets!\n");
323 for (i
= 0; i
< targetCount
; ++i
) {
324 const char *s0
= l
->string
;
325 targets
[i
] = malloc((strlen(s0
)+1)*sizeof(char));
327 printf("ERROR: Out of memory!\n");
330 strcpy(targets
[i
], s0
);
333 num_targets
= targetCount
;
335 printf("new targets:\n");
336 for (i = 0; i < num_targets; i++) {
337 printf("target %i: %s\n", i, targets[i]);
342 /* now make target */
343 if (!num_targets
) status
|= make(1, &all
, anyhow
);
344 else status
|= make(num_targets
, (const char **)targets
, anyhow
);
345 /* widely scattered cleanup */
351 if (globs
.cmdout
) fclose(globs
.cmdout
);
352 return status
? EXITBAD
: EXITOK
;