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)
111 char xxx[MAXPATHLEN];
117 while ((ch = getopt(argc, argv, "Cd:gmpVx:")) != -1)
126 if (*destdir == '\0')
127 strlcpy(destdir, optarg, sizeof(destdir));
129 errx(EXIT_FAILURE, "directory already set");
138 printf("%d\n", CONFIGVERS);
150 if (kernfile != NULL) {
151 kernconfdump(kernfile);
159 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
160 err(2, "%s", PREFIX);
161 if (freopen("DEFAULTS", "r", stdin) != NULL) {
165 if (freopen(PREFIX, "r", stdin) == NULL)
166 err(2, "%s", PREFIX);
169 if (*destdir != '\0') {
170 len = strlen(destdir);
171 while (len > 1 && destdir[len - 1] == '/')
172 destdir[--len] = '\0';
175 strlcpy(destdir, CDIR, sizeof(destdir));
176 strlcat(destdir, PREFIX, sizeof(destdir));
179 SLIST_INIT(&cputype);
183 STAILQ_INIT(&cfgfiles);
192 * Ensure that required elements (machine, cpu, ident) are present.
194 if (machinename == NULL) {
195 printf("Specify machine type, e.g. ``machine i386''\n");
199 printf("no ident line specified\n");
202 if (SLIST_EMPTY(&cputype)) {
203 printf("cpu type must be specified\n");
209 printf("%s\t%s\n",machinename,machinearch);
213 /* Make compile directory */
214 p = path((char *)NULL);
218 } else if (!S_ISDIR(buf.st_mode))
219 errx(EXIT_FAILURE, "%s isn't a directory", p);
222 * make symbolic links in compilation directory
223 * for "sys" (to make genassym.c work along with #include <sys/xxx>)
224 * and similarly for "machine".
227 (void)snprintf(xxx, sizeof(xxx), "../../include");
229 (void)snprintf(xxx, sizeof(xxx), "%s/%s/include",
230 srcdir, machinename);
231 (void) unlink(path("machine"));
232 (void) symlink(xxx, path("machine"));
233 if (strcmp(machinename, machinearch) != 0) {
235 * make symbolic links in compilation directory for
236 * machinearch, if it is different than machinename.
239 (void)snprintf(xxx, sizeof(xxx), "../../../%s/include",
242 (void)snprintf(xxx, sizeof(xxx), "%s/%s/include",
243 srcdir, machinearch);
244 (void) unlink(path(machinearch));
245 (void) symlink(xxx, path(machinearch));
247 configfile(); /* put config file into kernel*/
248 options(); /* make options .h files */
249 makefile(); /* build Makefile */
250 makeenv(); /* build env.c */
251 makehints(); /* build hints.c */
252 headers(); /* make a lot of .h files */
254 printf("Kernel build directory is %s\n", p);
255 printf("Don't forget to do ``make cleandepend && make depend''\n");
261 * determine the root of the kernel source tree
262 * and save that in srcdir.
271 if (realpath("../..", srcdir) == NULL)
272 err(EXIT_FAILURE, "Unable to find root of source tree");
273 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
274 (pwd = strdup(pwd)) != NULL) {
275 /* Remove the last two path components. */
276 for (i = 0; i < 2; i++) {
277 if ((p = strrchr(pwd, '/')) == NULL) {
283 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
284 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
285 strlcpy(srcdir, pwd, MAXPATHLEN);
294 fprintf(stderr, "usage: config [-CgmpV] [-d destdir] sysname\n");
295 fprintf(stderr, " config -x kernel\n");
301 * returns EOF on end of file
302 * NULL on end of line
303 * pointer to the word otherwise
308 static char line[80];
314 while ((ch = getc(fp)) != EOF)
315 if (ch != ' ' && ch != '\t')
318 return ((char *)EOF);
333 while ((ch = getc(fp)) != EOF) {
340 return ((char *)EOF);
341 (void) ungetc(ch, fp);
347 * like get_word but will accept something in double or single quotes
348 * (to allow embedded spaces).
351 get_quoted_word(FILE *fp)
353 static char line[256];
359 while ((ch = getc(fp)) != EOF)
360 if (ch != ' ' && ch != '\t')
363 return ((char *)EOF);
377 if (ch == '"' || ch == '\'') {
381 while ((ch = getc(fp)) != EOF) {
382 if (ch == quote && !escaped_nl)
384 if (ch == '\n' && !escaped_nl) {
386 printf("config: missing quote reading `%s'\n",
390 if (ch == '\\' && !escaped_nl) {
394 if (ch != quote && escaped_nl)
401 while ((ch = getc(fp)) != EOF) {
407 (void) ungetc(ch, fp);
411 return ((char *)EOF);
416 * prepend the path to a filename
419 path(const char *file)
424 asprintf(&cp, "%s/%s", destdir, file);
426 cp = strdup(destdir);
431 * Generate configuration file based on actual settings. With this mode, user
432 * will be able to obtain and build conifguration file with one command.
435 configfile_dynamic(struct sbuf *sb)
437 struct cputype *cput;
443 asprintf(&lend, "\\n\\\n");
444 assert(lend != NULL);
445 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
446 sbuf_printf(sb, "ident\t%s%s", ident, lend);
447 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
448 SLIST_FOREACH(cput, &cputype, cpu_next)
449 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
450 SLIST_FOREACH(ol, &mkopt, op_next)
451 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
453 SLIST_FOREACH(ol, &opt, op_next) {
454 if (strncmp(ol->op_name, "DEV_", 4) == 0)
456 sbuf_printf(sb, "options\t%s", ol->op_name);
457 if (ol->op_value != NULL) {
459 for (i = 0; i < strlen(ol->op_value); i++) {
460 if (ol->op_value[i] == '"')
461 sbuf_printf(sb, "\\%c",
464 sbuf_printf(sb, "%c",
467 sbuf_printf(sb, "%s", lend);
469 sbuf_printf(sb, "%s", lend);
473 * Mark this file as containing everything we need.
475 STAILQ_FOREACH(d, &dtab, d_next)
476 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
481 * Generate file from the configuration files.
484 configfile_filebased(struct sbuf *sb)
491 * Try to read all configuration files. Since those will be present as
492 * C string in the macro, we have to slash their ends then the line
495 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
496 cff = fopen(cf->cfg_path, "r");
498 warn("Couldn't open file %s", cf->cfg_path);
501 while ((i = getc(cff)) != EOF) {
503 sbuf_printf(sb, "\\n\\\n");
504 else if (i == '"' || i == '\'')
505 sbuf_printf(sb, "\\%c", i);
520 /* Add main configuration file to the list of files to be included */
522 p = path("config.c.new");
526 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
530 /* Is needed, can be used for backward compatibility. */
531 configfile_filebased(sb);
533 configfile_dynamic(sb);
537 * We print first part of the template, replace our tag with
538 * configuration files content and later continue writing our
541 p = strstr(kernconfstr, KERNCONFTAG);
543 errx(EXIT_FAILURE, "Something went terribly wrong!");
545 fprintf(fo, "%s", kernconfstr);
546 fprintf(fo, "%s", sbuf_data(sb));
547 p += strlen(KERNCONFTAG);
548 fprintf(fo, "%s", p);
551 moveifchanged(path("config.c.new"), path("config.c"));
557 * compare two files; rename if changed.
560 moveifchanged(const char *from_name, const char *to_name)
565 struct stat from_sb, to_sb;
570 if ((from_fd = open(from_name, O_RDONLY)) < 0)
571 err(EX_OSERR, "moveifchanged open(%s)", from_name);
573 if ((to_fd = open(to_name, O_RDONLY)) < 0)
576 if (!changed && fstat(from_fd, &from_sb) < 0)
577 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
579 if (!changed && fstat(to_fd, &to_sb) < 0)
580 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
582 if (!changed && from_sb.st_size != to_sb.st_size)
585 tsize = (size_t)from_sb.st_size;
588 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
590 err(EX_OSERR, "mmap %s", from_name);
591 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
593 err(EX_OSERR, "mmap %s", to_name);
595 changed = memcmp(p, q, tsize);
600 if (rename(from_name, to_name) < 0)
601 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
603 if (unlink(from_name) < 0)
604 err(EX_OSERR, "unlink(%s)", from_name);
609 cleanheaders(char *p)
613 struct file_list *fl;
618 remember("setdefs.h");
619 STAILQ_FOREACH(fl, &ftab, f_next)
623 * Scan the build directory and clean out stuff that looks like
624 * it might have been a leftover NFOO header, etc.
626 if ((dirp = opendir(p)) == NULL)
627 err(EX_OSERR, "opendir %s", p);
628 while ((dp = readdir(dirp)) != NULL) {
629 len = strlen(dp->d_name);
630 /* Skip non-headers */
631 if (len < 2 || dp->d_name[len - 2] != '.' ||
632 dp->d_name[len - 1] != 'h')
634 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
635 if (strchr(dp->d_name, '_') &&
636 strncmp(dp->d_name, "opt_", 4) != 0)
638 /* Check if it is a target file */
639 for (hl = htab; hl != NULL; hl = hl->h_next) {
640 if (eq(dp->d_name, hl->h_name)) {
646 printf("Removing stale header: %s\n", dp->d_name);
647 if (unlink(path(dp->d_name)) == -1)
648 warn("unlink %s", dp->d_name);
650 (void)closedir(dirp);
654 remember(const char *file)
659 if ((s = strrchr(file, '/')) != NULL)
664 if (index(s, '_') && strncmp(s, "opt_", 4) != 0) {
668 for (hl = htab; hl != NULL; hl = hl->h_next) {
669 if (eq(s, hl->h_name)) {
674 hl = calloc(1, sizeof(*hl));
676 err(EXIT_FAILURE, "calloc");
683 * This one is quick hack. Will be probably moved to elf(3) interface.
684 * It takes kernel configuration file name, passes it as an argument to
685 * elfdump -a, which output is parsed by some UNIX tools...
688 kernconfdump(const char *file)
692 int error, len, osz, r;
693 unsigned int i, off, size, t1, t2, align;
696 r = open(file, O_RDONLY);
698 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
699 error = fstat(r, &st);
701 err(EXIT_FAILURE, "fstat() failed");
702 if (S_ISDIR(st.st_mode))
703 errx(EXIT_FAILURE, "'%s' is a directory", file);
706 err(EXIT_FAILURE, "fdopen() failed");
710 err(EXIT_FAILURE, "Couldn't allocate memory");
711 /* ELF note section header. */
712 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
713 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
715 errx(EXIT_FAILURE, "asprintf() failed");
716 pp = popen(cmd, "r");
718 errx(EXIT_FAILURE, "popen() failed");
720 len = fread(o, osz, 1, pp);
722 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
725 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
726 "file. Either unsupported, or not compiled with "
727 "INCLUDE_CONFIG_FILE", file);
728 r = fseek(fp, off, SEEK_CUR);
730 err(EXIT_FAILURE, "fseek() failed");
731 for (i = 0; i < size; i++) {
736 * If '\0' is present in the middle of the configuration
737 * string, this means something very weird is happening.
738 * Make such case very visible. However, some architectures
739 * pad the length of the section with NULs to a multiple of
740 * sh_addralign, allow a NUL in that part of the section.
742 if (r == '\0' && (size - i) < align)
744 assert(r != '\0' && ("Char present in the configuration "
745 "string mustn't be equal to 0"));
752 badversion(int versreq)
754 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
755 fprintf(stderr, "config version = %d, ", CONFIGVERS);
756 fprintf(stderr, "version required = %d\n\n", versreq);
757 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
758 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
759 fprintf(stderr, "before trying this again.\n\n");
760 fprintf(stderr, "If running the new config fails check your config\n");
761 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
762 fprintf(stderr, "changes in config syntax, or option/device naming\n");
763 fprintf(stderr, "conventions\n\n");
774 ifp = open_makefile_template();
775 while (fgets(line, BUFSIZ, ifp) != 0) {
778 if (strncmp(line, "%VERSREQ=", 9) != 0)
780 versreq = atoi(line + 9);
781 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
782 versreq <= CONFIGVERS)