xref: /original-bsd/usr.sbin/config/mkmakefile.c (revision 0b685140)
1 /*
2  * mkmakefile.c	1.12	81/10/12
3  *	Functions in this file build the makefile from the files list
4  *	and the information in the config table
5  */
6 
7 #include <stdio.h>
8 #include <ctype.h>
9 #include "y.tab.h"
10 #include "config.h"
11 
12 #define next_word(fp, wd)\
13     { register char *word = get_word(fp);\
14 	if (word == EOF) return EOF; \
15 	else wd = word; }
16 
17 static struct file_list *fcur;
18 
19 /*
20  * fl_lookup
21  *	look up a file name
22  */
23 
24 struct file_list *fl_lookup(file)
25 register char *file;
26 {
27     register struct file_list *fp;
28 
29     for (fp = ftab ; fp != NULL; fp = fp->f_next)
30     {
31 	if (eq(fp->f_fn, file))
32 	    return fp;
33     }
34     return NULL;
35 }
36 
37 /*
38  * new_fent
39  *	Make a new file list entry
40  */
41 
42 struct file_list *new_fent()
43 {
44     register struct file_list *fp;
45 
46     fp = (struct file_list *) malloc(sizeof *fp);
47     fp->f_needs = fp->f_next = NULL;
48     if (fcur == NULL)
49 	fcur = ftab = fp;
50     else
51 	fcur->f_next = fp;
52     fcur = fp;
53     return fp;
54 }
55 
56 /*
57  * makefile:
58  *	Build the makefile from the skeleton
59  */
60 
61 makefile()
62 {
63     FILE *ifp, *ofp;
64     char line[BUFSIZ];
65     struct cputype *cp;
66     struct opt *op;
67 
68     read_files();			/* Read in the "files" file */
69     ifp = fopen("../conf/makefile", "r");
70     if (ifp == NULL) {
71 	perror("../conf/makefile");
72 	exit(1);
73     }
74     ofp = fopen(path("makefile"), "w");
75     if (ofp == NULL) {
76 	perror(path("makefile"));
77 	exit(1);
78     }
79     fprintf(ofp, "IDENT=-D%s", raise(ident));
80     if (cputype == NULL) {
81 	printf("cpu type must be specified\n");
82 	exit(1);
83     }
84     for (cp = cputype; cp; cp = cp->cpu_next)
85 	fprintf(ofp, " -D%s", cp->cpu_name);
86     for (op = opt; op; op = op->op_next)
87 	  fprintf(ofp, " -D%s", op->op_name);
88     fprintf(ofp, "\n");
89     if (hz == 0) {
90 #ifdef notdef
91 	printf("hz not specified; 50hz assumed\n");
92 #endif
93 	hz = 60;
94     }
95     if (hadtz == 0)
96 	printf("timezone not specified; gmt assumed\n");
97     if (maxusers == 0) {
98 	printf("maxusers not specified; 24 assumed\n");
99 	maxusers = 24;
100     } else if (maxusers < 8) {
101 	printf("minimum of 8 maxusers assumed\n");
102 	maxusers = 8;
103     } else if (maxusers > 128) {
104 	printf("maxusers truncated to 128\n");
105 	maxusers = 128;
106     }
107     fprintf(ofp, "PARAM=-DHZ=%d -DTIMEZONE=%d -DDST=%d -DMAXUSERS=%d\n",
108 	hz, timezone, dst, maxusers);
109     while(fgets(line, BUFSIZ, ifp) != NULL)
110     {
111 	if (*line != '%')
112 	{
113 	    fprintf(ofp, "%s", line);
114 	    continue;
115 	}
116 	else if (eq(line, "%OBJS\n"))
117 	    do_objs(ofp);
118 	else if (eq(line, "%CFILES\n"))
119 	    do_cfiles(ofp);
120 	else if (eq(line, "%RULES\n"))
121 	    do_rules(ofp);
122 	else if (eq(line, "%LOAD\n"))
123 	    do_load(ofp);
124 	else
125 	    fprintf(stderr, "Unknown %% construct in generic makefile: %s", line);
126     }
127     fclose(ifp);
128     fclose(ofp);
129 }
130 
131 /*
132  * files:
133  *	Read in the "files" file.
134  *	Store it in the ftab linked list
135  */
136 
137 read_files()
138 {
139     FILE *fp;
140     register struct file_list *tp;
141     register struct device *dp;
142     register char *wd, *this;
143     int type, nreqs, dev;
144 
145     fp = fopen("../conf/files", "r");
146     if (fp == NULL) {
147 	perror("../conf/files");
148 	exit(1);
149     }
150     ftab = NULL;
151     while((wd = get_word(fp)) != EOF)
152     {
153 	if (wd == NULL)
154 	    continue;
155 	this = ns(wd);
156 	/*
157 	 * Read standard/optional
158 	 */
159 	next_word(fp, wd);
160 	if (wd == NULL)
161 	{
162 	    fprintf(stderr, "Huh, no type for %s in files.\n", this);
163 	    exit(10);
164 	}
165 	if ((tp = fl_lookup(wd)) == NULL)
166 	    tp = new_fent();
167 	else
168 	    free(tp->f_fn);
169 	tp->f_fn = this;
170 	nreqs = dev = type = 0;
171 	if (eq(wd, "optional"))
172 	{
173 	    for (;;)
174 	    {
175 		next_word(fp, wd);
176 		if (wd == NULL || (dev = eq(wd, "device-driver")))
177 		{
178 		    if (nreqs == 0)
179 		    {
180 			fprintf(stderr, "Needed a dev for optional(%s)\n",
181 				this);
182 			exit(11);
183 		    }
184 		    else
185 			break;
186 		}
187 		nreqs++;
188 		if (tp->f_needs == NULL)
189 			tp->f_needs = ns(wd);
190 		for (dp = dtab ; dp != NULL; dp = dp->d_next)
191 		{
192 		    if (eq(dp->d_name, wd))
193 			break;
194 		}
195 		if (dp == NULL)
196 		{
197 		    type = INVISIBLE;
198 		    while ((wd = get_word(fp)) != NULL)
199 			;
200 		    break;
201 		}
202 	    }
203 	}
204 	if (dev == 0 && wd != NULL)
205 	{
206 	    next_word(fp, wd);
207 	    dev = (wd != NULL && eq(wd, "device-driver"));
208 	}
209 	if (type == 0)
210 	    type = dev ? DEVICE : NORMAL;
211 	tp->f_type = type;
212     }
213     fclose(fp);
214 }
215 
216 /*
217  * do_objs
218  *	Spew forth the OBJS definition
219  */
220 
221 do_objs(fp)
222 FILE *fp;
223 {
224     register struct file_list *tp;
225     register int lpos, len;
226     register char *cp, och, *sp;
227     char *tail();
228 
229     fprintf(fp, "OBJS=");
230     lpos = 6;
231     for (tp = ftab; tp != NULL; tp = tp->f_next)
232     {
233 	if (tp->f_type == INVISIBLE)
234 	    continue;
235 	sp = tail(tp->f_fn);
236 	cp = sp + (len = strlen(sp)) - 1;
237 	och = *cp;
238 	*cp = 'o';
239 	if (len + lpos > 72)
240 	{
241 	    lpos = 8;
242 	    fprintf(fp, "\\\n\t");
243 	}
244 	fprintf(fp, "%s ", sp);
245 	lpos += len + 1;
246 	*cp = och;
247     }
248     if (lpos != 8)
249 	putc('\n', fp);
250 }
251 
252 /*
253  * do_cfiles
254  *	Spew forth the CFILES definition
255  */
256 
257 do_cfiles(fp)
258 FILE *fp;
259 {
260     register struct file_list *tp;
261     register int lpos, len;
262 
263     fprintf(fp, "CFILES=");
264     lpos = 8;
265     for (tp = ftab; tp != NULL; tp = tp->f_next)
266     {
267 	if (tp->f_type == INVISIBLE)
268 	    continue;
269 	if (tp->f_fn[strlen(tp->f_fn)-1] != 'c')
270 	    continue;
271 	if ((len = 3 + strlen(tp->f_fn)) + lpos > 72)
272 	{
273 	    lpos = 8;
274 	    fprintf(fp, "\\\n\t");
275 	}
276 	fprintf(fp, "../%s ", tp->f_fn);
277 	lpos += len + 1;
278     }
279     if (lpos != 8)
280 	putc('\n', fp);
281 }
282 
283 /*
284  * tail:
285  *	Return tail end of a filename
286  */
287 
288 char *tail(fn)
289 char *fn;
290 {
291     register char *cp;
292 
293     cp = rindex(fn, '/');
294     return cp+1;
295 }
296 
297 /*
298  * do_rules:
299  *	Spit out the rules for making each file
300  */
301 
302 do_rules(f)
303 FILE *f;
304 {
305     register char *cp, *np, och, *tp;
306     register struct file_list *ftp;
307 
308     for (ftp = ftab; ftp != NULL; ftp = ftp->f_next)
309     {
310 	if (ftp->f_type == INVISIBLE)
311 	    continue;
312 	cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
313 	och = *cp;
314 	*cp = '\0';
315 	fprintf(f, "%so: ../%s%c\n", tail(np), np, och);
316 	tp = tail(np);
317 	if (och == 's')
318 	    fprintf(f, "\t${AS} -o %so ../%ss\n\n", tp, np);
319 	else if (ftp->f_type == NORMAL)
320 	{
321 	    fprintf(f, "\t${CC} -I. -c -S ${COPTS} ../%sc\n", np);
322 	    fprintf(f, "\t${C2} %ss | sed -f ../sys/asm.sed | ${AS} -o %so\n",
323 		    tp, tp);
324 	    fprintf(f, "\trm -f %ss\n\n", tp);
325 	}
326 	else if (ftp->f_type == DEVICE)
327 	{
328 	    fprintf(f, "\t${CC} -I. -c -S ${COPTS} ../%sc\n", np);
329 	    fprintf(f,"\t${C2} -i %ss | sed -f ../sys/asm.sed | ${AS} -o %so\n",
330 		    tp, tp);
331 	    fprintf(f, "\trm -f %ss\n\n", tp);
332 	}
333 	else
334 	    fprintf(stderr, "Don't know rules for %s", np);
335 	*cp = och;
336     }
337 }
338 
339 /*
340  * Create the load strings
341  */
342 
343 do_load(f)
344 register FILE *f;
345 {
346     register struct file_list *fl;
347     register bool first = TRUE;
348 
349     for (fl = conf_list; fl != NULL; fl = fl->f_next)
350     {
351 	fprintf(f, "%s: makefile locore.o ${OBJS} ioconf.o param.o swap%s.o\n",
352 		fl->f_needs, fl->f_fn);
353 	fprintf(f, "\t@echo loading %s\n\t@rm -f %s\n",
354 		fl->f_needs, fl->f_needs);
355 	if (first)
356 	{
357 		first = FALSE;
358 		fprintf(f, "\t@sh ../conf/newvers.sh\n");
359 		fprintf(f, "\t@cc $(CFLAGS) -c vers.c\n");
360 	}
361 	fprintf(f,
362 	    "\t@ld -n -o %s -e start -x -T 80000000 locore.o ${OBJS} vers.o ioconf.o param.o swap%s.o\n",
363 	    fl->f_needs, fl->f_fn);
364 	fprintf(f, "\t@echo rearranging symbols\n");
365 	fprintf(f, "\t@-symorder ../sys/symbols.sort %s\n", fl->f_needs);
366 	fprintf(f, "\t@size %s\n", fl->f_needs);
367 	fprintf(f, "\t@chmod 755 %s\n\n", fl->f_needs);
368     }
369     for (fl = conf_list; fl != NULL; fl = fl->f_next)
370     {
371 	fprintf(f, "swap%s.o: ../dev/swap%s.c\n", fl->f_fn, fl->f_fn);
372 	fprintf(f, "\t${CC} -I. -c -S ${COPTS} ../dev/swap%s.c\n", fl->f_fn);
373 	fprintf(f,
374 	    "\t${C2} swap%s.s | sed -f ../sys/asm.sed | ${AS} -o swap%s.o\n",
375 	    fl->f_fn, fl->f_fn);
376 	fprintf(f, "\trm -f swap%s.s\n\n", fl->f_fn);
377     }
378     fprintf(f, "all:");
379     for (fl = conf_list; fl != NULL; fl = fl->f_next)
380 	fprintf(f, " %s", fl->f_needs);
381     putc('\n', f);
382 }
383 
384 raise(str)
385 register char *str;
386 {
387     register char *cp = str;
388 
389     while(*str)
390     {
391 	if (islower(*str))
392 	    *str = toupper(*str);
393 	str++;
394     }
395     return cp;
396 }
397