Commit | Line | Data |
---|---|---|
984263bc MD |
1 | /* |
2 | * Copyright (c) 1980, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
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. | |
dc71b7ab | 13 | * 3. Neither the name of the University nor the names of its contributors |
984263bc MD |
14 | * may be used to endorse or promote products derived from this software |
15 | * without specific prior written permission. | |
16 | * | |
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 | |
27 | * SUCH DAMAGE. | |
1de703da MD |
28 | * |
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 $ | |
984263bc MD |
32 | */ |
33 | ||
984263bc MD |
34 | #include <sys/types.h> |
35 | #include <sys/stat.h> | |
984263bc MD |
36 | #include <sys/mman.h> |
37 | #include <sys/param.h> | |
38 | #include <ctype.h> | |
39 | #include <err.h> | |
2c3b1d1b | 40 | #include <fcntl.h> |
984263bc | 41 | #include <stdio.h> |
e2ea6619 | 42 | #include <dirent.h> |
984263bc MD |
43 | #include <sysexits.h> |
44 | #include <unistd.h> | |
45 | #include "y.tab.h" | |
46 | #include "config.h" | |
47 | ||
48 | #ifndef TRUE | |
49 | #define TRUE (1) | |
50 | #endif | |
51 | ||
52 | #ifndef FALSE | |
53 | #define FALSE (0) | |
54 | #endif | |
55 | ||
39619464 | 56 | #define CDIR "../compile/" |
984263bc | 57 | |
7ad467de SW |
58 | char * platformname; |
59 | char * machinename; | |
60 | char * machinearchname; | |
61 | ||
62 | struct cputype *cputype; | |
63 | struct opt *opt, *mkopt; | |
64 | struct opt_list *otab; | |
65 | ||
984263bc MD |
66 | char * PREFIX; |
67 | char destdir[MAXPATHLEN]; | |
6ac311d8 | 68 | char srcdir[MAXPATHLEN - 32]; |
984263bc MD |
69 | |
70 | static int no_config_clobber = TRUE; | |
71 | int debugging; | |
984263bc | 72 | |
52e9aa73 | 73 | extern int yyparse(void); |
2d8a3be7 EN |
74 | static void configfile(void); |
75 | static void get_srcdir(void); | |
76 | static void usage(void); | |
984263bc MD |
77 | |
78 | /* | |
79 | * Config builds a set of files for building a UNIX | |
80 | * system given a description of the desired system. | |
81 | */ | |
82 | int | |
1c5ef3d3 | 83 | main(int argc, char *argv[]) |
984263bc | 84 | { |
984263bc | 85 | struct stat buf; |
60833b7d | 86 | int ch, len; |
984263bc | 87 | char *p; |
c98d4740 | 88 | char linkdest[MAXPATHLEN]; |
89 | #if 0 | |
90 | unsigned int i; | |
91 | char linksrc[64]; | |
d511d5d5 | 92 | static const char *emus[] = { "linux" }; |
c98d4740 | 93 | #endif |
984263bc | 94 | |
831b6312 | 95 | while ((ch = getopt(argc, argv, "d:gr")) != -1) |
984263bc MD |
96 | switch (ch) { |
97 | case 'd': | |
98 | if (*destdir == '\0') | |
99 | strlcpy(destdir, optarg, sizeof(destdir)); | |
100 | else | |
101 | errx(2, "directory already set"); | |
102 | break; | |
103 | case 'g': | |
104 | debugging++; | |
105 | break; | |
984263bc MD |
106 | case 'r': |
107 | no_config_clobber = FALSE; | |
108 | break; | |
109 | case '?': | |
110 | default: | |
111 | usage(); | |
112 | } | |
113 | argc -= optind; | |
114 | argv += optind; | |
115 | ||
116 | if (argc != 1) | |
117 | usage(); | |
118 | ||
119 | if (freopen(PREFIX = *argv, "r", stdin) == NULL) | |
120 | err(2, "%s", PREFIX); | |
121 | ||
122 | if (*destdir != '\0') { | |
123 | len = strlen(destdir); | |
124 | while (len > 1 && destdir[len - 1] == '/') | |
125 | destdir[--len] = '\0'; | |
126 | get_srcdir(); | |
127 | } else { | |
128 | strlcpy(destdir, CDIR, sizeof(destdir)); | |
129 | strlcat(destdir, PREFIX, sizeof(destdir)); | |
130 | } | |
131 | ||
2038fb68 | 132 | p = path(NULL); |
984263bc MD |
133 | if (stat(p, &buf)) { |
134 | if (mkdir(p, 0777)) | |
135 | err(2, "%s", p); | |
136 | } | |
137 | else if ((buf.st_mode & S_IFMT) != S_IFDIR) { | |
138 | errx(2, "%s isn't a directory", p); | |
139 | } | |
140 | else if (!no_config_clobber) { | |
141 | char tmp[strlen(p) + 8]; | |
142 | ||
143 | fprintf(stderr, "Removing old directory %s: ", p); | |
144 | fflush(stderr); | |
145 | snprintf(tmp, sizeof(tmp), "rm -rf %s", p); | |
146 | if (system(tmp)) { | |
147 | fprintf(stderr, "Failed!\n"); | |
148 | err(2, "%s", tmp); | |
149 | } | |
150 | fprintf(stderr, "Done.\n"); | |
151 | if (mkdir(p, 0777)) | |
152 | err(2, "%s", p); | |
153 | } | |
154 | ||
155 | dtab = NULL; | |
156 | if (yyparse()) | |
157 | exit(3); | |
0955fd91 | 158 | if (platformname == NULL) { |
579b67d2 | 159 | printf("Specify platform architecture, e.g. 'platform pc64'\n"); |
0955fd91 MD |
160 | exit(1); |
161 | } | |
586468dd | 162 | if (machinename == NULL) { |
579b67d2 | 163 | printf("Specify machine architecture, e.g. 'machine x86_64'\n"); |
984263bc MD |
164 | exit(1); |
165 | } | |
e2ea6619 | 166 | if (machinearchname == NULL) { |
579b67d2 | 167 | printf("Specify cpu architecture, e.g. 'machine_arch x86_64'\n"); |
29f546fd MD |
168 | exit(1); |
169 | } | |
586468dd | 170 | newbus_ioconf(); |
1c5ef3d3 | 171 | |
984263bc | 172 | /* |
0955fd91 | 173 | * "machine" points into platform/<PLATFORM>/include |
984263bc | 174 | */ |
984263bc | 175 | if (*srcdir == '\0') |
0955fd91 MD |
176 | snprintf(linkdest, sizeof(linkdest), "../../platform/%s/include", |
177 | platformname); | |
984263bc | 178 | else |
0955fd91 MD |
179 | snprintf(linkdest, sizeof(linkdest), "%s/platform/%s/include", |
180 | srcdir, platformname); | |
1c5ef3d3 | 181 | symlink(linkdest, path("machine")); |
1f2de5d4 | 182 | |
be1c58aa | 183 | /* |
0955fd91 | 184 | * "machine_base" points into platform/<PLATFORM> |
be1c58aa MD |
185 | */ |
186 | if (*srcdir == '\0') | |
0955fd91 MD |
187 | snprintf(linkdest, sizeof(linkdest), "../../platform/%s", |
188 | platformname); | |
be1c58aa | 189 | else |
0955fd91 MD |
190 | snprintf(linkdest, sizeof(linkdest), "%s/platform/%s", |
191 | srcdir, platformname); | |
e2ea6619 | 192 | symlink(linkdest, path("machine_base")); |
be1c58aa | 193 | |
29f546fd | 194 | /* |
e2ea6619 | 195 | * "cpu" points to cpu/<MACHINE_ARCH>/include |
29f546fd | 196 | */ |
e2ea6619 MD |
197 | if (*srcdir == '\0') |
198 | snprintf(linkdest, sizeof(linkdest), | |
199 | "../../cpu/%s/include", machinearchname); | |
200 | else | |
201 | snprintf(linkdest, sizeof(linkdest), | |
202 | "%s/cpu/%s/include", srcdir, machinearchname); | |
203 | symlink(linkdest, path("cpu")); | |
29f546fd | 204 | |
e2ea6619 MD |
205 | /* |
206 | * "cpu_base" points to cpu/<MACHINE_ARCH> | |
207 | */ | |
208 | if (*srcdir == '\0') | |
209 | snprintf(linkdest, sizeof(linkdest), "../../cpu/%s", | |
210 | machinearchname); | |
211 | else | |
212 | snprintf(linkdest, sizeof(linkdest), "%s/cpu/%s", | |
213 | srcdir, machinearchname); | |
214 | symlink(linkdest, path("cpu_base")); | |
215 | ||
1f2de5d4 MD |
216 | /* |
217 | * XXX check directory structure for architecture subdirectories and | |
218 | * create the symlinks automatically XXX | |
219 | */ | |
c98d4740 | 220 | #if 0 |
e62ef63c | 221 | for (i = 0; i < NELEM(emus); ++i) { |
932f49b9 | 222 | if (*srcdir == 0) { |
1c5ef3d3 MD |
223 | snprintf(linkdest, sizeof(linkdest), |
224 | "../../emulation/%s/%s", | |
e2ea6619 | 225 | emus[i], machinearchname); |
932f49b9 | 226 | } else { |
1c5ef3d3 MD |
227 | snprintf(linkdest, sizeof(linkdest), |
228 | "%s/emulation/%s/%s", | |
e2ea6619 | 229 | srcdir, emus[i], machinearchname); |
932f49b9 | 230 | } |
1c5ef3d3 MD |
231 | snprintf(linksrc, sizeof(linksrc), "arch_%s", emus[i]); |
232 | symlink(linkdest, path(linksrc)); | |
1f2de5d4 | 233 | } |
c98d4740 | 234 | #endif |
1f2de5d4 | 235 | |
984263bc MD |
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); | |
1c5ef3d3 | 241 | exit(EX_OK); |
984263bc MD |
242 | } |
243 | ||
244 | /* | |
245 | * get_srcdir | |
246 | * determine the root of the kernel source tree | |
247 | * and save that in srcdir. | |
248 | */ | |
249 | static void | |
3a2e6dbb | 250 | get_srcdir(void) |
984263bc | 251 | { |
52da1405 | 252 | |
39619464 | 253 | if (realpath("..", srcdir) == NULL) |
984263bc MD |
254 | errx(2, "Unable to find root of source tree"); |
255 | } | |
256 | ||
257 | static void | |
3a2e6dbb | 258 | usage(void) |
984263bc | 259 | { |
52da1405 EN |
260 | |
261 | fprintf(stderr, "usage: config [-gpr] [-d destdir] sysname\n"); | |
262 | exit(1); | |
984263bc MD |
263 | } |
264 | ||
265 | /* | |
266 | * get_word | |
267 | * returns EOF on end of file | |
268 | * NULL on end of line | |
269 | * pointer to the word otherwise | |
270 | */ | |
271 | char * | |
c0c7f013 | 272 | get_word(FILE *fp) |
984263bc MD |
273 | { |
274 | static char line[80]; | |
c0c7f013 EN |
275 | int ch; |
276 | char *cp; | |
984263bc MD |
277 | int escaped_nl = 0; |
278 | ||
279 | begin: | |
280 | while ((ch = getc(fp)) != EOF) | |
281 | if (ch != ' ' && ch != '\t') | |
282 | break; | |
283 | if (ch == EOF) | |
52da1405 EN |
284 | return((char *)EOF); |
285 | if (ch == '\\') { | |
984263bc MD |
286 | escaped_nl = 1; |
287 | goto begin; | |
288 | } | |
289 | if (ch == '\n') { | |
52da1405 | 290 | if (escaped_nl) { |
984263bc MD |
291 | escaped_nl = 0; |
292 | goto begin; | |
293 | } | |
294 | else | |
52da1405 | 295 | return(NULL); |
984263bc MD |
296 | } |
297 | cp = line; | |
298 | *cp++ = ch; | |
299 | while ((ch = getc(fp)) != EOF) { | |
300 | if (isspace(ch)) | |
301 | break; | |
302 | *cp++ = ch; | |
303 | } | |
304 | *cp = 0; | |
305 | if (ch == EOF) | |
52da1405 | 306 | return((char *)EOF); |
08fdb9c1 | 307 | ungetc(ch, fp); |
52da1405 | 308 | return(line); |
984263bc MD |
309 | } |
310 | ||
311 | /* | |
312 | * get_quoted_word | |
313 | * like get_word but will accept something in double or single quotes | |
314 | * (to allow embedded spaces). | |
315 | */ | |
316 | char * | |
c0c7f013 | 317 | get_quoted_word(FILE *fp) |
984263bc MD |
318 | { |
319 | static char line[256]; | |
c0c7f013 EN |
320 | int ch; |
321 | char *cp; | |
984263bc MD |
322 | int escaped_nl = 0; |
323 | ||
324 | begin: | |
325 | while ((ch = getc(fp)) != EOF) | |
326 | if (ch != ' ' && ch != '\t') | |
327 | break; | |
328 | if (ch == EOF) | |
52da1405 | 329 | return((char *)EOF); |
1c5ef3d3 | 330 | if (ch == '\\') { |
984263bc MD |
331 | escaped_nl = 1; |
332 | goto begin; | |
333 | } | |
334 | if (ch == '\n') { | |
52da1405 | 335 | if (escaped_nl) { |
984263bc MD |
336 | escaped_nl = 0; |
337 | goto begin; | |
338 | } | |
339 | else | |
52da1405 | 340 | return(NULL); |
984263bc MD |
341 | } |
342 | cp = line; | |
343 | if (ch == '"' || ch == '\'') { | |
c0c7f013 | 344 | int quote = ch; |
984263bc MD |
345 | |
346 | while ((ch = getc(fp)) != EOF) { | |
347 | if (ch == quote) | |
348 | break; | |
349 | if (ch == '\n') { | |
350 | *cp = 0; | |
351 | printf("config: missing quote reading `%s'\n", | |
352 | line); | |
353 | exit(2); | |
354 | } | |
355 | *cp++ = ch; | |
356 | } | |
357 | } else { | |
358 | *cp++ = ch; | |
359 | while ((ch = getc(fp)) != EOF) { | |
360 | if (isspace(ch)) | |
361 | break; | |
362 | *cp++ = ch; | |
363 | } | |
364 | if (ch != EOF) | |
08fdb9c1 | 365 | ungetc(ch, fp); |
984263bc MD |
366 | } |
367 | *cp = 0; | |
368 | if (ch == EOF) | |
52da1405 EN |
369 | return((char *)EOF); |
370 | return(line); | |
984263bc MD |
371 | } |
372 | ||
373 | /* | |
374 | * prepend the path to a filename | |
375 | */ | |
376 | char * | |
67c34974 | 377 | path(const char *file) |
984263bc | 378 | { |
c0c7f013 | 379 | char *cp; |
984263bc MD |
380 | |
381 | cp = malloc((size_t)(strlen(destdir) + (file ? strlen(file) : 0) + 2)); | |
08fdb9c1 | 382 | strcpy(cp, destdir); |
52c13bff | 383 | if (file != NULL) { |
08fdb9c1 MD |
384 | strcat(cp, "/"); |
385 | strcat(cp, file); | |
984263bc | 386 | } |
52da1405 | 387 | return(cp); |
984263bc MD |
388 | } |
389 | ||
390 | static void | |
3a2e6dbb | 391 | configfile(void) |
984263bc MD |
392 | { |
393 | FILE *fi, *fo; | |
394 | char *p; | |
395 | int i; | |
396 | ||
397 | fi = fopen(PREFIX, "r"); | |
52c13bff | 398 | if (fi == NULL) |
984263bc | 399 | err(2, "%s", PREFIX); |
52da1405 | 400 | fo = fopen(p = path("config.c.new"), "w"); |
52c13bff | 401 | if (fo == NULL) |
984263bc MD |
402 | err(2, "%s", p); |
403 | fprintf(fo, "#include \"opt_config.h\"\n"); | |
404 | fprintf(fo, "#ifdef INCLUDE_CONFIG_FILE \n"); | |
af7f5f8a TN |
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"); | |
984263bc | 409 | fprintf(fo, "START CONFIG FILE %s\\n\\\n___", PREFIX); |
52da1405 | 410 | while (EOF != (i = getc(fi))) { |
984263bc MD |
411 | if (i == '\n') { |
412 | fprintf(fo, "\\n\\\n___"); | |
413 | } else if (i == '\"') { | |
414 | fprintf(fo, "\\\""); | |
415 | } else if (i == '\\') { | |
416 | fprintf(fo, "\\\\"); | |
417 | } else { | |
418 | putc(i, fo); | |
419 | } | |
420 | } | |
421 | fprintf(fo, "\\n\\\nEND CONFIG FILE %s\\n\\\n", PREFIX); | |
422 | fprintf(fo, "\";\n"); | |
423 | fprintf(fo, "\n#endif /* INCLUDE_CONFIG_FILE */\n"); | |
424 | fclose(fi); | |
425 | fclose(fo); | |
426 | moveifchanged(path("config.c.new"), path("config.c")); | |
427 | } | |
428 | ||
429 | /* | |
430 | * moveifchanged -- | |
431 | * compare two files; rename if changed. | |
432 | */ | |
433 | void | |
434 | moveifchanged(const char *from_name, const char *to_name) | |
435 | { | |
436 | char *p, *q; | |
437 | int changed; | |
438 | size_t tsize; | |
439 | struct stat from_sb, to_sb; | |
440 | int from_fd, to_fd; | |
441 | ||
442 | changed = 0; | |
443 | ||
444 | if ((from_fd = open(from_name, O_RDONLY)) < 0) | |
445 | err(EX_OSERR, "moveifchanged open(%s)", from_name); | |
446 | ||
447 | if ((to_fd = open(to_name, O_RDONLY)) < 0) | |
448 | changed++; | |
449 | ||
450 | if (!changed && fstat(from_fd, &from_sb) < 0) | |
451 | err(EX_OSERR, "moveifchanged fstat(%s)", from_name); | |
452 | ||
453 | if (!changed && fstat(to_fd, &to_sb) < 0) | |
454 | err(EX_OSERR, "moveifchanged fstat(%s)", to_name); | |
455 | ||
456 | if (!changed && from_sb.st_size != to_sb.st_size) | |
457 | changed++; | |
458 | ||
459 | tsize = (size_t)from_sb.st_size; | |
460 | ||
461 | if (!changed) { | |
462 | p = mmap(NULL, tsize, PROT_READ, MAP_SHARED, from_fd, (off_t)0); | |
463 | #ifndef MAP_FAILED | |
52da1405 | 464 | #define MAP_FAILED ((caddr_t)-1) |
984263bc MD |
465 | #endif |
466 | if (p == MAP_FAILED) | |
467 | err(EX_OSERR, "mmap %s", from_name); | |
468 | q = mmap(NULL, tsize, PROT_READ, MAP_SHARED, to_fd, (off_t)0); | |
469 | if (q == MAP_FAILED) | |
470 | err(EX_OSERR, "mmap %s", to_name); | |
471 | ||
472 | changed = memcmp(p, q, tsize); | |
473 | munmap(p, tsize); | |
474 | munmap(q, tsize); | |
475 | } | |
476 | if (changed) { | |
477 | if (rename(from_name, to_name) < 0) | |
478 | err(EX_OSERR, "rename(%s, %s)", from_name, to_name); | |
479 | } else { | |
480 | if (unlink(from_name) < 0) | |
481 | err(EX_OSERR, "unlink(%s)", from_name); | |
482 | } | |
483 | } |