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
91 static void configfile(void);
92 static void get_srcdir(void);
93 static void usage(void);
94 static void cleanheaders(char *);
95 static void kernconfdump(const char *);
96 static void badversion(void);
97 static void checkversion(void);
98 extern int yyparse(void);
102 struct hdr_list *h_next;
106 * Config builds a set of files for building a UNIX
107 * system given a description of the desired system.
110 main(int argc, char **argv)
117 struct includepath* ipath;
122 SLIST_INIT(&includepath);
123 while ((ch = getopt(argc, argv, "CI:d:gmps:Vx:")) != -1)
129 ipath = (struct includepath *) \
130 calloc(1, sizeof (struct includepath));
132 err(EXIT_FAILURE, "calloc");
133 ipath->path = optarg;
134 SLIST_INSERT_HEAD(&includepath, ipath, path_next);
140 if (*destdir == '\0')
141 strlcpy(destdir, optarg, sizeof(destdir));
143 errx(EXIT_FAILURE, "directory already set");
153 strlcpy(srcdir, optarg, sizeof(srcdir));
155 errx(EXIT_FAILURE, "src directory already set");
158 printf("%d\n", CONFIGVERS);
170 if (kernfile != NULL) {
171 kernconfdump(kernfile);
179 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
180 err(2, "%s", PREFIX);
181 if (freopen("DEFAULTS", "r", stdin) != NULL) {
185 if (freopen(PREFIX, "r", stdin) == NULL)
186 err(2, "%s", PREFIX);
189 if (*destdir != '\0') {
190 len = strlen(destdir);
191 while (len > 1 && destdir[len - 1] == '/')
192 destdir[--len] = '\0';
196 strlcpy(destdir, CDIR, sizeof(destdir));
197 strlcat(destdir, PREFIX, sizeof(destdir));
200 SLIST_INIT(&cputype);
204 STAILQ_INIT(&cfgfiles);
209 STAILQ_INIT(&envvars);
214 * Ensure that required elements (machine, cpu, ident) are present.
216 if (machinename == NULL) {
217 printf("Specify machine type, e.g. ``machine i386''\n");
221 printf("no ident line specified\n");
224 if (SLIST_EMPTY(&cputype)) {
225 printf("cpu type must be specified\n");
231 printf("%s\t%s\n",machinename,machinearch);
235 /* Make compile directory */
236 p = path((char *)NULL);
240 } else if (!S_ISDIR(buf.st_mode))
241 errx(EXIT_FAILURE, "%s isn't a directory", p);
243 configfile(); /* put config file into kernel*/
244 options(); /* make options .h files */
245 makefile(); /* build Makefile */
246 makeenv(); /* build env.c */
247 makehints(); /* build hints.c */
248 headers(); /* make a lot of .h files */
250 printf("Kernel build directory is %s\n", p);
251 printf("Don't forget to do ``make cleandepend && make depend''\n");
257 * determine the root of the kernel source tree
258 * and save that in srcdir.
267 if (realpath("../..", srcdir) == NULL)
268 err(EXIT_FAILURE, "Unable to find root of source tree");
269 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
270 (pwd = strdup(pwd)) != NULL) {
271 /* Remove the last two path components. */
272 for (i = 0; i < 2; i++) {
273 if ((p = strrchr(pwd, '/')) == NULL) {
279 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
280 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
281 strlcpy(srcdir, pwd, MAXPATHLEN);
291 "usage: config [-CgmpV] [-d destdir] [-s srcdir] sysname\n");
292 fprintf(stderr, " config -x kernel\n");
298 * returns EOF on end of file
299 * NULL on end of line
300 * pointer to the word otherwise
305 static char line[80];
311 while ((ch = getc(fp)) != EOF)
312 if (ch != ' ' && ch != '\t')
315 return ((char *)EOF);
330 /* Negation operator is a word by itself. */
335 while ((ch = getc(fp)) != EOF) {
342 return ((char *)EOF);
343 (void) ungetc(ch, fp);
349 * like get_word but will accept something in double or single quotes
350 * (to allow embedded spaces).
353 get_quoted_word(FILE *fp)
355 static char line[256];
361 while ((ch = getc(fp)) != EOF)
362 if (ch != ' ' && ch != '\t')
365 return ((char *)EOF);
379 if (ch == '"' || ch == '\'') {
383 while ((ch = getc(fp)) != EOF) {
384 if (ch == quote && !escaped_nl)
386 if (ch == '\n' && !escaped_nl) {
388 printf("config: missing quote reading `%s'\n",
392 if (ch == '\\' && !escaped_nl) {
396 if (ch != quote && escaped_nl)
403 while ((ch = getc(fp)) != EOF) {
409 (void) ungetc(ch, fp);
413 return ((char *)EOF);
418 * prepend the path to a filename
421 path(const char *file)
426 asprintf(&cp, "%s/%s", destdir, file);
428 cp = strdup(destdir);
433 * Generate configuration file based on actual settings. With this mode, user
434 * will be able to obtain and build conifguration file with one command.
437 configfile_dynamic(struct sbuf *sb)
439 struct cputype *cput;
445 asprintf(&lend, "\\n\\\n");
446 assert(lend != NULL);
447 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
448 sbuf_printf(sb, "ident\t%s%s", ident, lend);
449 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
450 SLIST_FOREACH(cput, &cputype, cpu_next)
451 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
452 SLIST_FOREACH(ol, &mkopt, op_next)
453 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
455 SLIST_FOREACH(ol, &opt, op_next) {
456 if (strncmp(ol->op_name, "DEV_", 4) == 0)
458 sbuf_printf(sb, "options\t%s", ol->op_name);
459 if (ol->op_value != NULL) {
461 for (i = 0; i < strlen(ol->op_value); i++) {
462 if (ol->op_value[i] == '"')
463 sbuf_printf(sb, "\\%c",
466 sbuf_printf(sb, "%c",
469 sbuf_printf(sb, "%s", lend);
471 sbuf_printf(sb, "%s", lend);
475 * Mark this file as containing everything we need.
477 STAILQ_FOREACH(d, &dtab, d_next)
478 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
483 * Generate file from the configuration files.
486 configfile_filebased(struct sbuf *sb)
493 * Try to read all configuration files. Since those will be present as
494 * C string in the macro, we have to slash their ends then the line
497 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
498 cff = fopen(cf->cfg_path, "r");
500 warn("Couldn't open file %s", cf->cfg_path);
503 while ((i = getc(cff)) != EOF) {
505 sbuf_printf(sb, "\\n\\\n");
506 else if (i == '"' || i == '\'')
507 sbuf_printf(sb, "\\%c", i);
522 /* Add main configuration file to the list of files to be included */
524 p = path("config.c.new");
528 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
532 /* Is needed, can be used for backward compatibility. */
533 configfile_filebased(sb);
535 configfile_dynamic(sb);
539 * We print first part of the template, replace our tag with
540 * configuration files content and later continue writing our
543 p = strstr(kernconfstr, KERNCONFTAG);
545 errx(EXIT_FAILURE, "Something went terribly wrong!");
547 fprintf(fo, "%s", kernconfstr);
548 fprintf(fo, "%s", sbuf_data(sb));
549 p += strlen(KERNCONFTAG);
550 fprintf(fo, "%s", p);
553 moveifchanged(path("config.c.new"), path("config.c"));
559 * compare two files; rename if changed.
562 moveifchanged(const char *from_name, const char *to_name)
567 struct stat from_sb, to_sb;
572 if ((from_fd = open(from_name, O_RDONLY)) < 0)
573 err(EX_OSERR, "moveifchanged open(%s)", from_name);
575 if ((to_fd = open(to_name, O_RDONLY)) < 0)
578 if (!changed && fstat(from_fd, &from_sb) < 0)
579 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
581 if (!changed && fstat(to_fd, &to_sb) < 0)
582 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
584 if (!changed && from_sb.st_size != to_sb.st_size)
587 tsize = (size_t)from_sb.st_size;
590 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
592 err(EX_OSERR, "mmap %s", from_name);
593 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
595 err(EX_OSERR, "mmap %s", to_name);
597 changed = memcmp(p, q, tsize);
602 if (rename(from_name, to_name) < 0)
603 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
605 if (unlink(from_name) < 0)
606 err(EX_OSERR, "unlink(%s)", from_name);
611 cleanheaders(char *p)
615 struct file_list *fl;
620 remember("setdefs.h");
621 STAILQ_FOREACH(fl, &ftab, f_next)
625 * Scan the build directory and clean out stuff that looks like
626 * it might have been a leftover NFOO header, etc.
628 if ((dirp = opendir(p)) == NULL)
629 err(EX_OSERR, "opendir %s", p);
630 while ((dp = readdir(dirp)) != NULL) {
631 len = strlen(dp->d_name);
632 /* Skip non-headers */
633 if (len < 2 || dp->d_name[len - 2] != '.' ||
634 dp->d_name[len - 1] != 'h')
636 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
637 if (strchr(dp->d_name, '_') &&
638 strncmp(dp->d_name, "opt_", 4) != 0)
640 /* Check if it is a target file */
641 for (hl = htab; hl != NULL; hl = hl->h_next) {
642 if (eq(dp->d_name, hl->h_name)) {
648 printf("Removing stale header: %s\n", dp->d_name);
649 if (unlink(path(dp->d_name)) == -1)
650 warn("unlink %s", dp->d_name);
652 (void)closedir(dirp);
656 remember(const char *file)
661 if ((s = strrchr(file, '/')) != NULL)
666 if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
670 for (hl = htab; hl != NULL; hl = hl->h_next) {
671 if (eq(s, hl->h_name)) {
676 hl = calloc(1, sizeof(*hl));
678 err(EXIT_FAILURE, "calloc");
685 * This one is quick hack. Will be probably moved to elf(3) interface.
686 * It takes kernel configuration file name, passes it as an argument to
687 * elfdump -a, which output is parsed by some UNIX tools...
690 kernconfdump(const char *file)
695 unsigned int i, off, size, t1, t2, align;
698 r = open(file, O_RDONLY);
700 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
701 error = fstat(r, &st);
703 err(EXIT_FAILURE, "fstat() failed");
704 if (S_ISDIR(st.st_mode))
705 errx(EXIT_FAILURE, "'%s' is a directory", file);
708 err(EXIT_FAILURE, "fdopen() failed");
712 err(EXIT_FAILURE, "Couldn't allocate memory");
713 /* ELF note section header. */
714 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
715 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
717 errx(EXIT_FAILURE, "asprintf() failed");
718 pp = popen(cmd, "r");
720 errx(EXIT_FAILURE, "popen() failed");
722 (void)fread(o, osz, 1, pp);
724 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
727 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
728 "file. Either unsupported, or not compiled with "
729 "INCLUDE_CONFIG_FILE", file);
730 r = fseek(fp, off, SEEK_CUR);
732 err(EXIT_FAILURE, "fseek() failed");
733 for (i = 0; i < size; i++) {
738 assert(i == size - 1 &&
739 ("\\0 found in the middle of a file"));
750 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
751 fprintf(stderr, "config version = %d, ", CONFIGVERS);
752 fprintf(stderr, "version required = %d\n\n", versreq);
753 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
754 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
755 fprintf(stderr, "before trying this again.\n\n");
756 fprintf(stderr, "If running the new config fails check your config\n");
757 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
758 fprintf(stderr, "changes in config syntax, or option/device naming\n");
759 fprintf(stderr, "conventions\n\n");
769 ifp = open_makefile_template();
770 while (fgets(line, BUFSIZ, ifp) != 0) {
773 if (strncmp(line, "%VERSREQ=", 9) != 0)
775 versreq = atoi(line + 9);
776 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
777 versreq <= CONFIGVERS)