Home | History | Annotate | Line # | Download | only in config
mkmakefile.c revision 1.20
      1 /*	$NetBSD: mkmakefile.c,v 1.20 2014/10/09 17:36:10 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;	/* XXX */
     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 		/* Try a makefile for the architecture second.
    104 		 */
    105 		(void)snprintf(buf, sizeof(buf), "arch/%s/conf/Makefile.%s",
    106 		    machinearch, machinearch);
    107 		free(ifname);
    108 		ifname = sourcepath(buf);
    109 		ifp = fopen(ifname, "r");
    110 	}
    111 	if (ifp == NULL) {
    112 		warn("cannot read %s", ifname);
    113 		goto bad2;
    114 	}
    115 
    116 	if ((ofp = fopen("Makefile.tmp", "w")) == NULL) {
    117 		warn("cannot write Makefile");
    118 		goto bad1;
    119 	}
    120 
    121 	emitdefs(ofp);
    122 
    123 	lineno = 0;
    124 	while (fgets(line, sizeof(line), ifp) != NULL) {
    125 		lineno++;
    126 		if ((version < 20090214 && line[0] != '%') || line[0] == '#') {
    127 			fputs(line, ofp);
    128 			continue;
    129 		}
    130 		if (strcmp(line, "%OBJS\n") == 0)
    131 			fn = usekobjs ? emitkobjs : emitobjs;
    132 		else if (strcmp(line, "%CFILES\n") == 0)
    133 			fn = emitcfiles;
    134 		else if (strcmp(line, "%SFILES\n") == 0)
    135 			fn = emitsfiles;
    136 		else if (strcmp(line, "%RULES\n") == 0)
    137 			fn = emitrules;
    138 		else if (strcmp(line, "%LOAD\n") == 0)
    139 			fn = emitload;
    140 		else if (strcmp(line, "%INCLUDES\n") == 0)
    141 			fn = emitincludes;
    142 		else if (strcmp(line, "%MAKEOPTIONSAPPEND\n") == 0)
    143 			fn = emitappmkoptions;
    144 		else if (strncmp(line, "%VERSION ", sizeof("%VERSION ")-1) == 0) {
    145 			int newvers;
    146 			if (sscanf(line, "%%VERSION %d\n", &newvers) != 1) {
    147 				cfgxerror(ifname, lineno, "syntax error for "
    148 				    "%%VERSION");
    149 			} else
    150 				setversion(newvers);
    151 			continue;
    152 		} else {
    153 			if (version < 20090214)
    154 				cfgxerror(ifname, lineno,
    155 				    "unknown %% construct ignored: %s", line);
    156 			else
    157 				emitsubs(ofp, line, ifname, lineno);
    158 			continue;
    159 		}
    160 		(*fn)(ofp);
    161 	}
    162 
    163 	fflush(ofp);
    164 	if (ferror(ofp))
    165 		goto wrerror;
    166 
    167 	if (ferror(ifp)) {
    168 		warn("error reading %s (at line %d)", ifname, lineno);
    169 		goto bad;
    170 	}
    171 
    172 	if (fclose(ofp)) {
    173 		ofp = NULL;
    174 		goto wrerror;
    175 	}
    176 	(void)fclose(ifp);
    177 
    178 	if (moveifchanged("Makefile.tmp", "Makefile") != 0) {
    179 		warn("error renaming Makefile");
    180 		goto bad2;
    181 	}
    182 	free(ifname);
    183 	return (0);
    184 
    185  wrerror:
    186 	warn("error writing Makefile");
    187  bad:
    188 	if (ofp != NULL)
    189 		(void)fclose(ofp);
    190  bad1:
    191 	(void)fclose(ifp);
    192 	/* (void)unlink("Makefile.tmp"); */
    193  bad2:
    194 	free(ifname);
    195 	return (1);
    196 }
    197 
    198 static void
    199 emitsubs(FILE *fp, const char *line, const char *file, int lineno)
    200 {
    201 	char *nextpct;
    202 	const char *optname;
    203 	struct nvlist *option;
    204 
    205 	while (*line != '\0') {
    206 		if (*line != '%') {
    207 			fputc(*line++, fp);
    208 			continue;
    209 		}
    210 
    211 		line++;
    212 		nextpct = strchr(line, '%');
    213 		if (nextpct == NULL) {
    214 			cfgxerror(file, lineno, "unbalanced %% or "
    215 			    "unknown construct");
    216 			return;
    217 		}
    218 		*nextpct = '\0';
    219 
    220 		if (*line == '\0')
    221 			fputc('%', fp);
    222 		else {
    223 			optname = intern(line);
    224 			if (!DEFINED_OPTION(optname)) {
    225 				cfgxerror(file, lineno, "unknown option %s",
    226 				    optname);
    227 				return;
    228 			}
    229 
    230 			if ((option = ht_lookup(opttab, optname)) == NULL)
    231 				option = ht_lookup(fsopttab, optname);
    232 			if (option != NULL)
    233 				fputs(option->nv_str ? option->nv_str : "1",
    234 				    fp);
    235 			/* Otherwise it's not a selected option and we don't
    236 			 * output anything. */
    237 		}
    238 
    239 		line = nextpct+1;
    240 	}
    241 }
    242 
    243 /*
    244  * Return (possibly in a static buffer) the name of the `source' for a
    245  * file.  If we have `options source', or if the file is marked `always
    246  * source', this is always the path from the `file' line; otherwise we
    247  * get the .o from the obj-directory.
    248  */
    249 static const char *
    250 srcpath(struct files *fi)
    251 {
    252 #if 1
    253 	/* Always have source, don't support object dirs for kernel builds. */
    254 	return (fi->fi_path);
    255 #else
    256 	static char buf[MAXPATHLEN];
    257 
    258 	if (have_source || (fi->fi_flags & FI_ALWAYSSRC) != 0)
    259 		return (fi->fi_path);
    260 	if (objpath == NULL) {
    261 		cfgerror("obj-directory not set");
    262 		return (NULL);
    263 	}
    264 	(void)snprintf(buf, sizeof buf, "%s/%s.o", objpath, fi->fi_base);
    265 	return (buf);
    266 #endif
    267 }
    268 
    269 static const char *
    270 filetype_prologue(struct filetype *fit)
    271 {
    272 	if (fit->fit_flags & FIT_NOPROLOGUE || *fit->fit_path == '/')
    273 		return ("");
    274 	else
    275 		return ("$S/");
    276 }
    277 
    278 static const char *
    279 prefix_prologue(const char *path)
    280 {
    281 	if (*path == '/')
    282 		return ("");
    283 	else
    284 		return ("$S/");
    285 }
    286 
    287 static void
    288 emitdefs(FILE *fp)
    289 {
    290 	struct nvlist *nv;
    291 	const char *sp;
    292 
    293 	fprintf(fp, "KERNEL_BUILD=%s\n", conffile);
    294 	fputs("IDENT=", fp);
    295 	sp = "";
    296 	for (nv = options; nv != NULL; nv = nv->nv_next) {
    297 
    298 		/* skip any options output to a header file */
    299 		if (DEFINED_OPTION(nv->nv_name))
    300 			continue;
    301 		fprintf(fp, "%s-D%s", sp, nv->nv_name);
    302 		if (nv->nv_str)
    303 		    fprintf(fp, "=\"%s\"", nv->nv_str);
    304 		sp = " ";
    305 	}
    306 	putc('\n', fp);
    307 	fprintf(fp, "PARAM=-DMAXUSERS=%d\n", maxusers);
    308 	fprintf(fp, "MACHINE=%s\n", machine);
    309 	if (*srcdir == '/' || *srcdir == '.') {
    310 		fprintf(fp, "S=\t%s\n", srcdir);
    311 	} else {
    312 		/*
    313 		 * libkern and libcompat "Makefile.inc"s want relative S
    314 		 * specification to begin with '.'.
    315 		 */
    316 		fprintf(fp, "S=\t./%s\n", srcdir);
    317 	}
    318 	for (nv = mkoptions; nv != NULL; nv = nv->nv_next)
    319 		fprintf(fp, "%s=%s\n", nv->nv_name, nv->nv_str);
    320 }
    321 
    322 static void
    323 emitobjs(FILE *fp)
    324 {
    325 	struct files *fi;
    326 	struct objects *oi;
    327 
    328 	fputs("OBJS= \\\n", fp);
    329 	TAILQ_FOREACH(fi, &allfiles, fi_next) {
    330 		if ((fi->fi_flags & FI_SEL) == 0)
    331 			continue;
    332 		fprintf(fp, "\t%s.o \\\n", fi->fi_base);
    333 	}
    334 	TAILQ_FOREACH(oi, &allobjects, oi_next) {
    335 		if ((oi->oi_flags & OI_SEL) == 0)
    336 			continue;
    337 		if (*oi->oi_path == '/') {
    338 			fprintf(fp, "\t%s \\\n", oi->oi_path);
    339 		} else {
    340 			if (oi->oi_prefix != NULL) {
    341 				fprintf(fp, "\t%s%s/%s \\\n",
    342 					    prefix_prologue(oi->oi_path),
    343 					    oi->oi_prefix, oi->oi_path);
    344 			} else {
    345 				fprintf(fp, "\t%s%s \\\n",
    346 				            filetype_prologue(&oi->oi_fit),
    347 				            oi->oi_path);
    348 			}
    349 		}
    350 	}
    351 	putc('\n', fp);
    352 }
    353 
    354 static void
    355 emitkobjs(FILE *fp)
    356 {
    357 	emitallkobjs(fp);
    358 	emitattrkobjs(fp);
    359 }
    360 
    361 static void
    362 emitallkobjs(FILE *fp)
    363 {
    364 
    365 	fputs("OBJS=", fp);
    366 	ht_enumerate(attrtab, emitallkobjscb, fp);
    367 	putc('\n', fp);
    368 }
    369 
    370 static int
    371 emitallkobjscb(const char *name, void *v, void *arg)
    372 {
    373 	struct attr *a = v;
    374 	FILE *fp = arg;
    375 
    376 	if (ht_lookup(selecttab, name) == NULL)
    377 		return 0;
    378 	if (TAILQ_EMPTY(&a->a_files))
    379 		return 0;
    380 	fprintf(fp, " %s.ko", name);
    381 	return 0;
    382 }
    383 
    384 static void
    385 emitattrkobjs(FILE *fp)
    386 {
    387 	extern struct	hashtab *attrtab;
    388 
    389 	ht_enumerate(attrtab, emitattrkobjscb, fp);
    390 }
    391 
    392 static int
    393 emitattrkobjscb(const char *name, void *v, void *arg)
    394 {
    395 	struct attr *a = v;
    396 	struct files *fi;
    397 	FILE *fp = arg;
    398 
    399 	if (ht_lookup(selecttab, name) == NULL)
    400 		return 0;
    401 	if (TAILQ_EMPTY(&a->a_files))
    402 		return 0;
    403 	fputc('\n', fp);
    404 	fprintf(fp, "OBJS.%s=", name);
    405 	TAILQ_FOREACH(fi, &a->a_files, fi_anext) {
    406 		fprintf(fp, " %s.o", fi->fi_base);
    407 	}
    408 	fputc('\n', fp);
    409 	fprintf(fp, "%s.ko: ${OBJS.%s}\n", name, name);
    410 	fprintf(fp, "\t${LINK_O}\n");
    411 	return 0;
    412 }
    413 
    414 static void
    415 emitcfiles(FILE *fp)
    416 {
    417 
    418 	emitfiles(fp, 'c', 0);
    419 }
    420 
    421 static void
    422 emitsfiles(FILE *fp)
    423 {
    424 
    425 	emitfiles(fp, 's', 'S');
    426 }
    427 
    428 static void
    429 emitfiles(FILE *fp, int suffix, int upper_suffix)
    430 {
    431 	struct files *fi;
    432 	int len;
    433 	const char *fpath;
    434  	struct config *cf;
    435  	char swapname[100];
    436 
    437 	fprintf(fp, "%cFILES= \\\n", toupper(suffix));
    438 	TAILQ_FOREACH(fi, &allfiles, fi_next) {
    439 		if ((fi->fi_flags & FI_SEL) == 0)
    440 			continue;
    441 		fpath = srcpath(fi);
    442 		len = strlen(fpath);
    443 		if (fpath[len - 1] != suffix && fpath[len - 1] != upper_suffix)
    444 			continue;
    445 		if (*fpath != '/') {
    446 			/* "$S/" */
    447  			if (fi->fi_prefix != NULL)
    448 				len += strlen(prefix_prologue(fi->fi_prefix)) +
    449 				       strlen(fi->fi_prefix) + 1;
    450 			else
    451 				len += strlen(filetype_prologue(&fi->fi_fit));
    452 		}
    453 		if (*fi->fi_path == '/') {
    454 			fprintf(fp, "\t%s \\\n", fpath);
    455 		} else {
    456 			if (fi->fi_prefix != NULL) {
    457 				fprintf(fp, "\t%s%s/%s \\\n",
    458 					    prefix_prologue(fi->fi_prefix),
    459 					    fi->fi_prefix, fpath);
    460 			} else {
    461 				fprintf(fp, "\t%s%s \\\n",
    462 				            filetype_prologue(&fi->fi_fit),
    463 				            fpath);
    464 			}
    465 		}
    466 	}
    467  	/*
    468  	 * The allfiles list does not include the configuration-specific
    469  	 * C source files.  These files should be eliminated someday, but
    470  	 * for now, we have to add them to ${CFILES} (and only ${CFILES}).
    471  	 */
    472  	if (suffix == 'c') {
    473  		TAILQ_FOREACH(cf, &allcf, cf_next) {
    474  			(void)snprintf(swapname, sizeof(swapname), "swap%s.c",
    475  			    cf->cf_name);
    476  			fprintf(fp, "\t%s \\\n", swapname);
    477  		}
    478  	}
    479 	putc('\n', fp);
    480 }
    481 
    482 /*
    483  * Emit the make-rules.
    484  */
    485 static void
    486 emitrules(FILE *fp)
    487 {
    488 	struct files *fi;
    489 	const char *cp, *fpath;
    490 	int ch;
    491 
    492 	TAILQ_FOREACH(fi, &allfiles, fi_next) {
    493 		if ((fi->fi_flags & FI_SEL) == 0)
    494 			continue;
    495 		fpath = srcpath(fi);
    496 		if (*fpath == '/') {
    497 			fprintf(fp, "%s.o: %s\n", fi->fi_base, fpath);
    498 		} else {
    499 			if (fi->fi_prefix != NULL) {
    500 				fprintf(fp, "%s.o: %s%s/%s\n", fi->fi_base,
    501 					    prefix_prologue(fi->fi_prefix),
    502 					    fi->fi_prefix, fpath);
    503 			} else {
    504 				fprintf(fp, "%s.o: %s%s\n",
    505 				            fi->fi_base,
    506 				            filetype_prologue(&fi->fi_fit),
    507 				            fpath);
    508  			}
    509 		}
    510 		if (fi->fi_mkrule != NULL) {
    511 			fprintf(fp, "\t%s\n\n", fi->fi_mkrule);
    512 		} else {
    513 			fputs("\t${NORMAL_", fp);
    514 			cp = strrchr(fpath, '.');
    515 			cp = cp == NULL ? fpath : cp + 1;
    516 			while ((ch = *cp++) != '\0') {
    517 				fputc(toupper(ch), fp);
    518 			}
    519 			fputs("}\n\n", fp);
    520 		}
    521 	}
    522 }
    523 
    524 /*
    525  * Emit the load commands.
    526  *
    527  * This function is not to be called `spurt'.
    528  */
    529 static void
    530 emitload(FILE *fp)
    531 {
    532 	struct config *cf;
    533 
    534 	fputs(".MAIN: all\nall:", fp);
    535 	TAILQ_FOREACH(cf, &allcf, cf_next) {
    536 		fprintf(fp, " %s", cf->cf_name);
    537 		/*
    538 		 * If we generate multiple configs inside the same build directory
    539 		 * with a parallel build, strange things may happen, so sequentialize
    540 		 * them.
    541 		 */
    542 		if (cf != TAILQ_LAST(&allcf,conftq))
    543 			fprintf(fp, " .WAIT");
    544 	}
    545 	fputs("\n\n", fp);
    546 	TAILQ_FOREACH(cf, &allcf, cf_next) {
    547 		fprintf(fp, "KERNELS+=%s\n", cf->cf_name);
    548 		fprintf(fp, "%s: ${SYSTEM_DEP} swap%s.o vers.o build_kernel\n",
    549 			cf->cf_name, cf->cf_name);
    550 		fprintf(fp, "swap%s.o: swap%s.c\n"
    551 			"\t${NORMAL_C}\n\n", cf->cf_name, cf->cf_name);
    552 	}
    553 	fputs("\n", fp);
    554 }
    555 
    556 /*
    557  * Emit include headers (for any prefixes encountered)
    558  */
    559 static void
    560 emitincludes(FILE *fp)
    561 {
    562 	struct prefix *pf;
    563 
    564 	SLIST_FOREACH(pf, &allprefixes, pf_next) {
    565 		fprintf(fp, "EXTRA_INCLUDES+=\t-I%s%s\n",
    566 		    prefix_prologue(pf->pf_prefix), pf->pf_prefix);
    567 	}
    568 }
    569 
    570 /*
    571  * Emit appending makeoptions.
    572  */
    573 static void
    574 emitappmkoptions(FILE *fp)
    575 {
    576 	struct nvlist *nv;
    577 	struct condexpr *cond;
    578 
    579 	for (nv = appmkoptions; nv != NULL; nv = nv->nv_next)
    580 		fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
    581 
    582 	for (nv = condmkoptions; nv != NULL; nv = nv->nv_next) {
    583 		cond = nv->nv_ptr;
    584 		if (expr_eval(cond, selectopt, NULL))
    585 			fprintf(fp, "%s+=%s\n", nv->nv_name, nv->nv_str);
    586 		condexpr_destroy(cond);
    587 		nv->nv_ptr = NULL;
    588 	}
    589 }
    590 
    591 static int
    592 /*ARGSUSED*/
    593 selectopt(const char *name, void *context)
    594 {
    595 
    596 	return (ht_lookup(selecttab, strtolower(name)) != NULL);
    597 }
    598