mkmakefile.c revision 1.73 1 1.73 riastrad /* $NetBSD: mkmakefile.c,v 1.73 2024/04/05 00:43:42 riastradh Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1992, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This software was developed by the Computer Systems Engineering group
8 1.1 thorpej * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 thorpej * contributed to Berkeley.
10 1.1 thorpej *
11 1.1 thorpej * All advertising materials mentioning features or use of this software
12 1.1 thorpej * must display the following acknowledgement:
13 1.1 thorpej * This product includes software developed by the University of
14 1.1 thorpej * California, Lawrence Berkeley Laboratories.
15 1.1 thorpej *
16 1.1 thorpej * Redistribution and use in source and binary forms, with or without
17 1.1 thorpej * modification, are permitted provided that the following conditions
18 1.1 thorpej * are met:
19 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
20 1.1 thorpej * notice, this list of conditions and the following disclaimer.
21 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
23 1.1 thorpej * documentation and/or other materials provided with the distribution.
24 1.1 thorpej * 3. Neither the name of the University nor the names of its contributors
25 1.1 thorpej * may be used to endorse or promote products derived from this software
26 1.1 thorpej * without specific prior written permission.
27 1.1 thorpej *
28 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 thorpej * SUCH DAMAGE.
39 1.1 thorpej *
40 1.1 thorpej * from: @(#)mkmakefile.c 8.1 (Berkeley) 6/6/93
41 1.1 thorpej */
42 1.1 thorpej
43 1.1 thorpej #if HAVE_NBTOOL_CONFIG_H
44 1.1 thorpej #include "nbtool_config.h"
45 1.1 thorpej #endif
46 1.1 thorpej
47 1.25 christos #include <sys/cdefs.h>
48 1.73 riastrad __RCSID("$NetBSD: mkmakefile.c,v 1.73 2024/04/05 00:43:42 riastradh Exp $");
49 1.25 christos
50 1.1 thorpej #include <sys/param.h>
51 1.1 thorpej #include <ctype.h>
52 1.1 thorpej #include <errno.h>
53 1.1 thorpej #include <stdio.h>
54 1.1 thorpej #include <stdlib.h>
55 1.1 thorpej #include <string.h>
56 1.6 christos #include <err.h>
57 1.27 uebayasi #include <util.h>
58 1.1 thorpej #include "defs.h"
59 1.1 thorpej #include "sem.h"
60 1.1 thorpej
61 1.1 thorpej /*
62 1.1 thorpej * Make the Makefile.
63 1.1 thorpej */
64 1.1 thorpej
65 1.5 dsl static void emitdefs(FILE *);
66 1.66 uebayasi static void emitallfiles(FILE *);
67 1.1 thorpej
68 1.66 uebayasi static void emitofiles(FILE *);
69 1.18 uebayasi static void emitallkobjs(FILE *);
70 1.18 uebayasi static int emitallkobjscb(const char *, void *, void *);
71 1.18 uebayasi static void emitattrkobjs(FILE *);
72 1.18 uebayasi static int emitattrkobjscb(const char *, void *, void *);
73 1.18 uebayasi static void emitkobjs(FILE *);
74 1.5 dsl static void emitcfiles(FILE *);
75 1.5 dsl static void emitsfiles(FILE *);
76 1.5 dsl static void emitrules(FILE *);
77 1.5 dsl static void emitload(FILE *);
78 1.5 dsl static void emitincludes(FILE *);
79 1.5 dsl static void emitappmkoptions(FILE *);
80 1.69 christos static void emitmkoption(FILE *, const char *, const struct nvlist *);
81 1.8 cube static void emitsubs(FILE *, const char *, const char *, int);
82 1.11 cube static int selectopt(const char *, void *);
83 1.1 thorpej
84 1.36 uebayasi int has_build_kernel;
85 1.36 uebayasi
86 1.1 thorpej int
87 1.1 thorpej mkmakefile(void)
88 1.1 thorpej {
89 1.1 thorpej FILE *ifp, *ofp;
90 1.1 thorpej int lineno;
91 1.5 dsl void (*fn)(FILE *);
92 1.38 uebayasi char line[BUFSIZ], ifname[200];
93 1.1 thorpej
94 1.36 uebayasi /*
95 1.36 uebayasi * Check if conf/Makefile.kern.inc defines "build_kernel".
96 1.36 uebayasi *
97 1.36 uebayasi * (This is usually done by checking "version" in sys/conf/files;
98 1.36 uebayasi * unfortunately the "build_kernel" change done around 2014 Aug didn't
99 1.36 uebayasi * bump that version. Thus this hack.)
100 1.36 uebayasi */
101 1.38 uebayasi (void)snprintf(ifname, sizeof(ifname), "%s/conf/Makefile.kern.inc",
102 1.38 uebayasi srcdir);
103 1.37 uebayasi if ((ifp = fopen(ifname, "r")) == NULL) {
104 1.36 uebayasi warn("cannot read %s", ifname);
105 1.36 uebayasi goto bad2;
106 1.36 uebayasi }
107 1.37 uebayasi while (fgets(line, sizeof(line), ifp) != NULL) {
108 1.37 uebayasi if (strncmp(line, "build_kernel:", 13) == 0) {
109 1.37 uebayasi has_build_kernel = 1;
110 1.37 uebayasi break;
111 1.37 uebayasi }
112 1.37 uebayasi }
113 1.36 uebayasi (void)fclose(ifp);
114 1.36 uebayasi
115 1.36 uebayasi /*
116 1.36 uebayasi * Try a makefile for the port first.
117 1.1 thorpej */
118 1.38 uebayasi (void)snprintf(ifname, sizeof(ifname), "%s/arch/%s/conf/Makefile.%s",
119 1.38 uebayasi srcdir, machine, machine);
120 1.1 thorpej if ((ifp = fopen(ifname, "r")) == NULL) {
121 1.24 uebayasi /*
122 1.24 uebayasi * Try a makefile for the architecture second.
123 1.1 thorpej */
124 1.38 uebayasi (void)snprintf(ifname, sizeof(ifname),
125 1.38 uebayasi "%s/arch/%s/conf/Makefile.%s",
126 1.38 uebayasi srcdir, machinearch, machinearch);
127 1.4 cube ifp = fopen(ifname, "r");
128 1.1 thorpej }
129 1.4 cube if (ifp == NULL) {
130 1.6 christos warn("cannot read %s", ifname);
131 1.4 cube goto bad2;
132 1.1 thorpej }
133 1.5 dsl
134 1.1 thorpej if ((ofp = fopen("Makefile.tmp", "w")) == NULL) {
135 1.6 christos warn("cannot write Makefile");
136 1.4 cube goto bad1;
137 1.1 thorpej }
138 1.5 dsl
139 1.5 dsl emitdefs(ofp);
140 1.5 dsl
141 1.1 thorpej lineno = 0;
142 1.1 thorpej while (fgets(line, sizeof(line), ifp) != NULL) {
143 1.1 thorpej lineno++;
144 1.10 cube if ((version < 20090214 && line[0] != '%') || line[0] == '#') {
145 1.5 dsl fputs(line, ofp);
146 1.1 thorpej continue;
147 1.1 thorpej }
148 1.1 thorpej if (strcmp(line, "%OBJS\n") == 0)
149 1.66 uebayasi fn = Mflag ? emitkobjs : emitofiles;
150 1.1 thorpej else if (strcmp(line, "%CFILES\n") == 0)
151 1.1 thorpej fn = emitcfiles;
152 1.1 thorpej else if (strcmp(line, "%SFILES\n") == 0)
153 1.1 thorpej fn = emitsfiles;
154 1.1 thorpej else if (strcmp(line, "%RULES\n") == 0)
155 1.1 thorpej fn = emitrules;
156 1.1 thorpej else if (strcmp(line, "%LOAD\n") == 0)
157 1.1 thorpej fn = emitload;
158 1.1 thorpej else if (strcmp(line, "%INCLUDES\n") == 0)
159 1.1 thorpej fn = emitincludes;
160 1.1 thorpej else if (strcmp(line, "%MAKEOPTIONSAPPEND\n") == 0)
161 1.1 thorpej fn = emitappmkoptions;
162 1.8 cube else if (strncmp(line, "%VERSION ", sizeof("%VERSION ")-1) == 0) {
163 1.8 cube int newvers;
164 1.8 cube if (sscanf(line, "%%VERSION %d\n", &newvers) != 1) {
165 1.8 cube cfgxerror(ifname, lineno, "syntax error for "
166 1.8 cube "%%VERSION");
167 1.8 cube } else
168 1.8 cube setversion(newvers);
169 1.8 cube continue;
170 1.8 cube } else {
171 1.8 cube if (version < 20090214)
172 1.8 cube cfgxerror(ifname, lineno,
173 1.8 cube "unknown %% construct ignored: %s", line);
174 1.8 cube else
175 1.8 cube emitsubs(ofp, line, ifname, lineno);
176 1.1 thorpej continue;
177 1.1 thorpej }
178 1.5 dsl (*fn)(ofp);
179 1.1 thorpej }
180 1.5 dsl
181 1.5 dsl fflush(ofp);
182 1.5 dsl if (ferror(ofp))
183 1.5 dsl goto wrerror;
184 1.5 dsl
185 1.1 thorpej if (ferror(ifp)) {
186 1.6 christos warn("error reading %s (at line %d)", ifname, lineno);
187 1.1 thorpej goto bad;
188 1.1 thorpej }
189 1.5 dsl
190 1.1 thorpej if (fclose(ofp)) {
191 1.1 thorpej ofp = NULL;
192 1.1 thorpej goto wrerror;
193 1.1 thorpej }
194 1.1 thorpej (void)fclose(ifp);
195 1.5 dsl
196 1.1 thorpej if (moveifchanged("Makefile.tmp", "Makefile") != 0) {
197 1.6 christos warn("error renaming Makefile");
198 1.4 cube goto bad2;
199 1.1 thorpej }
200 1.1 thorpej return (0);
201 1.4 cube
202 1.1 thorpej wrerror:
203 1.6 christos warn("error writing Makefile");
204 1.1 thorpej bad:
205 1.1 thorpej if (ofp != NULL)
206 1.1 thorpej (void)fclose(ofp);
207 1.4 cube bad1:
208 1.4 cube (void)fclose(ifp);
209 1.1 thorpej /* (void)unlink("Makefile.tmp"); */
210 1.4 cube bad2:
211 1.1 thorpej return (1);
212 1.1 thorpej }
213 1.1 thorpej
214 1.8 cube static void
215 1.69 christos emitmkoption(FILE *fp, const char *ass, const struct nvlist *nv)
216 1.69 christos {
217 1.70 christos const char *p;
218 1.69 christos
219 1.69 christos fprintf(fp, "%s%s", nv->nv_name, ass);
220 1.70 christos for (p = nv->nv_str; *p; p++) {
221 1.69 christos if (*p == '\n')
222 1.69 christos fputs(" \\", fp);
223 1.69 christos fputc(*p, fp);
224 1.69 christos }
225 1.69 christos fputc('\n', fp);
226 1.69 christos }
227 1.69 christos
228 1.69 christos static void
229 1.8 cube emitsubs(FILE *fp, const char *line, const char *file, int lineno)
230 1.8 cube {
231 1.9 enami char *nextpct;
232 1.9 enami const char *optname;
233 1.8 cube struct nvlist *option;
234 1.8 cube
235 1.8 cube while (*line != '\0') {
236 1.8 cube if (*line != '%') {
237 1.8 cube fputc(*line++, fp);
238 1.8 cube continue;
239 1.8 cube }
240 1.8 cube
241 1.8 cube line++;
242 1.8 cube nextpct = strchr(line, '%');
243 1.8 cube if (nextpct == NULL) {
244 1.8 cube cfgxerror(file, lineno, "unbalanced %% or "
245 1.8 cube "unknown construct");
246 1.8 cube return;
247 1.8 cube }
248 1.8 cube *nextpct = '\0';
249 1.8 cube
250 1.8 cube if (*line == '\0')
251 1.8 cube fputc('%', fp);
252 1.8 cube else {
253 1.8 cube optname = intern(line);
254 1.8 cube if (!DEFINED_OPTION(optname)) {
255 1.8 cube cfgxerror(file, lineno, "unknown option %s",
256 1.8 cube optname);
257 1.8 cube return;
258 1.8 cube }
259 1.8 cube
260 1.8 cube if ((option = ht_lookup(opttab, optname)) == NULL)
261 1.8 cube option = ht_lookup(fsopttab, optname);
262 1.8 cube if (option != NULL)
263 1.8 cube fputs(option->nv_str ? option->nv_str : "1",
264 1.8 cube fp);
265 1.24 uebayasi /*
266 1.24 uebayasi * Otherwise it's not a selected option and we don't
267 1.24 uebayasi * output anything.
268 1.24 uebayasi */
269 1.8 cube }
270 1.8 cube
271 1.24 uebayasi line = nextpct + 1;
272 1.8 cube }
273 1.8 cube }
274 1.8 cube
275 1.5 dsl static void
276 1.1 thorpej emitdefs(FILE *fp)
277 1.1 thorpej {
278 1.72 thorpej struct defoptlist *dl;
279 1.1 thorpej struct nvlist *nv;
280 1.1 thorpej
281 1.5 dsl fprintf(fp, "KERNEL_BUILD=%s\n", conffile);
282 1.29 uebayasi fputs("IDENT= \\\n", fp);
283 1.1 thorpej for (nv = options; nv != NULL; nv = nv->nv_next) {
284 1.1 thorpej
285 1.24 uebayasi /* Skip any options output to a header file */
286 1.1 thorpej if (DEFINED_OPTION(nv->nv_name))
287 1.1 thorpej continue;
288 1.33 uebayasi const char *s = nv->nv_str;
289 1.29 uebayasi fprintf(fp, "\t-D%s%s%s%s \\\n", nv->nv_name,
290 1.33 uebayasi s ? "=\"" : "",
291 1.33 uebayasi s ? s : "",
292 1.33 uebayasi s ? "\"" : "");
293 1.1 thorpej }
294 1.5 dsl putc('\n', fp);
295 1.5 dsl fprintf(fp, "MACHINE=%s\n", machine);
296 1.33 uebayasi
297 1.33 uebayasi const char *subdir = "";
298 1.33 uebayasi if (*srcdir != '/' && *srcdir != '.') {
299 1.1 thorpej /*
300 1.1 thorpej * libkern and libcompat "Makefile.inc"s want relative S
301 1.1 thorpej * specification to begin with '.'.
302 1.1 thorpej */
303 1.33 uebayasi subdir = "./";
304 1.1 thorpej }
305 1.33 uebayasi fprintf(fp, "S=\t%s%s\n", subdir, srcdir);
306 1.55 uebayasi if (Sflag) {
307 1.55 uebayasi fprintf(fp, ".PATH: $S\n");
308 1.55 uebayasi fprintf(fp, "___USE_SUFFIX_RULES___=1\n");
309 1.55 uebayasi }
310 1.1 thorpej for (nv = mkoptions; nv != NULL; nv = nv->nv_next)
311 1.69 christos emitmkoption(fp, "=", nv);
312 1.72 thorpej
313 1.72 thorpej /*
314 1.72 thorpej * Go through the options again and emit Makefile variables
315 1.72 thorpej * for those specified to get one.
316 1.72 thorpej */
317 1.72 thorpej for (nv = options; nv != NULL; nv = nv->nv_next) {
318 1.72 thorpej
319 1.72 thorpej dl = find_declared_option_option(nv->nv_name);
320 1.72 thorpej if (dl != NULL && dl->dl_mkvar) {
321 1.72 thorpej const char *s = nv->nv_str;
322 1.72 thorpej if (s == NULL) {
323 1.72 thorpej s = "1";
324 1.72 thorpej }
325 1.72 thorpej fprintf(fp, "KERNEL_OPT_%s=\"%s\"\n", nv->nv_name, s);
326 1.72 thorpej }
327 1.72 thorpej }
328 1.1 thorpej }
329 1.1 thorpej
330 1.5 dsl static void
331 1.53 uebayasi emitfile(FILE *fp, struct files *fi)
332 1.50 uebayasi {
333 1.56 uebayasi const char *defprologue = "$S/";
334 1.50 uebayasi const char *prologue, *prefix, *sep;
335 1.50 uebayasi
336 1.55 uebayasi if (Sflag)
337 1.56 uebayasi defprologue = "";
338 1.50 uebayasi prologue = prefix = sep = "";
339 1.53 uebayasi if (*fi->fi_path != '/') {
340 1.56 uebayasi prologue = defprologue;
341 1.53 uebayasi if (fi->fi_prefix != NULL) {
342 1.53 uebayasi if (*fi->fi_prefix == '/')
343 1.51 uebayasi prologue = "";
344 1.53 uebayasi prefix = fi->fi_prefix;
345 1.50 uebayasi sep = "/";
346 1.50 uebayasi }
347 1.50 uebayasi }
348 1.53 uebayasi fprintf(fp, "%s%s%s%s", prologue, prefix, sep, fi->fi_path);
349 1.50 uebayasi }
350 1.50 uebayasi
351 1.50 uebayasi static void
352 1.58 uebayasi emitfilerel(FILE *fp, struct files *fi)
353 1.58 uebayasi {
354 1.58 uebayasi const char *prefix, *sep;
355 1.58 uebayasi
356 1.58 uebayasi prefix = sep = "";
357 1.58 uebayasi if (*fi->fi_path != '/') {
358 1.58 uebayasi if (fi->fi_prefix != NULL) {
359 1.58 uebayasi prefix = fi->fi_prefix;
360 1.58 uebayasi sep = "/";
361 1.58 uebayasi }
362 1.58 uebayasi }
363 1.58 uebayasi fprintf(fp, "%s%s%s", prefix, sep, fi->fi_path);
364 1.58 uebayasi }
365 1.58 uebayasi
366 1.58 uebayasi static void
367 1.66 uebayasi emitofiles(FILE *fp)
368 1.1 thorpej {
369 1.1 thorpej
370 1.66 uebayasi emitallfiles(fp);
371 1.67 uebayasi fprintf(fp, "#%%OFILES\n");
372 1.1 thorpej }
373 1.1 thorpej
374 1.5 dsl static void
375 1.18 uebayasi emitkobjs(FILE *fp)
376 1.18 uebayasi {
377 1.18 uebayasi emitallkobjs(fp);
378 1.18 uebayasi emitattrkobjs(fp);
379 1.18 uebayasi }
380 1.18 uebayasi
381 1.27 uebayasi static int emitallkobjsweighcb(const char *name, void *v, void *arg);
382 1.27 uebayasi static void weighattr(struct attr *a);
383 1.27 uebayasi static int attrcmp(const void *l, const void *r);
384 1.27 uebayasi
385 1.27 uebayasi struct attr **attrbuf;
386 1.34 christos size_t attridx;
387 1.27 uebayasi
388 1.18 uebayasi static void
389 1.18 uebayasi emitallkobjs(FILE *fp)
390 1.18 uebayasi {
391 1.34 christos size_t i;
392 1.27 uebayasi
393 1.34 christos attrbuf = emalloc(nattrs * sizeof(*attrbuf));
394 1.27 uebayasi
395 1.27 uebayasi ht_enumerate(attrtab, emitallkobjsweighcb, NULL);
396 1.28 uebayasi ht_enumerate(attrtab, emitallkobjscb, NULL);
397 1.34 christos qsort(attrbuf, attridx, sizeof(struct attr *), attrcmp);
398 1.18 uebayasi
399 1.29 uebayasi fputs("OBJS= \\\n", fp);
400 1.27 uebayasi for (i = 0; i < attridx; i++)
401 1.29 uebayasi fprintf(fp, "\t%s.ko \\\n", attrbuf[i]->a_name);
402 1.18 uebayasi putc('\n', fp);
403 1.27 uebayasi
404 1.27 uebayasi free(attrbuf);
405 1.18 uebayasi }
406 1.18 uebayasi
407 1.18 uebayasi static int
408 1.18 uebayasi emitallkobjscb(const char *name, void *v, void *arg)
409 1.18 uebayasi {
410 1.18 uebayasi struct attr *a = v;
411 1.18 uebayasi
412 1.18 uebayasi if (ht_lookup(selecttab, name) == NULL)
413 1.18 uebayasi return 0;
414 1.18 uebayasi if (TAILQ_EMPTY(&a->a_files))
415 1.18 uebayasi return 0;
416 1.73 riastrad a->a_idx = attridx;
417 1.27 uebayasi attrbuf[attridx++] = a;
418 1.28 uebayasi /* XXX nattrs tracking is not exact yet */
419 1.28 uebayasi if (attridx == nattrs) {
420 1.28 uebayasi nattrs *= 2;
421 1.34 christos attrbuf = erealloc(attrbuf, nattrs * sizeof(*attrbuf));
422 1.28 uebayasi }
423 1.18 uebayasi return 0;
424 1.18 uebayasi }
425 1.18 uebayasi
426 1.27 uebayasi static int
427 1.27 uebayasi emitallkobjsweighcb(const char *name, void *v, void *arg)
428 1.27 uebayasi {
429 1.27 uebayasi struct attr *a = v;
430 1.27 uebayasi
431 1.27 uebayasi weighattr(a);
432 1.27 uebayasi return 0;
433 1.27 uebayasi }
434 1.27 uebayasi
435 1.27 uebayasi static void
436 1.27 uebayasi weighattr(struct attr *a)
437 1.27 uebayasi {
438 1.27 uebayasi struct attrlist *al;
439 1.27 uebayasi
440 1.27 uebayasi for (al = a->a_deps; al != NULL; al = al->al_next) {
441 1.27 uebayasi weighattr(al->al_this);
442 1.27 uebayasi }
443 1.27 uebayasi a->a_weight++;
444 1.27 uebayasi }
445 1.27 uebayasi
446 1.27 uebayasi static int
447 1.27 uebayasi attrcmp(const void *l, const void *r)
448 1.27 uebayasi {
449 1.27 uebayasi const struct attr * const *a = l, * const *b = r;
450 1.27 uebayasi const int wa = (*a)->a_weight, wb = (*b)->a_weight;
451 1.73 riastrad
452 1.73 riastrad /*
453 1.73 riastrad * Higher-weight first; then, among equal weights, earlier
454 1.73 riastrad * index first.
455 1.73 riastrad */
456 1.73 riastrad if (wa > wb)
457 1.73 riastrad return -1;
458 1.73 riastrad else if (wa < wb)
459 1.73 riastrad return +1;
460 1.73 riastrad else if ((*a)->a_idx < (*b)->a_idx)
461 1.73 riastrad return -1;
462 1.73 riastrad else if ((*a)->a_idx > (*b)->a_idx)
463 1.73 riastrad return +1;
464 1.73 riastrad else
465 1.73 riastrad abort(); /* no ties possible */
466 1.27 uebayasi }
467 1.27 uebayasi
468 1.18 uebayasi static void
469 1.18 uebayasi emitattrkobjs(FILE *fp)
470 1.18 uebayasi {
471 1.18 uebayasi extern struct hashtab *attrtab;
472 1.18 uebayasi
473 1.18 uebayasi ht_enumerate(attrtab, emitattrkobjscb, fp);
474 1.18 uebayasi }
475 1.18 uebayasi
476 1.18 uebayasi static int
477 1.18 uebayasi emitattrkobjscb(const char *name, void *v, void *arg)
478 1.18 uebayasi {
479 1.18 uebayasi struct attr *a = v;
480 1.18 uebayasi struct files *fi;
481 1.18 uebayasi FILE *fp = arg;
482 1.18 uebayasi
483 1.18 uebayasi if (ht_lookup(selecttab, name) == NULL)
484 1.18 uebayasi return 0;
485 1.18 uebayasi if (TAILQ_EMPTY(&a->a_files))
486 1.18 uebayasi return 0;
487 1.18 uebayasi fputc('\n', fp);
488 1.27 uebayasi fprintf(fp, "# %s (%d)\n", name, a->a_weight);
489 1.29 uebayasi fprintf(fp, "OBJS.%s= \\\n", name);
490 1.18 uebayasi TAILQ_FOREACH(fi, &a->a_files, fi_anext) {
491 1.29 uebayasi fprintf(fp, "\t%s.o \\\n", fi->fi_base);
492 1.18 uebayasi }
493 1.18 uebayasi fputc('\n', fp);
494 1.18 uebayasi fprintf(fp, "%s.ko: ${OBJS.%s}\n", name, name);
495 1.18 uebayasi fprintf(fp, "\t${LINK_O}\n");
496 1.18 uebayasi return 0;
497 1.18 uebayasi }
498 1.18 uebayasi
499 1.18 uebayasi static void
500 1.1 thorpej emitcfiles(FILE *fp)
501 1.1 thorpej {
502 1.1 thorpej
503 1.66 uebayasi emitallfiles(fp);
504 1.67 uebayasi fprintf(fp, "#%%CFILES\n");
505 1.1 thorpej }
506 1.1 thorpej
507 1.5 dsl static void
508 1.1 thorpej emitsfiles(FILE *fp)
509 1.1 thorpej {
510 1.1 thorpej
511 1.66 uebayasi emitallfiles(fp);
512 1.67 uebayasi fprintf(fp, "#%%SFILES\n");
513 1.1 thorpej }
514 1.1 thorpej
515 1.5 dsl static void
516 1.66 uebayasi emitallfiles(FILE *fp)
517 1.1 thorpej {
518 1.1 thorpej struct files *fi;
519 1.66 uebayasi static int called;
520 1.68 uebayasi int i;
521 1.63 uebayasi int found = 0;
522 1.1 thorpej
523 1.66 uebayasi if (called++ != 0)
524 1.66 uebayasi return;
525 1.68 uebayasi for (i = 0; i < (int)nselfiles; i++) {
526 1.68 uebayasi fi = selfiles[i];
527 1.66 uebayasi if (found++ == 0)
528 1.66 uebayasi fprintf(fp, "ALLFILES= \\\n");
529 1.50 uebayasi putc('\t', fp);
530 1.58 uebayasi emitfilerel(fp, fi);
531 1.50 uebayasi fputs(" \\\n", fp);
532 1.1 thorpej }
533 1.66 uebayasi fputc('\n', fp);
534 1.1 thorpej }
535 1.1 thorpej
536 1.1 thorpej /*
537 1.1 thorpej * Emit the make-rules.
538 1.1 thorpej */
539 1.5 dsl static void
540 1.1 thorpej emitrules(FILE *fp)
541 1.1 thorpej {
542 1.1 thorpej struct files *fi;
543 1.68 uebayasi int i;
544 1.63 uebayasi int found = 0;
545 1.1 thorpej
546 1.68 uebayasi for (i = 0; i < (int)nselfiles; i++) {
547 1.68 uebayasi fi = selfiles[i];
548 1.46 uebayasi if (fi->fi_mkrule == NULL)
549 1.46 uebayasi continue;
550 1.50 uebayasi fprintf(fp, "%s.o: ", fi->fi_base);
551 1.53 uebayasi emitfile(fp, fi);
552 1.50 uebayasi putc('\n', fp);
553 1.46 uebayasi fprintf(fp, "\t%s\n\n", fi->fi_mkrule);
554 1.68 uebayasi found++;
555 1.1 thorpej }
556 1.63 uebayasi if (found == 0)
557 1.63 uebayasi fprintf(fp, "#%%RULES\n");
558 1.1 thorpej }
559 1.1 thorpej
560 1.1 thorpej /*
561 1.1 thorpej * Emit the load commands.
562 1.1 thorpej *
563 1.1 thorpej * This function is not to be called `spurt'.
564 1.1 thorpej */
565 1.5 dsl static void
566 1.1 thorpej emitload(FILE *fp)
567 1.1 thorpej {
568 1.1 thorpej struct config *cf;
569 1.63 uebayasi int found = 0;
570 1.1 thorpej
571 1.36 uebayasi /*
572 1.36 uebayasi * Generate the backward-compatible "build_kernel" rule if
573 1.36 uebayasi * sys/conf/Makefile.kern.inc doesn't define any (pre-2014 Aug).
574 1.36 uebayasi */
575 1.36 uebayasi if (has_build_kernel == 0) {
576 1.36 uebayasi fprintf(fp, "build_kernel: .USE\n"
577 1.36 uebayasi "\t${SYSTEM_LD_HEAD}\n"
578 1.55 uebayasi "\t${SYSTEM_LD}%s\n"
579 1.36 uebayasi "\t${SYSTEM_LD_TAIL}\n"
580 1.55 uebayasi "\n",
581 1.55 uebayasi Sflag ? "" : " swap${.TARGET}.o");
582 1.36 uebayasi }
583 1.36 uebayasi /*
584 1.36 uebayasi * Generate per-kernel rules.
585 1.36 uebayasi */
586 1.1 thorpej TAILQ_FOREACH(cf, &allcf, cf_next) {
587 1.55 uebayasi char swapobj[100];
588 1.55 uebayasi
589 1.55 uebayasi if (Sflag) {
590 1.55 uebayasi swapobj[0] = '\0';
591 1.55 uebayasi } else {
592 1.55 uebayasi (void)snprintf(swapobj, sizeof(swapobj), " swap%s.o",
593 1.55 uebayasi cf->cf_name);
594 1.55 uebayasi }
595 1.17 joerg fprintf(fp, "KERNELS+=%s\n", cf->cf_name);
596 1.63 uebayasi found = 1;
597 1.1 thorpej }
598 1.63 uebayasi if (found == 0)
599 1.63 uebayasi fprintf(fp, "#%%LOAD\n");
600 1.1 thorpej }
601 1.1 thorpej
602 1.1 thorpej /*
603 1.1 thorpej * Emit include headers (for any prefixes encountered)
604 1.1 thorpej */
605 1.5 dsl static void
606 1.1 thorpej emitincludes(FILE *fp)
607 1.1 thorpej {
608 1.1 thorpej struct prefix *pf;
609 1.1 thorpej
610 1.1 thorpej SLIST_FOREACH(pf, &allprefixes, pf_next) {
611 1.50 uebayasi const char *prologue = (*pf->pf_prefix == '/') ? "" : "$S/";
612 1.50 uebayasi
613 1.5 dsl fprintf(fp, "EXTRA_INCLUDES+=\t-I%s%s\n",
614 1.50 uebayasi prologue, pf->pf_prefix);
615 1.1 thorpej }
616 1.1 thorpej }
617 1.1 thorpej
618 1.1 thorpej /*
619 1.71 riastrad * Emit all options included in a conditional expression
620 1.71 riastrad */
621 1.71 riastrad static void
622 1.71 riastrad emitopts(FILE *fp, struct condexpr *cond, int include)
623 1.71 riastrad {
624 1.71 riastrad
625 1.71 riastrad switch (cond->cx_type) {
626 1.71 riastrad case CX_ATOM:
627 1.71 riastrad if (include && selectopt(cond->cx_u.atom, NULL))
628 1.71 riastrad fprintf(fp, " %s", cond->cx_u.atom);
629 1.71 riastrad break;
630 1.71 riastrad case CX_NOT:
631 1.71 riastrad emitopts(fp, cond->cx_u.not, !include);
632 1.71 riastrad break;
633 1.71 riastrad case CX_AND:
634 1.71 riastrad emitopts(fp, cond->cx_u.and.left, include);
635 1.71 riastrad emitopts(fp, cond->cx_u.and.right, include);
636 1.71 riastrad break;
637 1.71 riastrad case CX_OR:
638 1.71 riastrad emitopts(fp, cond->cx_u.and.left, include);
639 1.71 riastrad emitopts(fp, cond->cx_u.and.right, include);
640 1.71 riastrad break;
641 1.71 riastrad default:
642 1.71 riastrad cfgerror("bug");
643 1.71 riastrad }
644 1.71 riastrad }
645 1.71 riastrad
646 1.71 riastrad /*
647 1.1 thorpej * Emit appending makeoptions.
648 1.1 thorpej */
649 1.5 dsl static void
650 1.1 thorpej emitappmkoptions(FILE *fp)
651 1.1 thorpej {
652 1.1 thorpej struct nvlist *nv;
653 1.14 dholland struct condexpr *cond;
654 1.71 riastrad size_t i;
655 1.71 riastrad
656 1.71 riastrad for (i = 0; i < nselfiles; i++) {
657 1.71 riastrad struct files *const fi = selfiles[i];
658 1.71 riastrad
659 1.71 riastrad if (fi->fi_optx) {
660 1.71 riastrad fprintf(fp, "OPT.%s.c+=", fi->fi_base);
661 1.71 riastrad emitopts(fp, fi->fi_optx, 1);
662 1.71 riastrad fprintf(fp, "\n");
663 1.71 riastrad }
664 1.71 riastrad }
665 1.1 thorpej
666 1.1 thorpej for (nv = appmkoptions; nv != NULL; nv = nv->nv_next)
667 1.5 dsl fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
668 1.1 thorpej
669 1.14 dholland for (nv = condmkoptions; nv != NULL; nv = nv->nv_next) {
670 1.14 dholland cond = nv->nv_ptr;
671 1.14 dholland if (expr_eval(cond, selectopt, NULL))
672 1.69 christos emitmkoption(fp, "+=", nv);
673 1.14 dholland condexpr_destroy(cond);
674 1.14 dholland nv->nv_ptr = NULL;
675 1.11 cube }
676 1.11 cube }
677 1.11 cube
678 1.11 cube static int
679 1.11 cube /*ARGSUSED*/
680 1.11 cube selectopt(const char *name, void *context)
681 1.11 cube {
682 1.11 cube
683 1.11 cube return (ht_lookup(selecttab, strtolower(name)) != NULL);
684 1.1 thorpej }
685