]> git.saurik.com Git - apple/xnu.git/blame - SETUP/config/mkmakefile.c
xnu-1699.22.73.tar.gz
[apple/xnu.git] / SETUP / config / mkmakefile.c
CommitLineData
6d2010ae
A
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#include "parser.h"
65#include "config.h"
66
67void read_files(void);
68void do_objs(FILE *fp, const char *msg, int ext);
69void do_ordered(FILE *fp);
70void do_files(FILE *fp, const char *msg, char ext);
71void do_machdep(FILE *ofp);
72void do_build(const char *name, void (*format)(FILE *));
73void do_rules(FILE *f);
74void do_load(FILE *f);
75struct file_list *do_systemspec(FILE *f, struct file_list *fl, int first);
76void do_swapspec(FILE *f, const char *name, char *sysname);
77void copy_dependencies(FILE *makin, FILE *makout);
78
79void build_cputypes(FILE *fp);
80void build_confdep(FILE *fp);
81
82struct file_list *fl_lookup(char *file);
83struct file_list *fltail_lookup(char *file);
84struct file_list *new_fent(void);
85
86void 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
99static struct file_list *fcur;
100const char *tail(const char *fn);
101char *allCaps(char *str);
102
103/*
104 * Lookup a file, by name.
105 */
106struct file_list *
107fl_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 */
121struct file_list *
122fltail_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 */
136struct file_list *
137new_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
155char *COPTS;
156static 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
185const char *
186get_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 */
205void
206makefile(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 */
352void
353read_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);
374openit:
375 fp = fopenp(VPATH, fname, pname, "r");
376 if (fp == 0) {
377 perror(fname);
378 exit(1);
379 }
380next:
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;
452nextopt:
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
551invis:
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
562doneopt:
563 if (nreqs == 0) {
564 printf("%s: what is %s optional on?\n",
565 fname, this);
566 exit(1);
567 }
568
569checkdev:
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
588save:
589getrest:
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
625int
626opteq(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
642void
643put_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
651void
652do_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
708cont:
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 */
717void
718do_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
747void
748do_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
778void
779do_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
805void
806build_confdep(FILE *fp)
807{
808 fprintf(fp, "#define MAXUSERS %d\n", maxusers);
809}
810
811/*
812 * Format cpu types file.
813 */
814
815void
816build_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
833void
834do_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 }
859copy:
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);
871same:
872 fclose(ofp);
873 fclose(tfp);
874}
875
876const char *
877tail(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 */
894void
895do_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 }
963 continue;
964 }
965 if (och == 'b') {
966 fprintf(f, "\t${B_RULE_1A}%s%.*s${B_RULE_1B}\n\n",
967 source_dir, (int)(tp-np), np);
968 continue;
969 }
970 extras = "";
971 switch (ftp->f_type) {
972
973 case NORMAL:
974 switch (machine) {
975
976 case MACHINE_MIPSY:
977 case MACHINE_MIPS:
978 fprintf(f, "\t@${RM} %so\n", tp);
979 fprintf(f, "\t${CC} ${CCNFLAGS}%s %s%s%sc\n\n",
980 (ftp->f_extra?ftp->f_extra:""), extras, source_dir, np);
981 continue;
982 #if 0
983 case MACHINE_SQT:
984 if (ftp->f_flags & SEDIT) {
985 fprintf(f, "\t${CC} -SO ${COPTS} %s%s%sc | \\\n", extras, source_dir, np);
986 fprintf(f, "\t${SEDCMD} | ${C2} | ${AS} ${CAFLAGS} -o %so\n\n", tp);
987 } else {
988 fprintf(f, "\t${CC} -c -O ${COPTS} %s%s%sc\n\n",
989 source_dir, extras, np);
990 }
991 break;
992 #endif 0
993 default:
994 goto common;
995 }
996 break;
997
998 case DRIVER:
999 switch (machine) {
1000
1001 case MACHINE_MIPSY:
1002 case MACHINE_MIPS:
1003 fprintf(f, "\t@${RM} %so\n", tp);
1004 fprintf(f, "\t${CC} ${CCDFLAGS}%s %s%s%sc\n\n",
1005 (ftp->f_extra?ftp->f_extra:""), extras, source_dir, np);
1006 continue;
1007 default:
1008 extras = "_D";
1009 goto common;
1010 }
1011 break;
1012
1013 case PROFILING:
1014 if (!profiling)
1015 continue;
1016 if (COPTS == 0) {
1017 fprintf(stderr,
1018 "config: COPTS undefined in generic makefile");
1019 COPTS = "";
1020 }
1021 switch (machine) {
1022 case MACHINE_MIPSY:
1023 case MACHINE_MIPS:
1024 fprintf(f, "\t@${RM} %so\n", tp);
1025 fprintf(f, "\t${CC} ${CCPFLAGS}%s %s../%sc\n\n",
1026 (ftp->f_extra?ftp->f_extra:""), extras, np);
1027 continue;
1028 case MACHINE_VAX:
1029 case MACHINE_ROMP:
1030 case MACHINE_SQT:
1031 case MACHINE_MMAX:
1032 case MACHINE_SUN3:
1033 case MACHINE_SUN4:
1034 case MACHINE_I386:
1035 case MACHINE_I860:
1036 case MACHINE_HPPA:
1037 case MACHINE_SPARC:
1038 case MACHINE_PPC:
1039 case MACHINE_ARM:
1040 case MACHINE_X86_64:
1041 extras = "_P";
1042 goto common;
1043 default:
1044 fprintf(stderr,
1045 "config: don't know how to profile kernel on this cpu\n");
1046 break;
1047 }
1048
1049 common:
1050 och_upper = och + 'A' - 'a';
1051 fprintf(f, "\t${%c_RULE_1A%s}", och_upper, extras);
1052 if (ftp->f_extra)
1053 fprintf(f, "%s", ftp->f_extra);
1054 fprintf(f, "%s%.*s${%c_RULE_1B%s}%s\n",
1055 source_dir, (int)(tp-np), np, och_upper, extras, nl);
1056 fprintf(f, "\t${%c_RULE_2%s}%s\n", och_upper, extras, nl);
1057 fprintf(f, "\t${%c_RULE_3%s}%s\n", och_upper, extras, nl);
1058 fprintf(f, "\t${%c_RULE_4%s}\n\n", och_upper, extras);
1059 break;
1060
1061 default:
1062 printf("Don't know rules for %s\n", np);
1063 break;
1064 }
1065 *cp = och;
1066 }
1067}
1068
1069/*
1070 * Create the load strings
1071 */
1072void
1073do_load(FILE *f)
1074{
1075 register struct file_list *fl;
1076 int first = 1;
1077
1078 fl = conf_list;
1079 while (fl) {
1080 if (fl->f_type != SYSTEMSPEC) {
1081 fl = fl->f_next;
1082 continue;
1083 }
1084 fl = do_systemspec(f, fl, first);
1085 if (first)
1086 first = 0;
1087 }
1088 fprintf(f, "LOAD =");
1089 for (fl = conf_list; fl != 0; fl = fl->f_next)
1090 if (fl->f_type == SYSTEMSPEC)
1091 fprintf(f, " %s", fl->f_needs);
1092#ifdef multimax
1093 fprintf(f, "\n\nall .ORDER: includelinks ${LOAD}\n");
1094#else multimax
1095 fprintf(f, "\n\nall: includelinks ${LOAD}\n");
1096#endif multimax
1097 fprintf(f, "\n");
1098}
1099
1100struct file_list *
1101do_systemspec(FILE *f, struct file_list *fl, __unused int first)
1102{
1103 /*
1104 * Variable for kernel name.
1105 */
1106 fprintf(f, "KERNEL_NAME=%s\n", fl->f_needs);
1107
1108 fprintf(f, "%s .ORDER: %s.sys ${SYSDEPS}\n",
1109 fl->f_needs, fl->f_needs);
1110 fprintf(f, "\t${SYS_RULE_1}\n");
1111 fprintf(f, "\t${SYS_RULE_2}\n");
1112 fprintf(f, "\t${SYS_RULE_3}\n");
1113 fprintf(f, "\t${SYS_RULE_4}\n\n");
1114 do_swapspec(f, fl->f_fn, fl->f_needs);
1115 for (fl = fl->f_next; fl != NULL && fl->f_type == SWAPSPEC; fl = fl->f_next)
1116 continue;
1117 return (fl);
1118}
1119
1120void
1121do_swapspec(__unused FILE *f, __unused const char *name, __unused char *sysname)
1122{
1123
1124#if DO_SWAPFILE
1125 char *gdir = eq(name, "generic")?"$(MACHINEDIR)/":"";
1126
1127 fprintf(f, "%s.sys:${P} ${PRELDDEPS} ${LDOBJS} ${LDDEPS}\n\n", sysname);
1128 fprintf(f, "%s.swap: swap%s.o\n", sysname, name);
1129 fprintf(f, "\t@rm -f $@\n");
1130 fprintf(f, "\t@cp swap%s.o $@\n\n", name);
1131 fprintf(f, "swap%s.o: %sswap%s.c ${SWAPDEPS}\n", name, gdir, name);
1132 if (machine == MACHINE_MIPSY || machine == MACHINE_MIPS) {
1133 fprintf(f, "\t@${RM} swap%s.o\n", name);
1134 fprintf(f, "\t${CC} ${CCNFLAGS} %sswap%s.c\n\n", gdir, name);
1135 } else {
1136 fprintf(f, "\t${C_RULE_1A}%s${C_RULE_1B}\n", gdir);
1137 fprintf(f, "\t${C_RULE_2}\n");
1138 fprintf(f, "\t${C_RULE_3}\n");
1139 fprintf(f, "\t${C_RULE_4}\n\n");
1140 }
1141#endif DO_SWAPFILE
1142}
1143
1144char *
1145allCaps(str)
1146 register char *str;
1147{
1148 register char *cp = str;
1149
1150 while (*str) {
1151 if (islower(*str))
1152 *str = toupper(*str);
1153 str++;
1154 }
1155 return (cp);
1156}
1157
1158#define OLDSALUTATION "# DO NOT DELETE THIS LINE"
1159
1160#define LINESIZE 1024
1161static char makbuf[LINESIZE]; /* one line buffer for makefile */
1162
1163void
1164copy_dependencies(FILE *makin, FILE *makout)
1165{
1166 register int oldlen = (sizeof OLDSALUTATION - 1);
1167
1168 while (fgets(makbuf, LINESIZE, makin) != NULL) {
1169 if (! strncmp(makbuf, OLDSALUTATION, oldlen))
1170 break;
1171 }
1172 while (fgets(makbuf, LINESIZE, makin) != NULL) {
1173 if (oldlen != 0)
1174 {
1175 if (makbuf[0] == '\n')
1176 continue;
1177 else
1178 oldlen = 0;
1179 }
1180 fputs(makbuf, makout);
1181 }
1182}