]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/config/mkmakefile.c
kern_environment: use any provided environments, evict hintmode/envmode
[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/param.h>
53 #include "y.tab.h"
54 #include "config.h"
55 #include "configvers.h"
56
57 static char *tail(char *);
58 static void do_clean(FILE *);
59 static void do_rules(FILE *);
60 static void do_xxfiles(char *, FILE *);
61 static void do_objs(FILE *);
62 static void do_before_depend(FILE *);
63 static int opteq(const char *, const char *);
64 static void read_files(void);
65
66 static void errout(const char *fmt, ...)
67 {
68         va_list ap;
69
70         va_start(ap, fmt);
71         vfprintf(stderr, fmt, ap);
72         va_end(ap);
73         exit(1);
74 }
75
76 /*
77  * Lookup a file, by name.
78  */
79 static struct file_list *
80 fl_lookup(char *file)
81 {
82         struct file_list *fp;
83
84         STAILQ_FOREACH(fp, &ftab, f_next) {
85                 if (eq(fp->f_fn, file))
86                         return (fp);
87         }
88         return (0);
89 }
90
91 /*
92  * Make a new file list entry
93  */
94 static struct file_list *
95 new_fent(void)
96 {
97         struct file_list *fp;
98
99         fp = (struct file_list *) calloc(1, sizeof *fp);
100         if (fp == NULL)
101                 err(EXIT_FAILURE, "calloc");
102         STAILQ_INSERT_TAIL(&ftab, fp, f_next);
103         return (fp);
104 }
105
106 /*
107  * Open the correct Makefile and return it, or error out.
108  */
109 FILE *
110 open_makefile_template(void)
111 {
112         FILE *ifp;
113         char line[BUFSIZ];
114
115         snprintf(line, sizeof(line), "../../conf/Makefile.%s", machinename);
116         ifp = fopen(line, "r");
117         if (ifp == NULL) {
118                 snprintf(line, sizeof(line), "Makefile.%s", machinename);
119                 ifp = fopen(line, "r");
120         }
121         if (ifp == NULL)
122                 err(1, "%s", line);
123         return (ifp);
124 }
125
126 /*
127  * Build the makefile from the skeleton
128  */
129 void
130 makefile(void)
131 {
132         FILE *ifp, *ofp;
133         char line[BUFSIZ];
134         struct opt *op, *t;
135
136         read_files();
137         ifp = open_makefile_template();
138         ofp = fopen(path("Makefile.new"), "w");
139         if (ofp == NULL)
140                 err(1, "%s", path("Makefile.new"));
141         fprintf(ofp, "KERN_IDENT=%s\n", ident);
142         fprintf(ofp, "MACHINE=%s\n", machinename);
143         fprintf(ofp, "MACHINE_ARCH=%s\n", machinearch);
144         SLIST_FOREACH_SAFE(op, &mkopt, op_next, t) {
145                 fprintf(ofp, "%s=%s", op->op_name, op->op_value);
146                 while ((op = SLIST_NEXT(op, op_append)) != NULL)
147                         fprintf(ofp, " %s", op->op_value);
148                 fprintf(ofp, "\n");
149         }
150         if (debugging)
151                 fprintf(ofp, "DEBUG=-g\n");
152         if (profiling)
153                 fprintf(ofp, "PROFLEVEL=%d\n", profiling);
154         if (*srcdir != '\0')
155                 fprintf(ofp,"S=%s\n", srcdir);
156         while (fgets(line, BUFSIZ, ifp) != NULL) {
157                 if (*line != '%') {
158                         fprintf(ofp, "%s", line);
159                         continue;
160                 }
161                 if (eq(line, "%BEFORE_DEPEND\n"))
162                         do_before_depend(ofp);
163                 else if (eq(line, "%OBJS\n"))
164                         do_objs(ofp);
165                 else if (strncmp(line, "%FILES.", 7) == 0)
166                         do_xxfiles(line, ofp);
167                 else if (eq(line, "%RULES\n"))
168                         do_rules(ofp);
169                 else if (eq(line, "%CLEAN\n"))
170                         do_clean(ofp);
171                 else if (strncmp(line, "%VERSREQ=", 9) == 0)
172                         line[0] = '\0'; /* handled elsewhere */
173                 else
174                         fprintf(stderr,
175                             "Unknown %% construct in generic makefile: %s",
176                             line);
177         }
178         (void) fclose(ifp);
179         (void) fclose(ofp);
180         moveifchanged(path("Makefile.new"), path("Makefile"));
181 }
182
183 /*
184  * Build hints.c from the skeleton
185  */
186 void
187 makehints(void)
188 {
189         FILE *ifp, *ofp;
190         char line[BUFSIZ];
191         char *s;
192         struct hint *hint;
193
194         ofp = fopen(path("hints.c.new"), "w");
195         if (ofp == NULL)
196                 err(1, "%s", path("hints.c.new"));
197         fprintf(ofp, "#include <sys/types.h>\n");
198         fprintf(ofp, "#include <sys/systm.h>\n");
199         fprintf(ofp, "\n");
200         fprintf(ofp, "char static_hints[] = {\n");
201         STAILQ_FOREACH(hint, &hints, hint_next) {
202                 ifp = fopen(hint->hint_name, "r");
203                 if (ifp == NULL)
204                         err(1, "%s", hint->hint_name);
205                 while (fgets(line, BUFSIZ, ifp) != NULL) {
206                         /* zap trailing CR and/or LF */
207                         while ((s = strrchr(line, '\n')) != NULL)
208                                 *s = '\0';
209                         while ((s = strrchr(line, '\r')) != NULL)
210                                 *s = '\0';
211                         /* remove # comments */
212                         s = strchr(line, '#');
213                         if (s)
214                                 *s = '\0';
215                         /* remove any whitespace and " characters */
216                         s = line;
217                         while (*s) {
218                                 if (*s == ' ' || *s == '\t' || *s == '"') {
219                                         while (*s) {
220                                                 s[0] = s[1];
221                                                 s++;
222                                         }
223                                         /* start over */
224                                         s = line;
225                                         continue;
226                                 }
227                                 s++;
228                         }
229                         /* anything left? */
230                         if (*line == '\0')
231                                 continue;
232                         fprintf(ofp, "\"%s\\0\"\n", line);
233                 }
234                 fclose(ifp);
235         }
236         fprintf(ofp, "\"\\0\"\n};\n");
237         fclose(ofp);
238         moveifchanged(path("hints.c.new"), path("hints.c"));
239 }
240
241 static void
242 sanitize_envline(char *result, const char *src)
243 {
244         const char *eq;
245         char c, *dst;
246         bool leading;
247
248         /* If there is no '=' it's not a well-formed name=value line. */
249         if ((eq = strchr(src, '=')) == NULL) {
250                 *result = 0;
251                 return;
252         }
253         dst = result;
254
255         /* Copy chars before the '=', skipping any leading spaces/quotes. */
256         leading = true;
257         while (src < eq) {
258                 c = *src++;
259                 if (leading && (isspace(c) || c == '"'))
260                         continue;
261                 *dst++ = c;
262                 leading = false;
263         }
264
265         /* If it was all leading space, we don't have a well-formed line. */
266         if (leading) {
267                 *result = 0;
268                 return;
269         }
270
271         /* Trim spaces/quotes immediately before the '=', then copy the '='. */
272         while (isspace(dst[-1]) || dst[-1] == '"')
273                 --dst;
274         *dst++ = *src++;
275
276         /* Copy chars after the '=', skipping any leading whitespace. */
277         leading = true;
278         while ((c = *src++) != 0) {
279                 if (leading && (isspace(c) || c == '"'))
280                         continue;
281                 *dst++ = c;
282                 leading = false;
283         }
284
285         /* If it was all leading space, it's a valid 'var=' (nil value). */
286         if (leading) {
287                 *dst = 0;
288                 return;
289         }
290
291         /* Trim trailing whitespace and quotes. */
292         while (isspace(dst[-1]) || dst[-1] == '"')
293                 --dst;
294
295         *dst = 0;
296 }
297
298 /*
299  * Build env.c from the skeleton
300  */
301 void
302 makeenv(void)
303 {
304         FILE *ifp, *ofp;
305         char line[BUFSIZ], result[BUFSIZ], *linep;
306         struct envvar *envvar;
307
308         ofp = fopen(path("env.c.new"), "w");
309         if (ofp == NULL)
310                 err(1, "%s", path("env.c.new"));
311         fprintf(ofp, "#include <sys/types.h>\n");
312         fprintf(ofp, "#include <sys/systm.h>\n");
313         fprintf(ofp, "\n");
314         fprintf(ofp, "char static_env[] = {\n");
315         STAILQ_FOREACH(envvar, &envvars, envvar_next) {
316                 if (envvar->env_is_file) {
317                         ifp = fopen(envvar->env_str, "r");
318                         if (ifp == NULL)
319                                 err(1, "%s", envvar->env_str);
320                         while (fgets(line, BUFSIZ, ifp) != NULL) {
321                                 sanitize_envline(result, line);
322                                 /* anything left? */
323                                 if (*result == '\0')
324                                         continue;
325                                 fprintf(ofp, "\"%s\\0\"\n", result);
326                         }
327                         fclose(ifp);
328                 } else {
329                         linep = envvar->env_str;
330                         sanitize_envline(result, linep);
331                         if (*result == '\0')
332                                 continue;
333                         fprintf(ofp, "\"%s\\0\"\n", result);
334                 }
335         }
336         fprintf(ofp, "\"\\0\"\n};\n");
337         fclose(ofp);
338         moveifchanged(path("env.c.new"), path("env.c"));
339 }
340
341 static void
342 read_file(char *fname)
343 {
344         char ifname[MAXPATHLEN];
345         FILE *fp;
346         struct file_list *tp;
347         struct device *dp;
348         struct opt *op;
349         char *wd, *this, *compilewith, *depends, *clean, *warning;
350         const char *objprefix;
351         int compile, match, nreqs, std, filetype, not,
352             imp_rule, no_obj, before_depend, nowerror;
353
354         fp = fopen(fname, "r");
355         if (fp == NULL)
356                 err(1, "%s", fname);
357 next:
358         /*
359          * include "filename"
360          * filename    [ standard | optional ]
361          *      [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
362          *      [ compile-with "compile rule" [no-implicit-rule] ]
363          *      [ dependency "dependency-list"] [ before-depend ]
364          *      [ clean "file-list"] [ warning "text warning" ]
365          *      [ obj-prefix "file prefix"]
366          */
367         wd = get_word(fp);
368         if (wd == (char *)EOF) {
369                 (void) fclose(fp);
370                 return;
371         } 
372         if (wd == NULL)
373                 goto next;
374         if (wd[0] == '#')
375         {
376                 while (((wd = get_word(fp)) != (char *)EOF) && wd)
377                         ;
378                 goto next;
379         }
380         if (eq(wd, "include")) {
381                 wd = get_quoted_word(fp);
382                 if (wd == (char *)EOF || wd == NULL)
383                         errout("%s: missing include filename.\n", fname);
384                 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
385                 read_file(ifname);
386                 while (((wd = get_word(fp)) != (char *)EOF) && wd)
387                         ;
388                 goto next;
389         }
390         this = ns(wd);
391         wd = get_word(fp);
392         if (wd == (char *)EOF)
393                 return;
394         if (wd == NULL)
395                 errout("%s: No type for %s.\n", fname, this);
396         tp = fl_lookup(this);
397         compile = 0;
398         match = 1;
399         nreqs = 0;
400         compilewith = 0;
401         depends = 0;
402         clean = 0;
403         warning = 0;
404         std = 0;
405         imp_rule = 0;
406         no_obj = 0;
407         before_depend = 0;
408         nowerror = 0;
409         not = 0;
410         filetype = NORMAL;
411         objprefix = "";
412         if (eq(wd, "standard"))
413                 std = 1;
414         else if (!eq(wd, "optional"))
415                 errout("%s: \"%s\" %s must be optional or standard\n",
416                     fname, wd, this);
417         for (wd = get_word(fp); wd; wd = get_word(fp)) {
418                 if (wd == (char *)EOF)
419                         return;
420                 if (eq(wd, "!")) {
421                         not = 1;
422                         continue;
423                 }
424                 if (eq(wd, "|")) {
425                         if (nreqs == 0)
426                                 errout("%s: syntax error describing %s\n",
427                                        fname, this);
428                         compile += match;
429                         match = 1;
430                         nreqs = 0;
431                         continue;
432                 }
433                 if (eq(wd, "no-obj")) {
434                         no_obj++;
435                         continue;
436                 }
437                 if (eq(wd, "no-implicit-rule")) {
438                         if (compilewith == NULL)
439                                 errout("%s: alternate rule required when "
440                                        "\"no-implicit-rule\" is specified for"
441                                        " %s.\n",
442                                        fname, this);
443                         imp_rule++;
444                         continue;
445                 }
446                 if (eq(wd, "before-depend")) {
447                         before_depend++;
448                         continue;
449                 }
450                 if (eq(wd, "dependency")) {
451                         wd = get_quoted_word(fp);
452                         if (wd == (char *)EOF || wd == NULL)
453                                 errout("%s: %s missing dependency string.\n",
454                                        fname, this);
455                         depends = ns(wd);
456                         continue;
457                 }
458                 if (eq(wd, "clean")) {
459                         wd = get_quoted_word(fp);
460                         if (wd == (char *)EOF || wd == NULL)
461                                 errout("%s: %s missing clean file list.\n",
462                                        fname, this);
463                         clean = ns(wd);
464                         continue;
465                 }
466                 if (eq(wd, "compile-with")) {
467                         wd = get_quoted_word(fp);
468                         if (wd == (char *)EOF || wd == NULL)
469                                 errout("%s: %s missing compile command string.\n",
470                                        fname, this);
471                         compilewith = ns(wd);
472                         continue;
473                 }
474                 if (eq(wd, "warning")) {
475                         wd = get_quoted_word(fp);
476                         if (wd == (char *)EOF || wd == NULL)
477                                 errout("%s: %s missing warning text string.\n",
478                                        fname, this);
479                         warning = ns(wd);
480                         continue;
481                 }
482                 if (eq(wd, "obj-prefix")) {
483                         wd = get_quoted_word(fp);
484                         if (wd == (char *)EOF || wd == NULL)
485                                 errout("%s: %s missing object prefix string.\n",
486                                        fname, this);
487                         objprefix = ns(wd);
488                         continue;
489                 }
490                 if (eq(wd, "nowerror")) {
491                         nowerror = 1;
492                         continue;
493                 }
494                 if (eq(wd, "local")) {
495                         filetype = LOCAL;
496                         continue;
497                 }
498                 if (eq(wd, "no-depend")) {
499                         filetype = NODEPEND;
500                         continue;
501                 }
502                 nreqs++;
503                 if (eq(wd, "profiling-routine")) {
504                         filetype = PROFILING;
505                         continue;
506                 }
507                 if (std)
508                         errout("standard entry %s has optional inclusion specifier %s!\n",
509                                this, wd);
510                 STAILQ_FOREACH(dp, &dtab, d_next)
511                         if (eq(dp->d_name, wd)) {
512                                 if (not)
513                                         match = 0;
514                                 else
515                                         dp->d_done |= DEVDONE;
516                                 goto nextparam;
517                         }
518                 SLIST_FOREACH(op, &opt, op_next)
519                         if (op->op_value == 0 && opteq(op->op_name, wd)) {
520                                 if (not)
521                                         match = 0;
522                                 goto nextparam;
523                         }
524                 match &= not;
525 nextparam:;
526                 not = 0;
527         }
528         compile += match;
529         if (compile && tp == NULL) {
530                 if (std == 0 && nreqs == 0)
531                         errout("%s: what is %s optional on?\n",
532                                fname, this);
533                 if (filetype == PROFILING && profiling == 0)
534                         goto next;
535                 tp = new_fent();
536                 tp->f_fn = this;
537                 tp->f_type = filetype;
538                 if (filetype == LOCAL)
539                         tp->f_srcprefix = "";
540                 else
541                         tp->f_srcprefix = "$S/";
542                 if (imp_rule)
543                         tp->f_flags |= NO_IMPLCT_RULE;
544                 if (no_obj)
545                         tp->f_flags |= NO_OBJ;
546                 if (before_depend)
547                         tp->f_flags |= BEFORE_DEPEND;
548                 if (nowerror)
549                         tp->f_flags |= NOWERROR;
550                 tp->f_compilewith = compilewith;
551                 tp->f_depends = depends;
552                 tp->f_clean = clean;
553                 tp->f_warn = warning;
554                 tp->f_objprefix = objprefix;
555         }
556         goto next;
557 }
558
559 /*
560  * Read in the information about files used in making the system.
561  * Store it in the ftab linked list.
562  */
563 static void
564 read_files(void)
565 {
566         char fname[MAXPATHLEN];
567         struct files_name *nl, *tnl;
568         
569         (void) snprintf(fname, sizeof(fname), "../../conf/files");
570         read_file(fname);
571         (void) snprintf(fname, sizeof(fname),
572                         "../../conf/files.%s", machinename);
573         read_file(fname);
574         for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
575                 read_file(nl->f_name);
576                 tnl = STAILQ_NEXT(nl, f_next);
577                 free(nl->f_name);
578                 free(nl);
579         }
580 }
581
582 static int
583 opteq(const char *cp, const char *dp)
584 {
585         char c, d;
586
587         for (; ; cp++, dp++) {
588                 if (*cp != *dp) {
589                         c = isupper(*cp) ? tolower(*cp) : *cp;
590                         d = isupper(*dp) ? tolower(*dp) : *dp;
591                         if (c != d)
592                                 return (0);
593                 }
594                 if (*cp == 0)
595                         return (1);
596         }
597 }
598
599 static void
600 do_before_depend(FILE *fp)
601 {
602         struct file_list *tp;
603         int lpos, len;
604
605         fputs("BEFORE_DEPEND=", fp);
606         lpos = 15;
607         STAILQ_FOREACH(tp, &ftab, f_next)
608                 if (tp->f_flags & BEFORE_DEPEND) {
609                         len = strlen(tp->f_fn);
610                         if ((len = 3 + len) + lpos > 72) {
611                                 lpos = 8;
612                                 fputs("\\\n\t", fp);
613                         }
614                         if (tp->f_flags & NO_IMPLCT_RULE)
615                                 fprintf(fp, "%s ", tp->f_fn);
616                         else
617                                 fprintf(fp, "%s%s ", tp->f_srcprefix,
618                                     tp->f_fn);
619                         lpos += len + 1;
620                 }
621         if (lpos != 8)
622                 putc('\n', fp);
623 }
624
625 static void
626 do_objs(FILE *fp)
627 {
628         struct file_list *tp;
629         int lpos, len;
630         char *cp, och, *sp;
631
632         fprintf(fp, "OBJS=");
633         lpos = 6;
634         STAILQ_FOREACH(tp, &ftab, f_next) {
635                 if (tp->f_flags & NO_OBJ)
636                         continue;
637                 sp = tail(tp->f_fn);
638                 cp = sp + (len = strlen(sp)) - 1;
639                 och = *cp;
640                 *cp = 'o';
641                 len += strlen(tp->f_objprefix);
642                 if (len + lpos > 72) {
643                         lpos = 8;
644                         fprintf(fp, "\\\n\t");
645                 }
646                 fprintf(fp, "%s%s ", tp->f_objprefix, sp);
647                 lpos += len + 1;
648                 *cp = och;
649         }
650         if (lpos != 8)
651                 putc('\n', fp);
652 }
653
654 static void
655 do_xxfiles(char *tag, FILE *fp)
656 {
657         struct file_list *tp;
658         int lpos, len, slen;
659         char *suff, *SUFF;
660
661         if (tag[strlen(tag) - 1] == '\n')
662                 tag[strlen(tag) - 1] = '\0';
663
664         suff = ns(tag + 7);
665         SUFF = ns(suff);
666         raisestr(SUFF);
667         slen = strlen(suff);
668
669         fprintf(fp, "%sFILES=", SUFF);
670         free(SUFF);
671         lpos = 8;
672         STAILQ_FOREACH(tp, &ftab, f_next)
673                 if (tp->f_type != NODEPEND) {
674                         len = strlen(tp->f_fn);
675                         if (tp->f_fn[len - slen - 1] != '.')
676                                 continue;
677                         if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
678                                 continue;
679                         if ((len = 3 + len) + lpos > 72) {
680                                 lpos = 8;
681                                 fputs("\\\n\t", fp);
682                         }
683                         fprintf(fp, "%s%s ", tp->f_srcprefix, tp->f_fn);
684                         lpos += len + 1;
685                 }
686         free(suff);
687         if (lpos != 8)
688                 putc('\n', fp);
689 }
690
691 static char *
692 tail(char *fn)
693 {
694         char *cp;
695
696         cp = strrchr(fn, '/');
697         if (cp == NULL)
698                 return (fn);
699         return (cp+1);
700 }
701
702 /*
703  * Create the makerules for each file
704  * which is part of the system.
705  */
706 static void
707 do_rules(FILE *f)
708 {
709         char *cp, *np, och;
710         struct file_list *ftp;
711         char *compilewith;
712         char cmd[128];
713
714         STAILQ_FOREACH(ftp, &ftab, f_next) {
715                 if (ftp->f_warn)
716                         fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
717                 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
718                 och = *cp;
719                 if (ftp->f_flags & NO_IMPLCT_RULE) {
720                         if (ftp->f_depends)
721                                 fprintf(f, "%s%s: %s\n",
722                                         ftp->f_objprefix, np, ftp->f_depends);
723                         else
724                                 fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
725                 }
726                 else {
727                         *cp = '\0';
728                         if (och == 'o') {
729                                 fprintf(f, "%s%so:\n\t-cp %s%so .\n\n",
730                                         ftp->f_objprefix, tail(np),
731                                         ftp->f_srcprefix, np);
732                                 continue;
733                         }
734                         if (ftp->f_depends) {
735                                 fprintf(f, "%s%so: %s%s%c %s\n",
736                                         ftp->f_objprefix, tail(np),
737                                         ftp->f_srcprefix, np, och,
738                                         ftp->f_depends);
739                         }
740                         else {
741                                 fprintf(f, "%s%so: %s%s%c\n",
742                                         ftp->f_objprefix, tail(np),
743                                         ftp->f_srcprefix, np, och);
744                         }
745                 }
746                 compilewith = ftp->f_compilewith;
747                 if (compilewith == NULL) {
748                         const char *ftype = NULL;
749
750                         switch (ftp->f_type) {
751                         case NORMAL:
752                                 ftype = "NORMAL";
753                                 break;
754                         case PROFILING:
755                                 if (!profiling)
756                                         continue;
757                                 ftype = "PROFILE";
758                                 break;
759                         default:
760                                 fprintf(stderr,
761                                     "config: don't know rules for %s\n", np);
762                                 break;
763                         }
764                         snprintf(cmd, sizeof(cmd),
765                             "${%s_%c%s}", ftype,
766                             toupper(och),
767                             ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
768                         compilewith = cmd;
769                 }
770                 *cp = och;
771                 if (strlen(ftp->f_objprefix))
772                         fprintf(f, "\t%s %s%s\n", compilewith,
773                             ftp->f_srcprefix, np);
774                 else
775                         fprintf(f, "\t%s\n", compilewith);
776
777                 if (!(ftp->f_flags & NO_OBJ))
778                         fprintf(f, "\t${NORMAL_CTFCONVERT}\n\n");
779                 else
780                         fprintf(f, "\n");
781         }
782 }
783
784 static void
785 do_clean(FILE *fp)
786 {
787         struct file_list *tp;
788         int lpos, len;
789
790         fputs("CLEAN=", fp);
791         lpos = 7;
792         STAILQ_FOREACH(tp, &ftab, f_next)
793                 if (tp->f_clean) {
794                         len = strlen(tp->f_clean);
795                         if (len + lpos > 72) {
796                                 lpos = 8;
797                                 fputs("\\\n\t", fp);
798                         }
799                         fprintf(fp, "%s ", tp->f_clean);
800                         lpos += len + 1;
801                 }
802         if (lpos != 8)
803                 putc('\n', fp);
804 }
805
806 char *
807 raisestr(char *str)
808 {
809         char *cp = str;
810
811         while (*str) {
812                 if (islower(*str))
813                         *str = toupper(*str);
814                 str++;
815         }
816         return (cp);
817 }