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