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];
115 while ((ch = getopt(argc, argv, "Cd:gpVx:")) != -1)
121 if (*destdir == '\0')
122 strlcpy(destdir, optarg, sizeof(destdir));
124 errx(EXIT_FAILURE, "directory already set");
133 printf("%d\n", CONFIGVERS);
145 if (kernfile != NULL) {
146 kernconfdump(kernfile);
154 if (stat(PREFIX, &buf) != 0 || !S_ISREG(buf.st_mode))
155 err(2, "%s", PREFIX);
156 if (freopen("DEFAULTS", "r", stdin) != NULL) {
160 if (freopen(PREFIX, "r", stdin) == NULL)
161 err(2, "%s", PREFIX);
164 if (*destdir != '\0') {
165 len = strlen(destdir);
166 while (len > 1 && destdir[len - 1] == '/')
167 destdir[--len] = '\0';
170 strlcpy(destdir, CDIR, sizeof(destdir));
171 strlcat(destdir, PREFIX, sizeof(destdir));
174 p = path((char *)NULL);
178 } else if (!S_ISDIR(buf.st_mode))
179 errx(EXIT_FAILURE, "%s isn't a directory", p);
181 SLIST_INIT(&cputype);
185 STAILQ_INIT(&cfgfiles);
194 * Ensure that required elements (machine, cpu, ident) are present.
196 if (machinename == NULL) {
197 printf("Specify machine type, e.g. ``machine i386''\n");
201 printf("no ident line specified\n");
204 if (SLIST_EMPTY(&cputype)) {
205 printf("cpu type must be specified\n");
211 * make symbolic links in compilation directory
212 * for "sys" (to make genassym.c work along with #include <sys/xxx>)
213 * and similarly for "machine".
216 (void)snprintf(xxx, sizeof(xxx), "../../include");
218 (void)snprintf(xxx, sizeof(xxx), "%s/%s/include",
219 srcdir, machinename);
220 (void) unlink(path("machine"));
221 (void) symlink(xxx, path("machine"));
222 if (strcmp(machinename, machinearch) != 0) {
224 * make symbolic links in compilation directory for
225 * machinearch, if it is different than machinename.
228 (void)snprintf(xxx, sizeof(xxx), "../../../%s/include",
231 (void)snprintf(xxx, sizeof(xxx), "%s/%s/include",
232 srcdir, machinearch);
233 (void) unlink(path(machinearch));
234 (void) symlink(xxx, path(machinearch));
236 configfile(); /* put config file into kernel*/
237 options(); /* make options .h files */
238 makefile(); /* build Makefile */
239 makeenv(); /* build env.c */
240 makehints(); /* build hints.c */
241 headers(); /* make a lot of .h files */
243 printf("Kernel build directory is %s\n", p);
244 printf("Don't forget to do ``make cleandepend && make depend''\n");
250 * determine the root of the kernel source tree
251 * and save that in srcdir.
260 if (realpath("../..", srcdir) == NULL)
261 err(EXIT_FAILURE, "Unable to find root of source tree");
262 if ((pwd = getenv("PWD")) != NULL && *pwd == '/' &&
263 (pwd = strdup(pwd)) != NULL) {
264 /* Remove the last two path components. */
265 for (i = 0; i < 2; i++) {
266 if ((p = strrchr(pwd, '/')) == NULL) {
272 if (stat(pwd, &lg) != -1 && stat(srcdir, &phy) != -1 &&
273 lg.st_dev == phy.st_dev && lg.st_ino == phy.st_ino)
274 strlcpy(srcdir, pwd, MAXPATHLEN);
283 fprintf(stderr, "usage: config [-CgpV] [-d destdir] sysname\n");
284 fprintf(stderr, " config -x kernel\n");
290 * returns EOF on end of file
291 * NULL on end of line
292 * pointer to the word otherwise
297 static char line[80];
303 while ((ch = getc(fp)) != EOF)
304 if (ch != ' ' && ch != '\t')
307 return ((char *)EOF);
322 while ((ch = getc(fp)) != EOF) {
329 return ((char *)EOF);
330 (void) ungetc(ch, fp);
336 * like get_word but will accept something in double or single quotes
337 * (to allow embedded spaces).
340 get_quoted_word(FILE *fp)
342 static char line[256];
348 while ((ch = getc(fp)) != EOF)
349 if (ch != ' ' && ch != '\t')
352 return ((char *)EOF);
366 if (ch == '"' || ch == '\'') {
369 while ((ch = getc(fp)) != EOF) {
374 printf("config: missing quote reading `%s'\n",
382 while ((ch = getc(fp)) != EOF) {
388 (void) ungetc(ch, fp);
392 return ((char *)EOF);
397 * prepend the path to a filename
400 path(const char *file)
405 asprintf(&cp, "%s/%s", destdir, file);
407 cp = strdup(destdir);
412 * Generate configuration file based on actual settings. With this mode, user
413 * will be able to obtain and build conifguration file with one command.
416 configfile_dynamic(struct sbuf *sb)
418 struct cputype *cput;
424 asprintf(&lend, "\\n\\\n");
425 assert(lend != NULL);
426 sbuf_printf(sb, "options\t%s%s", OPT_AUTOGEN, lend);
427 sbuf_printf(sb, "ident\t%s%s", ident, lend);
428 sbuf_printf(sb, "machine\t%s%s", machinename, lend);
429 SLIST_FOREACH(cput, &cputype, cpu_next)
430 sbuf_printf(sb, "cpu\t%s%s", cput->cpu_name, lend);
431 SLIST_FOREACH(ol, &mkopt, op_next)
432 sbuf_printf(sb, "makeoptions\t%s=%s%s", ol->op_name,
434 SLIST_FOREACH(ol, &opt, op_next) {
435 if (strncmp(ol->op_name, "DEV_", 4) == 0)
437 sbuf_printf(sb, "options\t%s", ol->op_name);
438 if (ol->op_value != NULL) {
440 for (i = 0; i < strlen(ol->op_value); i++) {
441 if (ol->op_value[i] == '"')
442 sbuf_printf(sb, "\\%c",
445 sbuf_printf(sb, "%c",
448 sbuf_printf(sb, "%s", lend);
450 sbuf_printf(sb, "%s", lend);
454 * Mark this file as containing everything we need.
456 STAILQ_FOREACH(d, &dtab, d_next)
457 sbuf_printf(sb, "device\t%s%s", d->d_name, lend);
462 * Generate file from the configuration files.
465 configfile_filebased(struct sbuf *sb)
472 * Try to read all configuration files. Since those will be present as
473 * C string in the macro, we have to slash their ends then the line
476 STAILQ_FOREACH(cf, &cfgfiles, cfg_next) {
477 cff = fopen(cf->cfg_path, "r");
479 warn("Couldn't open file %s", cf->cfg_path);
482 while ((i = getc(cff)) != EOF) {
484 sbuf_printf(sb, "\\n\\\n");
485 else if (i == '"' || i == '\'')
486 sbuf_printf(sb, "\\%c", i);
501 /* Add main configuration file to the list of files to be included */
503 p = path("config.c.new");
507 sb = sbuf_new(NULL, NULL, 2048, SBUF_AUTOEXTEND);
511 /* Is needed, can be used for backward compatibility. */
512 configfile_filebased(sb);
514 configfile_dynamic(sb);
518 * We print first part of the template, replace our tag with
519 * configuration files content and later continue writing our
522 p = strstr(kernconfstr, KERNCONFTAG);
524 errx(EXIT_FAILURE, "Something went terribly wrong!");
526 fprintf(fo, "%s", kernconfstr);
527 fprintf(fo, "%s", sbuf_data(sb));
528 p += strlen(KERNCONFTAG);
529 fprintf(fo, "%s", p);
532 moveifchanged(path("config.c.new"), path("config.c"));
538 * compare two files; rename if changed.
541 moveifchanged(const char *from_name, const char *to_name)
546 struct stat from_sb, to_sb;
551 if ((from_fd = open(from_name, O_RDONLY)) < 0)
552 err(EX_OSERR, "moveifchanged open(%s)", from_name);
554 if ((to_fd = open(to_name, O_RDONLY)) < 0)
557 if (!changed && fstat(from_fd, &from_sb) < 0)
558 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
560 if (!changed && fstat(to_fd, &to_sb) < 0)
561 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
563 if (!changed && from_sb.st_size != to_sb.st_size)
566 tsize = (size_t)from_sb.st_size;
569 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
571 err(EX_OSERR, "mmap %s", from_name);
572 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
574 err(EX_OSERR, "mmap %s", to_name);
576 changed = memcmp(p, q, tsize);
581 if (rename(from_name, to_name) < 0)
582 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
584 if (unlink(from_name) < 0)
585 err(EX_OSERR, "unlink(%s)", from_name);
590 cleanheaders(char *p)
594 struct file_list *fl;
599 remember("setdefs.h");
600 STAILQ_FOREACH(fl, &ftab, f_next)
604 * Scan the build directory and clean out stuff that looks like
605 * it might have been a leftover NFOO header, etc.
607 if ((dirp = opendir(p)) == NULL)
608 err(EX_OSERR, "opendir %s", p);
609 while ((dp = readdir(dirp)) != NULL) {
610 i = dp->d_namlen - 2;
611 /* Skip non-headers */
612 if (dp->d_name[i] != '.' || dp->d_name[i + 1] != 'h')
614 /* Skip special stuff, eg: bus_if.h, but check opt_*.h */
615 if (index(dp->d_name, '_') &&
616 strncmp(dp->d_name, "opt_", 4) != 0)
618 /* Check if it is a target file */
619 for (hl = htab; hl != NULL; hl = hl->h_next) {
620 if (eq(dp->d_name, hl->h_name)) {
626 printf("Removing stale header: %s\n", dp->d_name);
627 if (unlink(path(dp->d_name)) == -1)
628 warn("unlink %s", dp->d_name);
630 (void)closedir(dirp);
634 remember(const char *file)
639 if ((s = strrchr(file, '/')) != NULL)
644 if (index(s, '_') && strncmp(s, "opt_", 4) != 0) {
648 for (hl = htab; hl != NULL; hl = hl->h_next) {
649 if (eq(s, hl->h_name)) {
654 hl = calloc(1, sizeof(*hl));
656 err(EXIT_FAILURE, "calloc");
663 * This one is quick hack. Will be probably moved to elf(3) interface.
664 * It takes kernel configuration file name, passes it as an argument to
665 * elfdump -a, which output is parsed by some UNIX tools...
668 kernconfdump(const char *file)
672 int error, len, osz, r;
673 unsigned int i, off, size, t1, t2, align;
676 r = open(file, O_RDONLY);
678 err(EXIT_FAILURE, "Couldn't open file '%s'", file);
679 error = fstat(r, &st);
681 err(EXIT_FAILURE, "fstat() failed");
682 if (S_ISDIR(st.st_mode))
683 errx(EXIT_FAILURE, "'%s' is a directory", file);
686 err(EXIT_FAILURE, "fdopen() failed");
690 err(EXIT_FAILURE, "Couldn't allocate memory");
691 /* ELF note section header. */
692 asprintf(&cmd, "/usr/bin/elfdump -c %s | grep -A 8 kern_conf"
693 "| tail -5 | cut -d ' ' -f 2 | paste - - - - -", file);
695 errx(EXIT_FAILURE, "asprintf() failed");
696 pp = popen(cmd, "r");
698 errx(EXIT_FAILURE, "popen() failed");
700 len = fread(o, osz, 1, pp);
702 r = sscanf(o, "%d%d%d%d%d", &off, &size, &t1, &t2, &align);
705 errx(EXIT_FAILURE, "File %s doesn't contain configuration "
706 "file. Either unsupported, or not compiled with "
707 "INCLUDE_CONFIG_FILE", file);
708 r = fseek(fp, off, SEEK_CUR);
710 err(EXIT_FAILURE, "fseek() failed");
711 for (i = 0; i < size; i++) {
716 * If '\0' is present in the middle of the configuration
717 * string, this means something very weird is happening.
718 * Make such case very visible. However, some architectures
719 * pad the length of the section with NULs to a multiple of
720 * sh_addralign, allow a NUL in that part of the section.
722 if (r == '\0' && (size - i) < align)
724 assert(r != '\0' && ("Char present in the configuration "
725 "string mustn't be equal to 0"));
732 badversion(int versreq)
734 fprintf(stderr, "ERROR: version of config(8) does not match kernel!\n");
735 fprintf(stderr, "config version = %d, ", CONFIGVERS);
736 fprintf(stderr, "version required = %d\n\n", versreq);
737 fprintf(stderr, "Make sure that /usr/src/usr.sbin/config is in sync\n");
738 fprintf(stderr, "with your /usr/src/sys and install a new config binary\n");
739 fprintf(stderr, "before trying this again.\n\n");
740 fprintf(stderr, "If running the new config fails check your config\n");
741 fprintf(stderr, "file against the GENERIC or LINT config files for\n");
742 fprintf(stderr, "changes in config syntax, or option/device naming\n");
743 fprintf(stderr, "conventions\n\n");
754 ifp = open_makefile_template();
755 while (fgets(line, BUFSIZ, ifp) != 0) {
758 if (strncmp(line, "%VERSREQ=", 9) != 0)
760 versreq = atoi(line + 9);
761 if (MAJOR_VERS(versreq) == MAJOR_VERS(CONFIGVERS) &&
762 versreq <= CONFIGVERS)