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