2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
40 static const char rcsid[] =
44 #include <sys/types.h>
49 #include <sys/param.h>
61 #include "configvers.h"
71 #define CDIR "../compile/"
74 char destdir[MAXPATHLEN];
75 char srcdir[MAXPATHLEN];
83 * Preserve old behaviour in INCLUDE_CONFIG_FILE handling (files are included
88 static void configfile(void);
89 static void get_srcdir(void);
90 static void usage(void);
91 static void cleanheaders(char *);
92 static void kernconfdump(const char *);
96 struct hdr_list *h_next;
100 * Config builds a set of files for building a UNIX
101 * system given a description of the desired system.
104 main(int argc, char **argv)
110 char xxx[MAXPATHLEN];
114 while ((ch = getopt(argc, argv, "Cd:gpVx:")) != -1)
120 if (*destdir == '\0')
121 strlcpy(destdir, optarg, sizeof(destdir));
123 errx(2, "directory already set");
132 printf("%d\n", CONFIGVERS);
144 if (kernfile != NULL) {
145 kernconfdump(kernfile);
153 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
154 err(2, "%s", PREFIX);
155 if (freopen("DEFAULTS", "r", stdin) != NULL) {
159 if (freopen(PREFIX, "r", stdin) == NULL)
160 err(2, "%s", PREFIX);
163 if (*destdir != '\0') {
164 len = strlen(destdir);
165 while (len > 1 && destdir[len - 1] == '/')
166 destdir[--len] = '\0';
169 strlcpy(destdir, CDIR, sizeof(destdir));
170 strlcat(destdir, PREFIX, sizeof(destdir));
173 p = path((char *)NULL);
177 } else if (!S_ISDIR(buf.st_mode))
178 errx(2, "%s isn't a directory", p);
180 SLIST_INIT(&cputype);
184 STAILQ_INIT(&cfgfiles);
193 * Ensure that required elements (machine, cpu, ident) are present.
195 if (machinename == NULL) {
196 printf("Specify machine type, e.g. ``machine i386''\n");
200 printf("no ident line specified\n");
203 if (SLIST_EMPTY(&cputype)) {
204 printf("cpu type must be specified\n");
209 * make symbolic links in compilation directory
210 * for "sys" (to make genassym.c work along with #include <sys/xxx>)
211 * and similarly for "machine".
214 (void)snprintf(xxx, sizeof(xxx), "../../include");
216 (void)snprintf(xxx, sizeof(xxx), "%s/%s/include",
217 srcdir, machinename);
218 (void) unlink(path("machine"));
219 (void) symlink(xxx, path("machine"));
220 if (strcmp(machinename, machinearch) != 0) {
222 * make symbolic links in compilation directory for
223 * machinearch, if it is different than machinename.
226 (void)snprintf(xxx, sizeof(xxx), "../../../%s/include",
229 (void)snprintf(xxx, sizeof(xxx), "%s/%s/include",
230 srcdir, machinearch);
231 (void) unlink(path(machinearch));
232 (void) symlink(xxx, path(machinearch));
234 configfile(); /* put config file into kernel*/
235 options(); /* make options .h files */
236 makefile(); /* build Makefile */
237 makeenv(); /* build env.c */
238 makehints(); /* build hints.c */
239 headers(); /* make a lot of .h files */
241 printf("Kernel build directory is %s\n", p);
242 printf("Don't forget to do ``make cleandepend && make depend''\n");
248 * determine the root of the kernel source tree
249 * and save that in srcdir.
258 if (realpath("../..", srcdir) == NULL)
259 errx(2, "Unable to find root of source tree");
260 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
261 (pwd = strdup(pwd)) != NULL) {
262 /* Remove the last two path components. */
263 for (i = 0; i < 2; i++) {
264 if ((p = strrchr(pwd, '/')) == NULL) {
270 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
271 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
272 strlcpy(srcdir, pwd, MAXPATHLEN);
281 fprintf(stderr, "usage: config [-CgpV] [-d destdir] sysname\n");
282 fprintf(stderr, " config -x kernel\n");
288 * returns EOF on end of file
289 * NULL on end of line
290 * pointer to the word otherwise
295 static char line[80];
301 while ((ch = getc(fp)) != EOF)
302 if (ch != ' ' && ch != '\t')
305 return ((char *)EOF);
320 while ((ch = getc(fp)) != EOF) {
327 return ((char *)EOF);
328 (void) ungetc(ch, fp);
334 * like get_word but will accept something in double or single quotes
335 * (to allow embedded spaces).
338 get_quoted_word(FILE *fp)
340 static char line[256];
346 while ((ch = getc(fp)) != EOF)
347 if (ch != ' ' && ch != '\t')
350 return ((char *)EOF);
364 if (ch == '"' || ch == '\'') {
367 while ((ch = getc(fp)) != EOF) {
372 printf("config: missing quote reading `%s'\n",
380 while ((ch = getc(fp)) != EOF) {
386 (void) ungetc(ch, fp);
390 return ((char *)EOF);
395 * prepend the path to a filename
398 path(const char *file)
403 asprintf(&cp, "%s/%s", destdir, file);
405 cp = strdup(destdir);
410 * Generate configuration file based on actual settings. With this mode, user
411 * will be able to obtain and build conifguration file with one command.
414 configfile_dynamic(struct sbuf *sb)
416 struct cputype *cput;
422 asprintf(&lend, "\\n\\\n");
423 assert(lend != NULL);
424 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
425 sbuf_printf(sb, "ident\t%s%s", ident, lend);
426 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
427 SLIST_FOREACH(cput, &cputype, cpu_next)
428 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
429 SLIST_FOREACH(ol, &mkopt, op_next)
430 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
432 SLIST_FOREACH(ol, &opt, op_next) {
433 if (strncmp(ol->op_name, "DEV_", 4) == 0)
435 sbuf_printf(sb, "options\t%s", ol->op_name);
436 if (ol->op_value != NULL) {
438 for (i = 0; i < strlen(ol->op_value); i++) {
439 if (ol->op_value[i] == '"')
440 sbuf_printf(sb, "\\%c",
443 sbuf_printf(sb, "%c",
446 sbuf_printf(sb, "%s", lend);
448 sbuf_printf(sb, "%s", lend);
452 * Mark this file as containing everything we need.
454 STAILQ_FOREACH(d, &dtab, d_next)
455 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
460 * Generate file from the configuration files.
463 configfile_filebased(struct sbuf *sb)
470 * Try to read all configuration files. Since those will be present as
471 * C string in the macro, we have to slash their ends then the line
474 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
475 cff = fopen(cf->cfg_path, "r");
477 warn("Couldn't open file %s", cf->cfg_path);
480 while ((i = getc(cff)) != EOF) {
482 sbuf_printf(sb, "\\n\\\n");
483 else if (i == '"' || i == '\'')
484 sbuf_printf(sb, "\\%c", i);
499 /* Add main configuration file to the list of files to be included */
501 p = path("config.c.new");
505 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
509 /* Is needed, can be used for backward compatibility. */
510 configfile_filebased(sb);
512 configfile_dynamic(sb);
516 * We print first part of the tamplate, replace our tag with
517 * configuration files content and later continue writing our
520 p = strstr(kernconfstr, KERNCONFTAG);
522 errx(EXIT_FAILURE, "Something went terribly wrong!");
524 fprintf(fo, "%s", kernconfstr);
525 fprintf(fo, "%s", sbuf_data(sb));
526 p += strlen(KERNCONFTAG);
527 fprintf(fo, "%s", p);
530 moveifchanged(path("config.c.new"), path("config.c"));
536 * compare two files; rename if changed.
539 moveifchanged(const char *from_name, const char *to_name)
544 struct stat from_sb, to_sb;
549 if ((from_fd = open(from_name, O_RDONLY)) < 0)
550 err(EX_OSERR, "moveifchanged open(%s)", from_name);
552 if ((to_fd = open(to_name, O_RDONLY)) < 0)
555 if (!changed && fstat(from_fd, &from_sb) < 0)
556 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
558 if (!changed && fstat(to_fd, &to_sb) < 0)
559 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
561 if (!changed && from_sb.st_size != to_sb.st_size)
564 tsize = (size_t)from_sb.st_size;
567 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
569 err(EX_OSERR, "mmap %s", from_name);
570 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
572 err(EX_OSERR, "mmap %s", to_name);
574 changed = memcmp(p, q, tsize);
579 if (rename(from_name, to_name) < 0)
580 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
582 if (unlink(from_name) < 0)
583 err(EX_OSERR, "unlink(%s)", from_name);
588 cleanheaders(char *p)
592 struct file_list *fl;
597 remember("setdefs.h");
598 STAILQ_FOREACH(fl, &ftab, f_next)
602 * Scan the build directory and clean out stuff that looks like
603 * it might have been a leftover NFOO header, etc.
605 if ((dirp = opendir(p)) == NULL)
606 err(EX_OSERR, "opendir %s", p);
607 while ((dp = readdir(dirp)) != NULL) {
608 i = dp->d_namlen - 2;
609 /* Skip non-headers */
610 if (dp->d_name[i] != '.' || dp->d_name[i + 1] != 'h')
612 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
613 if (index(dp->d_name, '_') &&
614 strncmp(dp->d_name, "opt_", 4) != 0)
616 /* Check if it is a target file */
617 for (hl = htab; hl != NULL; hl = hl->h_next) {
618 if (eq(dp->d_name, hl->h_name)) {
624 printf("Removing stale header: %s\n", dp->d_name);
625 if (unlink(path(dp->d_name)) == -1)
626 warn("unlink %s", dp->d_name);
628 (void)closedir(dirp);
632 remember(const char *file)
637 if ((s = strrchr(file, '/')) != NULL)
642 if (index(s, '_') && strncmp(s, "opt_", 4) != 0) {
646 for (hl = htab; hl != NULL; hl = hl->h_next) {
647 if (eq(s, hl->h_name)) {
652 hl = calloc(1, sizeof(*hl));
659 * This one is quick hack. Will be probably moved to elf(3) interface.
660 * It takes kernel configuration file name, passes it as an argument to
661 * elfdump -a, which output is parsed by some UNIX tools...
664 kernconfdump(const char *file)
668 int error, len, osz, r;
669 unsigned int i, off, size;
672 r = open(file, O_RDONLY);
674 errx(EXIT_FAILURE, "Couldn't open file '%s'", file);
675 error = fstat(r, &st);
677 errx(EXIT_FAILURE, "fstat() failed");
678 if (S_ISDIR(st.st_mode))
679 errx(EXIT_FAILURE, "'%s' is a directory", file);
682 errx(EXIT_FAILURE, "fdopen() failed");
686 errx(EXIT_FAILURE, "Couldn't allocate memory");
687 /* ELF note section header. */
688 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 5 kern_conf"
689 "| tail -2 | cut -d ' ' -f 2 | paste - - -", file);
691 errx(EXIT_FAILURE, "asprintf() failed");
692 pp = popen(cmd, "r");
694 errx(EXIT_FAILURE, "popen() failed");
696 len = fread(o, osz, 1, pp);
698 r = sscanf(o, "%d\t%d", &off, &size);
701 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
702 "file. Either unsupported, or not compiled with "
703 "INCLUDE_CONFIG_FILE", file);
704 r = fseek(fp, off, SEEK_CUR);
706 errx(EXIT_FAILURE, "fseek() failed");
707 for (i = 0; i < size - 1; i++) {
712 * If '\0' is present in the middle of the configuration
713 * string, this means something very weird is happening.
714 * Make such case very visible.
716 assert(r != '\0' && ("Char present in the configuration "
717 "string mustn't be equal to 0"));