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 == '\'') {
353 while ((ch = getc(fp)) != EOF) {
358 printf("config: missing quote reading `%s'\n",
366 while ((ch = getc(fp)) != EOF) {
372 (void) ungetc(ch, fp);
376 return ((char *)EOF);
381 * prepend the path to a filename
384 path(const char *file)
389 asprintf(&cp, "%s/%s", destdir, file);
391 cp = strdup(destdir);
396 * Generate configuration file based on actual settings. With this mode, user
397 * will be able to obtain and build conifguration file with one command.
400 configfile_dynamic(struct sbuf *sb)
402 struct cputype *cput;
408 asprintf(&lend, "\\n\\\n");
409 assert(lend != NULL);
410 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
411 sbuf_printf(sb, "ident\t%s%s", ident, lend);
412 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
413 SLIST_FOREACH(cput, &cputype, cpu_next)
414 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
415 SLIST_FOREACH(ol, &mkopt, op_next)
416 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
418 SLIST_FOREACH(ol, &opt, op_next) {
419 if (strncmp(ol->op_name, "DEV_", 4) == 0)
421 sbuf_printf(sb, "options\t%s", ol->op_name);
422 if (ol->op_value != NULL) {
424 for (i = 0; i < strlen(ol->op_value); i++) {
425 if (ol->op_value[i] == '"')
426 sbuf_printf(sb, "\\%c",
429 sbuf_printf(sb, "%c",
432 sbuf_printf(sb, "%s", lend);
434 sbuf_printf(sb, "%s", lend);
438 * Mark this file as containing everything we need.
440 STAILQ_FOREACH(d, &dtab, d_next)
441 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
446 * Generate file from the configuration files.
449 configfile_filebased(struct sbuf *sb)
456 * Try to read all configuration files. Since those will be present as
457 * C string in the macro, we have to slash their ends then the line
460 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
461 cff = fopen(cf->cfg_path, "r");
463 warn("Couldn't open file %s", cf->cfg_path);
466 while ((i = getc(cff)) != EOF) {
468 sbuf_printf(sb, "\\n\\\n");
469 else if (i == '"' || i == '\'')
470 sbuf_printf(sb, "\\%c", i);
485 /* Add main configuration file to the list of files to be included */
487 p = path("config.c.new");
491 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
495 /* Is needed, can be used for backward compatibility. */
496 configfile_filebased(sb);
498 configfile_dynamic(sb);
502 * We print first part of the template, replace our tag with
503 * configuration files content and later continue writing our
506 p = strstr(kernconfstr, KERNCONFTAG);
508 errx(EXIT_FAILURE, "Something went terribly wrong!");
510 fprintf(fo, "%s", kernconfstr);
511 fprintf(fo, "%s", sbuf_data(sb));
512 p += strlen(KERNCONFTAG);
513 fprintf(fo, "%s", p);
516 moveifchanged(path("config.c.new"), path("config.c"));
522 * compare two files; rename if changed.
525 moveifchanged(const char *from_name, const char *to_name)
530 struct stat from_sb, to_sb;
535 if ((from_fd = open(from_name, O_RDONLY)) < 0)
536 err(EX_OSERR, "moveifchanged open(%s)", from_name);
538 if ((to_fd = open(to_name, O_RDONLY)) < 0)
541 if (!changed && fstat(from_fd, &from_sb) < 0)
542 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
544 if (!changed && fstat(to_fd, &to_sb) < 0)
545 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
547 if (!changed && from_sb.st_size != to_sb.st_size)
550 tsize = (size_t)from_sb.st_size;
553 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
555 err(EX_OSERR, "mmap %s", from_name);
556 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
558 err(EX_OSERR, "mmap %s", to_name);
560 changed = memcmp(p, q, tsize);
565 if (rename(from_name, to_name) < 0)
566 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
568 if (unlink(from_name) < 0)
569 err(EX_OSERR, "unlink(%s)", from_name);
574 cleanheaders(char *p)
578 struct file_list *fl;
583 remember("setdefs.h");
584 STAILQ_FOREACH(fl, &ftab, f_next)
588 * Scan the build directory and clean out stuff that looks like
589 * it might have been a leftover NFOO header, etc.
591 if ((dirp = opendir(p)) == NULL)
592 err(EX_OSERR, "opendir %s", p);
593 while ((dp = readdir(dirp)) != NULL) {
594 len = strlen(dp->d_name);
595 /* Skip non-headers */
596 if (len < 2 || dp->d_name[len - 2] != '.' ||
597 dp->d_name[len - 1] != 'h')
599 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
600 if (strchr(dp->d_name, '_') &&
601 strncmp(dp->d_name, "opt_", 4) != 0)
603 /* Check if it is a target file */
604 for (hl = htab; hl != NULL; hl = hl->h_next) {
605 if (eq(dp->d_name, hl->h_name)) {
611 printf("Removing stale header: %s\n", dp->d_name);
612 if (unlink(path(dp->d_name)) == -1)
613 warn("unlink %s", dp->d_name);
615 (void)closedir(dirp);
619 remember(const char *file)
624 if ((s = strrchr(file, '/')) != NULL)
629 if (index(s, '_') && strncmp(s, "opt_", 4) != 0) {
633 for (hl = htab; hl != NULL; hl = hl->h_next) {
634 if (eq(s, hl->h_name)) {
639 hl = calloc(1, sizeof(*hl));
641 err(EXIT_FAILURE, "calloc");
648 * This one is quick hack. Will be probably moved to elf(3) interface.
649 * It takes kernel configuration file name, passes it as an argument to
650 * elfdump -a, which output is parsed by some UNIX tools...
653 kernconfdump(const char *file)
657 int error, len, osz, r;
658 unsigned int i, off, size, t1, t2, align;
661 r = open(file, O_RDONLY);
663 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
664 error = fstat(r, &st);
666 err(EXIT_FAILURE, "fstat() failed");
667 if (S_ISDIR(st.st_mode))
668 errx(EXIT_FAILURE, "'%s' is a directory", file);
671 err(EXIT_FAILURE, "fdopen() failed");
675 err(EXIT_FAILURE, "Couldn't allocate memory");
676 /* ELF note section header. */
677 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
678 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
680 errx(EXIT_FAILURE, "asprintf() failed");
681 pp = popen(cmd, "r");
683 errx(EXIT_FAILURE, "popen() failed");
685 len = fread(o, osz, 1, pp);
687 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
690 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
691 "file. Either unsupported, or not compiled with "
692 "INCLUDE_CONFIG_FILE", file);
693 r = fseek(fp, off, SEEK_CUR);
695 err(EXIT_FAILURE, "fseek() failed");
696 for (i = 0; i < size; i++) {
701 * If '\0' is present in the middle of the configuration
702 * string, this means something very weird is happening.
703 * Make such case very visible. However, some architectures
704 * pad the length of the section with NULs to a multiple of
705 * sh_addralign, allow a NUL in that part of the section.
707 if (r == '\0' && (size - i) < align)
709 assert(r != '\0' && ("Char present in the configuration "
710 "string mustn't be equal to 0"));
717 badversion(int versreq)
719 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
720 fprintf(stderr, "config version = %d, ", CONFIGVERS);
721 fprintf(stderr, "version required = %d\n\n", versreq);
722 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
723 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
724 fprintf(stderr, "before trying this again.\n\n");
725 fprintf(stderr, "If running the new config fails check your config\n");
726 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
727 fprintf(stderr, "changes in config syntax, or option/device naming\n");
728 fprintf(stderr, "conventions\n\n");
739 ifp = open_makefile_template();
740 while (fgets(line, BUFSIZ, ifp) != 0) {
743 if (strncmp(line, "%VERSREQ=", 9) != 0)
745 versreq = atoi(line + 9);
746 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
747 versreq <= CONFIGVERS)