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/"
78 struct cfgfile_head cfgfiles;
79 struct cputype_head cputype;
80 struct opt_head opt, mkopt, rmopts;
81 struct opt_list_head otab;
82 struct envvar_head envvars;
83 struct hint_head hints;
84 struct includepath_head includepath;
87 char destdir[MAXPATHLEN];
88 char srcdir[MAXPATHLEN];
96 * Preserve old behaviour in INCLUDE_CONFIG_FILE handling (files are included
102 static void configfile(void);
103 static void get_srcdir(void);
104 static void usage(void);
105 static void cleanheaders(char *);
106 static void kernconfdump(const char *);
107 static void badversion(void);
108 static void checkversion(void);
109 extern int yyparse(void);
113 struct hdr_list *h_next;
117 * Config builds a set of files for building a UNIX
118 * system given a description of the desired system.
121 main(int argc, char **argv)
128 struct includepath* ipath;
133 SLIST_INIT(&includepath);
134 while ((ch = getopt(argc, argv, "CI:d:gmps:Vx:")) != -1)
140 ipath = (struct includepath *) \
141 calloc(1, sizeof (struct includepath));
143 err(EXIT_FAILURE, "calloc");
144 ipath->path = optarg;
145 SLIST_INSERT_HEAD(&includepath, ipath, path_next);
151 if (*destdir == '\0')
152 strlcpy(destdir, optarg, sizeof(destdir));
154 errx(EXIT_FAILURE, "directory already set");
164 strlcpy(srcdir, optarg, sizeof(srcdir));
166 errx(EXIT_FAILURE, "src directory already set");
169 printf("%d\n", CONFIGVERS);
181 if (kernfile != NULL) {
182 kernconfdump(kernfile);
190 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
191 err(2, "%s", PREFIX);
192 if (freopen("DEFAULTS", "r", stdin) != NULL) {
196 if (freopen(PREFIX, "r", stdin) == NULL)
197 err(2, "%s", PREFIX);
200 if (*destdir != '\0') {
201 len = strlen(destdir);
202 while (len > 1 && destdir[len - 1] == '/')
203 destdir[--len] = '\0';
207 strlcpy(destdir, CDIR, sizeof(destdir));
208 strlcat(destdir, PREFIX, sizeof(destdir));
211 SLIST_INIT(&cputype);
215 STAILQ_INIT(&cfgfiles);
220 STAILQ_INIT(&envvars);
225 * Ensure that required elements (machine, cpu, ident) are present.
227 if (machinename == NULL) {
228 printf("Specify machine type, e.g. ``machine i386''\n");
232 printf("no ident line specified\n");
235 if (SLIST_EMPTY(&cputype)) {
236 printf("cpu type must be specified\n");
242 printf("%s\t%s\n",machinename,machinearch);
246 /* Make compile directory */
247 p = path((char *)NULL);
251 } else if (!S_ISDIR(buf.st_mode))
252 errx(EXIT_FAILURE, "%s isn't a directory", p);
254 configfile(); /* put config file into kernel*/
255 options(); /* make options .h files */
256 makefile(); /* build Makefile */
257 makeenv(); /* build env.c */
258 makehints(); /* build hints.c */
259 headers(); /* make a lot of .h files */
261 printf("Kernel build directory is %s\n", p);
262 printf("Don't forget to do ``make cleandepend && make depend''\n");
268 * determine the root of the kernel source tree
269 * and save that in srcdir.
278 if (realpath("../..", srcdir) == NULL)
279 err(EXIT_FAILURE, "Unable to find root of source tree");
280 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
281 (pwd = strdup(pwd)) != NULL) {
282 /* Remove the last two path components. */
283 for (i = 0; i < 2; i++) {
284 if ((p = strrchr(pwd, '/')) == NULL) {
290 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
291 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
292 strlcpy(srcdir, pwd, MAXPATHLEN);
302 "usage: config [-CgmpV] [-d destdir] [-s srcdir] sysname\n");
303 fprintf(stderr, " config -x kernel\n");
309 * returns EOF on end of file
310 * NULL on end of line
311 * pointer to the word otherwise
316 static char line[80];
322 while ((ch = getc(fp)) != EOF)
323 if (ch != ' ' && ch != '\t')
326 return ((char *)EOF);
341 /* Negation operator is a word by itself. */
346 while ((ch = getc(fp)) != EOF) {
353 return ((char *)EOF);
354 (void) ungetc(ch, fp);
360 * like get_word but will accept something in double or single quotes
361 * (to allow embedded spaces).
364 get_quoted_word(FILE *fp)
366 static char line[256];
372 while ((ch = getc(fp)) != EOF)
373 if (ch != ' ' && ch != '\t')
376 return ((char *)EOF);
390 if (ch == '"' || ch == '\'') {
394 while ((ch = getc(fp)) != EOF) {
395 if (ch == quote && !escaped_nl)
397 if (ch == '\n' && !escaped_nl) {
399 printf("config: missing quote reading `%s'\n",
403 if (ch == '\\' && !escaped_nl) {
407 if (ch != quote && escaped_nl)
414 while ((ch = getc(fp)) != EOF) {
420 (void) ungetc(ch, fp);
424 return ((char *)EOF);
429 * prepend the path to a filename
432 path(const char *file)
437 asprintf(&cp, "%s/%s", destdir, file);
439 cp = strdup(destdir);
444 * Generate configuration file based on actual settings. With this mode, user
445 * will be able to obtain and build conifguration file with one command.
448 configfile_dynamic(struct sbuf *sb)
450 struct cputype *cput;
456 asprintf(&lend, "\\n\\\n");
457 assert(lend != NULL);
458 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
459 sbuf_printf(sb, "ident\t%s%s", ident, lend);
460 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
461 SLIST_FOREACH(cput, &cputype, cpu_next)
462 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
463 SLIST_FOREACH(ol, &mkopt, op_next)
464 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
466 SLIST_FOREACH(ol, &opt, op_next) {
467 if (strncmp(ol->op_name, "DEV_", 4) == 0)
469 sbuf_printf(sb, "options\t%s", ol->op_name);
470 if (ol->op_value != NULL) {
472 for (i = 0; i < strlen(ol->op_value); i++) {
473 if (ol->op_value[i] == '"')
474 sbuf_printf(sb, "\\%c",
477 sbuf_printf(sb, "%c",
480 sbuf_printf(sb, "%s", lend);
482 sbuf_printf(sb, "%s", lend);
486 * Mark this file as containing everything we need.
488 STAILQ_FOREACH(d, &dtab, d_next)
489 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
494 * Generate file from the configuration files.
497 configfile_filebased(struct sbuf *sb)
504 * Try to read all configuration files. Since those will be present as
505 * C string in the macro, we have to slash their ends then the line
508 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
509 cff = fopen(cf->cfg_path, "r");
511 warn("Couldn't open file %s", cf->cfg_path);
514 while ((i = getc(cff)) != EOF) {
516 sbuf_printf(sb, "\\n\\\n");
517 else if (i == '"' || i == '\'')
518 sbuf_printf(sb, "\\%c", i);
533 /* Add main configuration file to the list of files to be included */
535 p = path("config.c.new");
539 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
543 /* Is needed, can be used for backward compatibility. */
544 configfile_filebased(sb);
546 configfile_dynamic(sb);
550 * We print first part of the template, replace our tag with
551 * configuration files content and later continue writing our
554 p = strstr(kernconfstr, KERNCONFTAG);
556 errx(EXIT_FAILURE, "Something went terribly wrong!");
558 fprintf(fo, "%s", kernconfstr);
559 fprintf(fo, "%s", sbuf_data(sb));
560 p += strlen(KERNCONFTAG);
561 fprintf(fo, "%s", p);
564 moveifchanged(path("config.c.new"), path("config.c"));
570 * compare two files; rename if changed.
573 moveifchanged(const char *from_name, const char *to_name)
578 struct stat from_sb, to_sb;
583 if ((from_fd = open(from_name, O_RDONLY)) < 0)
584 err(EX_OSERR, "moveifchanged open(%s)", from_name);
586 if ((to_fd = open(to_name, O_RDONLY)) < 0)
589 if (!changed && fstat(from_fd, &from_sb) < 0)
590 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
592 if (!changed && fstat(to_fd, &to_sb) < 0)
593 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
595 if (!changed && from_sb.st_size != to_sb.st_size)
598 tsize = (size_t)from_sb.st_size;
601 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
603 err(EX_OSERR, "mmap %s", from_name);
604 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
606 err(EX_OSERR, "mmap %s", to_name);
608 changed = memcmp(p, q, tsize);
613 if (rename(from_name, to_name) < 0)
614 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
616 if (unlink(from_name) < 0)
617 err(EX_OSERR, "unlink(%s)", from_name);
622 cleanheaders(char *p)
626 struct file_list *fl;
631 remember("setdefs.h");
632 STAILQ_FOREACH(fl, &ftab, f_next)
636 * Scan the build directory and clean out stuff that looks like
637 * it might have been a leftover NFOO header, etc.
639 if ((dirp = opendir(p)) == NULL)
640 err(EX_OSERR, "opendir %s", p);
641 while ((dp = readdir(dirp)) != NULL) {
642 len = strlen(dp->d_name);
643 /* Skip non-headers */
644 if (len < 2 || dp->d_name[len - 2] != '.' ||
645 dp->d_name[len - 1] != 'h')
647 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
648 if (strchr(dp->d_name, '_') &&
649 strncmp(dp->d_name, "opt_", 4) != 0)
651 /* Check if it is a target file */
652 for (hl = htab; hl != NULL; hl = hl->h_next) {
653 if (eq(dp->d_name, hl->h_name)) {
659 printf("Removing stale header: %s\n", dp->d_name);
660 if (unlink(path(dp->d_name)) == -1)
661 warn("unlink %s", dp->d_name);
663 (void)closedir(dirp);
667 remember(const char *file)
672 if ((s = strrchr(file, '/')) != NULL)
677 if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
681 for (hl = htab; hl != NULL; hl = hl->h_next) {
682 if (eq(s, hl->h_name)) {
687 hl = calloc(1, sizeof(*hl));
689 err(EXIT_FAILURE, "calloc");
696 * This one is quick hack. Will be probably moved to elf(3) interface.
697 * It takes kernel configuration file name, passes it as an argument to
698 * elfdump -a, which output is parsed by some UNIX tools...
701 kernconfdump(const char *file)
706 unsigned int i, off, size, t1, t2, align;
709 r = open(file, O_RDONLY);
711 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
712 error = fstat(r, &st);
714 err(EXIT_FAILURE, "fstat() failed");
715 if (S_ISDIR(st.st_mode))
716 errx(EXIT_FAILURE, "'%s' is a directory", file);
719 err(EXIT_FAILURE, "fdopen() failed");
723 err(EXIT_FAILURE, "Couldn't allocate memory");
724 /* ELF note section header. */
725 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
726 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
728 errx(EXIT_FAILURE, "asprintf() failed");
729 pp = popen(cmd, "r");
731 errx(EXIT_FAILURE, "popen() failed");
733 (void)fread(o, osz, 1, pp);
735 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
738 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
739 "file. Either unsupported, or not compiled with "
740 "INCLUDE_CONFIG_FILE", file);
741 r = fseek(fp, off, SEEK_CUR);
743 err(EXIT_FAILURE, "fseek() failed");
744 for (i = 0; i < size; i++) {
749 assert(i == size - 1 &&
750 ("\\0 found in the middle of a file"));
761 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
762 fprintf(stderr, "config version = %d, ", CONFIGVERS);
763 fprintf(stderr, "version required = %d\n\n", versreq);
764 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
765 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
766 fprintf(stderr, "before trying this again.\n\n");
767 fprintf(stderr, "If running the new config fails check your config\n");
768 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
769 fprintf(stderr, "changes in config syntax, or option/device naming\n");
770 fprintf(stderr, "conventions\n\n");
780 ifp = open_makefile_template();
781 while (fgets(line, BUFSIZ, ifp) != 0) {
784 if (strncmp(line, "%VERSREQ=", 9) != 0)
786 versreq = atoi(line + 9);
787 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
788 versreq <= CONFIGVERS)