2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
40 static const char rcsid[] =
44 #include <sys/types.h>
49 #include <sys/param.h>
61 #include "configvers.h"
71 #define CDIR "../compile/"
74 char destdir[MAXPATHLEN];
75 char srcdir[MAXPATHLEN];
83 * Preserve old behaviour in INCLUDE_CONFIG_FILE handling (files are included
88 static void configfile(void);
89 static void get_srcdir(void);
90 static void usage(void);
91 static void cleanheaders(char *);
92 static void kernconfdump(const char *);
93 static void checkversion(void);
94 extern int yyparse(void);
98 struct hdr_list *h_next;
102 * Config builds a set of files for building a UNIX
103 * system given a description of the desired system.
106 main(int argc, char **argv)
113 struct includepath* ipath;
118 SLIST_INIT(&includepath);
119 while ((ch = getopt(argc, argv, "CI:d:gmpVx:")) != -1)
125 ipath = (struct includepath *) \
126 calloc(1, sizeof (struct includepath));
128 err(EXIT_FAILURE, "calloc");
129 ipath->path = optarg;
130 SLIST_INSERT_HEAD(&includepath, ipath, path_next);
136 if (*destdir == '\0')
137 strlcpy(destdir, optarg, sizeof(destdir));
139 errx(EXIT_FAILURE, "directory already set");
148 printf("%d\n", CONFIGVERS);
160 if (kernfile != NULL) {
161 kernconfdump(kernfile);
169 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
170 err(2, "%s", PREFIX);
171 if (freopen("DEFAULTS", "r", stdin) != NULL) {
175 if (freopen(PREFIX, "r", stdin) == NULL)
176 err(2, "%s", PREFIX);
179 if (*destdir != '\0') {
180 len = strlen(destdir);
181 while (len > 1 && destdir[len - 1] == '/')
182 destdir[--len] = '\0';
185 strlcpy(destdir, CDIR, sizeof(destdir));
186 strlcat(destdir, PREFIX, sizeof(destdir));
189 SLIST_INIT(&cputype);
193 STAILQ_INIT(&cfgfiles);
202 * Ensure that required elements (machine, cpu, ident) are present.
204 if (machinename == NULL) {
205 printf("Specify machine type, e.g. ``machine i386''\n");
209 printf("no ident line specified\n");
212 if (SLIST_EMPTY(&cputype)) {
213 printf("cpu type must be specified\n");
219 printf("%s\t%s\n",machinename,machinearch);
223 /* Make compile directory */
224 p = path((char *)NULL);
228 } else if (!S_ISDIR(buf.st_mode))
229 errx(EXIT_FAILURE, "%s isn't a directory", p);
231 configfile(); /* put config file into kernel*/
232 options(); /* make options .h files */
233 makefile(); /* build Makefile */
234 makeenv(); /* build env.c */
235 makehints(); /* build hints.c */
236 headers(); /* make a lot of .h files */
238 printf("Kernel build directory is %s\n", p);
239 printf("Don't forget to do ``make cleandepend && make depend''\n");
245 * determine the root of the kernel source tree
246 * and save that in srcdir.
255 if (realpath("../..", srcdir) == NULL)
256 err(EXIT_FAILURE, "Unable to find root of source tree");
257 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
258 (pwd = strdup(pwd)) != NULL) {
259 /* Remove the last two path components. */
260 for (i = 0; i < 2; i++) {
261 if ((p = strrchr(pwd, '/')) == NULL) {
267 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
268 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
269 strlcpy(srcdir, pwd, MAXPATHLEN);
278 fprintf(stderr, "usage: config [-CgmpV] [-d destdir] sysname\n");
279 fprintf(stderr, " config -x kernel\n");
285 * returns EOF on end of file
286 * NULL on end of line
287 * pointer to the word otherwise
292 static char line[80];
298 while ((ch = getc(fp)) != EOF)
299 if (ch != ' ' && ch != '\t')
302 return ((char *)EOF);
317 /* Negation operator is a word by itself. */
322 while ((ch = getc(fp)) != EOF) {
329 return ((char *)EOF);
330 (void) ungetc(ch, fp);
336 * like get_word but will accept something in double or single quotes
337 * (to allow embedded spaces).
340 get_quoted_word(FILE *fp)
342 static char line[256];
348 while ((ch = getc(fp)) != EOF)
349 if (ch != ' ' && ch != '\t')
352 return ((char *)EOF);
366 if (ch == '"' || ch == '\'') {
370 while ((ch = getc(fp)) != EOF) {
371 if (ch == quote && !escaped_nl)
373 if (ch == '\n' && !escaped_nl) {
375 printf("config: missing quote reading `%s'\n",
379 if (ch == '\\' && !escaped_nl) {
383 if (ch != quote && escaped_nl)
390 while ((ch = getc(fp)) != EOF) {
396 (void) ungetc(ch, fp);
400 return ((char *)EOF);
405 * prepend the path to a filename
408 path(const char *file)
413 asprintf(&cp, "%s/%s", destdir, file);
415 cp = strdup(destdir);
420 * Generate configuration file based on actual settings. With this mode, user
421 * will be able to obtain and build conifguration file with one command.
424 configfile_dynamic(struct sbuf *sb)
426 struct cputype *cput;
432 asprintf(&lend, "\\n\\\n");
433 assert(lend != NULL);
434 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
435 sbuf_printf(sb, "ident\t%s%s", ident, lend);
436 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
437 SLIST_FOREACH(cput, &cputype, cpu_next)
438 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
439 SLIST_FOREACH(ol, &mkopt, op_next)
440 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
442 SLIST_FOREACH(ol, &opt, op_next) {
443 if (strncmp(ol->op_name, "DEV_", 4) == 0)
445 sbuf_printf(sb, "options\t%s", ol->op_name);
446 if (ol->op_value != NULL) {
448 for (i = 0; i < strlen(ol->op_value); i++) {
449 if (ol->op_value[i] == '"')
450 sbuf_printf(sb, "\\%c",
453 sbuf_printf(sb, "%c",
456 sbuf_printf(sb, "%s", lend);
458 sbuf_printf(sb, "%s", lend);
462 * Mark this file as containing everything we need.
464 STAILQ_FOREACH(d, &dtab, d_next)
465 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
470 * Generate file from the configuration files.
473 configfile_filebased(struct sbuf *sb)
480 * Try to read all configuration files. Since those will be present as
481 * C string in the macro, we have to slash their ends then the line
484 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
485 cff = fopen(cf->cfg_path, "r");
487 warn("Couldn't open file %s", cf->cfg_path);
490 while ((i = getc(cff)) != EOF) {
492 sbuf_printf(sb, "\\n\\\n");
493 else if (i == '"' || i == '\'')
494 sbuf_printf(sb, "\\%c", i);
509 /* Add main configuration file to the list of files to be included */
511 p = path("config.c.new");
515 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
519 /* Is needed, can be used for backward compatibility. */
520 configfile_filebased(sb);
522 configfile_dynamic(sb);
526 * We print first part of the template, replace our tag with
527 * configuration files content and later continue writing our
530 p = strstr(kernconfstr, KERNCONFTAG);
532 errx(EXIT_FAILURE, "Something went terribly wrong!");
534 fprintf(fo, "%s", kernconfstr);
535 fprintf(fo, "%s", sbuf_data(sb));
536 p += strlen(KERNCONFTAG);
537 fprintf(fo, "%s", p);
540 moveifchanged(path("config.c.new"), path("config.c"));
546 * compare two files; rename if changed.
549 moveifchanged(const char *from_name, const char *to_name)
554 struct stat from_sb, to_sb;
559 if ((from_fd = open(from_name, O_RDONLY)) < 0)
560 err(EX_OSERR, "moveifchanged open(%s)", from_name);
562 if ((to_fd = open(to_name, O_RDONLY)) < 0)
565 if (!changed && fstat(from_fd, &from_sb) < 0)
566 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
568 if (!changed && fstat(to_fd, &to_sb) < 0)
569 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
571 if (!changed && from_sb.st_size != to_sb.st_size)
574 tsize = (size_t)from_sb.st_size;
577 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
579 err(EX_OSERR, "mmap %s", from_name);
580 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
582 err(EX_OSERR, "mmap %s", to_name);
584 changed = memcmp(p, q, tsize);
589 if (rename(from_name, to_name) < 0)
590 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
592 if (unlink(from_name) < 0)
593 err(EX_OSERR, "unlink(%s)", from_name);
598 cleanheaders(char *p)
602 struct file_list *fl;
607 remember("setdefs.h");
608 STAILQ_FOREACH(fl, &ftab, f_next)
612 * Scan the build directory and clean out stuff that looks like
613 * it might have been a leftover NFOO header, etc.
615 if ((dirp = opendir(p)) == NULL)
616 err(EX_OSERR, "opendir %s", p);
617 while ((dp = readdir(dirp)) != NULL) {
618 len = strlen(dp->d_name);
619 /* Skip non-headers */
620 if (len < 2 || dp->d_name[len - 2] != '.' ||
621 dp->d_name[len - 1] != 'h')
623 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
624 if (strchr(dp->d_name, '_') &&
625 strncmp(dp->d_name, "opt_", 4) != 0)
627 /* Check if it is a target file */
628 for (hl = htab; hl != NULL; hl = hl->h_next) {
629 if (eq(dp->d_name, hl->h_name)) {
635 printf("Removing stale header: %s\n", dp->d_name);
636 if (unlink(path(dp->d_name)) == -1)
637 warn("unlink %s", dp->d_name);
639 (void)closedir(dirp);
643 remember(const char *file)
648 if ((s = strrchr(file, '/')) != NULL)
653 if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
657 for (hl = htab; hl != NULL; hl = hl->h_next) {
658 if (eq(s, hl->h_name)) {
663 hl = calloc(1, sizeof(*hl));
665 err(EXIT_FAILURE, "calloc");
672 * This one is quick hack. Will be probably moved to elf(3) interface.
673 * It takes kernel configuration file name, passes it as an argument to
674 * elfdump -a, which output is parsed by some UNIX tools...
677 kernconfdump(const char *file)
681 int error, len, osz, r;
682 unsigned int i, off, size, t1, t2, align;
685 r = open(file, O_RDONLY);
687 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
688 error = fstat(r, &st);
690 err(EXIT_FAILURE, "fstat() failed");
691 if (S_ISDIR(st.st_mode))
692 errx(EXIT_FAILURE, "'%s' is a directory", file);
695 err(EXIT_FAILURE, "fdopen() failed");
699 err(EXIT_FAILURE, "Couldn't allocate memory");
700 /* ELF note section header. */
701 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
702 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
704 errx(EXIT_FAILURE, "asprintf() failed");
705 pp = popen(cmd, "r");
707 errx(EXIT_FAILURE, "popen() failed");
709 len = fread(o, osz, 1, pp);
711 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
714 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
715 "file. Either unsupported, or not compiled with "
716 "INCLUDE_CONFIG_FILE", file);
717 r = fseek(fp, off, SEEK_CUR);
719 err(EXIT_FAILURE, "fseek() failed");
720 for (i = 0; i < size; i++) {
725 assert(i == size - 1 &&
726 ("\\0 found in the middle of a file"));
735 badversion(int versreq)
737 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
738 fprintf(stderr, "config version = %d, ", CONFIGVERS);
739 fprintf(stderr, "version required = %d\n\n", versreq);
740 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
741 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
742 fprintf(stderr, "before trying this again.\n\n");
743 fprintf(stderr, "If running the new config fails check your config\n");
744 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
745 fprintf(stderr, "changes in config syntax, or option/device naming\n");
746 fprintf(stderr, "conventions\n\n");
757 ifp = open_makefile_template();
758 while (fgets(line, BUFSIZ, ifp) != 0) {
761 if (strncmp(line, "%VERSREQ=", 9) != 0)
763 versreq = atoi(line + 9);
764 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
765 versreq <= CONFIGVERS)