1/*
2 * Copyright (c) 1999 Apple Computer, 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
52static 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
65#ifdef __linux__
66#define __unused
67#endif
68
69#include "parser.h"
70#include "config.h"
71
72void	read_files(void);
73void	do_objs(FILE *fp, const char *msg, int ext);
74void	do_ordered(FILE *fp);
75void	do_files(FILE *fp, const char *msg, char ext);
76void	do_machdep(FILE *ofp);
77void	do_build(const char *name, void (*format)(FILE *));
78void	do_rules(FILE *f);
79void	do_load(FILE *f);
80struct file_list *do_systemspec(FILE *f, struct file_list *fl, int first);
81void	do_swapspec(FILE *f, const char *name, char *sysname);
82void	copy_dependencies(FILE *makin, FILE *makout);
83
84void	build_cputypes(FILE *fp);
85void	build_confdep(FILE *fp);
86
87struct file_list *fl_lookup(char *file);
88struct file_list *fltail_lookup(char *file);
89struct file_list *new_fent(void);
90
91void	put_source_file_name(FILE *fp, struct file_list *tp);
92
93
94#define DO_SWAPFILE	0
95
96#define next_word(fp, wd) \
97	{ register const char *word = get_word(fp); \
98	  if (word == (char *)EOF) \
99		return; \
100	  else \
101		wd = word; \
102	}
103
104static	struct file_list *fcur;
105const char *tail(const char *fn);
106char *allCaps(char *str);
107
108/*
109 * Lookup a file, by name.
110 */
111struct file_list *
112fl_lookup(char *file)
113{
114	register struct file_list *fp;
115
116	for (fp = ftab ; fp != 0; fp = fp->f_next) {
117		if (eq(fp->f_fn, file))
118			return (fp);
119	}
120	return (0);
121}
122
123/*
124 * Lookup a file, by final component name.
125 */
126struct file_list *
127fltail_lookup(char *file)
128{
129	register struct file_list *fp;
130
131	for (fp = ftab ; fp != 0; fp = fp->f_next) {
132		if (eq(tail(fp->f_fn), tail(file)))
133			return (fp);
134	}
135	return (0);
136}
137
138/*
139 * Make a new file list entry
140 */
141struct file_list *
142new_fent(void)
143{
144	register struct file_list *fp;
145
146	fp = (struct file_list *) malloc(sizeof *fp);
147	fp->f_needs = 0;
148	fp->f_next = 0;
149	fp->f_flags = 0;
150	fp->f_type = 0;
151	fp->f_extra = (char *) 0;
152	if (fcur == 0)
153		fcur = ftab = fp;
154	else
155		fcur->f_next = fp;
156	fcur = fp;
157	return (fp);
158}
159
160char	*COPTS;
161static	struct users {
162	int	u_default;
163	int	u_min;
164	int	u_max;
165} users[] = {
166	{ 24, 2, 1024 },		/* MACHINE_VAX */
167	{  8, 2, 32 },			/* MACHINE_SUN */
168	{ 16, 4, 32 },			/* MACHINE_ROMP */
169	{  8, 2, 32 },			/* MACHINE_SUN2 */
170	{  8, 2, 32 },			/* MACHINE_SUN3 */
171	{ 24, 8, 1024},			/* MACHINE_MMAX */
172	{ 32, 8, 1024},			/* MACHINE_SQT */
173	{  8, 2, 32 },			/* MACHINE_SUN4 */
174	{  2, 2, 1024 },		/* MACHINE_I386 */
175	{ 32, 8, 1024 },		/* MACHINE_IX */
176	{ 32, 8, 1024 },		/* MACHINE_MIPSY */
177	{ 32, 8, 1024 },		/* MACHINE_MIPS*/
178	{ 32, 8, 1024 },		/* MACHINE_I860*/
179	{  8, 2, 32 },			/* MACHINE_M68K */
180	{  8, 2, 32 },			/* MACHINE_M88K */
181	{  8, 2, 32 },			/* MACHINE_M98K */
182	{  8, 2, 32 },			/* MACHINE_HPPA */
183	{  8, 2, 32 },			/* MACHINE_SPARC */
184	{  8, 2, 32 },			/* MACHINE_PPC */
185	{  8, 2, 32 },			/* MACHINE_ARM */
186	{  8, 2, 32 },			/* MACHINE_X86_64 */
187	{  8, 2, 32 },			/* MACHINE_ARM64 */
188};
189#define NUSERS	(sizeof (users) / sizeof (users[0]))
190
191const char *
192get_VPATH(void)
193{
194    static char *vpath = NULL;
195
196    if ((vpath == NULL) &&
197	((vpath = getenv("VPATH")) != NULL) &&
198	(*vpath != ':')) {
199	register char *buf = malloc((unsigned)(strlen(vpath) + 2));
200
201	vpath = strcat(strcpy(buf, ":"), vpath);
202    }
203
204    return vpath ? vpath : "";
205}
206
207
208/*
209 * Build the makefile from the skeleton
210 */
211void
212makefile(void)
213{
214	FILE *ifp, *ofp;
215	FILE *dfp;
216	char pname[BUFSIZ];
217	char line[BUFSIZ];
218	struct opt *op;
219	struct users *up;
220
221	read_files();
222	(void) sprintf(line, "%s/Makefile.template", config_directory);
223	ifp = fopenp(VPATH, line, pname, "r");
224	if (ifp == 0) {
225		perror(line);
226		exit(1);
227	}
228	dfp = fopen(path("Makefile"), "r");
229	rename(path("Makefile"), path("Makefile.old"));
230	unlink(path("Makefile.old"));
231	unlink(path("M.d"));
232	if ((ofp = fopen(path("M.d"), "w")) == NULL) {
233		perror(path("M.d"));
234		/* We'll let this error go */
235	}
236	else
237	 	fclose(ofp);
238	ofp = fopen(path("Makefile"), "w");
239	if (ofp == 0) {
240		perror(path("Makefile"));
241		exit(1);
242	}
243	fprintf(ofp, "SOURCE_DIR=%s\n", source_directory);
244
245	if (machine == MACHINE_SUN || machine == MACHINE_SUN2
246	    || machine == MACHINE_SUN3 || machine == MACHINE_SUN4)
247		fprintf(ofp, "IDENT=-D%s -D%s", machinename, allCaps(ident));
248	else
249		fprintf(ofp, "IDENT=-D%s", allCaps(ident));
250	if (profiling)
251		fprintf(ofp, " -DGPROF");
252	if (cputype == 0) {
253		printf("cpu type must be specified\n");
254		exit(1);
255	}
256	do_build("cputypes.h", build_cputypes);
257
258	for (op = opt; op; op = op->op_next)
259		if (op->op_value)
260			fprintf(ofp, " -D%s=\"%s\"", op->op_name, op->op_value);
261		else
262			fprintf(ofp, " -D%s", op->op_name);
263	fprintf(ofp, "\n");
264	if ((unsigned)machine > NUSERS) {
265		printf("maxusers config info isn't present, using vax\n");
266		up = &users[MACHINE_VAX-1];
267	} else
268		up = &users[machine-1];
269	if (maxusers < up->u_min) {
270		maxusers = up->u_min;
271	} else if (maxusers > up->u_max)
272		printf("warning: maxusers > %d (%d)\n", up->u_max, maxusers);
273	if (maxusers) {
274		do_build("confdep.h", build_confdep);
275	}
276	for (op = mkopt; op; op = op->op_next)
277		if (op->op_value)
278			fprintf(ofp, "%s=%s\n", op->op_name, op->op_value);
279		else
280			fprintf(ofp, "%s\n", op->op_name);
281
282	while (fgets(line, BUFSIZ, ifp) != 0) {
283		if (*line == '%')
284			goto percent;
285		if (profiling && strncmp(line, "COPTS=", 6) == 0) {
286			register char *cp;
287			if (machine != MACHINE_MMAX)
288			    fprintf(ofp,
289				"GPROF.EX=$(SOURCE_DIR)/machdep/%s/gmon.ex\n", machinename);
290			cp = index(line, '\n');
291			if (cp)
292				*cp = 0;
293			cp = line + 6;
294			while (*cp && (*cp == ' ' || *cp == '\t'))
295				cp++;
296			COPTS = malloc((unsigned)(strlen(cp) + 1));
297			if (COPTS == 0) {
298				printf("config: out of memory\n");
299				exit(1);
300			}
301			strcpy(COPTS, cp);
302			if (machine == MACHINE_MIPSY || machine == MACHINE_MIPS) {
303				fprintf(ofp, "%s ${CCPROFOPT}\n", line);
304				fprintf(ofp, "PCOPTS=%s\n", cp);
305			} else if (machine == MACHINE_MMAX)
306				fprintf(ofp, "%s -p\n",line);
307			else
308				fprintf(ofp, "%s -pg\n", line);
309			continue;
310		}
311		fprintf(ofp, "%s", line);
312		continue;
313	percent:
314		if (eq(line, "%OBJS\n")) {
315			do_objs(ofp, "OBJS=", -1);
316		} else if (eq(line, "%CFILES\n")) {
317			do_files(ofp, "CFILES=", 'c');
318			do_objs(ofp, "COBJS=", 'c');
319		} else if (eq(line, "%MFILES\n")) {
320			do_files(ofp, "MFILES=", 'm');
321			do_objs(ofp, "MOBJS=", 'm');
322		} else if (eq(line, "%SFILES\n")) {
323			do_files(ofp, "SFILES=", 's');
324			do_objs(ofp, "SOBJS=", 's');
325		} else if (eq(line, "%BFILES\n"))
326			do_files(ofp, "BFILES=", 'b');
327		else if (eq(line, "%MACHDEP\n")) {
328			/*
329			 * Move do_machdep() after the mkopt stuff.
330			 */
331			for (op = mkopt; op; op = op->op_next)
332				fprintf(ofp, "%s=%s\n", op->op_name, op->op_value);
333			do_machdep(ofp);
334		} else if (eq(line, "%ORDERED\n"))
335			do_ordered(ofp);
336		else if (eq(line, "%RULES\n"))
337			do_rules(ofp);
338		else if (eq(line, "%LOAD\n"))
339			do_load(ofp);
340		else
341			fprintf(stderr,
342			    "Unknown %% construct in generic makefile: %s",
343			    line);
344	}
345	if (dfp != NULL)
346	{
347		copy_dependencies(dfp, ofp);
348		(void) fclose(dfp);
349	}
350	(void) fclose(ifp);
351	(void) fclose(ofp);
352}
353
354/*
355 * Read in the information about files used in making the system.
356 * Store it in the ftab linked list.
357 */
358void
359read_files(void)
360{
361	FILE *fp;
362	register struct file_list *tp, *pf;
363	register struct device *dp;
364	register struct opt *op;
365	const char *wd;
366	char *this, *needs;
367	const char *devorprof;
368	int options;
369	int not_option;
370	int ordered;
371	int sedit;				/* SQT */
372	char pname[BUFSIZ];
373	char fname[1024];
374	char *rest = (char *) 0;
375	struct cputype *cp;
376	int nreqs, first = 1, isdup;
377
378	ftab = 0;
379	(void) sprintf(fname, "%s/files", config_directory);
380openit:
381	fp = fopenp(VPATH, fname, pname, "r");
382	if (fp == 0) {
383		perror(fname);
384		exit(1);
385	}
386next:
387	options = 0;
388	rest = (char *) 0;
389	/*
390	 * filename	[ standard | optional ]
391	 *	[ dev* | profiling-routine ] [ device-driver]
392	 */
393	/*
394	 * MACHINE_SQT ONLY:
395	 *
396	 * filename	[ standard | optional ]
397	 *	[ ordered | sedit ]
398	 *	[ dev* | profiling-routine ] [ device-driver]
399	 */
400	wd = get_word(fp);
401	if (wd == (char *)EOF) {
402		(void) fclose(fp);
403		if (first == 1) {
404			(void) sprintf(fname, "%s/files.%s", config_directory, machinename);
405			first++;
406			goto openit;
407		}
408		if (first == 2) {
409			(void) sprintf(fname, "files.%s", allCaps(ident));
410			first++;
411			fp = fopenp(VPATH, fname, pname, "r");
412			if (fp != 0)
413				goto next;
414		}
415		return;
416	}
417	if (wd == 0)
418		goto next;
419	/*
420	 *  Allow comment lines beginning witha '#' character.
421	 */
422	if (*wd == '#')
423	{
424		while ((wd=get_word(fp)) && wd != (char *)EOF)
425			;
426		goto next;
427	}
428
429	this = ns(wd);
430	next_word(fp, wd);
431	if (wd == 0) {
432		printf("%s: No type for %s.\n",
433		    fname, this);
434		exit(1);
435	}
436	if ((pf = fl_lookup(this)) && (pf->f_type != INVISIBLE || pf->f_flags))
437		isdup = 1;
438	else
439		isdup = 0;
440	tp = 0;
441	if (first == 3 && (tp = fltail_lookup(this)) != 0)
442		printf("%s: Local file %s overrides %s.\n",
443		    fname, this, tp->f_fn);
444	nreqs = 0;
445	devorprof = "";
446	ordered = 0;
447	sedit = 1;				/* SQT: assume sedit for now */
448	needs = 0;
449	if (eq(wd, "standard"))
450		goto checkdev;
451	if (!eq(wd, "optional")) {
452		printf("%s: %s must be optional or standard\n", fname, this);
453		exit(1);
454	}
455	if (strncmp(this, "OPTIONS/", 8) == 0)
456		options++;
457	not_option = 0;
458nextopt:
459	next_word(fp, wd);
460	if (wd == 0)
461		goto doneopt;
462	if (eq(wd, "ordered")) {
463		ordered++;
464		goto nextopt;
465	}
466	if (machine == MACHINE_SQT && eq(wd, "sedit")) {
467		sedit++;
468		goto nextopt;
469	}
470	if (eq(wd, "not")) {
471		not_option = !not_option;
472		goto nextopt;
473	}
474	devorprof = wd;
475	if (eq(wd, "device-driver") || eq(wd, "profiling-routine")) {
476		next_word(fp, wd);
477		goto save;
478	}
479	nreqs++;
480	if (needs == 0 && nreqs == 1)
481		needs = ns(wd);
482	if (isdup)
483		goto invis;
484	if (options)
485	{
486		struct opt *lop = 0;
487		struct device tdev;
488
489		/*
490		 *  Allocate a pseudo-device entry which we will insert into
491		 *  the device list below.  The flags field is set non-zero to
492		 *  indicate an internal entry rather than one generated from
493		 *  the configuration file.  The slave field is set to define
494		 *  the corresponding symbol as 0 should we fail to find the
495		 *  option in the option list.
496		 */
497		init_dev(&tdev);
498		tdev.d_name = ns(wd);
499		tdev.d_type = PSEUDO_DEVICE;
500		tdev.d_flags++;
501		tdev.d_slave = 0;
502
503		for (op=opt; op; lop=op, op=op->op_next)
504		{
505			char *od = allCaps(ns(wd));
506
507			/*
508			 *  Found an option which matches the current device
509			 *  dependency identifier.  Set the slave field to
510			 *  define the option in the header file.
511			 */
512			if (strcmp(op->op_name, od) == 0)
513			{
514				tdev.d_slave = 1;
515				if (lop == 0)
516					opt = op->op_next;
517				else
518					lop->op_next = op->op_next;
519				free(op);
520				op = 0;
521			 }
522			free(od);
523			if (op == 0)
524				break;
525		}
526		newdev(&tdev);
527	}
528 	for (dp = dtab; dp != 0; dp = dp->d_next) {
529		if (eq(dp->d_name, wd) && (dp->d_type != PSEUDO_DEVICE || dp->d_slave)) {
530			if (not_option)
531				goto invis;	/* dont want file if option present */
532			else
533				goto nextopt;
534		}
535	}
536	if (not_option)
537		goto nextopt;		/* want file if option missing */
538
539	for (op = opt; op != 0; op = op->op_next)
540		if (op->op_value == 0 && opteq(op->op_name, wd)) {
541			if (nreqs == 1) {
542				free(needs);
543				needs = 0;
544			}
545			goto nextopt;
546		}
547
548	for (cp = cputype; cp; cp = cp->cpu_next)
549		if (opteq(cp->cpu_name, wd)) {
550			if (nreqs == 1) {
551				free(needs);
552				needs = 0;
553			}
554			goto nextopt;
555		}
556
557invis:
558	while ((wd = get_word(fp)) != 0)
559		;
560	if (tp == 0)
561		tp = new_fent();
562	tp->f_fn = this;
563	tp->f_type = INVISIBLE;
564	tp->f_needs = needs;
565	tp->f_flags = isdup;
566	goto next;
567
568doneopt:
569	if (nreqs == 0) {
570		printf("%s: what is %s optional on?\n",
571		    fname, this);
572		exit(1);
573	}
574
575checkdev:
576	if (wd) {
577		if (*wd == '|')
578			goto getrest;
579		next_word(fp, wd);
580		if (wd) {
581			if (eq(wd, "ordered")) {
582				ordered++;
583				goto checkdev;
584			}
585			if (machine == MACHINE_SQT && eq(wd, "sedit")) {
586				sedit++;
587				goto checkdev;
588			}
589			devorprof = wd;
590			next_word(fp, wd);
591		}
592	}
593
594save:
595getrest:
596	if (wd) {
597		if (*wd == '|') {
598			rest = ns(get_rest(fp));
599		} else {
600			printf("%s: syntax error describing %s\n",
601			       fname, this);
602			exit(1);
603		}
604	}
605	if (eq(devorprof, "profiling-routine") && profiling == 0)
606		goto next;
607	if (tp == 0)
608		tp = new_fent();
609	tp->f_fn = this;
610	tp->f_extra = rest;
611	if (options)
612		tp->f_type = INVISIBLE;
613	else
614	if (eq(devorprof, "device-driver"))
615		tp->f_type = DRIVER;
616	else if (eq(devorprof, "profiling-routine"))
617		tp->f_type = PROFILING;
618	else
619		tp->f_type = NORMAL;
620	tp->f_flags = 0;
621	if (ordered)
622		tp->f_flags |= ORDERED;
623	if (sedit)				/* SQT */
624		tp->f_flags |= SEDIT;
625	tp->f_needs = needs;
626	if (pf && pf->f_type == INVISIBLE)
627		pf->f_flags = 1;		/* mark as duplicate */
628	goto next;
629}
630
631int
632opteq(const char *cp, const char *dp)
633{
634	char c, d;
635
636	for (; ; cp++, dp++) {
637		if (*cp != *dp) {
638			c = isupper(*cp) ? tolower(*cp) : *cp;
639			d = isupper(*dp) ? tolower(*dp) : *dp;
640			if (c != d)
641				return (0);
642		}
643		if (*cp == 0)
644			return (1);
645	}
646}
647
648void
649put_source_file_name(FILE *fp, struct file_list *tp)
650{
651	if ((tp->f_fn[0] == '.') && (tp->f_fn[1] == '/'))
652		fprintf(fp, "%s ", tp->f_fn);
653	 else
654		fprintf(fp, "$(SOURCE_DIR)/%s ", tp->f_fn);
655}
656
657void
658do_objs(FILE *fp, const char *msg, int ext)
659{
660	register struct file_list *tp;
661	register int lpos, len;
662	char *cp;
663	char och;
664	const char *sp;
665#if	DO_SWAPFILE
666	register struct file_list *fl;
667	char swapname[32];
668#endif	/* DO_SWAPFILE */
669
670	fprintf(fp, "%s", msg);
671	lpos = strlen(msg);
672	for (tp = ftab; tp != 0; tp = tp->f_next) {
673		if (tp->f_type == INVISIBLE)
674			continue;
675
676		/*
677		 *	Check for '.o' file in list
678		 */
679		cp = tp->f_fn + (len = strlen(tp->f_fn)) - 1;
680		if ((ext == -1 && tp->f_flags & ORDERED) ||		/* not in objs */
681		    (ext != -1 && *cp != ext))
682			continue;
683		else if (*cp == 'o') {
684			if (len + lpos > 72) {
685				lpos = 8;
686				fprintf(fp, "\\\n\t");
687			}
688			put_source_file_name(fp, tp);
689			fprintf(fp, " ");
690			lpos += len + 1;
691			continue;
692		}
693		sp = tail(tp->f_fn);
694#if	DO_SWAPFILE
695		for (fl = conf_list; fl; fl = fl->f_next) {
696			if (fl->f_type != SWAPSPEC)
697				continue;
698			(void) sprintf(swapname, "swap%s.c", fl->f_fn);
699			if (eq(sp, swapname))
700				goto cont;
701		}
702#endif	/* DO_SWAPFILE */
703		cp = (char *)sp + (len = strlen(sp)) - 1;
704		och = *cp;
705		*cp = 'o';
706		if (len + lpos > 72) {
707			lpos = 8;
708			fprintf(fp, "\\\n\t");
709		}
710		fprintf(fp, "%s ", sp);
711		lpos += len + 1;
712		*cp = och;
713#if	DO_SWAPFILE
714cont:
715		;
716#endif	/* DO_SWAPFILE */
717	}
718	if (lpos != 8)
719		putc('\n', fp);
720}
721
722/* not presently used and probably broken,  use ORDERED instead */
723void
724do_ordered(FILE *fp)
725{
726	register struct file_list *tp;
727	register int lpos, len;
728	char *cp;
729	char och;
730	const char *sp;
731
732	fprintf(fp, "ORDERED=");
733	lpos = 10;
734	for (tp = ftab; tp != 0; tp = tp->f_next) {
735		if ((tp->f_flags & ORDERED) != ORDERED)
736			continue;
737		sp = tail(tp->f_fn);
738		cp = (char *)sp + (len = strlen(sp)) - 1;
739		och = *cp;
740		*cp = 'o';
741		if (len + lpos > 72) {
742			lpos = 8;
743			fprintf(fp, "\\\n\t");
744		}
745		fprintf(fp, "%s ", sp);
746		lpos += len + 1;
747		*cp = och;
748	}
749	if (lpos != 8)
750		putc('\n', fp);
751}
752
753void
754do_files(FILE *fp, const char *msg, char ext)
755{
756	register struct file_list *tp;
757	register int lpos, len=0; /* dvw: init to 0 */
758
759	fprintf(fp, "%s", msg);
760	lpos = 8;
761	for (tp = ftab; tp != 0; tp = tp->f_next) {
762		if (tp->f_type == INVISIBLE)
763			continue;
764		if (tp->f_fn[strlen(tp->f_fn)-1] != ext)
765			continue;
766		/*
767		 * Always generate a newline.
768		 * Our Makefile's aren't readable anyway.
769		 */
770
771		lpos = 8;
772		fprintf(fp, "\\\n\t");
773		put_source_file_name(fp, tp);
774		lpos += len + 1;
775	}
776	if (lpos != 8)
777		putc('\n', fp);
778}
779
780/*
781 *  Include machine dependent makefile in output
782 */
783
784void
785do_machdep(FILE *ofp)
786{
787	FILE *ifp;
788	char pname[BUFSIZ];
789	char line[BUFSIZ];
790
791	(void) sprintf(line, "%s/Makefile.%s", config_directory, machinename);
792	ifp = fopenp(VPATH, line, pname, "r");
793	if (ifp == 0) {
794		perror(line);
795		exit(1);
796	}
797	while (fgets(line, BUFSIZ, ifp) != 0) {
798		if (profiling && (strncmp(line, "LIBS=", 5) == 0))
799			fprintf(ofp,"LIBS=${LIBS_P}\n");
800		else
801			fputs(line, ofp);
802	}
803	fclose(ifp);
804}
805
806
807/*
808 *  Format configuration dependent parameter file.
809 */
810
811void
812build_confdep(FILE *fp)
813{
814	fprintf(fp, "#define MAXUSERS %d\n", maxusers);
815}
816
817/*
818 *  Format cpu types file.
819 */
820
821void
822build_cputypes(FILE *fp)
823{
824	struct cputype *cp;
825
826	for (cp = cputype; cp; cp = cp->cpu_next)
827		fprintf(fp, "#define\t%s\t1\n", cp->cpu_name);
828}
829
830
831
832/*
833 *  Build a define parameter file.  Create it first in a temporary location and
834 *  determine if this new contents differs from the old before actually
835 *  replacing the original (so as not to introduce avoidable extraneous
836 *  compilations).
837 */
838
839void
840do_build(const char *name, void (*format)(FILE *))
841{
842	static char temp[]="#config.tmp";
843	FILE *tfp, *ofp;
844	int c;
845
846	unlink(path(temp));
847	tfp = fopen(path(temp), "w+");
848	if (tfp == 0) {
849		perror(path(temp));
850		exit(1);
851	}
852	unlink(path(temp));
853	(*format)(tfp);
854	ofp = fopen(path(name), "r");
855	if (ofp != 0)
856	{
857		fseek(tfp, 0, 0);
858		while ((c = fgetc(tfp)) != EOF)
859			if (fgetc(ofp) != c)
860				goto copy;
861		if (fgetc(ofp) == EOF)
862			goto same;
863
864	}
865copy:
866	if (ofp)
867		fclose(ofp);
868	unlink(path(name));
869	ofp = fopen(path(name), "w");
870	if (ofp == 0) {
871		perror(path(name));
872		exit(1);
873	}
874	fseek(tfp, 0, 0);
875	while ((c = fgetc(tfp)) != EOF)
876		fputc(c, ofp);
877same:
878	fclose(ofp);
879	fclose(tfp);
880}
881
882const char *
883tail(const char *fn)
884{
885	register const char *cp;
886
887	cp = rindex(fn, '/');
888	if (cp == 0)
889		return (fn);
890	return (cp+1);
891}
892
893/*
894 * Create the makerules for each file
895 * which is part of the system.
896 * Devices are processed with the special c2 option -i
897 * which avoids any problem areas with i/o addressing
898 * (e.g. for the VAX); assembler files are processed by as.
899 */
900void
901do_rules(FILE *f)
902{
903	char *cp;
904	char *np, och;
905	const char *tp;
906	register struct file_list *ftp;
907	const char *extras = ""; /* dvw: init to "" */
908	char *source_dir;
909	char och_upper;
910	const char *nl = "";
911
912	for (ftp = ftab; ftp != 0; ftp = ftp->f_next) {
913		if (ftp->f_type == INVISIBLE)
914			continue;
915		cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
916		och = *cp;
917		/*
918			*	Don't compile '.o' files
919			*/
920		if (och == 'o')
921			continue;
922		/*
923			*	Determine where sources should come from
924			*/
925		if ((np[0] == '.') && (np[1] == '/')) {
926			source_dir = "";
927			np += 2;
928		} else
929			source_dir = "$(SOURCE_DIR)/";
930		*cp = '\0';
931		tp = tail(np);	/* dvw: init tp before 'if' */
932		if (och == 'o') {
933			fprintf(f, "%so: %so\n\t${O_RULE_1A}%s%.*s${O_RULE_1B}\n\n",
934					tp, np, source_dir, (int)(tp-np), np);
935			continue;
936		}
937		fprintf(f, "%so: %s%s%c\n", tp, source_dir, np, och);
938		if (och == 's') {
939			switch (machine) {
940			case MACHINE_MIPSY:
941			case MACHINE_MIPS:
942				switch (ftp->f_type) {
943				case NORMAL:
944				case DRIVER:
945					fprintf(f, "\t@${RM} %so\n", tp);
946					fprintf(f, "\t${CC} ${CCASFLAGS}%s %s%s%ss\n\n",
947						(ftp->f_extra?ftp->f_extra:""), extras, source_dir, np);
948					break;
949
950				case PROFILING:
951					if (!profiling)
952						continue;
953					fprintf(f, "\t@${RM} %so\n", tp);
954					fprintf(f, "\t${CC} ${CCPASFLAGS}%s %s%s%ss\n\n",
955						(ftp->f_extra?ftp->f_extra:""), extras, source_dir, np);
956					break;
957
958				default:
959					printf("Don't know rules for %s.s\n", np);
960					break;
961				}
962				break;
963			default:
964				fprintf(f, "\t${S_RULE_1A}%s%.*s${S_RULE_1B}%s\n",
965						source_dir, (int)(tp-np), np, nl);
966				fprintf(f, "\t${S_RULE_2}%s\n", nl);
967				fprintf(f, "\t${S_RULE_3}\n\n");
968				break;
969			}
970			continue;
971		}
972		if (och == 'b') {
973			fprintf(f, "\t${B_RULE_1A}%s%.*s${B_RULE_1B}\n\n",
974				source_dir, (int)(tp-np), np);
975			continue;
976		}
977		extras = "";
978		switch (ftp->f_type) {
979
980		case NORMAL:
981			switch (machine) {
982
983			case MACHINE_MIPSY:
984			case MACHINE_MIPS:
985				fprintf(f, "\t@${RM} %so\n", tp);
986				fprintf(f, "\t${CC} ${CCNFLAGS}%s %s%s%sc\n\n",
987					(ftp->f_extra?ftp->f_extra:""), extras, source_dir, np);
988				continue;
989	#if	0
990			case MACHINE_SQT:
991				if (ftp->f_flags & SEDIT) {
992					fprintf(f, "\t${CC} -SO ${COPTS} %s%s%sc | \\\n", extras, source_dir, np);
993					fprintf(f, "\t${SEDCMD} | ${C2} | ${AS} ${CAFLAGS} -o %so\n\n", tp);
994				} else {
995					fprintf(f, "\t${CC} -c -O ${COPTS} %s%s%sc\n\n",
996						source_dir, extras, np);
997				}
998				break;
999	#endif	/* 0 */
1000			default:
1001				goto common;
1002			}
1003			break;
1004
1005		case DRIVER:
1006			switch (machine) {
1007
1008			case MACHINE_MIPSY:
1009			case MACHINE_MIPS:
1010				fprintf(f, "\t@${RM} %so\n", tp);
1011				fprintf(f, "\t${CC} ${CCDFLAGS}%s %s%s%sc\n\n",
1012					(ftp->f_extra?ftp->f_extra:""), extras, source_dir, np);
1013				continue;
1014			default:
1015				extras = "_D";
1016				goto common;
1017			}
1018			break;
1019
1020		case PROFILING:
1021			if (!profiling)
1022				continue;
1023			if (COPTS == 0) {
1024				fprintf(stderr,
1025					"config: COPTS undefined in generic makefile");
1026				COPTS = "";
1027			}
1028			switch (machine) {
1029				case MACHINE_MIPSY:
1030				case MACHINE_MIPS:
1031					fprintf(f, "\t@${RM} %so\n", tp);
1032					fprintf(f, "\t${CC} ${CCPFLAGS}%s %s../%sc\n\n",
1033						(ftp->f_extra?ftp->f_extra:""), extras, np);
1034					continue;
1035				case MACHINE_VAX:
1036				case MACHINE_ROMP:
1037				case MACHINE_SQT:
1038				case MACHINE_MMAX:
1039				case MACHINE_SUN3:
1040				case MACHINE_SUN4:
1041				case MACHINE_I386:
1042				case MACHINE_I860:
1043				case MACHINE_HPPA:
1044				case MACHINE_SPARC:
1045				case MACHINE_PPC:
1046				case MACHINE_ARM:
1047				case MACHINE_ARM64:
1048				case MACHINE_X86_64:
1049					extras = "_P";
1050					goto common;
1051				default:
1052				fprintf(stderr,
1053					"config: don't know how to profile kernel on this cpu\n");
1054				break;
1055			}
1056
1057		common:
1058			och_upper = och + 'A' - 'a';
1059			fprintf(f, "\t${%c_RULE_1A%s}", och_upper, extras);
1060			if (ftp->f_extra)
1061				fprintf(f, "%s", ftp->f_extra);
1062			fprintf(f, "%s%.*s${%c_RULE_1B%s}%s\n",
1063					source_dir, (int)(tp-np), np, och_upper, extras, nl);
1064			fprintf(f, "\t${%c_RULE_2%s}%s\n", och_upper, extras, nl);
1065			fprintf(f, "\t${%c_RULE_3%s}%s\n", och_upper, extras, nl);
1066			fprintf(f, "\t${%c_RULE_4%s}\n\n", och_upper, extras);
1067			break;
1068
1069		default:
1070			printf("Don't know rules for %s\n", np);
1071			break;
1072		}
1073		*cp = och;
1074	}
1075}
1076
1077/*
1078 * Create the load strings
1079 */
1080void
1081do_load(FILE *f)
1082{
1083	register struct file_list *fl;
1084	int first = 1;
1085
1086	fl = conf_list;
1087	while (fl) {
1088		if (fl->f_type != SYSTEMSPEC) {
1089			fl = fl->f_next;
1090			continue;
1091		}
1092		fl = do_systemspec(f, fl, first);
1093		if (first)
1094			first = 0;
1095	}
1096	fprintf(f, "LOAD =");
1097	for (fl = conf_list; fl != 0; fl = fl->f_next)
1098		if (fl->f_type == SYSTEMSPEC)
1099			fprintf(f, " %s", fl->f_needs);
1100#ifdef	multimax
1101	fprintf(f, "\n\nall .ORDER: includelinks ${LOAD}\n");
1102#else	/* multimax */
1103	fprintf(f, "\n\nall: includelinks ${LOAD}\n");
1104#endif	/* multimax */
1105	fprintf(f, "\n");
1106}
1107
1108struct file_list *
1109do_systemspec(FILE *f, struct file_list *fl, __unused int first)
1110{
1111	/*
1112	 * Variable for kernel name.
1113	 */
1114	fprintf(f, "KERNEL_NAME=%s\n", fl->f_needs);
1115
1116	fprintf(f, "%s .ORDER: %s.sys ${SYSDEPS}\n",
1117		fl->f_needs, fl->f_needs);
1118	fprintf(f, "\t${SYS_RULE_1}\n");
1119	fprintf(f, "\t${SYS_RULE_2}\n");
1120	fprintf(f, "\t${SYS_RULE_3}\n");
1121	fprintf(f, "\t${SYS_RULE_4}\n\n");
1122	do_swapspec(f, fl->f_fn, fl->f_needs);
1123	for (fl = fl->f_next; fl != NULL && fl->f_type == SWAPSPEC; fl = fl->f_next)
1124		continue;
1125	return (fl);
1126}
1127
1128void
1129do_swapspec(__unused FILE *f, __unused const char *name, __unused char *sysname)
1130{
1131
1132#if	DO_SWAPFILE
1133	char *gdir = eq(name, "generic")?"$(MACHINEDIR)/":"";
1134
1135	fprintf(f, "%s.sys:${P} ${PRELDDEPS} ${LDOBJS} ${LDDEPS}\n\n", sysname);
1136	fprintf(f, "%s.swap: swap%s.o\n", sysname, name);
1137	fprintf(f, "\t@rm -f $@\n");
1138	fprintf(f, "\t@cp swap%s.o $@\n\n", name);
1139	fprintf(f, "swap%s.o: %sswap%s.c ${SWAPDEPS}\n", name, gdir, name);
1140	if (machine == MACHINE_MIPSY || machine == MACHINE_MIPS) {
1141		fprintf(f, "\t@${RM} swap%s.o\n", name);
1142		fprintf(f, "\t${CC} ${CCNFLAGS} %sswap%s.c\n\n", gdir, name);
1143	} else {
1144		fprintf(f, "\t${C_RULE_1A}%s${C_RULE_1B}\n", gdir);
1145		fprintf(f, "\t${C_RULE_2}\n");
1146		fprintf(f, "\t${C_RULE_3}\n");
1147		fprintf(f, "\t${C_RULE_4}\n\n");
1148	}
1149#endif	/* DO_SWAPFILE */
1150}
1151
1152char *
1153allCaps(str)
1154	register char *str;
1155{
1156	register char *cp = str;
1157
1158	while (*str) {
1159		if (islower(*str))
1160			*str = toupper(*str);
1161		str++;
1162	}
1163	return (cp);
1164}
1165
1166#define OLDSALUTATION "# DO NOT DELETE THIS LINE"
1167
1168#define LINESIZE 1024
1169static char makbuf[LINESIZE];		/* one line buffer for makefile */
1170
1171void
1172copy_dependencies(FILE *makin, FILE *makout)
1173{
1174	register int oldlen = (sizeof OLDSALUTATION - 1);
1175
1176	while (fgets(makbuf, LINESIZE, makin) != NULL) {
1177		if (! strncmp(makbuf, OLDSALUTATION, oldlen))
1178			break;
1179	}
1180	while (fgets(makbuf, LINESIZE, makin) != NULL) {
1181		if (oldlen != 0)
1182		{
1183			if (makbuf[0] == '\n')
1184				continue;
1185			else
1186				oldlen = 0;
1187		}
1188		fputs(makbuf, makout);
1189	}
1190}
1191