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