xref: /xnu-12377.41.6/SETUP/config/mkmakefile.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1 /*
2  * Copyright (c) 1999-2016 Apple Inc. All rights reserved.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  *
6  * "Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
7  * Reserved.  This file contains Original Code and/or Modifications of
8  * Original Code as defined in and that are subject to the Apple Public
9  * Source License Version 1.0 (the 'License').  You may not use this file
10  * except in compliance with the License.  Please obtain a copy of the
11  * License at http://www.apple.com/publicsource and read it before using
12  * this file.
13  *
14  * The Original Code and all software distributed under the License are
15  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
19  * License for the specific language governing rights and limitations
20  * under the License."
21  *
22  * @APPLE_LICENSE_HEADER_END@
23  */
24 /*
25  * Mach Operating System
26  * Copyright (c) 1990 Carnegie-Mellon University
27  * Copyright (c) 1989 Carnegie-Mellon University
28  * Copyright (c) 1988 Carnegie-Mellon University
29  * Copyright (c) 1987 Carnegie-Mellon University
30  * All rights reserved.  The CMU software License Agreement specifies
31  * the terms and conditions for use and redistribution.
32  */
33 
34 /*
35  * Copyright (c) 1980 Regents of the University of California.
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms are permitted
39  * provided that the above copyright notice and this paragraph are
40  * duplicated in all such forms and that any documentation,
41  * advertising materials, and other materials related to such
42  * distribution and use acknowledge that the software was developed
43  * by the University of California, Berkeley.  The name of the
44  * University may not be used to endorse or promote products derived
45  * from this software without specific prior written permission.
46  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
47  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
48  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
49  */
50 
51 #ifndef lint
52 static char sccsid[] __attribute__((used)) = "@(#)mkmakefile.c	5.21 (Berkeley) 6/18/88";
53 #endif /* not lint */
54 
55 /*
56  * Build the makefile for the system, from
57  * the information in the files files and the
58  * additional files for the machine being compiled to.
59  */
60 
61 #include <stdio.h>
62 #include <unistd.h>     /* for unlink */
63 #include <ctype.h>
64 #include "parser.h"
65 #include "config.h"
66 
67 void    read_files(void);
68 void    do_objs(FILE *fp, const char *msg, int ext, int flags);
69 void    do_files(FILE *fp, const char *msg, char ext);
70 void    do_machdep(FILE *ofp);
71 void    do_rules(FILE *f);
72 void    copy_dependencies(FILE *makin, FILE *makout);
73 
74 struct file_list *fl_lookup(char *file);
75 struct file_list *fltail_lookup(char *file);
76 struct file_list *new_fent(void);
77 
78 void    put_source_file_name(FILE *fp, struct file_list *tp);
79 
80 
81 #define next_word(fp, wd) \
82 	{ const char *word = get_word(fp); \
83 	  if (word == (char *)EOF) \
84 	        return; \
85 	  else \
86 	        wd = word; \
87 	}
88 
89 static  struct file_list *fcur;
90 const char *tail(const char *fn);
91 char *allCaps(char *str);
92 
93 /*
94  * Lookup a file, by name.
95  */
96 struct file_list *
fl_lookup(char * file)97 fl_lookup(char *file)
98 {
99 	struct file_list *fp;
100 
101 	for (fp = ftab; fp != 0; fp = fp->f_next) {
102 		if (eq(fp->f_fn, file)) {
103 			return fp;
104 		}
105 	}
106 	return 0;
107 }
108 
109 /*
110  * Lookup a file, by final component name.
111  */
112 struct file_list *
fltail_lookup(char * file)113 fltail_lookup(char *file)
114 {
115 	struct file_list *fp;
116 
117 	for (fp = ftab; fp != 0; fp = fp->f_next) {
118 		if (eq(tail(fp->f_fn), tail(file))) {
119 			return fp;
120 		}
121 	}
122 	return 0;
123 }
124 
125 /*
126  * Make a new file list entry
127  */
128 struct file_list *
new_fent(void)129 new_fent(void)
130 {
131 	struct file_list *fp;
132 
133 	fp = (struct file_list *) malloc(sizeof *fp);
134 	fp->f_needs = 0;
135 	fp->f_next = 0;
136 	fp->f_flags = 0;
137 	fp->f_type = 0;
138 	fp->f_extra = (char *) 0;
139 	if (fcur == 0) {
140 		fcur = ftab = fp;
141 	} else {
142 		fcur->f_next = fp;
143 	}
144 	fcur = fp;
145 	return fp;
146 }
147 
148 char    *COPTS;
149 
150 const char *
get_VPATH(void)151 get_VPATH(void)
152 {
153 	static char *vpath = NULL;
154 
155 	if ((vpath == NULL) &&
156 	    ((vpath = getenv("VPATH")) != NULL) &&
157 	    (*vpath != ':')) {
158 		char *buf = malloc((unsigned)(strlen(vpath) + 2));
159 
160 		vpath = strcat(strcpy(buf, ":"), vpath);
161 	}
162 
163 	return vpath ? vpath : "";
164 }
165 
166 
167 /*
168  * Build the makefile from the skeleton
169  */
170 void
makefile(void)171 makefile(void)
172 {
173 	FILE *ifp, *ofp;
174 	FILE *dfp;
175 	char pname[BUFSIZ];
176 	char line[BUFSIZ];
177 	struct opt *op;
178 
179 	read_files();
180 	(void) sprintf(line, "%s/Makefile.template", config_directory);
181 	ifp = fopenp(VPATH, line, pname, "r");
182 	if (ifp == 0) {
183 		perror(line);
184 		exit(1);
185 	}
186 	dfp = fopen(path("Makefile"), "r");
187 	rename(path("Makefile"), path("Makefile.old"));
188 	unlink(path("Makefile.old"));
189 	ofp = fopen(path("Makefile"), "w");
190 	if (ofp == 0) {
191 		perror(path("Makefile"));
192 		exit(1);
193 	}
194 	fprintf(ofp, "SOURCE_DIR=%s\n", source_directory);
195 
196 	fprintf(ofp, "export CONFIG_DEFINES =");
197 	if (profiling) {
198 		fprintf(ofp, " -DGPROF");
199 	}
200 
201 	for (op = opt; op; op = op->op_next) {
202 		if (op->op_value) {
203 			fprintf(ofp, " -D%s=\"%s\"", op->op_name, op->op_value);
204 		} else {
205 			fprintf(ofp, " -D%s", op->op_name);
206 		}
207 	}
208 	fprintf(ofp, "\n");
209 	for (op = mkopt; op; op = op->op_next) {
210 		if (op->op_value) {
211 			fprintf(ofp, "%s=%s\n", op->op_name, op->op_value);
212 		} else {
213 			fprintf(ofp, "%s\n", op->op_name);
214 		}
215 	}
216 
217 	while (fgets(line, BUFSIZ, ifp) != 0) {
218 		if (*line == '%') {
219 			goto percent;
220 		}
221 		if (profiling && strncmp(line, "COPTS=", 6) == 0) {
222 			char *cp;
223 			fprintf(ofp,
224 			    "GPROF.EX=$(SOURCE_DIR)/machdep/%s/gmon.ex\n", machinename);
225 			cp = index(line, '\n');
226 			if (cp) {
227 				*cp = 0;
228 			}
229 			cp = line + 6;
230 			while (*cp && (*cp == ' ' || *cp == '\t')) {
231 				cp++;
232 			}
233 			COPTS = malloc((unsigned)(strlen(cp) + 1));
234 			if (COPTS == 0) {
235 				printf("config: out of memory\n");
236 				exit(1);
237 			}
238 			strcpy(COPTS, cp);
239 			fprintf(ofp, "%s -pg\n", line);
240 			continue;
241 		}
242 		fprintf(ofp, "%s", line);
243 		continue;
244 percent:
245 		if (eq(line, "%OBJS\n")) {
246 			do_objs(ofp, "OBJS=", -1, 0);
247 		} else if (eq(line, "%LIBOBJS\n")) {
248 			do_objs(ofp, "LIBOBJS=", -1, LIBRARYDEP);
249 		} else if (eq(line, "%CFILES\n")) {
250 			do_files(ofp, "CFILES=", 'c');
251 			do_objs(ofp, "COBJS=", 'c', 0);
252 		} else if (eq(line, "%CXXFILES\n")) {
253 			do_files(ofp, "CXXFILES=", 'p');
254 			do_objs(ofp, "CXXOBJS=", 'p', 0);
255 		} else if (eq(line, "%SFILES\n")) {
256 			do_files(ofp, "SFILES=", 's');
257 			do_objs(ofp, "SOBJS=", 's', 0);
258 		} else if (eq(line, "%MACHDEP\n")) {
259 			do_machdep(ofp);
260 		} else if (eq(line, "%RULES\n")) {
261 			do_rules(ofp);
262 		} else {
263 			fprintf(stderr,
264 			    "Unknown %% construct in generic makefile: %s",
265 			    line);
266 		}
267 	}
268 	if (dfp != NULL) {
269 		copy_dependencies(dfp, ofp);
270 		(void) fclose(dfp);
271 	}
272 	(void) fclose(ifp);
273 	(void) fclose(ofp);
274 }
275 
276 /*
277  * Read in the information about files used in making the system.
278  * Store it in the ftab linked list.
279  */
280 void
read_files(void)281 read_files(void)
282 {
283 	FILE *fp;
284 	struct file_list *tp, *pf;
285 	struct device *dp;
286 	struct opt *op;
287 	const char *wd;
288 	char *this, *needs;
289 	const char *devorprof;
290 	int options;
291 	int not_option;
292 	int f_flags;
293 	char pname[BUFSIZ];
294 	char fname[1024];
295 	char *rest = (char *) 0;
296 	int nreqs, first = 1, isdup;
297 
298 	ftab = 0;
299 	(void) sprintf(fname, "%s/files", config_directory);
300 openit:
301 	fp = fopenp(VPATH, fname, pname, "r");
302 	if (fp == 0) {
303 		perror(fname);
304 		exit(1);
305 	}
306 next:
307 	options = 0;
308 	rest = (char *) 0;
309 	/*
310 	 * filename	[ standard | optional ]
311 	 *	[ dev* | profiling-routine ] [ device-driver]
312 	 */
313 	wd = get_word(fp);
314 	if (wd == (char *)EOF) {
315 		(void) fclose(fp);
316 		if (first == 1) {
317 			(void) sprintf(fname, "%s/files.%s", config_directory, machinename);
318 			first++;
319 			goto openit;
320 		}
321 		return;
322 	}
323 	if (wd == 0) {
324 		goto next;
325 	}
326 	/*
327 	 *  Allow comment lines beginning witha '#' character.
328 	 */
329 	if (*wd == '#') {
330 		while ((wd = get_word(fp)) && wd != (char *)EOF) {
331 			;
332 		}
333 		goto next;
334 	}
335 
336 	this = ns(wd);
337 	next_word(fp, wd);
338 	if (wd == 0) {
339 		printf("%s: No type for %s.\n",
340 		    fname, this);
341 		exit(1);
342 	}
343 	if ((pf = fl_lookup(this)) && (pf->f_type != INVISIBLE || pf->f_flags)) {
344 		isdup = 1;
345 	} else {
346 		isdup = 0;
347 	}
348 	tp = 0;
349 	nreqs = 0;
350 	devorprof = "";
351 	needs = 0;
352 	f_flags = 0;
353 	if (eq(wd, "standard")) {
354 		goto checkdev;
355 	}
356 	if (!eq(wd, "optional")) {
357 		printf("%s: %s must be optional or standard\n", fname, this);
358 		exit(1);
359 	}
360 	if (strncmp(this, "OPTIONS/", 8) == 0) {
361 		options++;
362 	}
363 	not_option = 0;
364 nextopt:
365 	next_word(fp, wd);
366 	if (wd == 0) {
367 		goto doneopt;
368 	}
369 	if (eq(wd, "not")) {
370 		not_option = !not_option;
371 		goto nextopt;
372 	}
373 	devorprof = wd;
374 	if (eq(wd, "device-driver") || eq(wd, "profiling-routine")) {
375 		next_word(fp, wd);
376 		goto save;
377 	}
378 	if (eq(wd, "xnu-library")) {
379 		f_flags |= LIBRARYDEP;
380 		goto nextopt;
381 	}
382 	if (eq(wd, "bound-checks")) {
383 		if (f_flags & BOUND_CHECKS_MASK) {
384 			printf("%s: cannot combine bound-checks options\n", fname);
385 			exit(1);
386 		}
387 		f_flags |= BOUND_CHECKS;
388 		goto nextopt;
389 	}
390 	if (eq(wd, "bound-checks-pending")) {
391 		if (f_flags & BOUND_CHECKS_MASK) {
392 			printf("%s: cannot combine bound-checks options\n", fname);
393 			exit(1);
394 		}
395 		f_flags |= BOUND_CHECKS_PENDING;
396 		goto nextopt;
397 	}
398 	if (eq(wd, "bound-checks-soft")) {
399 		if (f_flags & BOUND_CHECKS_MASK) {
400 			printf("%s: cannot combine bound-checks options\n", fname);
401 			exit(1);
402 		}
403 		f_flags |= BOUND_CHECKS_SOFT;
404 		goto nextopt;
405 	}
406 	if (eq(wd, "bound-checks-debug")) {
407 		if (f_flags & BOUND_CHECKS_MASK) {
408 			printf("%s: cannot combine bound-checks options\n", fname);
409 			exit(1);
410 		}
411 		f_flags |= BOUND_CHECKS_DEBUG;
412 		goto nextopt;
413 	}
414 	if (eq(wd, "bound-checks-seed")) {
415 		if (f_flags & BOUND_CHECKS_MASK) {
416 			printf("%s: cannot combine bound-checks options\n", fname);
417 			exit(1);
418 		}
419 		f_flags |= BOUND_CHECKS_SEED;
420 		goto nextopt;
421 	}
422 	if (eq(wd, "bound-checks-new-checks")) {
423 		if (!(f_flags & BOUND_CHECKS_MASK)) {
424 			printf("%s: cannot use bound-checks-new-checks without a "
425 			    "bound-check* option\n", fname);
426 			exit(1);
427 		}
428 		f_flags |= BOUND_CHECKS_NEW_CHECKS;
429 		goto nextopt;
430 	}
431 	nreqs++;
432 	if (needs == 0 && nreqs == 1) {
433 		needs = ns(wd);
434 	}
435 	if (isdup) {
436 		goto invis;
437 	}
438 	if (options) {
439 		struct opt *lop = 0;
440 		struct device tdev;
441 
442 		/*
443 		 *  Allocate a pseudo-device entry which we will insert into
444 		 *  the device list below.  The flags field is set non-zero to
445 		 *  indicate an internal entry rather than one generated from
446 		 *  the configuration file.  The slave field is set to define
447 		 *  the corresponding symbol as 0 should we fail to find the
448 		 *  option in the option list.
449 		 */
450 		init_dev(&tdev);
451 		tdev.d_name = ns(wd);
452 		tdev.d_type = PSEUDO_DEVICE;
453 		tdev.d_flags++;
454 		tdev.d_slave = 0;
455 
456 		for (op = opt; op; lop = op, op = op->op_next) {
457 			char *od = allCaps(ns(wd));
458 
459 			/*
460 			 *  Found an option which matches the current device
461 			 *  dependency identifier.  Set the slave field to
462 			 *  define the option in the header file.
463 			 */
464 			if (strcmp(op->op_name, od) == 0) {
465 				tdev.d_slave = 1;
466 				if (lop == 0) {
467 					opt = op->op_next;
468 				} else {
469 					lop->op_next = op->op_next;
470 				}
471 				free(op);
472 				op = 0;
473 			}
474 			free(od);
475 			if (op == 0) {
476 				break;
477 			}
478 		}
479 		newdev(&tdev);
480 	}
481 	for (dp = dtab; dp != 0; dp = dp->d_next) {
482 		if (eq(dp->d_name, wd) && (dp->d_type != PSEUDO_DEVICE || dp->d_slave)) {
483 			if (not_option) {
484 				goto invis;     /* dont want file if option present */
485 			} else {
486 				goto nextopt;
487 			}
488 		}
489 	}
490 	if (not_option) {
491 		goto nextopt;           /* want file if option missing */
492 	}
493 	for (op = opt; op != 0; op = op->op_next) {
494 		if (op->op_value == 0 && opteq(op->op_name, wd)) {
495 			if (nreqs == 1) {
496 				free(needs);
497 				needs = 0;
498 			}
499 			goto nextopt;
500 		}
501 	}
502 
503 invis:
504 	while ((wd = get_word(fp)) != 0) {
505 		;
506 	}
507 	if (tp == 0) {
508 		tp = new_fent();
509 	}
510 	tp->f_fn = this;
511 	tp->f_type = INVISIBLE;
512 	tp->f_needs = needs;
513 	tp->f_flags = isdup;
514 	goto next;
515 
516 doneopt:
517 	if (nreqs == 0) {
518 		printf("%s: what is %s optional on?\n",
519 		    fname, this);
520 		exit(1);
521 	}
522 
523 checkdev:
524 	if (wd) {
525 		if (*wd == '|') {
526 			goto getrest;
527 		}
528 		next_word(fp, wd);
529 		while (wd) {
530 			if (eq(wd, "xnu-library")) {
531 				f_flags |= LIBRARYDEP;
532 				next_word(fp, wd);
533 				continue;
534 			}
535 			if (eq(wd, "bound-checks")) {
536 				if (f_flags & BOUND_CHECKS_MASK) {
537 					printf("%s: cannot combine bound-checks options\n", fname);
538 					exit(1);
539 				}
540 				f_flags |= BOUND_CHECKS;
541 				next_word(fp, wd);
542 				continue;
543 			}
544 			if (eq(wd, "bound-checks-pending")) {
545 				if (f_flags & BOUND_CHECKS_MASK) {
546 					printf("%s: cannot combine bound-checks options\n", fname);
547 					exit(1);
548 				}
549 				f_flags |= BOUND_CHECKS_PENDING;
550 				next_word(fp, wd);
551 				continue;
552 			}
553 			if (eq(wd, "bound-checks-soft")) {
554 				if (f_flags & BOUND_CHECKS_MASK) {
555 					printf("%s: cannot combine bound-checks options\n", fname);
556 					exit(1);
557 				}
558 				f_flags |= BOUND_CHECKS_SOFT;
559 				next_word(fp, wd);
560 				continue;
561 			}
562 			if (eq(wd, "bound-checks-debug")) {
563 				if (f_flags & BOUND_CHECKS_MASK) {
564 					printf("%s: cannot combine bound-checks options\n", fname);
565 					exit(1);
566 				}
567 				f_flags |= BOUND_CHECKS_DEBUG;
568 				next_word(fp, wd);
569 				continue;
570 			}
571 			if (eq(wd, "bound-checks-seed")) {
572 				if (f_flags & BOUND_CHECKS_MASK) {
573 					printf("%s: cannot combine bound-checks options\n", fname);
574 					exit(1);
575 				}
576 				f_flags |= BOUND_CHECKS_SEED;
577 				next_word(fp, wd);
578 				continue;
579 			}
580 			if (eq(wd, "bound-checks-new-checks")) {
581 				if (!(f_flags & BOUND_CHECKS_MASK)) {
582 					printf("%s: cannot use bound-checks-new-checks without a "
583 					    "bound-check* option\n", fname);
584 					exit(1);
585 				}
586 				f_flags |= BOUND_CHECKS_NEW_CHECKS;
587 				next_word(fp, wd);
588 				continue;
589 			}
590 
591 			devorprof = wd;
592 			next_word(fp, wd);
593 			break;
594 		}
595 	}
596 
597 save:
598 getrest:
599 	if (wd) {
600 		if (*wd == '|') {
601 			rest = ns(get_rest(fp));
602 		} else {
603 			printf("%s: syntax error describing %s\n",
604 			    fname, this);
605 			exit(1);
606 		}
607 	}
608 	if (eq(devorprof, "profiling-routine") && profiling == 0) {
609 		goto next;
610 	}
611 	if (tp == 0) {
612 		tp = new_fent();
613 	}
614 	tp->f_fn = this;
615 	tp->f_extra = rest;
616 	if (options) {
617 		tp->f_type = INVISIBLE;
618 	} else if (eq(devorprof, "device-driver")) {
619 		tp->f_type = DRIVER;
620 	} else if (eq(devorprof, "profiling-routine")) {
621 		tp->f_type = PROFILING;
622 	} else {
623 		tp->f_type = NORMAL;
624 	}
625 	tp->f_flags = f_flags;
626 	tp->f_needs = needs;
627 	if (pf && pf->f_type == INVISIBLE) {
628 		pf->f_flags = 1;                /* mark as duplicate */
629 	}
630 	goto next;
631 }
632 
633 int
opteq(const char * cp,const char * dp)634 opteq(const char *cp, const char *dp)
635 {
636 	char c, d;
637 
638 	for (;; cp++, dp++) {
639 		if (*cp != *dp) {
640 			c = isupper(*cp) ? tolower(*cp) : *cp;
641 			d = isupper(*dp) ? tolower(*dp) : *dp;
642 			if (c != d) {
643 				return 0;
644 			}
645 		}
646 		if (*cp == 0) {
647 			return 1;
648 		}
649 	}
650 }
651 
652 void
put_source_file_name(FILE * fp,struct file_list * tp)653 put_source_file_name(FILE *fp, struct file_list *tp)
654 {
655 	if ((tp->f_fn[0] == '.') && (tp->f_fn[1] == '/')) {
656 		fprintf(fp, "%s ", tp->f_fn);
657 	} else {
658 		fprintf(fp, "$(SOURCE_DIR)/%s ", tp->f_fn);
659 	}
660 }
661 
662 void
do_objs(FILE * fp,const char * msg,int ext,int flags)663 do_objs(FILE *fp, const char *msg, int ext, int flags)
664 {
665 	struct file_list *tp;
666 	int lpos, len;
667 	char *cp;
668 	char och;
669 	const char *sp;
670 
671 	fprintf(fp, "%s", msg);
672 	lpos = strlen(msg);
673 	for (tp = ftab; tp != 0; tp = tp->f_next) {
674 		if (tp->f_type == INVISIBLE) {
675 			continue;
676 		}
677 
678 		/*
679 		 * Check flags (if any)
680 		 */
681 		if (flags && ((tp->f_flags & flags) != flags)) {
682 			continue;
683 		}
684 
685 		/*
686 		 *	Check for '.o' file in list
687 		 */
688 		cp = tp->f_fn + (len = strlen(tp->f_fn)) - 1;
689 		if (ext != -1 && *cp != ext) {
690 			continue;
691 		} else if (*cp == 'o') {
692 			if (len + lpos > 72) {
693 				lpos = 8;
694 				fprintf(fp, "\\\n\t");
695 			}
696 			put_source_file_name(fp, tp);
697 			fprintf(fp, " ");
698 			lpos += len + 1;
699 			continue;
700 		}
701 		sp = tail(tp->f_fn);
702 		cp = (char *)sp + (len = strlen(sp)) - 1;
703 		och = *cp;
704 		*cp = 'o';
705 		if (len + lpos > 72) {
706 			lpos = 8;
707 			fprintf(fp, "\\\n\t");
708 		}
709 		fprintf(fp, "%s ", sp);
710 		lpos += len + 1;
711 		*cp = och;
712 	}
713 	putc('\n', fp);
714 }
715 
716 void
do_files(FILE * fp,const char * msg,char ext)717 do_files(FILE *fp, const char *msg, char ext)
718 {
719 	struct file_list *tp;
720 	int lpos, len = 0; /* dvw: init to 0 */
721 
722 	fprintf(fp, "%s", msg);
723 	lpos = 8;
724 	for (tp = ftab; tp != 0; tp = tp->f_next) {
725 		if (tp->f_type == INVISIBLE) {
726 			continue;
727 		}
728 		if (tp->f_fn[strlen(tp->f_fn) - 1] != ext) {
729 			continue;
730 		}
731 		/*
732 		 * Always generate a newline.
733 		 * Our Makefile's aren't readable anyway.
734 		 */
735 
736 		lpos = 8;
737 		fprintf(fp, "\\\n\t");
738 		put_source_file_name(fp, tp);
739 		lpos += len + 1;
740 	}
741 	putc('\n', fp);
742 }
743 
744 /*
745  *  Include machine dependent makefile in output
746  */
747 
748 void
do_machdep(FILE * ofp)749 do_machdep(FILE *ofp)
750 {
751 	FILE *ifp;
752 	char pname[BUFSIZ];
753 	char line[BUFSIZ];
754 
755 	(void) sprintf(line, "%s/Makefile.%s", config_directory, machinename);
756 	ifp = fopenp(VPATH, line, pname, "r");
757 	if (ifp == 0) {
758 		perror(line);
759 		exit(1);
760 	}
761 	while (fgets(line, BUFSIZ, ifp) != 0) {
762 		if (profiling && (strncmp(line, "LIBS=", 5) == 0)) {
763 			fprintf(ofp, "LIBS=${LIBS_P}\n");
764 		} else {
765 			fputs(line, ofp);
766 		}
767 	}
768 	fclose(ifp);
769 }
770 
771 const char *
tail(const char * fn)772 tail(const char *fn)
773 {
774 	const char *cp;
775 
776 	cp = rindex(fn, '/');
777 	if (cp == 0) {
778 		return fn;
779 	}
780 	return cp + 1;
781 }
782 
783 void emit_bounds_checks_new_checks_lines(struct file_list * ftp, FILE *f, const char* tp);
784 void
emit_bounds_checks_new_checks_lines(struct file_list * ftp,FILE * f,const char * tp)785 emit_bounds_checks_new_checks_lines(struct file_list * ftp, FILE *f, const char* tp)
786 {
787 	// We don't specify `-fbounds-safety-bringup-missing-checks` directly
788 	// here because `-fbounds-safety` is dynamically disabled at build time for
789 	// x86_64. Instead `CFLAGS_BOUND_CHECKS_ENABLE_NEW_CHECKS` and
790 	// `CFLAGS_BOUND_CHECKS_DISABLE_NEW_CHECKS` will be set to the appropriate
791 	// flag name if the `BOUND_CHECKS` make file variable is not `0`. See
792 	// `MakeInc.def`.
793 
794 	if (!(ftp->f_flags & BOUND_CHECKS_NEW_CHECKS)) {
795 		// Explicitly disable the new checks when building with
796 		// `-fbounds-safety`.
797 		//
798 		// While this is technically unnecessary (this is currently clang's
799 		// default) the behavior will eventually change (rdar://134095657).
800 		// Explicitly setting the flag means that when clang's behavior changes
801 		// the semantics of the conf files will remain the same (i.e. not
802 		// specifiying `bound-checks-new-checks` means new checks are disabled).
803 		fprintf(f, "%so_CFLAGS_ADD += ${CFLAGS_BOUND_CHECKS_DISABLE_NEW_CHECKS}\n", tp);
804 		return;
805 	}
806 
807 	// Enable all new checks
808 	fprintf(f, "%so_CFLAGS_ADD += ${CFLAGS_BOUND_CHECKS_ENABLE_NEW_CHECKS}\n", tp);
809 }
810 
811 /*
812  * Create the makerules for each file
813  * which is part of the system.
814  * Devices are processed with the special c2 option -i
815  * which avoids any problem areas with i/o addressing
816  * (e.g. for the VAX); assembler files are processed by as.
817  */
818 void
do_rules(FILE * f)819 do_rules(FILE *f)
820 {
821 	char *cp;
822 	char *np, och;
823 	const char *tp;
824 	struct file_list *ftp;
825 	const char *extras = ""; /* dvw: init to "" */
826 	char *source_dir;
827 	char och_upper;
828 	const char *nl = "";
829 
830 	for (ftp = ftab; ftp != 0; ftp = ftp->f_next) {
831 		if (ftp->f_type == INVISIBLE) {
832 			continue;
833 		}
834 		cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
835 		och = *cp;
836 		/*
837 		 *	Don't compile '.o' files
838 		 */
839 		if (och == 'o') {
840 			continue;
841 		}
842 		/*
843 		 *	Determine where sources should come from
844 		 */
845 		if ((np[0] == '.') && (np[1] == '/')) {
846 			source_dir = "";
847 			np += 2;
848 		} else {
849 			source_dir = "$(SOURCE_DIR)/";
850 		}
851 		*cp = '\0';
852 		tp = tail(np);  /* dvw: init tp before 'if' */
853 		fprintf(f, "-include %sd\n", tp);
854 		switch (ftp->f_flags & BOUND_CHECKS_MASK) {
855 		case BOUND_CHECKS_PENDING:
856 			fprintf(f, "%so_CFLAGS_ADD += ${CFLAGS_BOUND_CHECKS_PENDING}\n", tp);
857 			break;
858 		case BOUND_CHECKS:
859 			fprintf(f, "%so_CFLAGS_ADD += ${CFLAGS_BOUND_CHECKS}\n", tp);
860 			emit_bounds_checks_new_checks_lines(ftp, f, tp);
861 			break;
862 		case BOUND_CHECKS_SOFT:
863 			fprintf(f, "ifeq ($(CURRENT_KERNEL_CONFIG),RELEASE)\n");
864 			fprintf(f, "%so_CFLAGS_ADD += ${CFLAGS_BOUND_CHECKS_PENDING}\n", tp);
865 			fprintf(f, "else\n");
866 			fprintf(f, "%so_CFLAGS_ADD += ${CFLAGS_BOUND_CHECKS}\n", tp);
867 			fprintf(f, "%so_CFLAGS_ADD += ${CFLAGS_BOUND_CHECKS_SOFT}\n", tp);
868 			emit_bounds_checks_new_checks_lines(ftp, f, tp);
869 			fprintf(f, "endif # CURRENT_KERNEL_CONFIG\n");
870 			break;
871 		case BOUND_CHECKS_DEBUG:
872 			fprintf(f, "%so_CFLAGS_ADD += ${CFLAGS_BOUND_CHECKS}\n", tp);
873 			fprintf(f, "%so_CFLAGS_ADD += ${CFLAGS_BOUND_CHECKS_DEBUG}\n", tp);
874 			emit_bounds_checks_new_checks_lines(ftp, f, tp);
875 			break;
876 		case BOUND_CHECKS_SEED:
877 			fprintf(f, "ifeq ($(CURRENT_KERNEL_CONFIG),RELEASE)\n");
878 			fprintf(f, "%so_CFLAGS_ADD += ${CFLAGS_BOUND_CHECKS}\n", tp);
879 			fprintf(f, "%so_CFLAGS_ADD += ${CFLAGS_BOUND_CHECKS_SOFT}\n", tp);
880 			fprintf(f, "else\n");
881 			fprintf(f, "%so_CFLAGS_ADD += ${CFLAGS_BOUND_CHECKS}\n", tp);
882 			fprintf(f, "endif # CURRENT_KERNEL_CONFIG\n");
883 			emit_bounds_checks_new_checks_lines(ftp, f, tp);
884 			break;
885 		}
886 		fprintf(f, "%so: %s%s%c\n", tp, source_dir, np, och);
887 		if (och == 's') {
888 			fprintf(f, "\t${S_RULE_0}\n");
889 			fprintf(f, "\t${S_RULE_1A} %s%.*s${S_RULE_1B}%s\n",
890 			    source_dir, (int)(tp - np), np, nl);
891 			fprintf(f, "\t${S_RULE_2}%s\n", nl);
892 			continue;
893 		}
894 		extras = "";
895 		switch (ftp->f_type) {
896 		case NORMAL:
897 			goto common;
898 			break;
899 
900 		case DRIVER:
901 			extras = "_D";
902 			goto common;
903 			break;
904 
905 		case PROFILING:
906 			if (!profiling) {
907 				continue;
908 			}
909 			if (COPTS == 0) {
910 				fprintf(stderr,
911 				    "config: COPTS undefined in generic makefile");
912 				COPTS = "";
913 			}
914 			extras = "_P";
915 			goto common;
916 
917 common:
918 			och_upper = och + 'A' - 'a';
919 			fprintf(f, "\t${%c_RULE_0%s}\n", och_upper, extras);
920 			fprintf(f, "\t${%c_RULE_1A%s}", och_upper, extras);
921 			if (ftp->f_extra) {
922 				fprintf(f, "%s", ftp->f_extra);
923 			}
924 			fprintf(f, " %s%.*s${%c_RULE_1B%s}%s\n",
925 			    source_dir, (int)(tp - np), np, och_upper, extras, nl);
926 
927 			fprintf(f, "\t${%c_RULE_2%s}%s\n", och_upper, extras, nl);
928 			fprintf(f, "\t${%c_RULE_3%s}%s\n", och_upper, extras, nl);
929 			fprintf(f, "\t${%c_RULE_4%s}%s\n", och_upper, extras, nl);
930 			break;
931 
932 		default:
933 			printf("Don't know rules for %s\n", np);
934 			break;
935 		}
936 		*cp = och;
937 	}
938 }
939 
940 char *
allCaps(char * str)941 allCaps(char *str)
942 {
943 	char *cp = str;
944 
945 	while (*str) {
946 		if (islower(*str)) {
947 			*str = toupper(*str);
948 		}
949 		str++;
950 	}
951 	return cp;
952 }
953 
954 #define OLDSALUTATION "# DO NOT DELETE THIS LINE"
955 
956 #define LINESIZE 1024
957 static char makbuf[LINESIZE];           /* one line buffer for makefile */
958 
959 void
copy_dependencies(FILE * makin,FILE * makout)960 copy_dependencies(FILE *makin, FILE *makout)
961 {
962 	int oldlen = (sizeof OLDSALUTATION - 1);
963 
964 	while (fgets(makbuf, LINESIZE, makin) != NULL) {
965 		if (!strncmp(makbuf, OLDSALUTATION, oldlen)) {
966 			break;
967 		}
968 	}
969 	while (fgets(makbuf, LINESIZE, makin) != NULL) {
970 		if (oldlen != 0) {
971 			if (makbuf[0] == '\n') {
972 				continue;
973 			} else {
974 				oldlen = 0;
975 			}
976 		}
977 		fputs(makbuf, makout);
978 	}
979 }
980