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)
116 while ((ch = getopt(argc, argv, "Cd:gmpVx:")) != -1)
125 if (*destdir == '\0')
126 strlcpy(destdir, optarg, sizeof(destdir));
128 errx(EXIT_FAILURE, "directory already set");
137 printf("%d\n", CONFIGVERS);
149 if (kernfile != NULL) {
150 kernconfdump(kernfile);
158 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
159 err(2, "%s", PREFIX);
160 if (freopen("DEFAULTS", "r", stdin) != NULL) {
164 if (freopen(PREFIX, "r", stdin) == NULL)
165 err(2, "%s", PREFIX);
168 if (*destdir != '\0') {
169 len = strlen(destdir);
170 while (len > 1 && destdir[len - 1] == '/')
171 destdir[--len] = '\0';
174 strlcpy(destdir, CDIR, sizeof(destdir));
175 strlcat(destdir, PREFIX, sizeof(destdir));
178 SLIST_INIT(&cputype);
182 STAILQ_INIT(&cfgfiles);
191 * Ensure that required elements (machine, cpu, ident) are present.
193 if (machinename == NULL) {
194 printf("Specify machine type, e.g. ``machine i386''\n");
198 printf("no ident line specified\n");
201 if (SLIST_EMPTY(&cputype)) {
202 printf("cpu type must be specified\n");
208 printf("%s\t%s\n",machinename,machinearch);
212 /* Make compile directory */
213 p = path((char *)NULL);
217 } else if (!S_ISDIR(buf.st_mode))
218 errx(EXIT_FAILURE, "%s isn't a directory", p);
220 configfile(); /* put config file into kernel*/
221 options(); /* make options .h files */
222 makefile(); /* build Makefile */
223 makeenv(); /* build env.c */
224 makehints(); /* build hints.c */
225 headers(); /* make a lot of .h files */
227 printf("Kernel build directory is %s\n", p);
228 printf("Don't forget to do ``make cleandepend && make depend''\n");
234 * determine the root of the kernel source tree
235 * and save that in srcdir.
244 if (realpath("../..", srcdir) == NULL)
245 err(EXIT_FAILURE, "Unable to find root of source tree");
246 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
247 (pwd = strdup(pwd)) != NULL) {
248 /* Remove the last two path components. */
249 for (i = 0; i < 2; i++) {
250 if ((p = strrchr(pwd, '/')) == NULL) {
256 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
257 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
258 strlcpy(srcdir, pwd, MAXPATHLEN);
267 fprintf(stderr, "usage: config [-CgmpV] [-d destdir] sysname\n");
268 fprintf(stderr, " config -x kernel\n");
274 * returns EOF on end of file
275 * NULL on end of line
276 * pointer to the word otherwise
281 static char line[80];
287 while ((ch = getc(fp)) != EOF)
288 if (ch != ' ' && ch != '\t')
291 return ((char *)EOF);
306 while ((ch = getc(fp)) != EOF) {
313 return ((char *)EOF);
314 (void) ungetc(ch, fp);
320 * like get_word but will accept something in double or single quotes
321 * (to allow embedded spaces).
324 get_quoted_word(FILE *fp)
326 static char line[256];
332 while ((ch = getc(fp)) != EOF)
333 if (ch != ' ' && ch != '\t')
336 return ((char *)EOF);
350 if (ch == '"' || ch == '\'') {
354 while ((ch = getc(fp)) != EOF) {
355 if (ch == quote && !escaped_nl)
357 if (ch == '\n' && !escaped_nl) {
359 printf("config: missing quote reading `%s'\n",
363 if (ch == '\\' && !escaped_nl) {
367 if (ch != quote && escaped_nl)
374 while ((ch = getc(fp)) != EOF) {
380 (void) ungetc(ch, fp);
384 return ((char *)EOF);
389 * prepend the path to a filename
392 path(const char *file)
397 asprintf(&cp, "%s/%s", destdir, file);
399 cp = strdup(destdir);
404 * Generate configuration file based on actual settings. With this mode, user
405 * will be able to obtain and build conifguration file with one command.
408 configfile_dynamic(struct sbuf *sb)
410 struct cputype *cput;
416 asprintf(&lend, "\\n\\\n");
417 assert(lend != NULL);
418 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
419 sbuf_printf(sb, "ident\t%s%s", ident, lend);
420 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
421 SLIST_FOREACH(cput, &cputype, cpu_next)
422 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
423 SLIST_FOREACH(ol, &mkopt, op_next)
424 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
426 SLIST_FOREACH(ol, &opt, op_next) {
427 if (strncmp(ol->op_name, "DEV_", 4) == 0)
429 sbuf_printf(sb, "options\t%s", ol->op_name);
430 if (ol->op_value != NULL) {
432 for (i = 0; i < strlen(ol->op_value); i++) {
433 if (ol->op_value[i] == '"')
434 sbuf_printf(sb, "\\%c",
437 sbuf_printf(sb, "%c",
440 sbuf_printf(sb, "%s", lend);
442 sbuf_printf(sb, "%s", lend);
446 * Mark this file as containing everything we need.
448 STAILQ_FOREACH(d, &dtab, d_next)
449 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
454 * Generate file from the configuration files.
457 configfile_filebased(struct sbuf *sb)
464 * Try to read all configuration files. Since those will be present as
465 * C string in the macro, we have to slash their ends then the line
468 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
469 cff = fopen(cf->cfg_path, "r");
471 warn("Couldn't open file %s", cf->cfg_path);
474 while ((i = getc(cff)) != EOF) {
476 sbuf_printf(sb, "\\n\\\n");
477 else if (i == '"' || i == '\'')
478 sbuf_printf(sb, "\\%c", i);
493 /* Add main configuration file to the list of files to be included */
495 p = path("config.c.new");
499 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
503 /* Is needed, can be used for backward compatibility. */
504 configfile_filebased(sb);
506 configfile_dynamic(sb);
510 * We print first part of the template, replace our tag with
511 * configuration files content and later continue writing our
514 p = strstr(kernconfstr, KERNCONFTAG);
516 errx(EXIT_FAILURE, "Something went terribly wrong!");
518 fprintf(fo, "%s", kernconfstr);
519 fprintf(fo, "%s", sbuf_data(sb));
520 p += strlen(KERNCONFTAG);
521 fprintf(fo, "%s", p);
524 moveifchanged(path("config.c.new"), path("config.c"));
530 * compare two files; rename if changed.
533 moveifchanged(const char *from_name, const char *to_name)
538 struct stat from_sb, to_sb;
543 if ((from_fd = open(from_name, O_RDONLY)) < 0)
544 err(EX_OSERR, "moveifchanged open(%s)", from_name);
546 if ((to_fd = open(to_name, O_RDONLY)) < 0)
549 if (!changed && fstat(from_fd, &from_sb) < 0)
550 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
552 if (!changed && fstat(to_fd, &to_sb) < 0)
553 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
555 if (!changed && from_sb.st_size != to_sb.st_size)
558 tsize = (size_t)from_sb.st_size;
561 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
563 err(EX_OSERR, "mmap %s", from_name);
564 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
566 err(EX_OSERR, "mmap %s", to_name);
568 changed = memcmp(p, q, tsize);
573 if (rename(from_name, to_name) < 0)
574 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
576 if (unlink(from_name) < 0)
577 err(EX_OSERR, "unlink(%s)", from_name);
582 cleanheaders(char *p)
586 struct file_list *fl;
591 remember("setdefs.h");
592 STAILQ_FOREACH(fl, &ftab, f_next)
596 * Scan the build directory and clean out stuff that looks like
597 * it might have been a leftover NFOO header, etc.
599 if ((dirp = opendir(p)) == NULL)
600 err(EX_OSERR, "opendir %s", p);
601 while ((dp = readdir(dirp)) != NULL) {
602 len = strlen(dp->d_name);
603 /* Skip non-headers */
604 if (len < 2 || dp->d_name[len - 2] != '.' ||
605 dp->d_name[len - 1] != 'h')
607 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
608 if (strchr(dp->d_name, '_') &&
609 strncmp(dp->d_name, "opt_", 4) != 0)
611 /* Check if it is a target file */
612 for (hl = htab; hl != NULL; hl = hl->h_next) {
613 if (eq(dp->d_name, hl->h_name)) {
619 printf("Removing stale header: %s\n", dp->d_name);
620 if (unlink(path(dp->d_name)) == -1)
621 warn("unlink %s", dp->d_name);
623 (void)closedir(dirp);
627 remember(const char *file)
632 if ((s = strrchr(file, '/')) != NULL)
637 if (index(s, '_') && strncmp(s, "opt_", 4) != 0) {
641 for (hl = htab; hl != NULL; hl = hl->h_next) {
642 if (eq(s, hl->h_name)) {
647 hl = calloc(1, sizeof(*hl));
649 err(EXIT_FAILURE, "calloc");
656 * This one is quick hack. Will be probably moved to elf(3) interface.
657 * It takes kernel configuration file name, passes it as an argument to
658 * elfdump -a, which output is parsed by some UNIX tools...
661 kernconfdump(const char *file)
665 int error, len, osz, r;
666 unsigned int i, off, size, t1, t2, align;
669 r = open(file, O_RDONLY);
671 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
672 error = fstat(r, &st);
674 err(EXIT_FAILURE, "fstat() failed");
675 if (S_ISDIR(st.st_mode))
676 errx(EXIT_FAILURE, "'%s' is a directory", file);
679 err(EXIT_FAILURE, "fdopen() failed");
683 err(EXIT_FAILURE, "Couldn't allocate memory");
684 /* ELF note section header. */
685 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
686 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
688 errx(EXIT_FAILURE, "asprintf() failed");
689 pp = popen(cmd, "r");
691 errx(EXIT_FAILURE, "popen() failed");
693 len = fread(o, osz, 1, pp);
695 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
698 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
699 "file. Either unsupported, or not compiled with "
700 "INCLUDE_CONFIG_FILE", file);
701 r = fseek(fp, off, SEEK_CUR);
703 err(EXIT_FAILURE, "fseek() failed");
704 for (i = 0; i < size; i++) {
709 * If '\0' is present in the middle of the configuration
710 * string, this means something very weird is happening.
711 * Make such case very visible. However, some architectures
712 * pad the length of the section with NULs to a multiple of
713 * sh_addralign, allow a NUL in that part of the section.
715 if (r == '\0' && (size - i) < align)
717 assert(r != '\0' && ("Char present in the configuration "
718 "string mustn't be equal to 0"));
725 badversion(int versreq)
727 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
728 fprintf(stderr, "config version = %d, ", CONFIGVERS);
729 fprintf(stderr, "version required = %d\n\n", versreq);
730 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
731 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
732 fprintf(stderr, "before trying this again.\n\n");
733 fprintf(stderr, "If running the new config fails check your config\n");
734 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
735 fprintf(stderr, "changes in config syntax, or option/device naming\n");
736 fprintf(stderr, "conventions\n\n");
747 ifp = open_makefile_template();
748 while (fgets(line, BUFSIZ, ifp) != 0) {
751 if (strncmp(line, "%VERSREQ=", 9) != 0)
753 versreq = atoi(line + 9);
754 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
755 versreq <= CONFIGVERS)