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>
50 #include <sys/param.h>
65 #include "configvers.h"
75 #define CDIR "../compile/"
80 struct cfgfile_head cfgfiles;
81 struct cputype_head cputype;
82 struct opt_head opt, mkopt, rmopts;
83 struct opt_list_head otab;
84 struct envvar_head envvars;
85 struct hint_head hints;
86 struct includepath_head includepath;
89 char destdir[MAXPATHLEN];
90 char srcdir[MAXPATHLEN];
97 * Preserve old behaviour in INCLUDE_CONFIG_FILE handling (files are included
103 static void configfile(void);
104 static void get_srcdir(void);
105 static void usage(void);
106 static void cleanheaders(char *);
107 static void kernconfdump(const char *);
108 static void badversion(void);
109 static void checkversion(void);
113 struct hdr_list *h_next;
116 static std::stringstream line_buf;
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;
132 bool cust_dest = false;
136 SLIST_INIT(&includepath);
137 SLIST_INIT(&cputype);
141 STAILQ_INIT(&cfgfiles);
146 STAILQ_INIT(&envvars);
147 while ((ch = getopt(argc, argv, "Cd:gI:mps:Vx:")) != -1)
153 if (*destdir == '\0')
154 strlcpy(destdir, optarg, sizeof(destdir));
156 errx(EXIT_FAILURE, "directory already set");
163 ipath = (struct includepath *) \
164 calloc(1, sizeof (struct includepath));
166 err(EXIT_FAILURE, "calloc");
167 ipath->path = optarg;
168 SLIST_INSERT_HEAD(&includepath, ipath, path_next);
175 strlcpy(srcdir, optarg, sizeof(srcdir));
177 errx(EXIT_FAILURE, "src directory already set");
180 printf("%d\n", CONFIGVERS);
192 if (kernfile != NULL) {
193 kernconfdump(kernfile);
201 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
202 err(2, "%s", PREFIX);
203 if (freopen("DEFAULTS", "r", stdin) != NULL) {
207 if (freopen(PREFIX, "r", stdin) == NULL)
208 err(2, "%s", PREFIX);
211 if (*destdir != '\0') {
212 len = strlen(destdir);
213 while (len > 1 && destdir[len - 1] == '/')
214 destdir[--len] = '\0';
218 strlcpy(destdir, CDIR, sizeof(destdir));
219 strlcat(destdir, PREFIX, sizeof(destdir));
226 * Ensure that required elements (machine, cpu, ident) are present.
228 if (machinename == NULL) {
229 printf("Specify machine type, e.g. ``machine i386''\n");
233 printf("no ident line specified\n");
236 if (SLIST_EMPTY(&cputype)) {
237 printf("cpu type must be specified\n");
243 printf("%s\t%s\n",machinename,machinearch);
248 * Make CDIR directory, if doing a default destination. Some version
249 * control systems delete empty directories and this seemlessly copes.
251 if (!cust_dest && stat(CDIR, &buf))
252 if (mkdir(CDIR, 0777))
254 /* Create the compile directory */
255 p = path((char *)NULL);
259 } else if (!S_ISDIR(buf.st_mode))
260 errx(EXIT_FAILURE, "%s isn't a directory", p);
262 configfile(); /* put config file into kernel*/
263 options(); /* make options .h files */
264 makefile(); /* build Makefile */
265 makeenv(); /* build env.c */
266 makehints(); /* build hints.c */
267 headers(); /* make a lot of .h files */
269 printf("Kernel build directory is %s\n", p);
270 printf("Don't forget to do ``make cleandepend && make depend''\n");
276 * determine the root of the kernel source tree
277 * and save that in srcdir.
286 if (realpath("../..", srcdir) == NULL)
287 err(EXIT_FAILURE, "Unable to find root of source tree");
288 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
289 (pwd = strdup(pwd)) != NULL) {
290 /* Remove the last two path components. */
291 for (i = 0; i < 2; i++) {
292 if ((p = strrchr(pwd, '/')) == NULL) {
298 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
299 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
300 strlcpy(srcdir, pwd, MAXPATHLEN);
310 "usage: config [-CgmpV] [-d destdir] [-s srcdir] sysname\n");
311 fprintf(stderr, " config -x kernel\n");
327 if (!line_buf.good()) {
328 errx(EXIT_FAILURE, "failed to generate line buffer, "
329 "partial line = %s", line_buf.str().c_str());
332 return line_buf.str();
337 * returns EOF on end of file
338 * NULL on end of line
339 * pointer to the word otherwise
349 while ((ch = getc(fp)) != EOF)
350 if (ch != ' ' && ch != '\t')
353 return (configword().eof(true));
364 return (configword().eol(true));
366 line_buf << (char)ch;
367 /* Negation operator is a word by itself. */
369 return (configword(get_line_buf()));
371 while ((ch = getc(fp)) != EOF) {
374 line_buf << (char)ch;
377 return (configword().eof(true));
378 (void) ungetc(ch, fp);
379 return (configword(get_line_buf()));
384 * like get_word but will accept something in double or single quotes
385 * (to allow embedded spaces).
388 get_quoted_word(FILE *fp)
395 while ((ch = getc(fp)) != EOF)
396 if (ch != ' ' && ch != '\t')
399 return (configword().eof(true));
410 return (configword().eol(true));
412 if (ch == '"' || ch == '\'') {
416 while ((ch = getc(fp)) != EOF) {
417 if (ch == quote && !escaped_nl)
419 if (ch == '\n' && !escaped_nl) {
420 printf("config: missing quote reading `%s'\n",
421 get_line_buf().c_str());
424 if (ch == '\\' && !escaped_nl) {
428 if (ch != quote && escaped_nl)
430 line_buf << (char)ch;
434 line_buf << (char)ch;
435 while ((ch = getc(fp)) != EOF) {
438 line_buf << (char)ch;
441 (void) ungetc(ch, fp);
444 return (configword().eof(true));
445 return (configword(get_line_buf()));
449 * prepend the path to a filename
452 path(const char *file)
457 asprintf(&cp, "%s/%s", destdir, file);
459 cp = strdup(destdir);
461 err(EXIT_FAILURE, "malloc");
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(std::ostringstream &cfg)
472 struct cputype *cput;
478 asprintf(&lend, "\\n\\\n");
479 assert(lend != NULL);
480 cfg << "options\t" << OPT_AUTOGEN << lend;
481 cfg << "ident\t" << ident << lend;
482 cfg << "machine\t" << machinename << lend;
483 SLIST_FOREACH(cput, &cputype, cpu_next)
484 cfg << "cpu\t" << cput->cpu_name << lend;
485 SLIST_FOREACH(ol, &mkopt, op_next)
486 cfg << "makeoptions\t" << ol->op_name << '=' <<
487 ol->op_value << lend;
488 SLIST_FOREACH(ol, &opt, op_next) {
489 if (strncmp(ol->op_name, "DEV_", 4) == 0)
491 cfg << "options\t" << 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 cfg << '\\' << ol->op_value[i];
498 cfg << ol->op_value[i];
505 * Mark this file as containing everything we need.
507 STAILQ_FOREACH(d, &dtab, d_next)
508 cfg << "device\t" << d->d_name << lend;
513 * Generate file from the configuration files.
516 configfile_filebased(std::ostringstream &cfg)
523 * Try to read all configuration files. Since those will be present as
524 * C string in the macro, we have to slash their ends then the line
527 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
528 cff = fopen(cf->cfg_path, "r");
530 warn("Couldn't open file %s", cf->cfg_path);
533 while ((i = getc(cff)) != EOF) {
536 else if (i == '"' || i == '\'')
549 std::ostringstream cfg;
552 /* Add main configuration file to the list of files to be included */
554 p = path("config.c.new");
561 /* Is needed, can be used for backward compatibility. */
562 configfile_filebased(cfg);
564 configfile_dynamic(cfg);
569 * We print first part of the template, replace our tag with
570 * configuration files content and later continue writing our
573 p = strstr(kernconfstr, KERNCONFTAG);
575 errx(EXIT_FAILURE, "Something went terribly wrong!");
577 fprintf(fo, "%s", kernconfstr);
578 fprintf(fo, "%s", cfg.str().c_str());
579 p += strlen(KERNCONFTAG);
580 fprintf(fo, "%s", p);
582 moveifchanged("config.c.new", "config.c");
588 * compare two files; rename if changed.
591 moveifchanged(const char *from_name, const char *to_name)
594 char *from_path, *to_path;
597 struct stat from_sb, to_sb;
602 from_path = path(from_name);
603 to_path = path(to_name);
604 if ((from_fd = open(from_path, O_RDONLY)) < 0)
605 err(EX_OSERR, "moveifchanged open(%s)", from_name);
607 if ((to_fd = open(to_path, O_RDONLY)) < 0)
610 if (!changed && fstat(from_fd, &from_sb) < 0)
611 err(EX_OSERR, "moveifchanged fstat(%s)", from_path);
613 if (!changed && fstat(to_fd, &to_sb) < 0)
614 err(EX_OSERR, "moveifchanged fstat(%s)", to_path);
616 if (!changed && from_sb.st_size != to_sb.st_size)
620 tsize = (size_t)from_sb.st_size;
622 p = (char *)mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd,
625 err(EX_OSERR, "mmap %s", from_path);
626 q = (char *)mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd,
629 err(EX_OSERR, "mmap %s", to_path);
631 changed = memcmp(p, q, tsize);
637 if (rename(from_path, to_path) < 0)
638 err(EX_OSERR, "rename(%s, %s)", from_path, to_path);
640 if (unlink(from_path) < 0)
641 err(EX_OSERR, "unlink(%s)", from_path);
653 cleanheaders(char *p)
657 struct file_list *fl;
662 remember("setdefs.h");
663 STAILQ_FOREACH(fl, &ftab, f_next)
667 * Scan the build directory and clean out stuff that looks like
668 * it might have been a leftover NFOO header, etc.
670 if ((dirp = opendir(p)) == NULL)
671 err(EX_OSERR, "opendir %s", p);
672 while ((dp = readdir(dirp)) != NULL) {
673 len = strlen(dp->d_name);
674 /* Skip non-headers */
675 if (len < 2 || dp->d_name[len - 2] != '.' ||
676 dp->d_name[len - 1] != 'h')
678 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
679 if (strchr(dp->d_name, '_') &&
680 strncmp(dp->d_name, "opt_", 4) != 0)
682 /* Check if it is a target file */
683 for (hl = htab; hl != NULL; hl = hl->h_next) {
684 if (eq(dp->d_name, hl->h_name)) {
690 printf("Removing stale header: %s\n", dp->d_name);
691 p = path(dp->d_name);
693 warn("unlink %s", dp->d_name);
696 (void)closedir(dirp);
700 remember(const char *file)
705 if ((s = strrchr(file, '/')) != NULL)
710 if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
711 free(__DECONST(char *, s));
714 for (hl = htab; hl != NULL; hl = hl->h_next) {
715 if (eq(s, hl->h_name)) {
716 free(__DECONST(char *, s));
720 hl = (struct hdr_list *)calloc(1, sizeof(*hl));
722 err(EXIT_FAILURE, "calloc");
729 * This one is quick hack. Will be probably moved to elf(3) interface.
730 * It takes kernel configuration file name, passes it as an argument to
731 * elfdump -a, which output is parsed by some UNIX tools...
734 kernconfdump(const char *file)
739 size_t i, off, size, t1, t2, align;
742 r = open(file, O_RDONLY);
744 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
745 error = fstat(r, &st);
747 err(EXIT_FAILURE, "fstat() failed");
748 if (S_ISDIR(st.st_mode))
749 errx(EXIT_FAILURE, "'%s' is a directory", file);
752 err(EXIT_FAILURE, "fdopen() failed");
754 o = (char *)calloc(1, osz);
756 err(EXIT_FAILURE, "Couldn't allocate memory");
757 /* ELF note section header. */
758 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
759 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
761 errx(EXIT_FAILURE, "asprintf() failed");
762 pp = popen(cmd, "r");
764 errx(EXIT_FAILURE, "popen() failed");
766 (void)fread(o, osz, 1, pp);
768 r = sscanf(o, "%zu%zu%zu%zu%zu", &off, &size, &t1, &t2, &align);
770 if (size > SIZE_MAX - off || off + size > (size_t)st.st_size)
771 errx(EXIT_FAILURE, "%s: incoherent ELF headers", file);
773 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
774 "file. Either unsupported, or not compiled with "
775 "INCLUDE_CONFIG_FILE", file);
776 r = fseek(fp, off, SEEK_CUR);
778 err(EXIT_FAILURE, "fseek() failed");
779 for (i = 0; i < size; i++) {
784 assert(i == size - 1 &&
785 ("\\0 found in the middle of a file"));
796 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
797 fprintf(stderr, "config version = %d, ", CONFIGVERS);
798 fprintf(stderr, "version required = %d\n\n", versreq);
799 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
800 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
801 fprintf(stderr, "before trying this again.\n\n");
802 fprintf(stderr, "If running the new config fails check your config\n");
803 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
804 fprintf(stderr, "changes in config syntax, or option/device naming\n");
805 fprintf(stderr, "conventions\n\n");
815 ifp = open_makefile_template();
816 while (fgets(line, BUFSIZ, ifp) != 0) {
819 if (strncmp(line, "%VERSREQ=", 9) != 0)
821 versreq = atoi(line + 9);
822 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
823 versreq <= CONFIGVERS)