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