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