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