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;
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);
178 strlcpy(srcdir, optarg, sizeof(srcdir));
180 errx(EXIT_FAILURE, "src directory already set");
183 printf("%d\n", CONFIGVERS);
195 if (kernfile != NULL) {
196 kernconfdump(kernfile);
204 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
205 err(2, "%s", PREFIX);
206 if (freopen("DEFAULTS", "r", stdin) != NULL) {
210 if (freopen(PREFIX, "r", stdin) == NULL)
211 err(2, "%s", PREFIX);
214 if (*destdir != '\0') {
215 len = strlen(destdir);
216 while (len > 1 && destdir[len - 1] == '/')
217 destdir[--len] = '\0';
221 strlcpy(destdir, CDIR, sizeof(destdir));
222 strlcat(destdir, PREFIX, sizeof(destdir));
229 * Ensure that required elements (machine, cpu, ident) are present.
231 if (machinename == NULL) {
232 printf("Specify machine type, e.g. ``machine i386''\n");
236 printf("no ident line specified\n");
239 if (SLIST_EMPTY(&cputype)) {
240 printf("cpu type must be specified\n");
246 printf("%s\t%s\n",machinename,machinearch);
251 * Make CDIR directory, if doing a default destination. Some version
252 * control systems delete empty directories and this seemlessly copes.
254 if (!cust_dest && stat(CDIR, &buf))
255 if (mkdir(CDIR, 0777))
257 /* Create the compile directory */
258 p = path((char *)NULL);
262 } else if (!S_ISDIR(buf.st_mode))
263 errx(EXIT_FAILURE, "%s isn't a directory", p);
265 configfile(); /* put config file into kernel*/
266 options(); /* make options .h files */
267 makefile(); /* build Makefile */
268 makeenv(); /* build env.c */
269 makehints(); /* build hints.c */
270 headers(); /* make a lot of .h files */
272 printf("Kernel build directory is %s\n", p);
273 printf("Don't forget to do ``make cleandepend && make depend''\n");
279 * determine the root of the kernel source tree
280 * and save that in srcdir.
289 if (realpath("../..", srcdir) == NULL)
290 err(EXIT_FAILURE, "Unable to find root of source tree");
291 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
292 (pwd = strdup(pwd)) != NULL) {
293 /* Remove the last two path components. */
294 for (i = 0; i < 2; i++) {
295 if ((p = strrchr(pwd, '/')) == NULL) {
301 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
302 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
303 strlcpy(srcdir, pwd, MAXPATHLEN);
313 "usage: config [-CgmpV] [-d destdir] [-s srcdir] sysname\n");
314 fprintf(stderr, " config -x kernel\n");
321 if (line_buf == NULL) {
322 line_buf = sbuf_new(NULL, NULL, 80, SBUF_AUTOEXTEND);
323 if (line_buf == NULL) {
324 errx(EXIT_FAILURE, "failed to allocate line buffer");
327 sbuf_clear(line_buf);
334 if (sbuf_finish(line_buf) != 0) {
335 errx(EXIT_FAILURE, "failed to generate line buffer, "
336 "partial line = %s", sbuf_data(line_buf));
338 return sbuf_data(line_buf);
343 * returns EOF on end of file
344 * NULL on end of line
345 * pointer to the word otherwise
355 while ((ch = getc(fp)) != EOF)
356 if (ch != ' ' && ch != '\t')
359 return ((char *)EOF);
372 sbuf_putc(line_buf, ch);
373 /* Negation operator is a word by itself. */
375 return get_line_buf();
377 while ((ch = getc(fp)) != EOF) {
380 sbuf_putc(line_buf, ch);
383 return ((char *)EOF);
384 (void) ungetc(ch, fp);
385 return (get_line_buf());
390 * like get_word but will accept something in double or single quotes
391 * (to allow embedded spaces).
394 get_quoted_word(FILE *fp)
401 while ((ch = getc(fp)) != EOF)
402 if (ch != ' ' && ch != '\t')
405 return ((char *)EOF);
418 if (ch == '"' || ch == '\'') {
422 while ((ch = getc(fp)) != EOF) {
423 if (ch == quote && !escaped_nl)
425 if (ch == '\n' && !escaped_nl) {
426 printf("config: missing quote reading `%s'\n",
430 if (ch == '\\' && !escaped_nl) {
434 if (ch != quote && escaped_nl)
435 sbuf_putc(line_buf, '\\');
436 sbuf_putc(line_buf, ch);
440 sbuf_putc(line_buf, ch);
441 while ((ch = getc(fp)) != EOF) {
444 sbuf_putc(line_buf, ch);
447 (void) ungetc(ch, fp);
450 return ((char *)EOF);
451 return (get_line_buf());
455 * prepend the path to a filename
458 path(const char *file)
463 asprintf(&cp, "%s/%s", destdir, file);
465 cp = strdup(destdir);
470 * Generate configuration file based on actual settings. With this mode, user
471 * will be able to obtain and build conifguration file with one command.
474 configfile_dynamic(struct sbuf *sb)
476 struct cputype *cput;
482 asprintf(&lend, "\\n\\\n");
483 assert(lend != NULL);
484 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
485 sbuf_printf(sb, "ident\t%s%s", ident, lend);
486 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
487 SLIST_FOREACH(cput, &cputype, cpu_next)
488 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
489 SLIST_FOREACH(ol, &mkopt, op_next)
490 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
492 SLIST_FOREACH(ol, &opt, op_next) {
493 if (strncmp(ol->op_name, "DEV_", 4) == 0)
495 sbuf_printf(sb, "options\t%s", ol->op_name);
496 if (ol->op_value != NULL) {
498 for (i = 0; i < strlen(ol->op_value); i++) {
499 if (ol->op_value[i] == '"')
500 sbuf_printf(sb, "\\%c",
503 sbuf_printf(sb, "%c",
506 sbuf_printf(sb, "%s", lend);
508 sbuf_printf(sb, "%s", lend);
512 * Mark this file as containing everything we need.
514 STAILQ_FOREACH(d, &dtab, d_next)
515 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
520 * Generate file from the configuration files.
523 configfile_filebased(struct sbuf *sb)
530 * Try to read all configuration files. Since those will be present as
531 * C string in the macro, we have to slash their ends then the line
534 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
535 cff = fopen(cf->cfg_path, "r");
537 warn("Couldn't open file %s", cf->cfg_path);
540 while ((i = getc(cff)) != EOF) {
542 sbuf_printf(sb, "\\n\\\n");
543 else if (i == '"' || i == '\'')
544 sbuf_printf(sb, "\\%c", i);
559 /* Add main configuration file to the list of files to be included */
561 p = path("config.c.new");
565 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
569 /* Is needed, can be used for backward compatibility. */
570 configfile_filebased(sb);
572 configfile_dynamic(sb);
576 * We print first part of the template, replace our tag with
577 * configuration files content and later continue writing our
580 p = strstr(kernconfstr, KERNCONFTAG);
582 errx(EXIT_FAILURE, "Something went terribly wrong!");
584 fprintf(fo, "%s", kernconfstr);
585 fprintf(fo, "%s", sbuf_data(sb));
586 p += strlen(KERNCONFTAG);
587 fprintf(fo, "%s", p);
590 moveifchanged(path("config.c.new"), path("config.c"));
596 * compare two files; rename if changed.
599 moveifchanged(const char *from_name, const char *to_name)
604 struct stat from_sb, to_sb;
609 if ((from_fd = open(from_name, O_RDONLY)) < 0)
610 err(EX_OSERR, "moveifchanged open(%s)", from_name);
612 if ((to_fd = open(to_name, O_RDONLY)) < 0)
615 if (!changed && fstat(from_fd, &from_sb) < 0)
616 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
618 if (!changed && fstat(to_fd, &to_sb) < 0)
619 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
621 if (!changed && from_sb.st_size != to_sb.st_size)
624 tsize = (size_t)from_sb.st_size;
627 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
629 err(EX_OSERR, "mmap %s", from_name);
630 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
632 err(EX_OSERR, "mmap %s", to_name);
634 changed = memcmp(p, q, tsize);
639 if (rename(from_name, to_name) < 0)
640 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
642 if (unlink(from_name) < 0)
643 err(EX_OSERR, "unlink(%s)", from_name);
648 cleanheaders(char *p)
652 struct file_list *fl;
657 remember("setdefs.h");
658 STAILQ_FOREACH(fl, &ftab, f_next)
662 * Scan the build directory and clean out stuff that looks like
663 * it might have been a leftover NFOO header, etc.
665 if ((dirp = opendir(p)) == NULL)
666 err(EX_OSERR, "opendir %s", p);
667 while ((dp = readdir(dirp)) != NULL) {
668 len = strlen(dp->d_name);
669 /* Skip non-headers */
670 if (len < 2 || dp->d_name[len - 2] != '.' ||
671 dp->d_name[len - 1] != 'h')
673 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
674 if (strchr(dp->d_name, '_') &&
675 strncmp(dp->d_name, "opt_", 4) != 0)
677 /* Check if it is a target file */
678 for (hl = htab; hl != NULL; hl = hl->h_next) {
679 if (eq(dp->d_name, hl->h_name)) {
685 printf("Removing stale header: %s\n", dp->d_name);
686 if (unlink(path(dp->d_name)) == -1)
687 warn("unlink %s", dp->d_name);
689 (void)closedir(dirp);
693 remember(const char *file)
698 if ((s = strrchr(file, '/')) != NULL)
703 if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
707 for (hl = htab; hl != NULL; hl = hl->h_next) {
708 if (eq(s, hl->h_name)) {
713 hl = calloc(1, sizeof(*hl));
715 err(EXIT_FAILURE, "calloc");
722 * This one is quick hack. Will be probably moved to elf(3) interface.
723 * It takes kernel configuration file name, passes it as an argument to
724 * elfdump -a, which output is parsed by some UNIX tools...
727 kernconfdump(const char *file)
732 unsigned int i, off, size, t1, t2, align;
735 r = open(file, O_RDONLY);
737 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
738 error = fstat(r, &st);
740 err(EXIT_FAILURE, "fstat() failed");
741 if (S_ISDIR(st.st_mode))
742 errx(EXIT_FAILURE, "'%s' is a directory", file);
745 err(EXIT_FAILURE, "fdopen() failed");
749 err(EXIT_FAILURE, "Couldn't allocate memory");
750 /* ELF note section header. */
751 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
752 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
754 errx(EXIT_FAILURE, "asprintf() failed");
755 pp = popen(cmd, "r");
757 errx(EXIT_FAILURE, "popen() failed");
759 (void)fread(o, osz, 1, pp);
761 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
764 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
765 "file. Either unsupported, or not compiled with "
766 "INCLUDE_CONFIG_FILE", file);
767 r = fseek(fp, off, SEEK_CUR);
769 err(EXIT_FAILURE, "fseek() failed");
770 for (i = 0; i < size; i++) {
775 assert(i == size - 1 &&
776 ("\\0 found in the middle of a file"));
787 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
788 fprintf(stderr, "config version = %d, ", CONFIGVERS);
789 fprintf(stderr, "version required = %d\n\n", versreq);
790 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
791 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
792 fprintf(stderr, "before trying this again.\n\n");
793 fprintf(stderr, "If running the new config fails check your config\n");
794 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
795 fprintf(stderr, "changes in config syntax, or option/device naming\n");
796 fprintf(stderr, "conventions\n\n");
806 ifp = open_makefile_template();
807 while (fgets(line, BUFSIZ, ifp) != 0) {
810 if (strncmp(line, "%VERSREQ=", 9) != 0)
812 versreq = atoi(line + 9);
813 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
814 versreq <= CONFIGVERS)