date and time created 93/05/23 13:50:47 by hibler
[unix-history] / usr / src / usr.sbin / config.new / mkmakefile.c
CommitLineData
a1c56c28
CT
1/*
2 * Copyright (c) 1992 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * All advertising materials mentioning features or use of this software
10 * must display the following acknowledgement:
11 * This product includes software developed by the University of
12 * California, Lawrence Berkeley Laboratories.
13 *
14 * %sccs.include.redist.c%
15 *
b8d1308c 16 * @(#)mkmakefile.c 5.2 (Berkeley) %G%
a1c56c28
CT
17 */
18
19#include <sys/param.h>
20#include <ctype.h>
21#include <errno.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include "config.h"
26
27/*
28 * Make the Makefile.
29 */
30
31static int emitdefs __P((FILE *));
32static int emitobjs __P((FILE *));
33static int emitcfiles __P((FILE *));
34static int emitsfiles __P((FILE *));
35static int emitfiles __P((FILE *, int));
36static int emitrules __P((FILE *));
37static int emitload __P((FILE *));
38
39int
40mkmakefile()
41{
42 register FILE *ifp, *ofp;
43 register int lineno;
44 register int (*fn) __P((FILE *));
45 register char *ofname;
46 char line[BUFSIZ], ifname[200];
47
48 (void)sprintf(ifname, "Makefile.%s", machine);
49 if ((ifp = fopen(ifname, "r")) == NULL) {
50 (void)fprintf(stderr, "config: cannot read %s: %s\n",
51 ifname, strerror(errno));
52 return (1);
53 }
54 ofname = path("Makefile");
55 if ((ofp = fopen(ofname, "w")) == NULL) {
56 (void)fprintf(stderr, "config: cannot write %s: %s\n",
57 ofname, strerror(errno));
58 free(ofname);
59 return (1);
60 }
61 if (emitdefs(ofp) != 0)
62 goto wrerror;
63 lineno = 0;
64 while (fgets(line, sizeof(line), ifp) != NULL) {
65 lineno++;
66 if (line[0] != '%') {
67 if (fputs(line, ofp) < 0)
68 goto wrerror;
69 continue;
70 }
71 if (strcmp(line, "%OBJS\n") == 0)
72 fn = emitobjs;
73 else if (strcmp(line, "%CFILES\n") == 0)
74 fn = emitcfiles;
75 else if (strcmp(line, "%SFILES\n") == 0)
76 fn = emitsfiles;
77 else if (strcmp(line, "%RULES\n") == 0)
78 fn = emitrules;
79 else if (strcmp(line, "%LOAD\n") == 0)
80 fn = emitload;
81 else {
82 xerror(ifname, lineno,
83 "unknown %% construct ignored: %s", line);
84 continue;
85 }
86 if ((*fn)(ofp))
87 goto wrerror;
88 }
89 if (ferror(ifp)) {
90 (void)fprintf(stderr,
91 "config: error reading %s (at line %d): %s\n",
92 ifname, lineno, strerror(errno));
93 goto bad;
94 /* (void)unlink(ofname); */
95 free(ofname);
96 return (1);
97 }
98 if (fclose(ofp)) {
99 ofp = NULL;
100 goto wrerror;
101 }
102 (void)fclose(ifp);
103 free(ofname);
104 return (0);
105wrerror:
106 (void)fprintf(stderr, "config: error writing %s: %s\n",
107 ofname, strerror(errno));
108bad:
109 if (ofp != NULL)
110 (void)fclose(ofp);
111 /* (void)unlink(ofname); */
112 free(ofname);
113 return (1);
114}
115
116static int
117emitdefs(fp)
118 register FILE *fp;
119{
120 register struct nvlist *nv;
121 register char *sp;
122
123 if (fputs("IDENT=", fp) < 0)
124 return (1);
125 sp = "";
126 for (nv = options; nv != NULL; nv = nv->nv_next) {
127 if (fprintf(fp, "%s-D%s%s%s", sp, nv->nv_name,
128 nv->nv_str ? "=" : "", nv->nv_str ? nv->nv_str : "") < 0)
129 return (1);
130 sp = " ";
131 }
132 if (putc('\n', fp) < 0)
133 return (1);
134 if (fprintf(fp, "PARAM=-DMAXUSERS=%d\n", maxusers) < 0)
135 return (1);
136 for (nv = mkoptions; nv != NULL; nv = nv->nv_next)
137 if (fprintf(fp, "%s=%s\n", nv->nv_name, nv->nv_str) < 0)
138 return (1);
139 return (0);
140}
141
142static int
143emitobjs(fp)
144 register FILE *fp;
145{
146 register struct files *fi;
147 register int lpos, len, sp;
148
149 if (fputs("OBJS=", fp) < 0)
150 return (1);
151 sp = '\t';
152 lpos = 7;
153 for (fi = allfiles; fi != NULL; fi = fi->fi_next) {
154 if ((fi->fi_flags & FI_SEL) == 0)
155 continue;
156 len = strlen(fi->fi_base) + 2;
157 if (lpos + len > 72) {
158 if (fputs(" \\\n", fp) < 0)
159 return (1);
160 sp = '\t';
161 lpos = 7;
162 }
163 if (fprintf(fp, "%c%s.o", sp, fi->fi_base) < 0)
164 return (1);
165 lpos += len + 1;
166 sp = ' ';
167 }
168 if (lpos != 7 && putc('\n', fp) < 0)
169 return (1);
170 return (0);
171}
172
173static int
174emitcfiles(fp)
175 FILE *fp;
176{
177
178 return (emitfiles(fp, 'c'));
179}
180
181static int
182emitsfiles(fp)
183 FILE *fp;
184{
185
186 return (emitfiles(fp, 's'));
187}
188
189static int
190emitfiles(fp, suffix)
191 register FILE *fp;
192 int suffix;
193{
194 register struct files *fi;
195 register struct config *cf;
196 register int lpos, len, sp;
197 char swapname[100];
198
199 if (fprintf(fp, "%cFILES=", toupper(suffix)) < 0)
200 return (1);
201 sp = '\t';
202 lpos = 7;
203 for (fi = allfiles; fi != NULL; fi = fi->fi_next) {
204 if ((fi->fi_flags & FI_SEL) == 0)
205 continue;
206 len = strlen(fi->fi_path);
207 if (fi->fi_path[len - 1] != suffix)
208 continue;
209 if (*fi->fi_path != '/')
210 len += 3; /* "$S/" */
211 if (lpos + len > 72) {
212 if (fputs(" \\\n", fp) < 0)
213 return (1);
214 sp = '\t';
215 lpos = 7;
216 }
217 if (fprintf(fp, "%c%s%s", sp, *fi->fi_path != '/' ? "$S/" : "",
218 fi->fi_path) < 0)
219 return (1);
220 lpos += len + 1;
221 sp = ' ';
222 }
223 for (cf = allcf; cf != NULL; cf = cf->cf_next) {
224 if (cf->cf_root == NULL)
225 (void)sprintf(swapname, "$S/%s/%s/swapgeneric.c",
226 machine, machine);
227 else
228 (void)sprintf(swapname, "swap%s.c", cf->cf_name);
229 len = strlen(swapname);
230 if (lpos + len > 72) {
231 if (fputs(" \\\n", fp) < 0)
232 return (1);
233 sp = '\t';
234 lpos = 7;
235 }
236 if (fprintf(fp, "%c%s", sp, swapname) < 0)
237 return (1);
238 lpos += len + 1;
239 sp = ' ';
240 }
241 if (lpos != 7 && putc('\n', fp) < 0)
242 return (1);
243 return (0);
244}
245
246/*
247 * Emit the make-rules.
248 */
249static int
250emitrules(fp)
251 register FILE *fp;
252{
253 register struct files *fi;
254 register const char *cp;
255 int ch;
256 char buf[200];
257
258 for (fi = allfiles; fi != NULL; fi = fi->fi_next) {
259 if ((fi->fi_flags & FI_SEL) == 0)
260 continue;
261 if (fprintf(fp, "%s.o: %s%s\n", fi->fi_base,
262 *fi->fi_path != '/' ? "$S/" : "", fi->fi_path) < 0)
263 return (1);
264 if ((cp = fi->fi_mkrule) == NULL) {
265 cp = fi->fi_flags & FI_DRIVER ? "DRIVER" : "NORMAL";
266 ch = fi->fi_lastc;
267 if (islower(ch))
268 ch = toupper(ch);
269 (void)sprintf(buf, "${%s_%c%s}", cp, ch,
270 fi->fi_flags & FI_CONFIGDEP ? "_C" : "");
271 cp = buf;
272 }
273 if (fprintf(fp, "\t%s\n\n", cp) < 0)
274 return (1);
275 }
276 return (0);
277}
278
279/*
280 * Emit the load commands.
281 *
282 * This function is not to be called `spurt'.
283 */
284static int
285emitload(fp)
286 register FILE *fp;
287{
288 register struct config *cf;
289 register const char *nm, *swname;
290 int first;
291
292 if (fputs("all:", fp) < 0)
293 return (1);
294 for (cf = allcf; cf != NULL; cf = cf->cf_next) {
295 if (fprintf(fp, " %s", cf->cf_name) < 0)
296 return (1);
297 }
298 if (fputs("\n\n", fp) < 0)
299 return (1);
300 for (first = 1, cf = allcf; cf != NULL; cf = cf->cf_next) {
301 nm = cf->cf_name;
302 swname = cf->cf_root != NULL ? cf->cf_name : "generic";
303 if (fprintf(fp, "%s: ${SYSTEM_DEP} swap%s.o", nm, swname) < 0)
304 return (1);
305 if (first) {
306 if (fputs(" newvers", fp) < 0)
307 return (1);
308 first = 0;
309 }
310 if (fprintf(fp, "\n\
311\t${SYSTEM_LD_HEAD}\n\
312\t${SYSTEM_LD} swap%s.o\n\
313\t${SYSTEM_LD_TAIL}\n\
314\n\
315swap%s.o: ", swname, swname) < 0)
316 return (1);
317 if (cf->cf_root != NULL) {
318 if (fprintf(fp, "swap%s.c\n", nm) < 0)
319 return (1);
320 } else {
321 if (fprintf(fp, "$S/%s/%s/swapgeneric.c\n",
322 machine, machine) < 0)
323 return (1);
324 }
325 if (fputs("\t${NORMAL_C}\n\n", fp) < 0)
326 return (1);
327 }
328 return (0);
329}