2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
42 static const char rcsid[] =
46 #include <sys/types.h>
51 #include <sys/param.h>
63 #include "configvers.h"
73 #define CDIR "../compile/"
76 char destdir[MAXPATHLEN];
77 char srcdir[MAXPATHLEN];
85 * Preserve old behaviour in INCLUDE_CONFIG_FILE handling (files are included
90 static void configfile(void);
91 static void get_srcdir(void);
92 static void usage(void);
93 static void cleanheaders(char *);
94 static void kernconfdump(const char *);
95 static void checkversion(void);
96 extern int yyparse(void);
100 struct hdr_list *h_next;
104 * Config builds a set of files for building a UNIX
105 * system given a description of the desired system.
108 main(int argc, char **argv)
115 struct includepath* ipath;
120 SLIST_INIT(&includepath);
121 while ((ch = getopt(argc, argv, "CI:d:gmpsVx:")) != -1)
127 ipath = (struct includepath *) \
128 calloc(1, sizeof (struct includepath));
130 err(EXIT_FAILURE, "calloc");
131 ipath->path = optarg;
132 SLIST_INSERT_HEAD(&includepath, ipath, path_next);
138 if (*destdir == '\0')
139 strlcpy(destdir, optarg, sizeof(destdir));
141 errx(EXIT_FAILURE, "directory already set");
151 strlcpy(srcdir, optarg, sizeof(srcdir));
153 errx(EXIT_FAILURE, "src directory already set");
156 printf("%d\n", CONFIGVERS);
168 if (kernfile != NULL) {
169 kernconfdump(kernfile);
177 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
178 err(2, "%s", PREFIX);
179 if (freopen("DEFAULTS", "r", stdin) != NULL) {
183 if (freopen(PREFIX, "r", stdin) == NULL)
184 err(2, "%s", PREFIX);
187 if (*destdir != '\0') {
188 len = strlen(destdir);
189 while (len > 1 && destdir[len - 1] == '/')
190 destdir[--len] = '\0';
194 strlcpy(destdir, CDIR, sizeof(destdir));
195 strlcat(destdir, PREFIX, sizeof(destdir));
198 SLIST_INIT(&cputype);
202 STAILQ_INIT(&cfgfiles);
211 * Ensure that required elements (machine, cpu, ident) are present.
213 if (machinename == NULL) {
214 printf("Specify machine type, e.g. ``machine i386''\n");
218 printf("no ident line specified\n");
221 if (SLIST_EMPTY(&cputype)) {
222 printf("cpu type must be specified\n");
228 printf("%s\t%s\n",machinename,machinearch);
232 /* Make compile directory */
233 p = path((char *)NULL);
237 } else if (!S_ISDIR(buf.st_mode))
238 errx(EXIT_FAILURE, "%s isn't a directory", p);
240 configfile(); /* put config file into kernel*/
241 options(); /* make options .h files */
242 makefile(); /* build Makefile */
243 makeenv(); /* build env.c */
244 makehints(); /* build hints.c */
245 headers(); /* make a lot of .h files */
247 printf("Kernel build directory is %s\n", p);
248 printf("Don't forget to do ``make cleandepend && make depend''\n");
254 * determine the root of the kernel source tree
255 * and save that in srcdir.
264 if (realpath("../..", srcdir) == NULL)
265 err(EXIT_FAILURE, "Unable to find root of source tree");
266 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
267 (pwd = strdup(pwd)) != NULL) {
268 /* Remove the last two path components. */
269 for (i = 0; i < 2; i++) {
270 if ((p = strrchr(pwd, '/')) == NULL) {
276 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
277 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
278 strlcpy(srcdir, pwd, MAXPATHLEN);
288 "usage: config [-CgmpV] [-d destdir] [-s srcdir] sysname\n");
289 fprintf(stderr, " config -x kernel\n");
295 * returns EOF on end of file
296 * NULL on end of line
297 * pointer to the word otherwise
302 static char line[80];
308 while ((ch = getc(fp)) != EOF)
309 if (ch != ' ' && ch != '\t')
312 return ((char *)EOF);
327 /* Negation operator is a word by itself. */
332 while ((ch = getc(fp)) != EOF) {
339 return ((char *)EOF);
340 (void) ungetc(ch, fp);
346 * like get_word but will accept something in double or single quotes
347 * (to allow embedded spaces).
350 get_quoted_word(FILE *fp)
352 static char line[256];
358 while ((ch = getc(fp)) != EOF)
359 if (ch != ' ' && ch != '\t')
362 return ((char *)EOF);
376 if (ch == '"' || ch == '\'') {
380 while ((ch = getc(fp)) != EOF) {
381 if (ch == quote && !escaped_nl)
383 if (ch == '\n' && !escaped_nl) {
385 printf("config: missing quote reading `%s'\n",
389 if (ch == '\\' && !escaped_nl) {
393 if (ch != quote && escaped_nl)
400 while ((ch = getc(fp)) != EOF) {
406 (void) ungetc(ch, fp);
410 return ((char *)EOF);
415 * prepend the path to a filename
418 path(const char *file)
423 asprintf(&cp, "%s/%s", destdir, file);
425 cp = strdup(destdir);
430 * Generate configuration file based on actual settings. With this mode, user
431 * will be able to obtain and build conifguration file with one command.
434 configfile_dynamic(struct sbuf *sb)
436 struct cputype *cput;
442 asprintf(&lend, "\\n\\\n");
443 assert(lend != NULL);
444 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
445 sbuf_printf(sb, "ident\t%s%s", ident, lend);
446 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
447 SLIST_FOREACH(cput, &cputype, cpu_next)
448 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
449 SLIST_FOREACH(ol, &mkopt, op_next)
450 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
452 SLIST_FOREACH(ol, &opt, op_next) {
453 if (strncmp(ol->op_name, "DEV_", 4) == 0)
455 sbuf_printf(sb, "options\t%s", ol->op_name);
456 if (ol->op_value != NULL) {
458 for (i = 0; i < strlen(ol->op_value); i++) {
459 if (ol->op_value[i] == '"')
460 sbuf_printf(sb, "\\%c",
463 sbuf_printf(sb, "%c",
466 sbuf_printf(sb, "%s", lend);
468 sbuf_printf(sb, "%s", lend);
472 * Mark this file as containing everything we need.
474 STAILQ_FOREACH(d, &dtab, d_next)
475 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
480 * Generate file from the configuration files.
483 configfile_filebased(struct sbuf *sb)
490 * Try to read all configuration files. Since those will be present as
491 * C string in the macro, we have to slash their ends then the line
494 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
495 cff = fopen(cf->cfg_path, "r");
497 warn("Couldn't open file %s", cf->cfg_path);
500 while ((i = getc(cff)) != EOF) {
502 sbuf_printf(sb, "\\n\\\n");
503 else if (i == '"' || i == '\'')
504 sbuf_printf(sb, "\\%c", i);
519 /* Add main configuration file to the list of files to be included */
521 p = path("config.c.new");
525 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
529 /* Is needed, can be used for backward compatibility. */
530 configfile_filebased(sb);
532 configfile_dynamic(sb);
536 * We print first part of the template, replace our tag with
537 * configuration files content and later continue writing our
540 p = strstr(kernconfstr, KERNCONFTAG);
542 errx(EXIT_FAILURE, "Something went terribly wrong!");
544 fprintf(fo, "%s", kernconfstr);
545 fprintf(fo, "%s", sbuf_data(sb));
546 p += strlen(KERNCONFTAG);
547 fprintf(fo, "%s", p);
550 moveifchanged(path("config.c.new"), path("config.c"));
556 * compare two files; rename if changed.
559 moveifchanged(const char *from_name, const char *to_name)
564 struct stat from_sb, to_sb;
569 if ((from_fd = open(from_name, O_RDONLY)) < 0)
570 err(EX_OSERR, "moveifchanged open(%s)", from_name);
572 if ((to_fd = open(to_name, O_RDONLY)) < 0)
575 if (!changed && fstat(from_fd, &from_sb) < 0)
576 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
578 if (!changed && fstat(to_fd, &to_sb) < 0)
579 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
581 if (!changed && from_sb.st_size != to_sb.st_size)
584 tsize = (size_t)from_sb.st_size;
587 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
589 err(EX_OSERR, "mmap %s", from_name);
590 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
592 err(EX_OSERR, "mmap %s", to_name);
594 changed = memcmp(p, q, tsize);
599 if (rename(from_name, to_name) < 0)
600 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
602 if (unlink(from_name) < 0)
603 err(EX_OSERR, "unlink(%s)", from_name);
608 cleanheaders(char *p)
612 struct file_list *fl;
617 remember("setdefs.h");
618 STAILQ_FOREACH(fl, &ftab, f_next)
622 * Scan the build directory and clean out stuff that looks like
623 * it might have been a leftover NFOO header, etc.
625 if ((dirp = opendir(p)) == NULL)
626 err(EX_OSERR, "opendir %s", p);
627 while ((dp = readdir(dirp)) != NULL) {
628 len = strlen(dp->d_name);
629 /* Skip non-headers */
630 if (len < 2 || dp->d_name[len - 2] != '.' ||
631 dp->d_name[len - 1] != 'h')
633 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
634 if (strchr(dp->d_name, '_') &&
635 strncmp(dp->d_name, "opt_", 4) != 0)
637 /* Check if it is a target file */
638 for (hl = htab; hl != NULL; hl = hl->h_next) {
639 if (eq(dp->d_name, hl->h_name)) {
645 printf("Removing stale header: %s\n", dp->d_name);
646 if (unlink(path(dp->d_name)) == -1)
647 warn("unlink %s", dp->d_name);
649 (void)closedir(dirp);
653 remember(const char *file)
658 if ((s = strrchr(file, '/')) != NULL)
663 if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
667 for (hl = htab; hl != NULL; hl = hl->h_next) {
668 if (eq(s, hl->h_name)) {
673 hl = calloc(1, sizeof(*hl));
675 err(EXIT_FAILURE, "calloc");
682 * This one is quick hack. Will be probably moved to elf(3) interface.
683 * It takes kernel configuration file name, passes it as an argument to
684 * elfdump -a, which output is parsed by some UNIX tools...
687 kernconfdump(const char *file)
692 unsigned int i, off, size, t1, t2, align;
695 r = open(file, O_RDONLY);
697 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
698 error = fstat(r, &st);
700 err(EXIT_FAILURE, "fstat() failed");
701 if (S_ISDIR(st.st_mode))
702 errx(EXIT_FAILURE, "'%s' is a directory", file);
705 err(EXIT_FAILURE, "fdopen() failed");
709 err(EXIT_FAILURE, "Couldn't allocate memory");
710 /* ELF note section header. */
711 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
712 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
714 errx(EXIT_FAILURE, "asprintf() failed");
715 pp = popen(cmd, "r");
717 errx(EXIT_FAILURE, "popen() failed");
719 (void)fread(o, osz, 1, pp);
721 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
724 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
725 "file. Either unsupported, or not compiled with "
726 "INCLUDE_CONFIG_FILE", file);
727 r = fseek(fp, off, SEEK_CUR);
729 err(EXIT_FAILURE, "fseek() failed");
730 for (i = 0; i < size; i++) {
735 assert(i == size - 1 &&
736 ("\\0 found in the middle of a file"));
745 badversion(int versreq)
747 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
748 fprintf(stderr, "config version = %d, ", CONFIGVERS);
749 fprintf(stderr, "version required = %d\n\n", versreq);
750 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
751 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
752 fprintf(stderr, "before trying this again.\n\n");
753 fprintf(stderr, "If running the new config fails check your config\n");
754 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
755 fprintf(stderr, "changes in config syntax, or option/device naming\n");
756 fprintf(stderr, "conventions\n\n");
767 ifp = open_makefile_template();
768 while (fgets(line, BUFSIZ, ifp) != 0) {
771 if (strncmp(line, "%VERSREQ=", 9) != 0)
773 versreq = atoi(line + 9);
774 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
775 versreq <= CONFIGVERS)