1 /* $NetBSD: mkmakefile.c,v 1.12 2009/03/13 20:44:59 cube Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratories.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * from: @(#)mkmakefile.c 8.1 (Berkeley) 6/6/93
43 #if HAVE_NBTOOL_CONFIG_H
44 #include "nbtool_config.h"
47 #include <sys/param.h>
61 static const char *srcpath(struct files
*);
63 static const char *prefix_prologue(const char *);
64 static const char *filetype_prologue(struct filetype
*);
67 static void emitdefs(FILE *);
68 static void emitfiles(FILE *, int, int);
70 static void emitobjs(FILE *);
71 static void emitcfiles(FILE *);
72 static void emitsfiles(FILE *);
73 static void emitrules(FILE *);
74 static void emitload(FILE *);
75 static void emitincludes(FILE *);
76 static void emitappmkoptions(FILE *);
77 static void emitsubs(FILE *, const char *, const char *, int);
78 static int selectopt(const char *, void *);
87 char line
[BUFSIZ
], buf
[200];
89 /* Try a makefile for the port first.
91 (void)snprintf(buf
, sizeof(buf
), "arch/%s/conf/Makefile.%s",
93 ifname
= sourcepath(buf
);
94 if ((ifp
= fopen(ifname
, "r")) == NULL
) {
95 /* Try a makefile for the architecture second.
97 (void)snprintf(buf
, sizeof(buf
), "arch/%s/conf/Makefile.%s",
98 machinearch
, machinearch
);
100 ifname
= sourcepath(buf
);
101 ifp
= fopen(ifname
, "r");
104 warn("cannot read %s", ifname
);
108 if ((ofp
= fopen("Makefile.tmp", "w")) == NULL
) {
109 warn("cannot write Makefile");
116 while (fgets(line
, sizeof(line
), ifp
) != NULL
) {
118 if ((version
< 20090214 && line
[0] != '%') || line
[0] == '#') {
122 if (strcmp(line
, "%OBJS\n") == 0)
124 else if (strcmp(line
, "%CFILES\n") == 0)
126 else if (strcmp(line
, "%SFILES\n") == 0)
128 else if (strcmp(line
, "%RULES\n") == 0)
130 else if (strcmp(line
, "%LOAD\n") == 0)
132 else if (strcmp(line
, "%INCLUDES\n") == 0)
134 else if (strcmp(line
, "%MAKEOPTIONSAPPEND\n") == 0)
135 fn
= emitappmkoptions
;
136 else if (strncmp(line
, "%VERSION ", sizeof("%VERSION ")-1) == 0) {
138 if (sscanf(line
, "%%VERSION %d\n", &newvers
) != 1) {
139 cfgxerror(ifname
, lineno
, "syntax error for "
145 if (version
< 20090214)
146 cfgxerror(ifname
, lineno
,
147 "unknown %% construct ignored: %s", line
);
149 emitsubs(ofp
, line
, ifname
, lineno
);
160 warn("error reading %s (at line %d)", ifname
, lineno
);
170 if (moveifchanged("Makefile.tmp", "Makefile") != 0) {
171 warn("error renaming Makefile");
178 warn("error writing Makefile");
184 /* (void)unlink("Makefile.tmp"); */
191 emitsubs(FILE *fp
, const char *line
, const char *file
, int lineno
)
195 struct nvlist
*option
;
197 while (*line
!= '\0') {
204 nextpct
= strchr(line
, '%');
205 if (nextpct
== NULL
) {
206 cfgxerror(file
, lineno
, "unbalanced %% or "
207 "unknown construct");
215 optname
= intern(line
);
216 if (!DEFINED_OPTION(optname
)) {
217 cfgxerror(file
, lineno
, "unknown option %s",
222 if ((option
= ht_lookup(opttab
, optname
)) == NULL
)
223 option
= ht_lookup(fsopttab
, optname
);
225 fputs(option
->nv_str
? option
->nv_str
: "1",
227 /* Otherwise it's not a selected option and we don't
228 * output anything. */
236 * Return (possibly in a static buffer) the name of the `source' for a
237 * file. If we have `options source', or if the file is marked `always
238 * source', this is always the path from the `file' line; otherwise we
239 * get the .o from the obj-directory.
242 srcpath(struct files
*fi
)
245 /* Always have source, don't support object dirs for kernel builds. */
246 return (fi
->fi_path
);
248 static char buf
[MAXPATHLEN
];
250 if (have_source
|| (fi
->fi_flags
& FI_ALWAYSSRC
) != 0)
251 return (fi
->fi_path
);
252 if (objpath
== NULL
) {
253 cfgerror("obj-directory not set");
256 (void)snprintf(buf
, sizeof buf
, "%s/%s.o", objpath
, fi
->fi_base
);
262 filetype_prologue(struct filetype
*fit
)
264 if (fit
->fit_flags
& FIT_NOPROLOGUE
|| *fit
->fit_path
== '/')
271 prefix_prologue(const char *path
)
285 fprintf(fp
, "KERNEL_BUILD=%s\n", conffile
);
288 for (nv
= options
; nv
!= NULL
; nv
= nv
->nv_next
) {
290 /* skip any options output to a header file */
291 if (DEFINED_OPTION(nv
->nv_name
))
293 fprintf(fp
, "%s-D%s", sp
, nv
->nv_name
);
295 fprintf(fp
, "=\"%s\"", nv
->nv_str
);
299 fprintf(fp
, "PARAM=-DMAXUSERS=%d\n", maxusers
);
300 fprintf(fp
, "MACHINE=%s\n", machine
);
301 if (*srcdir
== '/' || *srcdir
== '.') {
302 fprintf(fp
, "S=\t%s\n", srcdir
);
305 * libkern and libcompat "Makefile.inc"s want relative S
306 * specification to begin with '.'.
308 fprintf(fp
, "S=\t./%s\n", srcdir
);
310 for (nv
= mkoptions
; nv
!= NULL
; nv
= nv
->nv_next
)
311 fprintf(fp
, "%s=%s\n", nv
->nv_name
, nv
->nv_str
);
324 TAILQ_FOREACH(fi
, &allfiles
, fi_next
) {
325 if ((fi
->fi_flags
& FI_SEL
) == 0)
327 len
= strlen(fi
->fi_base
) + 2;
328 if (lpos
+ len
> 72) {
333 fprintf(fp
, "%c%s.o", sp
, fi
->fi_base
);
337 TAILQ_FOREACH(oi
, &allobjects
, oi_next
) {
338 if ((oi
->oi_flags
& OI_SEL
) == 0)
340 len
= strlen(oi
->oi_path
);
341 if (*oi
->oi_path
!= '/')
344 if (oi
->oi_prefix
!= NULL
)
345 len
+= strlen(prefix_prologue(oi
->oi_path
)) +
346 strlen(oi
->oi_prefix
) + 1;
348 len
+= strlen(filetype_prologue(&oi
->oi_fit
));
350 if (lpos
+ len
> 72) {
355 if (*oi
->oi_path
== '/') {
356 fprintf(fp
, "%c%s", sp
, oi
->oi_path
);
358 if (oi
->oi_prefix
!= NULL
) {
359 fprintf(fp
, "%c%s%s/%s", sp
,
360 prefix_prologue(oi
->oi_path
),
361 oi
->oi_prefix
, oi
->oi_path
);
363 fprintf(fp
, "%c%s%s", sp
,
364 filetype_prologue(&oi
->oi_fit
),
378 emitfiles(fp
, 'c', 0);
385 emitfiles(fp
, 's', 'S');
389 emitfiles(FILE *fp
, int suffix
, int upper_suffix
)
397 fprintf(fp
, "%cFILES=", toupper(suffix
));
400 TAILQ_FOREACH(fi
, &allfiles
, fi_next
) {
401 if ((fi
->fi_flags
& FI_SEL
) == 0)
405 if (fpath
[len
- 1] != suffix
&& fpath
[len
- 1] != upper_suffix
)
409 if (fi
->fi_prefix
!= NULL
)
410 len
+= strlen(prefix_prologue(fi
->fi_prefix
)) +
411 strlen(fi
->fi_prefix
) + 1;
413 len
+= strlen(filetype_prologue(&fi
->fi_fit
));
415 if (lpos
+ len
> 72) {
420 if (*fi
->fi_path
== '/') {
421 fprintf(fp
, "%c%s", sp
, fpath
);
423 if (fi
->fi_prefix
!= NULL
) {
424 fprintf(fp
, "%c%s%s/%s", sp
,
425 prefix_prologue(fi
->fi_prefix
),
426 fi
->fi_prefix
, fpath
);
428 fprintf(fp
, "%c%s%s", sp
,
429 filetype_prologue(&fi
->fi_fit
),
437 * The allfiles list does not include the configuration-specific
438 * C source files. These files should be eliminated someday, but
439 * for now, we have to add them to ${CFILES} (and only ${CFILES}).
442 TAILQ_FOREACH(cf
, &allcf
, cf_next
) {
443 (void)snprintf(swapname
, sizeof(swapname
), "swap%s.c",
445 len
= strlen(swapname
);
446 if (lpos
+ len
> 72) {
451 fprintf(fp
, "%c%s", sp
, swapname
);
460 * Emit the make-rules.
466 const char *cp
, *fpath
;
469 TAILQ_FOREACH(fi
, &allfiles
, fi_next
) {
470 if ((fi
->fi_flags
& FI_SEL
) == 0)
474 fprintf(fp
, "%s.o: %s\n", fi
->fi_base
, fpath
);
476 if (fi
->fi_prefix
!= NULL
) {
477 fprintf(fp
, "%s.o: %s%s/%s\n", fi
->fi_base
,
478 prefix_prologue(fi
->fi_prefix
),
479 fi
->fi_prefix
, fpath
);
481 fprintf(fp
, "%s.o: %s%s\n",
483 filetype_prologue(&fi
->fi_fit
),
487 if (fi
->fi_mkrule
!= NULL
) {
488 fprintf(fp
, "\t%s\n\n", fi
->fi_mkrule
);
490 fputs("\t${NORMAL_", fp
);
491 cp
= strrchr(fpath
, '.');
492 cp
= cp
== NULL
? fpath
: cp
+ 1;
493 while ((ch
= *cp
++) != '\0') {
494 fputc(toupper(ch
), fp
);
502 * Emit the load commands.
504 * This function is not to be called `spurt'.
510 const char *nm
, *swname
;
512 fputs(".MAIN: all\nall:", fp
);
513 TAILQ_FOREACH(cf
, &allcf
, cf_next
) {
514 fprintf(fp
, " %s", cf
->cf_name
);
517 TAILQ_FOREACH(cf
, &allcf
, cf_next
) {
520 cf
->cf_root
!= NULL
? cf
->cf_name
: "generic";
521 fprintf(fp
, "KERNELS+=%s\n", nm
);
522 fprintf(fp
, "%s: ${SYSTEM_DEP} swap${.TARGET}.o vers.o", nm
);
524 "\t${SYSTEM_LD_HEAD}\n"
525 "\t${SYSTEM_LD} swap${.TARGET}.o\n"
526 "\t${SYSTEM_LD_TAIL}\n"
528 "swap%s.o: swap%s.c\n"
529 "\t${NORMAL_C}\n\n", swname
, swname
);
534 * Emit include headers (for any prefixes encountered)
537 emitincludes(FILE *fp
)
541 SLIST_FOREACH(pf
, &allprefixes
, pf_next
) {
542 fprintf(fp
, "EXTRA_INCLUDES+=\t-I%s%s\n",
543 prefix_prologue(pf
->pf_prefix
), pf
->pf_prefix
);
548 * Emit appending makeoptions.
551 emitappmkoptions(FILE *fp
)
555 for (nv
= appmkoptions
; nv
!= NULL
; nv
= nv
->nv_next
)
556 fprintf(fp
, "%s+=%s\n", nv
->nv_name
, nv
->nv_str
);
558 for (nv
= condmkoptions
; nv
!= NULL
; nv
= nv
->nv_next
)
560 if (expr_eval(nv
->nv_ptr
, selectopt
, NULL
))
561 fprintf(fp
, "%s+=%s\n", nv
->nv_name
, nv
->nv_str
);
562 expr_free(nv
->nv_ptr
);
568 selectopt(const char *name
, void *context
)
571 return (ht_lookup(selecttab
, strtolower(name
)) != NULL
);