GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/config/mkmakefile.c Lines: 0 240 0.0 %
Date: 2017-11-13 Branches: 0 228 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: mkmakefile.c,v 1.45 2017/11/05 10:29:24 rpe Exp $	*/
2
/*	$NetBSD: mkmakefile.c,v 1.34 1997/02/02 21:12:36 thorpej Exp $	*/
3
4
/*
5
 * Copyright (c) 1992, 1993
6
 *	The Regents of the University of California.  All rights reserved.
7
 *
8
 * This software was developed by the Computer Systems Engineering group
9
 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10
 * contributed to Berkeley.
11
 *
12
 * All advertising materials mentioning features or use of this software
13
 * must display the following acknowledgement:
14
 *	This product includes software developed by the University of
15
 *	California, Lawrence Berkeley Laboratories.
16
 *
17
 * Redistribution and use in source and binary forms, with or without
18
 * modification, are permitted provided that the following conditions
19
 * are met:
20
 * 1. Redistributions of source code must retain the above copyright
21
 *    notice, this list of conditions and the following disclaimer.
22
 * 2. Redistributions in binary form must reproduce the above copyright
23
 *    notice, this list of conditions and the following disclaimer in the
24
 *    documentation and/or other materials provided with the distribution.
25
 * 3. Neither the name of the University nor the names of its contributors
26
 *    may be used to endorse or promote products derived from this software
27
 *    without specific prior written permission.
28
 *
29
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39
 * SUCH DAMAGE.
40
 *
41
 *	from: @(#)mkmakefile.c	8.1 (Berkeley) 6/6/93
42
 */
43
44
#include <ctype.h>
45
#include <err.h>
46
#include <errno.h>
47
#include <stdio.h>
48
#include <stdlib.h>
49
#include <string.h>
50
51
#include "config.h"
52
#include "sem.h"
53
54
/*
55
 * Make the Makefile.
56
 */
57
58
static const char *srcpath(struct files *);
59
60
static int emitdefs(FILE *);
61
static int emitreconfig(FILE *);
62
static int emitfiles(FILE *, int);
63
64
static int emitobjs(FILE *);
65
static int emitcfiles(FILE *);
66
static int emitsfiles(FILE *);
67
static int emitrules(FILE *);
68
static int emitload(FILE *);
69
70
int
71
mkmakefile(void)
72
{
73
	FILE *ifp, *ofp;
74
	int lineno;
75
	int (*fn)(FILE *);
76
	char *ifname;
77
	char line[BUFSIZ], buf[200];
78
79
	(void)snprintf(buf, sizeof buf, "arch/%s/conf/Makefile.%s",
80
	    machine, machine);
81
	ifname = sourcepath(buf);
82
	if ((ifp = fopen(ifname, "r")) == NULL) {
83
		warn("cannot read %s", ifname);
84
		free(ifname);
85
		return (1);
86
	}
87
	if ((ofp = fopen("Makefile", "w")) == NULL) {
88
		warn("cannot write Makefile");
89
		free(ifname);
90
		(void)fclose(ifp);
91
		return (1);
92
	}
93
	if (emitdefs(ofp) != 0)
94
		goto wrerror;
95
	lineno = 0;
96
	while (fgets(line, sizeof(line), ifp) != NULL) {
97
		lineno++;
98
		if (line[0] != '%') {
99
			if (fputs(line, ofp) < 0)
100
				goto wrerror;
101
			continue;
102
		}
103
		if (strcmp(line, "%OBJS\n") == 0)
104
			fn = emitobjs;
105
		else if (strcmp(line, "%CFILES\n") == 0)
106
			fn = emitcfiles;
107
		else if (strcmp(line, "%SFILES\n") == 0)
108
			fn = emitsfiles;
109
		else if (strcmp(line, "%RULES\n") == 0)
110
			fn = emitrules;
111
		else if (strcmp(line, "%LOAD\n") == 0)
112
			fn = emitload;
113
		else {
114
			xerror(ifname, lineno,
115
			    "unknown %% construct ignored: %s", line);
116
			continue;
117
		}
118
		if ((*fn)(ofp))
119
			goto wrerror;
120
	}
121
	if (startdir != NULL) {
122
		if (emitreconfig(ofp) != 0)
123
			goto wrerror;
124
	}
125
	if (ferror(ifp)) {
126
		warn("error reading %s (at line %d)", ifname, lineno);
127
		goto bad;
128
	}
129
	if (fclose(ofp)) {
130
		ofp = NULL;
131
		goto wrerror;
132
	}
133
	(void)fclose(ifp);
134
	free(ifname);
135
	return (0);
136
wrerror:
137
	warn("error writing Makefile");
138
bad:
139
	if (ofp != NULL)
140
		(void)fclose(ofp);
141
	/* (void)unlink("Makefile"); */
142
	free(ifname);
143
	return (1);
144
}
145
146
char *
147
expandname(const char *_nam)
148
{
149
	char *ret = NULL, *nam, *n, *s, *e, *expand;
150
	const char *var = NULL;
151
152
	if ((nam = n = strdup(_nam)) == NULL)
153
		errx(1, "out of memory");
154
155
	while (*n) {
156
		/* Search for a ${name} to expand */
157
		if ((s = strchr(n, '$')) == NULL) {
158
			if (ret == NULL)
159
				break;
160
			if (asprintf(&expand, "%s%s", ret, n) == -1)
161
				errx(1, "out of memory");
162
			free(ret);
163
			ret = expand;
164
			break;
165
		}
166
		*s++ = '\0';
167
		if (*s != '{')
168
			error("{");
169
		e = strchr(++s, '}');
170
		if (!e)
171
			error("}");
172
		*e = '\0';
173
174
		if (strcmp(s, "MACHINE_ARCH") == 0)
175
			var = machinearch ? machinearch : machine;
176
		else if (strcmp(s, "MACHINE") == 0)
177
			var = machine;
178
		else
179
			error("variable `%s' not supported", s);
180
181
		if (asprintf(&expand, "%s%s", ret ? ret : nam, var) == -1)
182
			errx(1, "out of memory");
183
		free(ret);
184
		ret = expand;
185
		n = e + 1;
186
	}
187
	free(nam);
188
	return (ret);
189
}
190
191
/*
192
 * Return (possibly in a static buffer) the name of the `source' for a
193
 * file.  If we have `options source', or if the file is marked `always
194
 * source', this is always the path from the `file' line; otherwise we
195
 * get the .o from the obj-directory.
196
 */
