fixed pkg-config rule
[k8jam.git] / src / jam.c
blob5b63872ad585c15c6359c96ea317a565577f5539
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.
15 * jam.c - make redux
17 * See Jam.html for usage information.
19 * These comments document the code.
21 * The top half of the code is structured such:
23 * jam
24 * / | \
25 * +---+ | \
26 * / | \
27 * jamgram option \
28 * / | \ \
29 * / | \ \
30 * / | \ |
31 * scan | compile make
32 * | | / | \ / | \
33 * | | / | \ / | \
34 * | | / | \ / | \
35 * jambase parse | rules search make1
36 * | | | \
37 * | | | \
38 * | | | \
39 * builtins timestamp command execute
40 * |
41 * |
42 * |
43 * filesys
46 * The support routines are called by all of the above, but themselves
47 * are layered thus:
49 * variable|expand
50 * / | | |
51 * / | | |
52 * / | | |
53 * lists | | pathsys
54 * \ | |
55 * \ | |
56 * \ | |
57 * newstr |
58 * \ |
59 * \ |
60 * \ |
61 * hash
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
101 #include "jam.h"
102 #include "option.h"
103 #include "patchlevel.h"
105 /* These get various function declarations. */
106 #include "lists.h"
107 #include "parse.h"
108 #include "variable.h"
109 #include "compile.h"
110 #include "builtins.h"
111 #include "rules.h"
112 #include "newstr.h"
113 #include "scan.h"
114 #include "timestamp.h"
115 #include "make.h"
116 #include "hcache.h"
118 /* And UNIX for this */
119 #ifdef unix
120 # include <sys/utsname.h>
121 #endif
124 struct globs globs = {
125 .noexec = 0,
126 .jobs = 1,
127 .quitquick = 0,
128 .newestfirst = 0, /* newestfirst */
129 .debug = { 0, 1 }, /* display actions */
130 .cmdout = NULL, /* output commands, not run them */
131 //#ifdef OPT_IMPROVED_PROGRESS_EXT
132 .updating = 0,
133 .progress = NULL,
134 //#endif
138 /* symbols to be defined as true for use in Jambase */
139 static const char *othersyms[] = { OSMAJOR, OSMINOR, OSPLAT, JAMVERSYM, 0 } ;
142 /* Known for sure:
143 * OS2 needs extern environ
145 #ifndef use_environ
146 # define use_environ environ
147 # if !defined(__WATCOM__) && !defined(OS_OS2) && !defined(OS_NT)
148 extern char **environ;
149 # endif
150 #endif
153 int main (int argc, char **argv, char **arg_environ) {
154 int n, num_targets;
155 const char *s;
156 struct option optv[N_OPTS];
157 char *targets[N_TARGETS];
158 const char *all = "all";
159 int anyhow = 0;
160 int status;
161 int wasOptH = 0;
163 --argc; ++argv;
164 if ((num_targets = getoptions(argc, argv, "H:d:j:f:gs:t:ano:qvZ", optv, targets)) < 0) {
165 printf("usage: jam [ options ] targets...\n\n"
166 "-a Build all targets, even if they are current.\n"
167 "-dx Display (a)actions (c)causes (d)dependencies\n"
168 " (m)make tree (x)commands (0-9) debug levels.\n"
169 "-fx Read x instead of Jambase.\n"
170 "-g Build from newest sources first.\n"
171 "-jx Run up to x shell commands concurrently.\n"
172 "-n Don't actually execute the updating actions.\n"
173 "-ox Write the updating actions to file x.\n"
174 "-q Quit quickly as soon as a target fails.\n"
175 "-sx=y Set variable x=y, overriding environment.\n"
176 "-tx Rebuild x, even if it is up-to-date.\n"
177 "-v Print the version of jam and exit.\n"
178 "-Hx Show header cache statistics (a)ll, (h)its, (s)tats.\n"
179 "-Z Register old-style all-upper builtins.\n"
181 exit(EXITBAD);
183 /* version info. */
184 if ((s = getoptval(optv, 'v', 0))) {
185 printf("K8-Jam %s. %s. ", VERSION, OSMINOR );
186 printf(
187 "(C) 1993-2003 Christopher Seiwald, see http://www.freetype.org/jam/\n"
188 "changes by Ketmar // Vampire Avalon, psyc://ketmar.no-ip.org/~ketmar\n"
190 return EXITOK;
192 /* pick up interesting options */
193 if ((s = getoptval(optv, 'n', 0))) globs.noexec++, DEBUG_MAKE = DEBUG_MAKEQ = DEBUG_EXEC = 1;
194 if ((s = getoptval(optv, 'q', 0))) globs.quitquick = 1;
195 if ((s = getoptval(optv, 'a', 0))) anyhow++;
196 if ((s = getoptval(optv, 'j', 0))) globs.jobs = atoi(s);
197 if ((s = getoptval(optv, 'g', 0))) globs.newestfirst = 1;
198 if ((s = getoptval(optv, 'Z', 0))) compat_old_builtin_names = 1;
199 /* turn on/off debugging */
200 for (n = 0; (s = getoptval(optv, 'd', n)) != 0; ++n) {
201 int i = atoi(s);
202 /* first -d, turn off defaults. */
203 if (!n) DEBUG_MAKE = DEBUG_MAKEQ = DEBUG_EXEC = 0;
204 /* n turns on levels 1-n */
205 /* +n turns on level n */
206 /* c turns on named display c */
207 if (i < 0 || i >= DEBUG_MAX) {
208 printf( "Invalid debug level '%s'.\n", s );
209 } else if (*s == '+') {
210 globs.debug[i] = 1;
211 } else if (i) {
212 while (i) globs.debug[i--] = 1;
213 } else {
214 while (*s) {
215 switch (*s++) {
216 case 'a': DEBUG_MAKE = DEBUG_MAKEQ = 1; break;
217 case 'c': DEBUG_CAUSES = 1; break;
218 case 'd': DEBUG_DEPENDS = 1; break;
219 case 'm': DEBUG_MAKEPROG = 1; break;
220 case 'x': DEBUG_EXEC = 1; break;
221 case '0': break;
222 default: printf("Invalid debug flag '%c'.\n", s[-1]);
227 /* header cache statistics */
228 for (n = 0; (s = getoptval(optv, 'H', n)) != 0; ++n) {
229 wasOptH = 1;
230 for (; *s; ++s) {
231 switch (*s) {
232 case 'a': optShowHCacheStats = optShowHCacheInfo = 1; break;
233 case 'h': optShowHCacheInfo = 1; break;
234 case 's': optShowHCacheStats = 1; break;
238 if (wasOptH && (optShowHCacheStats == 0 && optShowHCacheInfo == 0)) optShowHCacheStats = optShowHCacheInfo = 1;
239 /* set JAMDATE first */
241 char buf[128], *p;
242 time_t clock;
244 time(&clock);
245 strcpy(buf, ctime(&clock));
246 /* trim newline from date */
247 p = buf+strlen(buf);
248 while (p >= buf && *((unsigned char *)p) <= ' ') --p;
249 *(++p) = '\0';
250 var_set("JAMDATE", list_new(L0, buf, 0), VAR_SET);
252 /* And JAMUNAME */
253 #ifdef unix
255 struct utsname u;
257 if (uname(&u) >= 0) {
258 LIST *l = L0;
259 l = list_new(l, u.machine, 0);
260 l = list_new(l, u.version, 0);
261 l = list_new(l, u.release, 0);
262 l = list_new(l, u.nodename, 0);
263 l = list_new(l, u.sysname, 0);
264 var_set("JAMUNAME", l, VAR_SET);
267 #endif /* unix */
268 /* Jam defined variables OS, OSPLAT */
269 var_defines(othersyms, 1);
270 /* load up environment variables */
271 var_defines((const char **)use_environ, 0);
272 /* load up variables set on command line. */
273 for (n = 0; (s = getoptval(optv, 's', n)) != 0; ++n) {
274 const char *symv[2];
275 symv[0] = s;
276 symv[1] = 0;
277 var_defines(symv, 1);
279 /* add JAMCMDARGS */
281 LIST *l0 = L0, *l1 = L0;
283 if (num_targets < 1) {
284 l0 = list_new(l0, "all", 0);
285 l1 = list_new(l1, "all", 0);
286 } else {
287 for (n = 0; n < num_targets; ++n) {
288 /*printf("target %i: %s\n", n, targets[n]);*/
289 l0 = list_new(l0, targets[n], 0);
290 l1 = list_new(l1, targets[n], 0);
293 var_set("JAMCMDARGS", l0, VAR_SET);
294 var_set("JAM_TARGETS", l1, VAR_SET); /* k8 */
296 /* initialize built-in rules */
297 load_builtins();
298 /* parse ruleset */
299 for (n = 0; (s = getoptval(optv, 'f', n)) != 0; ++n) parse_file(s);
300 if (!n) parse_file("::Jambase");
301 status = yyanyerrors();
302 /* manually touch -t targets */
303 for (n = 0; (s = getoptval(optv, 't', n)) != 0; ++n) touchtarget(s);
304 /* if an output file is specified, set globs.cmdout to that */
305 if ((s = getoptval(optv, 'o', 0)) != 0) {
306 if (!(globs.cmdout = fopen(s, "w"))) {
307 printf("Failed to write to '%s'\n", s);
308 exit(EXITBAD);
310 ++globs.noexec;
312 #ifndef NO_OPT_JAM_TARGETS_VARIABLE_EXT
313 /* ported from Haiku */
314 /* get value of variable JAM_TARGETS and build the targets */
316 LIST *l = var_get("JAM_TARGETS");
317 int targetCount = list_length(l);
318 int i;
320 if (targetCount == 0) {
321 /* no targets: nothing to do */
322 printf("No targets. Nothing to do.\n");
323 exit(EXITOK);
325 if (targetCount >= N_TARGETS) {
326 printf("ERROR: Too many targets!\n");
327 exit(EXITBAD);
329 for (i = 0; i < targetCount; ++i) {
330 const char *s0 = l->string;
331 targets[i] = malloc((strlen(s0)+1)*sizeof(char));
332 if (!targets[i]) {
333 printf("ERROR: Out of memory!\n");
334 exit(EXITBAD);
336 strcpy(targets[i], s0);
337 l = l->next;
339 num_targets = targetCount;
341 printf("new targets:\n");
342 for (i = 0; i < num_targets; i++) {
343 printf("target %i: %s\n", i, targets[i]);
347 #endif
348 /* now make target */
349 if (!num_targets) status |= make(1, &all, anyhow);
350 else status |= make(num_targets, (const char **)targets, anyhow);
351 /* widely scattered cleanup */
352 var_done();
353 donerules();
354 donestamps();
355 donestr();
356 /* close cmdout */
357 if (globs.cmdout) fclose(globs.cmdout);
358 return status ? EXITBAD : EXITOK;