Home | History | Annotate | Line # | Download | only in config
mkmakefile.c revision 1.4
      1  1.4     cube /*	$NetBSD: mkmakefile.c,v 1.4 2006/03/19 16:27:13 cube 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.1  thorpej #include <sys/param.h>
     48  1.1  thorpej #include <ctype.h>
     49  1.1  thorpej #include <errno.h>
     50  1.1  thorpej #include <stdio.h>
     51  1.1  thorpej #include <stdlib.h>
     52  1.1  thorpej #include <string.h>
     53  1.1  thorpej #include "defs.h"
     54  1.1  thorpej #include "sem.h"
     55  1.1  thorpej 
     56  1.1  thorpej /*
     57  1.1  thorpej  * Make the Makefile.
     58  1.1  thorpej  */
     59  1.1  thorpej 
     60  1.1  thorpej static const char *srcpath(struct files *);
     61  1.1  thorpej 
     62  1.1  thorpej static const char *prefix_prologue(const char *);
     63  1.2      erh static const char *filetype_prologue(struct filetype *);
     64  1.2      erh 
     65  1.1  thorpej 
     66  1.1  thorpej static int emitdefs(FILE *);
     67  1.1  thorpej static int emitfiles(FILE *, int, int);
     68  1.1  thorpej 
     69  1.1  thorpej static int emitobjs(FILE *);
     70  1.1  thorpej static int emitcfiles(FILE *);
     71  1.1  thorpej static int emitsfiles(FILE *);
     72  1.1  thorpej static int emitrules(FILE *);
     73  1.1  thorpej static int emitload(FILE *);
     74  1.1  thorpej static int emitincludes(FILE *);
     75  1.1  thorpej static int emitappmkoptions(FILE *);
     76  1.1  thorpej 
     77  1.1  thorpej int
     78  1.1  thorpej mkmakefile(void)
     79  1.1  thorpej {
     80  1.1  thorpej 	FILE *ifp, *ofp;
     81  1.1  thorpej 	int lineno;
     82  1.1  thorpej 	int (*fn)(FILE *);
     83  1.1  thorpej 	char *ifname;
     84  1.1  thorpej 	char line[BUFSIZ], buf[200];
     85  1.1  thorpej 
     86  1.1  thorpej 	/* Try a makefile for the port first.
     87  1.1  thorpej 	 */
     88  1.1  thorpej 	(void)snprintf(buf, sizeof(buf), "arch/%s/conf/Makefile.%s",
     89  1.1  thorpej 	    machine, machine);
     90  1.1  thorpej 	ifname = sourcepath(buf);
     91  1.1  thorpej 	if ((ifp = fopen(ifname, "r")) == NULL) {
     92  1.1  thorpej 		/* Try a makefile for the architecture second.
     93  1.1  thorpej 		 */
     94  1.1  thorpej 		(void)snprintf(buf, sizeof(buf), "arch/%s/conf/Makefile.%s",
     95  1.1  thorpej 		    machinearch, machinearch);
     96  1.4     cube 		free(ifname);
     97  1.1  thorpej 		ifname = sourcepath(buf);
     98  1.4     cube 		ifp = fopen(ifname, "r");
     99  1.1  thorpej 	}
    100  1.4     cube 	if (ifp == NULL) {
    101  1.1  thorpej 		(void)fprintf(stderr, "config: cannot read %s: %s\n",
    102  1.1  thorpej 		    ifname, strerror(errno));
    103  1.4     cube 		goto bad2;
    104  1.1  thorpej 	}
    105  1.1  thorpej 	if ((ofp = fopen("Makefile.tmp", "w")) == NULL) {
    106  1.1  thorpej 		(void)fprintf(stderr, "config: cannot write Makefile: %s\n",
    107  1.1  thorpej 		    strerror(errno));
    108  1.4     cube 		goto bad1;
    109  1.1  thorpej 	}
    110  1.1  thorpej 	if (emitdefs(ofp) != 0)
    111  1.1  thorpej 		goto wrerror;
    112  1.1  thorpej 	lineno = 0;
    113  1.1  thorpej 	while (fgets(line, sizeof(line), ifp) != NULL) {
    114  1.1  thorpej 		lineno++;
    115  1.1  thorpej 		if (line[0] != '%') {
    116  1.1  thorpej 			if (fputs(line, ofp) < 0)
    117  1.1  thorpej 				goto wrerror;
    118  1.1  thorpej 			continue;
    119  1.1  thorpej 		}
    120  1.1  thorpej 		if (strcmp(line, "%OBJS\n") == 0)
    121  1.1  thorpej 			fn = emitobjs;
    122  1.1  thorpej 		else if (strcmp(line, "%CFILES\n") == 0)
    123  1.1  thorpej 			fn = emitcfiles;
    124  1.1  thorpej 		else if (strcmp(line, "%SFILES\n") == 0)
    125  1.1  thorpej 			fn = emitsfiles;
    126  1.1  thorpej 		else if (strcmp(line, "%RULES\n") == 0)
    127  1.1  thorpej 			fn = emitrules;
    128  1.1  thorpej 		else if (strcmp(line, "%LOAD\n") == 0)
    129  1.1  thorpej 			fn = emitload;
    130  1.1  thorpej 		else if (strcmp(line, "%INCLUDES\n") == 0)
    131  1.1  thorpej 			fn = emitincludes;
    132  1.1  thorpej 		else if (strcmp(line, "%MAKEOPTIONSAPPEND\n") == 0)
    133  1.1  thorpej 			fn = emitappmkoptions;
    134  1.1  thorpej 		else {
    135  1.1  thorpej 			xerror(ifname, lineno,
    136  1.1  thorpej 			    "unknown %% construct ignored: %s", line);
    137  1.1  thorpej 			continue;
    138  1.1  thorpej 		}
    139  1.1  thorpej 		if ((*fn)(ofp))
    140  1.1  thorpej 			goto wrerror;
    141  1.1  thorpej 	}
    142  1.1  thorpej 	if (ferror(ifp)) {
    143  1.1  thorpej 		(void)fprintf(stderr,
    144  1.1  thorpej 		    "config: error reading %s (at line %d): %s\n",
    145  1.1  thorpej 		    ifname, lineno, strerror(errno));
    146  1.1  thorpej 		goto bad;
    147  1.1  thorpej 	}
    148  1.1  thorpej 	if (fclose(ofp)) {
    149  1.1  thorpej 		ofp = NULL;
    150  1.1  thorpej 		goto wrerror;
    151  1.1  thorpej 	}
    152  1.1  thorpej 	(void)fclose(ifp);
    153  1.1  thorpej 	if (moveifchanged("Makefile.tmp", "Makefile") != 0) {
    154  1.1  thorpej 		(void)fprintf(stderr,
    155  1.1  thorpej 		    "config: error renaming Makefile: %s\n",
    156  1.1  thorpej 		    strerror(errno));
    157  1.4     cube 		goto bad2;
    158  1.1  thorpej 	}
    159  1.1  thorpej 	free(ifname);
    160  1.1  thorpej 	return (0);
    161  1.4     cube 
    162  1.1  thorpej  wrerror:
    163  1.1  thorpej 	(void)fprintf(stderr, "config: error writing Makefile: %s\n",
    164  1.1  thorpej 	    strerror(errno));
    165  1.1  thorpej  bad:
    166  1.1  thorpej 	if (ofp != NULL)
    167  1.1  thorpej 		(void)fclose(ofp);
    168  1.4     cube  bad1:
    169  1.4     cube 	(void)fclose(ifp);
    170  1.1  thorpej 	/* (void)unlink("Makefile.tmp"); */
    171  1.4     cube  bad2:
    172  1.1  thorpej 	free(ifname);
    173  1.1  thorpej 	return (1);
    174  1.1  thorpej }
    175  1.1  thorpej 
    176  1.1  thorpej /*
    177  1.1  thorpej  * Return (possibly in a static buffer) the name of the `source' for a
    178  1.1  thorpej  * file.  If we have `options source', or if the file is marked `always
    179  1.1  thorpej  * source', this is always the path from the `file' line; otherwise we
    180  1.1  thorpej  * get the .o from the obj-directory.
    181  1.1  thorpej  */
    182  1.1  thorpej static const char *
    183  1.1  thorpej srcpath(struct files *fi)
    184  1.1  thorpej {
    185  1.1  thorpej #if 1
    186  1.1  thorpej 	/* Always have source, don't support object dirs for kernel builds. */
    187  1.1  thorpej 	return (fi->fi_path);
    188  1.1  thorpej #else
    189  1.1  thorpej 	static char buf[MAXPATHLEN];
    190  1.1  thorpej 
    191  1.1  thorpej 	if (have_source || (fi->fi_flags & FI_ALWAYSSRC) != 0)
    192  1.1  thorpej 		return (fi->fi_path);
    193  1.1  thorpej 	if (objpath == NULL) {
    194  1.1  thorpej 		error("obj-directory not set");
    195  1.1  thorpej 		return (NULL);
    196  1.1  thorpej 	}
    197  1.1  thorpej 	(void)snprintf(buf, sizeof buf, "%s/%s.o", objpath, fi->fi_base);
    198  1.1  thorpej 	return (buf);
    199  1.1  thorpej #endif
    200  1.1  thorpej }
    201  1.1  thorpej 
    202  1.1  thorpej static const char *
    203  1.2      erh filetype_prologue(struct filetype *fit)
    204  1.2      erh {
    205  1.2      erh 	if (fit->fit_flags & FIT_NOPROLOGUE || *fit->fit_path == '/')
    206  1.2      erh 		return ("");
    207  1.2      erh 	else
    208  1.2      erh 		return ("$S/");
    209  1.2      erh }
    210  1.2      erh 
    211  1.2      erh static const char *
    212  1.1  thorpej prefix_prologue(const char *path)
    213  1.1  thorpej {
    214  1.1  thorpej 	if (*path == '/')
    215  1.1  thorpej 		return ("");
    216  1.1  thorpej 	else
    217  1.1  thorpej 		return ("$S/");
    218  1.1  thorpej }
    219  1.1  thorpej 
    220  1.1  thorpej static int
    221  1.1  thorpej emitdefs(FILE *fp)
    222  1.1  thorpej {
    223  1.1  thorpej 	struct nvlist *nv;
    224  1.1  thorpej 	char *sp;
    225  1.1  thorpej 
    226  1.1  thorpej 	if (fprintf(fp, "KERNEL_BUILD=%s\n", conffile) < 0)
    227  1.1  thorpej 		return (1);
    228  1.1  thorpej 	if (fputs("IDENT=", fp) < 0)
    229  1.1  thorpej 		return (1);
    230  1.1  thorpej 	sp = "";
    231  1.1  thorpej 	for (nv = options; nv != NULL; nv = nv->nv_next) {
    232  1.1  thorpej 
    233  1.1  thorpej 		/* skip any options output to a header file */
    234  1.1  thorpej 		if (DEFINED_OPTION(nv->nv_name))
    235  1.1  thorpej 			continue;
    236  1.1  thorpej 		if (fprintf(fp, "%s-D%s", sp, nv->nv_name) < 0)
    237  1.1  thorpej 		    return 1;
    238  1.1  thorpej 		if (nv->nv_str)
    239  1.1  thorpej 		    if (fprintf(fp, "=\"%s\"", nv->nv_str) < 0)
    240  1.1  thorpej 			return 1;
    241  1.1  thorpej 		sp = " ";
    242  1.1  thorpej 	}
    243  1.1  thorpej 	if (putc('\n', fp) < 0)
    244  1.1  thorpej 		return (1);
    245  1.1  thorpej 	if (fprintf(fp, "PARAM=-DMAXUSERS=%d\n", maxusers) < 0)
    246  1.1  thorpej 		return (1);
    247  1.1  thorpej 	if (fprintf(fp, "MACHINE=%s\n", machine) < 0)
    248  1.1  thorpej 		return (1);
    249  1.1  thorpej 	if (*srcdir == '/' || *srcdir == '.') {
    250  1.1  thorpej 		if (fprintf(fp, "S=\t%s\n", srcdir) < 0)
    251  1.1  thorpej 			return (1);
    252  1.1  thorpej 	} else {
    253  1.1  thorpej 		/*
    254  1.1  thorpej 		 * libkern and libcompat "Makefile.inc"s want relative S
    255  1.1  thorpej 		 * specification to begin with '.'.
    256  1.1  thorpej 		 */
    257  1.1  thorpej 		if (fprintf(fp, "S=\t./%s\n", srcdir) < 0)
    258  1.1  thorpej 			return (1);
    259  1.1  thorpej 	}
    260  1.1  thorpej 	for (nv = mkoptions; nv != NULL; nv = nv->nv_next)
    261  1.1  thorpej 		if (fprintf(fp, "%s=%s\n", nv->nv_name, nv->nv_str) < 0)
    262  1.1  thorpej 			return (1);
    263  1.1  thorpej 	return (0);
    264  1.1  thorpej }
    265  1.1  thorpej 
    266  1.1  thorpej static int
    267  1.1  thorpej emitobjs(FILE *fp)
    268  1.1  thorpej {
    269  1.1  thorpej 	struct files *fi;
    270  1.1  thorpej 	struct objects *oi;
    271  1.1  thorpej 	int lpos, len, sp;
    272  1.1  thorpej 
    273  1.1  thorpej 	if (fputs("OBJS=", fp) < 0)
    274  1.1  thorpej 		return (1);
    275  1.1  thorpej 	sp = '\t';
    276  1.1  thorpej 	lpos = 7;
    277  1.1  thorpej 	TAILQ_FOREACH(fi, &allfiles, fi_next) {
    278  1.1  thorpej 		if ((fi->fi_flags & FI_SEL) == 0)
    279  1.1  thorpej 			continue;
    280  1.1  thorpej 		len = strlen(fi->fi_base) + 2;
    281  1.1  thorpej 		if (lpos + len > 72) {
    282  1.1  thorpej 			if (fputs(" \\\n", fp) < 0)
    283  1.1  thorpej 				return (1);
    284  1.1  thorpej 			sp = '\t';
    285  1.1  thorpej 			lpos = 7;
    286  1.1  thorpej 		}
    287  1.1  thorpej 		if (fprintf(fp, "%c%s.o", sp, fi->fi_base) < 0)
    288  1.1  thorpej 			return (1);
    289  1.1  thorpej 		lpos += len + 1;
    290  1.1  thorpej 		sp = ' ';
    291  1.1  thorpej 	}
    292  1.1  thorpej 	TAILQ_FOREACH(oi, &allobjects, oi_next) {
    293  1.1  thorpej 		if ((oi->oi_flags & OI_SEL) == 0)
    294  1.1  thorpej 			continue;
    295  1.1  thorpej 		len = strlen(oi->oi_path);
    296  1.2      erh 		if (*oi->oi_path != '/')
    297  1.2      erh 		{
    298  1.2      erh 			/* e.g. "$S/" */
    299  1.2      erh  			if (oi->oi_prefix != NULL)
    300  1.2      erh 				len += strlen(prefix_prologue(oi->oi_path)) +
    301  1.2      erh 				       strlen(oi->oi_prefix) + 1;
    302  1.2      erh 			else
    303  1.2      erh 				len += strlen(filetype_prologue(&oi->oi_fit));
    304  1.1  thorpej 		}
    305  1.1  thorpej 		if (lpos + len > 72) {
    306  1.1  thorpej 			if (fputs(" \\\n", fp) < 0)
    307  1.1  thorpej 				return (1);
    308  1.1  thorpej 			sp = '\t';
    309  1.1  thorpej 			lpos = 7;
    310  1.1  thorpej 		}
    311  1.1  thorpej 		if (*oi->oi_path == '/') {
    312  1.1  thorpej 			if (fprintf(fp, "%c%s", sp, oi->oi_path) < 0)
    313  1.1  thorpej 				return (1);
    314  1.1  thorpej 		} else {
    315  1.1  thorpej 			if (oi->oi_prefix != NULL) {
    316  1.1  thorpej 				if (fprintf(fp, "%c%s%s/%s", sp,
    317  1.2      erh 				    prefix_prologue(oi->oi_path),
    318  1.1  thorpej 				    oi->oi_prefix, oi->oi_path) < 0)
    319  1.1  thorpej 					return (1);
    320  1.1  thorpej 			} else {
    321  1.2      erh 				if (fprintf(fp, "%c%s%s", sp,
    322  1.2      erh 				            filetype_prologue(&oi->oi_fit),
    323  1.2      erh 				            oi->oi_path) < 0)
    324  1.1  thorpej 					return (1);
    325  1.1  thorpej 			}
    326  1.1  thorpej 		}
    327  1.1  thorpej 		lpos += len + 1;
    328  1.1  thorpej 		sp = ' ';
    329  1.1  thorpej 	}
    330  1.1  thorpej 	if (putc('\n', fp) < 0)
    331  1.1  thorpej 		return (1);
    332  1.1  thorpej 	return (0);
    333  1.1  thorpej }
    334  1.1  thorpej 
    335  1.1  thorpej static int
    336  1.1  thorpej emitcfiles(FILE *fp)
    337  1.1  thorpej {
    338  1.1  thorpej 
    339  1.1  thorpej 	return (emitfiles(fp, 'c', 0));
    340  1.1  thorpej }
    341  1.1  thorpej 
    342  1.1  thorpej static int
    343  1.1  thorpej emitsfiles(FILE *fp)
    344  1.1  thorpej {
    345  1.1  thorpej 
    346  1.1  thorpej 	return (emitfiles(fp, 's', 1));
    347  1.1  thorpej }
    348  1.1  thorpej 
    349  1.1  thorpej static int
    350  1.1  thorpej emitfiles(FILE *fp, int suffix, int upper_suffix)
    351  1.1  thorpej {
    352  1.1  thorpej 	struct files *fi;
    353  1.1  thorpej 	int lpos, len, sp;
    354  1.1  thorpej 	const char *fpath;
    355  1.3      erh  	struct config *cf;
    356  1.3      erh  	char swapname[100];
    357  1.1  thorpej 
    358  1.1  thorpej 	if (fprintf(fp, "%cFILES=", toupper(suffix)) < 0)
    359  1.1  thorpej 		return (1);
    360  1.1  thorpej 	sp = '\t';
    361  1.1  thorpej 	lpos = 7;
    362  1.1  thorpej 	TAILQ_FOREACH(fi, &allfiles, fi_next) {
    363  1.1  thorpej 		if ((fi->fi_flags & FI_SEL) == 0)
    364  1.1  thorpej 			continue;
    365  1.1  thorpej 		if ((fpath = srcpath(fi)) == NULL)
    366  1.2      erh 			return (1);
    367  1.1  thorpej 		len = strlen(fpath);
    368  1.1  thorpej 		if (! ((fpath[len - 1] == suffix) ||
    369  1.1  thorpej 		    (upper_suffix && fpath[len - 1] == toupper(suffix))))
    370  1.1  thorpej 			continue;
    371  1.2      erh 		if (*fpath != '/')
    372  1.2      erh 		{
    373  1.2      erh 			/* "$S/" */
    374  1.2      erh  			if (fi->fi_prefix != NULL)
    375  1.2      erh 				len += strlen(prefix_prologue(fi->fi_prefix)) +
    376  1.2      erh 				       strlen(fi->fi_prefix) + 1;
    377  1.2      erh 			else
    378  1.2      erh 				len += strlen(filetype_prologue(&fi->fi_fit));
    379  1.1  thorpej 		}
    380  1.1  thorpej 		if (lpos + len > 72) {
    381  1.1  thorpej 			if (fputs(" \\\n", fp) < 0)
    382  1.1  thorpej 				return (1);
    383  1.1  thorpej 			sp = '\t';
    384  1.1  thorpej 			lpos = 7;
    385  1.1  thorpej 		}
    386  1.1  thorpej 		if (*fi->fi_path == '/') {
    387  1.2      erh 			if (fprintf(fp, "%c%s", sp, fpath) < 0)
    388  1.1  thorpej 				return (1);
    389  1.1  thorpej 		} else {
    390  1.1  thorpej 			if (fi->fi_prefix != NULL) {
    391  1.1  thorpej 				if (fprintf(fp, "%c%s%s/%s", sp,
    392  1.1  thorpej 				    prefix_prologue(fi->fi_prefix),
    393  1.2      erh 				    fi->fi_prefix, fpath) < 0)
    394  1.1  thorpej 					return (1);
    395  1.1  thorpej 			} else {
    396  1.2      erh 				if (fprintf(fp, "%c%s%s", sp,
    397  1.2      erh 				            filetype_prologue(&fi->fi_fit),
    398  1.2      erh 				            fpath) < 0)
    399  1.1  thorpej 					return (1);
    400  1.1  thorpej 			}
    401  1.1  thorpej 		}
    402  1.1  thorpej 		lpos += len + 1;
    403  1.1  thorpej 		sp = ' ';
    404  1.1  thorpej 	}
    405  1.3      erh  	/*
    406  1.3      erh  	 * The allfiles list does not include the configuration-specific
    407  1.3      erh  	 * C source files.  These files should be eliminated someday, but
    408  1.3      erh  	 * for now, we have to add them to ${CFILES} (and only ${CFILES}).
    409  1.3      erh  	 */
    410  1.3      erh  	if (suffix == 'c') {
    411  1.3      erh  		TAILQ_FOREACH(cf, &allcf, cf_next) {
    412  1.3      erh  			(void)snprintf(swapname, sizeof(swapname), "swap%s.c",
    413  1.3      erh  			    cf->cf_name);
    414  1.3      erh  			len = strlen(swapname);
    415  1.3      erh  			if (lpos + len > 72) {
    416  1.3      erh  				if (fputs(" \\\n", fp) < 0)
    417  1.3      erh  					return (1);
    418  1.3      erh  				sp = '\t';
    419  1.3      erh  				lpos = 7;
    420  1.3      erh  			}
    421  1.3      erh  			if (fprintf(fp, "%c%s", sp, swapname) < 0)
    422  1.3      erh  				return (1);
    423  1.3      erh  			lpos += len + 1;
    424  1.3      erh  			sp = ' ';
    425  1.3      erh  		}
    426  1.3      erh  	}
    427  1.1  thorpej 	if (putc('\n', fp) < 0)
    428  1.1  thorpej 		return (1);
    429  1.1  thorpej 	return (0);
    430  1.1  thorpej }
    431  1.1  thorpej 
    432  1.1  thorpej /*
    433  1.1  thorpej  * Emit the make-rules.
    434  1.1  thorpej  */
    435  1.1  thorpej static int
    436  1.1  thorpej emitrules(FILE *fp)
    437  1.1  thorpej {
    438  1.1  thorpej 	struct files *fi;
    439  1.1  thorpej 	const char *cp, *fpath;
    440  1.1  thorpej 	int ch;
    441  1.1  thorpej 	char buf[200];
    442  1.1  thorpej 
    443  1.1  thorpej 	TAILQ_FOREACH(fi, &allfiles, fi_next) {
    444  1.1  thorpej 		if ((fi->fi_flags & FI_SEL) == 0)
    445  1.1  thorpej 			continue;
    446  1.1  thorpej 		if ((fpath = srcpath(fi)) == NULL)
    447  1.1  thorpej 			return (1);
    448  1.1  thorpej 		if (*fpath == '/') {
    449  1.1  thorpej 			if (fprintf(fp, "%s.o: %s\n", fi->fi_base, fpath) < 0)
    450  1.1  thorpej 				return (1);
    451  1.1  thorpej 		} else {
    452  1.1  thorpej 			if (fi->fi_prefix != NULL) {
    453  1.1  thorpej 				if (fprintf(fp, "%s.o: %s%s/%s\n", fi->fi_base,
    454  1.1  thorpej 				    prefix_prologue(fi->fi_prefix),
    455  1.1  thorpej 				    fi->fi_prefix, fpath) < 0)
    456  1.1  thorpej 					return (1);
    457  1.1  thorpej 			} else {
    458  1.2      erh 				if (fprintf(fp, "%s.o: %s%s\n",
    459  1.2      erh 				            fi->fi_base,
    460  1.2      erh 				            filetype_prologue(&fi->fi_fit),
    461  1.2      erh 				            fpath) < 0)
    462  1.1  thorpej 					return (1);
    463  1.1  thorpej 			}
    464  1.1  thorpej 		}
    465  1.1  thorpej 		if ((cp = fi->fi_mkrule) == NULL) {
    466  1.1  thorpej 			cp = "NORMAL";
    467  1.1  thorpej 			ch = fpath[strlen(fpath) - 1];
    468  1.1  thorpej 			if (islower(ch))
    469  1.1  thorpej 				ch = toupper(ch);
    470  1.1  thorpej 			(void)snprintf(buf, sizeof(buf), "${%s_%c}", cp, ch);
    471  1.1  thorpej 			cp = buf;
    472  1.1  thorpej 		}
    473  1.1  thorpej 		if (fprintf(fp, "\t%s\n\n", cp) < 0)
    474  1.1  thorpej 			return (1);
    475  1.1  thorpej 	}
    476  1.1  thorpej 	return (0);
    477  1.1  thorpej }
    478  1.1  thorpej 
    479  1.1  thorpej /*
    480  1.1  thorpej  * Emit the load commands.
    481  1.1  thorpej  *
    482  1.1  thorpej  * This function is not to be called `spurt'.
    483  1.1  thorpej  */
    484  1.1  thorpej static int
    485  1.1  thorpej emitload(FILE *fp)
    486  1.1  thorpej {
    487  1.1  thorpej 	struct config *cf;
    488  1.1  thorpej 	const char *nm, *swname;
    489  1.1  thorpej 
    490  1.1  thorpej 	if (fputs(".MAIN: all\nall:", fp) < 0)
    491  1.1  thorpej 		return (1);
    492  1.1  thorpej 	TAILQ_FOREACH(cf, &allcf, cf_next) {
    493  1.1  thorpej 		if (fprintf(fp, " %s", cf->cf_name) < 0)
    494  1.1  thorpej 			return (1);
    495  1.1  thorpej 	}
    496  1.1  thorpej 	if (fputs("\n\n", fp) < 0)
    497  1.1  thorpej 		return (1);
    498  1.1  thorpej 	TAILQ_FOREACH(cf, &allcf, cf_next) {
    499  1.1  thorpej 		nm = cf->cf_name;
    500  1.1  thorpej 		swname =
    501  1.1  thorpej 		    cf->cf_root != NULL ? cf->cf_name : "generic";
    502  1.1  thorpej 		if (fprintf(fp, "KERNELS+=%s\n", nm) < 0)
    503  1.1  thorpej 			return (1);
    504  1.3      erh 		if (fprintf(fp, "%s: ${SYSTEM_DEP} swap${.TARGET}.o vers.o", nm) < 0)
    505  1.1  thorpej 			return (1);
    506  1.1  thorpej 		if (fprintf(fp, "\n\
    507  1.1  thorpej \t${SYSTEM_LD_HEAD}\n\
    508  1.3      erh \t${SYSTEM_LD} swap${.TARGET}.o\n\
    509  1.1  thorpej \t${SYSTEM_LD_TAIL}\n\
    510  1.3      erh \n\
    511  1.3      erh swap%s.o: swap%s.c\n\
    512  1.3      erh \t${NORMAL_C}\n\n", swname, swname) < 0)
    513  1.1  thorpej 			return (1);
    514  1.1  thorpej 	}
    515  1.1  thorpej 	return (0);
    516  1.1  thorpej }
    517  1.1  thorpej 
    518  1.1  thorpej /*
    519  1.1  thorpej  * Emit include headers (for any prefixes encountered)
    520  1.1  thorpej  */
    521  1.1  thorpej static int
    522  1.1  thorpej emitincludes(FILE *fp)
    523  1.1  thorpej {
    524  1.1  thorpej 	struct prefix *pf;
    525  1.1  thorpej 
    526  1.1  thorpej 	SLIST_FOREACH(pf, &allprefixes, pf_next) {
    527  1.1  thorpej 		if (fprintf(fp, "EXTRA_INCLUDES+=\t-I%s%s\n",
    528  1.1  thorpej 		    prefix_prologue(pf->pf_prefix), pf->pf_prefix) < 0)
    529  1.1  thorpej 			return (1);
    530  1.1  thorpej 	}
    531  1.1  thorpej 
    532  1.1  thorpej 	return (0);
    533  1.1  thorpej }
    534  1.1  thorpej 
    535  1.1  thorpej static int
    536  1.1  thorpej print_condmkopts(const char *name, void *value, void *arg)
    537  1.1  thorpej {
    538  1.1  thorpej 	struct nvlist *nv;
    539  1.1  thorpej 	FILE *fp = arg;
    540  1.1  thorpej 
    541  1.1  thorpej 	if (ht_lookup(selecttab, name) == 0)
    542  1.1  thorpej 		return (0);
    543  1.1  thorpej 
    544  1.1  thorpej 	for (nv = value; nv != NULL; nv = nv->nv_next)
    545  1.1  thorpej 		if (fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str) < 0)
    546  1.1  thorpej 			return (1);
    547  1.1  thorpej 
    548  1.1  thorpej 	return (0);
    549  1.1  thorpej }
    550  1.1  thorpej 
    551  1.1  thorpej /*
    552  1.1  thorpej  * Emit appending makeoptions.
    553  1.1  thorpej  */
    554  1.1  thorpej static int
    555  1.1  thorpej emitappmkoptions(FILE *fp)
    556  1.1  thorpej {
    557  1.1  thorpej 	struct nvlist *nv;
    558  1.1  thorpej 
    559  1.1  thorpej 	for (nv = appmkoptions; nv != NULL; nv = nv->nv_next)
    560  1.1  thorpej 		if (fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str) < 0)
    561  1.1  thorpej 			return (1);
    562  1.1  thorpej 
    563  1.1  thorpej 	return (ht_enumerate(condmkopttab, print_condmkopts, fp));
    564  1.1  thorpej }
    565