Home | History | Annotate | Line # | Download | only in config
mkmakefile.c revision 1.24
      1 /*	$NetBSD: mkmakefile.c,v 1.24 2014/10/09 19:33:43 uebayasi Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratories.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	from: @(#)mkmakefile.c	8.1 (Berkeley) 6/6/93
     41  */
     42 
     43 #if HAVE_NBTOOL_CONFIG_H
     44 #include "nbtool_config.h"
     45 #endif
     46 
     47 #include <sys/param.h>
     48 #include <ctype.h>
     49 #include <errno.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <err.h>
     54 #include "defs.h"
     55 #include "sem.h"
     56 
     57 /*
     58  * Make the Makefile.
     59  */
     60 
     61 static const char *srcpath(struct files *);
     62 
     63 static const char *prefix_prologue(const char *);
     64 static const char *filetype_prologue(struct filetype *);
     65 
     66 
     67 static void emitdefs(FILE *);
     68 static void emitfiles(FILE *, int, int);
     69 
     70 static void emitobjs(FILE *);
     71 static void emitallkobjs(FILE *);
     72 static int emitallkobjscb(const char *, void *, void *);
     73 static void emitattrkobjs(FILE *);
     74 static int emitattrkobjscb(const char *, void *, void *);
     75 static void emitkobjs(FILE *);
     76 static void emitcfiles(FILE *);
     77 static void emitsfiles(FILE *);
     78 static void emitrules(FILE *);
     79 static void emitload(FILE *);
     80 static void emitincludes(FILE *);
     81 static void emitappmkoptions(FILE *);
     82 static void emitsubs(FILE *, const char *, const char *, int);
     83 static int  selectopt(const char *, void *);
     84 
     85 /* Generate Makefile to build things per-attribute *.ko (a.k.a modular build). */
     86 int usekobjs = 0;
     87 
     88 int
     89 mkmakefile(void)
     90 {
     91 	FILE *ifp, *ofp;
     92 	int lineno;
     93 	void (*fn)(FILE *);
     94 	char *ifname;
     95 	char line[BUFSIZ], buf[200];
     96 
     97 	/* Try a makefile for the port first.
     98 	 */
     99 	(void)snprintf(buf, sizeof(buf), "arch/%s/conf/Makefile.%s",
    100 	    machine, machine);
    101 	ifname = sourcepath(buf);
    102 	if ((ifp = fopen(ifname, "r")) == NULL) {
    103 		/*
    104 		 * Try a makefile for the architecture second.
    105 		 */
    106 		(void)snprintf(buf, sizeof(buf), "arch/%s/conf/Makefile.%s",
    107 		    machinearch, machinearch);
    108 		free(ifname);
    109 		ifname = sourcepath(buf);
    110 		ifp = fopen(ifname, "r");
    111 	}
    112 	if (ifp == NULL) {
    113 		warn("cannot read %s", ifname);
    114 		goto bad2;
    115 	}
    116 
    117 	if ((ofp = fopen("Makefile.tmp", "w")) == NULL) {
    118 		warn("cannot write Makefile");
    119 		goto bad1;
    120 	}
    121 
    122 	emitdefs(ofp);
    123 
    124 	lineno = 0;
    125 	while (fgets(line, sizeof(line), ifp) != NULL) {
    126 		lineno++;
    127 		if ((version < 20090214 && line[0] != '%') || line[0] == '#') {
    128 			fputs(line, ofp);
    129 			continue;
    130 		}
    131 		if (strcmp(line, "%OBJS\n") == 0)
    132 			fn = usekobjs ? emitkobjs : emitobjs;
    133 		else if (strcmp(line, "%CFILES\n") == 0)
    134 			fn = emitcfiles;
    135 		else if (strcmp(line, "%SFILES\n") == 0)
    136 			fn = emitsfiles;
    137 		else if (strcmp(line, "%RULES\n") == 0)
    138 			fn = emitrules;
    139 		else if (strcmp(line, "%LOAD\n") == 0)
    140 			fn = emitload;
    141 		else if (strcmp(line, "%INCLUDES\n") == 0)
    142 			fn = emitincludes;
    143 		else if (strcmp(line, "%MAKEOPTIONSAPPEND\n") == 0)
    144 			fn = emitappmkoptions;
    145 		else if (strncmp(line, "%VERSION ", sizeof("%VERSION ")-1) == 0) {
    146 			int newvers;
    147 			if (sscanf(line, "%%VERSION %d\n", &newvers) != 1) {
    148 				cfgxerror(ifname, lineno, "syntax error for "
    149 				    "%%VERSION");
    150 			} else
    151 				setversion(newvers);
    152 			continue;
    153 		} else {
    154 			if (version < 20090214)
    155 				cfgxerror(ifname, lineno,
    156 				    "unknown %% construct ignored: %s", line);
    157 			else
    158 				emitsubs(ofp, line, ifname, lineno);
    159 			continue;
    160 		}
    161 		(*fn)(ofp);
    162 	}
    163 
    164 	fflush(ofp);
    165 	if (ferror(ofp))
    166 		goto wrerror;
    167 
    168 	if (ferror(ifp)) {
    169 		warn("error reading %s (at line %d)", ifname, lineno);
    170 		goto bad;
    171 	}
    172 
    173 	if (fclose(ofp)) {
    174 		ofp = NULL;
    175 		goto wrerror;
    176 	}
    177 	(void)fclose(ifp);
    178 
    179 	if (moveifchanged("Makefile.tmp", "Makefile") != 0) {
    180 		warn("error renaming Makefile");
    181 		goto bad2;
    182 	}
    183 	free(ifname);
    184 	return (0);
    185 
    186  wrerror:
    187 	warn("error writing Makefile");
    188  bad:
    189 	if (ofp != NULL)
    190 		(void)fclose(ofp);
    191  bad1:
    192 	(void)fclose(ifp);
    193 	/* (void)unlink("Makefile.tmp"); */
    194  bad2:
    195 	free(ifname);
    196 	return (1);
    197 }
    198 
    199 static void
    200 emitsubs(FILE *fp, const char *line, const char *file, int lineno)
    201 {
    202 	char *nextpct;
    203 	const char *optname;
    204 	struct nvlist *option;
    205 
    206 	while (*line != '\0') {
    207 		if (*line != '%') {
    208 			fputc(*line++, fp);
    209 			continue;
    210 		}
    211 
    212 		line++;
    213 		nextpct = strchr(line, '%');
    214 		if (nextpct == NULL) {
    215 			cfgxerror(file, lineno, "unbalanced %% or "
    216 			    "unknown construct");
    217 			return;
    218 		}
    219 		*nextpct = '\0';
    220 
    221 		if (*line == '\0')
    222 			fputc('%', fp);
    223 		else {
    224 			optname = intern(line);
    225 			if (!DEFINED_OPTION(optname)) {
    226 				cfgxerror(file, lineno, "unknown option %s",
    227 				    optname);
    228 				return;
    229 			}
    230 
    231 			if ((option = ht_lookup(opttab, optname)) == NULL)
    232 				option = ht_lookup(fsopttab, optname);
    233 			if (option != NULL)
    234 				fputs(option->nv_str ? option->nv_str : "1",
    235 				    fp);
    236 			/*
    237 			 * Otherwise it's not a selected option and we don't
    238 			 * output anything.
    239 			 */
    240 		}
    241 
    242 		line = nextpct + 1;
    243 	}
    244 }
    245 
    246 /*
    247  * Return (possibly in a static buffer) the name of the `source' for a
    248  * file.  If we have `options source', or if the file is marked `always
    249  * source', this is always the path from the `file' line; otherwise we
    250  * get the .o from the obj-directory.
    251  */
    252 static const char *
    253 srcpath(struct files *fi)
    254 {
    255 #if 1
    256 	/* Always have source, don't support object dirs for kernel builds. */
    257 	return (fi->fi_path);
    258 #else
    259 	static char buf[MAXPATHLEN];
    260 
    261 	if (have_source || (fi->fi_flags & FI_ALWAYSSRC) != 0)
    262 		return (fi->fi_path);
    263 	if (objpath == NULL) {
    264 		cfgerror("obj-directory not set");
    265 		return (NULL);
    266 	}
    267 	(void)snprintf(buf, sizeof buf, "%s/%s.o", objpath, fi->fi_base);
    268 	return (buf);
    269 #endif
    270 }
    271 
    272 static const char *
    273 filetype_prologue(struct filetype *fit)
    274 {
    275 	if (*fit->fit_path == '/')
    276 		return ("");
    277 	else
    278 		return ("$S/");
    279 }
    280 
    281 static const char *
    282 prefix_prologue(const char *path)
    283 {
    284 	if (*path == '/')
    285 		return ("");
    286 	else
    287 		return ("$S/");
    288 }
    289 
    290 static void
    291 emitdefs(FILE *fp)
    292 {
    293 	struct nvlist *nv;
    294 	const char *sp;
    295 
    296 	fprintf(fp, "KERNEL_BUILD=%s\n", conffile);
    297 	fputs("IDENT=", fp);
    298 	sp = "";
    299 	for (nv = options; nv != NULL; nv = nv->nv_next) {
    300 
    301 		/* Skip any options output to a header file */
    302 		if (DEFINED_OPTION(nv->nv_name))
    303 			continue;
    304 		fprintf(fp, "%s-D%s", sp, nv->nv_name);
    305 		if (nv->nv_str)
    306 			fprintf(fp, "=\"%s\"", nv->nv_str);
    307 		sp = " ";
    308 	}
    309 	putc('\n', fp);
    310 	fprintf(fp, "PARAM=-DMAXUSERS=%d\n", maxusers);
    311 	fprintf(fp, "MACHINE=%s\n", machine);
    312 	if (*srcdir == '/' || *srcdir == '.') {
    313 		fprintf(fp, "S=\t%s\n", srcdir);
    314 	} else {
    315 		/*
    316 		 * libkern and libcompat "Makefile.inc"s want relative S
    317 		 * specification to begin with '.'.
    318 		 */
    319 		fprintf(fp, "S=\t./%s\n", srcdir);
    320 	}
    321 	for (nv = mkoptions; nv != NULL; nv = nv->nv_next)
    322 		fprintf(fp, "%s=%s\n", nv->nv_name, nv->nv_str);
    323 }
    324 
    325 static void
    326 emitobjs(FILE *fp)
    327 {
    328 	struct files *fi;
    329 	struct objects *oi;
    330 
    331 	fputs("OBJS= \\\n", fp);
    332 	TAILQ_FOREACH(fi, &allfiles, fi_next) {
    333 		if ((fi->fi_flags & FI_SEL) == 0)
    334 			continue;
    335 		fprintf(fp, "\t%s.o \\\n", fi->fi_base);
    336 	}
    337 	TAILQ_FOREACH(oi, &allobjects, oi_next) {
    338 		if ((oi->oi_flags & OI_SEL) == 0)
    339 			continue;
    340 		if (*oi->oi_path == '/') {
    341 			fprintf(fp, "\t%s \\\n", oi->oi_path);
    342 		} else {
    343 			if (oi->oi_prefix != NULL) {
    344 				fprintf(fp, "\t%s%s/%s \\\n",
    345 				    prefix_prologue(oi->oi_path),
    346 				    oi->oi_prefix, oi->oi_path);
    347 			} else {
    348 				fprintf(fp, "\t%s%s \\\n",
    349 				    filetype_prologue(&oi->oi_fit),
    350 				    oi->oi_path);
    351 			}
    352 		}
    353 	}
    354 	putc('\n', fp);
    355 }
    356 
    357 static void
    358 emitkobjs(FILE *fp)
    359 {
    360 	emitallkobjs(fp);
    361 	emitattrkobjs(fp);
    362 }
    363 
    364 static void
    365 emitallkobjs(FILE *fp)
    366 {
    367 
    368 	fputs("OBJS=", fp);
    369 	ht_enumerate(attrtab, emitallkobjscb, fp);
    370 	putc('\n', fp);
    371 }
    372 
    373 static int
    374 emitallkobjscb(const char *name, void *v, void *arg)
    375 {
    376 	struct attr *a = v;
    377 	FILE *fp = arg;
    378 
    379 	if (ht_lookup(selecttab, name) == NULL)
    380 		return 0;
    381 	if (TAILQ_EMPTY(&a->a_files))
    382 		return 0;
    383 	fprintf(fp, " %s.ko", name);
    384 	return 0;
    385 }
    386 
    387 static void
    388 emitattrkobjs(FILE *fp)
    389 {
    390 	extern struct	hashtab *attrtab;
    391 
    392 	ht_enumerate(attrtab, emitattrkobjscb, fp);
    393 }
    394 
    395 static int
    396 emitattrkobjscb(const char *name, void *v, void *arg)
    397 {
    398 	struct attr *a = v;
    399 	struct files *fi;
    400 	FILE *fp = arg;
    401 
    402 	if (ht_lookup(selecttab, name) == NULL)
    403 		return 0;
    404 	if (TAILQ_EMPTY(&a->a_files))
    405 		return 0;
    406 	fputc('\n', fp);
    407 	fprintf(fp, "OBJS.%s=", name);
    408 	TAILQ_FOREACH(fi, &a->a_files, fi_anext) {
    409 		fprintf(fp, " %s.o", fi->fi_base);
    410 	}
    411 	fputc('\n', fp);
    412 	fprintf(fp, "%s.ko: ${OBJS.%s}\n", name, name);
    413 	fprintf(fp, "\t${LINK_O}\n");
    414 	return 0;
    415 }
    416 
    417 static void
    418 emitcfiles(FILE *fp)
    419 {
    420 
    421 	emitfiles(fp, 'c', 0);
    422 }
    423 
    424 static void
    425 emitsfiles(FILE *fp)
    426 {
    427 
    428 	emitfiles(fp, 's', 'S');
    429 }
    430 
    431 static void
    432 emitfiles(FILE *fp, int suffix, int upper_suffix)
    433 {
    434 	struct files *fi;
    435 	int len;
    436 	const char *fpath;
    437  	struct config *cf;
    438  	char swapname[100];
    439 
    440 	fprintf(fp, "%cFILES= \\\n", toupper(suffix));
    441 	TAILQ_FOREACH(fi, &allfiles, fi_next) {
    442 		if ((fi->fi_flags & FI_SEL) == 0)
    443 			continue;
    444 		fpath = srcpath(fi);
    445 		len = strlen(fpath);
    446 		if (fpath[len - 1] != suffix && fpath[len - 1] != upper_suffix)
    447 			continue;
    448 		if (*fi->fi_path == '/') {
    449 			fprintf(fp, "\t%s \\\n", fpath);
    450 		} else {
    451 			if (fi->fi_prefix != NULL) {
    452 				fprintf(fp, "\t%s%s/%s \\\n",
    453 				    prefix_prologue(fi->fi_prefix),
    454 				    fi->fi_prefix, fpath);
    455 			} else {
    456 				fprintf(fp, "\t%s%s \\\n",
    457 				    filetype_prologue(&fi->fi_fit),
    458 				    fpath);
    459 			}
    460 		}
    461 	}
    462  	/*
    463  	 * The allfiles list does not include the configuration-specific
    464  	 * C source files.  These files should be eliminated someday, but
    465  	 * for now, we have to add them to ${CFILES} (and only ${CFILES}).
    466  	 */
    467  	if (suffix == 'c') {
    468  		TAILQ_FOREACH(cf, &allcf, cf_next) {
    469  			(void)snprintf(swapname, sizeof(swapname), "swap%s.c",
    470  			    cf->cf_name);
    471  			fprintf(fp, "\t%s \\\n", swapname);
    472  		}
    473  	}
    474 	putc('\n', fp);
    475 }
    476 
    477 /*
    478  * Emit the make-rules.
    479  */
    480 static void
    481 emitrules(FILE *fp)
    482 {
    483 	struct files *fi;
    484 	const char *cp, *fpath;
    485 	int ch;
    486 
    487 	TAILQ_FOREACH(fi, &allfiles, fi_next) {
    488 		if ((fi->fi_flags & FI_SEL) == 0)
    489 			continue;
    490 		fpath = srcpath(fi);
    491 		if (*fpath == '/') {
    492 			fprintf(fp, "%s.o: %s\n", fi->fi_base, fpath);
    493 		} else {
    494 			if (fi->fi_prefix != NULL) {
    495 				fprintf(fp, "%s.o: %s%s/%s\n", fi->fi_base,
    496 				    prefix_prologue(fi->fi_prefix),
    497 				    fi->fi_prefix, fpath);
    498 			} else {
    499 				fprintf(fp, "%s.o: %s%s\n",
    500 				    fi->fi_base,
    501 				    filetype_prologue(&fi->fi_fit),
    502 				    fpath);
    503  			}
    504 		}
    505 		if (fi->fi_mkrule != NULL) {
    506 			fprintf(fp, "\t%s\n\n", fi->fi_mkrule);
    507 		} else {
    508 			fputs("\t${NORMAL_", fp);
    509 			cp = strrchr(fpath, '.');
    510 			cp = cp == NULL ? fpath : cp + 1;
    511 			while ((ch = *cp++) != '\0') {
    512 				fputc(toupper(ch), fp);
    513 			}
    514 			fputs("}\n\n", fp);
    515 		}
    516 	}
    517 }
    518 
    519 /*
    520  * Emit the load commands.
    521  *
    522  * This function is not to be called `spurt'.
    523  */
    524 static void
    525 emitload(FILE *fp)
    526 {
    527 	struct config *cf;
    528 
    529 	fputs(".MAIN: all\nall:", fp);
    530 	TAILQ_FOREACH(cf, &allcf, cf_next) {
    531 		fprintf(fp, " %s", cf->cf_name);
    532 		/*
    533 		 * If we generate multiple configs inside the same build directory
    534 		 * with a parallel build, strange things may happen, so sequentialize
    535 		 * them.
    536 		 */
    537 		if (cf != TAILQ_LAST(&allcf,conftq))
    538 			fprintf(fp, " .WAIT");
    539 	}
    540 	fputs("\n\n", fp);
    541 	TAILQ_FOREACH(cf, &allcf, cf_next) {
    542 		fprintf(fp, "KERNELS+=%s\n", cf->cf_name);
    543 		fprintf(fp, "%s: ${SYSTEM_DEP} swap%s.o vers.o build_kernel\n",
    544 		    cf->cf_name, cf->cf_name);
    545 		fprintf(fp, "swap%s.o: swap%s.c\n"
    546 		    "\t${NORMAL_C}\n\n", cf->cf_name, cf->cf_name);
    547 	}
    548 	fputs("\n", fp);
    549 }
    550 
    551 /*
    552  * Emit include headers (for any prefixes encountered)
    553  */
    554 static void
    555 emitincludes(FILE *fp)
    556 {
    557 	struct prefix *pf;
    558 
    559 	SLIST_FOREACH(pf, &allprefixes, pf_next) {
    560 		fprintf(fp, "EXTRA_INCLUDES+=\t-I%s%s\n",
    561 		    prefix_prologue(pf->pf_prefix), pf->pf_prefix);
    562 	}
    563 }
    564 
    565 /*
    566  * Emit appending makeoptions.
    567  */
    568 static void
    569 emitappmkoptions(FILE *fp)
    570 {
    571 	struct nvlist *nv;
    572 	struct condexpr *cond;
    573 
    574 	for (nv = appmkoptions; nv != NULL; nv = nv->nv_next)
    575 		fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
    576 
    577 	for (nv = condmkoptions; nv != NULL; nv = nv->nv_next) {
    578 		cond = nv->nv_ptr;
    579 		if (expr_eval(cond, selectopt, NULL))
    580 			fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
    581 		condexpr_destroy(cond);
    582 		nv->nv_ptr = NULL;
    583 	}
    584 }
    585 
    586 static int
    587 /*ARGSUSED*/
    588 selectopt(const char *name, void *context)
    589 {
    590 
    591 	return (ht_lookup(selecttab, strtolower(name)) != NULL);
    592 }
    593