now installs with symbol table (helpful for debugging dbx)
[unix-history] / usr / src / usr.sbin / config / mkmakefile.c
CommitLineData
15eb15d7 1/* mkmakefile.c 1.26 83/04/24 */
28061b3f 2
01d17851 3/*
28061b3f
BJ
4 * Build the makefile for the system, from
5 * the information in the files files and the
6 * additional files for the machine being compiled to.
01d17851
MT
7 */
8
9#include <stdio.h>
d5429061 10#include <ctype.h>
01d17851
MT
11#include "y.tab.h"
12#include "config.h"
13
28061b3f
BJ
14#define next_word(fp, wd) \
15 { register char *word = get_word(fp); \
22d68ad0
BJ
16 if (word == (char *)EOF) \
17 return; \
28061b3f
BJ
18 else \
19 wd = word; \
20 }
01d17851 21
28061b3f 22static struct file_list *fcur;
15eb15d7 23char *tail();
01d17851
MT
24
25/*
28061b3f 26 * Lookup a file, by make.
01d17851 27 */
28061b3f
BJ
28struct file_list *
29fl_lookup(file)
30 register char *file;
01d17851 31{
28061b3f
BJ
32 register struct file_list *fp;
33
22d68ad0 34 for (fp = ftab ; fp != 0; fp = fp->f_next) {
28061b3f
BJ
35 if (eq(fp->f_fn, file))
36 return (fp);
37 }
38 return (0);
01d17851
MT
39}
40
15eb15d7
KM
41/*
42 * Lookup a file, by final component name.
43 */
44struct file_list *
45fltail_lookup(file)
46 register char *file;
47{
48 register struct file_list *fp;
49
50 for (fp = ftab ; fp != 0; fp = fp->f_next) {
51 if (eq(tail(fp->f_fn), tail(file)))
52 return (fp);
53 }
54 return (0);
55}
56
01d17851 57/*
28061b3f 58 * Make a new file list entry
01d17851 59 */
28061b3f
BJ
60struct file_list *
61new_fent()
01d17851 62{
28061b3f
BJ
63 register struct file_list *fp;
64
65 fp = (struct file_list *) malloc(sizeof *fp);
22d68ad0
BJ
66 fp->f_needs = 0;
67 fp->f_next = 0;
68 if (fcur == 0)
28061b3f
BJ
69 fcur = ftab = fp;
70 else
71 fcur->f_next = fp;
72 fcur = fp;
73 return (fp);
01d17851
MT
74}
75
28061b3f
BJ
76char *COPTS;
77
01d17851 78/*
28061b3f 79 * Build the makefile from the skeleton
01d17851 80 */
01d17851
MT
81makefile()
82{
28061b3f
BJ
83 FILE *ifp, *ofp;
84 char line[BUFSIZ];
85 struct opt *op;
86
87 read_files();
88 strcpy(line, "../conf/makefile.");
22d68ad0 89 (void) strcat(line, machinename);
28061b3f 90 ifp = fopen(line, "r");
22d68ad0 91 if (ifp == 0) {
28061b3f
BJ
92 perror(line);
93 exit(1);
94 }
95 ofp = fopen(path("makefile"), "w");
22d68ad0 96 if (ofp == 0) {
28061b3f
BJ
97 perror(path("makefile"));
98 exit(1);
99 }
100 fprintf(ofp, "IDENT=-D%s", raise(ident));
101 if (profiling)
102 fprintf(ofp, " -DGPROF");
103 if (cputype == 0) {
104 printf("cpu type must be specified\n");
105 exit(1);
106 }
107 { struct cputype *cp;
108 for (cp = cputype; cp; cp = cp->cpu_next)
109 fprintf(ofp, " -D%s", cp->cpu_name);
110 }
111 for (op = opt; op; op = op->op_next)
112 if (op->op_value)
113 fprintf(ofp, " -D%s=\"%s\"", op->op_name, op->op_value);
114 else
115 fprintf(ofp, " -D%s", op->op_name);
116 fprintf(ofp, "\n");
117 if (hadtz == 0)
118 printf("timezone not specified; gmt assumed\n");
401f5ce4 119#ifdef vax
28061b3f
BJ
120 if (maxusers == 0) {
121 printf("maxusers not specified; 24 assumed\n");
122 maxusers = 24;
123 } else if (maxusers < 8) {
124 printf("minimum of 8 maxusers assumed\n");
125 maxusers = 8;
126 } else if (maxusers > 128) {
127 printf("maxusers truncated to 128\n");
128 maxusers = 128;
129 }
401f5ce4
SL
130#endif
131#ifdef sun
132 if (maxusers == 0) {
133 printf("maxusers not specified; 8 assumed\n");
134 maxusers = 8;
135 } else if (maxusers < 2) {
136 printf("minimum of 2 maxusers assumed\n");
137 maxusers = 2;
138 } else if (maxusers > 32) {
139 printf("maxusers truncated to 32\n");
140 maxusers = 32;
141 }
142#endif
28061b3f
BJ
143 fprintf(ofp, "PARAM=-DTIMEZONE=%d -DDST=%d -DMAXUSERS=%d\n",
144 timezone, dst, maxusers);
22d68ad0 145 while (fgets(line, BUFSIZ, ifp) != 0) {
28061b3f
BJ
146 if (*line == '%')
147 goto percent;
148 if (profiling && strncmp(line, "COPTS=", 6) == 0) {
149 register char *cp;
150
c7456392 151 fprintf(ofp, "GPROF.EX=/usr/src/lib/libc/csu/gmon.ex\n");
28061b3f
BJ
152 cp = index(line, '\n');
153 if (cp)
154 *cp = 0;
155 cp = line + 6;
156 while (*cp && (*cp == ' ' || *cp == '\t'))
157 cp++;
22d68ad0 158 COPTS = malloc((unsigned)(strlen(cp) + 1));
28061b3f 159 if (COPTS == 0) {
f2db9eb7 160 printf("config: out of memory\n");
28061b3f
BJ
161 exit(1);
162 }
163 strcpy(COPTS, cp);
164 fprintf(ofp, "%s -pg\n", line);
165 continue;
8486638c 166 }
28061b3f 167 fprintf(ofp, "%s", line);
8486638c 168 continue;
28061b3f
BJ
169 percent:
170 if (eq(line, "%OBJS\n"))
171 do_objs(ofp);
172 else if (eq(line, "%CFILES\n"))
173 do_cfiles(ofp);
174 else if (eq(line, "%RULES\n"))
175 do_rules(ofp);
176 else if (eq(line, "%LOAD\n"))
177 do_load(ofp);
178 else
179 fprintf(stderr,
180 "Unknown %% construct in generic makefile: %s",
181 line);
01d17851 182 }
73845e07
SL
183 (void) fclose(ifp);
184 (void) fclose(ofp);
01d17851
MT
185}
186
187/*
28061b3f
BJ
188 * Read in the information about files used in making the system.
189 * Store it in the ftab linked list.
01d17851 190 */
01d17851
MT
191read_files()
192{
28061b3f
BJ
193 FILE *fp;
194 register struct file_list *tp;
195 register struct device *dp;
196 char *wd, *this, *needs, *devorprof;
197 char fname[32];
198 int nreqs;
199 int first = 1;
200
22d68ad0
BJ
201 ftab = 0;
202 (void) strcpy(fname, "files");
28061b3f
BJ
203openit:
204 fp = fopen(fname, "r");
22d68ad0 205 if (fp == 0) {
73845e07 206 perror(fname);
28061b3f
BJ
207 exit(1);
208 }
209next:
210 /* filename [ standard | optional dev* ] [ device-driver ] */
211 wd = get_word(fp);
22d68ad0
BJ
212 if (wd == (char *)EOF) {
213 (void) fclose(fp);
15eb15d7 214 if (first == 1) {
22d68ad0 215 (void) sprintf(fname, "files.%s", machinename);
15eb15d7 216 first++;
28061b3f
BJ
217 goto openit;
218 }
15eb15d7
KM
219 if (first == 2) {
220 (void) sprintf(fname, "files.%s", raise(ident));
221 first++;
222 fp = fopen(fname, "r");
223 if (fp != 0)
224 goto next;
225 }
28061b3f
BJ
226 return;
227 }
22d68ad0 228 if (wd == 0)
28061b3f 229 goto next;
01d17851 230 this = ns(wd);
01d17851 231 next_word(fp, wd);
22d68ad0 232 if (wd == 0) {
f2db9eb7 233 printf("%s: No type for %s.\n",
28061b3f
BJ
234 fname, this);
235 exit(1);
01d17851 236 }
28061b3f 237 if (fl_lookup(this)) {
f2db9eb7 238 printf("%s: Duplicate file %s.\n",
28061b3f
BJ
239 fname, this);
240 exit(1);
241 }
15eb15d7
KM
242 tp = 0;
243 if (first == 3 && (tp = fltail_lookup(this)) != 0)
244 printf("%s: Local file %s overrides %s.\n",
245 fname, this, tp->f_fn);
28061b3f
BJ
246 nreqs = 0;
247 devorprof = "";
248 needs = 0;
249 if (eq(wd, "standard"))
250 goto checkdev;
251 if (!eq(wd, "optional")) {
73845e07 252 printf("%s: %s must be optional or standard\n",
28061b3f
BJ
253 fname, this);
254 exit(1);
255 }
256nextopt:
257 next_word(fp, wd);
22d68ad0 258 if (wd == 0)
28061b3f
BJ
259 goto doneopt;
260 devorprof = wd;
73845e07
SL
261 if (eq(wd, "device-driver") || eq(wd, "profiling-routine")) {
262 next_word(fp, wd);
28061b3f 263 goto save;
73845e07 264 }
28061b3f
BJ
265 nreqs++;
266 if (needs == 0)
267 needs = ns(wd);
22d68ad0 268 for (dp = dtab; dp != 0; dp = dp->d_next)
28061b3f
BJ
269 if (eq(dp->d_name, wd))
270 goto nextopt;
22d68ad0 271 while ((wd = get_word(fp)) != 0)
28061b3f 272 ;
15eb15d7
KM
273 if (tp == 0)
274 tp = new_fent();
01d17851 275 tp->f_fn = this;
28061b3f
BJ
276 tp->f_type = INVISIBLE;
277 tp->f_needs = needs;
278 goto next;
279doneopt:
280 if (nreqs == 0) {
f2db9eb7 281 printf("%s: what is %s optional on?\n",
28061b3f
BJ
282 fname, this);
283 exit(1);
284 }
285checkdev:
286 if (wd) {
2fcca161 287 next_word(fp, wd);
22d68ad0 288 if (wd != 0) {
28061b3f
BJ
289 devorprof = wd;
290 next_word(fp, wd);
2fcca161 291 }
01d17851 292 }
28061b3f 293save:
22d68ad0 294 if (wd != 0) {
f2db9eb7 295 printf("%s: syntax error describing %s\n",
28061b3f
BJ
296 fname, this);
297 exit(1);
2fcca161 298 }
73845e07
SL
299 if (eq(devorprof, "profiling-routine") && profiling == 0)
300 goto next;
15eb15d7
KM
301 if (tp == 0)
302 tp = new_fent();
28061b3f
BJ
303 tp->f_fn = this;
304 if (eq(devorprof, "device-driver"))
305 tp->f_type = DEVICE;
306 else if (eq(devorprof, "profiling-routine"))
307 tp->f_type = PROFILING;
308 else
309 tp->f_type = NORMAL;
310 tp->f_needs = needs;
311 goto next;
01d17851
MT
312}
313
01d17851 314do_objs(fp)
28061b3f 315 FILE *fp;
01d17851 316{
15eb15d7 317 register struct file_list *tp, *fl;
28061b3f
BJ
318 register int lpos, len;
319 register char *cp, och, *sp;
15eb15d7 320 char swapname[32];
28061b3f
BJ
321
322 fprintf(fp, "OBJS=");
323 lpos = 6;
22d68ad0 324 for (tp = ftab; tp != 0; tp = tp->f_next) {
28061b3f
BJ
325 if (tp->f_type == INVISIBLE)
326 continue;
327 sp = tail(tp->f_fn);
15eb15d7
KM
328 for (fl = conf_list; fl != 0; fl = fl->f_next) {
329 (void) sprintf(swapname, "swap%s.c", fl->f_fn);
330 if (eq(sp, swapname))
331 goto cont;
332 }
28061b3f
BJ
333 cp = sp + (len = strlen(sp)) - 1;
334 och = *cp;
335 *cp = 'o';
336 if (len + lpos > 72) {
337 lpos = 8;
338 fprintf(fp, "\\\n\t");
339 }
340 fprintf(fp, "%s ", sp);
341 lpos += len + 1;
342 *cp = och;
15eb15d7 343cont:;
01d17851 344 }
28061b3f
BJ
345 if (lpos != 8)
346 putc('\n', fp);
01d17851
MT
347}
348
01d17851 349do_cfiles(fp)
28061b3f 350 FILE *fp;
01d17851 351{
28061b3f
BJ
352 register struct file_list *tp;
353 register int lpos, len;
354
355 fprintf(fp, "CFILES=");
356 lpos = 8;
22d68ad0 357 for (tp = ftab; tp != 0; tp = tp->f_next) {
28061b3f
BJ
358 if (tp->f_type == INVISIBLE)
359 continue;
360 if (tp->f_fn[strlen(tp->f_fn)-1] != 'c')
361 continue;
362 if ((len = 3 + strlen(tp->f_fn)) + lpos > 72) {
363 lpos = 8;
364 fprintf(fp, "\\\n\t");
365 }
366 fprintf(fp, "../%s ", tp->f_fn);
367 lpos += len + 1;
01d17851 368 }
28061b3f
BJ
369 if (lpos != 8)
370 putc('\n', fp);
01d17851
MT
371}
372
28061b3f
BJ
373char *
374tail(fn)
375 char *fn;
01d17851 376{
28061b3f 377 register char *cp;
01d17851 378
28061b3f 379 cp = rindex(fn, '/');
15eb15d7
KM
380 if (cp == 0)
381 return (fn);
28061b3f 382 return (cp+1);
01d17851
MT
383}
384
385/*
28061b3f
BJ
386 * Create the makerules for each file
387 * which is part of the system.
388 * Devices are processed with the special c2 option -i
389 * which avoids any problem areas with i/o addressing
390 * (e.g. for the VAX); assembler files are processed by as.
01d17851 391 */
01d17851 392do_rules(f)
28061b3f 393 FILE *f;
01d17851 394{
28061b3f
BJ
395 register char *cp, *np, och, *tp;
396 register struct file_list *ftp;
01d17851 397
22d68ad0 398for (ftp = ftab; ftp != 0; ftp = ftp->f_next) {
01d17851 399 if (ftp->f_type == INVISIBLE)
28061b3f 400 continue;
01d17851
MT
401 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
402 och = *cp;
403 *cp = '\0';
404 fprintf(f, "%so: ../%s%c\n", tail(np), np, och);
405 tp = tail(np);
28061b3f
BJ
406 if (och == 's') {
407 fprintf(f, "\t${AS} -o %so ../%ss\n\n", tp, np);
408 continue;
01d17851 409 }
28061b3f
BJ
410 switch (ftp->f_type) {
411
412 case NORMAL:
413 switch (machine) {
414
415 case MACHINE_VAX:
416 fprintf(f, "\t${CC} -I. -c -S ${COPTS} ../%sc\n", np);
417 fprintf(f, "\t${C2} %ss | sed -f ../%s/asm.sed |",
418 tp, machinename);
419 fprintf(f, " ${AS} -o %so\n", tp);
420 fprintf(f, "\trm -f %ss\n\n", tp);
421 break;
422
423 case MACHINE_SUN:
424 fprintf(f, "\t${CC} -I. -c -O ${COPTS} ../%sc\n\n", np);
425 break;
426 }
427 break;
428
429 case DEVICE:
430 switch (machine) {
431
432 case MACHINE_VAX:
433 fprintf(f, "\t${CC} -I. -c -S ${COPTS} ../%sc\n", np);
434 fprintf(f,"\t${C2} -i %ss | sed -f ../%s/asm.sed |",
435 tp, machinename);
436 fprintf(f, " ${AS} -o %so\n", tp);
437 fprintf(f, "\trm -f %ss\n\n", tp);
438 break;
439
440 case MACHINE_SUN:
441 fprintf(f, "\t${CC} -I. -c -O ${COPTS} ../%sc\n\n", np);
442 }
443 break;
444
445 case PROFILING:
8486638c
SL
446 if (!profiling)
447 continue;
448 if (COPTS == 0) {
449 fprintf(stderr,
28061b3f 450 "config: COPTS undefined in generic makefile");
8486638c
SL
451 COPTS = "";
452 }
28061b3f
BJ
453 switch (machine) {
454
455 case MACHINE_VAX:
456 fprintf(f, "\t${CC} -I. -c -S %s ../%sc\n", COPTS, np);
c7456392 457 fprintf(f, "\tex - %ss < ${GPROF.EX}\n", tp);
28061b3f 458 fprintf(f,
c7456392
KM
459 "\tsed -f ../vax/asm.sed %ss | ${AS} -o %so\n",
460 tp, tp);
28061b3f
BJ
461 fprintf(f, "\trm -f %ss\n\n", tp);
462 break;
463
464 case MACHINE_SUN:
465 fprintf(stderr,
466 "config: don't know how to profile kernel on sun\n");
467 break;
468 }
73845e07 469 break;
28061b3f
BJ
470
471 default:
f2db9eb7 472 printf("Don't know rules for %s", np);
28061b3f
BJ
473 break;
474 }
01d17851 475 *cp = och;
28061b3f 476}
01d17851
MT
477}
478
479/*
480 * Create the load strings
481 */
01d17851 482do_load(f)
28061b3f 483 register FILE *f;
01d17851 484{
28061b3f 485 register struct file_list *fl;
821eeaae 486 int first = 1;
15eb15d7 487 char swapname[32];
28061b3f 488
22d68ad0 489 for (fl = conf_list; fl != 0; fl = fl->f_next) {
28061b3f
BJ
490 fprintf(f, "%s: makefile locore.o ${OBJS} param.o",
491 fl->f_needs);
492 fprintf(f, " ioconf.o swap%s.o\n", fl->f_fn);
493 fprintf(f, "\t@echo loading %s\n\t@rm -f %s\n",
494 fl->f_needs, fl->f_needs);
495 if (first) {
496 first = 0;
497 fprintf(f, "\t@sh ../conf/newvers.sh\n");
498 fprintf(f, "\t@${CC} $(CFLAGS) -c vers.c\n");
499 }
500 switch (machine) {
501
502 case MACHINE_VAX:
503 fprintf(f, "\t@${LD} -n -o %s -e start -x -T 80000000 ",
504 fl->f_needs);
505 fprintf(f, "locore.o ${OBJS} vers.o ioconf.o param.o ");
506 fprintf(f, "swap%s.o\n", fl->f_fn);
507 break;
508
509 case MACHINE_SUN:
510 fprintf(f, "\t@${LD} -o %s -e start -x -T 4000 ",
511 fl->f_needs);
512 fprintf(f, "locore.o ${OBJS} vers.o ioconf.o param.o ");
513 fprintf(f, "swap%s.o\n", fl->f_fn);
514 break;
515 }
516 fprintf(f, "\t@echo rearranging symbols\n");
517 fprintf(f, "\t@-symorder ../%s/symbols.sort %s\n",
518 machinename, fl->f_needs);
519 fprintf(f, "\t@size %s\n", fl->f_needs);
520 fprintf(f, "\t@chmod 755 %s\n\n", fl->f_needs);
190b5698 521 }
22d68ad0 522 for (fl = conf_list; fl != 0; fl = fl->f_next) {
15eb15d7
KM
523 (void) sprintf(swapname, "swap%s.c", fl->f_fn);
524 if (fltail_lookup(swapname) != 0)
525 continue;
28061b3f
BJ
526 fprintf(f, "swap%s.o: ../%s/swap%s.c\n",
527 fl->f_fn, machinename, fl->f_fn);
528 switch (machine) {
529
530 case MACHINE_VAX:
531 fprintf(f, "\t${CC} -I. -c -S ${COPTS}");
532 fprintf(f, " ../%s/swap%s.c\n", machinename, fl->f_fn);
533 fprintf(f, "\t${C2} swap%s.s | sed -f ../%s/asm.sed",
534 fl->f_fn, machinename);
535 fprintf(f, " | ${AS} -o swap%s.o\n",
536 fl->f_fn);
537 fprintf(f, "\trm -f swap%s.s\n\n", fl->f_fn);
538 break;
539
540 case MACHINE_SUN:
541 fprintf(f, "\t${CC} -I. -c -O ${COPTS} ");
542 fprintf(f, "../%s/swap%s.c\n\n", machinename, fl->f_fn);
543 break;
544 }
545 }
546 fprintf(f, "all:");
22d68ad0 547 for (fl = conf_list; fl != 0; fl = fl->f_next)
28061b3f
BJ
548 fprintf(f, " %s", fl->f_needs);
549 fprintf(f, "\n");
01d17851 550}
d5429061 551
22d68ad0 552char *
d5429061 553raise(str)
28061b3f 554 register char *str;
d5429061 555{
28061b3f
BJ
556 register char *cp = str;
557
558 while (*str) {
559 if (islower(*str))
560 *str = toupper(*str);
561 str++;
562 }
563 return (cp);
d5429061 564}