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