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;
130 bool cust_dest = false;
134 SLIST_INIT(&includepath);
135 SLIST_INIT(&cputype);
139 STAILQ_INIT(&cfgfiles);
144 STAILQ_INIT(&envvars);
145 while ((ch = getopt(argc, argv, "Cd:gI:mps:Vx:")) != -1)
151 if (*destdir == '\0')
152 strlcpy(destdir, optarg, sizeof(destdir));
154 errx(EXIT_FAILURE, "directory already set");
161 ipath = (struct includepath *) \
162 calloc(1, sizeof (struct includepath));
164 err(EXIT_FAILURE, "calloc");
165 ipath->path = optarg;
166 SLIST_INSERT_HEAD(&includepath, ipath, path_next);
176 strlcpy(srcdir, optarg, sizeof(srcdir));
178 errx(EXIT_FAILURE, "src directory already set");
181 printf("%d\n", CONFIGVERS);
193 if (kernfile != NULL) {
194 kernconfdump(kernfile);
202 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
203 err(2, "%s", PREFIX);
204 if (freopen("DEFAULTS", "r", stdin) != NULL) {
208 if (freopen(PREFIX, "r", stdin) == NULL)
209 err(2, "%s", PREFIX);
212 if (*destdir != '\0') {
213 len = strlen(destdir);
214 while (len > 1 && destdir[len - 1] == '/')
215 destdir[--len] = '\0';
219 strlcpy(destdir, CDIR, sizeof(destdir));
220 strlcat(destdir, PREFIX, sizeof(destdir));
227 * Ensure that required elements (machine, cpu, ident) are present.
229 if (machinename == NULL) {
230 printf("Specify machine type, e.g. ``machine i386''\n");
234 printf("no ident line specified\n");
237 if (SLIST_EMPTY(&cputype)) {
238 printf("cpu type must be specified\n");
244 printf("%s\t%s\n",machinename,machinearch);
249 * Make CDIR directory, if doing a default destination. Some version
250 * control systems delete empty directories and this seemlessly copes.
252 if (!cust_dest && stat(CDIR, &buf))
253 if (mkdir(CDIR, 0777))
255 /* Create the compile directory */
256 p = path((char *)NULL);
260 } else if (!S_ISDIR(buf.st_mode))
261 errx(EXIT_FAILURE, "%s isn't a directory", p);
263 configfile(); /* put config file into kernel*/
264 options(); /* make options .h files */
265 makefile(); /* build Makefile */
266 makeenv(); /* build env.c */
267 makehints(); /* build hints.c */
268 headers(); /* make a lot of .h files */
270 printf("Kernel build directory is %s\n", p);
271 printf("Don't forget to do ``make cleandepend && make depend''\n");
277 * determine the root of the kernel source tree
278 * and save that in srcdir.
287 if (realpath("../..", srcdir) == NULL)
288 err(EXIT_FAILURE, "Unable to find root of source tree");
289 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
290 (pwd = strdup(pwd)) != NULL) {
291 /* Remove the last two path components. */
292 for (i = 0; i < 2; i++) {
293 if ((p = strrchr(pwd, '/')) == NULL) {
299 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
300 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
301 strlcpy(srcdir, pwd, MAXPATHLEN);
311 "usage: config [-CgmpV] [-d destdir] [-s srcdir] sysname\n");
312 fprintf(stderr, " config -x kernel\n");
318 * returns EOF on end of file
319 * NULL on end of line
320 * pointer to the word otherwise
325 static char line[80];
331 while ((ch = getc(fp)) != EOF)
332 if (ch != ' ' && ch != '\t')
335 return ((char *)EOF);
350 /* Negation operator is a word by itself. */
355 while ((ch = getc(fp)) != EOF) {
362 return ((char *)EOF);
363 (void) ungetc(ch, fp);
369 * like get_word but will accept something in double or single quotes
370 * (to allow embedded spaces).
373 get_quoted_word(FILE *fp)
375 static char line[256];
381 while ((ch = getc(fp)) != EOF)
382 if (ch != ' ' && ch != '\t')
385 return ((char *)EOF);
399 if (ch == '"' || ch == '\'') {
403 while ((ch = getc(fp)) != EOF) {
404 if (ch == quote && !escaped_nl)
406 if (ch == '\n' && !escaped_nl) {
408 printf("config: missing quote reading `%s'\n",
412 if (ch == '\\' && !escaped_nl) {
416 if (ch != quote && escaped_nl)
423 while ((ch = getc(fp)) != EOF) {
429 (void) ungetc(ch, fp);
433 return ((char *)EOF);
438 * prepend the path to a filename
441 path(const char *file)
446 asprintf(&cp, "%s/%s", destdir, file);
448 cp = strdup(destdir);
453 * Generate configuration file based on actual settings. With this mode, user
454 * will be able to obtain and build conifguration file with one command.
457 configfile_dynamic(struct sbuf *sb)
459 struct cputype *cput;
465 asprintf(&lend, "\\n\\\n");
466 assert(lend != NULL);
467 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
468 sbuf_printf(sb, "ident\t%s%s", ident, lend);
469 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
470 SLIST_FOREACH(cput, &cputype, cpu_next)
471 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
472 SLIST_FOREACH(ol, &mkopt, op_next)
473 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
475 SLIST_FOREACH(ol, &opt, op_next) {
476 if (strncmp(ol->op_name, "DEV_", 4) == 0)
478 sbuf_printf(sb, "options\t%s", ol->op_name);
479 if (ol->op_value != NULL) {
481 for (i = 0; i < strlen(ol->op_value); i++) {
482 if (ol->op_value[i] == '"')
483 sbuf_printf(sb, "\\%c",
486 sbuf_printf(sb, "%c",
489 sbuf_printf(sb, "%s", lend);
491 sbuf_printf(sb, "%s", lend);
495 * Mark this file as containing everything we need.
497 STAILQ_FOREACH(d, &dtab, d_next)
498 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
503 * Generate file from the configuration files.
506 configfile_filebased(struct sbuf *sb)
513 * Try to read all configuration files. Since those will be present as
514 * C string in the macro, we have to slash their ends then the line
517 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
518 cff = fopen(cf->cfg_path, "r");
520 warn("Couldn't open file %s", cf->cfg_path);
523 while ((i = getc(cff)) != EOF) {
525 sbuf_printf(sb, "\\n\\\n");
526 else if (i == '"' || i == '\'')
527 sbuf_printf(sb, "\\%c", i);
542 /* Add main configuration file to the list of files to be included */
544 p = path("config.c.new");
548 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
552 /* Is needed, can be used for backward compatibility. */
553 configfile_filebased(sb);
555 configfile_dynamic(sb);
559 * We print first part of the template, replace our tag with
560 * configuration files content and later continue writing our
563 p = strstr(kernconfstr, KERNCONFTAG);
565 errx(EXIT_FAILURE, "Something went terribly wrong!");
567 fprintf(fo, "%s", kernconfstr);
568 fprintf(fo, "%s", sbuf_data(sb));
569 p += strlen(KERNCONFTAG);
570 fprintf(fo, "%s", p);
573 moveifchanged(path("config.c.new"), path("config.c"));
579 * compare two files; rename if changed.
582 moveifchanged(const char *from_name, const char *to_name)
587 struct stat from_sb, to_sb;
592 if ((from_fd = open(from_name, O_RDONLY)) < 0)
593 err(EX_OSERR, "moveifchanged open(%s)", from_name);
595 if ((to_fd = open(to_name, O_RDONLY)) < 0)
598 if (!changed && fstat(from_fd, &from_sb) < 0)
599 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
601 if (!changed && fstat(to_fd, &to_sb) < 0)
602 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
604 if (!changed && from_sb.st_size != to_sb.st_size)
607 tsize = (size_t)from_sb.st_size;
610 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
612 err(EX_OSERR, "mmap %s", from_name);
613 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
615 err(EX_OSERR, "mmap %s", to_name);
617 changed = memcmp(p, q, tsize);
622 if (rename(from_name, to_name) < 0)
623 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
625 if (unlink(from_name) < 0)
626 err(EX_OSERR, "unlink(%s)", from_name);
631 cleanheaders(char *p)
635 struct file_list *fl;
640 remember("setdefs.h");
641 STAILQ_FOREACH(fl, &ftab, f_next)
645 * Scan the build directory and clean out stuff that looks like
646 * it might have been a leftover NFOO header, etc.
648 if ((dirp = opendir(p)) == NULL)
649 err(EX_OSERR, "opendir %s", p);
650 while ((dp = readdir(dirp)) != NULL) {
651 len = strlen(dp->d_name);
652 /* Skip non-headers */
653 if (len < 2 || dp->d_name[len - 2] != '.' ||
654 dp->d_name[len - 1] != 'h')
656 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
657 if (strchr(dp->d_name, '_') &&
658 strncmp(dp->d_name, "opt_", 4) != 0)
660 /* Check if it is a target file */
661 for (hl = htab; hl != NULL; hl = hl->h_next) {
662 if (eq(dp->d_name, hl->h_name)) {
668 printf("Removing stale header: %s\n", dp->d_name);
669 if (unlink(path(dp->d_name)) == -1)
670 warn("unlink %s", dp->d_name);
672 (void)closedir(dirp);
676 remember(const char *file)
681 if ((s = strrchr(file, '/')) != NULL)
686 if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
690 for (hl = htab; hl != NULL; hl = hl->h_next) {
691 if (eq(s, hl->h_name)) {
696 hl = calloc(1, sizeof(*hl));
698 err(EXIT_FAILURE, "calloc");
705 * This one is quick hack. Will be probably moved to elf(3) interface.
706 * It takes kernel configuration file name, passes it as an argument to
707 * elfdump -a, which output is parsed by some UNIX tools...
710 kernconfdump(const char *file)
715 unsigned int i, off, size, t1, t2, align;
718 r = open(file, O_RDONLY);
720 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
721 error = fstat(r, &st);
723 err(EXIT_FAILURE, "fstat() failed");
724 if (S_ISDIR(st.st_mode))
725 errx(EXIT_FAILURE, "'%s' is a directory", file);
728 err(EXIT_FAILURE, "fdopen() failed");
732 err(EXIT_FAILURE, "Couldn't allocate memory");
733 /* ELF note section header. */
734 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
735 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
737 errx(EXIT_FAILURE, "asprintf() failed");
738 pp = popen(cmd, "r");
740 errx(EXIT_FAILURE, "popen() failed");
742 (void)fread(o, osz, 1, pp);
744 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
747 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
748 "file. Either unsupported, or not compiled with "
749 "INCLUDE_CONFIG_FILE", file);
750 r = fseek(fp, off, SEEK_CUR);
752 err(EXIT_FAILURE, "fseek() failed");
753 for (i = 0; i < size; i++) {
758 assert(i == size - 1 &&
759 ("\\0 found in the middle of a file"));
770 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
771 fprintf(stderr, "config version = %d, ", CONFIGVERS);
772 fprintf(stderr, "version required = %d\n\n", versreq);
773 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
774 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
775 fprintf(stderr, "before trying this again.\n\n");
776 fprintf(stderr, "If running the new config fails check your config\n");
777 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
778 fprintf(stderr, "changes in config syntax, or option/device naming\n");
779 fprintf(stderr, "conventions\n\n");
789 ifp = open_makefile_template();
790 while (fgets(line, BUFSIZ, ifp) != 0) {
793 if (strncmp(line, "%VERSREQ=", 9) != 0)
795 versreq = atoi(line + 9);
796 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
797 versreq <= CONFIGVERS)