]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/config/mkmakefile.c
Re-insert variable disappeared during mis-application of r336973
[FreeBSD/FreeBSD.git] / usr.sbin / config / mkmakefile.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1990, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #ifndef lint
33 #if 0
34 static char sccsid[] = "@(#)mkmakefile.c        8.1 (Berkeley) 6/6/93";
35 #endif
36 static const char rcsid[] =
37   "$FreeBSD$";
38 #endif /* not lint */
39
40 /*
41  * Build the makefile for the system, from
42  * the information in the files files and the
43  * additional files for the machine being compiled to.
44  */
45
46 #include <ctype.h>
47 #include <err.h>
48 #include <stdarg.h>
49 #include <stdbool.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <sys/cnv.h>
53 #include <sys/nv.h>
54 #include <sys/param.h>
55 #include "y.tab.h"
56 #include "config.h"
57 #include "configvers.h"
58
59 static char *tail(char *);
60 static void do_clean(FILE *);
61 static void do_rules(FILE *);
62 static void do_xxfiles(char *, FILE *);
63 static void do_objs(FILE *);
64 static void do_before_depend(FILE *);
65 static int opteq(const char *, const char *);
66 static void read_files(void);
67 static void sanitize_envline(char *result, const char *src);
68 static bool preprocess(char *line, char *result);
69 static void process_into_file(char *line, FILE *ofp);
70 static void process_into_nvlist(char *line, nvlist_t *nvl);
71 static void dump_nvlist(nvlist_t *nvl, FILE *ofp);
72
73 static void errout(const char *fmt, ...)
74 {
75         va_list ap;
76
77         va_start(ap, fmt);
78         vfprintf(stderr, fmt, ap);
79         va_end(ap);
80         exit(1);
81 }
82
83 /*
84  * Lookup a file, by name.
85  */
86 static struct file_list *
87 fl_lookup(char *file)
88 {
89         struct file_list *fp;
90
91         STAILQ_FOREACH(fp, &ftab, f_next) {
92                 if (eq(fp->f_fn, file))
93                         return (fp);
94         }
95         return (0);
96 }
97
98 /*
99  * Make a new file list entry
100  */
101 static struct file_list *
102 new_fent(void)
103 {
104         struct file_list *fp;
105
106         fp = (struct file_list *) calloc(1, sizeof *fp);
107         if (fp == NULL)
108                 err(EXIT_FAILURE, "calloc");
109         STAILQ_INSERT_TAIL(&ftab, fp, f_next);
110         return (fp);
111 }
112
113 /*
114  * Open the correct Makefile and return it, or error out.
115  */
116 FILE *
117 open_makefile_template(void)
118 {
119         FILE *ifp;
120         char line[BUFSIZ];
121
122         snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
123         ifp = fopen(line, "r");
124         if (ifp == NULL) {
125                 snprintf(line, sizeof(line), "Makefile.%s", machinename);
126                 ifp = fopen(line, "r");
127         }
128         if (ifp == NULL)
129                 err(1, "%s", line);
130         return (ifp);
131 }
132
133 /*
134  * Build the makefile from the skeleton
135  */
136 void
137 makefile(void)
138 {
139         FILE *ifp, *ofp;
140         char line[BUFSIZ];
141         struct opt *op, *t;
142
143         read_files();
144         ifp = open_makefile_template();
145         ofp = fopen(path("Makefile.new"), "w");
146         if (ofp == NULL)
147                 err(1, "%s", path("Makefile.new"));
148         fprintf(ofp, "KERN_IDENT=%s\n", ident);
149         fprintf(ofp, "MACHINE=%s\n", machinename);
150         fprintf(ofp, "MACHINE_ARCH=%s\n", machinearch);
151         SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
152                 fprintf(ofp, "%s=%s", op->op_name, op->op_value);
153                 while ((op = SLIST_NEXT(op, op_append)) != NULL)
154                         fprintf(ofp, " %s", op->op_value);
155                 fprintf(ofp, "\n");
156         }
157         if (debugging)
158                 fprintf(ofp, "DEBUG=-g\n");
159         if (profiling)
160                 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
161         if (*srcdir != '\0')
162                 fprintf(ofp,"S=%s\n", srcdir);
163         while (fgets(line, BUFSIZ, ifp) != NULL) {
164                 if (*line != '%') {
165                         fprintf(ofp, "%s", line);
166                         continue;
167                 }
168                 if (eq(line, "%BEFORE_DEPEND\n"))
169                         do_before_depend(ofp);
170                 else if (eq(line, "%OBJS\n"))
171                         do_objs(ofp);
172                 else if (strncmp(line, "%FILES.", 7) == 0)
173                         do_xxfiles(line, ofp);
174                 else if (eq(line, "%RULES\n"))
175                         do_rules(ofp);
176                 else if (eq(line, "%CLEAN\n"))
177                         do_clean(ofp);
178                 else if (strncmp(line, "%VERSREQ=", 9) == 0)
179                         line[0] = '\0'; /* handled elsewhere */
180                 else
181                         fprintf(stderr,
182                             "Unknown %% construct in generic makefile: %s",
183                             line);
184         }
185         (void) fclose(ifp);
186         (void) fclose(ofp);
187         moveifchanged(path("Makefile.new"), path("Makefile"));
188 }
189
190 static void
191 sanitize_envline(char *result, const char *src)
192 {
193         const char *eq;
194         char c, *dst;
195         bool leading;
196
197         /* If there is no '=' it's not a well-formed name=value line. */
198         if ((eq = strchr(src, '=')) == NULL) {
199                 *result = 0;
200                 return;
201         }
202         dst = result;
203
204         /* Copy chars before the '=', skipping any leading spaces/quotes. */
205         leading = true;
206         while (src < eq) {
207                 c = *src++;
208                 if (leading && (isspace(c) || c == '"'))
209                         continue;
210                 *dst++ = c;
211                 leading = false;
212         }
213
214         /* If it was all leading space, we don't have a well-formed line. */
215         if (leading) {
216                 *result = 0;
217                 return;
218         }
219
220         /* Trim spaces/quotes immediately before the '=', then copy the '='. */
221         while (isspace(dst[-1]) || dst[-1] == '"')
222                 --dst;
223         *dst++ = *src++;
224
225         /* Copy chars after the '=', skipping any leading whitespace. */
226         leading = true;
227         while ((c = *src++) != 0) {
228                 if (leading && (isspace(c) || c == '"'))
229                         continue;
230                 *dst++ = c;
231                 leading = false;
232         }
233
234         /* If it was all leading space, it's a valid 'var=' (nil value). */
235         if (leading) {
236                 *dst = 0;
237                 return;
238         }
239
240         /* Trim trailing whitespace and quotes. */
241         while (isspace(dst[-1]) || dst[-1] == '"')
242                 --dst;
243
244         *dst = 0;
245 }
246
247 /*
248  * Returns true if the caller may use the string.
249  */
250 static bool
251 preprocess(char *line, char *result)
252 {
253
254         char result[BUFSIZ], *s;
255
256         /* Strip any comments */
257         if ((s = strchr(line, '#')) != NULL)
258                 *s = '\0';
259         sanitize_envline(result, line);
260         /* Return true if it's non-empty */
261         return (*result != '\0');
262 }
263
264 static void
265 process_into_file(char *line, FILE *ofp)
266 {
267         char result[BUFSIZ];
268
269         if (preprocess(line, result))
270                 fprintf(ofp, "\"%s\\0\"\n", result);
271 }
272
273 static void
274 process_into_nvlist(char *line, nvlist_t *nvl)
275 {
276         char result[BUFSIZ], *s;
277
278         if (preprocess(line, result)) {
279                 s = strchr(result, '=');
280                 *s = '\0';
281                 if (nvlist_exists(nvl, result))
282                         nvlist_free(nvl, result);
283                 nvlist_add_string(nvl, result, s + 1);
284         }
285 }
286
287 static void
288 dump_nvlist(nvlist_t *nvl, FILE *ofp)
289 {
290         const char *name;
291         void *cookie;
292
293         if (nvl == NULL)
294                 return;
295
296         while (!nvlist_empty(nvl)) {
297                 cookie = NULL;
298                 name = nvlist_next(nvl, NULL, &cookie);
299                 fprintf(ofp, "\"%s=%s\\0\"\n", name,
300                      cnvlist_get_string(cookie));
301
302                 cnvlist_free_string(cookie);
303         }
304 }
305
306 /*
307  * Build hints.c from the skeleton
308  */
309 void
310 makehints(void)
311 {
312         FILE *ifp, *ofp;
313         nvlist_t *nvl;
314         char line[BUFSIZ];
315         struct hint *hint;
316
317         ofp = fopen(path("hints.c.new"), "w");
318         if (ofp == NULL)
319                 err(1, "%s", path("hints.c.new"));
320         fprintf(ofp, "#include <sys/types.h>\n");
321         fprintf(ofp, "#include <sys/systm.h>\n");
322         fprintf(ofp, "\n");
323         /*
324          * Write out hintmode for older kernels. Remove when config(8) major
325          * version rolls over.
326          */
327         if (versreq <= CONFIGVERS_ENVMODE_REQ)
328                 fprintf(ofp, "int hintmode = %d;\n",
329                         !STAILQ_EMPTY(&hints) ? 1 : 0);
330         fprintf(ofp, "char static_hints[] = {\n");
331         nvl = nvlist_create(0);
332         STAILQ_FOREACH(hint, &hints, hint_next) {
333                 ifp = fopen(hint->hint_name, "r");
334                 if (ifp == NULL)
335                         err(1, "%s", hint->hint_name);
336                 while (fgets(line, BUFSIZ, ifp) != NULL)
337                         process_into_nvlist(line, nvl);
338                 dump_nvlist(nvl, ofp);
339                 fclose(ifp);
340         }
341         nvlist_destroy(nvl);
342         fprintf(ofp, "\"\\0\"\n};\n");
343         fclose(ofp);
344         moveifchanged(path("hints.c.new"), path("hints.c"));
345 }
346
347 /*
348  * Build env.c from the skeleton
349  */
350 void
351 makeenv(void)
352 {
353         FILE *ifp, *ofp;
354         nvlist_t *nvl;
355         char line[BUFSIZ];
356         struct envvar *envvar;
357
358         ofp = fopen(path("env.c.new"), "w");
359         if (ofp == NULL)
360                 err(1, "%s", path("env.c.new"));
361         fprintf(ofp, "#include <sys/types.h>\n");
362         fprintf(ofp, "#include <sys/systm.h>\n");
363         fprintf(ofp, "\n");
364         /*
365          * Write out envmode for older kernels. Remove when config(8) major
366          * version rolls over.
367          */
368         if (versreq <= CONFIGVERS_ENVMODE_REQ)
369                 fprintf(ofp, "int envmode = %d;\n",
370                         !STAILQ_EMPTY(&envvars) ? 1 : 0);
371         fprintf(ofp, "char static_env[] = {\n");
372         nvl = nvlist_create(0);
373         STAILQ_FOREACH(envvar, &envvars, envvar_next) {
374                 if (envvar->env_is_file) {
375                         ifp = fopen(envvar->env_str, "r");
376                         if (ifp == NULL)
377                                 err(1, "%s", envvar->env_str);
378                         while (fgets(line, BUFSIZ, ifp) != NULL)
379                                 process_into_nvlist(line, nvl);
380                         dump_nvlist(nvl, ofp);
381                         fclose(ifp);
382                 } else
383                         process_into_file(envvar->env_str, ofp);
384         }
385         nvlist_destroy(nvl);
386         fprintf(ofp, "\"\\0\"\n};\n");
387         fclose(ofp);
388         moveifchanged(path("env.c.new"), path("env.c"));
389 }
390
391 static void
392 read_file(char *fname)
393 {
394         char ifname[MAXPATHLEN];
395         FILE *fp;
396         struct file_list *tp;
397         struct device *dp;
398         struct opt *op;
399         char *wd, *this, *compilewith, *depends, *clean, *warning;
400         const char *objprefix;
401         int compile, match, nreqs, std, filetype, not,
402             imp_rule, no_obj, before_depend, nowerror;
403
404         fp = fopen(fname, "r");
405         if (fp == NULL)
406                 err(1, "%s", fname);
407 next:
408         /*
409          * include "filename"
410          * filename    [ standard | optional ]
411          *      [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
412          *      [ compile-with "compile rule" [no-implicit-rule] ]
413          *      [ dependency "dependency-list"] [ before-depend ]
414          *      [ clean "file-list"] [ warning "text warning" ]
415          *      [ obj-prefix "file prefix"]
416          */
417         wd = get_word(fp);
418         if (wd == (char *)EOF) {
419                 (void) fclose(fp);
420                 return;
421         } 
422         if (wd == NULL)
423                 goto next;
424         if (wd[0] == '#')
425         {
426                 while (((wd = get_word(fp)) != (char *)EOF) && wd)
427                         ;
428                 goto next;
429         }
430         if (eq(wd, "include")) {
431                 wd = get_quoted_word(fp);
432                 if (wd == (char *)EOF || wd == NULL)
433                         errout("%s: missing include filename.\n", fname);
434                 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
435                 read_file(ifname);
436                 while (((wd = get_word(fp)) != (char *)EOF) && wd)
437                         ;
438                 goto next;
439         }
440         this = ns(wd);
441         wd = get_word(fp);
442         if (wd == (char *)EOF)
443                 return;
444         if (wd == NULL)
445                 errout("%s: No type for %s.\n", fname, this);
446         tp = fl_lookup(this);
447         compile = 0;
448         match = 1;
449         nreqs = 0;
450         compilewith = 0;
451         depends = 0;
452         clean = 0;
453         warning = 0;
454         std = 0;
455         imp_rule = 0;
456         no_obj = 0;
457         before_depend = 0;
458         nowerror = 0;
459         not = 0;
460         filetype = NORMAL;
461         objprefix = "";
462         if (eq(wd, "standard"))
463                 std = 1;
464         else if (!eq(wd, "optional"))
465                 errout("%s: \"%s\" %s must be optional or standard\n",
466                     fname, wd, this);
467         for (wd = get_word(fp); wd; wd = get_word(fp)) {
468                 if (wd == (char *)EOF)
469                         return;
470                 if (eq(wd, "!")) {
471                         not = 1;
472                         continue;
473                 }
474                 if (eq(wd, "|")) {
475                         if (nreqs == 0)
476                                 errout("%s: syntax error describing %s\n",
477                                        fname, this);
478                         compile += match;
479                         match = 1;
480                         nreqs = 0;
481                         continue;
482                 }
483                 if (eq(wd, "no-obj")) {
484                         no_obj++;
485                         continue;
486                 }
487                 if (eq(wd, "no-implicit-rule")) {
488                         if (compilewith == NULL)
489                                 errout("%s: alternate rule required when "
490                                        "\"no-implicit-rule\" is specified for"
491                                        " %s.\n",
492                                        fname, this);
493                         imp_rule++;
494                         continue;
495                 }
496                 if (eq(wd, "before-depend")) {
497                         before_depend++;
498                         continue;
499                 }
500                 if (eq(wd, "dependency")) {
501                         wd = get_quoted_word(fp);
502                         if (wd == (char *)EOF || wd == NULL)
503                                 errout("%s: %s missing dependency string.\n",
504                                        fname, this);
505                         depends = ns(wd);
506                         continue;
507                 }
508                 if (eq(wd, "clean")) {
509                         wd = get_quoted_word(fp);
510                         if (wd == (char *)EOF || wd == NULL)
511                                 errout("%s: %s missing clean file list.\n",
512                                        fname, this);
513                         clean = ns(wd);
514                         continue;
515                 }
516                 if (eq(wd, "compile-with")) {
517                         wd = get_quoted_word(fp);
518                         if (wd == (char *)EOF || wd == NULL)
519                                 errout("%s: %s missing compile command string.\n",
520                                        fname, this);
521                         compilewith = ns(wd);
522                         continue;
523                 }
524                 if (eq(wd, "warning")) {
525                         wd = get_quoted_word(fp);
526                         if (wd == (char *)EOF || wd == NULL)
527                                 errout("%s: %s missing warning text string.\n",
528                                        fname, this);
529                         warning = ns(wd);
530                         continue;
531                 }
532                 if (eq(wd, "obj-prefix")) {
533                         wd = get_quoted_word(fp);
534                         if (wd == (char *)EOF || wd == NULL)
535                                 errout("%s: %s missing object prefix string.\n",
536                                        fname, this);
537                         objprefix = ns(wd);
538                         continue;
539                 }
540                 if (eq(wd, "nowerror")) {
541                         nowerror = 1;
542                         continue;
543                 }
544                 if (eq(wd, "local")) {
545                         filetype = LOCAL;
546                         continue;
547                 }
548                 if (eq(wd, "no-depend")) {
549                         filetype = NODEPEND;
550                         continue;
551                 }
552                 nreqs++;
553                 if (eq(wd, "profiling-routine")) {
554                         filetype = PROFILING;
555                         continue;
556                 }
557                 if (std)
558                         errout("standard entry %s has optional inclusion specifier %s!\n",
559                                this, wd);
560                 STAILQ_FOREACH(dp, &dtab, d_next)
561                         if (eq(dp->d_name, wd)) {
562                                 if (not)
563                                         match = 0;
564                                 else
565                                         dp->d_done |= DEVDONE;
566                                 goto nextparam;
567                         }
568                 SLIST_FOREACH(op, &opt, op_next)
569                         if (op->op_value == 0 && opteq(op->op_name, wd)) {
570                                 if (not)
571                                         match = 0;
572                                 goto nextparam;
573                         }
574                 match &= not;
575 nextparam:;
576                 not = 0;
577         }
578         compile += match;
579         if (compile && tp == NULL) {
580                 if (std == 0 && nreqs == 0)
581                         errout("%s: what is %s optional on?\n",
582                                fname, this);
583                 if (filetype == PROFILING && profiling == 0)
584                         goto next;
585                 tp = new_fent();
586                 tp->f_fn = this;
587                 tp->f_type = filetype;
588                 if (filetype == LOCAL)
589                         tp->f_srcprefix = "";
590                 else
591                         tp->f_srcprefix = "$S/";
592                 if (imp_rule)
593                         tp->f_flags |= NO_IMPLCT_RULE;
594                 if (no_obj)
595                         tp->f_flags |= NO_OBJ;
596                 if (before_depend)
597                         tp->f_flags |= BEFORE_DEPEND;
598                 if (nowerror)
599                         tp->f_flags |= NOWERROR;
600                 tp->f_compilewith = compilewith;
601                 tp->f_depends = depends;
602                 tp->f_clean = clean;
603                 tp->f_warn = warning;
604                 tp->f_objprefix = objprefix;
605         }
606         goto next;
607 }
608
609 /*
610  * Read in the information about files used in making the system.
611  * Store it in the ftab linked list.
612  */
613 static void
614 read_files(void)
615 {
616         char fname[MAXPATHLEN];
617         struct files_name *nl, *tnl;
618         
619         (void) snprintf(fname, sizeof(fname), "../../conf/files");
620         read_file(fname);
621         (void) snprintf(fname, sizeof(fname),
622                         "../../conf/files.%s", machinename);
623         read_file(fname);
624         for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
625                 read_file(nl->f_name);
626                 tnl = STAILQ_NEXT(nl, f_next);
627                 free(nl->f_name);
628                 free(nl);
629         }
630 }
631
632 static int
633 opteq(const char *cp, const char *dp)
634 {
635         char c, d;
636
637         for (; ; cp++, dp++) {
638                 if (*cp != *dp) {
639                         c = isupper(*cp) ? tolower(*cp) : *cp;
640                         d = isupper(*dp) ? tolower(*dp) : *dp;
641                         if (c != d)
642                                 return (0);
643                 }
644                 if (*cp == 0)
645                         return (1);
646         }
647 }
648
649 static void
650 do_before_depend(FILE *fp)
651 {
652         struct file_list *tp;
653         int lpos, len;
654
655         fputs("BEFORE_DEPEND=", fp);
656         lpos = 15;
657         STAILQ_FOREACH(tp, &ftab, f_next)
658                 if (tp->f_flags & BEFORE_DEPEND) {
659                         len = strlen(tp->f_fn);
660                         if ((len = 3 + len) + lpos > 72) {
661                                 lpos = 8;
662                                 fputs("\\\n\t", fp);
663                         }
664                         if (tp->f_flags & NO_IMPLCT_RULE)
665                                 fprintf(fp, "%s ", tp->f_fn);
666                         else
667                                 fprintf(fp, "%s%s ", tp->f_srcprefix,
668                                     tp->f_fn);
669                         lpos += len + 1;
670                 }
671         if (lpos != 8)
672                 putc('\n', fp);
673 }
674
675 static void
676 do_objs(FILE *fp)
677 {
678         struct file_list *tp;
679         int lpos, len;
680         char *cp, och, *sp;
681
682         fprintf(fp, "OBJS=");
683         lpos = 6;
684         STAILQ_FOREACH(tp, &ftab, f_next) {
685                 if (tp->f_flags & NO_OBJ)
686                         continue;
687                 sp = tail(tp->f_fn);
688                 cp = sp + (len = strlen(sp)) - 1;
689                 och = *cp;
690                 *cp = 'o';
691                 len += strlen(tp->f_objprefix);
692                 if (len + lpos > 72) {
693                         lpos = 8;
694                         fprintf(fp, "\\\n\t");
695                 }
696                 fprintf(fp, "%s%s ", tp->f_objprefix, sp);
697                 lpos += len + 1;
698                 *cp = och;
699         }
700         if (lpos != 8)
701                 putc('\n', fp);
702 }
703
704 static void
705 do_xxfiles(char *tag, FILE *fp)
706 {
707         struct file_list *tp;
708         int lpos, len, slen;
709         char *suff, *SUFF;
710
711         if (tag[strlen(tag) - 1] == '\n')
712                 tag[strlen(tag) - 1] = '\0';
713
714         suff = ns(tag + 7);
715         SUFF = ns(suff);
716         raisestr(SUFF);
717         slen = strlen(suff);
718
719         fprintf(fp, "%sFILES=", SUFF);
720         free(SUFF);
721         lpos = 8;
722         STAILQ_FOREACH(tp, &ftab, f_next)
723                 if (tp->f_type != NODEPEND) {
724                         len = strlen(tp->f_fn);
725                         if (tp->f_fn[len - slen - 1] != '.')
726                                 continue;
727                         if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
728                                 continue;
729                         if ((len = 3 + len) + lpos > 72) {
730                                 lpos = 8;
731                                 fputs("\\\n\t", fp);
732                         }
733                         fprintf(fp, "%s%s ", tp->f_srcprefix, tp->f_fn);
734                         lpos += len + 1;
735                 }
736         free(suff);
737         if (lpos != 8)
738                 putc('\n', fp);
739 }
740
741 static char *
742 tail(char *fn)
743 {
744         char *cp;
745
746         cp = strrchr(fn, '/');
747         if (cp == NULL)
748                 return (fn);
749         return (cp+1);
750 }
751
752 /*
753  * Create the makerules for each file
754  * which is part of the system.
755  */
756 static void
757 do_rules(FILE *f)
758 {
759         char *cp, *np, och;
760         struct file_list *ftp;
761         char *compilewith;
762         char cmd[128];
763
764         STAILQ_FOREACH(ftp, &ftab, f_next) {
765                 if (ftp->f_warn)
766                         fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
767                 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
768                 och = *cp;
769                 if (ftp->f_flags & NO_IMPLCT_RULE) {
770                         if (ftp->f_depends)
771                                 fprintf(f, "%s%s: %s\n",
772                                         ftp->f_objprefix, np, ftp->f_depends);
773                         else
774                                 fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
775                 }
776                 else {
777                         *cp = '\0';
778                         if (och == 'o') {
779                                 fprintf(f, "%s%so:\n\t-cp %s%so .\n\n",
780                                         ftp->f_objprefix, tail(np),
781                                         ftp->f_srcprefix, np);
782                                 continue;
783                         }
784                         if (ftp->f_depends) {
785                                 fprintf(f, "%s%so: %s%s%c %s\n",
786                                         ftp->f_objprefix, tail(np),
787                                         ftp->f_srcprefix, np, och,
788                                         ftp->f_depends);
789                         }
790                         else {
791                                 fprintf(f, "%s%so: %s%s%c\n",
792                                         ftp->f_objprefix, tail(np),
793                                         ftp->f_srcprefix, np, och);
794                         }
795                 }
796                 compilewith = ftp->f_compilewith;
797                 if (compilewith == NULL) {
798                         const char *ftype = NULL;
799
800                         switch (ftp->f_type) {
801                         case NORMAL:
802                                 ftype = "NORMAL";
803                                 break;
804                         case PROFILING:
805                                 if (!profiling)
806                                         continue;
807                                 ftype = "PROFILE";
808                                 break;
809                         default:
810                                 fprintf(stderr,
811                                     "config: don't know rules for %s\n", np);
812                                 break;
813                         }
814                         snprintf(cmd, sizeof(cmd),
815                             "${%s_%c%s}", ftype,
816                             toupper(och),
817                             ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
818                         compilewith = cmd;
819                 }
820                 *cp = och;
821                 if (strlen(ftp->f_objprefix))
822                         fprintf(f, "\t%s %s%s\n", compilewith,
823                             ftp->f_srcprefix, np);
824                 else
825                         fprintf(f, "\t%s\n", compilewith);
826
827                 if (!(ftp->f_flags & NO_OBJ))
828                         fprintf(f, "\t${NORMAL_CTFCONVERT}\n\n");
829                 else
830                         fprintf(f, "\n");
831         }
832 }
833
834 static void
835 do_clean(FILE *fp)
836 {
837         struct file_list *tp;
838         int lpos, len;
839
840         fputs("CLEAN=", fp);
841         lpos = 7;
842         STAILQ_FOREACH(tp, &ftab, f_next)
843                 if (tp->f_clean) {
844                         len = strlen(tp->f_clean);
845                         if (len + lpos > 72) {
846                                 lpos = 8;
847                                 fputs("\\\n\t", fp);
848                         }
849                         fprintf(fp, "%s ", tp->f_clean);
850                         lpos += len + 1;
851                 }
852         if (lpos != 8)
853                 putc('\n', fp);
854 }
855
856 char *
857 raisestr(char *str)
858 {
859         char *cp = str;
860
861         while (*str) {
862                 if (islower(*str))
863                         *str = toupper(*str);
864                 str++;
865         }
866         return (cp);
867 }