]>
Commit | Line | Data |
---|---|---|
6d2010ae | 1 | /* |
39037602 | 2 | * Copyright (c) 1999-2016 Apple Inc. All rights reserved. |
6d2010ae A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
39037602 | 5 | * |
6d2010ae A |
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. | |
39037602 | 13 | * |
6d2010ae A |
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." | |
39037602 | 21 | * |
6d2010ae A |
22 | * @APPLE_LICENSE_HEADER_END@ |
23 | */ | |
39037602 | 24 | /* |
6d2010ae A |
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> | |
0a7de745 | 62 | #include <unistd.h> /* for unlink */ |
6d2010ae A |
63 | #include <ctype.h> |
64 | #include "parser.h" | |
65 | #include "config.h" | |
66 | ||
0a7de745 | 67 | void read_files(void); |
a991bd8d | 68 | void do_objs(FILE *fp, const char *msg, int ext, int flags); |
0a7de745 A |
69 | void do_files(FILE *fp, const char *msg, char ext); |
70 | void do_machdep(FILE *ofp); | |
71 | void do_rules(FILE *f); | |
72 | void copy_dependencies(FILE *makin, FILE *makout); | |
6d2010ae | 73 | |
6d2010ae A |
74 | struct file_list *fl_lookup(char *file); |
75 | struct file_list *fltail_lookup(char *file); | |
76 | struct file_list *new_fent(void); | |
77 | ||
0a7de745 | 78 | void put_source_file_name(FILE *fp, struct file_list *tp); |
6d2010ae A |
79 | |
80 | ||
6d2010ae | 81 | #define next_word(fp, wd) \ |
39037602 | 82 | { const char *word = get_word(fp); \ |
6d2010ae | 83 | if (word == (char *)EOF) \ |
0a7de745 | 84 | return; \ |
6d2010ae | 85 | else \ |
0a7de745 | 86 | wd = word; \ |
6d2010ae A |
87 | } |
88 | ||
0a7de745 | 89 | static struct file_list *fcur; |
6d2010ae A |
90 | const char *tail(const char *fn); |
91 | char *allCaps(char *str); | |
92 | ||
93 | /* | |
94 | * Lookup a file, by name. | |
95 | */ | |
96 | struct file_list * | |
97 | fl_lookup(char *file) | |
98 | { | |
39037602 | 99 | struct file_list *fp; |
6d2010ae | 100 | |
0a7de745 A |
101 | for (fp = ftab; fp != 0; fp = fp->f_next) { |
102 | if (eq(fp->f_fn, file)) { | |
103 | return fp; | |
104 | } | |
6d2010ae | 105 | } |
0a7de745 | 106 | return 0; |
6d2010ae A |
107 | } |
108 | ||
109 | /* | |
110 | * Lookup a file, by final component name. | |
111 | */ | |
112 | struct file_list * | |
113 | fltail_lookup(char *file) | |
114 | { | |
39037602 | 115 | struct file_list *fp; |
6d2010ae | 116 | |
0a7de745 A |
117 | for (fp = ftab; fp != 0; fp = fp->f_next) { |
118 | if (eq(tail(fp->f_fn), tail(file))) { | |
119 | return fp; | |
120 | } | |
6d2010ae | 121 | } |
0a7de745 | 122 | return 0; |
6d2010ae A |
123 | } |
124 | ||
125 | /* | |
126 | * Make a new file list entry | |
127 | */ | |
128 | struct file_list * | |
129 | new_fent(void) | |
130 | { | |
39037602 | 131 | struct file_list *fp; |
6d2010ae A |
132 | |
133 | fp = (struct file_list *) malloc(sizeof *fp); | |
134 | fp->f_needs = 0; | |
135 | fp->f_next = 0; | |
136 | fp->f_flags = 0; | |
137 | fp->f_type = 0; | |
138 | fp->f_extra = (char *) 0; | |
0a7de745 | 139 | if (fcur == 0) { |
6d2010ae | 140 | fcur = ftab = fp; |
0a7de745 | 141 | } else { |
6d2010ae | 142 | fcur->f_next = fp; |
0a7de745 | 143 | } |
6d2010ae | 144 | fcur = fp; |
0a7de745 | 145 | return fp; |
6d2010ae A |
146 | } |
147 | ||
0a7de745 | 148 | char *COPTS; |
6d2010ae A |
149 | |
150 | const char * | |
151 | get_VPATH(void) | |
152 | { | |
0a7de745 | 153 | static char *vpath = NULL; |
6d2010ae | 154 | |
0a7de745 A |
155 | if ((vpath == NULL) && |
156 | ((vpath = getenv("VPATH")) != NULL) && | |
157 | (*vpath != ':')) { | |
158 | char *buf = malloc((unsigned)(strlen(vpath) + 2)); | |
6d2010ae | 159 | |
0a7de745 A |
160 | vpath = strcat(strcpy(buf, ":"), vpath); |
161 | } | |
6d2010ae | 162 | |
0a7de745 | 163 | return vpath ? vpath : ""; |
6d2010ae A |
164 | } |
165 | ||
166 | ||
167 | /* | |
168 | * Build the makefile from the skeleton | |
169 | */ | |
170 | void | |
171 | makefile(void) | |
172 | { | |
173 | FILE *ifp, *ofp; | |
174 | FILE *dfp; | |
175 | char pname[BUFSIZ]; | |
176 | char line[BUFSIZ]; | |
177 | struct opt *op; | |
6d2010ae A |
178 | |
179 | read_files(); | |
180 | (void) sprintf(line, "%s/Makefile.template", config_directory); | |
181 | ifp = fopenp(VPATH, line, pname, "r"); | |
182 | if (ifp == 0) { | |
183 | perror(line); | |
184 | exit(1); | |
185 | } | |
186 | dfp = fopen(path("Makefile"), "r"); | |
187 | rename(path("Makefile"), path("Makefile.old")); | |
188 | unlink(path("Makefile.old")); | |
6d2010ae A |
189 | ofp = fopen(path("Makefile"), "w"); |
190 | if (ofp == 0) { | |
191 | perror(path("Makefile")); | |
192 | exit(1); | |
193 | } | |
194 | fprintf(ofp, "SOURCE_DIR=%s\n", source_directory); | |
195 | ||
fe8ab488 | 196 | fprintf(ofp, "export CONFIG_DEFINES ="); |
0a7de745 | 197 | if (profiling) { |
6d2010ae | 198 | fprintf(ofp, " -DGPROF"); |
0a7de745 | 199 | } |
6d2010ae | 200 | |
0a7de745 A |
201 | for (op = opt; op; op = op->op_next) { |
202 | if (op->op_value) { | |
6d2010ae | 203 | fprintf(ofp, " -D%s=\"%s\"", op->op_name, op->op_value); |
0a7de745 | 204 | } else { |
6d2010ae | 205 | fprintf(ofp, " -D%s", op->op_name); |
0a7de745 A |
206 | } |
207 | } | |
6d2010ae | 208 | fprintf(ofp, "\n"); |
0a7de745 A |
209 | for (op = mkopt; op; op = op->op_next) { |
210 | if (op->op_value) { | |
6d2010ae | 211 | fprintf(ofp, "%s=%s\n", op->op_name, op->op_value); |
0a7de745 | 212 | } else { |
6d2010ae | 213 | fprintf(ofp, "%s\n", op->op_name); |
0a7de745 A |
214 | } |
215 | } | |
6d2010ae A |
216 | |
217 | while (fgets(line, BUFSIZ, ifp) != 0) { | |
0a7de745 | 218 | if (*line == '%') { |
6d2010ae | 219 | goto percent; |
0a7de745 | 220 | } |
6d2010ae | 221 | if (profiling && strncmp(line, "COPTS=", 6) == 0) { |
39037602 | 222 | char *cp; |
fe8ab488 | 223 | fprintf(ofp, |
0a7de745 | 224 | "GPROF.EX=$(SOURCE_DIR)/machdep/%s/gmon.ex\n", machinename); |
6d2010ae | 225 | cp = index(line, '\n'); |
0a7de745 | 226 | if (cp) { |
6d2010ae | 227 | *cp = 0; |
0a7de745 | 228 | } |
6d2010ae | 229 | cp = line + 6; |
0a7de745 | 230 | while (*cp && (*cp == ' ' || *cp == '\t')) { |
6d2010ae | 231 | cp++; |
0a7de745 | 232 | } |
6d2010ae A |
233 | COPTS = malloc((unsigned)(strlen(cp) + 1)); |
234 | if (COPTS == 0) { | |
235 | printf("config: out of memory\n"); | |
236 | exit(1); | |
237 | } | |
238 | strcpy(COPTS, cp); | |
fe8ab488 | 239 | fprintf(ofp, "%s -pg\n", line); |
6d2010ae A |
240 | continue; |
241 | } | |
242 | fprintf(ofp, "%s", line); | |
243 | continue; | |
0a7de745 | 244 | percent: |
6d2010ae | 245 | if (eq(line, "%OBJS\n")) { |
a991bd8d A |
246 | do_objs(ofp, "OBJS=", -1, 0); |
247 | } else if (eq(line, "%LIBOBJS\n")) { | |
248 | do_objs(ofp, "LIBOBJS=", -1, LIBRARYDEP); | |
6d2010ae A |
249 | } else if (eq(line, "%CFILES\n")) { |
250 | do_files(ofp, "CFILES=", 'c'); | |
a991bd8d | 251 | do_objs(ofp, "COBJS=", 'c', 0); |
fe8ab488 A |
252 | } else if (eq(line, "%CXXFILES\n")) { |
253 | do_files(ofp, "CXXFILES=", 'p'); | |
a991bd8d | 254 | do_objs(ofp, "CXXOBJS=", 'p', 0); |
6d2010ae A |
255 | } else if (eq(line, "%SFILES\n")) { |
256 | do_files(ofp, "SFILES=", 's'); | |
a991bd8d | 257 | do_objs(ofp, "SOBJS=", 's', 0); |
39236c6e | 258 | } else if (eq(line, "%MACHDEP\n")) { |
6d2010ae | 259 | do_machdep(ofp); |
0a7de745 | 260 | } else if (eq(line, "%RULES\n")) { |
6d2010ae | 261 | do_rules(ofp); |
0a7de745 | 262 | } else { |
6d2010ae A |
263 | fprintf(stderr, |
264 | "Unknown %% construct in generic makefile: %s", | |
265 | line); | |
0a7de745 | 266 | } |
6d2010ae | 267 | } |
0a7de745 | 268 | if (dfp != NULL) { |
6d2010ae A |
269 | copy_dependencies(dfp, ofp); |
270 | (void) fclose(dfp); | |
271 | } | |
272 | (void) fclose(ifp); | |
273 | (void) fclose(ofp); | |
274 | } | |
275 | ||
276 | /* | |
277 | * Read in the information about files used in making the system. | |
278 | * Store it in the ftab linked list. | |
279 | */ | |
280 | void | |
281 | read_files(void) | |
282 | { | |
283 | FILE *fp; | |
39037602 A |
284 | struct file_list *tp, *pf; |
285 | struct device *dp; | |
286 | struct opt *op; | |
6d2010ae A |
287 | const char *wd; |
288 | char *this, *needs; | |
289 | const char *devorprof; | |
290 | int options; | |
291 | int not_option; | |
a991bd8d | 292 | int for_xnu_lib; |
6d2010ae A |
293 | char pname[BUFSIZ]; |
294 | char fname[1024]; | |
295 | char *rest = (char *) 0; | |
6d2010ae A |
296 | int nreqs, first = 1, isdup; |
297 | ||
298 | ftab = 0; | |
299 | (void) sprintf(fname, "%s/files", config_directory); | |
300 | openit: | |
301 | fp = fopenp(VPATH, fname, pname, "r"); | |
302 | if (fp == 0) { | |
303 | perror(fname); | |
304 | exit(1); | |
305 | } | |
306 | next: | |
307 | options = 0; | |
308 | rest = (char *) 0; | |
309 | /* | |
310 | * filename [ standard | optional ] | |
311 | * [ dev* | profiling-routine ] [ device-driver] | |
312 | */ | |
6d2010ae A |
313 | wd = get_word(fp); |
314 | if (wd == (char *)EOF) { | |
315 | (void) fclose(fp); | |
316 | if (first == 1) { | |
317 | (void) sprintf(fname, "%s/files.%s", config_directory, machinename); | |
318 | first++; | |
319 | goto openit; | |
320 | } | |
6d2010ae A |
321 | return; |
322 | } | |
0a7de745 | 323 | if (wd == 0) { |
6d2010ae | 324 | goto next; |
0a7de745 | 325 | } |
6d2010ae A |
326 | /* |
327 | * Allow comment lines beginning witha '#' character. | |
328 | */ | |
0a7de745 A |
329 | if (*wd == '#') { |
330 | while ((wd = get_word(fp)) && wd != (char *)EOF) { | |
6d2010ae | 331 | ; |
0a7de745 | 332 | } |
6d2010ae A |
333 | goto next; |
334 | } | |
335 | ||
336 | this = ns(wd); | |
337 | next_word(fp, wd); | |
338 | if (wd == 0) { | |
339 | printf("%s: No type for %s.\n", | |
340 | fname, this); | |
341 | exit(1); | |
342 | } | |
0a7de745 | 343 | if ((pf = fl_lookup(this)) && (pf->f_type != INVISIBLE || pf->f_flags)) { |
6d2010ae | 344 | isdup = 1; |
0a7de745 | 345 | } else { |
6d2010ae | 346 | isdup = 0; |
0a7de745 | 347 | } |
6d2010ae | 348 | tp = 0; |
6d2010ae A |
349 | nreqs = 0; |
350 | devorprof = ""; | |
6d2010ae | 351 | needs = 0; |
a991bd8d | 352 | for_xnu_lib = 0; |
0a7de745 | 353 | if (eq(wd, "standard")) { |
6d2010ae | 354 | goto checkdev; |
0a7de745 | 355 | } |
6d2010ae A |
356 | if (!eq(wd, "optional")) { |
357 | printf("%s: %s must be optional or standard\n", fname, this); | |
358 | exit(1); | |
359 | } | |
0a7de745 | 360 | if (strncmp(this, "OPTIONS/", 8) == 0) { |
6d2010ae | 361 | options++; |
0a7de745 | 362 | } |
6d2010ae A |
363 | not_option = 0; |
364 | nextopt: | |
365 | next_word(fp, wd); | |
0a7de745 | 366 | if (wd == 0) { |
6d2010ae | 367 | goto doneopt; |
0a7de745 | 368 | } |
6d2010ae A |
369 | if (eq(wd, "not")) { |
370 | not_option = !not_option; | |
371 | goto nextopt; | |
372 | } | |
373 | devorprof = wd; | |
374 | if (eq(wd, "device-driver") || eq(wd, "profiling-routine")) { | |
375 | next_word(fp, wd); | |
376 | goto save; | |
377 | } | |
a991bd8d A |
378 | if (eq(wd, "xnu-library")) { |
379 | for_xnu_lib = 1; | |
380 | goto nextopt; | |
381 | } | |
6d2010ae | 382 | nreqs++; |
0a7de745 | 383 | if (needs == 0 && nreqs == 1) { |
6d2010ae | 384 | needs = ns(wd); |
0a7de745 A |
385 | } |
386 | if (isdup) { | |
6d2010ae | 387 | goto invis; |
0a7de745 A |
388 | } |
389 | if (options) { | |
6d2010ae A |
390 | struct opt *lop = 0; |
391 | struct device tdev; | |
392 | ||
393 | /* | |
394 | * Allocate a pseudo-device entry which we will insert into | |
395 | * the device list below. The flags field is set non-zero to | |
396 | * indicate an internal entry rather than one generated from | |
397 | * the configuration file. The slave field is set to define | |
398 | * the corresponding symbol as 0 should we fail to find the | |
399 | * option in the option list. | |
400 | */ | |
401 | init_dev(&tdev); | |
402 | tdev.d_name = ns(wd); | |
403 | tdev.d_type = PSEUDO_DEVICE; | |
404 | tdev.d_flags++; | |
405 | tdev.d_slave = 0; | |
406 | ||
0a7de745 | 407 | for (op = opt; op; lop = op, op = op->op_next) { |
6d2010ae A |
408 | char *od = allCaps(ns(wd)); |
409 | ||
410 | /* | |
411 | * Found an option which matches the current device | |
412 | * dependency identifier. Set the slave field to | |
413 | * define the option in the header file. | |
414 | */ | |
0a7de745 | 415 | if (strcmp(op->op_name, od) == 0) { |
6d2010ae | 416 | tdev.d_slave = 1; |
0a7de745 | 417 | if (lop == 0) { |
6d2010ae | 418 | opt = op->op_next; |
0a7de745 | 419 | } else { |
6d2010ae | 420 | lop->op_next = op->op_next; |
0a7de745 | 421 | } |
6d2010ae A |
422 | free(op); |
423 | op = 0; | |
0a7de745 | 424 | } |
6d2010ae | 425 | free(od); |
0a7de745 | 426 | if (op == 0) { |
6d2010ae | 427 | break; |
0a7de745 | 428 | } |
6d2010ae A |
429 | } |
430 | newdev(&tdev); | |
431 | } | |
0a7de745 | 432 | for (dp = dtab; dp != 0; dp = dp->d_next) { |
6d2010ae | 433 | if (eq(dp->d_name, wd) && (dp->d_type != PSEUDO_DEVICE || dp->d_slave)) { |
0a7de745 A |
434 | if (not_option) { |
435 | goto invis; /* dont want file if option present */ | |
436 | } else { | |
6d2010ae | 437 | goto nextopt; |
0a7de745 | 438 | } |
6d2010ae A |
439 | } |
440 | } | |
0a7de745 A |
441 | if (not_option) { |
442 | goto nextopt; /* want file if option missing */ | |
443 | } | |
444 | for (op = opt; op != 0; op = op->op_next) { | |
6d2010ae A |
445 | if (op->op_value == 0 && opteq(op->op_name, wd)) { |
446 | if (nreqs == 1) { | |
447 | free(needs); | |
448 | needs = 0; | |
449 | } | |
450 | goto nextopt; | |
451 | } | |
0a7de745 | 452 | } |
6d2010ae | 453 | |
6d2010ae | 454 | invis: |
0a7de745 | 455 | while ((wd = get_word(fp)) != 0) { |
6d2010ae | 456 | ; |
0a7de745 A |
457 | } |
458 | if (tp == 0) { | |
6d2010ae | 459 | tp = new_fent(); |
0a7de745 | 460 | } |
6d2010ae A |
461 | tp->f_fn = this; |
462 | tp->f_type = INVISIBLE; | |
463 | tp->f_needs = needs; | |
464 | tp->f_flags = isdup; | |
465 | goto next; | |
466 | ||
467 | doneopt: | |
468 | if (nreqs == 0) { | |
469 | printf("%s: what is %s optional on?\n", | |
470 | fname, this); | |
471 | exit(1); | |
472 | } | |
473 | ||
474 | checkdev: | |
475 | if (wd) { | |
0a7de745 | 476 | if (*wd == '|') { |
6d2010ae | 477 | goto getrest; |
0a7de745 | 478 | } |
6d2010ae | 479 | next_word(fp, wd); |
a991bd8d A |
480 | if (wd && eq(wd, "xnu-library")) { |
481 | for_xnu_lib = 1; | |
482 | next_word(fp, wd); | |
483 | } | |
6d2010ae | 484 | if (wd) { |
6d2010ae A |
485 | devorprof = wd; |
486 | next_word(fp, wd); | |
487 | } | |
488 | } | |
489 | ||
490 | save: | |
491 | getrest: | |
492 | if (wd) { | |
493 | if (*wd == '|') { | |
494 | rest = ns(get_rest(fp)); | |
495 | } else { | |
496 | printf("%s: syntax error describing %s\n", | |
0a7de745 | 497 | fname, this); |
6d2010ae A |
498 | exit(1); |
499 | } | |
500 | } | |
0a7de745 | 501 | if (eq(devorprof, "profiling-routine") && profiling == 0) { |
6d2010ae | 502 | goto next; |
0a7de745 A |
503 | } |
504 | if (tp == 0) { | |
6d2010ae | 505 | tp = new_fent(); |
0a7de745 | 506 | } |
6d2010ae A |
507 | tp->f_fn = this; |
508 | tp->f_extra = rest; | |
0a7de745 | 509 | if (options) { |
6d2010ae | 510 | tp->f_type = INVISIBLE; |
0a7de745 | 511 | } else if (eq(devorprof, "device-driver")) { |
6d2010ae | 512 | tp->f_type = DRIVER; |
0a7de745 | 513 | } else if (eq(devorprof, "profiling-routine")) { |
6d2010ae | 514 | tp->f_type = PROFILING; |
0a7de745 | 515 | } else { |
6d2010ae | 516 | tp->f_type = NORMAL; |
0a7de745 | 517 | } |
6d2010ae | 518 | tp->f_flags = 0; |
6d2010ae | 519 | tp->f_needs = needs; |
0a7de745 A |
520 | if (pf && pf->f_type == INVISIBLE) { |
521 | pf->f_flags = 1; /* mark as duplicate */ | |
522 | } | |
a991bd8d A |
523 | if (for_xnu_lib) { |
524 | tp->f_flags |= LIBRARYDEP; | |
525 | } | |
6d2010ae A |
526 | goto next; |
527 | } | |
528 | ||
529 | int | |
530 | opteq(const char *cp, const char *dp) | |
531 | { | |
532 | char c, d; | |
533 | ||
0a7de745 | 534 | for (;; cp++, dp++) { |
6d2010ae A |
535 | if (*cp != *dp) { |
536 | c = isupper(*cp) ? tolower(*cp) : *cp; | |
537 | d = isupper(*dp) ? tolower(*dp) : *dp; | |
0a7de745 A |
538 | if (c != d) { |
539 | return 0; | |
540 | } | |
541 | } | |
542 | if (*cp == 0) { | |
543 | return 1; | |
6d2010ae | 544 | } |
6d2010ae A |
545 | } |
546 | } | |
547 | ||
548 | void | |
549 | put_source_file_name(FILE *fp, struct file_list *tp) | |
550 | { | |
0a7de745 | 551 | if ((tp->f_fn[0] == '.') && (tp->f_fn[1] == '/')) { |
6d2010ae | 552 | fprintf(fp, "%s ", tp->f_fn); |
0a7de745 | 553 | } else { |
6d2010ae | 554 | fprintf(fp, "$(SOURCE_DIR)/%s ", tp->f_fn); |
0a7de745 | 555 | } |
6d2010ae A |
556 | } |
557 | ||
558 | void | |
a991bd8d | 559 | do_objs(FILE *fp, const char *msg, int ext, int flags) |
6d2010ae | 560 | { |
39037602 A |
561 | struct file_list *tp; |
562 | int lpos, len; | |
6d2010ae A |
563 | char *cp; |
564 | char och; | |
565 | const char *sp; | |
6d2010ae A |
566 | |
567 | fprintf(fp, "%s", msg); | |
568 | lpos = strlen(msg); | |
569 | for (tp = ftab; tp != 0; tp = tp->f_next) { | |
0a7de745 | 570 | if (tp->f_type == INVISIBLE) { |
6d2010ae | 571 | continue; |
0a7de745 | 572 | } |
6d2010ae | 573 | |
a991bd8d A |
574 | /* |
575 | * Check flags (if any) | |
576 | */ | |
577 | if (flags && ((tp->f_flags & flags) != flags)) { | |
578 | continue; | |
579 | } | |
580 | ||
6d2010ae A |
581 | /* |
582 | * Check for '.o' file in list | |
583 | */ | |
584 | cp = tp->f_fn + (len = strlen(tp->f_fn)) - 1; | |
0a7de745 | 585 | if (ext != -1 && *cp != ext) { |
6d2010ae | 586 | continue; |
0a7de745 | 587 | } else if (*cp == 'o') { |
6d2010ae A |
588 | if (len + lpos > 72) { |
589 | lpos = 8; | |
590 | fprintf(fp, "\\\n\t"); | |
591 | } | |
592 | put_source_file_name(fp, tp); | |
593 | fprintf(fp, " "); | |
594 | lpos += len + 1; | |
595 | continue; | |
596 | } | |
597 | sp = tail(tp->f_fn); | |
6d2010ae A |
598 | cp = (char *)sp + (len = strlen(sp)) - 1; |
599 | och = *cp; | |
600 | *cp = 'o'; | |
601 | if (len + lpos > 72) { | |
602 | lpos = 8; | |
603 | fprintf(fp, "\\\n\t"); | |
604 | } | |
605 | fprintf(fp, "%s ", sp); | |
606 | lpos += len + 1; | |
607 | *cp = och; | |
6d2010ae | 608 | } |
fe8ab488 | 609 | putc('\n', fp); |
6d2010ae A |
610 | } |
611 | ||
612 | void | |
613 | do_files(FILE *fp, const char *msg, char ext) | |
614 | { | |
39037602 | 615 | struct file_list *tp; |
0a7de745 | 616 | int lpos, len = 0; /* dvw: init to 0 */ |
6d2010ae A |
617 | |
618 | fprintf(fp, "%s", msg); | |
619 | lpos = 8; | |
620 | for (tp = ftab; tp != 0; tp = tp->f_next) { | |
0a7de745 | 621 | if (tp->f_type == INVISIBLE) { |
6d2010ae | 622 | continue; |
0a7de745 A |
623 | } |
624 | if (tp->f_fn[strlen(tp->f_fn) - 1] != ext) { | |
6d2010ae | 625 | continue; |
0a7de745 | 626 | } |
6d2010ae A |
627 | /* |
628 | * Always generate a newline. | |
629 | * Our Makefile's aren't readable anyway. | |
630 | */ | |
631 | ||
632 | lpos = 8; | |
633 | fprintf(fp, "\\\n\t"); | |
634 | put_source_file_name(fp, tp); | |
635 | lpos += len + 1; | |
636 | } | |
fe8ab488 | 637 | putc('\n', fp); |
6d2010ae A |
638 | } |
639 | ||
640 | /* | |
641 | * Include machine dependent makefile in output | |
642 | */ | |
643 | ||
644 | void | |
645 | do_machdep(FILE *ofp) | |
646 | { | |
647 | FILE *ifp; | |
648 | char pname[BUFSIZ]; | |
649 | char line[BUFSIZ]; | |
650 | ||
651 | (void) sprintf(line, "%s/Makefile.%s", config_directory, machinename); | |
652 | ifp = fopenp(VPATH, line, pname, "r"); | |
653 | if (ifp == 0) { | |
654 | perror(line); | |
655 | exit(1); | |
656 | } | |
657 | while (fgets(line, BUFSIZ, ifp) != 0) { | |
0a7de745 A |
658 | if (profiling && (strncmp(line, "LIBS=", 5) == 0)) { |
659 | fprintf(ofp, "LIBS=${LIBS_P}\n"); | |
660 | } else { | |
6d2010ae | 661 | fputs(line, ofp); |
0a7de745 | 662 | } |
6d2010ae A |
663 | } |
664 | fclose(ifp); | |
665 | } | |
666 | ||
6d2010ae A |
667 | const char * |
668 | tail(const char *fn) | |
669 | { | |
39037602 | 670 | const char *cp; |
6d2010ae A |
671 | |
672 | cp = rindex(fn, '/'); | |
0a7de745 A |
673 | if (cp == 0) { |
674 | return fn; | |
675 | } | |
676 | return cp + 1; | |
6d2010ae A |
677 | } |
678 | ||
679 | /* | |
680 | * Create the makerules for each file | |
681 | * which is part of the system. | |
682 | * Devices are processed with the special c2 option -i | |
683 | * which avoids any problem areas with i/o addressing | |
684 | * (e.g. for the VAX); assembler files are processed by as. | |
685 | */ | |
686 | void | |
687 | do_rules(FILE *f) | |
688 | { | |
689 | char *cp; | |
690 | char *np, och; | |
691 | const char *tp; | |
39037602 | 692 | struct file_list *ftp; |
6d2010ae A |
693 | const char *extras = ""; /* dvw: init to "" */ |
694 | char *source_dir; | |
695 | char och_upper; | |
696 | const char *nl = ""; | |
697 | ||
698 | for (ftp = ftab; ftp != 0; ftp = ftp->f_next) { | |
0a7de745 | 699 | if (ftp->f_type == INVISIBLE) { |
6d2010ae | 700 | continue; |
0a7de745 | 701 | } |
6d2010ae A |
702 | cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1; |
703 | och = *cp; | |
704 | /* | |
0a7de745 A |
705 | * Don't compile '.o' files |
706 | */ | |
707 | if (och == 'o') { | |
6d2010ae | 708 | continue; |
0a7de745 | 709 | } |
6d2010ae | 710 | /* |
0a7de745 A |
711 | * Determine where sources should come from |
712 | */ | |
6d2010ae A |
713 | if ((np[0] == '.') && (np[1] == '/')) { |
714 | source_dir = ""; | |
715 | np += 2; | |
0a7de745 | 716 | } else { |
6d2010ae | 717 | source_dir = "$(SOURCE_DIR)/"; |
0a7de745 | 718 | } |
6d2010ae | 719 | *cp = '\0'; |
0a7de745 | 720 | tp = tail(np); /* dvw: init tp before 'if' */ |
39236c6e | 721 | fprintf(f, "-include %sd\n", tp); |
6d2010ae A |
722 | fprintf(f, "%so: %s%s%c\n", tp, source_dir, np, och); |
723 | if (och == 's') { | |
fe8ab488 A |
724 | fprintf(f, "\t${S_RULE_0}\n"); |
725 | fprintf(f, "\t${S_RULE_1A}%s%.*s${S_RULE_1B}%s\n", | |
0a7de745 | 726 | source_dir, (int)(tp - np), np, nl); |
fe8ab488 | 727 | fprintf(f, "\t${S_RULE_2}%s\n", nl); |
6d2010ae A |
728 | continue; |
729 | } | |
6d2010ae A |
730 | extras = ""; |
731 | switch (ftp->f_type) { | |
6d2010ae | 732 | case NORMAL: |
fe8ab488 | 733 | goto common; |
6d2010ae | 734 | break; |
0a7de745 | 735 | |
6d2010ae | 736 | case DRIVER: |
fe8ab488 A |
737 | extras = "_D"; |
738 | goto common; | |
6d2010ae | 739 | break; |
0a7de745 | 740 | |
6d2010ae | 741 | case PROFILING: |
0a7de745 | 742 | if (!profiling) { |
6d2010ae | 743 | continue; |
0a7de745 | 744 | } |
6d2010ae A |
745 | if (COPTS == 0) { |
746 | fprintf(stderr, | |
0a7de745 | 747 | "config: COPTS undefined in generic makefile"); |
6d2010ae A |
748 | COPTS = ""; |
749 | } | |
fe8ab488 A |
750 | extras = "_P"; |
751 | goto common; | |
0a7de745 A |
752 | |
753 | common: | |
6d2010ae | 754 | och_upper = och + 'A' - 'a'; |
39236c6e | 755 | fprintf(f, "\t${%c_RULE_0%s}\n", och_upper, extras); |
6d2010ae | 756 | fprintf(f, "\t${%c_RULE_1A%s}", och_upper, extras); |
0a7de745 | 757 | if (ftp->f_extra) { |
6d2010ae | 758 | fprintf(f, "%s", ftp->f_extra); |
0a7de745 | 759 | } |
6d2010ae | 760 | fprintf(f, "%s%.*s${%c_RULE_1B%s}%s\n", |
0a7de745 | 761 | source_dir, (int)(tp - np), np, och_upper, extras, nl); |
39236c6e | 762 | |
6d2010ae | 763 | fprintf(f, "\t${%c_RULE_2%s}%s\n", och_upper, extras, nl); |
39037602 | 764 | fprintf(f, "\t${%c_RULE_3%s}%s\n", och_upper, extras, nl); |
cb323159 A |
765 | fprintf(f, "\t$(if ${%c_RULE_4A%s},${%c_RULE_4A%s}", |
766 | och_upper, extras, och_upper, extras); | |
0a7de745 | 767 | if (ftp->f_extra) { |
39037602 | 768 | fprintf(f, "%s", ftp->f_extra); |
0a7de745 | 769 | } |
cb323159 | 770 | fprintf(f, "%s%.*s${%c_RULE_4B%s}%s)\n", |
0a7de745 | 771 | source_dir, (int)(tp - np), np, och_upper, extras, nl); |
6d2010ae | 772 | break; |
0a7de745 | 773 | |
6d2010ae A |
774 | default: |
775 | printf("Don't know rules for %s\n", np); | |
776 | break; | |
777 | } | |
778 | *cp = och; | |
779 | } | |
780 | } | |
781 | ||
6d2010ae | 782 | char * |
39037602 | 783 | allCaps(char *str) |
6d2010ae | 784 | { |
39037602 | 785 | char *cp = str; |
6d2010ae A |
786 | |
787 | while (*str) { | |
0a7de745 | 788 | if (islower(*str)) { |
6d2010ae | 789 | *str = toupper(*str); |
0a7de745 | 790 | } |
6d2010ae A |
791 | str++; |
792 | } | |
0a7de745 | 793 | return cp; |
6d2010ae A |
794 | } |
795 | ||
796 | #define OLDSALUTATION "# DO NOT DELETE THIS LINE" | |
797 | ||
798 | #define LINESIZE 1024 | |
0a7de745 | 799 | static char makbuf[LINESIZE]; /* one line buffer for makefile */ |
6d2010ae A |
800 | |
801 | void | |
802 | copy_dependencies(FILE *makin, FILE *makout) | |
803 | { | |
39037602 | 804 | int oldlen = (sizeof OLDSALUTATION - 1); |
6d2010ae A |
805 | |
806 | while (fgets(makbuf, LINESIZE, makin) != NULL) { | |
0a7de745 | 807 | if (!strncmp(makbuf, OLDSALUTATION, oldlen)) { |
6d2010ae | 808 | break; |
0a7de745 | 809 | } |
6d2010ae A |
810 | } |
811 | while (fgets(makbuf, LINESIZE, makin) != NULL) { | |
0a7de745 A |
812 | if (oldlen != 0) { |
813 | if (makbuf[0] == '\n') { | |
6d2010ae | 814 | continue; |
0a7de745 | 815 | } else { |
6d2010ae | 816 | oldlen = 0; |
0a7de745 | 817 | } |
6d2010ae A |
818 | } |
819 | fputs(makbuf, makout); | |
820 | } | |
821 | } |