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;
116 static struct sbuf *line_buf = NULL;
119 * Config builds a set of files for building a UNIX
120 * system given a description of the desired system.
123 main(int argc, char **argv)
130 struct includepath* ipath;
135 SLIST_INIT(&includepath);
136 while ((ch = getopt(argc, argv, "CI:d:gmps:Vx:")) != -1)
142 ipath = (struct includepath *) \
143 calloc(1, sizeof (struct includepath));
145 err(EXIT_FAILURE, "calloc");
146 ipath->path = optarg;
147 SLIST_INSERT_HEAD(&includepath, ipath, path_next);
153 if (*destdir == '\0')
154 strlcpy(destdir, optarg, sizeof(destdir));
156 errx(EXIT_FAILURE, "directory already set");
166 strlcpy(srcdir, optarg, sizeof(srcdir));
168 errx(EXIT_FAILURE, "src directory already set");
171 printf("%d\n", CONFIGVERS);
183 if (kernfile != NULL) {
184 kernconfdump(kernfile);
192 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
193 err(2, "%s", PREFIX);
194 if (freopen("DEFAULTS", "r", stdin) != NULL) {
198 if (freopen(PREFIX, "r", stdin) == NULL)
199 err(2, "%s", PREFIX);
202 if (*destdir != '\0') {
203 len = strlen(destdir);
204 while (len > 1 && destdir[len - 1] == '/')
205 destdir[--len] = '\0';
209 strlcpy(destdir, CDIR, sizeof(destdir));
210 strlcat(destdir, PREFIX, sizeof(destdir));
213 SLIST_INIT(&cputype);
217 STAILQ_INIT(&cfgfiles);
222 STAILQ_INIT(&envvars);
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);
248 /* Make compile directory */
249 p = path((char *)NULL);
253 } else if (!S_ISDIR(buf.st_mode))
254 errx(EXIT_FAILURE, "%s isn't a directory", p);
256 configfile(); /* put config file into kernel*/
257 options(); /* make options .h files */
258 makefile(); /* build Makefile */
259 makeenv(); /* build env.c */
260 makehints(); /* build hints.c */
261 headers(); /* make a lot of .h files */
263 printf("Kernel build directory is %s\n", p);
264 printf("Don't forget to do ``make cleandepend && make depend''\n");
270 * determine the root of the kernel source tree
271 * and save that in srcdir.
280 if (realpath("../..", srcdir) == NULL)
281 err(EXIT_FAILURE, "Unable to find root of source tree");
282 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
283 (pwd = strdup(pwd)) != NULL) {
284 /* Remove the last two path components. */
285 for (i = 0; i < 2; i++) {
286 if ((p = strrchr(pwd, '/')) == NULL) {
292 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
293 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
294 strlcpy(srcdir, pwd, MAXPATHLEN);
304 "usage: config [-CgmpV] [-d destdir] [-s srcdir] sysname\n");
305 fprintf(stderr, " config -x kernel\n");
312 if (line_buf == NULL) {
313 line_buf = sbuf_new(NULL, NULL, 80, SBUF_AUTOEXTEND);
314 if (line_buf == NULL) {
315 errx(EXIT_FAILURE, "failed to allocate line buffer");
318 sbuf_clear(line_buf);
325 if (sbuf_finish(line_buf) != 0) {
326 errx(EXIT_FAILURE, "failed to generate line buffer, "
327 "partial line = %s", sbuf_data(line_buf));
329 return sbuf_data(line_buf);
334 * returns EOF on end of file
335 * NULL on end of line
336 * pointer to the word otherwise
346 while ((ch = getc(fp)) != EOF)
347 if (ch != ' ' && ch != '\t')
350 return ((char *)EOF);
363 sbuf_putc(line_buf, ch);
364 /* Negation operator is a word by itself. */
366 return get_line_buf();
368 while ((ch = getc(fp)) != EOF) {
371 sbuf_putc(line_buf, ch);
374 return ((char *)EOF);
375 (void) ungetc(ch, fp);
376 return (get_line_buf());
381 * like get_word but will accept something in double or single quotes
382 * (to allow embedded spaces).
385 get_quoted_word(FILE *fp)
392 while ((ch = getc(fp)) != EOF)
393 if (ch != ' ' && ch != '\t')
396 return ((char *)EOF);
409 if (ch == '"' || ch == '\'') {
413 while ((ch = getc(fp)) != EOF) {
414 if (ch == quote && !escaped_nl)
416 if (ch == '\n' && !escaped_nl) {
417 printf("config: missing quote reading `%s'\n",
421 if (ch == '\\' && !escaped_nl) {
425 if (ch != quote && escaped_nl)
426 sbuf_putc(line_buf, '\\');
427 sbuf_putc(line_buf, ch);
431 sbuf_putc(line_buf, ch);
432 while ((ch = getc(fp)) != EOF) {
435 sbuf_putc(line_buf, ch);
438 (void) ungetc(ch, fp);
441 return ((char *)EOF);
442 return (get_line_buf());
446 * prepend the path to a filename
449 path(const char *file)
454 asprintf(&cp, "%s/%s", destdir, file);
456 cp = strdup(destdir);
461 * Generate configuration file based on actual settings. With this mode, user
462 * will be able to obtain and build conifguration file with one command.
465 configfile_dynamic(struct sbuf *sb)
467 struct cputype *cput;
473 asprintf(&lend, "\\n\\\n");
474 assert(lend != NULL);
475 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
476 sbuf_printf(sb, "ident\t%s%s", ident, lend);
477 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
478 SLIST_FOREACH(cput, &cputype, cpu_next)
479 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
480 SLIST_FOREACH(ol, &mkopt, op_next)
481 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
483 SLIST_FOREACH(ol, &opt, op_next) {
484 if (strncmp(ol->op_name, "DEV_", 4) == 0)
486 sbuf_printf(sb, "options\t%s", ol->op_name);
487 if (ol->op_value != NULL) {
489 for (i = 0; i < strlen(ol->op_value); i++) {
490 if (ol->op_value[i] == '"')
491 sbuf_printf(sb, "\\%c",
494 sbuf_printf(sb, "%c",
497 sbuf_printf(sb, "%s", lend);
499 sbuf_printf(sb, "%s", lend);
503 * Mark this file as containing everything we need.
505 STAILQ_FOREACH(d, &dtab, d_next)
506 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
511 * Generate file from the configuration files.
514 configfile_filebased(struct sbuf *sb)
521 * Try to read all configuration files. Since those will be present as
522 * C string in the macro, we have to slash their ends then the line
525 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
526 cff = fopen(cf->cfg_path, "r");
528 warn("Couldn't open file %s", cf->cfg_path);
531 while ((i = getc(cff)) != EOF) {
533 sbuf_printf(sb, "\\n\\\n");
534 else if (i == '"' || i == '\'')
535 sbuf_printf(sb, "\\%c", i);
550 /* Add main configuration file to the list of files to be included */
552 p = path("config.c.new");
556 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
560 /* Is needed, can be used for backward compatibility. */
561 configfile_filebased(sb);
563 configfile_dynamic(sb);
567 * We print first part of the template, replace our tag with
568 * configuration files content and later continue writing our
571 p = strstr(kernconfstr, KERNCONFTAG);
573 errx(EXIT_FAILURE, "Something went terribly wrong!");
575 fprintf(fo, "%s", kernconfstr);
576 fprintf(fo, "%s", sbuf_data(sb));
577 p += strlen(KERNCONFTAG);
578 fprintf(fo, "%s", p);
581 moveifchanged(path("config.c.new"), path("config.c"));
587 * compare two files; rename if changed.
590 moveifchanged(const char *from_name, const char *to_name)
595 struct stat from_sb, to_sb;
600 if ((from_fd = open(from_name, O_RDONLY)) < 0)
601 err(EX_OSERR, "moveifchanged open(%s)", from_name);
603 if ((to_fd = open(to_name, O_RDONLY)) < 0)
606 if (!changed && fstat(from_fd, &from_sb) < 0)
607 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
609 if (!changed && fstat(to_fd, &to_sb) < 0)
610 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
612 if (!changed && from_sb.st_size != to_sb.st_size)
615 tsize = (size_t)from_sb.st_size;
618 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
620 err(EX_OSERR, "mmap %s", from_name);
621 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
623 err(EX_OSERR, "mmap %s", to_name);
625 changed = memcmp(p, q, tsize);
630 if (rename(from_name, to_name) < 0)
631 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
633 if (unlink(from_name) < 0)
634 err(EX_OSERR, "unlink(%s)", from_name);
639 cleanheaders(char *p)
643 struct file_list *fl;
648 remember("setdefs.h");
649 STAILQ_FOREACH(fl, &ftab, f_next)
653 * Scan the build directory and clean out stuff that looks like
654 * it might have been a leftover NFOO header, etc.
656 if ((dirp = opendir(p)) == NULL)
657 err(EX_OSERR, "opendir %s", p);
658 while ((dp = readdir(dirp)) != NULL) {
659 len = strlen(dp->d_name);
660 /* Skip non-headers */
661 if (len < 2 || dp->d_name[len - 2] != '.' ||
662 dp->d_name[len - 1] != 'h')
664 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
665 if (strchr(dp->d_name, '_') &&
666 strncmp(dp->d_name, "opt_", 4) != 0)
668 /* Check if it is a target file */
669 for (hl = htab; hl != NULL; hl = hl->h_next) {
670 if (eq(dp->d_name, hl->h_name)) {
676 printf("Removing stale header: %s\n", dp->d_name);
677 if (unlink(path(dp->d_name)) == -1)
678 warn("unlink %s", dp->d_name);
680 (void)closedir(dirp);
684 remember(const char *file)
689 if ((s = strrchr(file, '/')) != NULL)
694 if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
698 for (hl = htab; hl != NULL; hl = hl->h_next) {
699 if (eq(s, hl->h_name)) {
704 hl = calloc(1, sizeof(*hl));
706 err(EXIT_FAILURE, "calloc");
713 * This one is quick hack. Will be probably moved to elf(3) interface.
714 * It takes kernel configuration file name, passes it as an argument to
715 * elfdump -a, which output is parsed by some UNIX tools...
718 kernconfdump(const char *file)
723 unsigned int i, off, size, t1, t2, align;
726 r = open(file, O_RDONLY);
728 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
729 error = fstat(r, &st);
731 err(EXIT_FAILURE, "fstat() failed");
732 if (S_ISDIR(st.st_mode))
733 errx(EXIT_FAILURE, "'%s' is a directory", file);
736 err(EXIT_FAILURE, "fdopen() failed");
740 err(EXIT_FAILURE, "Couldn't allocate memory");
741 /* ELF note section header. */
742 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
743 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
745 errx(EXIT_FAILURE, "asprintf() failed");
746 pp = popen(cmd, "r");
748 errx(EXIT_FAILURE, "popen() failed");
750 (void)fread(o, osz, 1, pp);
752 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
755 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
756 "file. Either unsupported, or not compiled with "
757 "INCLUDE_CONFIG_FILE", file);
758 r = fseek(fp, off, SEEK_CUR);
760 err(EXIT_FAILURE, "fseek() failed");
761 for (i = 0; i < size; i++) {
766 assert(i == size - 1 &&
767 ("\\0 found in the middle of a file"));
778 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
779 fprintf(stderr, "config version = %d, ", CONFIGVERS);
780 fprintf(stderr, "version required = %d\n\n", versreq);
781 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
782 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
783 fprintf(stderr, "before trying this again.\n\n");
784 fprintf(stderr, "If running the new config fails check your config\n");
785 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
786 fprintf(stderr, "changes in config syntax, or option/device naming\n");
787 fprintf(stderr, "conventions\n\n");
797 ifp = open_makefile_template();
798 while (fgets(line, BUFSIZ, ifp) != 0) {
801 if (strncmp(line, "%VERSREQ=", 9) != 0)
803 versreq = atoi(line + 9);
804 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
805 versreq <= CONFIGVERS)