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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)main.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.sbin/config/main.c,v 1.37.2.3 2001/06/13 00:25:53 cg Exp $
36 * $DragonFly: src/usr.sbin/config/main.c,v 1.20 2006/11/19 07:49:34 sephe Exp $
39 #include <sys/types.h>
43 #include <sys/param.h>
61 #define CDIR "../compile/"
64 char destdir[MAXPATHLEN];
65 char srcdir[MAXPATHLEN];
67 static int no_config_clobber = TRUE;
71 static void configfile(void);
72 static void get_srcdir(void);
73 static void usage(void);
76 * Config builds a set of files for building a UNIX
77 * system given a description of the desired system.
80 main(int argc, char *argv[])
86 char linksrc[64], linkdest[MAXPATHLEN];
87 static const char *emus[] = { "linux" };
89 while ((ch = getopt(argc, argv, "d:gprn")) != -1)
93 strlcpy(destdir, optarg, sizeof(destdir));
95 errx(2, "directory already set");
104 /* no_config_clobber is now true by default, no-op */
106 "*** Using obsolete config option '-n' ***\n");
109 no_config_clobber = FALSE;
121 if (freopen(PREFIX = *argv, "r", stdin) == NULL)
122 err(2, "%s", PREFIX);
124 if (*destdir != '\0') {
125 len = strlen(destdir);
126 while (len > 1 && destdir[len - 1] == '/')
127 destdir[--len] = '\0';
130 strlcpy(destdir, CDIR, sizeof(destdir));
131 strlcat(destdir, PREFIX, sizeof(destdir));
134 p = path((char *)NULL);
139 else if ((buf.st_mode & S_IFMT) != S_IFDIR) {
140 errx(2, "%s isn't a directory", p);
142 else if (!no_config_clobber) {
143 char tmp[strlen(p) + 8];
145 fprintf(stderr, "Removing old directory %s: ", p);
147 snprintf(tmp, sizeof(tmp), "rm -rf %s", p);
149 fprintf(stderr, "Failed!\n");
152 fprintf(stderr, "Done.\n");
160 if (machinename == NULL) {
161 printf("Specify platform architecture, e.g. 'machine pc32'\n");
164 if (machinearchname == NULL) {
165 printf("Specify cpu architecture, e.g. 'machine_arch i386'\n");
171 * "machine" points into machine/<MACHINE>/include
174 snprintf(linkdest, sizeof(linkdest), "../../machine/%s/include",
177 snprintf(linkdest, sizeof(linkdest), "%s/machine/%s/include",
178 srcdir, machinename);
179 symlink(linkdest, path("machine"));
182 * "machine_base" points into machine/<MACHINE>
185 snprintf(linkdest, sizeof(linkdest), "../../machine/%s",
188 snprintf(linkdest, sizeof(linkdest), "%s/machine/%s",
189 srcdir, machinename);
190 symlink(linkdest, path("machine_base"));
193 * "cpu" points to cpu/<MACHINE_ARCH>/include
196 snprintf(linkdest, sizeof(linkdest),
197 "../../cpu/%s/include", machinearchname);
199 snprintf(linkdest, sizeof(linkdest),
200 "%s/cpu/%s/include", srcdir, machinearchname);
201 symlink(linkdest, path("cpu"));
204 * "cpu_base" points to cpu/<MACHINE_ARCH>
207 snprintf(linkdest, sizeof(linkdest), "../../cpu/%s",
210 snprintf(linkdest, sizeof(linkdest), "%s/cpu/%s",
211 srcdir, machinearchname);
212 symlink(linkdest, path("cpu_base"));
215 * XXX check directory structure for architecture subdirectories and
216 * create the symlinks automatically XXX
219 snprintf(linkdest, sizeof(linkdest),
220 "../../../../../net/i4b/include/%s",
223 snprintf(linkdest, sizeof(linkdest), "%s/net/i4b/include/%s",
224 srcdir, machinearchname);
225 mkdir(path("net"), 0755);
226 mkdir(path("net/i4b"), 0755);
227 mkdir(path("net/i4b/include"), 0755);
228 symlink(linkdest, path("net/i4b/include/machine"));
230 for (i = 0; i < sizeof(emus) / sizeof(emus[0]); ++i) {
232 snprintf(linkdest, sizeof(linkdest),
233 "../../emulation/%s/%s",
234 emus[i], machinearchname);
236 snprintf(linkdest, sizeof(linkdest),
237 "%s/emulation/%s/%s",
238 srcdir, emus[i], machinearchname);
240 snprintf(linksrc, sizeof(linksrc), "arch_%s", emus[i]);
241 symlink(linkdest, path(linksrc));
244 options(); /* make options .h files */
245 makefile(); /* build Makefile */
246 headers(); /* make a lot of .h files */
247 configfile(); /* put config file into kernel*/
248 printf("Kernel build directory is %s\n", p);
254 * determine the root of the kernel source tree
255 * and save that in srcdir.
261 if (realpath("..", srcdir) == NULL)
262 errx(2, "Unable to find root of source tree");
269 fprintf(stderr, "usage: config [-gpr] [-d destdir] sysname\n");
275 * returns EOF on end of file
276 * NULL on end of line
277 * pointer to the word otherwise
282 static char line[80];
288 while ((ch = getc(fp)) != EOF)
289 if (ch != ' ' && ch != '\t')
307 while ((ch = getc(fp)) != EOF) {
321 * like get_word but will accept something in double or single quotes
322 * (to allow embedded spaces).
325 get_quoted_word(FILE *fp)
327 static char line[256];
333 while ((ch = getc(fp)) != EOF)
334 if (ch != ' ' && ch != '\t')
351 if (ch == '"' || ch == '\'') {
354 while ((ch = getc(fp)) != EOF) {
359 printf("config: missing quote reading `%s'\n",
367 while ((ch = getc(fp)) != EOF) {
382 * prepend the path to a filename
385 path(const char *file)
389 cp = malloc((size_t)(strlen(destdir) + (file ? strlen(file) : 0) + 2));
405 fi = fopen(PREFIX, "r");
407 err(2, "%s", PREFIX);
408 fo = fopen(p = path("config.c.new"), "w");
411 fprintf(fo, "#include \"opt_config.h\"\n");
412 fprintf(fo, "#ifdef INCLUDE_CONFIG_FILE \n");
413 fprintf(fo, "static const char config[] = \"\\\n");
414 fprintf(fo, "START CONFIG FILE %s\\n\\\n___", PREFIX);
415 while (EOF != (i = getc(fi))) {
417 fprintf(fo, "\\n\\\n___");
418 } else if (i == '\"') {
420 } else if (i == '\\') {
426 fprintf(fo, "\\n\\\nEND CONFIG FILE %s\\n\\\n", PREFIX);
427 fprintf(fo, "\";\n");
428 fprintf(fo, "\n#endif /* INCLUDE_CONFIG_FILE */\n");
431 moveifchanged(path("config.c.new"), path("config.c"));
436 * compare two files; rename if changed.
439 moveifchanged(const char *from_name, const char *to_name)
444 struct stat from_sb, to_sb;
449 if ((from_fd = open(from_name, O_RDONLY)) < 0)
450 err(EX_OSERR, "moveifchanged open(%s)", from_name);
452 if ((to_fd = open(to_name, O_RDONLY)) < 0)
455 if (!changed && fstat(from_fd, &from_sb) < 0)
456 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
458 if (!changed && fstat(to_fd, &to_sb) < 0)
459 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
461 if (!changed && from_sb.st_size != to_sb.st_size)
464 tsize = (size_t)from_sb.st_size;
467 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
469 #define MAP_FAILED ((caddr_t)-1)
472 err(EX_OSERR, "mmap %s", from_name);
473 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
475 err(EX_OSERR, "mmap %s", to_name);
477 changed = memcmp(p, q, tsize);
482 if (rename(from_name, to_name) < 0)
483 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
485 if (unlink(from_name) < 0)
486 err(EX_OSERR, "unlink(%s)", from_name);