added "jammod" command and "genman" module
[k8jam.git] / src / jam.c
blob4f631282497cf4edb28de957eeb42e6beef7b503
1 /*
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
6 * are clearly marked.
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/>.
26 * jam.c - make redux
28 * See Jam.html for usage information.
30 * These comments document the code.
32 * The top half of the code is structured such:
34 * jam
35 * / | \
36 * +---+ | \
37 * / | \
38 * jamgram option \
39 * / | \ \
40 * / | \ \
41 * / | \ |
42 * scan | compile make
43 * | | / | \ / | \
44 * | | / | \ / | \
45 * | | / | \ / | \
46 * jambase parse | rules search make1
47 * | | | \
48 * | | | \
49 * | | | \
50 * builtins timestamp command execute
51 * |
52 * |
53 * |
54 * filesys
57 * The support routines are called by all of the above, but themselves
58 * are layered thus:
60 * variable|expand
61 * / | | |
62 * / | | |
63 * / | | |
64 * lists | | pathsys
65 * \ | |
66 * \ | |
67 * \ | |
68 * newstr |
69 * \ |
70 * \ |
71 * \ |
72 * hash
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
101 #include "jam.h"
102 #include "jambase.h"
103 #include "option.h"
104 #include "patchlevel.h"
106 /* These get various function declarations. */
107 #include "lists.h"
108 #include "parse.h"
109 #include "variable.h"
110 #include "compile.h"
111 #include "builtins.h"
112 #include "rules.h"
113 #include "newstr.h"
114 #include "scan.h"
115 #include "timestamp.h"
116 #include "make.h"
117 #include "hcache.h"
119 #include "genman.h"
121 /* And UNIX for this */
122 #ifdef unix
123 # include <sys/utsname.h>
124 #endif
127 #ifdef USE_DIETLIBC
128 char *stpcpy (char *dst, const char *src) {
129 while ((*dst++ = *src++));
130 return (dst-1);
132 #endif
135 struct globs globs = {
136 .noexec = 0,
137 .jobs = 1,
138 .quitquick = 0,
139 .newestfirst = 0, /* newestfirst */
140 .debug = { 0, 1 }, /* display actions */
141 .cmdout = NULL, /* output commands, not run them */
142 //#ifdef OPT_IMPROVED_PROGRESS_EXT
143 .updating = 0,
144 .progress = NULL,
145 //#endif
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 } ;
153 /* Known for sure:
154 * OS2 needs extern environ
156 #ifndef use_environ
157 # define use_environ environ
158 # if !defined(__WATCOM__) && !defined(OS_NT)
159 extern char **environ;
160 # endif
161 #endif
164 static void fixJobs (void) {
165 LIST *var = var_get("K8JAM-JOBS"); /*FIXME: a perfectly idiotic name*/
166 char buf[128];
168 if (var) {
169 int cnt = list_length(var);
171 if (cnt == 0) {
172 globs.jobs = 1;
173 } else {
174 int jj = atoi(var->string);
176 if (jj < 1) jj = 1; else if (jj > 64) jj = 64;
177 globs.jobs = jj;
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) {
189 int n, num_targets;
190 const char *s;
191 struct option optv[N_OPTS];
192 char *targets[N_TARGETS];
193 const char *all = "all";
194 int anyhow = 0;
195 int status;
196 int wasOptH = 0;
197 const char *mybin = argv[0];
198 if (!mybin) mybin = "k8jam";
200 --argc; ++argv;
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"
217 "\ndebug levels:\n"
218 " 1 same as -da, but don't show quiet actions\n"
219 " 2 nothing\n"
220 " 3 same as -dm\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"
227 "\nmodules:\n"
228 " jammod genman infile.txt [outfile.man]\n"
230 exit(EXITBAD);
232 /* version info. */
233 if ((s = getoptval(optv, 'v', 0))) {
234 printf("K8Jam %s %s\n", VERSION, OSMINOR);
235 printf(
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"
240 exit(EXITOK);
242 /* pick up interesting options */
243 if ((s = getoptval(optv, 'W', 0))) {
244 FILE *fl = fopen("Jambase", "w");
245 if (fl != NULL) {
246 printf("writing Jambase...\n");
247 jambase_unpack();
248 for (int f = 0; jambase[f]; ++f) fprintf(fl, "%s", jambase[f]);
249 fclose(fl);
251 exit(EXITOK);
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) {
260 int i = atoi(s);
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 == '+') {
269 globs.debug[i] = 1;
270 } else if (i) {
271 for (; i > 0; --i) globs.debug[i] = 1;
272 } else {
273 while (*s) {
274 switch (*s++) {
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;
282 case '0': 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) {
290 wasOptH = 1;
291 for (; *s; ++s) {
292 switch (*s) {
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 */
302 char buf[128], *p;
303 time_t clock;
305 time(&clock);
306 strcpy(buf, ctime(&clock));
307 /* trim newline from date */
308 p = buf+strlen(buf);
309 while (p >= buf && *((unsigned char *)p) <= ' ') --p;
310 *(++p) = '\0';
311 var_set("JAMDATE", list_new(L0, buf, 0), VAR_SET);
313 /* And JAMUNAME */
314 #ifdef unix
316 struct utsname u;
318 if (uname(&u) >= 0) {
319 LIST *l = L0;
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);
328 #endif /* unix */
329 /* run modules */
330 if (num_targets > 0 && strcmp(targets[0], "jammod") == 0) {
331 if (num_targets < 2) {
332 fprintf(stderr, "ERROR: module name missing!\n");
333 exit(EXITBAD);
335 if (strcmp(targets[1], "genman") == 0) {
336 if (num_targets < 3 || num_targets > 4) {
337 fprintf(stderr, "ERROR: invalid genman module invocation!\n");
338 exit(EXITBAD);
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");
346 } else {
347 strcpy(ext, ".man");
349 } else {
350 strcpy(destmanfile, targets[3]);
352 if (DEBUG_EXEC) printf("jammod genman \"%s\" \"%s\"\n", targets[2], destmanfile);
353 if (!globs.noexec) {
354 generate_man(targets[2], destmanfile);
356 exit(EXITOK);
358 fprintf(stderr, "ERROR: unknown module '%s'!\n", targets[1]);
359 exit(EXITBAD);
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) {
367 const char *symv[2];
368 symv[0] = s;
369 symv[1] = 0;
370 var_defines(symv, 1);
372 var_set("JAM_BINARY", list_new(L0, mybin, 0), VAR_SET);
373 /* add JAMCMDARGS */
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);
380 } else {
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);
388 /* k8 */
389 var_set("JAM_TARGETS", l1, VAR_SET);
391 /* add JAMCONFIGARGS */
393 LIST *l0 = L0;
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 */
399 load_builtins();
400 /* parse ruleset */
401 for (n = 0; (s = getoptval(optv, 'f', n)) != 0; ++n) parse_file(s);
402 if (!n) parse_file("::Jambase");
403 status = 0; /*yyanyerrors();*/
404 if (status) {
405 printf("FATAL: parsing error occured!\n");
406 exit(EXITBAD);
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);
414 exit(EXITBAD);
416 ++globs.noexec;
418 /* fix number of jobs (if necessary) */
419 fixJobs();
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);
426 int i;
428 if (targetCount == 0) {
429 /* no targets: nothing to do */
430 printf("No targets. Nothing to do.\n");
431 exit(EXITOK);
433 if (targetCount >= N_TARGETS) {
434 printf("ERROR: Too many targets!\n");
435 exit(EXITBAD);
437 for (i = 0; i < targetCount; ++i) {
438 const char *s0 = l->string;
439 targets[i] = malloc((strlen(s0)+1)*sizeof(char));
440 if (!targets[i]) {
441 printf("ERROR: Out of memory!\n");
442 exit(EXITBAD);
444 strcpy(targets[i], s0);
445 l = l->next;
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]);
455 #endif
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();
463 //#endif
464 regexp_done();
465 var_done();
466 donerules();
467 donestamps();
468 donestr();
469 /* close cmdout */
470 if (globs.cmdout) fclose(globs.cmdout);
471 return (status ? EXITBAD : EXITOK);