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