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];
95 * Preserve old behaviour in INCLUDE_CONFIG_FILE handling (files are included
101 static void configfile(void);
102 static void get_srcdir(void);
103 static void usage(void);
104 static void cleanheaders(char *);
105 static void kernconfdump(const char *);
106 static void badversion(void);
107 static void checkversion(void);
108 extern int yyparse(void);
112 struct hdr_list *h_next;
115 static struct sbuf *line_buf = NULL;
118 * Config builds a set of files for building a UNIX
119 * system given a description of the desired system.
122 main(int argc, char **argv)
129 struct includepath* ipath;
131 bool cust_dest = false;
135 SLIST_INIT(&includepath);
136 SLIST_INIT(&cputype);
140 STAILQ_INIT(&cfgfiles);
145 STAILQ_INIT(&envvars);
146 while ((ch = getopt(argc, argv, "Cd:gI:mps:Vx:")) != -1)
152 if (*destdir == '\0')
153 strlcpy(destdir, optarg, sizeof(destdir));
155 errx(EXIT_FAILURE, "directory already set");
162 ipath = (struct includepath *) \
163 calloc(1, sizeof (struct includepath));
165 err(EXIT_FAILURE, "calloc");
166 ipath->path = optarg;
167 SLIST_INSERT_HEAD(&includepath, ipath, path_next);
174 strlcpy(srcdir, optarg, sizeof(srcdir));
176 errx(EXIT_FAILURE, "src directory already set");
179 printf("%d\n", CONFIGVERS);
191 if (kernfile != NULL) {
192 kernconfdump(kernfile);
200 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
201 err(2, "%s", PREFIX);
202 if (freopen("DEFAULTS", "r", stdin) != NULL) {
206 if (freopen(PREFIX, "r", stdin) == NULL)
207 err(2, "%s", PREFIX);
210 if (*destdir != '\0') {
211 len = strlen(destdir);
212 while (len > 1 && destdir[len - 1] == '/')
213 destdir[--len] = '\0';
217 strlcpy(destdir, CDIR, sizeof(destdir));
218 strlcat(destdir, PREFIX, sizeof(destdir));
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);
247 * Make CDIR directory, if doing a default destination. Some version
248 * control systems delete empty directories and this seemlessly copes.
250 if (!cust_dest && stat(CDIR, &buf))
251 if (mkdir(CDIR, 0777))
253 /* Create the compile directory */
254 p = path((char *)NULL);
258 } else if (!S_ISDIR(buf.st_mode))
259 errx(EXIT_FAILURE, "%s isn't a directory", p);
261 configfile(); /* put config file into kernel*/
262 options(); /* make options .h files */
263 makefile(); /* build Makefile */
264 makeenv(); /* build env.c */
265 makehints(); /* build hints.c */
266 headers(); /* make a lot of .h files */
268 printf("Kernel build directory is %s\n", p);
269 printf("Don't forget to do ``make cleandepend && make depend''\n");
275 * determine the root of the kernel source tree
276 * and save that in srcdir.
285 if (realpath("../..", srcdir) == NULL)
286 err(EXIT_FAILURE, "Unable to find root of source tree");
287 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
288 (pwd = strdup(pwd)) != NULL) {
289 /* Remove the last two path components. */
290 for (i = 0; i < 2; i++) {
291 if ((p = strrchr(pwd, '/')) == NULL) {
297 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
298 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
299 strlcpy(srcdir, pwd, MAXPATHLEN);
309 "usage: config [-CgmpV] [-d destdir] [-s srcdir] sysname\n");
310 fprintf(stderr, " config -x kernel\n");
317 if (line_buf == NULL) {
318 line_buf = sbuf_new(NULL, NULL, 80, SBUF_AUTOEXTEND);
319 if (line_buf == NULL) {
320 errx(EXIT_FAILURE, "failed to allocate line buffer");
323 sbuf_clear(line_buf);
330 if (sbuf_finish(line_buf) != 0) {
331 errx(EXIT_FAILURE, "failed to generate line buffer, "
332 "partial line = %s", sbuf_data(line_buf));
334 return sbuf_data(line_buf);
339 * returns EOF on end of file
340 * NULL on end of line
341 * pointer to the word otherwise
351 while ((ch = getc(fp)) != EOF)
352 if (ch != ' ' && ch != '\t')
355 return ((char *)EOF);
368 sbuf_putc(line_buf, ch);
369 /* Negation operator is a word by itself. */
371 return get_line_buf();
373 while ((ch = getc(fp)) != EOF) {
376 sbuf_putc(line_buf, ch);
379 return ((char *)EOF);
380 (void) ungetc(ch, fp);
381 return (get_line_buf());
386 * like get_word but will accept something in double or single quotes
387 * (to allow embedded spaces).
390 get_quoted_word(FILE *fp)
397 while ((ch = getc(fp)) != EOF)
398 if (ch != ' ' && ch != '\t')
401 return ((char *)EOF);
414 if (ch == '"' || ch == '\'') {
418 while ((ch = getc(fp)) != EOF) {
419 if (ch == quote && !escaped_nl)
421 if (ch == '\n' && !escaped_nl) {
422 printf("config: missing quote reading `%s'\n",
426 if (ch == '\\' && !escaped_nl) {
430 if (ch != quote && escaped_nl)
431 sbuf_putc(line_buf, '\\');
432 sbuf_putc(line_buf, ch);
436 sbuf_putc(line_buf, ch);
437 while ((ch = getc(fp)) != EOF) {
440 sbuf_putc(line_buf, ch);
443 (void) ungetc(ch, fp);
446 return ((char *)EOF);
447 return (get_line_buf());
451 * prepend the path to a filename
454 path(const char *file)
459 asprintf(&cp, "%s/%s", destdir, file);
461 cp = strdup(destdir);
466 * Generate configuration file based on actual settings. With this mode, user
467 * will be able to obtain and build conifguration file with one command.
470 configfile_dynamic(struct sbuf *sb)
472 struct cputype *cput;
478 asprintf(&lend, "\\n\\\n");
479 assert(lend != NULL);
480 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
481 sbuf_printf(sb, "ident\t%s%s", ident, lend);
482 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
483 SLIST_FOREACH(cput, &cputype, cpu_next)
484 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
485 SLIST_FOREACH(ol, &mkopt, op_next)
486 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
488 SLIST_FOREACH(ol, &opt, op_next) {
489 if (strncmp(ol->op_name, "DEV_", 4) == 0)
491 sbuf_printf(sb, "options\t%s", ol->op_name);
492 if (ol->op_value != NULL) {
494 for (i = 0; i < strlen(ol->op_value); i++) {
495 if (ol->op_value[i] == '"')
496 sbuf_printf(sb, "\\%c",
499 sbuf_printf(sb, "%c",
502 sbuf_printf(sb, "%s", lend);
504 sbuf_printf(sb, "%s", lend);
508 * Mark this file as containing everything we need.
510 STAILQ_FOREACH(d, &dtab, d_next)
511 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
516 * Generate file from the configuration files.
519 configfile_filebased(struct sbuf *sb)
526 * Try to read all configuration files. Since those will be present as
527 * C string in the macro, we have to slash their ends then the line
530 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
531 cff = fopen(cf->cfg_path, "r");
533 warn("Couldn't open file %s", cf->cfg_path);
536 while ((i = getc(cff)) != EOF) {
538 sbuf_printf(sb, "\\n\\\n");
539 else if (i == '"' || i == '\'')
540 sbuf_printf(sb, "\\%c", i);
555 /* Add main configuration file to the list of files to be included */
557 p = path("config.c.new");
561 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
565 /* Is needed, can be used for backward compatibility. */
566 configfile_filebased(sb);
568 configfile_dynamic(sb);
572 * We print first part of the template, replace our tag with
573 * configuration files content and later continue writing our
576 p = strstr(kernconfstr, KERNCONFTAG);
578 errx(EXIT_FAILURE, "Something went terribly wrong!");
580 fprintf(fo, "%s", kernconfstr);
581 fprintf(fo, "%s", sbuf_data(sb));
582 p += strlen(KERNCONFTAG);
583 fprintf(fo, "%s", p);
586 moveifchanged(path("config.c.new"), path("config.c"));
592 * compare two files; rename if changed.
595 moveifchanged(const char *from_name, const char *to_name)
600 struct stat from_sb, to_sb;
605 if ((from_fd = open(from_name, O_RDONLY)) < 0)
606 err(EX_OSERR, "moveifchanged open(%s)", from_name);
608 if ((to_fd = open(to_name, O_RDONLY)) < 0)
611 if (!changed && fstat(from_fd, &from_sb) < 0)
612 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
614 if (!changed && fstat(to_fd, &to_sb) < 0)
615 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
617 if (!changed && from_sb.st_size != to_sb.st_size)
620 tsize = (size_t)from_sb.st_size;
623 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
625 err(EX_OSERR, "mmap %s", from_name);
626 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
628 err(EX_OSERR, "mmap %s", to_name);
630 changed = memcmp(p, q, tsize);
635 if (rename(from_name, to_name) < 0)
636 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
638 if (unlink(from_name) < 0)
639 err(EX_OSERR, "unlink(%s)", from_name);
644 cleanheaders(char *p)
648 struct file_list *fl;
653 remember("setdefs.h");
654 STAILQ_FOREACH(fl, &ftab, f_next)
658 * Scan the build directory and clean out stuff that looks like
659 * it might have been a leftover NFOO header, etc.
661 if ((dirp = opendir(p)) == NULL)
662 err(EX_OSERR, "opendir %s", p);
663 while ((dp = readdir(dirp)) != NULL) {
664 len = strlen(dp->d_name);
665 /* Skip non-headers */
666 if (len < 2 || dp->d_name[len - 2] != '.' ||
667 dp->d_name[len - 1] != 'h')
669 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
670 if (strchr(dp->d_name, '_') &&
671 strncmp(dp->d_name, "opt_", 4) != 0)
673 /* Check if it is a target file */
674 for (hl = htab; hl != NULL; hl = hl->h_next) {
675 if (eq(dp->d_name, hl->h_name)) {
681 printf("Removing stale header: %s\n", dp->d_name);
682 if (unlink(path(dp->d_name)) == -1)
683 warn("unlink %s", dp->d_name);
685 (void)closedir(dirp);
689 remember(const char *file)
694 if ((s = strrchr(file, '/')) != NULL)
699 if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
703 for (hl = htab; hl != NULL; hl = hl->h_next) {
704 if (eq(s, hl->h_name)) {
709 hl = calloc(1, sizeof(*hl));
711 err(EXIT_FAILURE, "calloc");
718 * This one is quick hack. Will be probably moved to elf(3) interface.
719 * It takes kernel configuration file name, passes it as an argument to
720 * elfdump -a, which output is parsed by some UNIX tools...
723 kernconfdump(const char *file)
728 unsigned int i, off, size, t1, t2, align;
731 r = open(file, O_RDONLY);
733 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
734 error = fstat(r, &st);
736 err(EXIT_FAILURE, "fstat() failed");
737 if (S_ISDIR(st.st_mode))
738 errx(EXIT_FAILURE, "'%s' is a directory", file);
741 err(EXIT_FAILURE, "fdopen() failed");
745 err(EXIT_FAILURE, "Couldn't allocate memory");
746 /* ELF note section header. */
747 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
748 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
750 errx(EXIT_FAILURE, "asprintf() failed");
751 pp = popen(cmd, "r");
753 errx(EXIT_FAILURE, "popen() failed");
755 (void)fread(o, osz, 1, pp);
757 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
760 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
761 "file. Either unsupported, or not compiled with "
762 "INCLUDE_CONFIG_FILE", file);
763 r = fseek(fp, off, SEEK_CUR);
765 err(EXIT_FAILURE, "fseek() failed");
766 for (i = 0; i < size; i++) {
771 assert(i == size - 1 &&
772 ("\\0 found in the middle of a file"));
783 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
784 fprintf(stderr, "config version = %d, ", CONFIGVERS);
785 fprintf(stderr, "version required = %d\n\n", versreq);
786 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
787 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
788 fprintf(stderr, "before trying this again.\n\n");
789 fprintf(stderr, "If running the new config fails check your config\n");
790 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
791 fprintf(stderr, "changes in config syntax, or option/device naming\n");
792 fprintf(stderr, "conventions\n\n");
802 ifp = open_makefile_template();
803 while (fgets(line, BUFSIZ, ifp) != 0) {
806 if (strncmp(line, "%VERSREQ=", 9) != 0)
808 versreq = atoi(line + 9);
809 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
810 versreq <= CONFIGVERS)