mkmakefile.c revision 1.28 1 /* $NetBSD: mkmakefile.c,v 1.28 2014/11/15 12:18:55 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.28 2014/11/15 12:18:55 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 = emalloc((size_t)nattrs * sizeof(attrbuf));
378
379 ht_enumerate(attrtab, emitallkobjsweighcb, NULL);
380 ht_enumerate(attrtab, emitallkobjscb, NULL);
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 /* XXX nattrs tracking is not exact yet */
402 if (attridx == nattrs) {
403 nattrs *= 2;
404 attrbuf = erealloc(attrbuf, (size_t)nattrs * sizeof(attrbuf));
405 }
406 return 0;
407 }
408
409 static int
410 emitallkobjsweighcb(const char *name, void *v, void *arg)
411 {
412 struct attr *a = v;
413
414 weighattr(a);
415 return 0;
416 }
417
418 static void
419 weighattr(struct attr *a)
420 {
421 struct attrlist *al;
422
423 for (al = a->a_deps; al != NULL; al = al->al_next) {
424 weighattr(al->al_this);
425 }
426 a->a_weight++;
427 }
428
429 static int
430 attrcmp(const void *l, const void *r)
431 {
432 const struct attr * const *a = l, * const *b = r;
433 const int wa = (*a)->a_weight, wb = (*b)->a_weight;
434 return (wa > wb) ? -1 : (wa < wb) ? 1 : 0;
435 }
436
437 static void
438 emitattrkobjs(FILE *fp)
439 {
440 extern struct hashtab *attrtab;
441
442 ht_enumerate(attrtab, emitattrkobjscb, fp);
443 }
444
445 static int
446 emitattrkobjscb(const char *name, void *v, void *arg)
447 {
448 struct attr *a = v;
449 struct files *fi;
450 FILE *fp = arg;
451
452 if (ht_lookup(selecttab, name) == NULL)
453 return 0;
454 if (TAILQ_EMPTY(&a->a_files))
455 return 0;
456 fputc('\n', fp);
457 fprintf(fp, "# %s (%d)\n", name, a->a_weight);
458 fprintf(fp, "OBJS.%s=", name);
459 TAILQ_FOREACH(fi, &a->a_files, fi_anext) {
460 fprintf(fp, " %s.o", fi->fi_base);
461 }
462 fputc('\n', fp);
463 fprintf(fp, "%s.ko: ${OBJS.%s}\n", name, name);
464 fprintf(fp, "\t${LINK_O}\n");
465 return 0;
466 }
467
468 static void
469 emitcfiles(FILE *fp)
470 {
471
472 emitfiles(fp, 'c', 0);
473 }
474
475 static void
476 emitsfiles(FILE *fp)
477 {
478
479 emitfiles(fp, 's', 'S');
480 }
481
482 static void
483 emitfiles(FILE *fp, int suffix, int upper_suffix)
484 {
485 struct files *fi;
486 size_t len;
487 const char *fpath;
488 struct config *cf;
489 char swapname[100];
490
491 fprintf(fp, "%cFILES= \\\n", toupper(suffix));
492 TAILQ_FOREACH(fi, &allfiles, fi_next) {
493 if ((fi->fi_flags & FI_SEL) == 0)
494 continue;
495 fpath = srcpath(fi);
496 len = strlen(fpath);
497 if (fpath[len - 1] != suffix && fpath[len - 1] != upper_suffix)
498 continue;
499 if (*fi->fi_path == '/') {
500 fprintf(fp, "\t%s \\\n", fpath);
501 } else {
502 if (fi->fi_prefix != NULL) {
503 fprintf(fp, "\t%s%s/%s \\\n",
504 prefix_prologue(fi->fi_prefix),
505 fi->fi_prefix, fpath);
506 } else {
507 fprintf(fp, "\t%s%s \\\n",
508 filetype_prologue(&fi->fi_fit),
509 fpath);
510 }
511 }
512 }
513 /*
514 * The allfiles list does not include the configuration-specific
515 * C source files. These files should be eliminated someday, but
516 * for now, we have to add them to ${CFILES} (and only ${CFILES}).
517 */
518 if (suffix == 'c') {
519 TAILQ_FOREACH(cf, &allcf, cf_next) {
520 (void)snprintf(swapname, sizeof(swapname), "swap%s.c",
521 cf->cf_name);
522 fprintf(fp, "\t%s \\\n", swapname);
523 }
524 }
525 putc('\n', fp);
526 }
527
528 /*
529 * Emit the make-rules.
530 */
531 static void
532 emitrules(FILE *fp)
533 {
534 struct files *fi;
535 const char *cp, *fpath;
536 int ch;
537
538 TAILQ_FOREACH(fi, &allfiles, fi_next) {
539 if ((fi->fi_flags & FI_SEL) == 0)
540 continue;
541 fpath = srcpath(fi);
542 if (*fpath == '/') {
543 fprintf(fp, "%s.o: %s\n", fi->fi_base, fpath);
544 } else {
545 if (fi->fi_prefix != NULL) {
546 fprintf(fp, "%s.o: %s%s/%s\n", fi->fi_base,
547 prefix_prologue(fi->fi_prefix),
548 fi->fi_prefix, fpath);
549 } else {
550 fprintf(fp, "%s.o: %s%s\n",
551 fi->fi_base,
552 filetype_prologue(&fi->fi_fit),
553 fpath);
554 }
555 }
556 if (fi->fi_mkrule != NULL) {
557 fprintf(fp, "\t%s\n\n", fi->fi_mkrule);
558 } else {
559 fputs("\t${NORMAL_", fp);
560 cp = strrchr(fpath, '.');
561 cp = cp == NULL ? fpath : cp + 1;
562 while ((ch = *cp++) != '\0') {
563 fputc(toupper(ch), fp);
564 }
565 fputs("}\n\n", fp);
566 }
567 }
568 }
569
570 /*
571 * Emit the load commands.
572 *
573 * This function is not to be called `spurt'.
574 */
575 static void
576 emitload(FILE *fp)
577 {
578 struct config *cf;
579
580 fputs(".MAIN: all\nall:", fp);
581 TAILQ_FOREACH(cf, &allcf, cf_next) {
582 fprintf(fp, " %s", cf->cf_name);
583 /*
584 * If we generate multiple configs inside the same build directory
585 * with a parallel build, strange things may happen, so sequentialize
586 * them.
587 */
588 if (cf != TAILQ_LAST(&allcf,conftq))
589 fprintf(fp, " .WAIT");
590 }
591 fputs("\n\n", fp);
592 TAILQ_FOREACH(cf, &allcf, cf_next) {
593 fprintf(fp, "KERNELS+=%s\n", cf->cf_name);
594 fprintf(fp, "%s: ${SYSTEM_DEP} swap%s.o vers.o build_kernel\n",
595 cf->cf_name, cf->cf_name);
596 fprintf(fp, "swap%s.o: swap%s.c\n"
597 "\t${NORMAL_C}\n\n", cf->cf_name, cf->cf_name);
598 }
599 fputs("\n", fp);
600 }
601
602 /*
603 * Emit include headers (for any prefixes encountered)
604 */
605 static void
606 emitincludes(FILE *fp)
607 {
608 struct prefix *pf;
609
610 SLIST_FOREACH(pf, &allprefixes, pf_next) {
611 fprintf(fp, "EXTRA_INCLUDES+=\t-I%s%s\n",
612 prefix_prologue(pf->pf_prefix), pf->pf_prefix);
613 }
614 }
615
616 /*
617 * Emit appending makeoptions.
618 */
619 static void
620 emitappmkoptions(FILE *fp)
621 {
622 struct nvlist *nv;
623 struct condexpr *cond;
624
625 for (nv = appmkoptions; nv != NULL; nv = nv->nv_next)
626 fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
627
628 for (nv = condmkoptions; nv != NULL; nv = nv->nv_next) {
629 cond = nv->nv_ptr;
630 if (expr_eval(cond, selectopt, NULL))
631 fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
632 condexpr_destroy(cond);
633 nv->nv_ptr = NULL;
634 }
635 }
636
637 static int
638 /*ARGSUSED*/
639 selectopt(const char *name, void *context)
640 {
641
642 return (ht_lookup(selecttab, strtolower(name)) != NULL);
643 }
644