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. 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
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)main.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.sbin/config/main.c,v 1.37.2.3 2001/06/13 00:25:53 cg Exp $
34 #include <sys/types.h>
38 #include <sys/param.h>
56 #define CDIR "../compile/"
59 char destdir[MAXPATHLEN];
60 char srcdir[MAXPATHLEN - 32];
62 static int no_config_clobber = TRUE;
65 extern int yyparse(void);
66 static void configfile(void);
67 static void get_srcdir(void);
68 static void usage(void);
71 * Config builds a set of files for building a UNIX
72 * system given a description of the desired system.
75 main(int argc, char *argv[])
80 char linkdest[MAXPATHLEN];
84 static const char *emus[] = { "linux" };
87 while ((ch = getopt(argc, argv, "d:gr")) != -1)
91 strlcpy(destdir, optarg, sizeof(destdir));
93 errx(2, "directory already set");
99 no_config_clobber = FALSE;
111 if (freopen(PREFIX = *argv, "r", stdin) == NULL)
112 err(2, "%s", PREFIX);
114 if (*destdir != '\0') {
115 len = strlen(destdir);
116 while (len > 1 && destdir[len - 1] == '/')
117 destdir[--len] = '\0';
120 strlcpy(destdir, CDIR, sizeof(destdir));
121 strlcat(destdir, PREFIX, sizeof(destdir));
129 else if ((buf.st_mode & S_IFMT) != S_IFDIR) {
130 errx(2, "%s isn't a directory", p);
132 else if (!no_config_clobber) {
133 char tmp[strlen(p) + 8];
135 fprintf(stderr, "Removing old directory %s: ", p);
137 snprintf(tmp, sizeof(tmp), "rm -rf %s", p);
139 fprintf(stderr, "Failed!\n");
142 fprintf(stderr, "Done.\n");
150 if (platformname == NULL) {
151 printf("Specify platform architecture, e.g. 'platform pc64'\n");
154 if (machinename == NULL) {
155 printf("Specify machine architecture, e.g. 'machine x86_64'\n");
158 if (machinearchname == NULL) {
159 printf("Specify cpu architecture, e.g. 'machine_arch x86_64'\n");
165 * "machine" points into platform/<PLATFORM>/include
168 snprintf(linkdest, sizeof(linkdest), "../../platform/%s/include",
171 snprintf(linkdest, sizeof(linkdest), "%s/platform/%s/include",
172 srcdir, platformname);
173 symlink(linkdest, path("machine"));
176 * "machine_base" points into platform/<PLATFORM>
179 snprintf(linkdest, sizeof(linkdest), "../../platform/%s",
182 snprintf(linkdest, sizeof(linkdest), "%s/platform/%s",
183 srcdir, platformname);
184 symlink(linkdest, path("machine_base"));
187 * "cpu" points to cpu/<MACHINE_ARCH>/include
190 snprintf(linkdest, sizeof(linkdest),
191 "../../cpu/%s/include", machinearchname);
193 snprintf(linkdest, sizeof(linkdest),
194 "%s/cpu/%s/include", srcdir, machinearchname);
195 symlink(linkdest, path("cpu"));
198 * "cpu_base" points to cpu/<MACHINE_ARCH>
201 snprintf(linkdest, sizeof(linkdest), "../../cpu/%s",
204 snprintf(linkdest, sizeof(linkdest), "%s/cpu/%s",
205 srcdir, machinearchname);
206 symlink(linkdest, path("cpu_base"));
209 * XXX check directory structure for architecture subdirectories and
210 * create the symlinks automatically XXX
213 for (i = 0; i < NELEM(emus); ++i) {
215 snprintf(linkdest, sizeof(linkdest),
216 "../../emulation/%s/%s",
217 emus[i], machinearchname);
219 snprintf(linkdest, sizeof(linkdest),
220 "%s/emulation/%s/%s",
221 srcdir, emus[i], machinearchname);
223 snprintf(linksrc, sizeof(linksrc), "arch_%s", emus[i]);
224 symlink(linkdest, path(linksrc));
228 options(); /* make options .h files */
229 makefile(); /* build Makefile */
230 headers(); /* make a lot of .h files */
231 configfile(); /* put config file into kernel*/
232 printf("Kernel build directory is %s\n", p);
238 * determine the root of the kernel source tree
239 * and save that in srcdir.
245 if (realpath("..", srcdir) == NULL)
246 errx(2, "Unable to find root of source tree");
253 fprintf(stderr, "usage: config [-gpr] [-d destdir] sysname\n");
259 * returns EOF on end of file
260 * NULL on end of line
261 * pointer to the word otherwise
266 static char line[80];
272 while ((ch = getc(fp)) != EOF)
273 if (ch != ' ' && ch != '\t')
291 while ((ch = getc(fp)) != EOF) {
305 * like get_word but will accept something in double or single quotes
306 * (to allow embedded spaces).
309 get_quoted_word(FILE *fp)
311 static char line[256];
317 while ((ch = getc(fp)) != EOF)
318 if (ch != ' ' && ch != '\t')
335 if (ch == '"' || ch == '\'') {
338 while ((ch = getc(fp)) != EOF) {
343 printf("config: missing quote reading `%s'\n",
351 while ((ch = getc(fp)) != EOF) {
366 * prepend the path to a filename
369 path(const char *file)
373 cp = malloc((size_t)(strlen(destdir) + (file ? strlen(file) : 0) + 2));
389 fi = fopen(PREFIX, "r");
391 err(2, "%s", PREFIX);
392 fo = fopen(p = path("config.c.new"), "w");
395 fprintf(fo, "#include \"opt_config.h\"\n");
396 fprintf(fo, "#ifdef INCLUDE_CONFIG_FILE \n");
397 fprintf(fo, "/* Mark config as used, so gcc doesn't optimize it away. */\n");
398 fprintf(fo, "#include <sys/types.h>\n");
399 fprintf(fo, "__used\n");
400 fprintf(fo, "static const char config[] = \"\\\n");
401 fprintf(fo, "START CONFIG FILE %s\\n\\\n___", PREFIX);
402 while (EOF != (i = getc(fi))) {
404 fprintf(fo, "\\n\\\n___");
405 } else if (i == '\"') {
407 } else if (i == '\\') {
413 fprintf(fo, "\\n\\\nEND CONFIG FILE %s\\n\\\n", PREFIX);
414 fprintf(fo, "\";\n");
415 fprintf(fo, "\n#endif /* INCLUDE_CONFIG_FILE */\n");
418 moveifchanged(path("config.c.new"), path("config.c"));
423 * compare two files; rename if changed.
426 moveifchanged(const char *from_name, const char *to_name)
431 struct stat from_sb, to_sb;
436 if ((from_fd = open(from_name, O_RDONLY)) < 0)
437 err(EX_OSERR, "moveifchanged open(%s)", from_name);
439 if ((to_fd = open(to_name, O_RDONLY)) < 0)
442 if (!changed && fstat(from_fd, &from_sb) < 0)
443 err(EX_OSERR, "moveifchanged fstat(%s)", from_name);
445 if (!changed && fstat(to_fd, &to_sb) < 0)
446 err(EX_OSERR, "moveifchanged fstat(%s)", to_name);
448 if (!changed && from_sb.st_size != to_sb.st_size)
451 tsize = (size_t)from_sb.st_size;
454 p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0);
456 #define MAP_FAILED ((caddr_t)-1)
459 err(EX_OSERR, "mmap %s", from_name);
460 q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0);
462 err(EX_OSERR, "mmap %s", to_name);
464 changed = memcmp(p, q, tsize);
469 if (rename(from_name, to_name) < 0)
470 err(EX_OSERR, "rename(%s, %s)", from_name, to_name);
472 if (unlink(from_name) < 0)
473 err(EX_OSERR, "unlink(%s)", from_name);