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