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 *);
93 static void checkversion(void);
94 extern int yyparse(void);
98 struct hdr_list *h_next;
102 * Config builds a set of files for building a UNIX
103 * system given a description of the desired system.
106 main(int argc, char **argv)
113 struct includepath* ipath;
118 SLIST_INIT(&includepath);
119 while ((ch = getopt(argc, argv, "CI:d:gmpsVx:")) != -1)
125 ipath = (struct includepath *) \
126 calloc(1, sizeof (struct includepath));
128 err(EXIT_FAILURE, "calloc");
129 ipath->path = optarg;
130 SLIST_INSERT_HEAD(&includepath, ipath, path_next);
136 if (*destdir == '\0')
137 strlcpy(destdir, optarg, sizeof(destdir));
139 errx(EXIT_FAILURE, "directory already set");
149 strlcpy(srcdir, optarg, sizeof(srcdir));
151 errx(EXIT_FAILURE, "src directory already set");
154 printf("%d\n", CONFIGVERS);
166 if (kernfile != NULL) {
167 kernconfdump(kernfile);
175 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
176 err(2, "%s", PREFIX);
177 if (freopen("DEFAULTS", "r", stdin) != NULL) {
181 if (freopen(PREFIX, "r", stdin) == NULL)
182 err(2, "%s", PREFIX);
185 if (*destdir != '\0') {
186 len = strlen(destdir);
187 while (len > 1 && destdir[len - 1] == '/')
188 destdir[--len] = '\0';
192 strlcpy(destdir, CDIR, sizeof(destdir));
193 strlcat(destdir, PREFIX, sizeof(destdir));
196 SLIST_INIT(&cputype);
200 STAILQ_INIT(&cfgfiles);
209 * Ensure that required elements (machine, cpu, ident) are present.
211 if (machinename == NULL) {
212 printf("Specify machine type, e.g. ``machine i386''\n");
216 printf("no ident line specified\n");
219 if (SLIST_EMPTY(&cputype)) {
220 printf("cpu type must be specified\n");
226 printf("%s\t%s\n",machinename,machinearch);
230 /* Make compile directory */
231 p = path((char *)NULL);
235 } else if (!S_ISDIR(buf.st_mode))
236 errx(EXIT_FAILURE, "%s isn't a directory", p);
238 configfile(); /* put config file into kernel*/
239 options(); /* make options .h files */
240 makefile(); /* build Makefile */
241 makeenv(); /* build env.c */
242 makehints(); /* build hints.c */
243 headers(); /* make a lot of .h files */
245 printf("Kernel build directory is %s\n", p);
246 printf("Don't forget to do ``make cleandepend && make depend''\n");
252 * determine the root of the kernel source tree
253 * and save that in srcdir.
262 if (realpath("../..", srcdir) == NULL)
263 err(EXIT_FAILURE, "Unable to find root of source tree");
264 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
265 (pwd = strdup(pwd)) != NULL) {
266 /* Remove the last two path components. */
267 for (i = 0; i < 2; i++) {
268 if ((p = strrchr(pwd, '/')) == NULL) {
274 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
275 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
276 strlcpy(srcdir, pwd, MAXPATHLEN);
286 "usage: config [-CgmpV] [-d destdir] [-s srcdir] sysname\n");
287 fprintf(stderr, " config -x kernel\n");
293 * returns EOF on end of file
294 * NULL on end of line
295 * pointer to the word otherwise
300 static char line[80];
306 while ((ch = getc(fp)) != EOF)
307 if (ch != ' ' && ch != '\t')
310 return ((char *)EOF);
325 /* Negation operator is a word by itself. */
330 while ((ch = getc(fp)) != EOF) {
337 return ((char *)EOF);
338 (void) ungetc(ch, fp);
344 * like get_word but will accept something in double or single quotes
345 * (to allow embedded spaces).
348 get_quoted_word(FILE *fp)
350 static char line[256];
356 while ((ch = getc(fp)) != EOF)
357 if (ch != ' ' && ch != '\t')
360 return ((char *)EOF);
374 if (ch == '"' || ch == '\'') {
378 while ((ch = getc(fp)) != EOF) {
379 if (ch == quote && !escaped_nl)
381 if (ch == '\n' && !escaped_nl) {
383 printf("config: missing quote reading `%s'\n",
387 if (ch == '\\' && !escaped_nl) {
391 if (ch != quote && escaped_nl)
398 while ((ch = getc(fp)) != EOF) {
404 (void) ungetc(ch, fp);
408 return ((char *)EOF);
413 * prepend the path to a filename
416 path(const char *file)
421 asprintf(&cp, "%s/%s", destdir, file);
423 cp = strdup(destdir);
428 * Generate configuration file based on actual settings. With this mode, user
429 * will be able to obtain and build conifguration file with one command.
432 configfile_dynamic(struct sbuf *sb)
434 struct cputype *cput;
440 asprintf(&lend, "\\n\\\n");
441 assert(lend != NULL);
442 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
443 sbuf_printf(sb, "ident\t%s%s", ident, lend);
444 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
445 SLIST_FOREACH(cput, &cputype, cpu_next)
446 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
447 SLIST_FOREACH(ol, &mkopt, op_next)
448 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
450 SLIST_FOREACH(ol, &opt, op_next) {
451 if (strncmp(ol->op_name, "DEV_", 4) == 0)
453 sbuf_printf(sb, "options\t%s", ol->op_name);
454 if (ol->op_value != NULL) {
456 for (i = 0; i < strlen(ol->op_value); i++) {
457 if (ol->op_value[i] == '"')
458 sbuf_printf(sb, "\\%c",
461 sbuf_printf(sb, "%c",
464 sbuf_printf(sb, "%s", lend);
466 sbuf_printf(sb, "%s", lend);
470 * Mark this file as containing everything we need.
472 STAILQ_FOREACH(d, &dtab, d_next)
473 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
478 * Generate file from the configuration files.
481 configfile_filebased(struct sbuf *sb)
488 * Try to read all configuration files. Since those will be present as
489 * C string in the macro, we have to slash their ends then the line
492 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
493 cff = fopen(cf->cfg_path, "r");
495 warn("Couldn't open file %s", cf->cfg_path);
498 while ((i = getc(cff)) != EOF) {
500 sbuf_printf(sb, "\\n\\\n");
501 else if (i == '"' || i == '\'')
502 sbuf_printf(sb, "\\%c", i);
517 /* Add main configuration file to the list of files to be included */
519 p = path("config.c.new");
523 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
527 /* Is needed, can be used for backward compatibility. */
528 configfile_filebased(sb);
530 configfile_dynamic(sb);
534 * We print first part of the template, replace our tag with
535 * configuration files content and later continue writing our
538 p = strstr(kernconfstr, KERNCONFTAG);
540 errx(EXIT_FAILURE, "Something went terribly wrong!");
542 fprintf(fo, "%s", kernconfstr);
543 fprintf(fo, "%s", sbuf_data(sb));
544 p += strlen(KERNCONFTAG);
545 fprintf(fo, "%s", p);
548 moveifchanged(path("config.c.new"), path("config.c"));
554 * compare two files; rename if changed.
557 moveifchanged(const char *from_name, const char *to_name)
562 struct stat from_sb, to_sb;
567 if ((from_fd = open(from_name, O_RDONLY)) < 0)
568 err(EX_OSERR, "moveifchanged open(%s)", from_name);
570 if ((to_fd = open(to_name, O_RDONLY)) < 0)
573 if (!changed && fstat(from_fd, &from_sb) < 0)
574 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
576 if (!changed && fstat(to_fd, &to_sb) < 0)
577 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
579 if (!changed && from_sb.st_size != to_sb.st_size)
582 tsize = (size_t)from_sb.st_size;
585 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
587 err(EX_OSERR, "mmap %s", from_name);
588 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
590 err(EX_OSERR, "mmap %s", to_name);
592 changed = memcmp(p, q, tsize);
597 if (rename(from_name, to_name) < 0)
598 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
600 if (unlink(from_name) < 0)
601 err(EX_OSERR, "unlink(%s)", from_name);
606 cleanheaders(char *p)
610 struct file_list *fl;
615 remember("setdefs.h");
616 STAILQ_FOREACH(fl, &ftab, f_next)
620 * Scan the build directory and clean out stuff that looks like
621 * it might have been a leftover NFOO header, etc.
623 if ((dirp = opendir(p)) == NULL)
624 err(EX_OSERR, "opendir %s", p);
625 while ((dp = readdir(dirp)) != NULL) {
626 len = strlen(dp->d_name);
627 /* Skip non-headers */
628 if (len < 2 || dp->d_name[len - 2] != '.' ||
629 dp->d_name[len - 1] != 'h')
631 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
632 if (strchr(dp->d_name, '_') &&
633 strncmp(dp->d_name, "opt_", 4) != 0)
635 /* Check if it is a target file */
636 for (hl = htab; hl != NULL; hl = hl->h_next) {
637 if (eq(dp->d_name, hl->h_name)) {
643 printf("Removing stale header: %s\n", dp->d_name);
644 if (unlink(path(dp->d_name)) == -1)
645 warn("unlink %s", dp->d_name);
647 (void)closedir(dirp);
651 remember(const char *file)
656 if ((s = strrchr(file, '/')) != NULL)
661 if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
665 for (hl = htab; hl != NULL; hl = hl->h_next) {
666 if (eq(s, hl->h_name)) {
671 hl = calloc(1, sizeof(*hl));
673 err(EXIT_FAILURE, "calloc");
680 * This one is quick hack. Will be probably moved to elf(3) interface.
681 * It takes kernel configuration file name, passes it as an argument to
682 * elfdump -a, which output is parsed by some UNIX tools...
685 kernconfdump(const char *file)
690 unsigned int i, off, size, t1, t2, align;
693 r = open(file, O_RDONLY);
695 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
696 error = fstat(r, &st);
698 err(EXIT_FAILURE, "fstat() failed");
699 if (S_ISDIR(st.st_mode))
700 errx(EXIT_FAILURE, "'%s' is a directory", file);
703 err(EXIT_FAILURE, "fdopen() failed");
707 err(EXIT_FAILURE, "Couldn't allocate memory");
708 /* ELF note section header. */
709 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
710 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
712 errx(EXIT_FAILURE, "asprintf() failed");
713 pp = popen(cmd, "r");
715 errx(EXIT_FAILURE, "popen() failed");
717 (void)fread(o, osz, 1, pp);
719 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
722 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
723 "file. Either unsupported, or not compiled with "
724 "INCLUDE_CONFIG_FILE", file);
725 r = fseek(fp, off, SEEK_CUR);
727 err(EXIT_FAILURE, "fseek() failed");
728 for (i = 0; i < size; i++) {
733 assert(i == size - 1 &&
734 ("\\0 found in the middle of a file"));
743 badversion(int versreq)
745 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
746 fprintf(stderr, "config version = %d, ", CONFIGVERS);
747 fprintf(stderr, "version required = %d\n\n", versreq);
748 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
749 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
750 fprintf(stderr, "before trying this again.\n\n");
751 fprintf(stderr, "If running the new config fails check your config\n");
752 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
753 fprintf(stderr, "changes in config syntax, or option/device naming\n");
754 fprintf(stderr, "conventions\n\n");
765 ifp = open_makefile_template();
766 while (fgets(line, BUFSIZ, ifp) != 0) {
769 if (strncmp(line, "%VERSREQ=", 9) != 0)
771 versreq = atoi(line + 9);
772 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
773 versreq <= CONFIGVERS)