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