xref: /netbsd-src/usr.bin/config/mkmakefile.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: mkmakefile.c,v 1.70 2017/06/16 02:01:10 christos 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.70 2017/06/16 02:01:10 christos 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 void emitdefs(FILE *);
66 static void emitallfiles(FILE *);
67 
68 static void emitofiles(FILE *);
69 static void emitallkobjs(FILE *);
70 static int emitallkobjscb(const char *, void *, void *);
71 static void emitattrkobjs(FILE *);
72 static int emitattrkobjscb(const char *, void *, void *);
73 static void emitkobjs(FILE *);
74 static void emitcfiles(FILE *);
75 static void emitsfiles(FILE *);
76 static void emitrules(FILE *);
77 static void emitload(FILE *);
78 static void emitincludes(FILE *);
79 static void emitappmkoptions(FILE *);
80 static void emitmkoption(FILE *, const char *, const struct nvlist *);
81 static void emitsubs(FILE *, const char *, const char *, int);
82 static int  selectopt(const char *, void *);
83 
84 int has_build_kernel;
85 
86 int
87 mkmakefile(void)
88 {
89 	FILE *ifp, *ofp;
90 	int lineno;
91 	void (*fn)(FILE *);
92 	char line[BUFSIZ], ifname[200];
93 
94 	/*
95 	 * Check if conf/Makefile.kern.inc defines "build_kernel".
96 	 *
97 	 * (This is usually done by checking "version" in sys/conf/files;
98 	 * unfortunately the "build_kernel" change done around 2014 Aug didn't
99 	 * bump that version.  Thus this hack.)
100 	 */
101 	(void)snprintf(ifname, sizeof(ifname), "%s/conf/Makefile.kern.inc",
102 	    srcdir);
103 	if ((ifp = fopen(ifname, "r")) == NULL) {
104 		warn("cannot read %s", ifname);
105 		goto bad2;
106 	}
107 	while (fgets(line, sizeof(line), ifp) != NULL) {
108 		if (strncmp(line, "build_kernel:", 13) == 0) {
109 			has_build_kernel = 1;
110 			break;
111 		}
112 	}
113 	(void)fclose(ifp);
114 
115 	/*
116 	 * Try a makefile for the port first.
117 	 */
118 	(void)snprintf(ifname, sizeof(ifname), "%s/arch/%s/conf/Makefile.%s",
119 	    srcdir, machine, machine);
120 	if ((ifp = fopen(ifname, "r")) == NULL) {
121 		/*
122 		 * Try a makefile for the architecture second.
123 		 */
124 		(void)snprintf(ifname, sizeof(ifname),
125 		    "%s/arch/%s/conf/Makefile.%s",
126 		    srcdir, machinearch, machinearch);
127 		ifp = fopen(ifname, "r");
128 	}
129 	if (ifp == NULL) {
130 		warn("cannot read %s", ifname);
131 		goto bad2;
132 	}
133 
134 	if ((ofp = fopen("Makefile.tmp", "w")) == NULL) {
135 		warn("cannot write Makefile");
136 		goto bad1;
137 	}
138 
139 	emitdefs(ofp);
140 
141 	lineno = 0;
142 	while (fgets(line, sizeof(line), ifp) != NULL) {
143 		lineno++;
144 		if ((version < 20090214 && line[0] != '%') || line[0] == '#') {
145 			fputs(line, ofp);
146 			continue;
147 		}
148 		if (strcmp(line, "%OBJS\n") == 0)
149 			fn = Mflag ? emitkobjs : emitofiles;
150 		else if (strcmp(line, "%CFILES\n") == 0)
151 			fn = emitcfiles;
152 		else if (strcmp(line, "%SFILES\n") == 0)
153 			fn = emitsfiles;
154 		else if (strcmp(line, "%RULES\n") == 0)
155 			fn = emitrules;
156 		else if (strcmp(line, "%LOAD\n") == 0)
157 			fn = emitload;
158 		else if (strcmp(line, "%INCLUDES\n") == 0)
159 			fn = emitincludes;
160 		else if (strcmp(line, "%MAKEOPTIONSAPPEND\n") == 0)
161 			fn = emitappmkoptions;
162 		else if (strncmp(line, "%VERSION ", sizeof("%VERSION ")-1) == 0) {
163 			int newvers;
164 			if (sscanf(line, "%%VERSION %d\n", &newvers) != 1) {
165 				cfgxerror(ifname, lineno, "syntax error for "
166 				    "%%VERSION");
167 			} else
168 				setversion(newvers);
169 			continue;
170 		} else {
171 			if (version < 20090214)
172 				cfgxerror(ifname, lineno,
173 				    "unknown %% construct ignored: %s", line);
174 			else
175 				emitsubs(ofp, line, ifname, lineno);
176 			continue;
177 		}
178 		(*fn)(ofp);
179 	}
180 
181 	fflush(ofp);
182 	if (ferror(ofp))
183 		goto wrerror;
184 
185 	if (ferror(ifp)) {
186 		warn("error reading %s (at line %d)", ifname, lineno);
187 		goto bad;
188 	}
189 
190 	if (fclose(ofp)) {
191 		ofp = NULL;
192 		goto wrerror;
193 	}
194 	(void)fclose(ifp);
195 
196 	if (moveifchanged("Makefile.tmp", "Makefile") != 0) {
197 		warn("error renaming Makefile");
198 		goto bad2;
199 	}
200 	return (0);
201 
202  wrerror:
203 	warn("error writing Makefile");
204  bad:
205 	if (ofp != NULL)
206 		(void)fclose(ofp);
207  bad1:
208 	(void)fclose(ifp);
209 	/* (void)unlink("Makefile.tmp"); */
210  bad2:
211 	return (1);
212 }
213 
214 static void
215 emitmkoption(FILE *fp, const char *ass, const struct nvlist *nv)
216 {
217 	const char *p;
218 
219 	fprintf(fp, "%s%s", nv->nv_name, ass);
220 	for (p = nv->nv_str; *p; p++) {
221 		if (*p == '\n')
222 			fputs(" \\", fp);
223 		fputc(*p, fp);
224 	}
225 	fputc('\n', fp);
226 }
227 
228 static void
229 emitsubs(FILE *fp, const char *line, const char *file, int lineno)
230 {
231 	char *nextpct;
232 	const char *optname;
233 	struct nvlist *option;
234 
235 	while (*line != '\0') {
236 		if (*line != '%') {
237 			fputc(*line++, fp);
238 			continue;
239 		}
240 
241 		line++;
242 		nextpct = strchr(line, '%');
243 		if (nextpct == NULL) {
244 			cfgxerror(file, lineno, "unbalanced %% or "
245 			    "unknown construct");
246 			return;
247 		}
248 		*nextpct = '\0';
249 
250 		if (*line == '\0')
251 			fputc('%', fp);
252 		else {
253 			optname = intern(line);
254 			if (!DEFINED_OPTION(optname)) {
255 				cfgxerror(file, lineno, "unknown option %s",
256 				    optname);
257 				return;
258 			}
259 
260 			if ((option = ht_lookup(opttab, optname)) == NULL)
261 				option = ht_lookup(fsopttab, optname);
262 			if (option != NULL)
263 				fputs(option->nv_str ? option->nv_str : "1",
264 				    fp);
265 			/*
266 			 * Otherwise it's not a selected option and we don't
267 			 * output anything.
268 			 */
269 		}
270 
271 		line = nextpct + 1;
272 	}
273 }
274 
275 static void
276 emitdefs(FILE *fp)
277 {
278 	struct nvlist *nv;
279 
280 	fprintf(fp, "KERNEL_BUILD=%s\n", conffile);
281 	fputs("IDENT= \\\n", fp);
282 	for (nv = options; nv != NULL; nv = nv->nv_next) {
283 
284 		/* Skip any options output to a header file */
285 		if (DEFINED_OPTION(nv->nv_name))
286 			continue;
287 		const char *s = nv->nv_str;
288 		fprintf(fp, "\t-D%s%s%s%s \\\n", nv->nv_name,
289 		    s ? "=\"" : "",
290 		    s ? s : "",
291 		    s ? "\"" : "");
292 	}
293 	putc('\n', fp);
294 	fprintf(fp, "MACHINE=%s\n", machine);
295 
296 	const char *subdir = "";
297 	if (*srcdir != '/' && *srcdir != '.') {
298 		/*
299 		 * libkern and libcompat "Makefile.inc"s want relative S
300 		 * specification to begin with '.'.
301 		 */
302 		subdir = "./";
303 	}
304 	fprintf(fp, "S=\t%s%s\n", subdir, srcdir);
305 	if (Sflag) {
306 		fprintf(fp, ".PATH: $S\n");
307 		fprintf(fp, "___USE_SUFFIX_RULES___=1\n");
308 	}
309 	for (nv = mkoptions; nv != NULL; nv = nv->nv_next)
310 		emitmkoption(fp, "=", nv);
311 }
312 
313 static void
314 emitfile(FILE *fp, struct files *fi)
315 {
316 	const char *defprologue = "$S/";
317 	const char *prologue, *prefix, *sep;
318 
319 	if (Sflag)
320 		defprologue = "";
321 	prologue = prefix = sep = "";
322 	if (*fi->fi_path != '/') {
323 		prologue = defprologue;
324 		if (fi->fi_prefix != NULL) {
325 			if (*fi->fi_prefix == '/')
326 				prologue = "";
327 			prefix = fi->fi_prefix;
328 			sep = "/";
329 		}
330 	}
331 	fprintf(fp, "%s%s%s%s", prologue, prefix, sep, fi->fi_path);
332 }
333 
334 static void
335 emitfilerel(FILE *fp, struct files *fi)
336 {
337 	const char *prefix, *sep;
338 
339 	prefix = sep = "";
340 	if (*fi->fi_path != '/') {
341 		if (fi->fi_prefix != NULL) {
342 			prefix = fi->fi_prefix;
343 			sep = "/";
344 		}
345 	}
346 	fprintf(fp, "%s%s%s", prefix, sep, fi->fi_path);
347 }
348 
349 static void
350 emitofiles(FILE *fp)
351 {
352 
353 	emitallfiles(fp);
354 	fprintf(fp, "#%%OFILES\n");
355 }
356 
357 static void
358 emitkobjs(FILE *fp)
359 {
360 	emitallkobjs(fp);
361 	emitattrkobjs(fp);
362 }
363 
364 static int emitallkobjsweighcb(const char *name, void *v, void *arg);
365 static void weighattr(struct attr *a);
366 static int attrcmp(const void *l, const void *r);
367 
368 struct attr **attrbuf;
369 size_t attridx;
370 
371 static void
372 emitallkobjs(FILE *fp)
373 {
374 	size_t i;
375 
376 	attrbuf = emalloc(nattrs * sizeof(*attrbuf));
377 
378 	ht_enumerate(attrtab, emitallkobjsweighcb, NULL);
379 	ht_enumerate(attrtab, emitallkobjscb, NULL);
380 	qsort(attrbuf, attridx, sizeof(struct attr *), attrcmp);
381 
382 	fputs("OBJS= \\\n", fp);
383 	for (i = 0; i < attridx; i++)
384 		fprintf(fp, "\t%s.ko \\\n", attrbuf[i]->a_name);
385 	putc('\n', fp);
386 
387 	free(attrbuf);
388 }
389 
390 static int
391 emitallkobjscb(const char *name, void *v, void *arg)
392 {
393 	struct attr *a = v;
394 
395 	if (ht_lookup(selecttab, name) == NULL)
396 		return 0;
397 	if (TAILQ_EMPTY(&a->a_files))
398 		return 0;
399 	attrbuf[attridx++] = a;
400 	/* XXX nattrs tracking is not exact yet */
401 	if (attridx == nattrs) {
402 		nattrs *= 2;
403 		attrbuf = erealloc(attrbuf, nattrs * sizeof(*attrbuf));
404 	}
405 	return 0;
406 }
407 
408 static int
409 emitallkobjsweighcb(const char *name, void *v, void *arg)
410 {
411 	struct attr *a = v;
412 
413 	weighattr(a);
414 	return 0;
415 }
416 
417 static void
418 weighattr(struct attr *a)
419 {
420 	struct attrlist *al;
421 
422 	for (al = a->a_deps; al != NULL; al = al->al_next) {
423 		weighattr(al->al_this);
424 	}
425 	a->a_weight++;
426 }
427 
428 static int
429 attrcmp(const void *l, const void *r)
430 {
431 	const struct attr * const *a = l, * const *b = r;
432 	const int wa = (*a)->a_weight, wb = (*b)->a_weight;
433 	return (wa > wb) ? -1 : (wa < wb) ? 1 : 0;
434 }
435 
436 static void
437 emitattrkobjs(FILE *fp)
438 {
439 	extern struct	hashtab *attrtab;
440 
441 	ht_enumerate(attrtab, emitattrkobjscb, fp);
442 }
443 
444 static int
445 emitattrkobjscb(const char *name, void *v, void *arg)
446 {
447 	struct attr *a = v;
448 	struct files *fi;
449 	FILE *fp = arg;
450 
451 	if (ht_lookup(selecttab, name) == NULL)
452 		return 0;
453 	if (TAILQ_EMPTY(&a->a_files))
454 		return 0;
455 	fputc('\n', fp);
456 	fprintf(fp, "# %s (%d)\n", name, a->a_weight);
457 	fprintf(fp, "OBJS.%s= \\\n", name);
458 	TAILQ_FOREACH(fi, &a->a_files, fi_anext) {
459 		fprintf(fp, "\t%s.o \\\n", fi->fi_base);
460 	}
461 	fputc('\n', fp);
462 	fprintf(fp, "%s.ko: ${OBJS.%s}\n", name, name);
463 	fprintf(fp, "\t${LINK_O}\n");
464 	return 0;
465 }
466 
467 static void
468 emitcfiles(FILE *fp)
469 {
470 
471 	emitallfiles(fp);
472 	fprintf(fp, "#%%CFILES\n");
473 }
474 
475 static void
476 emitsfiles(FILE *fp)
477 {
478 
479 	emitallfiles(fp);
480 	fprintf(fp, "#%%SFILES\n");
481 }
482 
483 static void
484 emitallfiles(FILE *fp)
485 {
486 	struct files *fi;
487 	static int called;
488 	int i;
489 	int found = 0;
490 
491 	if (called++ != 0)
492 		return;
493 	for (i = 0; i < (int)nselfiles; i++) {
494 		fi = selfiles[i];
495 		if (found++ == 0)
496 			fprintf(fp, "ALLFILES= \\\n");
497 		putc('\t', fp);
498 		emitfilerel(fp, fi);
499 		fputs(" \\\n", fp);
500 	}
501 	fputc('\n', fp);
502 }
503 
504 /*
505  * Emit the make-rules.
506  */
507 static void
508 emitrules(FILE *fp)
509 {
510 	struct files *fi;
511 	int i;
512 	int found = 0;
513 
514 	for (i = 0; i < (int)nselfiles; i++) {
515 		fi = selfiles[i];
516 		if (fi->fi_mkrule == NULL)
517 			continue;
518 		fprintf(fp, "%s.o: ", fi->fi_base);
519 		emitfile(fp, fi);
520 		putc('\n', fp);
521 		fprintf(fp, "\t%s\n\n", fi->fi_mkrule);
522 		found++;
523 	}
524 	if (found == 0)
525 		fprintf(fp, "#%%RULES\n");
526 }
527 
528 /*
529  * Emit the load commands.
530  *
531  * This function is not to be called `spurt'.
532  */
533 static void
534 emitload(FILE *fp)
535 {
536 	struct config *cf;
537 	int found = 0;
538 
539 	/*
540 	 * Generate the backward-compatible "build_kernel" rule if
541 	 * sys/conf/Makefile.kern.inc doesn't define any (pre-2014 Aug).
542 	 */
543 	if (has_build_kernel == 0) {
544 		fprintf(fp, "build_kernel: .USE\n"
545 		    "\t${SYSTEM_LD_HEAD}\n"
546 		    "\t${SYSTEM_LD}%s\n"
547 		    "\t${SYSTEM_LD_TAIL}\n"
548 		    "\n",
549 		    Sflag ? "" : " swap${.TARGET}.o");
550 	}
551 	/*
552 	 * Generate per-kernel rules.
553 	 */
554 	TAILQ_FOREACH(cf, &allcf, cf_next) {
555 		char swapobj[100];
556 
557 		if (Sflag) {
558 			swapobj[0] = '\0';
559 		} else {
560 			(void)snprintf(swapobj, sizeof(swapobj), " swap%s.o",
561 	 		    cf->cf_name);
562 		}
563 		fprintf(fp, "KERNELS+=%s\n", cf->cf_name);
564 		found = 1;
565 	}
566 	if (found == 0)
567 		fprintf(fp, "#%%LOAD\n");
568 }
569 
570 /*
571  * Emit include headers (for any prefixes encountered)
572  */
573 static void
574 emitincludes(FILE *fp)
575 {
576 	struct prefix *pf;
577 
578 	SLIST_FOREACH(pf, &allprefixes, pf_next) {
579 		const char *prologue = (*pf->pf_prefix == '/') ? "" : "$S/";
580 
581 		fprintf(fp, "EXTRA_INCLUDES+=\t-I%s%s\n",
582 		    prologue, pf->pf_prefix);
583 	}
584 }
585 
586 /*
587  * Emit appending makeoptions.
588  */
589 static void
590 emitappmkoptions(FILE *fp)
591 {
592 	struct nvlist *nv;
593 	struct condexpr *cond;
594 
595 	for (nv = appmkoptions; nv != NULL; nv = nv->nv_next)
596 		fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
597 
598 	for (nv = condmkoptions; nv != NULL; nv = nv->nv_next) {
599 		cond = nv->nv_ptr;
600 		if (expr_eval(cond, selectopt, NULL))
601 			emitmkoption(fp, "+=", nv);
602 		condexpr_destroy(cond);
603 		nv->nv_ptr = NULL;
604 	}
605 }
606 
607 static int
608 /*ARGSUSED*/
609 selectopt(const char *name, void *context)
610 {
611 
612 	return (ht_lookup(selecttab, strtolower(name)) != NULL);
613 }
614