197
static const char *
198
srcpath(struct files *fi)
199
{
200
	/* Always have source, don't support object dirs for kernel builds. */
201
	struct nvlist *nv, *nv1;
202
	char *expand, *source;
203
204
	/* Search path list for files we will want to use */
205
	if (fi->fi_nvpath->nv_next == NULL) {
206
		nv = fi->fi_nvpath;
207
		goto onlyone;
208
	}
209
210
	for (nv = fi->fi_nvpath; nv; nv = nv->nv_next) {
211
		expand = expandname(nv->nv_name);
212
		source = sourcepath(expand ? expand : nv->nv_name);
213
		if (access(source, R_OK) == 0) {
214
			/* XXX poolalloc() prevents freeing old nv_name */
215
			if (expand)
216
				nv->nv_name = intern(expand);
217
			break;
218
		}
219
		free(expand);
220
		free(source);
221
	}
222
	if (nv == NULL)
223
		nv = fi->fi_nvpath;
224
225
	/*
226
	 * Now that we know which path is selected, delete all the
227
	 * other paths to skip the access() checks next time.
228
	 */
229
	while ((nv1 = fi->fi_nvpath)) {
230
		nv1 = nv1->nv_next;
231
		if (fi->fi_nvpath != nv)
232
			nvfree(fi->fi_nvpath);
233
		fi->fi_nvpath = nv1;
234
	}
235
	fi->fi_nvpath = nv;
236
	nv->nv_next = NULL;
237
onlyone:
238
	return (nv->nv_name);
239
}
240
241
static int
242
emitdefs(FILE *fp)
243
{
244
	struct nvlist *nv;
245
	char *sp;
246
247
	if (fputs("IDENT=", fp) < 0)
248
		return (1);
249
	sp = "";
250
	for (nv = options; nv != NULL; nv = nv->nv_next) {
251
		if (ht_lookup(defopttab, nv->nv_name) != NULL)
252
			continue;
253
		if (fprintf(fp, "%s-D%s", sp, nv->nv_name) < 0)
254
		    return 1;
255
		if (nv->nv_str)
256
		    if (fprintf(fp, "=\"%s\"", nv->nv_str) < 0)
257
			return 1;
258
		sp = " ";
259
	}
260
	if (putc('\n', fp) < 0)
261
		return (1);
262
	if (fprintf(fp, "PARAM=-DMAXUSERS=%d\n", maxusers) < 0)
263
		return (1);
264
	if (fprintf(fp, "S=\t%s\n", srcdir) < 0)
265
		return (1);
266
	if (fprintf(fp, "_mach=%s\n", machine) < 0)
267
		return (1);
268
	if (fprintf(fp, "_arch=%s\n", machinearch ? machinearch : machine) < 0)
269
		return (1);
270
	for (nv = mkoptions; nv != NULL; nv = nv->nv_next)
271
		if (fprintf(fp, "%s=%s\n", nv->nv_name, nv->nv_str) < 0)
272
			return (1);
273
	return (0);
274
}
275
276
static int
277
emitreconfig(FILE *fp)
278
{
279
	if (fputs("\n"
280
	    ".PHONY: config\n"
281
	    "config:\n", fp) < 0)
282
		return (1);
283
	if (fprintf(fp, "\tcd %s && config ", startdir) < 0)
284
		return (1);
285
	if (pflag) {
286
		if (fputs("-p ", fp) < 0)
287
			return (1);
288
	}
289
	if (sflag) {
290
		if (fprintf(fp, "-s %s ", sflag) < 0)
291
			return (1);
292
	}
293
	if (bflag) {
294
		if (fprintf(fp, "-b %s ", bflag) < 0)
295
			return (1);
296
	}
297
	/* other options */
298
	if (fprintf(fp, "%s\n", conffile) < 0)
299
		return (1);
300
	return (0);
301
}
302
303
static int
304
emitobjs(FILE *fp)
305
{
306
	struct files *fi;
307
	struct objects *oi;
308
	int lpos, len, sp;
309
	const char *fpath;
310
311
	if (fputs("OBJS=", fp) < 0)
312
		return (1);
313
	sp = '\t';
314
	lpos = 7;
315
	for (fi = allfiles; fi != NULL; fi = fi->fi_next) {
316
		if ((fi->fi_flags & FI_SEL) == 0)
317
			continue;
318
		if ((fpath = srcpath(fi)) == NULL)
319
			return (1);
320
		len = strlen(fi->fi_base) + 3;
321
		if (lpos + len > 72) {
322
			if (fputs(" \\\n", fp) < 0)
323
				return (1);
324
			sp = '\t';
325
			lpos = 7;
326
		}
327
		if (fprintf(fp, "%c%s.o", sp, fi->fi_base) < 0)
328
			return (1);
329
		lpos += len + 1;
330
		sp = ' ';
331
	}
332
	for (oi = allobjects; oi != NULL; oi = oi->oi_next) {
333
		if ((oi->oi_flags & OI_SEL) == 0)
334
			continue;
335
		len = strlen(oi->oi_path) + 3;
336
		if (lpos + len > 72) {
337
			if (fputs(" \\\n", fp) < 0)
338
				return (1);
339
			sp = '\t';
340
			lpos = 7;
341
		}
342
		if (fprintf(fp, "%c$S/%s", sp, oi->oi_path) < 0)
343
			return (1);
344
		lpos += len + 1;
345
		sp = ' ';
346
	}
347
	if (putc('\n', fp) < 0)
348
		return (1);
349
	return (0);
350
}
351
352
static int
353
emitcfiles(FILE *fp)
354
{
355
356
	return (emitfiles(fp, 'c'));
357
}
358
359
static int
360
emitsfiles(FILE *fp)
361
{
362
363
	return (emitfiles(fp, 's'));
364
}
365
366
static int
367
emitfiles(FILE *fp, int suffix)
368
{
369
	struct files *fi;
370
	int lpos, len, sp;
371
	const char *fpath;
372
	char uppersuffix = toupper((unsigned char)suffix);
373
374
	if (fprintf(fp, "%cFILES=", uppersuffix) < 0)
375
		return (1);
376
	sp = '\t';
377
	lpos = 7;
378
	for (fi = allfiles; fi != NULL; fi = fi->fi_next) {
379
		if ((fi->fi_flags & FI_SEL) == 0)
380
			continue;
381
		if ((fpath = srcpath(fi)) == NULL)
382
			return (1);
383
		len = strlen(fpath);
384
		if (fpath[len - 1] != suffix && fpath[len - 1] != uppersuffix)
385
			continue;
386
		if (*fpath != '/')
387
			len += 3;	/* "$S/" */
388
		if (lpos + len > 72) {
389
			if (fputs(" \\\n", fp) < 0)
390
				return (1);
391
			sp = '\t';
392
			lpos = 7;
393
		}
394
		if (fprintf(fp, "%c%s%s", sp, *fpath != '/' ? "$S/" : "",
395
		    fpath) < 0)
396
			return (1);
397
		lpos += len + 1;
398
		sp = ' ';
399
	}
400
	if (putc('\n', fp) < 0)
401
		return (1);
402
	return (0);
403
}
404
405
/*
406
 * Emit the make-rules.
407
 */
