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 checkversion(void);
97 extern int yyparse(void);
101 struct hdr_list *h_next;
105 * Config builds a set of files for building a UNIX
106 * system given a description of the desired system.
109 main(int argc, char **argv)
116 struct includepath* ipath;
121 SLIST_INIT(&includepath);
122 while ((ch = getopt(argc, argv, "CI:d:gmps:Vx:")) != -1)
128 ipath = (struct includepath *) \
129 calloc(1, sizeof (struct includepath));
131 err(EXIT_FAILURE, "calloc");
132 ipath->path = optarg;
133 SLIST_INSERT_HEAD(&includepath, ipath, path_next);
139 if (*destdir == '\0')
140 strlcpy(destdir, optarg, sizeof(destdir));
142 errx(EXIT_FAILURE, "directory already set");
152 strlcpy(srcdir, optarg, sizeof(srcdir));
154 errx(EXIT_FAILURE, "src directory already set");
157 printf("%d\n", CONFIGVERS);
169 if (kernfile != NULL) {
170 kernconfdump(kernfile);
178 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
179 err(2, "%s", PREFIX);
180 if (freopen("DEFAULTS", "r", stdin) != NULL) {
184 if (freopen(PREFIX, "r", stdin) == NULL)
185 err(2, "%s", PREFIX);
188 if (*destdir != '\0') {
189 len = strlen(destdir);
190 while (len > 1 && destdir[len - 1] == '/')
191 destdir[--len] = '\0';
195 strlcpy(destdir, CDIR, sizeof(destdir));
196 strlcat(destdir, PREFIX, sizeof(destdir));
199 SLIST_INIT(&cputype);
203 STAILQ_INIT(&cfgfiles);
208 STAILQ_INIT(&envvars);
213 * Ensure that required elements (machine, cpu, ident) are present.
215 if (machinename == NULL) {
216 printf("Specify machine type, e.g. ``machine i386''\n");
220 printf("no ident line specified\n");
223 if (SLIST_EMPTY(&cputype)) {
224 printf("cpu type must be specified\n");
230 printf("%s\t%s\n",machinename,machinearch);
234 /* Make compile directory */
235 p = path((char *)NULL);
239 } else if (!S_ISDIR(buf.st_mode))
240 errx(EXIT_FAILURE, "%s isn't a directory", p);
242 configfile(); /* put config file into kernel*/
243 options(); /* make options .h files */
244 makefile(); /* build Makefile */
245 makeenv(); /* build env.c */
246 makehints(); /* build hints.c */
247 headers(); /* make a lot of .h files */
249 printf("Kernel build directory is %s\n", p);
250 printf("Don't forget to do ``make cleandepend && make depend''\n");
256 * determine the root of the kernel source tree
257 * and save that in srcdir.
266 if (realpath("../..", srcdir) == NULL)
267 err(EXIT_FAILURE, "Unable to find root of source tree");
268 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
269 (pwd = strdup(pwd)) != NULL) {
270 /* Remove the last two path components. */
271 for (i = 0; i < 2; i++) {
272 if ((p = strrchr(pwd, '/')) == NULL) {
278 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
279 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
280 strlcpy(srcdir, pwd, MAXPATHLEN);
290 "usage: config [-CgmpV] [-d destdir] [-s srcdir] sysname\n");
291 fprintf(stderr, " config -x kernel\n");
297 * returns EOF on end of file
298 * NULL on end of line
299 * pointer to the word otherwise
304 static char line[80];
310 while ((ch = getc(fp)) != EOF)
311 if (ch != ' ' && ch != '\t')
314 return ((char *)EOF);
329 /* Negation operator is a word by itself. */
334 while ((ch = getc(fp)) != EOF) {
341 return ((char *)EOF);
342 (void) ungetc(ch, fp);
348 * like get_word but will accept something in double or single quotes
349 * (to allow embedded spaces).
352 get_quoted_word(FILE *fp)
354 static char line[256];
360 while ((ch = getc(fp)) != EOF)
361 if (ch != ' ' && ch != '\t')
364 return ((char *)EOF);
378 if (ch == '"' || ch == '\'') {
382 while ((ch = getc(fp)) != EOF) {
383 if (ch == quote && !escaped_nl)
385 if (ch == '\n' && !escaped_nl) {
387 printf("config: missing quote reading `%s'\n",
391 if (ch == '\\' && !escaped_nl) {
395 if (ch != quote && escaped_nl)
402 while ((ch = getc(fp)) != EOF) {
408 (void) ungetc(ch, fp);
412 return ((char *)EOF);
417 * prepend the path to a filename
420 path(const char *file)
425 asprintf(&cp, "%s/%s", destdir, file);
427 cp = strdup(destdir);
432 * Generate configuration file based on actual settings. With this mode, user
433 * will be able to obtain and build conifguration file with one command.
436 configfile_dynamic(struct sbuf *sb)
438 struct cputype *cput;
444 asprintf(&lend, "\\n\\\n");
445 assert(lend != NULL);
446 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
447 sbuf_printf(sb, "ident\t%s%s", ident, lend);
448 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
449 SLIST_FOREACH(cput, &cputype, cpu_next)
450 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
451 SLIST_FOREACH(ol, &mkopt, op_next)
452 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
454 SLIST_FOREACH(ol, &opt, op_next) {
455 if (strncmp(ol->op_name, "DEV_", 4) == 0)
457 sbuf_printf(sb, "options\t%s", ol->op_name);
458 if (ol->op_value != NULL) {
460 for (i = 0; i < strlen(ol->op_value); i++) {
461 if (ol->op_value[i] == '"')
462 sbuf_printf(sb, "\\%c",
465 sbuf_printf(sb, "%c",
468 sbuf_printf(sb, "%s", lend);
470 sbuf_printf(sb, "%s", lend);
474 * Mark this file as containing everything we need.
476 STAILQ_FOREACH(d, &dtab, d_next)
477 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
482 * Generate file from the configuration files.
485 configfile_filebased(struct sbuf *sb)
492 * Try to read all configuration files. Since those will be present as
493 * C string in the macro, we have to slash their ends then the line
496 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
497 cff = fopen(cf->cfg_path, "r");
499 warn("Couldn't open file %s", cf->cfg_path);
502 while ((i = getc(cff)) != EOF) {
504 sbuf_printf(sb, "\\n\\\n");
505 else if (i == '"' || i == '\'')
506 sbuf_printf(sb, "\\%c", i);
521 /* Add main configuration file to the list of files to be included */
523 p = path("config.c.new");
527 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
531 /* Is needed, can be used for backward compatibility. */
532 configfile_filebased(sb);
534 configfile_dynamic(sb);
538 * We print first part of the template, replace our tag with
539 * configuration files content and later continue writing our
542 p = strstr(kernconfstr, KERNCONFTAG);
544 errx(EXIT_FAILURE, "Something went terribly wrong!");
546 fprintf(fo, "%s", kernconfstr);
547 fprintf(fo, "%s", sbuf_data(sb));
548 p += strlen(KERNCONFTAG);
549 fprintf(fo, "%s", p);
552 moveifchanged(path("config.c.new"), path("config.c"));
558 * compare two files; rename if changed.
561 moveifchanged(const char *from_name, const char *to_name)
566 struct stat from_sb, to_sb;
571 if ((from_fd = open(from_name, O_RDONLY)) < 0)
572 err(EX_OSERR, "moveifchanged open(%s)", from_name);
574 if ((to_fd = open(to_name, O_RDONLY)) < 0)
577 if (!changed && fstat(from_fd, &from_sb) < 0)
578 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
580 if (!changed && fstat(to_fd, &to_sb) < 0)
581 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
583 if (!changed && from_sb.st_size != to_sb.st_size)
586 tsize = (size_t)from_sb.st_size;
589 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
591 err(EX_OSERR, "mmap %s", from_name);
592 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
594 err(EX_OSERR, "mmap %s", to_name);
596 changed = memcmp(p, q, tsize);
601 if (rename(from_name, to_name) < 0)
602 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
604 if (unlink(from_name) < 0)
605 err(EX_OSERR, "unlink(%s)", from_name);
610 cleanheaders(char *p)
614 struct file_list *fl;
619 remember("setdefs.h");
620 STAILQ_FOREACH(fl, &ftab, f_next)
624 * Scan the build directory and clean out stuff that looks like
625 * it might have been a leftover NFOO header, etc.
627 if ((dirp = opendir(p)) == NULL)
628 err(EX_OSERR, "opendir %s", p);
629 while ((dp = readdir(dirp)) != NULL) {
630 len = strlen(dp->d_name);
631 /* Skip non-headers */
632 if (len < 2 || dp->d_name[len - 2] != '.' ||
633 dp->d_name[len - 1] != 'h')
635 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
636 if (strchr(dp->d_name, '_') &&
637 strncmp(dp->d_name, "opt_", 4) != 0)
639 /* Check if it is a target file */
640 for (hl = htab; hl != NULL; hl = hl->h_next) {
641 if (eq(dp->d_name, hl->h_name)) {
647 printf("Removing stale header: %s\n", dp->d_name);
648 if (unlink(path(dp->d_name)) == -1)
649 warn("unlink %s", dp->d_name);
651 (void)closedir(dirp);
655 remember(const char *file)
660 if ((s = strrchr(file, '/')) != NULL)
665 if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
669 for (hl = htab; hl != NULL; hl = hl->h_next) {
670 if (eq(s, hl->h_name)) {
675 hl = calloc(1, sizeof(*hl));
677 err(EXIT_FAILURE, "calloc");
684 * This one is quick hack. Will be probably moved to elf(3) interface.
685 * It takes kernel configuration file name, passes it as an argument to
686 * elfdump -a, which output is parsed by some UNIX tools...
689 kernconfdump(const char *file)
694 unsigned int i, off, size, t1, t2, align;
697 r = open(file, O_RDONLY);
699 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
700 error = fstat(r, &st);
702 err(EXIT_FAILURE, "fstat() failed");
703 if (S_ISDIR(st.st_mode))
704 errx(EXIT_FAILURE, "'%s' is a directory", file);
707 err(EXIT_FAILURE, "fdopen() failed");
711 err(EXIT_FAILURE, "Couldn't allocate memory");
712 /* ELF note section header. */
713 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
714 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
716 errx(EXIT_FAILURE, "asprintf() failed");
717 pp = popen(cmd, "r");
719 errx(EXIT_FAILURE, "popen() failed");
721 (void)fread(o, osz, 1, pp);
723 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
726 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
727 "file. Either unsupported, or not compiled with "
728 "INCLUDE_CONFIG_FILE", file);
729 r = fseek(fp, off, SEEK_CUR);
731 err(EXIT_FAILURE, "fseek() failed");
732 for (i = 0; i < size; i++) {
737 assert(i == size - 1 &&
738 ("\\0 found in the middle of a file"));
749 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
750 fprintf(stderr, "config version = %d, ", CONFIGVERS);
751 fprintf(stderr, "version required = %d\n\n", versreq);
752 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
753 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
754 fprintf(stderr, "before trying this again.\n\n");
755 fprintf(stderr, "If running the new config fails check your config\n");
756 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
757 fprintf(stderr, "changes in config syntax, or option/device naming\n");
758 fprintf(stderr, "conventions\n\n");
768 ifp = open_makefile_template();
769 while (fgets(line, BUFSIZ, ifp) != 0) {
772 if (strncmp(line, "%VERSREQ=", 9) != 0)
774 versreq = atoi(line + 9);
775 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
776 versreq <= CONFIGVERS)