2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
4 * License is hereby granted to use this software and distribute it
5 * freely, as long as this copyright notice is retained and modifications
8 * ALL WARRANTIES ARE HEREBY DISCLAIMED.
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * See Jam.html for usage information.
30 * These comments document the code.
32 * The top half of the code is structured such:
46 * jambase parse | rules search make1
50 * builtins timestamp command execute
57 * The support routines are called by all of the above, but themselves
74 * Roughly, the modules are:
76 * builtins.c - jam's built-in rules
77 * command.c - maintain lists of commands
78 * compile.c - compile parsed jam statements
79 * execcmd.c - execute a shell script on UNIX
80 * expand.c - expand a buffer, given variable values
81 * file*.c - scan directories and archives on *
82 * hash.c - simple in-memory hashing routines
83 * headers.c - handle #includes in source files
84 * jambase.c - compilable copy of Jambase
85 * jamgram.y - jam grammar
86 * lists.c - maintain lists of strings
87 * make.c - bring a target up to date, once rules are in place
88 * make1.c - execute command to bring targets up to date
89 * newstr.c - string manipulation routines
90 * option.c - command line option processing
91 * parse.c - make and destroy parse trees as driven by the parser
92 * path*.c - manipulate file names on *
93 * hash.c - simple in-memory hashing routines
94 * re9.c - regexp engine
95 * rules.c - access to RULEs, TARGETs, and ACTIONs
96 * scan.c - the jam yacc scanner
97 * search.c - find a target along $(SEARCH) or $(LOCATE)
98 * timestamp.c - get the timestamp of a file or archive member
99 * variable.c - handle jam multi-element variables
104 #include "patchlevel.h"
106 /* These get various function declarations. */
109 #include "variable.h"
111 #include "builtins.h"
115 #include "timestamp.h"
121 /* And UNIX for this */
123 # include <sys/utsname.h>
128 char *stpcpy (char *dst
, const char *src
) {
129 while ((*dst
++ = *src
++));
135 struct globs globs
= {
139 .newestfirst
= 0, /* newestfirst */
140 .debug
= { 0, 1 }, /* display actions */
141 .cmdout
= NULL
, /* output commands, not run them */
142 //#ifdef OPT_IMPROVED_PROGRESS_EXT
149 /* symbols to be defined as true for use in Jambase */
150 static const char *othersyms
[] = { OSMAJOR
, OSMINOR
, OSPLAT
, JAMVERSYM
, JAMVERSYM_HI
, JAMVERSYM_MID
, JAMVERSYM_LO
, 0 } ;
154 * OS2 needs extern environ
157 # define use_environ environ
158 # if !defined(__WATCOM__) && !defined(OS_NT)
159 extern char **environ
;
164 static void fixJobs (void) {
165 LIST
*var
= var_get("K8JAM-JOBS"); /*FIXME: a perfectly idiotic name*/
169 int cnt
= list_length(var
);
174 int jj
= atoi(var
->string
);
176 if (jj
< 1) jj
= 1; else if (jj
> 64) jj
= 64;
181 if (globs
.jobs
< 1) globs
.jobs
= 1; else if (globs
.jobs
> 64) globs
.jobs
= 64;
182 snprintf(buf
, sizeof(buf
), "%d", globs
.jobs
);
183 var
= list_new(L0
, buf
, 0);
184 var_set("K8JAM-JOBS", var
, VAR_SET
);
188 int main (int argc
, char **argv
, char **arg_environ
) {
191 struct option optv
[N_OPTS
];
192 char *targets
[N_TARGETS
];
193 const char *all
= "all";
197 const char *mybin
= argv
[0];
198 if (!mybin
) mybin
= "k8jam";
201 if ((num_targets
= getoptions(argc
, argv
, "H:d:j:f:gs:t:ano:qvZW", optv
, targets
)) < 0) {
202 printf("usage: jam [ options ] targets...\n\n"
203 "-a Build all targets, even if they are current.\n"
204 "-dx Display (a)actions (c)causes (d)dependencies\n"
205 " (m)make tree (x)commands ([+]0-9) debug levels.\n"
206 "-fx Read x instead of Jambase.\n"
207 "-g Build from newest sources first.\n"
208 "-jx Run up to x shell commands concurrently.\n"
209 "-n Don't actually execute the updating actions.\n"
210 "-ox Write the updating actions to file x.\n"
211 "-q Quit quickly as soon as a target fails.\n"
212 "-sx=y Set variable x=y, overriding environment.\n"
213 "-tx Rebuild x, even if it is up-to-date.\n"
214 "-v Print the version of jam and exit.\n"
215 "-Hx Show header cache statistics (a)ll, (h)its, (s)tats.\n"
216 "-W Write built-in Jambase and exit.\n"
218 " 1 same as -da, but don't show quiet actions\n"
221 " 4 execcmds()'s work\n"
222 " 5 rule invocations\n"
223 " 6 result of header scan, dir scan and attempts at binding\n"
224 " 7 variable settings\n"
225 " 8 variable fetches and expansions, 'if' calculations\n"
226 " 9 list manipulation, scanner tokens, memory use\n"
228 " jammod genman infile.txt [outfile.man]\n"
233 if ((s
= getoptval(optv
, 'v', 0))) {
234 printf("K8Jam %s %s\n", VERSION
, OSMINOR
);
236 "(C) 1993-2003 Christopher Seiwald at all\n"
237 "changes by Ketmar // Vampire Avalon, psyc://ketmar.no-ip.org/~ketmar\n"
238 "http://repo.or.cz/w/k8jam.git\n"
242 /* pick up interesting options */
243 if ((s
= getoptval(optv
, 'W', 0))) {
244 FILE *fl
= fopen("Jambase", "w");
246 printf("writing Jambase...\n");
248 for (int f
= 0; jambase
[f
]; ++f
) fprintf(fl
, "%s", jambase
[f
]);
253 if ((s
= getoptval(optv
, 'n', 0))) ++globs
.noexec
, DEBUG_MAKE
= DEBUG_MAKEQ
= DEBUG_EXEC
= 1;
254 if ((s
= getoptval(optv
, 'q', 0))) globs
.quitquick
= 1;
255 if ((s
= getoptval(optv
, 'a', 0))) ++anyhow
;
256 if ((s
= getoptval(optv
, 'j', 0))) globs
.jobs
= atoi(s
);
257 if ((s
= getoptval(optv
, 'g', 0))) globs
.newestfirst
= 1;
258 /* turn on/off debugging */
259 for (n
= 0; (s
= getoptval(optv
, 'd', n
)) != 0; ++n
) {
261 /* first -d, turn off defaults. */
262 if (!n
) DEBUG_MAKE
= DEBUG_MAKEQ
= DEBUG_EXEC
= 0;
263 /* n turns on levels 1-n */
264 /* +n turns on level n */
265 /* c turns on named display c */
266 if (i
< 0 || i
>= DEBUG_MAX
) {
267 printf( "Invalid debug level '%s'.\n", s
);
268 } else if (*s
== '+') {
271 for (; i
> 0; --i
) globs
.debug
[i
] = 1;
275 case 'a': DEBUG_MAKE
= DEBUG_MAKEQ
= 1; break;
276 case 'c': DEBUG_CAUSES
= 1; break;
277 case 'd': DEBUG_DEPENDS
= 1; break;
278 case 'm': DEBUG_MAKEPROG
= 1; break;
279 case 'x': DEBUG_EXEC
= 1; break;
280 case 'h': DEBUG_HEADER
= 1; break;
281 case 's': DEBUG_SCAN
= 1; break;
283 default: printf("Invalid debug flag '%c'.\n", s
[-1]);
288 /* header cache statistics */
289 for (n
= 0; (s
= getoptval(optv
, 'H', n
)) != 0; ++n
) {
293 case 'a': optShowHCacheStats
= optShowHCacheInfo
= 1; break;
294 case 'h': optShowHCacheInfo
= 1; break;
295 case 's': optShowHCacheStats
= 1; break;
299 if (wasOptH
&& (optShowHCacheStats
== 0 && optShowHCacheInfo
== 0)) optShowHCacheStats
= optShowHCacheInfo
= 1;
300 /* set JAMDATE first */
306 strcpy(buf
, ctime(&clock
));
307 /* trim newline from date */
309 while (p
>= buf
&& *((unsigned char *)p
) <= ' ') --p
;
311 var_set("JAMDATE", list_new(L0
, buf
, 0), VAR_SET
);
318 if (uname(&u
) >= 0) {
320 l
= list_new(l
, u
.machine
, 0);
321 l
= list_new(l
, u
.version
, 0);
322 l
= list_new(l
, u
.release
, 0);
323 l
= list_new(l
, u
.nodename
, 0);
324 l
= list_new(l
, u
.sysname
, 0);
325 var_set("JAMUNAME", l
, VAR_SET
);
330 if (num_targets
> 0 && strcmp(targets
[0], "jammod") == 0) {
331 if (num_targets
< 2) {
332 fprintf(stderr
, "ERROR: module name missing!\n");
335 if (strcmp(targets
[1], "genman") == 0) {
336 if (num_targets
< 3 || num_targets
> 4) {
337 fprintf(stderr
, "ERROR: invalid genman module invocation!\n");
340 static char destmanfile
[4096];
341 if (num_targets
== 3) {
342 strcpy(destmanfile
, targets
[2]);
343 char *ext
= strrchr(destmanfile
, '.');
344 if (!ext
|| strchr(ext
, '/')) {
345 strcat(destmanfile
, ".man");
350 strcpy(destmanfile
, targets
[3]);
352 if (DEBUG_EXEC
) printf("jammod genman \"%s\" \"%s\"\n", targets
[2], destmanfile
);
354 generate_man(targets
[2], destmanfile
);
358 fprintf(stderr
, "ERROR: unknown module '%s'!\n", targets
[1]);
361 /* load up environment variables */
362 var_defines((const char **)use_environ
, 0);
363 /* Jam defined variables OS, OSPLAT */
364 var_defines(othersyms
, 1);
365 /* load up variables set on command line. */
366 for (n
= 0; (s
= getoptval(optv
, 's', n
)) != 0; ++n
) {
370 var_defines(symv
, 1);
372 var_set("JAM_BINARY", list_new(L0
, mybin
, 0), VAR_SET
);
375 LIST
*l0
= L0
, *l1
= L0
;
377 if (num_targets
< 1) {
378 l0
= list_new(l0
, "all", 0);
379 l1
= list_new(l1
, "all", 0);
381 for (n
= 0; n
< num_targets
; ++n
) {
382 //printf("target %i: %s\n", n, targets[n]);
383 l0
= list_new(l0
, targets
[n
], 0);
384 l1
= list_new(l1
, targets
[n
], 0);
387 var_set("JAMCMDARGS", l0
, VAR_SET
);
389 var_set("JAM_TARGETS", l1
, VAR_SET
);
391 /* add JAMCONFIGARGS */
395 for (n
= 0; n
< num_cfgargs
; ++n
) l0
= list_new(l0
, cfgargs
[n
], 0);
396 var_set("JAMCONFIGARGS", l0
, VAR_SET
);
398 /* initialize built-in rules */
401 for (n
= 0; (s
= getoptval(optv
, 'f', n
)) != 0; ++n
) parse_file(s
);
402 if (!n
) parse_file("::Jambase");
403 status
= 0; /*yyanyerrors();*/
405 printf("FATAL: parsing error occured!\n");
408 /* manually touch -t targets */
409 for (n
= 0; (s
= getoptval(optv
, 't', n
)) != 0; ++n
) touchtarget(s
);
410 /* if an output file is specified, set globs.cmdout to that */
411 if ((s
= getoptval(optv
, 'o', 0)) != 0) {
412 if (!(globs
.cmdout
= fopen(s
, "w"))) {
413 printf("Failed to write to '%s'\n", s
);
418 /* fix number of jobs (if necessary) */
420 #ifndef NO_OPT_JAM_TARGETS_VARIABLE_EXT
421 /* ported from Haiku */
422 /* get value of variable JAM_TARGETS and build the targets */
424 LIST
*l
= var_get("JAM_TARGETS");
425 int targetCount
= list_length(l
);
428 if (targetCount
== 0) {
429 /* no targets: nothing to do */
430 printf("No targets. Nothing to do.\n");
433 if (targetCount
>= N_TARGETS
) {
434 printf("ERROR: Too many targets!\n");
437 for (i
= 0; i
< targetCount
; ++i
) {
438 const char *s0
= l
->string
;
439 targets
[i
] = malloc((strlen(s0
)+1)*sizeof(char));
441 printf("ERROR: Out of memory!\n");
444 strcpy(targets
[i
], s0
);
447 num_targets
= targetCount
;
449 printf("new targets:\n");
450 for (i = 0; i < num_targets; i++) {
451 printf("target %i: %s\n", i, targets[i]);
456 /* now make target */
457 if (!num_targets
) status
|= make(1, &all
, anyhow
);
458 else status
|= make(num_targets
, (const char **)targets
, anyhow
);
459 /* widely scattered cleanup */
460 //#ifdef OPT_HEADER_CACHE_EXT
461 /* note that cache will not be written if it is not updated, so 'clean' will work ok */
462 if (!globs
.noexec
) hcache_done();
470 if (globs
.cmdout
) fclose(globs
.cmdout
);
471 return (status
? EXITBAD
: EXITOK
);