mkmakefile.c revision 1.8 1 /* $NetBSD: mkmakefile.c,v 1.8 2009/02/15 01:39:54 cube Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratories.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * from: @(#)mkmakefile.c 8.1 (Berkeley) 6/6/93
41 */
42
43 #if HAVE_NBTOOL_CONFIG_H
44 #include "nbtool_config.h"
45 #endif
46
47 #include <sys/param.h>
48 #include <ctype.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <err.h>
54 #include "defs.h"
55 #include "sem.h"
56
57 /*
58 * Make the Makefile.
59 */
60
61 static const char *srcpath(struct files *);
62
63 static const char *prefix_prologue(const char *);
64 static const char *filetype_prologue(struct filetype *);
65
66
67 static void emitdefs(FILE *);
68 static void emitfiles(FILE *, int, int);
69
70 static void emitobjs(FILE *);
71 static void emitcfiles(FILE *);
72 static void emitsfiles(FILE *);
73 static void emitrules(FILE *);
74 static void emitload(FILE *);
75 static void emitincludes(FILE *);
76 static void emitappmkoptions(FILE *);
77 static void emitsubs(FILE *, const char *, const char *, int);
78
79 int
80 mkmakefile(void)
81 {
82 FILE *ifp, *ofp;
83 int lineno;
84 void (*fn)(FILE *);
85 char *ifname;
86 char line[BUFSIZ], buf[200];
87
88 /* Try a makefile for the port first.
89 */
90 (void)snprintf(buf, sizeof(buf), "arch/%s/conf/Makefile.%s",
91 machine, machine);
92 ifname = sourcepath(buf);
93 if ((ifp = fopen(ifname, "r")) == NULL) {
94 /* Try a makefile for the architecture second.
95 */
96 (void)snprintf(buf, sizeof(buf), "arch/%s/conf/Makefile.%s",
97 machinearch, machinearch);
98 free(ifname);
99 ifname = sourcepath(buf);
100 ifp = fopen(ifname, "r");
101 }
102 if (ifp == NULL) {
103 warn("cannot read %s", ifname);
104 goto bad2;
105 }
106
107 if ((ofp = fopen("Makefile.tmp", "w")) == NULL) {
108 warn("cannot write Makefile");
109 goto bad1;
110 }
111
112 emitdefs(ofp);
113
114 lineno = 0;
115 while (fgets(line, sizeof(line), ifp) != NULL) {
116 lineno++;
117 if (version < 20090214 && line[0] != '%') {
118 fputs(line, ofp);
119 continue;
120 }
121 if (strcmp(line, "%OBJS\n") == 0)
122 fn = emitobjs;
123 else if (strcmp(line, "%CFILES\n") == 0)
124 fn = emitcfiles;
125 else if (strcmp(line, "%SFILES\n") == 0)
126 fn = emitsfiles;
127 else if (strcmp(line, "%RULES\n") == 0)
128 fn = emitrules;
129 else if (strcmp(line, "%LOAD\n") == 0)
130 fn = emitload;
131 else if (strcmp(line, "%INCLUDES\n") == 0)
132 fn = emitincludes;
133 else if (strcmp(line, "%MAKEOPTIONSAPPEND\n") == 0)
134 fn = emitappmkoptions;
135 else if (strncmp(line, "%VERSION ", sizeof("%VERSION ")-1) == 0) {
136 int newvers;
137 if (sscanf(line, "%%VERSION %d\n", &newvers) != 1) {
138 cfgxerror(ifname, lineno, "syntax error for "
139 "%%VERSION");
140 } else
141 setversion(newvers);
142 continue;
143 } else {
144 if (version < 20090214)
145 cfgxerror(ifname, lineno,
146 "unknown %% construct ignored: %s", line);
147 else
148 emitsubs(ofp, line, ifname, lineno);
149 continue;
150 }
151 (*fn)(ofp);
152 }
153
154 fflush(ofp);
155 if (ferror(ofp))
156 goto wrerror;
157
158 if (ferror(ifp)) {
159 warn("error reading %s (at line %d)", ifname, lineno);
160 goto bad;
161 }
162
163 if (fclose(ofp)) {
164 ofp = NULL;
165 goto wrerror;
166 }
167 (void)fclose(ifp);
168
169 if (moveifchanged("Makefile.tmp", "Makefile") != 0) {
170 warn("error renaming Makefile");
171 goto bad2;
172 }
173 free(ifname);
174 return (0);
175
176 wrerror:
177 warn("error writing Makefile");
178 bad:
179 if (ofp != NULL)
180 (void)fclose(ofp);
181 bad1:
182 (void)fclose(ifp);
183 /* (void)unlink("Makefile.tmp"); */
184 bad2:
185 free(ifname);
186 return (1);
187 }
188
189 static void
190 emitsubs(FILE *fp, const char *line, const char *file, int lineno)
191 {
192 char *nextpct, *optname;
193 struct nvlist *option;
194
195 while (*line != '\0') {
196 if (*line != '%') {
197 fputc(*line++, fp);
198 continue;
199 }
200
201 line++;
202 nextpct = strchr(line, '%');
203 if (nextpct == NULL) {
204 cfgxerror(file, lineno, "unbalanced %% or "
205 "unknown construct");
206 return;
207 }
208 *nextpct = '\0';
209
210 if (*line == '\0')
211 fputc('%', fp);
212 else {
213 optname = intern(line);
214 if (!DEFINED_OPTION(optname)) {
215 cfgxerror(file, lineno, "unknown option %s",
216 optname);
217 return;
218 }
219
220 if ((option = ht_lookup(opttab, optname)) == NULL)
221 option = ht_lookup(fsopttab, optname);
222 if (option != NULL)
223 fputs(option->nv_str ? option->nv_str : "1",
224 fp);
225 /* Otherwise it's not a selected option and we don't
226 * output anything. */
227 }
228
229 line = nextpct+1;
230 }
231 }
232
233 /*
234 * Return (possibly in a static buffer) the name of the `source' for a
235 * file. If we have `options source', or if the file is marked `always
236 * source', this is always the path from the `file' line; otherwise we
237 * get the .o from the obj-directory.
238 */
239 static const char *
240 srcpath(struct files *fi)
241 {
242 #if 1
243 /* Always have source, don't support object dirs for kernel builds. */
244 return (fi->fi_path);
245 #else
246 static char buf[MAXPATHLEN];
247
248 if (have_source || (fi->fi_flags & FI_ALWAYSSRC) != 0)
249 return (fi->fi_path);
250 if (objpath == NULL) {
251 cfgerror("obj-directory not set");
252 return (NULL);
253 }
254 (void)snprintf(buf, sizeof buf, "%s/%s.o", objpath, fi->fi_base);
255 return (buf);
256 #endif
257 }
258
259 static const char *
260 filetype_prologue(struct filetype *fit)
261 {
262 if (fit->fit_flags & FIT_NOPROLOGUE || *fit->fit_path == '/')
263 return ("");
264 else
265 return ("$S/");
266 }
267
268 static const char *
269 prefix_prologue(const char *path)
270 {
271 if (*path == '/')
272 return ("");
273 else
274 return ("$S/");
275 }
276
277 static void
278 emitdefs(FILE *fp)
279 {
280 struct nvlist *nv;
281 char *sp;
282
283 fprintf(fp, "KERNEL_BUILD=%s\n", conffile);
284 fputs("IDENT=", fp);
285 sp = "";
286 for (nv = options; nv != NULL; nv = nv->nv_next) {
287
288 /* skip any options output to a header file */
289 if (DEFINED_OPTION(nv->nv_name))
290 continue;
291 fprintf(fp, "%s-D%s", sp, nv->nv_name);
292 if (nv->nv_str)
293 fprintf(fp, "=\"%s\"", nv->nv_str);
294 sp = " ";
295 }
296 putc('\n', fp);
297 fprintf(fp, "PARAM=-DMAXUSERS=%d\n", maxusers);
298 fprintf(fp, "MACHINE=%s\n", machine);
299 if (*srcdir == '/' || *srcdir == '.') {
300 fprintf(fp, "S=\t%s\n", srcdir);
301 } else {
302 /*
303 * libkern and libcompat "Makefile.inc"s want relative S
304 * specification to begin with '.'.
305 */
306 fprintf(fp, "S=\t./%s\n", srcdir);
307 }
308 for (nv = mkoptions; nv != NULL; nv = nv->nv_next)
309 fprintf(fp, "%s=%s\n", nv->nv_name, nv->nv_str);
310 }
311
312 static void
313 emitobjs(FILE *fp)
314 {
315 struct files *fi;
316 struct objects *oi;
317 int lpos, len, sp;
318
319 fputs("OBJS=", fp);
320 sp = '\t';
321 lpos = 7;
322 TAILQ_FOREACH(fi, &allfiles, fi_next) {
323 if ((fi->fi_flags & FI_SEL) == 0)
324 continue;
325 len = strlen(fi->fi_base) + 2;
326 if (lpos + len > 72) {
327 fputs(" \\\n", fp);
328 sp = '\t';
329 lpos = 7;
330 }
331 fprintf(fp, "%c%s.o", sp, fi->fi_base);
332 lpos += len + 1;
333 sp = ' ';
334 }
335 TAILQ_FOREACH(oi, &allobjects, oi_next) {
336 if ((oi->oi_flags & OI_SEL) == 0)
337 continue;
338 len = strlen(oi->oi_path);
339 if (*oi->oi_path != '/')
340 {
341 /* e.g. "$S/" */
342 if (oi->oi_prefix != NULL)
343 len += strlen(prefix_prologue(oi->oi_path)) +
344 strlen(oi->oi_prefix) + 1;
345 else
346 len += strlen(filetype_prologue(&oi->oi_fit));
347 }
348 if (lpos + len > 72) {
349 fputs(" \\\n", fp);
350 sp = '\t';
351 lpos = 7;
352 }
353 if (*oi->oi_path == '/') {
354 fprintf(fp, "%c%s", sp, oi->oi_path);
355 } else {
356 if (oi->oi_prefix != NULL) {
357 fprintf(fp, "%c%s%s/%s", sp,
358 prefix_prologue(oi->oi_path),
359 oi->oi_prefix, oi->oi_path);
360 } else {
361 fprintf(fp, "%c%s%s", sp,
362 filetype_prologue(&oi->oi_fit),
363 oi->oi_path);
364 }
365 }
366 lpos += len + 1;
367 sp = ' ';
368 }
369 putc('\n', fp);
370 }
371
372 static void
373 emitcfiles(FILE *fp)
374 {
375
376 emitfiles(fp, 'c', 0);
377 }
378
379 static void
380 emitsfiles(FILE *fp)
381 {
382
383 emitfiles(fp, 's', 'S');
384 }
385
386 static void
387 emitfiles(FILE *fp, int suffix, int upper_suffix)
388 {
389 struct files *fi;
390 int lpos, len, sp;
391 const char *fpath;
392 struct config *cf;
393 char swapname[100];
394
395 fprintf(fp, "%cFILES=", toupper(suffix));
396 sp = '\t';
397 lpos = 7;
398 TAILQ_FOREACH(fi, &allfiles, fi_next) {
399 if ((fi->fi_flags & FI_SEL) == 0)
400 continue;
401 fpath = srcpath(fi);
402 len = strlen(fpath);
403 if (fpath[len - 1] != suffix && fpath[len - 1] != upper_suffix)
404 continue;
405 if (*fpath != '/') {
406 /* "$S/" */
407 if (fi->fi_prefix != NULL)
408 len += strlen(prefix_prologue(fi->fi_prefix)) +
409 strlen(fi->fi_prefix) + 1;
410 else
411 len += strlen(filetype_prologue(&fi->fi_fit));
412 }
413 if (lpos + len > 72) {
414 fputs(" \\\n", fp);
415 sp = '\t';
416 lpos = 7;
417 }
418 if (*fi->fi_path == '/') {
419 fprintf(fp, "%c%s", sp, fpath);
420 } else {
421 if (fi->fi_prefix != NULL) {
422 fprintf(fp, "%c%s%s/%s", sp,
423 prefix_prologue(fi->fi_prefix),
424 fi->fi_prefix, fpath);
425 } else {
426 fprintf(fp, "%c%s%s", sp,
427 filetype_prologue(&fi->fi_fit),
428 fpath);
429 }
430 }
431 lpos += len + 1;
432 sp = ' ';
433 }
434 /*
435 * The allfiles list does not include the configuration-specific
436 * C source files. These files should be eliminated someday, but
437 * for now, we have to add them to ${CFILES} (and only ${CFILES}).
438 */
439 if (suffix == 'c') {
440 TAILQ_FOREACH(cf, &allcf, cf_next) {
441 (void)snprintf(swapname, sizeof(swapname), "swap%s.c",
442 cf->cf_name);
443 len = strlen(swapname);
444 if (lpos + len > 72) {
445 fputs(" \\\n", fp);
446 sp = '\t';
447 lpos = 7;
448 }
449 fprintf(fp, "%c%s", sp, swapname);
450 lpos += len + 1;
451 sp = ' ';
452 }
453 }
454 putc('\n', fp);
455 }
456
457 /*
458 * Emit the make-rules.
459 */
460 static void
461 emitrules(FILE *fp)
462 {
463 struct files *fi;
464 const char *cp, *fpath;
465 int ch;
466
467 TAILQ_FOREACH(fi, &allfiles, fi_next) {
468 if ((fi->fi_flags & FI_SEL) == 0)
469 continue;
470 fpath = srcpath(fi);
471 if (*fpath == '/') {
472 fprintf(fp, "%s.o: %s\n", fi->fi_base, fpath);
473 } else {
474 if (fi->fi_prefix != NULL) {
475 fprintf(fp, "%s.o: %s%s/%s\n", fi->fi_base,
476 prefix_prologue(fi->fi_prefix),
477 fi->fi_prefix, fpath);
478 } else {
479 fprintf(fp, "%s.o: %s%s\n",
480 fi->fi_base,
481 filetype_prologue(&fi->fi_fit),
482 fpath);
483 }
484 }
485 if (fi->fi_mkrule != NULL) {
486 fprintf(fp, "\t%s\n\n", fi->fi_mkrule);
487 } else {
488 fputs("\t${NORMAL_", fp);
489 cp = strrchr(fpath, '.');
490 cp = cp == NULL ? fpath : cp + 1;
491 while ((ch = *cp++) != '\0') {
492 fputc(toupper(ch), fp);
493 }
494 fputs("}\n\n", fp);
495 }
496 }
497 }
498
499 /*
500 * Emit the load commands.
501 *
502 * This function is not to be called `spurt'.
503 */
504 static void
505 emitload(FILE *fp)
506 {
507 struct config *cf;
508 const char *nm, *swname;
509
510 fputs(".MAIN: all\nall:", fp);
511 TAILQ_FOREACH(cf, &allcf, cf_next) {
512 fprintf(fp, " %s", cf->cf_name);
513 }
514 fputs("\n\n", fp);
515 TAILQ_FOREACH(cf, &allcf, cf_next) {
516 nm = cf->cf_name;
517 swname =
518 cf->cf_root != NULL ? cf->cf_name : "generic";
519 fprintf(fp, "KERNELS+=%s\n", nm);
520 fprintf(fp, "%s: ${SYSTEM_DEP} swap${.TARGET}.o vers.o", nm);
521 fprintf(fp, "\n"
522 "\t${SYSTEM_LD_HEAD}\n"
523 "\t${SYSTEM_LD} swap${.TARGET}.o\n"
524 "\t${SYSTEM_LD_TAIL}\n"
525 "\n"
526 "swap%s.o: swap%s.c\n"
527 "\t${NORMAL_C}\n\n", swname, swname);
528 }
529 }
530
531 /*
532 * Emit include headers (for any prefixes encountered)
533 */
534 static void
535 emitincludes(FILE *fp)
536 {
537 struct prefix *pf;
538
539 SLIST_FOREACH(pf, &allprefixes, pf_next) {
540 fprintf(fp, "EXTRA_INCLUDES+=\t-I%s%s\n",
541 prefix_prologue(pf->pf_prefix), pf->pf_prefix);
542 }
543 }
544
545 static int
546 print_condmkopts(const char *name, void *value, void *arg)
547 {
548 struct nvlist *nv;
549 FILE *fp = arg;
550
551 if (ht_lookup(selecttab, name) == 0)
552 return (0);
553
554 for (nv = value; nv != NULL; nv = nv->nv_next)
555 fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
556
557 return (0);
558 }
559
560 /*
561 * Emit appending makeoptions.
562 */
563 static void
564 emitappmkoptions(FILE *fp)
565 {
566 struct nvlist *nv;
567
568 for (nv = appmkoptions; nv != NULL; nv = nv->nv_next)
569 fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
570
571 ht_enumerate(condmkopttab, print_condmkopts, fp);
572 }
573