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);
97 struct hdr_list *h_next;
101 * Config builds a set of files for building a UNIX
102 * system given a description of the desired system.
105 main(int argc, char **argv)
112 struct includepath* ipath;
117 SLIST_INIT(&includepath);
118 while ((ch = getopt(argc, argv, "CI:d:gmpVx:")) != -1)
124 ipath = (struct includepath *) \
125 calloc(1, sizeof (struct includepath));
127 err(EXIT_FAILURE, "calloc");
128 ipath->path = optarg;
129 SLIST_INSERT_HEAD(&includepath, ipath, path_next);
135 if (*destdir == '\0')
136 strlcpy(destdir, optarg, sizeof(destdir));
138 errx(EXIT_FAILURE, "directory already set");
147 printf("%d\n", CONFIGVERS);
159 if (kernfile != NULL) {
160 kernconfdump(kernfile);
168 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
169 err(2, "%s", PREFIX);
170 if (freopen("DEFAULTS", "r", stdin) != NULL) {
174 if (freopen(PREFIX, "r", stdin) == NULL)
175 err(2, "%s", PREFIX);
178 if (*destdir != '\0') {
179 len = strlen(destdir);
180 while (len > 1 && destdir[len - 1] == '/')
181 destdir[--len] = '\0';
184 strlcpy(destdir, CDIR, sizeof(destdir));
185 strlcat(destdir, PREFIX, sizeof(destdir));
188 SLIST_INIT(&cputype);
192 STAILQ_INIT(&cfgfiles);
201 * Ensure that required elements (machine, cpu, ident) are present.
203 if (machinename == NULL) {
204 printf("Specify machine type, e.g. ``machine i386''\n");
208 printf("no ident line specified\n");
211 if (SLIST_EMPTY(&cputype)) {
212 printf("cpu type must be specified\n");
218 printf("%s\t%s\n",machinename,machinearch);
222 /* Make compile directory */
223 p = path((char *)NULL);
227 } else if (!S_ISDIR(buf.st_mode))
228 errx(EXIT_FAILURE, "%s isn't a directory", p);
230 configfile(); /* put config file into kernel*/
231 options(); /* make options .h files */
232 makefile(); /* build Makefile */
233 makeenv(); /* build env.c */
234 makehints(); /* build hints.c */
235 headers(); /* make a lot of .h files */
237 printf("Kernel build directory is %s\n", p);
238 printf("Don't forget to do ``make cleandepend && make depend''\n");
244 * determine the root of the kernel source tree
245 * and save that in srcdir.
254 if (realpath("../..", srcdir) == NULL)
255 err(EXIT_FAILURE, "Unable to find root of source tree");
256 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
257 (pwd = strdup(pwd)) != NULL) {
258 /* Remove the last two path components. */
259 for (i = 0; i < 2; i++) {
260 if ((p = strrchr(pwd, '/')) == NULL) {
266 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
267 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
268 strlcpy(srcdir, pwd, MAXPATHLEN);
277 fprintf(stderr, "usage: config [-CgmpV] [-d destdir] sysname\n");
278 fprintf(stderr, " config -x kernel\n");
284 * returns EOF on end of file
285 * NULL on end of line
286 * pointer to the word otherwise
291 static char line[80];
297 while ((ch = getc(fp)) != EOF)
298 if (ch != ' ' && ch != '\t')
301 return ((char *)EOF);
316 while ((ch = getc(fp)) != EOF) {
323 return ((char *)EOF);
324 (void) ungetc(ch, fp);
330 * like get_word but will accept something in double or single quotes
331 * (to allow embedded spaces).
334 get_quoted_word(FILE *fp)
336 static char line[256];
342 while ((ch = getc(fp)) != EOF)
343 if (ch != ' ' && ch != '\t')
346 return ((char *)EOF);
360 if (ch == '"' || ch == '\'') {
364 while ((ch = getc(fp)) != EOF) {
365 if (ch == quote && !escaped_nl)
367 if (ch == '\n' && !escaped_nl) {
369 printf("config: missing quote reading `%s'\n",
373 if (ch == '\\' && !escaped_nl) {
377 if (ch != quote && escaped_nl)
384 while ((ch = getc(fp)) != EOF) {
390 (void) ungetc(ch, fp);
394 return ((char *)EOF);
399 * prepend the path to a filename
402 path(const char *file)
407 asprintf(&cp, "%s/%s", destdir, file);
409 cp = strdup(destdir);
414 * Generate configuration file based on actual settings. With this mode, user
415 * will be able to obtain and build conifguration file with one command.
418 configfile_dynamic(struct sbuf *sb)
420 struct cputype *cput;
426 asprintf(&lend, "\\n\\\n");
427 assert(lend != NULL);
428 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
429 sbuf_printf(sb, "ident\t%s%s", ident, lend);
430 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
431 SLIST_FOREACH(cput, &cputype, cpu_next)
432 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
433 SLIST_FOREACH(ol, &mkopt, op_next)
434 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
436 SLIST_FOREACH(ol, &opt, op_next) {
437 if (strncmp(ol->op_name, "DEV_", 4) == 0)
439 sbuf_printf(sb, "options\t%s", ol->op_name);
440 if (ol->op_value != NULL) {
442 for (i = 0; i < strlen(ol->op_value); i++) {
443 if (ol->op_value[i] == '"')
444 sbuf_printf(sb, "\\%c",
447 sbuf_printf(sb, "%c",
450 sbuf_printf(sb, "%s", lend);
452 sbuf_printf(sb, "%s", lend);
456 * Mark this file as containing everything we need.
458 STAILQ_FOREACH(d, &dtab, d_next)
459 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
464 * Generate file from the configuration files.
467 configfile_filebased(struct sbuf *sb)
474 * Try to read all configuration files. Since those will be present as
475 * C string in the macro, we have to slash their ends then the line
478 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
479 cff = fopen(cf->cfg_path, "r");
481 warn("Couldn't open file %s", cf->cfg_path);
484 while ((i = getc(cff)) != EOF) {
486 sbuf_printf(sb, "\\n\\\n");
487 else if (i == '"' || i == '\'')
488 sbuf_printf(sb, "\\%c", i);
503 /* Add main configuration file to the list of files to be included */
505 p = path("config.c.new");
509 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
513 /* Is needed, can be used for backward compatibility. */
514 configfile_filebased(sb);
516 configfile_dynamic(sb);
520 * We print first part of the template, replace our tag with
521 * configuration files content and later continue writing our
524 p = strstr(kernconfstr, KERNCONFTAG);
526 errx(EXIT_FAILURE, "Something went terribly wrong!");
528 fprintf(fo, "%s", kernconfstr);
529 fprintf(fo, "%s", sbuf_data(sb));
530 p += strlen(KERNCONFTAG);
531 fprintf(fo, "%s", p);
534 moveifchanged(path("config.c.new"), path("config.c"));
540 * compare two files; rename if changed.
543 moveifchanged(const char *from_name, const char *to_name)
548 struct stat from_sb, to_sb;
553 if ((from_fd = open(from_name, O_RDONLY)) < 0)
554 err(EX_OSERR, "moveifchanged open(%s)", from_name);
556 if ((to_fd = open(to_name, O_RDONLY)) < 0)
559 if (!changed && fstat(from_fd, &from_sb) < 0)
560 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
562 if (!changed && fstat(to_fd, &to_sb) < 0)
563 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
565 if (!changed && from_sb.st_size != to_sb.st_size)
568 tsize = (size_t)from_sb.st_size;
571 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
573 err(EX_OSERR, "mmap %s", from_name);
574 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
576 err(EX_OSERR, "mmap %s", to_name);
578 changed = memcmp(p, q, tsize);
583 if (rename(from_name, to_name) < 0)
584 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
586 if (unlink(from_name) < 0)
587 err(EX_OSERR, "unlink(%s)", from_name);
592 cleanheaders(char *p)
596 struct file_list *fl;
601 remember("setdefs.h");
602 STAILQ_FOREACH(fl, &ftab, f_next)
606 * Scan the build directory and clean out stuff that looks like
607 * it might have been a leftover NFOO header, etc.
609 if ((dirp = opendir(p)) == NULL)
610 err(EX_OSERR, "opendir %s", p);
611 while ((dp = readdir(dirp)) != NULL) {
612 len = strlen(dp->d_name);
613 /* Skip non-headers */
614 if (len < 2 || dp->d_name[len - 2] != '.' ||
615 dp->d_name[len - 1] != 'h')
617 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
618 if (strchr(dp->d_name, '_') &&
619 strncmp(dp->d_name, "opt_", 4) != 0)
621 /* Check if it is a target file */
622 for (hl = htab; hl != NULL; hl = hl->h_next) {
623 if (eq(dp->d_name, hl->h_name)) {
629 printf("Removing stale header: %s\n", dp->d_name);
630 if (unlink(path(dp->d_name)) == -1)
631 warn("unlink %s", dp->d_name);
633 (void)closedir(dirp);
637 remember(const char *file)
642 if ((s = strrchr(file, '/')) != NULL)
647 if (index(s, '_') && strncmp(s, "opt_", 4) != 0) {
651 for (hl = htab; hl != NULL; hl = hl->h_next) {
652 if (eq(s, hl->h_name)) {
657 hl = calloc(1, sizeof(*hl));
659 err(EXIT_FAILURE, "calloc");
666 * This one is quick hack. Will be probably moved to elf(3) interface.
667 * It takes kernel configuration file name, passes it as an argument to
668 * elfdump -a, which output is parsed by some UNIX tools...
671 kernconfdump(const char *file)
675 int error, len, osz, r;
676 unsigned int i, off, size, t1, t2, align;
679 r = open(file, O_RDONLY);
681 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
682 error = fstat(r, &st);
684 err(EXIT_FAILURE, "fstat() failed");
685 if (S_ISDIR(st.st_mode))
686 errx(EXIT_FAILURE, "'%s' is a directory", file);
689 err(EXIT_FAILURE, "fdopen() failed");
693 err(EXIT_FAILURE, "Couldn't allocate memory");
694 /* ELF note section header. */
695 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
696 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
698 errx(EXIT_FAILURE, "asprintf() failed");
699 pp = popen(cmd, "r");
701 errx(EXIT_FAILURE, "popen() failed");
703 len = fread(o, osz, 1, pp);
705 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
708 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
709 "file. Either unsupported, or not compiled with "
710 "INCLUDE_CONFIG_FILE", file);
711 r = fseek(fp, off, SEEK_CUR);
713 err(EXIT_FAILURE, "fseek() failed");
714 for (i = 0; i < size; i++) {
719 * If '\0' is present in the middle of the configuration
720 * string, this means something very weird is happening.
721 * Make such case very visible. However, some architectures
722 * pad the length of the section with NULs to a multiple of
723 * sh_addralign, allow a NUL in that part of the section.
725 if (r == '\0' && (size - i) < align)
727 assert(r != '\0' && ("Char present in the configuration "
728 "string mustn't be equal to 0"));
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)