1 /* 2 * mkmakefile.c 1.10 81/05/18 3 * Functions in this file build the makefile from the files list 4 * and the information in the config table 5 */ 6 7 #include <stdio.h> 8 #include <ctype.h> 9 #include "y.tab.h" 10 #include "config.h" 11 12 #define next_word(fp, wd)\ 13 { register char *word = get_word(fp);\ 14 if (word == EOF) return EOF; \ 15 else wd = word; } 16 17 static struct file_list *fcur; 18 19 /* 20 * fl_lookup 21 * look up a file name 22 */ 23 24 struct file_list *fl_lookup(file) 25 register char *file; 26 { 27 register struct file_list *fp; 28 29 for (fp = ftab ; fp != NULL; fp = fp->f_next) 30 { 31 if (eq(fp->f_fn, file)) 32 return fp; 33 } 34 return NULL; 35 } 36 37 /* 38 * new_fent 39 * Make a new file list entry 40 */ 41 42 struct file_list *new_fent() 43 { 44 register struct file_list *fp; 45 46 fp = (struct file_list *) malloc(sizeof *fp); 47 fp->f_needs = fp->f_next = NULL; 48 if (fcur == NULL) 49 fcur = ftab = fp; 50 else 51 fcur->f_next = fp; 52 fcur = fp; 53 return fp; 54 } 55 56 /* 57 * makefile: 58 * Build the makefile from the skeleton 59 */ 60 61 makefile() 62 { 63 FILE *ifp, *ofp; 64 char line[BUFSIZ]; 65 struct cputype *cp; 66 struct opt *op; 67 68 read_files(); /* Read in the "files" file */ 69 ifp = fopen("../conf/makefile", "r"); 70 if (ifp == NULL) { 71 perror("../conf/makefile"); 72 exit(1); 73 } 74 ofp = fopen(path("makefile"), "w"); 75 if (ofp == NULL) { 76 perror(path("makefile")); 77 exit(1); 78 } 79 fprintf(ofp, "IDENT=-D%s", raise(ident)); 80 if (cputype == NULL) { 81 printf("cpu type must be specified\n"); 82 exit(1); 83 } 84 for (cp = cputype; cp; cp = cp->cpu_next) 85 fprintf(ofp, " -D%s", cp->cpu_name); 86 for (op = opt; op; op = op->op_next) 87 fprintf(ofp, " -D%s", op->op_name); 88 fprintf(ofp, "\n"); 89 if (hz == 0) { 90 #ifdef notdef 91 printf("hz not specified; 50hz assumed\n"); 92 #endif 93 hz = 60; 94 } 95 if (hadtz == 0) 96 printf("timezone not specified; gmt assumed\n"); 97 if (maxusers == 0) { 98 printf("maxusers not specified; 24 assumed\n"); 99 maxusers = 24; 100 } else if (maxusers < 8) { 101 printf("minimum of 8 maxusers assumed\n"); 102 maxusers = 8; 103 } else if (maxusers > 128) { 104 printf("maxusers truncated to 128\n"); 105 maxusers = 128; 106 } 107 fprintf(ofp, "PARAM=-DHZ=%d -DTIMEZONE=%d -DDST=%d -DMAXUSERS=%d\n", 108 hz, timezone, dst, maxusers); 109 while(fgets(line, BUFSIZ, ifp) != NULL) 110 { 111 if (*line != '%') 112 { 113 fprintf(ofp, "%s", line); 114 continue; 115 } 116 else if (eq(line, "%OBJS\n")) 117 do_objs(ofp); 118 else if (eq(line, "%CFILES\n")) 119 do_cfiles(ofp); 120 else if (eq(line, "%RULES\n")) 121 do_rules(ofp); 122 else if (eq(line, "%LOAD\n")) 123 do_load(ofp); 124 else 125 fprintf(stderr, "Unknown %% construct in generic makefile: %s", line); 126 } 127 fclose(ifp); 128 fclose(ofp); 129 } 130 131 /* 132 * files: 133 * Read in the "files" file. 134 * Store it in the ftab linked list 135 */ 136 137 read_files() 138 { 139 FILE *fp; 140 register struct file_list *tp; 141 register struct device *dp; 142 register char *wd, *this; 143 int type; 144 145 fp = fopen("../conf/files", "r"); 146 if (fp == NULL) { 147 perror("../conf/files"); 148 exit(1); 149 } 150 ftab = NULL; 151 while((wd = get_word(fp)) != EOF) 152 { 153 if (wd == NULL) 154 continue; 155 this = ns(wd); 156 /* 157 * Readad standard/optional 158 */ 159 next_word(fp, wd); 160 if (wd == NULL) 161 { 162 fprintf(stderr, "Huh, no type for %s in files.\n", this); 163 exit(10); 164 } 165 if ((tp = fl_lookup(wd)) == NULL) 166 tp = new_fent(); 167 else 168 free(tp->f_fn); 169 tp->f_fn = this; 170 type = 0; 171 if (eq(wd, "optional")) 172 { 173 next_word(fp, wd); 174 if (wd == NULL) 175 { 176 fprintf(stderr, "Needed a dev for optional(%s)\n", this); 177 exit(11); 178 } 179 tp->f_needs = ns(wd); 180 for (dp = dtab ; dp != NULL; dp = dp->d_next) 181 { 182 if (eq(dp->d_name, wd)) 183 break; 184 } 185 if (dp == NULL) 186 type = INVISIBLE; 187 } 188 next_word(fp, wd); 189 if (type == 0 && wd != NULL) 190 type = DEVICE; 191 else if (type == 0) 192 type = NORMAL; 193 tp->f_type = type; 194 } 195 fclose(fp); 196 } 197 198 /* 199 * do_objs 200 * Spew forth the OBJS definition 201 */ 202 203 do_objs(fp) 204 FILE *fp; 205 { 206 register struct file_list *tp; 207 register int lpos, len; 208 register char *cp, och, *sp; 209 char *tail(); 210 211 fprintf(fp, "OBJS="); 212 lpos = 6; 213 for (tp = ftab; tp != NULL; tp = tp->f_next) 214 { 215 if (tp->f_type == INVISIBLE) 216 continue; 217 sp = tail(tp->f_fn); 218 cp = sp + (len = strlen(sp)) - 1; 219 och = *cp; 220 *cp = 'o'; 221 if (len + lpos > 72) 222 { 223 lpos = 8; 224 fprintf(fp, "\\\n\t"); 225 } 226 fprintf(fp, "%s ", sp); 227 lpos += len + 1; 228 *cp = och; 229 } 230 if (lpos != 8) 231 putc('\n', fp); 232 } 233 234 /* 235 * do_cfiles 236 * Spew forth the CFILES definition 237 */ 238 239 do_cfiles(fp) 240 FILE *fp; 241 { 242 register struct file_list *tp; 243 register int lpos, len; 244 245 fprintf(fp, "CFILES="); 246 lpos = 8; 247 for (tp = ftab; tp != NULL; tp = tp->f_next) 248 { 249 if (tp->f_type == INVISIBLE) 250 continue; 251 if (tp->f_fn[strlen(tp->f_fn)-1] != 'c') 252 continue; 253 if ((len = 3 + strlen(tp->f_fn)) + lpos > 72) 254 { 255 lpos = 8; 256 fprintf(fp, "\\\n\t"); 257 } 258 fprintf(fp, "../%s ", tp->f_fn); 259 lpos += len + 1; 260 } 261 if (lpos != 8) 262 putc('\n', fp); 263 } 264 265 /* 266 * tail: 267 * Return tail end of a filename 268 */ 269 270 char *tail(fn) 271 char *fn; 272 { 273 register char *cp; 274 275 cp = rindex(fn, '/'); 276 return cp+1; 277 } 278 279 /* 280 * do_rules: 281 * Spit out the rules for making each file 282 */ 283 284 do_rules(f) 285 FILE *f; 286 { 287 register char *cp, *np, och, *tp; 288 register struct file_list *ftp; 289 290 for (ftp = ftab; ftp != NULL; ftp = ftp->f_next) 291 { 292 if (ftp->f_type == INVISIBLE) 293 continue; 294 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1; 295 och = *cp; 296 *cp = '\0'; 297 fprintf(f, "%so: ../%s%c\n", tail(np), np, och); 298 tp = tail(np); 299 if (och == 's') 300 fprintf(f, "\t${AS} -o %so ../%ss\n\n", tp, np); 301 else if (ftp->f_type == NORMAL) 302 { 303 fprintf(f, "\t${CC} -I. -c -S ${COPTS} ../%sc\n", np); 304 fprintf(f, "\t${C2} %ss | sed -f ../sys/asm.sed | ${AS} -o %so\n", 305 tp, tp); 306 fprintf(f, "\trm -f %ss\n\n", tp); 307 } 308 else if (ftp->f_type == DEVICE) 309 { 310 fprintf(f, "\t${CC} -I. -c -S ${COPTS} ../%sc\n", np); 311 fprintf(f,"\t${C2} -i %ss | sed -f ../sys/asm.sed | ${AS} -o %so\n", 312 tp, tp); 313 fprintf(f, "\trm -f %ss\n\n", tp); 314 } 315 else 316 fprintf(stderr, "Don't know rules for %s", np); 317 *cp = och; 318 } 319 } 320 321 /* 322 * Create the load strings 323 */ 324 325 do_load(f) 326 register FILE *f; 327 { 328 register struct file_list *fl; 329 register bool first = TRUE; 330 331 for (fl = conf_list; fl != NULL; fl = fl->f_next) 332 { 333 fprintf(f, "%s: makefile locore.o ${OBJS} ioconf.o param.o swap%s.o\n", 334 fl->f_needs, fl->f_fn); 335 fprintf(f, "\t@echo loading %s\n\t@rm -f %s\n", 336 fl->f_needs, fl->f_needs); 337 if (first) 338 { 339 first = FALSE; 340 fprintf(f, "\t@sh ../conf/newvers.sh\n"); 341 fprintf(f, "\t@cc $(CFLAGS) -c vers.c\n"); 342 } 343 fprintf(f, 344 "\t@ld -n -o %s -e start -x -T 80000000 locore.o ${OBJS} vers.o ioconf.o param.o swap%s.o\n", 345 fl->f_needs, fl->f_fn); 346 fprintf(f, "\t@echo rearranging symbols\n"); 347 fprintf(f, "\t@-symorder ../sys/symbols.sort %s\n", fl->f_needs); 348 fprintf(f, "\t@size %s\n", fl->f_needs); 349 fprintf(f, "\t@chmod 755 %s\n\n", fl->f_needs); 350 } 351 for (fl = conf_list; fl != NULL; fl = fl->f_next) 352 { 353 fprintf(f, "swap%s.o: ../dev/swap%s.c\n", fl->f_fn, fl->f_fn); 354 fprintf(f, "\t${CC} -I. -c -S ${COPTS} ../dev/swap%s.c\n", fl->f_fn); 355 fprintf(f, 356 "\t${C2} swap%s.s | sed -f ../sys/asm.sed | ${AS} -o swap%s.o\n", 357 fl->f_fn, fl->f_fn); 358 fprintf(f, "\trm -f swap%s.s\n\n", fl->f_fn); 359 } 360 fprintf(f, "all:"); 361 for (fl = conf_list; fl != NULL; fl = fl->f_next) 362 fprintf(f, " %s", fl->f_needs); 363 putc('\n', f); 364 } 365 366 raise(str) 367 register char *str; 368 { 369 register char *cp = str; 370 371 while(*str) 372 { 373 if (islower(*str)) 374 *str = toupper(*str); 375 str++; 376 } 377 return cp; 378 } 379