]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/config/mkmakefile.c
Revert r335995 due to accidental changes snuck in
[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         fclose(ofp);
340         moveifchanged(path("env.c.new"), path("env.c"));
341 }
342
343 static void
344 read_file(char *fname)
345 {
346         char ifname[MAXPATHLEN];
347         FILE *fp;
348         struct file_list *tp;
349         struct device *dp;
350         struct opt *op;
351         char *wd, *this, *compilewith, *depends, *clean, *warning;
352         const char *objprefix;
353         int compile, match, nreqs, std, filetype, not,
354             imp_rule, no_obj, before_depend, nowerror;
355
356         fp = fopen(fname, "r");
357         if (fp == NULL)
358                 err(1, "%s", fname);
359 next:
360         /*
361          * include "filename"
362          * filename    [ standard | optional ]
363          *      [ dev* [ | dev* ... ] | profiling-routine ] [ no-obj ]
364          *      [ compile-with "compile rule" [no-implicit-rule] ]
365          *      [ dependency "dependency-list"] [ before-depend ]
366          *      [ clean "file-list"] [ warning "text warning" ]
367          *      [ obj-prefix "file prefix"]
368          */
369         wd = get_word(fp);
370         if (wd == (char *)EOF) {
371                 (void) fclose(fp);
372                 return;
373         } 
374         if (wd == NULL)
375                 goto next;
376         if (wd[0] == '#')
377         {
378                 while (((wd = get_word(fp)) != (char *)EOF) && wd)
379                         ;
380                 goto next;
381         }
382         if (eq(wd, "include")) {
383                 wd = get_quoted_word(fp);
384                 if (wd == (char *)EOF || wd == NULL)
385                         errout("%s: missing include filename.\n", fname);
386                 (void) snprintf(ifname, sizeof(ifname), "../../%s", wd);
387                 read_file(ifname);
388                 while (((wd = get_word(fp)) != (char *)EOF) && wd)
389                         ;
390                 goto next;
391         }
392         this = ns(wd);
393         wd = get_word(fp);
394         if (wd == (char *)EOF)
395                 return;
396         if (wd == NULL)
397                 errout("%s: No type for %s.\n", fname, this);
398         tp = fl_lookup(this);
399         compile = 0;
400         match = 1;
401         nreqs = 0;
402         compilewith = 0;
403         depends = 0;
404         clean = 0;
405         warning = 0;
406         std = 0;
407         imp_rule = 0;
408         no_obj = 0;
409         before_depend = 0;
410         nowerror = 0;
411         not = 0;
412         filetype = NORMAL;
413         objprefix = "";
414         if (eq(wd, "standard"))
415                 std = 1;
416         else if (!eq(wd, "optional"))
417                 errout("%s: \"%s\" %s must be optional or standard\n",
418                     fname, wd, this);
419         for (wd = get_word(fp); wd; wd = get_word(fp)) {
420                 if (wd == (char *)EOF)
421                         return;
422                 if (eq(wd, "!")) {
423                         not = 1;
424                         continue;
425                 }
426                 if (eq(wd, "|")) {
427                         if (nreqs == 0)
428                                 errout("%s: syntax error describing %s\n",
429                                        fname, this);
430                         compile += match;
431                         match = 1;
432                         nreqs = 0;
433                         continue;
434                 }
435                 if (eq(wd, "no-obj")) {
436                         no_obj++;
437                         continue;
438                 }
439                 if (eq(wd, "no-implicit-rule")) {
440                         if (compilewith == NULL)
441                                 errout("%s: alternate rule required when "
442                                        "\"no-implicit-rule\" is specified for"
443                                        " %s.\n",
444                                        fname, this);
445                         imp_rule++;
446                         continue;
447                 }
448                 if (eq(wd, "before-depend")) {
449                         before_depend++;
450                         continue;
451                 }
452                 if (eq(wd, "dependency")) {
453                         wd = get_quoted_word(fp);
454                         if (wd == (char *)EOF || wd == NULL)
455                                 errout("%s: %s missing dependency string.\n",
456                                        fname, this);
457                         depends = ns(wd);
458                         continue;
459                 }
460                 if (eq(wd, "clean")) {
461                         wd = get_quoted_word(fp);
462                         if (wd == (char *)EOF || wd == NULL)
463                                 errout("%s: %s missing clean file list.\n",
464                                        fname, this);
465                         clean = ns(wd);
466                         continue;
467                 }
468                 if (eq(wd, "compile-with")) {
469                         wd = get_quoted_word(fp);
470                         if (wd == (char *)EOF || wd == NULL)
471                                 errout("%s: %s missing compile command string.\n",
472                                        fname, this);
473                         compilewith = ns(wd);
474                         continue;
475                 }
476                 if (eq(wd, "warning")) {
477                         wd = get_quoted_word(fp);
478                         if (wd == (char *)EOF || wd == NULL)
479                                 errout("%s: %s missing warning text string.\n",
480                                        fname, this);
481                         warning = ns(wd);
482                         continue;
483                 }
484                 if (eq(wd, "obj-prefix")) {
485                         wd = get_quoted_word(fp);
486                         if (wd == (char *)EOF || wd == NULL)
487                                 errout("%s: %s missing object prefix string.\n",
488                                        fname, this);
489                         objprefix = ns(wd);
490                         continue;
491                 }
492                 if (eq(wd, "nowerror")) {
493                         nowerror = 1;
494                         continue;
495                 }
496                 if (eq(wd, "local")) {
497                         filetype = LOCAL;
498                         continue;
499                 }
500                 if (eq(wd, "no-depend")) {
501                         filetype = NODEPEND;
502                         continue;
503                 }
504                 nreqs++;
505                 if (eq(wd, "profiling-routine")) {
506                         filetype = PROFILING;
507                         continue;
508                 }
509                 if (std)
510                         errout("standard entry %s has optional inclusion specifier %s!\n",
511                                this, wd);
512                 STAILQ_FOREACH(dp, &dtab, d_next)
513                         if (eq(dp->d_name, wd)) {
514                                 if (not)
515                                         match = 0;
516                                 else
517                                         dp->d_done |= DEVDONE;
518                                 goto nextparam;
519                         }
520                 SLIST_FOREACH(op, &opt, op_next)
521                         if (op->op_value == 0 && opteq(op->op_name, wd)) {
522                                 if (not)
523                                         match = 0;
524                                 goto nextparam;
525                         }
526                 match &= not;
527 nextparam:;
528                 not = 0;
529         }
530         compile += match;
531         if (compile && tp == NULL) {
532                 if (std == 0 && nreqs == 0)
533                         errout("%s: what is %s optional on?\n",
534                                fname, this);
535                 if (filetype == PROFILING && profiling == 0)
536                         goto next;
537                 tp = new_fent();
538                 tp->f_fn = this;
539                 tp->f_type = filetype;
540                 if (filetype == LOCAL)
541                         tp->f_srcprefix = "";
542                 else
543                         tp->f_srcprefix = "$S/";
544                 if (imp_rule)
545                         tp->f_flags |= NO_IMPLCT_RULE;
546                 if (no_obj)
547                         tp->f_flags |= NO_OBJ;
548                 if (before_depend)
549                         tp->f_flags |= BEFORE_DEPEND;
550                 if (nowerror)
551                         tp->f_flags |= NOWERROR;
552                 tp->f_compilewith = compilewith;
553                 tp->f_depends = depends;
554                 tp->f_clean = clean;
555                 tp->f_warn = warning;
556                 tp->f_objprefix = objprefix;
557         }
558         goto next;
559 }
560
561 /*
562  * Read in the information about files used in making the system.
563  * Store it in the ftab linked list.
564  */
565 static void
566 read_files(void)
567 {
568         char fname[MAXPATHLEN];
569         struct files_name *nl, *tnl;
570         
571         (void) snprintf(fname, sizeof(fname), "../../conf/files");
572         read_file(fname);
573         (void) snprintf(fname, sizeof(fname),
574                         "../../conf/files.%s", machinename);
575         read_file(fname);
576         for (nl = STAILQ_FIRST(&fntab); nl != NULL; nl = tnl) {
577                 read_file(nl->f_name);
578                 tnl = STAILQ_NEXT(nl, f_next);
579                 free(nl->f_name);
580                 free(nl);
581         }
582 }
583
584 static int
585 opteq(const char *cp, const char *dp)
586 {
587         char c, d;
588
589         for (; ; cp++, dp++) {
590                 if (*cp != *dp) {
591                         c = isupper(*cp) ? tolower(*cp) : *cp;
592                         d = isupper(*dp) ? tolower(*dp) : *dp;
593                         if (c != d)
594                                 return (0);
595                 }
596                 if (*cp == 0)
597                         return (1);
598         }
599 }
600
601 static void
602 do_before_depend(FILE *fp)
603 {
604         struct file_list *tp;
605         int lpos, len;
606
607         fputs("BEFORE_DEPEND=", fp);
608         lpos = 15;
609         STAILQ_FOREACH(tp, &ftab, f_next)
610                 if (tp->f_flags & BEFORE_DEPEND) {
611                         len = strlen(tp->f_fn);
612                         if ((len = 3 + len) + lpos > 72) {
613                                 lpos = 8;
614                                 fputs("\\\n\t", fp);
615                         }
616                         if (tp->f_flags & NO_IMPLCT_RULE)
617                                 fprintf(fp, "%s ", tp->f_fn);
618                         else
619                                 fprintf(fp, "%s%s ", tp->f_srcprefix,
620                                     tp->f_fn);
621                         lpos += len + 1;
622                 }
623         if (lpos != 8)
624                 putc('\n', fp);
625 }
626
627 static void
628 do_objs(FILE *fp)
629 {
630         struct file_list *tp;
631         int lpos, len;
632         char *cp, och, *sp;
633
634         fprintf(fp, "OBJS=");
635         lpos = 6;
636         STAILQ_FOREACH(tp, &ftab, f_next) {
637                 if (tp->f_flags & NO_OBJ)
638                         continue;
639                 sp = tail(tp->f_fn);
640                 cp = sp + (len = strlen(sp)) - 1;
641                 och = *cp;
642                 *cp = 'o';
643                 len += strlen(tp->f_objprefix);
644                 if (len + lpos > 72) {
645                         lpos = 8;
646                         fprintf(fp, "\\\n\t");
647                 }
648                 fprintf(fp, "%s%s ", tp->f_objprefix, sp);
649                 lpos += len + 1;
650                 *cp = och;
651         }
652         if (lpos != 8)
653                 putc('\n', fp);
654 }
655
656 static void
657 do_xxfiles(char *tag, FILE *fp)
658 {
659         struct file_list *tp;
660         int lpos, len, slen;
661         char *suff, *SUFF;
662
663         if (tag[strlen(tag) - 1] == '\n')
664                 tag[strlen(tag) - 1] = '\0';
665
666         suff = ns(tag + 7);
667         SUFF = ns(suff);
668         raisestr(SUFF);
669         slen = strlen(suff);
670
671         fprintf(fp, "%sFILES=", SUFF);
672         free(SUFF);
673         lpos = 8;
674         STAILQ_FOREACH(tp, &ftab, f_next)
675                 if (tp->f_type != NODEPEND) {
676                         len = strlen(tp->f_fn);
677                         if (tp->f_fn[len - slen - 1] != '.')
678                                 continue;
679                         if (strcasecmp(&tp->f_fn[len - slen], suff) != 0)
680                                 continue;
681                         if ((len = 3 + len) + lpos > 72) {
682                                 lpos = 8;
683                                 fputs("\\\n\t", fp);
684                         }
685                         fprintf(fp, "%s%s ", tp->f_srcprefix, tp->f_fn);
686                         lpos += len + 1;
687                 }
688         free(suff);
689         if (lpos != 8)
690                 putc('\n', fp);
691 }
692
693 static char *
694 tail(char *fn)
695 {
696         char *cp;
697
698         cp = strrchr(fn, '/');
699         if (cp == NULL)
700                 return (fn);
701         return (cp+1);
702 }
703
704 /*
705  * Create the makerules for each file
706  * which is part of the system.
707  */
708 static void
709 do_rules(FILE *f)
710 {
711         char *cp, *np, och;
712         struct file_list *ftp;
713         char *compilewith;
714         char cmd[128];
715
716         STAILQ_FOREACH(ftp, &ftab, f_next) {
717                 if (ftp->f_warn)
718                         fprintf(stderr, "WARNING: %s\n", ftp->f_warn);
719                 cp = (np = ftp->f_fn) + strlen(ftp->f_fn) - 1;
720                 och = *cp;
721                 if (ftp->f_flags & NO_IMPLCT_RULE) {
722                         if (ftp->f_depends)
723                                 fprintf(f, "%s%s: %s\n",
724                                         ftp->f_objprefix, np, ftp->f_depends);
725                         else
726                                 fprintf(f, "%s%s: \n", ftp->f_objprefix, np);
727                 }
728                 else {
729                         *cp = '\0';
730                         if (och == 'o') {
731                                 fprintf(f, "%s%so:\n\t-cp %s%so .\n\n",
732                                         ftp->f_objprefix, tail(np),
733                                         ftp->f_srcprefix, np);
734                                 continue;
735                         }
736                         if (ftp->f_depends) {
737                                 fprintf(f, "%s%so: %s%s%c %s\n",
738                                         ftp->f_objprefix, tail(np),
739                                         ftp->f_srcprefix, np, och,
740                                         ftp->f_depends);
741                         }
742                         else {
743                                 fprintf(f, "%s%so: %s%s%c\n",
744                                         ftp->f_objprefix, tail(np),
745                                         ftp->f_srcprefix, np, och);
746                         }
747                 }
748                 compilewith = ftp->f_compilewith;
749                 if (compilewith == NULL) {
750                         const char *ftype = NULL;
751
752                         switch (ftp->f_type) {
753                         case NORMAL:
754                                 ftype = "NORMAL";
755                                 break;
756                         case PROFILING:
757                                 if (!profiling)
758                                         continue;
759                                 ftype = "PROFILE";
760                                 break;
761                         default:
762                                 fprintf(stderr,
763                                     "config: don't know rules for %s\n", np);
764                                 break;
765                         }
766                         snprintf(cmd, sizeof(cmd),
767                             "${%s_%c%s}", ftype,
768                             toupper(och),
769                             ftp->f_flags & NOWERROR ? "_NOWERROR" : "");
770                         compilewith = cmd;
771                 }
772                 *cp = och;
773                 if (strlen(ftp->f_objprefix))
774                         fprintf(f, "\t%s %s%s\n", compilewith,
775                             ftp->f_srcprefix, np);
776                 else
777                         fprintf(f, "\t%s\n", compilewith);
778
779                 if (!(ftp->f_flags & NO_OBJ))
780                         fprintf(f, "\t${NORMAL_CTFCONVERT}\n\n");
781                 else
782                         fprintf(f, "\n");
783         }
784 }
785
786 static void
787 do_clean(FILE *fp)
788 {
789         struct file_list *tp;
790         int lpos, len;
791
792         fputs("CLEAN=", fp);
793         lpos = 7;
794         STAILQ_FOREACH(tp, &ftab, f_next)
795                 if (tp->f_clean) {
796                         len = strlen(tp->f_clean);
797                         if (len + lpos > 72) {
798                                 lpos = 8;
799                                 fputs("\\\n\t", fp);
800                         }
801                         fprintf(fp, "%s ", tp->f_clean);
802                         lpos += len + 1;
803                 }
804         if (lpos != 8)
805                 putc('\n', fp);
806 }
807
808 char *
809 raisestr(char *str)
810 {
811         char *cp = str;
812
813         while (*str) {
814                 if (islower(*str))
815                         *str = toupper(*str);
816                 str++;
817         }
818         return (cp);
819 }