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