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