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