408
static int
409
emit_1rule(FILE *fp, struct files *fi, const char *fpath, const char *suffix)
410
{
411
	if (fprintf(fp, "%s%s: %s%s\n", fi->fi_base, suffix,
412
	    *fpath != '/' ? "$S/" : "", fpath) < 0)
413
		return (1);
414
	if (fi->fi_mkrule != NULL) {
415
		if (fprintf(fp, "\t%s\n\n", fi->fi_mkrule) < 0)
416
			return (1);
417
	}
418
	return (0);
419
}
420
421
static int
422
emitrules(FILE *fp)
423
{
424
	struct files *fi;
425
	const char *fpath;
426
427
	/* write suffixes */
428
	if (fprintf(fp,
429
	    ".SUFFIXES:\n"
430
	    ".SUFFIXES: .s .S .c .o\n\n"
431
432
	    ".PHONY: depend all install clean tags newbsd update-link\n\n"
433
434
	    ".c.o:\n"
435
	    "\t${NORMAL_C}\n\n"
436
437
	    ".s.o:\n"
438
	    "\t${NORMAL_S}\n\n"
439
440
	    ".S.o:\n"
441
	    "\t${NORMAL_S}\n\n") < 0)
442
		return (1);
443
444
445
	for (fi = allfiles; fi != NULL; fi = fi->fi_next) {
446
		if ((fi->fi_flags & FI_SEL) == 0)
447
			continue;
448
		if ((fpath = srcpath(fi)) == NULL)
449
			return (1);
450
		/* special rule: need to emit them independently */
451
		if (fi->fi_mkrule) {
452
			if (emit_1rule(fp, fi, fpath, ".o"))
453
				return (1);
454
		/* simple default rule */
455
		} else {
456
			if (fprintf(fp, "%s.o: %s%s\n", fi->fi_base,
457
			    *fpath != '/' ? "$S/" : "", fpath) < 0)
458
				return (1);
459
		}
460
461
	}
462
	return (0);
463
}
464
465
/*
466
 * Emit the load commands.
467
 *
468
 * This function is not to be called `spurt'.
469
 */
