mkmakefile.c revision 1.10 1 /* $NetBSD: mkmakefile.c,v 1.10 2009/02/20 05:20:25 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] != '%') || 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;
193 const char *optname;
194 struct nvlist *option;
195
196 while (*line != '\0') {
197 if (*line != '%') {
198 fputc(*line++, fp);
199 continue;
200 }
201
202 line++;
203 nextpct = strchr(line, '%');
204 if (nextpct == NULL) {
205 cfgxerror(file, lineno, "unbalanced %% or "
206 "unknown construct");
207 return;
208 }
209 *nextpct = '\0';
210
211 if (*line == '\0')
212 fputc('%', fp);
213 else {
214 optname = intern(line);
215 if (!DEFINED_OPTION(optname)) {
216 cfgxerror(file, lineno, "unknown option %s",
217 optname);
218 return;
219 }
220
221 if ((option = ht_lookup(opttab, optname)) == NULL)
222 option = ht_lookup(fsopttab, optname);
223 if (option != NULL)
224 fputs(option->nv_str ? option->nv_str : "1",
225 fp);
226 /* Otherwise it's not a selected option and we don't
227 * output anything. */
228 }
229
230 line = nextpct+1;
231 }
232 }
233
234 /*
235 * Return (possibly in a static buffer) the name of the `source' for a
236 * file. If we have `options source', or if the file is marked `always
237 * source', this is always the path from the `file' line; otherwise we
238 * get the .o from the obj-directory.
239 */
240 static const char *
241 srcpath(struct files *fi)
242 {
243 #if 1
244 /* Always have source, don't support object dirs for kernel builds. */
245 return (fi->fi_path);
246 #else
247 static char buf[MAXPATHLEN];
248
249 if (have_source || (fi->fi_flags & FI_ALWAYSSRC) != 0)
250 return (fi->fi_path);
251 if (objpath == NULL) {
252 cfgerror("obj-directory not set");
253 return (NULL);
254 }
255 (void)snprintf(buf, sizeof buf, "%s/%s.o", objpath, fi->fi_base);
256 return (buf);
257 #endif
258 }
259
260 static const char *
261 filetype_prologue(struct filetype *fit)
262 {
263 if (fit->fit_flags & FIT_NOPROLOGUE || *fit->fit_path == '/')
264 return ("");
265 else
266 return ("$S/");
267 }
268
269 static const char *
270 prefix_prologue(const char *path)
271 {
272 if (*path == '/')
273 return ("");
274 else
275 return ("$S/");
276 }
277
278 static void
279 emitdefs(FILE *fp)
280 {
281 struct nvlist *nv;
282 char *sp;
283
284 fprintf(fp, "KERNEL_BUILD=%s\n", conffile);
285 fputs("IDENT=", fp);
286 sp = "";
287 for (nv = options; nv != NULL; nv = nv->nv_next) {
288
289 /* skip any options output to a header file */
290 if (DEFINED_OPTION(nv->nv_name))
291 continue;
292 fprintf(fp, "%s-D%s", sp, nv->nv_name);
293 if (nv->nv_str)
294 fprintf(fp, "=\"%s\"", nv->nv_str);
295 sp = " ";
296 }
297 putc('\n', fp);
298 fprintf(fp, "PARAM=-DMAXUSERS=%d\n", maxusers);
299 fprintf(fp, "MACHINE=%s\n", machine);
300 if (*srcdir == '/' || *srcdir == '.') {
301 fprintf(fp, "S=\t%s\n", srcdir);
302 } else {
303 /*
304 * libkern and libcompat "Makefile.inc"s want relative S
305 * specification to begin with '.'.
306 */
307 fprintf(fp, "S=\t./%s\n", srcdir);
308 }
309 for (nv = mkoptions; nv != NULL; nv = nv->nv_next)
310 fprintf(fp, "%s=%s\n", nv->nv_name, nv->nv_str);
311 }
312
313 static void
314 emitobjs(FILE *fp)
315 {
316 struct files *fi;
317 struct objects *oi;
318 int lpos, len, sp;
319
320 fputs("OBJS=", fp);
321 sp = '\t';
322 lpos = 7;
323 TAILQ_FOREACH(fi, &allfiles, fi_next) {
324 if ((fi->fi_flags & FI_SEL) == 0)
325 continue;
326 len = strlen(fi->fi_base) + 2;
327 if (lpos + len > 72) {
328 fputs(" \\\n", fp);
329 sp = '\t';
330 lpos = 7;
331 }
332 fprintf(fp, "%c%s.o", sp, fi->fi_base);
333 lpos += len + 1;
334 sp = ' ';
335 }
336 TAILQ_FOREACH(oi, &allobjects, oi_next) {
337 if ((oi->oi_flags & OI_SEL) == 0)
338 continue;
339 len = strlen(oi->oi_path);
340 if (*oi->oi_path != '/')
341 {
342 /* e.g. "$S/" */
343 if (oi->oi_prefix != NULL)
344 len += strlen(prefix_prologue(oi->oi_path)) +
345 strlen(oi->oi_prefix) + 1;
346 else
347 len += strlen(filetype_prologue(&oi->oi_fit));
348 }
349 if (lpos + len > 72) {
350 fputs(" \\\n", fp);
351 sp = '\t';
352 lpos = 7;
353 }
354 if (*oi->oi_path == '/') {
355 fprintf(fp, "%c%s", sp, oi->oi_path);
356 } else {
357 if (oi->oi_prefix != NULL) {
358 fprintf(fp, "%c%s%s/%s", sp,
359 prefix_prologue(oi->oi_path),
360 oi->oi_prefix, oi->oi_path);
361 } else {
362 fprintf(fp, "%c%s%s", sp,
363 filetype_prologue(&oi->oi_fit),
364 oi->oi_path);
365 }
366 }
367 lpos += len + 1;
368 sp = ' ';
369 }
370 putc('\n', fp);
371 }
372
373 static void
374 emitcfiles(FILE *fp)
375 {
376
377 emitfiles(fp, 'c', 0);
378 }
379
380 static void
381 emitsfiles(FILE *fp)
382 {
383
384 emitfiles(fp, 's', 'S');
385 }
386
387 static void
388 emitfiles(FILE *fp, int suffix, int upper_suffix)
389 {
390 struct files *fi;
391 int lpos, len, sp;
392 const char *fpath;
393 struct config *cf;
394 char swapname[100];
395
396 fprintf(fp, "%cFILES=", toupper(suffix));
397 sp = '\t';
398 lpos = 7;
399 TAILQ_FOREACH(fi, &allfiles, fi_next) {
400 if ((fi->fi_flags & FI_SEL) == 0)
401 continue;
402 fpath = srcpath(fi);
403 len = strlen(fpath);
404 if (fpath[len - 1] != suffix && fpath[len - 1] != upper_suffix)
405 continue;
406 if (*fpath != '/') {
407 /* "$S/" */
408 if (fi->fi_prefix != NULL)
409 len += strlen(prefix_prologue(fi->fi_prefix)) +
410 strlen(fi->fi_prefix) + 1;
411 else
412 len += strlen(filetype_prologue(&fi->fi_fit));
413 }
414 if (lpos + len > 72) {
415 fputs(" \\\n", fp);
416 sp = '\t';
417 lpos = 7;
418 }
419 if (*fi->fi_path == '/') {
420 fprintf(fp, "%c%s", sp, fpath);
421 } else {
422 if (fi->fi_prefix != NULL) {
423 fprintf(fp, "%c%s%s/%s", sp,
424 prefix_prologue(fi->fi_prefix),
425 fi->fi_prefix, fpath);
426 } else {
427 fprintf(fp, "%c%s%s", sp,
428 filetype_prologue(&fi->fi_fit),
429 fpath);
430 }
431 }
432 lpos += len + 1;
433 sp = ' ';
434 }
435 /*
436 * The allfiles list does not include the configuration-specific
437 * C source files. These files should be eliminated someday, but
438 * for now, we have to add them to ${CFILES} (and only ${CFILES}).
439 */
440 if (suffix == 'c') {
441 TAILQ_FOREACH(cf, &allcf, cf_next) {
442 (void)snprintf(swapname, sizeof(swapname), "swap%s.c",
443 cf->cf_name);
444 len = strlen(swapname);
445 if (lpos + len > 72) {
446 fputs(" \\\n", fp);
447 sp = '\t';
448 lpos = 7;
449 }
450 fprintf(fp, "%c%s", sp, swapname);
451 lpos += len + 1;
452 sp = ' ';
453 }
454 }
455 putc('\n', fp);
456 }
457
458 /*
459 * Emit the make-rules.
460 */
461 static void
462 emitrules(FILE *fp)
463 {
464 struct files *fi;
465 const char *cp, *fpath;
466 int ch;
467
468 TAILQ_FOREACH(fi, &allfiles, fi_next) {
469 if ((fi->fi_flags & FI_SEL) == 0)
470 continue;
471 fpath = srcpath(fi);
472 if (*fpath == '/') {
473 fprintf(fp, "%s.o: %s\n", fi->fi_base, fpath);
474 } else {
475 if (fi->fi_prefix != NULL) {
476 fprintf(fp, "%s.o: %s%s/%s\n", fi->fi_base,
477 prefix_prologue(fi->fi_prefix),
478 fi->fi_prefix, fpath);
479 } else {
480 fprintf(fp, "%s.o: %s%s\n",
481 fi->fi_base,
482 filetype_prologue(&fi->fi_fit),
483 fpath);
484 }
485 }
486 if (fi->fi_mkrule != NULL) {
487 fprintf(fp, "\t%s\n\n", fi->fi_mkrule);
488 } else {
489 fputs("\t${NORMAL_", fp);
490 cp = strrchr(fpath, '.');
491 cp = cp == NULL ? fpath : cp + 1;
492 while ((ch = *cp++) != '\0') {
493 fputc(toupper(ch), fp);
494 }
495 fputs("}\n\n", fp);
496 }
497 }
498 }
499
500 /*
501 * Emit the load commands.
502 *
503 * This function is not to be called `spurt'.
504 */
505 static void
506 emitload(FILE *fp)
507 {
508 struct config *cf;
509 const char *nm, *swname;
510
511 fputs(".MAIN: all\nall:", fp);
512 TAILQ_FOREACH(cf, &allcf, cf_next) {
513 fprintf(fp, " %s", cf->cf_name);
514 }
515 fputs("\n\n", fp);
516 TAILQ_FOREACH(cf, &allcf, cf_next) {
517 nm = cf->cf_name;
518 swname =
519 cf->cf_root != NULL ? cf->cf_name : "generic";
520 fprintf(fp, "KERNELS+=%s\n", nm);
521 fprintf(fp, "%s: ${SYSTEM_DEP} swap${.TARGET}.o vers.o", nm);
522 fprintf(fp, "\n"
523 "\t${SYSTEM_LD_HEAD}\n"
524 "\t${SYSTEM_LD} swap${.TARGET}.o\n"
525 "\t${SYSTEM_LD_TAIL}\n"
526 "\n"
527 "swap%s.o: swap%s.c\n"
528 "\t${NORMAL_C}\n\n", swname, swname);
529 }
530 }
531
532 /*
533 * Emit include headers (for any prefixes encountered)
534 */
535 static void
536 emitincludes(FILE *fp)
537 {
538 struct prefix *pf;
539
540 SLIST_FOREACH(pf, &allprefixes, pf_next) {
541 fprintf(fp, "EXTRA_INCLUDES+=\t-I%s%s\n",
542 prefix_prologue(pf->pf_prefix), pf->pf_prefix);
543 }
544 }
545
546 static int
547 print_condmkopts(const char *name, void *value, void *arg)
548 {
549 struct nvlist *nv;
550 FILE *fp = arg;
551
552 if (ht_lookup(selecttab, name) == 0)
553 return (0);
554
555 for (nv = value; nv != NULL; nv = nv->nv_next)
556 fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
557
558 return (0);
559 }
560
561 /*
562 * Emit appending makeoptions.
563 */
564 static void
565 emitappmkoptions(FILE *fp)
566 {
567 struct nvlist *nv;
568
569 for (nv = appmkoptions; nv != NULL; nv = nv->nv_next)
570 fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
571
572 ht_enumerate(condmkopttab, print_condmkopts, fp);
573 }
574