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 while ((ch = getc(fp)) != EOF) {
324 return ((char *)EOF);
325 (void) ungetc(ch, fp);
331 * like get_word but will accept something in double or single quotes
332 * (to allow embedded spaces).
335 get_quoted_word(FILE *fp)
337 static char line[256];
343 while ((ch = getc(fp)) != EOF)
344 if (ch != ' ' && ch != '\t')
347 return ((char *)EOF);
361 if (ch == '"' || ch == '\'') {
365 while ((ch = getc(fp)) != EOF) {
366 if (ch == quote && !escaped_nl)
368 if (ch == '\n' && !escaped_nl) {
370 printf("config: missing quote reading `%s'\n",
374 if (ch == '\\' && !escaped_nl) {
378 if (ch != quote && escaped_nl)
385 while ((ch = getc(fp)) != EOF) {
391 (void) ungetc(ch, fp);
395 return ((char *)EOF);
400 * prepend the path to a filename
403 path(const char *file)
408 asprintf(&cp, "%s/%s", destdir, file);
410 cp = strdup(destdir);
415 * Generate configuration file based on actual settings. With this mode, user
416 * will be able to obtain and build conifguration file with one command.
419 configfile_dynamic(struct sbuf *sb)
421 struct cputype *cput;
427 asprintf(&lend, "\\n\\\n");
428 assert(lend != NULL);
429 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
430 sbuf_printf(sb, "ident\t%s%s", ident, lend);
431 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
432 SLIST_FOREACH(cput, &cputype, cpu_next)
433 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
434 SLIST_FOREACH(ol, &mkopt, op_next)
435 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
437 SLIST_FOREACH(ol, &opt, op_next) {
438 if (strncmp(ol->op_name, "DEV_", 4) == 0)
440 sbuf_printf(sb, "options\t%s", ol->op_name);
441 if (ol->op_value != NULL) {
443 for (i = 0; i < strlen(ol->op_value); i++) {
444 if (ol->op_value[i] == '"')
445 sbuf_printf(sb, "\\%c",
448 sbuf_printf(sb, "%c",
451 sbuf_printf(sb, "%s", lend);
453 sbuf_printf(sb, "%s", lend);
457 * Mark this file as containing everything we need.
459 STAILQ_FOREACH(d, &dtab, d_next)
460 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
465 * Generate file from the configuration files.
468 configfile_filebased(struct sbuf *sb)
475 * Try to read all configuration files. Since those will be present as
476 * C string in the macro, we have to slash their ends then the line
479 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
480 cff = fopen(cf->cfg_path, "r");
482 warn("Couldn't open file %s", cf->cfg_path);
485 while ((i = getc(cff)) != EOF) {
487 sbuf_printf(sb, "\\n\\\n");
488 else if (i == '"' || i == '\'')
489 sbuf_printf(sb, "\\%c", i);
504 /* Add main configuration file to the list of files to be included */
506 p = path("config.c.new");
510 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
514 /* Is needed, can be used for backward compatibility. */
515 configfile_filebased(sb);
517 configfile_dynamic(sb);
521 * We print first part of the template, replace our tag with
522 * configuration files content and later continue writing our
525 p = strstr(kernconfstr, KERNCONFTAG);
527 errx(EXIT_FAILURE, "Something went terribly wrong!");
529 fprintf(fo, "%s", kernconfstr);
530 fprintf(fo, "%s", sbuf_data(sb));
531 p += strlen(KERNCONFTAG);
532 fprintf(fo, "%s", p);
535 moveifchanged(path("config.c.new"), path("config.c"));
541 * compare two files; rename if changed.
544 moveifchanged(const char *from_name, const char *to_name)
549 struct stat from_sb, to_sb;
554 if ((from_fd = open(from_name, O_RDONLY)) < 0)
555 err(EX_OSERR, "moveifchanged open(%s)", from_name);
557 if ((to_fd = open(to_name, O_RDONLY)) < 0)
560 if (!changed && fstat(from_fd, &from_sb) < 0)
561 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
563 if (!changed && fstat(to_fd, &to_sb) < 0)
564 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
566 if (!changed && from_sb.st_size != to_sb.st_size)
569 tsize = (size_t)from_sb.st_size;
572 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
574 err(EX_OSERR, "mmap %s", from_name);
575 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
577 err(EX_OSERR, "mmap %s", to_name);
579 changed = memcmp(p, q, tsize);
584 if (rename(from_name, to_name) < 0)
585 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
587 if (unlink(from_name) < 0)
588 err(EX_OSERR, "unlink(%s)", from_name);
593 cleanheaders(char *p)
597 struct file_list *fl;
602 remember("setdefs.h");
603 STAILQ_FOREACH(fl, &ftab, f_next)
607 * Scan the build directory and clean out stuff that looks like
608 * it might have been a leftover NFOO header, etc.
610 if ((dirp = opendir(p)) == NULL)
611 err(EX_OSERR, "opendir %s", p);
612 while ((dp = readdir(dirp)) != NULL) {
613 len = strlen(dp->d_name);
614 /* Skip non-headers */
615 if (len < 2 || dp->d_name[len - 2] != '.' ||
616 dp->d_name[len - 1] != 'h')
618 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
619 if (strchr(dp->d_name, '_') &&
620 strncmp(dp->d_name, "opt_", 4) != 0)
622 /* Check if it is a target file */
623 for (hl = htab; hl != NULL; hl = hl->h_next) {
624 if (eq(dp->d_name, hl->h_name)) {
630 printf("Removing stale header: %s\n", dp->d_name);
631 if (unlink(path(dp->d_name)) == -1)
632 warn("unlink %s", dp->d_name);
634 (void)closedir(dirp);
638 remember(const char *file)
643 if ((s = strrchr(file, '/')) != NULL)
648 if (strchr(s, '_') && strncmp(s, "opt_", 4) != 0) {
652 for (hl = htab; hl != NULL; hl = hl->h_next) {
653 if (eq(s, hl->h_name)) {
658 hl = calloc(1, sizeof(*hl));
660 err(EXIT_FAILURE, "calloc");
667 * This one is quick hack. Will be probably moved to elf(3) interface.
668 * It takes kernel configuration file name, passes it as an argument to
669 * elfdump -a, which output is parsed by some UNIX tools...
672 kernconfdump(const char *file)
676 int error, len, osz, r;
677 unsigned int i, off, size, t1, t2, align;
680 r = open(file, O_RDONLY);
682 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
683 error = fstat(r, &st);
685 err(EXIT_FAILURE, "fstat() failed");
686 if (S_ISDIR(st.st_mode))
687 errx(EXIT_FAILURE, "'%s' is a directory", file);
690 err(EXIT_FAILURE, "fdopen() failed");
694 err(EXIT_FAILURE, "Couldn't allocate memory");
695 /* ELF note section header. */
696 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
697 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
699 errx(EXIT_FAILURE, "asprintf() failed");
700 pp = popen(cmd, "r");
702 errx(EXIT_FAILURE, "popen() failed");
704 len = fread(o, osz, 1, pp);
706 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
709 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
710 "file. Either unsupported, or not compiled with "
711 "INCLUDE_CONFIG_FILE", file);
712 r = fseek(fp, off, SEEK_CUR);
714 err(EXIT_FAILURE, "fseek() failed");
715 for (i = 0; i < size; i++) {
720 assert(i == size - 1 &&
721 ("\\0 found in the middle of a file"));
730 badversion(int versreq)
732 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
733 fprintf(stderr, "config version = %d, ", CONFIGVERS);
734 fprintf(stderr, "version required = %d\n\n", versreq);
735 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
736 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
737 fprintf(stderr, "before trying this again.\n\n");
738 fprintf(stderr, "If running the new config fails check your config\n");
739 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
740 fprintf(stderr, "changes in config syntax, or option/device naming\n");
741 fprintf(stderr, "conventions\n\n");
752 ifp = open_makefile_template();
753 while (fgets(line, BUFSIZ, ifp) != 0) {
756 if (strncmp(line, "%VERSREQ=", 9) != 0)
758 versreq = atoi(line + 9);
759 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
760 versreq <= CONFIGVERS)