xref: /dragonfly/usr.sbin/config/mkmakefile.c (revision a32bc35d)
1 /*
2  * Copyright (c) 1993, 19801990
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)mkmakefile.c	8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.sbin/config/mkmakefile.c,v 1.51.2.3 2001/01/23 00:09:32 peter Exp $
35  */
36 
37 /*
38  * Build the makefile for the system, from
39  * the information in the 'files' files and the
40  * additional files for the machine being compiled to.
41  */
42 
43 #include <ctype.h>
44 #include <err.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <sys/param.h>
48 #include "y.tab.h"
49 #include "config.h"
50 #include "configvers.h"
51 
52 #define next_word(fp, wd)						\
53 	{								\
54 		char *word;						\
55 									\
56 		word = get_word(fp);					\
57 		if (word == (char *)EOF)				\
58 			return;						\
59 		else							\
60 			wd = word;					\
61 	}
62 #define next_quoted_word(fp, wd)					\
63 	{								\
64 		char *word;						\
65 									\
66 		word = get_quoted_word(fp);				\
67 		if (word == (char *)EOF)				\
68 			return;						\
69 		else							\
70 			wd = word;					\
71 	}
72 
73 static struct file_list *fcur;
74 
75 static char *tail(char *);
76 static void do_clean(FILE *);
77 static void do_rules(FILE *);
78 static void do_sfiles(FILE *);
79 static void do_mfiles(FILE *);
80 static void do_cfiles(FILE *);
81 static void do_objs(FILE *);
82 static void do_before_depend(FILE *);
83 static int opteq(char *, char *);
84 static void read_files(void);
85 
86 /*
87  * Lookup a file, by name.
88  */
89 static struct file_list *
90 fl_lookup(char *file)
91 {
92 	struct file_list *fp;
93 
94 	for (fp = ftab; fp != NULL; fp = fp->f_next) {
95 		if (strcmp(fp->f_fn, file) == 0)
96 			return(fp);
97 	}
98 	return(0);
99 }
100 
101 /*
102  * Lookup a file, by final component name.
103  */
104 static struct file_list *
105 fltail_lookup(char *file)
106 {
107 	struct file_list *fp;
108 
109 	for (fp = ftab; fp != NULL; fp = fp->f_next) {
110 		if (strcmp(tail(fp->f_fn), tail(file)) == 0)
111 			return(fp);
112 	}
113 	return(0);
114 }
115 
116 /*
117  * Make a new file list entry
118  */
119 static struct file_list *
120 new_fent(void)
121 {
122 	struct file_list *fp;
123 
124 	fp = malloc(sizeof(*fp));
125 	bzero(fp, sizeof(*fp));
126 	if (fcur == NULL)
127 		fcur = ftab = fp;
128 	else
129 		fcur->f_next = fp;
130 	fcur = fp;
131 	return(fp);
132 }
133 
134 /*
135  * Build the makefile from the skeleton
136  */
137 void
138 makefile(void)
139 {
140 	FILE *ifp, *ofp;
141 	char line[BUFSIZ];
142 	struct opt *op;
143 	int versreq;
144 
145 	read_files();
146 	snprintf(line, sizeof(line), "../platform/%s/conf/Makefile",
147 		 platformname);
148 	ifp = fopen(line, "r");
149 	if (ifp == NULL) {
150 		snprintf(line, sizeof(line), "Makefile.%s", platformname);
151 		ifp = fopen(line, "r");
152 	}
153 	if (ifp == NULL)
154 		err(1, "%s", line);
155 	ofp = fopen(path("Makefile.new"), "w");
156 	if (ofp == NULL)
157 		err(1, "%s", path("Makefile.new"));
158 	fprintf(ofp, "KERN_IDENT=%s\n", raisestr(ident));
159 	fprintf(ofp, "MACHINE_PLATFORM=%s\n", platformname);
160 	fprintf(ofp, "MACHINE=%s\n", machinename);
161 	fprintf(ofp, "MACHINE_ARCH=%s\n", machinearchname);
162 	fprintf(ofp, ".makeenv MACHINE_PLATFORM\n");
163 	fprintf(ofp, ".makeenv MACHINE\n");
164 	fprintf(ofp, ".makeenv MACHINE_ARCH\n");
165 	fprintf(ofp, "IDENT=");
166 	if (profiling) {
167 		/* Don't compile kernel profiling code for vkernel */
168 		if (strncmp(platformname, "vkernel", 6) != 0)
169 			fprintf(ofp, " -DGPROF");
170 	}
171 
172 	if (cputype == 0) {
173 		printf("cpu type must be specified\n");
174 		exit(1);
175 	}
176 	fprintf(ofp, "\n");
177 	for (op = mkopt; op != NULL; op = op->op_next)
178 		fprintf(ofp, "%s=%s\n", op->op_name, op->op_value);
179 	if (debugging)
180 		fprintf(ofp, "DEBUG=-g\n");
181 	if (profiling) {
182 		fprintf(ofp, "PROF=-pg\n");
183 		fprintf(ofp, "PROFLEVEL=%d\n", profiling);
184 	}
185 	if (*srcdir != '\0')
186 		fprintf(ofp,"S=%s\n", srcdir);
187 	while (fgets(line, BUFSIZ, ifp) != 0) {
188 		if (*line != '%') {
189 			fprintf(ofp, "%s", line);
190 			continue;
191 		}
192 		if (strcmp(line, "%BEFORE_DEPEND\n") == 0)
193 			do_before_depend(ofp);
194 		else if (strcmp(line, "%OBJS\n") == 0)
195 			do_objs(ofp);
196 		else if (strcmp(line, "%MFILES\n") == 0)
197 			do_mfiles(ofp);
198 		else if (strcmp(line, "%CFILES\n") == 0)
199 			do_cfiles(ofp);
200 		else if (strcmp(line, "%SFILES\n") == 0)
201 			do_sfiles(ofp);
202 		else if (strcmp(line, "%RULES\n") == 0)
203 			do_rules(ofp);
204 		else if (strcmp(line, "%CLEAN\n") == 0)
205 			do_clean(ofp);
206 		else if (strncmp(line, "%VERSREQ=", sizeof("%VERSREQ=") - 1) == 0) {
207 			versreq = atoi(line + sizeof("%VERSREQ=") - 1);
208 			if (versreq != CONFIGVERS) {
209 				fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
210 				fprintf(stderr, "config version = %d, ", CONFIGVERS);
211 				fprintf(stderr, "version required = %d\n\n", versreq);
212 				fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
213 				fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
214 				fprintf(stderr, "before trying this again.\n\n");
215 				fprintf(stderr, "If running the new config fails check your config\n");
216 				fprintf(stderr, "file against the GENERIC or LINT config files for\n");
217 				fprintf(stderr, "changes in config syntax, or option/device naming\n");
218 				fprintf(stderr, "conventions\n\n");
219 				exit(1);
220 			}
221 		} else
222 			fprintf(stderr,
223 			    "Unknown %% construct in generic makefile: %s",
224 			    line);
225 	}
226 	fclose(ifp);
227 	fclose(ofp);
228 	moveifchanged(path("Makefile.new"), path("Makefile"));
229 }
230 
231 /*
232  * Read in the information about files used in making the system.
233  * Store it in the ftab linked list.
234  */
235 static void
236 read_files(void)
237 {
238 	FILE *fp;
239 	struct file_list *tp, *pf;
240 	struct device *dp;
241 	struct device *save_dp;
242 	struct opt *op;
243 	char *wd, *this, *needs, *special, *depends, *clean, *warning;
244 	char fname[MAXPATHLEN];
245 	int nonoptional;
246 	int nreqs, first = 1, configdep, isdup, std, filetype,
247 	    imp_rule, no_obj, before_depend, nowerror, mandatory;
248 
249 	ftab = 0;
250 	save_dp = NULL;
251 	if (ident == NULL) {
252 		printf("no ident line specified\n");
253 		exit(1);
254 	}
255 	snprintf(fname, sizeof(fname), "../conf/files");
256 openit:
257 	fp = fopen(fname, "r");
258 	if (fp == NULL)
259 		err(1, "%s", fname);
260 next:
261 	/*
262 	 * filename    [ standard | mandatory | optional ] [ config-dependent ]
263 	 *	[ dev* | profiling-routine ] [ no-obj ]
264 	 *	[ compile-with "compile rule" [no-implicit-rule] ]
265 	 *      [ dependency "dependency-list"] [ before-depend ]
266 	 *	[ clean "file-list"] [ warning "text warning" ]
267 	 */
268 	wd = get_word(fp);
269 	if (wd == (char *)EOF) {
270 		fclose(fp);
271 		if (first == 1) {
272 			first++;
273 			snprintf(fname, sizeof(fname),
274 			    "../platform/%s/conf/files",
275 			    platformname);
276 			fp = fopen(fname, "r");
277 			if (fp != NULL)
278 				goto next;
279 			snprintf(fname, sizeof(fname),
280 			    "files.%s", platformname);
281 			goto openit;
282 		}
283 		if (first == 2) {
284 			first++;
285 			snprintf(fname, sizeof(fname),
286 			    "files.%s", raisestr(ident));
287 			fp = fopen(fname, "r");
288 			if (fp != NULL)
289 				goto next;
290 		}
291 		return;
292 	}
293 	if (wd == NULL)
294 		goto next;
295 	if (wd[0] == '#')
296 	{
297 		while (((wd = get_word(fp)) != (char *)EOF) && wd)
298 			;
299 		goto next;
300 	}
301 	this = strdup(wd);
302 	next_word(fp, wd);
303 	if (wd == NULL) {
304 		printf("%s: No type for %s.\n",
305 		    fname, this);
306 		exit(1);
307 	}
308 	if ((pf = fl_lookup(this)) && (pf->f_type != INVISIBLE || pf->f_flags))
309 		isdup = 1;
310 	else
311 		isdup = 0;
312 	tp = NULL;
313 	if (first == 3 && pf == NULL && (tp = fltail_lookup(this)) != NULL) {
314 		if (tp->f_type != INVISIBLE || tp->f_flags)
315 			printf("%s: Local file %s overrides %s.\n",
316 			    fname, this, tp->f_fn);
317 		else
318 			printf("%s: Local file %s could override %s"
319 			    " with a different kernel configuration.\n",
320 			    fname, this, tp->f_fn);
321 	}
322 	nreqs = 0;
323 	special = NULL;
324 	depends = NULL;
325 	clean = NULL;
326 	warning = NULL;
327 	configdep = 0;
328 	needs = NULL;
329 	std = mandatory = nonoptional = 0;
330 	imp_rule = 0;
331 	no_obj = 0;
332 	before_depend = 0;
333 	nowerror = 0;
334 	filetype = NORMAL;
335 	if (strcmp(wd, "standard") == 0) {
336 		std = 1;
337 	} else if (strcmp(wd, "mandatory") == 0) {
338 		/*
339 		 * If an entry is marked "mandatory", config will abort if
340 		 * it's not called by a configuration line in the config
341 		 * file.  Apart from this, the device is handled like one
342 		 * marked "optional".
343 		 */
344 		mandatory = 1;
345 	} else if (strcmp(wd, "nonoptional") == 0) {
346 		nonoptional = 1;
347 	} else if (strcmp(wd, "optional") == 0) {
348 		/* don't need to do anything */
349 	} else {
350 		printf("%s: %s must be optional, mandatory or standard\n",
351 		       fname, this);
352 		printf("Alternatively, your version of config(8) may be out of sync with your\nkernel source.\n");
353 		exit(1);
354 	}
355 nextparam:
356 	next_word(fp, wd);
357 	if (wd == NULL)
358 		goto doneparam;
359 	if (strcmp(wd, "config-dependent") == 0) {
360 		configdep++;
361 		goto nextparam;
362 	}
363 	if (strcmp(wd, "no-obj") == 0) {
364 		no_obj++;
365 		goto nextparam;
366 	}
367 	if (strcmp(wd, "no-implicit-rule") == 0) {
368 		if (special == NULL) {
369 			printf("%s: alternate rule required when "
370 			       "\"no-implicit-rule\" is specified.\n",
371 			       fname);
372 		}
373 		imp_rule++;
374 		goto nextparam;
375 	}
376 	if (strcmp(wd, "before-depend") == 0) {
377 		before_depend++;
378 		goto nextparam;
379 	}
380 	if (strcmp(wd, "dependency") == 0) {
381 		next_quoted_word(fp, wd);
382 		if (wd == NULL) {
383 			printf("%s: %s missing compile command string.\n",
384 			       fname, this);
385 			exit(1);
386 		}
387 		depends = strdup(wd);
388 		goto nextparam;
389 	}
390 	if (strcmp(wd, "clean") == 0) {
391 		next_quoted_word(fp, wd);
392 		if (wd == NULL) {
393 			printf("%s: %s missing clean file list.\n",
394 			       fname, this);
395 			exit(1);
396 		}
397 		clean = strdup(wd);
398 		goto nextparam;
399 	}
400 	if (strcmp(wd, "compile-with") == 0) {
401 		next_quoted_word(fp, wd);
402 		if (wd == NULL) {
403 			printf("%s: %s missing compile command string.\n",
404 			       fname, this);
405 			exit(1);
406 		}
407 		special = strdup(wd);
408 		goto nextparam;
409 	}
410 	if (strcmp(wd, "nowerror") == 0) {
411 		nowerror++;
412 		goto nextparam;
413 	}
414 	if (strcmp(wd, "warning") == 0) {
415 		next_quoted_word(fp, wd);
416 		if (wd == NULL) {
417 			printf("%s: %s missing warning text string.\n",
418 				fname, this);
419 			exit(1);
420 		}
421 		warning = strdup(wd);
422 		goto nextparam;
423 	}
424 	nreqs++;
425 	if (strcmp(wd, "local") == 0) {
426 		filetype = LOCAL;
427 		goto nextparam;
428 	}
429 	if (strcmp(wd, "no-depend") == 0) {
430 		filetype = NODEPEND;
431 		goto nextparam;
432 	}
433 	if (strcmp(wd, "device-driver") == 0) {
434 		printf("%s: `device-driver' flag obsolete.\n", fname);
435 		exit(1);
436 	}
437 	if (strcmp(wd, "profiling-routine") == 0) {
438 		filetype = PROFILING;
439 		goto nextparam;
440 	}
441 	if (needs == NULL && nreqs == 1)
442 		needs = strdup(wd);
443 	if (isdup)
444 		goto invis;
445 	for (dp = dtab; dp != NULL; save_dp = dp, dp = dp->d_next)
446 		if (strcmp(dp->d_name, wd) == 0) {
447 			if (std && dp->d_type == PSEUDO_DEVICE &&
448 			    dp->d_count <= 0)
449 				dp->d_count = 1;
450 			goto nextparam;
451 		}
452 	if (mandatory) {
453 		printf("%s: mandatory device \"%s\" not found\n",
454 		       fname, wd);
455 		exit(1);
456 	}
457 	if (std) {
458 		dp = malloc(sizeof(*dp));
459 		bzero(dp, sizeof(*dp));
460 		init_dev(dp);
461 		dp->d_name = strdup(wd);
462 		dp->d_type = PSEUDO_DEVICE;
463 		dp->d_count = 1;
464 		save_dp->d_next = dp;
465 		goto nextparam;
466 	}
467 	for (op = opt; op != NULL; op = op->op_next) {
468 		if (op->op_value == 0 && opteq(op->op_name, wd)) {
469 			if (nreqs == 1) {
470 				free(needs);
471 				needs = NULL;
472 			}
473 			goto nextparam;
474 		}
475 	}
476 	if (nonoptional) {
477 		printf("%s: the option \"%s\" MUST be specified\n",
478 			fname, wd);
479 		exit(1);
480 	}
481 invis:
482 	while ((wd = get_word(fp)) != NULL)
483 		;
484 	if (tp == NULL)
485 		tp = new_fent();
486 	tp->f_fn = this;
487 	tp->f_type = INVISIBLE;
488 	tp->f_needs = needs;
489 	tp->f_flags = isdup;
490 	tp->f_special = special;
491 	tp->f_depends = depends;
492 	tp->f_clean = clean;
493 	tp->f_warn = warning;
494 	goto next;
495 
496 doneparam:
497 	if (std == 0 && nreqs == 0) {
498 		printf("%s: what is %s optional on?\n",
499 		    fname, this);
500 		exit(1);
501 	}
502 
503 	if (wd != NULL) {
504 		printf("%s: syntax error describing %s\n",
505 		    fname, this);
506 		exit(1);
507 	}
508 	if (filetype == PROFILING && profiling == 0)
509 		goto next;
510 	if (tp == NULL)
511 		tp = new_fent();
512 	tp->f_fn = this;
513 	tp->f_type = filetype;
514 	tp->f_flags = 0;
515 	if (configdep)
516 		tp->f_flags |= CONFIGDEP;
517 	if (imp_rule)
518 		tp->f_flags |= NO_IMPLCT_RULE;
519 	if (no_obj)
520 		tp->f_flags |= NO_OBJ;
521 	if (before_depend)
522 		tp->f_flags |= BEFORE_DEPEND;
523 	if (nowerror)
524 		tp->f_flags |= NOWERROR;
525 	if (imp_rule)
526 		tp->f_flags |= NO_IMPLCT_RULE;
527 	if (no_obj)
528 		tp->f_flags |= NO_OBJ;
529 	tp->f_needs = needs;
530 	tp->f_special = special;
531 	tp->f_depends = depends;
532 	tp->f_clean = clean;
533 	tp->f_warn = warning;
534 	if (pf && pf->f_type == INVISIBLE)
535 		pf->f_flags = 1;		/* mark as duplicate */
536 	goto next;
537 }
538 
539 static int
540 opteq(char *cp, char *dp)
541 {
542 	char c, d;
543 
544 	for (;; cp++, dp++) {
545 		if (*cp != *dp) {
546 			c = isupper(*cp) ? tolower(*cp) : *cp;
547 			d = isupper(*dp) ? tolower(*dp) : *dp;
548 			if (c != d)
549 				return(0);
550 		}
551 		if (*cp == 0)
552 			return(1);
553 	}
554 }
555 
556 static void
557 do_before_depend(FILE *fp)
558 {
559 	struct file_list *tp;
560 	int lpos, len;
561 
562 	fputs("BEFORE_DEPEND=", fp);
563 	lpos = 15;
564 	for (tp = ftab; tp != NULL; tp = tp->f_next)
565 		if (tp->f_flags & BEFORE_DEPEND) {
566 			len = strlen(tp->f_fn);
567 			if ((len = 3 + len) + lpos > 72) {
568 				lpos = 8;
569 				fputs("\\\n\t", fp);
570 			}
571 			if (tp->f_flags & NO_IMPLCT_RULE)
572 				fprintf(fp, "%s ", tp->f_fn);
573 			else
574 				fprintf(fp, "$S/%s ", tp->f_fn);
575 			lpos += len + 1;
576 		}
577 	if (lpos != 8)
578 		putc('\n', fp);
579 }
580 
581 static void
582 do_objs(FILE *fp)
583 {
584 	struct file_list *tp;
585 	int lpos, len;
586 	char *cp, och, *sp;
587 
588 	fprintf(fp, "OBJS=");
589 	lpos = 6;
590 	for (tp = ftab; tp != NULL; tp = tp->f_next) {
591 		if (tp->f_type == INVISIBLE || tp->f_flags & NO_OBJ)
592 			continue;
593 		sp = tail(tp->f_fn);
594 		cp = sp + (len = strlen(sp)) - 1;
595 		och = *cp;
596 		*cp = 'o';
597 		if (len + lpos > 72) {
598 			lpos = 8;
599 			fprintf(fp, "\\\n\t");
600 		}
601 		fprintf(fp, "%s ", sp);
602 		lpos += len + 1;
603 		*cp = och;
604 	}
605 	if (lpos != 8)
606 		putc('\n', fp);
607 }
608 
609 static void
610 do_cfiles(FILE *fp)
611 {
612 	struct file_list *tp;
613 	int lpos, len;
614 
615 	fputs("CFILES=", fp);
616 	lpos = 8;
617 	for (tp = ftab; tp != NULL; tp = tp->f_next)
618 		if (tp->f_type != INVISIBLE && tp->f_type != NODEPEND) {
619 			len = strlen(tp->f_fn);
620 			if (tp->f_fn[len - 1] != 'c')
621 				continue;
622 			if ((len = 3 + len) + lpos > 72) {
623 				lpos = 8;
624 				fputs("\\\n\t", fp);
625 			}
626 			if (tp->f_type != LOCAL)
627 				fprintf(fp, "$S/%s ", tp->f_fn);
628 			else
629 				fprintf(fp, "%s ", tp->f_fn);
630 
631 			lpos += len + 1;
632 		}
633 	if (lpos != 8)
634 		putc('\n', fp);
635 }
636 
637 static void
638 do_mfiles(FILE *fp)
639 {
640 	struct file_list *tp;
641 	int lpos, len;
642 
643 	fputs("MFILES=", fp);
644 	lpos = 8;
645 	for (tp = ftab; tp != NULL; tp = tp->f_next)
646 		if (tp->f_type != INVISIBLE) {
647 			len = strlen(tp->f_fn);
648 			if (tp->f_fn[len - 1] != 'm' || tp->f_fn[len - 2] != '.')
649 				continue;
650 			if ((len = 3 + len) + lpos > 72) {
651 				lpos = 8;
652 				fputs("\\\n\t", fp);
653 			}
654 			fprintf(fp, "$S/%s ", tp->f_fn);
655 			lpos += len + 1;
656 		}
657 	if (lpos != 8)
658 		putc('\n', fp);
659 }
660 
661 static void
662 do_sfiles(FILE *fp)
663 {
664 	struct file_list *tp;
665 	int lpos, len;
666 
667 	fputs("SFILES=", fp);
668 	lpos = 8;
669 	for (tp = ftab; tp != NULL; tp = tp->f_next)
670 		if (tp->f_type != INVISIBLE) {
671 			len = strlen(tp->f_fn);
672 			if (tp->f_fn[len - 1] != 'S' && tp->f_fn[len - 1] != 's')
673 				continue;
674 			if ((len = 3 + len) + lpos > 72) {
675 				lpos = 8;
676 				fputs("\\\n\t", fp);
677 			}
678 			fprintf(fp, "$S/%s ", tp->f_fn);
679 			lpos += len + 1;
680 		}
681 	if (lpos != 8)
682 		putc('\n', fp);
683 }
684 
685 
686 static char *
687 tail(char *fn)
688 {
689 	char *cp;
690 
691 	cp = strrchr(fn, '/');
692 	if (cp == NULL)
693 		return(fn);
694 	return(cp + 1);
695 }
696 
697 /*
698  * Create the makerules for each file
699  * which is part of the system.
700  * Devices are processed with the special c2 option -i
701  * which avoids any problem areas with i/o addressing
702  * (e.g. for the VAX); assembler files are processed by as.
703  */
704 static void
705 do_rules(FILE *f)
706 {
707 	char *cp, *np, och, *tp;
708 	struct file_list *ftp;
709 	char *special;
710 
711 	for (ftp = ftab; ftp != NULL; ftp = ftp->f_next) {
712 		if (ftp->f_type == INVISIBLE)
713 			continue;
714 		if (ftp->f_warn != NULL)
715 			printf("WARNING: %s\n", ftp->f_warn);
716 		cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
717 		och = *cp;
718 		if (ftp->f_flags & NO_IMPLCT_RULE) {
719 			if (ftp->f_depends)
720 				fprintf(f, "%s: %s\n", np, ftp->f_depends);
721 			else
722 				fprintf(f, "%s: \n", np);
723 		}
724 		else {
725 			*cp = '\0';
726 			if (och == 'o') {
727 				fprintf(f, "%so:\n\t-cp $S/%so .\n\n",
728 					tail(np), np);
729 				continue;
730 			}
731 			if (ftp->f_depends)
732 				fprintf(f, "%so: $S/%s%c %s\n", tail(np),
733 					np, och, ftp->f_depends);
734 			else
735 				fprintf(f, "%so: $S/%s%c\n", tail(np),
736 					np, och);
737 		}
738 		tp = tail(np);
739 		special = ftp->f_special;
740 		if (special == NULL) {
741 			const char *ftype = NULL;
742 			static char cmd[128];
743 
744 			switch (ftp->f_type) {
745 
746 			case NORMAL:
747 				ftype = "NORMAL";
748 				break;
749 
750 			case PROFILING:
751 				if (!profiling)
752 					continue;
753 				ftype = "PROFILE";
754 				break;
755 
756 			default:
757 				printf("config: don't know rules for %s\n", np);
758 				break;
759 			}
760 			snprintf(cmd, sizeof(cmd), "${%s_%c%s}%s",
761 			    ftype, toupper(och),
762 			    ftp->f_flags & CONFIGDEP ? "_C" : "",
763 			    ftp->f_flags & NOWERROR ? "" : " ${WERROR}");
764 			special = cmd;
765 		}
766 		*cp = och;
767 		fprintf(f, "\t%s\n\n", special);
768 	}
769 }
770 
771 static void
772 do_clean(FILE *fp)
773 {
774 	struct file_list *tp;
775 	int lpos, len;
776 
777 	fputs("CLEAN=", fp);
778 	lpos = 7;
779 	for (tp = ftab; tp != NULL; tp = tp->f_next)
780 		if (tp->f_clean) {
781 			len = strlen(tp->f_clean);
782 			if (len + lpos > 72) {
783 				lpos = 8;
784 				fputs("\\\n\t", fp);
785 			}
786 			fprintf(fp, "%s ", tp->f_clean);
787 			lpos += len + 1;
788 		}
789 	if (lpos != 8)
790 		putc('\n', fp);
791 }
792 
793 char *
794 raisestr(char *str)
795 {
796 	char *cp = str;
797 
798 	while (*str) {
799 		if (islower(*str))
800 			*str = toupper(*str);
801 		str++;
802 	}
803 	return(cp);
804 }
805