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