470
static int
471
emitload(FILE *fp)
472
{
473
	struct config *cf;
474
	const char *nm, *swname;
475
	int first;
476
477
	if (fputs("all:", fp) < 0)
478
		return (1);
479
	for (cf = allcf; cf != NULL; cf = cf->cf_next) {
480
		if (fprintf(fp, " %s", cf->cf_name) < 0)
481
			return (1);
482
	}
483
	if (fputs("\n\n", fp) < 0)
484
		return (1);
485
	for (first = 1, cf = allcf; cf != NULL; cf = cf->cf_next) {
486
		nm = cf->cf_name;
487
		swname =
488
		    cf->cf_root != NULL ? cf->cf_name : "generic";
489
		if (fprintf(fp, "%s: ${SYSTEM_DEP} swap%s.o", nm, swname) < 0)
490
			return (1);
491
		if (first) {
492
			if (fputs(" vers.o", fp) < 0)
493
				return (1);
494
			first = 0;
495
		}
496
		if (fprintf(fp, "\n"
497
		    "\t${SYSTEM_LD_HEAD}\n"
498
		    "\t${SYSTEM_LD} swap%s.o\n"
499
		    "\t${SYSTEM_LD_TAIL}\n"
500
		    "\n"
501
		    "swap%s.o: ", swname, swname) < 0)
502
			return (1);
503
		if (cf->cf_root != NULL) {
504
			if (fprintf(fp, "swap%s.c\n", nm) < 0)
505
				return (1);
506
		} else {
507
			if (fprintf(fp, "$S/conf/swapgeneric.c\n") < 0)
508
				return (1);
509
		}
510
		if (fputs("\t${NORMAL_C}\n\n", fp) < 0)
511
			return (1);
512
513
		if (fprintf(fp, "new%s:\n", nm) < 0)
514
			return (1);
515
		if (fprintf(fp,
516
		    "\t${MAKE_GAP}\n"
517
		    "\t${SYSTEM_LD_HEAD}\n"
518
		    "\t${SYSTEM_LD} swap%s.o\n"
519
		    "\t${SYSTEM_LD_TAIL}\n"
520
		    "\tmv -f new%s %s\n\n",
521
		    swname, nm, nm) < 0)
522
			return (1);
523
524
		if (fprintf(fp, "update-link:\n") < 0)
525
			return (1);
526
		if (fprintf(fp,
527
		    "\tmkdir -p -m 700 /usr/share/relink/kernel\n"
528
		    "\trm -rf /usr/share/relink/kernel/%s /usr/share/relink/kernel.tgz\n"
529
		    "\tmkdir /usr/share/relink/kernel/%s\n"
530
		    "\ttar -chf - Makefile makegap.sh ld.script *.o | \\\n"
531
		    "\t    tar -C /usr/share/relink/kernel/%s -xf -\n\n",
532
		    last_component, last_component, last_component) < 0)
533
			return (1);
534
	}
535
	return (0);
536
}