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