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>
37 #include <sys/param.h>
56 #define CDIR "../compile/"
60 char * machinearchname
;
62 struct cputype
*cputype
;
63 struct opt
*opt
, *mkopt
;
64 struct opt_list
*otab
;
67 char destdir
[MAXPATHLEN
];
68 char srcdir
[MAXPATHLEN
- 32];
70 static int no_config_clobber
= TRUE
;
73 extern int yyparse(void);
74 static void configfile(void);
75 static void get_srcdir(void);
76 static void usage(void);
79 * Config builds a set of files for building a UNIX
80 * system given a description of the desired system.
83 main(int argc
, char *argv
[])
88 char linkdest
[MAXPATHLEN
];
92 static const char *emus
[] = { "linux" };
95 while ((ch
= getopt(argc
, argv
, "d:gr")) != -1)
99 strlcpy(destdir
, optarg
, sizeof(destdir
));
101 errx(2, "directory already set");
107 no_config_clobber
= FALSE
;
119 if (freopen(PREFIX
= *argv
, "r", stdin
) == NULL
)
120 err(2, "%s", PREFIX
);
122 if (*destdir
!= '\0') {
123 len
= strlen(destdir
);
124 while (len
> 1 && destdir
[len
- 1] == '/')
125 destdir
[--len
] = '\0';
128 strlcpy(destdir
, CDIR
, sizeof(destdir
));
129 strlcat(destdir
, PREFIX
, sizeof(destdir
));
137 else if ((buf
.st_mode
& S_IFMT
) != S_IFDIR
) {
138 errx(2, "%s isn't a directory", p
);
140 else if (!no_config_clobber
) {
141 char tmp
[strlen(p
) + 8];
143 fprintf(stderr
, "Removing old directory %s: ", p
);
145 snprintf(tmp
, sizeof(tmp
), "rm -rf %s", p
);
147 fprintf(stderr
, "Failed!\n");
150 fprintf(stderr
, "Done.\n");
158 if (platformname
== NULL
) {
159 printf("Specify platform architecture, e.g. 'platform pc64'\n");
162 if (machinename
== NULL
) {
163 printf("Specify machine architecture, e.g. 'machine x86_64'\n");
166 if (machinearchname
== NULL
) {
167 printf("Specify cpu architecture, e.g. 'machine_arch x86_64'\n");
173 * "machine" points into platform/<PLATFORM>/include
176 snprintf(linkdest
, sizeof(linkdest
), "../../platform/%s/include",
179 snprintf(linkdest
, sizeof(linkdest
), "%s/platform/%s/include",
180 srcdir
, platformname
);
181 symlink(linkdest
, path("machine"));
184 * "machine_base" points into platform/<PLATFORM>
187 snprintf(linkdest
, sizeof(linkdest
), "../../platform/%s",
190 snprintf(linkdest
, sizeof(linkdest
), "%s/platform/%s",
191 srcdir
, platformname
);
192 symlink(linkdest
, path("machine_base"));
195 * "cpu" points to cpu/<MACHINE_ARCH>/include
198 snprintf(linkdest
, sizeof(linkdest
),
199 "../../cpu/%s/include", machinearchname
);
201 snprintf(linkdest
, sizeof(linkdest
),
202 "%s/cpu/%s/include", srcdir
, machinearchname
);
203 symlink(linkdest
, path("cpu"));
206 * "cpu_base" points to cpu/<MACHINE_ARCH>
209 snprintf(linkdest
, sizeof(linkdest
), "../../cpu/%s",
212 snprintf(linkdest
, sizeof(linkdest
), "%s/cpu/%s",
213 srcdir
, machinearchname
);
214 symlink(linkdest
, path("cpu_base"));
217 * XXX check directory structure for architecture subdirectories and
218 * create the symlinks automatically XXX
221 for (i
= 0; i
< NELEM(emus
); ++i
) {
223 snprintf(linkdest
, sizeof(linkdest
),
224 "../../emulation/%s/%s",
225 emus
[i
], machinearchname
);
227 snprintf(linkdest
, sizeof(linkdest
),
228 "%s/emulation/%s/%s",
229 srcdir
, emus
[i
], machinearchname
);
231 snprintf(linksrc
, sizeof(linksrc
), "arch_%s", emus
[i
]);
232 symlink(linkdest
, path(linksrc
));
236 options(); /* make options .h files */
237 makefile(); /* build Makefile */
238 headers(); /* make a lot of .h files */
239 configfile(); /* put config file into kernel*/
240 printf("Kernel build directory is %s\n", p
);
246 * determine the root of the kernel source tree
247 * and save that in srcdir.
253 if (realpath("..", srcdir
) == NULL
)
254 errx(2, "Unable to find root of source tree");
261 fprintf(stderr
, "usage: config [-gpr] [-d destdir] sysname\n");
267 * returns EOF on end of file
268 * NULL on end of line
269 * pointer to the word otherwise
274 static char line
[80];
280 while ((ch
= getc(fp
)) != EOF
)
281 if (ch
!= ' ' && ch
!= '\t')
299 while ((ch
= getc(fp
)) != EOF
) {
313 * like get_word but will accept something in double or single quotes
314 * (to allow embedded spaces).
317 get_quoted_word(FILE *fp
)
319 static char line
[256];
325 while ((ch
= getc(fp
)) != EOF
)
326 if (ch
!= ' ' && ch
!= '\t')
343 if (ch
== '"' || ch
== '\'') {
346 while ((ch
= getc(fp
)) != EOF
) {
351 printf("config: missing quote reading `%s'\n",
359 while ((ch
= getc(fp
)) != EOF
) {
374 * prepend the path to a filename
377 path(const char *file
)
381 cp
= malloc((size_t)(strlen(destdir
) + (file
? strlen(file
) : 0) + 2));
397 fi
= fopen(PREFIX
, "r");
399 err(2, "%s", PREFIX
);
400 fo
= fopen(p
= path("config.c.new"), "w");
403 fprintf(fo
, "#include \"opt_config.h\"\n");
404 fprintf(fo
, "#ifdef INCLUDE_CONFIG_FILE \n");
405 fprintf(fo
, "/* Mark config as used, so gcc doesn't optimize it away. */\n");
406 fprintf(fo
, "#include <sys/types.h>\n");
407 fprintf(fo
, "__used\n");
408 fprintf(fo
, "static const char config[] = \"\\\n");
409 fprintf(fo
, "START CONFIG FILE %s\\n\\\n___", PREFIX
);
410 while (EOF
!= (i
= getc(fi
))) {
412 fprintf(fo
, "\\n\\\n___");
413 } else if (i
== '\"') {
415 } else if (i
== '\\') {
421 fprintf(fo
, "\\n\\\nEND CONFIG FILE %s\\n\\\n", PREFIX
);
422 fprintf(fo
, "\";\n");
423 fprintf(fo
, "\n#endif /* INCLUDE_CONFIG_FILE */\n");
426 moveifchanged(path("config.c.new"), path("config.c"));
431 * compare two files; rename if changed.
434 moveifchanged(const char *from_name
, const char *to_name
)
439 struct stat from_sb
, to_sb
;
444 if ((from_fd
= open(from_name
, O_RDONLY
)) < 0)
445 err(EX_OSERR
, "moveifchanged open(%s)", from_name
);
447 if ((to_fd
= open(to_name
, O_RDONLY
)) < 0)
450 if (!changed
&& fstat(from_fd
, &from_sb
) < 0)
451 err(EX_OSERR
, "moveifchanged fstat(%s)", from_name
);
453 if (!changed
&& fstat(to_fd
, &to_sb
) < 0)
454 err(EX_OSERR
, "moveifchanged fstat(%s)", to_name
);
456 if (!changed
&& from_sb
.st_size
!= to_sb
.st_size
)
459 tsize
= (size_t)from_sb
.st_size
;
462 p
= mmap(NULL
, tsize
, PROT_READ
, MAP_SHARED
, from_fd
, (off_t
)0);
464 #define MAP_FAILED ((caddr_t)-1)
467 err(EX_OSERR
, "mmap %s", from_name
);
468 q
= mmap(NULL
, tsize
, PROT_READ
, MAP_SHARED
, to_fd
, (off_t
)0);
470 err(EX_OSERR
, "mmap %s", to_name
);
472 changed
= memcmp(p
, q
, tsize
);
477 if (rename(from_name
, to_name
) < 0)
478 err(EX_OSERR
, "rename(%s, %s)", from_name
, to_name
);
480 if (unlink(from_name
) < 0)
481 err(EX_OSERR
, "unlink(%s)", from_name
);