mkmakefile.c revision 1.36 1 /* $NetBSD: mkmakefile.c,v 1.36 2014/12/15 15:41:18 uebayasi 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/cdefs.h>
48 __RCSID("$NetBSD: mkmakefile.c,v 1.36 2014/12/15 15:41:18 uebayasi Exp $");
49
50 #include <sys/param.h>
51 #include <ctype.h>
52 #include <errno.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <err.h>
57 #include <util.h>
58 #include "defs.h"
59 #include "sem.h"
60
61 /*
62 * Make the Makefile.
63 */
64
65 static const char *srcpath(struct files *);
66
67 static const char *prefix_prologue(const char *);
68 static const char *filetype_prologue(struct filetype *);
69
70
71 static void emitdefs(FILE *);
72 static void emitfiles(FILE *, int, int);
73
74 static void emitobjs(FILE *);
75 static void emitallkobjs(FILE *);
76 static int emitallkobjscb(const char *, void *, void *);
77 static void emitattrkobjs(FILE *);
78 static int emitattrkobjscb(const char *, void *, void *);
79 static void emitkobjs(FILE *);
80 static void emitcfiles(FILE *);
81 static void emitsfiles(FILE *);
82 static void emitrules(FILE *);
83 static void emitload(FILE *);
84 static void emitincludes(FILE *);
85 static void emitappmkoptions(FILE *);
86 static void emitsubs(FILE *, const char *, const char *, int);
87 static int selectopt(const char *, void *);
88
89 int has_build_kernel;
90
91 int
92 mkmakefile(void)
93 {
94 FILE *ifp, *ofp;
95 int lineno;
96 void (*fn)(FILE *);
97 char *ifname;
98 char line[BUFSIZ], buf[200];
99
100 /*
101 * Check if conf/Makefile.kern.inc defines "build_kernel".
102 *
103 * (This is usually done by checking "version" in sys/conf/files;
104 * unfortunately the "build_kernel" change done around 2014 Aug didn't
105 * bump that version. Thus this hack.)
106 */
107 (void)snprintf(buf, sizeof(buf), "conf/Makefile.kern.inc");
108 ifname = sourcepath(buf);
109 if ((ifp = fopen(ifname, "r")) != NULL) {
110 while (fgets(line, sizeof(line), ifp) != NULL) {
111 fputc('+', stderr);
112 if (strncmp(line, "build_kernel:", 13) == 0) {
113 has_build_kernel = 1;
114 break;
115 }
116 }
117 }
118 if (ifp == NULL) {
119 warn("cannot read %s", ifname);
120 goto bad2;
121 }
122 (void)fclose(ifp);
123
124 /*
125 * Try a makefile for the port first.
126 */
127 (void)snprintf(buf, sizeof(buf), "arch/%s/conf/Makefile.%s",
128 machine, machine);
129 ifname = sourcepath(buf);
130 if ((ifp = fopen(ifname, "r")) == NULL) {
131 /*
132 * Try a makefile for the architecture second.
133 */
134 (void)snprintf(buf, sizeof(buf), "arch/%s/conf/Makefile.%s",
135 machinearch, machinearch);
136 free(ifname);
137 ifname = sourcepath(buf);
138 ifp = fopen(ifname, "r");
139 }
140 if (ifp == NULL) {
141 warn("cannot read %s", ifname);
142 goto bad2;
143 }
144
145 if ((ofp = fopen("Makefile.tmp", "w")) == NULL) {
146 warn("cannot write Makefile");
147 goto bad1;
148 }
149
150 emitdefs(ofp);
151
152 lineno = 0;
153 while (fgets(line, sizeof(line), ifp) != NULL) {
154 lineno++;
155 if ((version < 20090214 && line[0] != '%') || line[0] == '#') {
156 fputs(line, ofp);
157 continue;
158 }
159 if (strcmp(line, "%OBJS\n") == 0)
160 fn = Mflag ? emitkobjs : emitobjs;
161 else if (strcmp(line, "%CFILES\n") == 0)
162 fn = emitcfiles;
163 else if (strcmp(line, "%SFILES\n") == 0)
164 fn = emitsfiles;
165 else if (strcmp(line, "%RULES\n") == 0)
166 fn = emitrules;
167 else if (strcmp(line, "%LOAD\n") == 0)
168 fn = emitload;
169 else if (strcmp(line, "%INCLUDES\n") == 0)
170 fn = emitincludes;
171 else if (strcmp(line, "%MAKEOPTIONSAPPEND\n") == 0)
172 fn = emitappmkoptions;
173 else if (strncmp(line, "%VERSION ", sizeof("%VERSION ")-1) == 0) {
174 int newvers;
175 if (sscanf(line, "%%VERSION %d\n", &newvers) != 1) {
176 cfgxerror(ifname, lineno, "syntax error for "
177 "%%VERSION");
178 } else
179 setversion(newvers);
180 continue;
181 } else {
182 if (version < 20090214)
183 cfgxerror(ifname, lineno,
184 "unknown %% construct ignored: %s", line);
185 else
186 emitsubs(ofp, line, ifname, lineno);
187 continue;
188 }
189 (*fn)(ofp);
190 }
191
192 fflush(ofp);
193 if (ferror(ofp))
194 goto wrerror;
195
196 if (ferror(ifp)) {
197 warn("error reading %s (at line %d)", ifname, lineno);
198 goto bad;
199 }
200
201 if (fclose(ofp)) {
202 ofp = NULL;
203 goto wrerror;
204 }
205 (void)fclose(ifp);
206
207 if (moveifchanged("Makefile.tmp", "Makefile") != 0) {
208 warn("error renaming Makefile");
209 goto bad2;
210 }
211 free(ifname);
212 return (0);
213
214 wrerror:
215 warn("error writing Makefile");
216 bad:
217 if (ofp != NULL)
218 (void)fclose(ofp);
219 bad1:
220 (void)fclose(ifp);
221 /* (void)unlink("Makefile.tmp"); */
222 bad2:
223 free(ifname);
224 return (1);
225 }
226
227 static void
228 emitsubs(FILE *fp, const char *line, const char *file, int lineno)
229 {
230 char *nextpct;
231 const char *optname;
232 struct nvlist *option;
233
234 while (*line != '\0') {
235 if (*line != '%') {
236 fputc(*line++, fp);
237 continue;
238 }
239
240 line++;
241 nextpct = strchr(line, '%');
242 if (nextpct == NULL) {
243 cfgxerror(file, lineno, "unbalanced %% or "
244 "unknown construct");
245 return;
246 }
247 *nextpct = '\0';
248
249 if (*line == '\0')
250 fputc('%', fp);
251 else {
252 optname = intern(line);
253 if (!DEFINED_OPTION(optname)) {
254 cfgxerror(file, lineno, "unknown option %s",
255 optname);
256 return;
257 }
258
259 if ((option = ht_lookup(opttab, optname)) == NULL)
260 option = ht_lookup(fsopttab, optname);
261 if (option != NULL)
262 fputs(option->nv_str ? option->nv_str : "1",
263 fp);
264 /*
265 * Otherwise it's not a selected option and we don't
266 * output anything.
267 */
268 }
269
270 line = nextpct + 1;
271 }
272 }
273
274 /*
275 * Return (possibly in a static buffer) the name of the `source' for a
276 * file. If we have `options source', or if the file is marked `always
277 * source', this is always the path from the `file' line; otherwise we
278 * get the .o from the obj-directory.
279 */
280 static const char *
281 srcpath(struct files *fi)
282 {
283 #if 1
284 /* Always have source, don't support object dirs for kernel builds. */
285 return (fi->fi_path);
286 #else
287 static char buf[MAXPATHLEN];
288
289 if (have_source || (fi->fi_flags & FI_ALWAYSSRC) != 0)
290 return (fi->fi_path);
291 if (objpath == NULL) {
292 cfgerror("obj-directory not set");
293 return (NULL);
294 }
295 (void)snprintf(buf, sizeof buf, "%s/%s.o", objpath, fi->fi_base);
296 return (buf);
297 #endif
298 }
299
300 static const char *
301 filetype_prologue(struct filetype *fit)
302 {
303
304 return (*fit->fit_path == '/') ? "" : "$S/";
305 }
306
307 static const char *
308 prefix_prologue(const char *path)
309 {
310
311 return (*path == '/') ? "" : "$S/";
312 }
313
314 static void
315 emitdefs(FILE *fp)
316 {
317 struct nvlist *nv;
318
319 fprintf(fp, "KERNEL_BUILD=%s\n", conffile);
320 fputs("IDENT= \\\n", fp);
321 for (nv = options; nv != NULL; nv = nv->nv_next) {
322
323 /* Skip any options output to a header file */
324 if (DEFINED_OPTION(nv->nv_name))
325 continue;
326 const char *s = nv->nv_str;
327 fprintf(fp, "\t-D%s%s%s%s \\\n", nv->nv_name,
328 s ? "=\"" : "",
329 s ? s : "",
330 s ? "\"" : "");
331 }
332 putc('\n', fp);
333 fprintf(fp, "PARAM=-DMAXUSERS=%d\n", maxusers);
334 fprintf(fp, "MACHINE=%s\n", machine);
335
336 const char *subdir = "";
337 if (*srcdir != '/' && *srcdir != '.') {
338 /*
339 * libkern and libcompat "Makefile.inc"s want relative S
340 * specification to begin with '.'.
341 */
342 subdir = "./";
343 }
344 fprintf(fp, "S=\t%s%s\n", subdir, srcdir);
345 for (nv = mkoptions; nv != NULL; nv = nv->nv_next)
346 fprintf(fp, "%s=%s\n", nv->nv_name, nv->nv_str);
347 }
348
349 static void
350 emitobjs(FILE *fp)
351 {
352 struct files *fi;
353 struct objects *oi;
354
355 fputs("OBJS= \\\n", fp);
356 TAILQ_FOREACH(fi, &allfiles, fi_next) {
357 if ((fi->fi_flags & FI_SEL) == 0)
358 continue;
359 fprintf(fp, "\t%s.o \\\n", fi->fi_base);
360 }
361 TAILQ_FOREACH(oi, &allobjects, oi_next) {
362 const char *prologue, *prefix, *sep;
363
364 if ((oi->oi_flags & OI_SEL) == 0)
365 continue;
366 prologue = prefix = sep = "";
367 if (*oi->oi_path != '/') {
368 if (oi->oi_prefix != NULL) {
369 prologue = prefix_prologue(oi->oi_path);
370 prefix = oi->oi_prefix;
371 sep = "/";
372 } else {
373 prologue = filetype_prologue(&oi->oi_fit);
374 }
375 }
376 fprintf(fp, "\t%s%s%s%s \\\n", prologue, prefix, sep,
377 oi->oi_path);
378 }
379 putc('\n', fp);
380 }
381
382 static void
383 emitkobjs(FILE *fp)
384 {
385 emitallkobjs(fp);
386 emitattrkobjs(fp);
387 }
388
389 static int emitallkobjsweighcb(const char *name, void *v, void *arg);
390 static void weighattr(struct attr *a);
391 static int attrcmp(const void *l, const void *r);
392
393 struct attr **attrbuf;
394 size_t attridx;
395
396 static void
397 emitallkobjs(FILE *fp)
398 {
399 size_t i;
400
401 attrbuf = emalloc(nattrs * sizeof(*attrbuf));
402
403 ht_enumerate(attrtab, emitallkobjsweighcb, NULL);
404 ht_enumerate(attrtab, emitallkobjscb, NULL);
405 qsort(attrbuf, attridx, sizeof(struct attr *), attrcmp);
406
407 fputs("OBJS= \\\n", fp);
408 for (i = 0; i < attridx; i++)
409 fprintf(fp, "\t%s.ko \\\n", attrbuf[i]->a_name);
410 putc('\n', fp);
411
412 free(attrbuf);
413 }
414
415 static int
416 emitallkobjscb(const char *name, void *v, void *arg)
417 {
418 struct attr *a = v;
419
420 if (ht_lookup(selecttab, name) == NULL)
421 return 0;
422 if (TAILQ_EMPTY(&a->a_files))
423 return 0;
424 attrbuf[attridx++] = a;
425 /* XXX nattrs tracking is not exact yet */
426 if (attridx == nattrs) {
427 nattrs *= 2;
428 attrbuf = erealloc(attrbuf, nattrs * sizeof(*attrbuf));
429 }
430 return 0;
431 }
432
433 static int
434 emitallkobjsweighcb(const char *name, void *v, void *arg)
435 {
436 struct attr *a = v;
437
438 weighattr(a);
439 return 0;
440 }
441
442 static void
443 weighattr(struct attr *a)
444 {
445 struct attrlist *al;
446
447 for (al = a->a_deps; al != NULL; al = al->al_next) {
448 weighattr(al->al_this);
449 }
450 a->a_weight++;
451 }
452
453 static int
454 attrcmp(const void *l, const void *r)
455 {
456 const struct attr * const *a = l, * const *b = r;
457 const int wa = (*a)->a_weight, wb = (*b)->a_weight;
458 return (wa > wb) ? -1 : (wa < wb) ? 1 : 0;
459 }
460
461 static void
462 emitattrkobjs(FILE *fp)
463 {
464 extern struct hashtab *attrtab;
465
466 ht_enumerate(attrtab, emitattrkobjscb, fp);
467 }
468
469 static int
470 emitattrkobjscb(const char *name, void *v, void *arg)
471 {
472 struct attr *a = v;
473 struct files *fi;
474 FILE *fp = arg;
475
476 if (ht_lookup(selecttab, name) == NULL)
477 return 0;
478 if (TAILQ_EMPTY(&a->a_files))
479 return 0;
480 fputc('\n', fp);
481 fprintf(fp, "# %s (%d)\n", name, a->a_weight);
482 fprintf(fp, "OBJS.%s= \\\n", name);
483 TAILQ_FOREACH(fi, &a->a_files, fi_anext) {
484 fprintf(fp, "\t%s.o \\\n", fi->fi_base);
485 }
486 fputc('\n', fp);
487 fprintf(fp, "%s.ko: ${OBJS.%s}\n", name, name);
488 fprintf(fp, "\t${LINK_O}\n");
489 return 0;
490 }
491
492 static void
493 emitcfiles(FILE *fp)
494 {
495
496 emitfiles(fp, 'c', 0);
497 }
498
499 static void
500 emitsfiles(FILE *fp)
501 {
502
503 emitfiles(fp, 's', 'S');
504 }
505
506 static void
507 emitfiles(FILE *fp, int suffix, int upper_suffix)
508 {
509 struct files *fi;
510 const char *fpath;
511 struct config *cf;
512 char swapname[100];
513
514 fprintf(fp, "%cFILES= \\\n", toupper(suffix));
515 TAILQ_FOREACH(fi, &allfiles, fi_next) {
516 const char *prologue, *prefix, *sep;
517
518 if ((fi->fi_flags & FI_SEL) == 0)
519 continue;
520 fpath = srcpath(fi);
521 if (fi->fi_suffix != suffix && fi->fi_suffix != upper_suffix)
522 continue;
523 prologue = prefix = sep = "";
524 if (*fi->fi_path != '/') {
525 if (fi->fi_prefix != NULL) {
526 prologue = prefix_prologue(fi->fi_prefix);
527 prefix = fi->fi_prefix;
528 sep = "/";
529 } else {
530 prologue = filetype_prologue(&fi->fi_fit);
531 }
532 }
533 fprintf(fp, "\t%s%s%s%s \\\n",
534 prologue, prefix, sep, fpath);
535 }
536 /*
537 * The allfiles list does not include the configuration-specific
538 * C source files. These files should be eliminated someday, but
539 * for now, we have to add them to ${CFILES} (and only ${CFILES}).
540 */
541 if (suffix == 'c') {
542 TAILQ_FOREACH(cf, &allcf, cf_next) {
543 (void)snprintf(swapname, sizeof(swapname), "swap%s.c",
544 cf->cf_name);
545 fprintf(fp, "\t%s \\\n", swapname);
546 }
547 }
548 putc('\n', fp);
549 }
550
551 /*
552 * Emit the make-rules.
553 */
554 static void
555 emitrules(FILE *fp)
556 {
557 struct files *fi;
558 const char *fpath;
559
560 TAILQ_FOREACH(fi, &allfiles, fi_next) {
561 const char *prologue, *prefix, *sep;
562
563 if ((fi->fi_flags & FI_SEL) == 0)
564 continue;
565 fpath = srcpath(fi);
566 prologue = prefix = sep = "";
567 if (*fpath != '/') {
568 if (fi->fi_prefix != NULL) {
569 prologue = prefix_prologue(fi->fi_prefix);
570 prefix = fi->fi_prefix;
571 sep = "/";
572 } else {
573 prologue = filetype_prologue(&fi->fi_fit);
574 }
575 }
576 fprintf(fp, "%s.o: %s%s%s%s\n", fi->fi_base,
577 prologue, prefix, sep, fpath);
578 if (fi->fi_mkrule != NULL) {
579 fprintf(fp, "\t%s\n\n", fi->fi_mkrule);
580 } else {
581 fprintf(fp, "\t${NORMAL_%c}\n\n", toupper(fi->fi_suffix));
582 }
583 }
584 }
585
586 /*
587 * Emit the load commands.
588 *
589 * This function is not to be called `spurt'.
590 */
591 static void
592 emitload(FILE *fp)
593 {
594 struct config *cf;
595
596 fputs(".MAIN: all\n", fp);
597 fputs("all:", fp);
598 TAILQ_FOREACH(cf, &allcf, cf_next) {
599 fprintf(fp, " %s", cf->cf_name);
600 /*
601 * If we generate multiple configs inside the same build directory
602 * with a parallel build, strange things may happen, so sequentialize
603 * them.
604 */
605 if (cf != TAILQ_LAST(&allcf,conftq))
606 fprintf(fp, " .WAIT");
607 }
608 fputs("\n\n", fp);
609 /*
610 * Generate the backward-compatible "build_kernel" rule if
611 * sys/conf/Makefile.kern.inc doesn't define any (pre-2014 Aug).
612 */
613 if (has_build_kernel == 0) {
614 fprintf(fp, "build_kernel: .USE\n"
615 "\t${SYSTEM_LD_HEAD}\n"
616 "\t${SYSTEM_LD} swap${.TARGET}.o\n"
617 "\t${SYSTEM_LD_TAIL}\n"
618 "\n");
619 }
620 /*
621 * Generate per-kernel rules.
622 */
623 TAILQ_FOREACH(cf, &allcf, cf_next) {
624 fprintf(fp, "KERNELS+=%s\n", cf->cf_name);
625 fprintf(fp, "%s: ${SYSTEM_DEP} swap%s.o vers.o build_kernel\n",
626 cf->cf_name, cf->cf_name);
627 fprintf(fp, "swap%s.o: swap%s.c\n"
628 "\t${NORMAL_C}\n\n", cf->cf_name, cf->cf_name);
629 }
630 fputs("\n", fp);
631 }
632
633 /*
634 * Emit include headers (for any prefixes encountered)
635 */
636 static void
637 emitincludes(FILE *fp)
638 {
639 struct prefix *pf;
640
641 SLIST_FOREACH(pf, &allprefixes, pf_next) {
642 fprintf(fp, "EXTRA_INCLUDES+=\t-I%s%s\n",
643 prefix_prologue(pf->pf_prefix), pf->pf_prefix);
644 }
645 }
646
647 /*
648 * Emit appending makeoptions.
649 */
650 static void
651 emitappmkoptions(FILE *fp)
652 {
653 struct nvlist *nv;
654 struct condexpr *cond;
655
656 for (nv = appmkoptions; nv != NULL; nv = nv->nv_next)
657 fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
658
659 for (nv = condmkoptions; nv != NULL; nv = nv->nv_next) {
660 cond = nv->nv_ptr;
661 if (expr_eval(cond, selectopt, NULL))
662 fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
663 condexpr_destroy(cond);
664 nv->nv_ptr = NULL;
665 }
666 }
667
668 static int
669 /*ARGSUSED*/
670 selectopt(const char *name, void *context)
671 {
672
673 return (ht_lookup(selecttab, strtolower(name)) != NULL);
674 }
675