1.0.32.33: GENCGC tuning parameters
[sbcl.git] / tools-for-build / grovel-headers.c
1 /*
2  * Rummage through the system header files using the C compiler itself
3  * as a parser, extracting stuff like preprocessor constants and the
4  * sizes and signedness of basic system types, and write it out as
5  * Lisp code.
6  */
7
8 /*
9  * This software is part of the SBCL system. See the README file for
10  * more information.
11  *
12  * While most of SBCL is derived from the CMU CL system, many
13  * utilities for the build process (like this one) were written from
14  * scratch after the fork from CMU CL.
15  *
16  * This software is in the public domain and is provided with
17  * absolutely no warranty. See the COPYING and CREDITS files for
18  * more information.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #ifdef _WIN32
25   #define WIN32_LEAN_AND_MEAN
26   #include <windows.h>
27   #include <shlobj.h>
28 #else
29   #include <sys/times.h>
30   #include <sys/wait.h>
31   #include <sys/ioctl.h>
32   #include <sys/termios.h>
33   #ifdef __APPLE_CC__
34     #include "../src/runtime/darwin-dlshim.h"
35     #include "../src/runtime/darwin-langinfo.h"
36   #else
37     #include <dlfcn.h>
38     #include <langinfo.h>
39   #endif
40 #endif
41
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <signal.h>
46 #include <errno.h>
47
48 #include "genesis/config.h"
49
50 #ifdef LISP_FEATURE_HPUX
51 #include <sys/bsdtty.h> /* for TIOCGPGRP */
52 #endif
53
54 #ifdef LISP_FEATURE_BSD
55   #include <sys/param.h>
56   #include <sys/sysctl.h>
57 #endif
58
59 #include "wrap.h"
60 #include "gc.h"
61
62 #define DEFTYPE(lispname,cname) { cname foo; \
63     printf("(define-alien-type " lispname " (%s %d))\n", (((foo=-1)<0) ? "sb!alien:signed" : "unsigned"), (8 * (sizeof foo))); }
64
65 void
66 defconstant(char* lisp_name, unsigned long unix_number)
67 {
68     printf("(defconstant %s %lu) ; #x%lx\n",
69            lisp_name, unix_number, unix_number);
70 }
71
72 void deferrno(char* lisp_name, unsigned long unix_number)
73 {
74     defconstant(lisp_name, unix_number);
75 }
76
77 void defsignal(char* lisp_name, unsigned long unix_number)
78 {
79     defconstant(lisp_name, unix_number);
80 }
81
82 int
83 main(int argc, char *argv[])
84 {
85     /* don't need no steenking command line arguments */
86     if (1 != argc) {
87         fprintf(stderr, "argh! command line argument(s)\n");
88         exit(1);
89     }
90
91     /* don't need no steenking hand-editing */
92     printf(
93 ";;;; This is an automatically generated file, please do not hand-edit it.\n\
94 ;;;; See the program \"grovel-headers.c\".\n\
95 \n\
96 ");
97 #ifdef _WIN32
98     printf("(in-package \"SB!WIN32\")\n\n");
99
100     defconstant ("input-record-size", sizeof (INPUT_RECORD));
101
102     defconstant ("MAX_PATH", MAX_PATH);
103
104     printf(";;; CSIDL\n");
105
106     defconstant ("CSIDL_DESKTOP", CSIDL_DESKTOP);
107     defconstant ("CSIDL_INTERNET", CSIDL_INTERNET);
108     defconstant ("CSIDL_PROGRAMS", CSIDL_PROGRAMS);
109     defconstant ("CSIDL_CONTROLS", CSIDL_CONTROLS);
110     defconstant ("CSIDL_PRINTERS", CSIDL_PRINTERS);
111     defconstant ("CSIDL_PERSONAL", CSIDL_PERSONAL);
112     defconstant ("CSIDL_FAVORITES", CSIDL_FAVORITES);
113     defconstant ("CSIDL_STARTUP", CSIDL_STARTUP);
114     defconstant ("CSIDL_RECENT", CSIDL_RECENT);
115     defconstant ("CSIDL_SENDTO", CSIDL_SENDTO);
116     defconstant ("CSIDL_BITBUCKET", CSIDL_BITBUCKET);
117     defconstant ("CSIDL_STARTMENU", CSIDL_STARTMENU);
118     defconstant ("CSIDL_DESKTOPDIRECTORY", CSIDL_DESKTOPDIRECTORY);
119     defconstant ("CSIDL_DRIVES", CSIDL_DRIVES);
120     defconstant ("CSIDL_NETWORK", CSIDL_NETWORK);
121     defconstant ("CSIDL_NETHOOD", CSIDL_NETHOOD);
122     defconstant ("CSIDL_FONTS", CSIDL_FONTS);
123     defconstant ("CSIDL_TEMPLATES", CSIDL_TEMPLATES);
124     defconstant ("CSIDL_COMMON_STARTMENU", CSIDL_COMMON_STARTMENU);
125     defconstant ("CSIDL_COMMON_PROGRAMS", CSIDL_COMMON_PROGRAMS);
126     defconstant ("CSIDL_COMMON_STARTUP", CSIDL_COMMON_STARTUP);
127     defconstant ("CSIDL_COMMON_DESKTOPDIRECTORY", CSIDL_COMMON_DESKTOPDIRECTORY);
128     defconstant ("CSIDL_APPDATA", CSIDL_APPDATA);
129     defconstant ("CSIDL_PRINTHOOD", CSIDL_PRINTHOOD);
130     defconstant ("CSIDL_LOCAL_APPDATA", CSIDL_LOCAL_APPDATA);
131     defconstant ("CSIDL_ALTSTARTUP", CSIDL_ALTSTARTUP);
132     defconstant ("CSIDL_COMMON_ALTSTARTUP", CSIDL_COMMON_ALTSTARTUP);
133     defconstant ("CSIDL_COMMON_FAVORITES", CSIDL_COMMON_FAVORITES);
134     defconstant ("CSIDL_INTERNET_CACHE", CSIDL_INTERNET_CACHE);
135     defconstant ("CSIDL_COOKIES", CSIDL_COOKIES);
136     defconstant ("CSIDL_HISTORY", CSIDL_HISTORY);
137     defconstant ("CSIDL_COMMON_APPDATA", CSIDL_COMMON_APPDATA);
138     defconstant ("CSIDL_WINDOWS", CSIDL_WINDOWS);
139     defconstant ("CSIDL_SYSTEM", CSIDL_SYSTEM);
140     defconstant ("CSIDL_PROGRAM_FILES", CSIDL_PROGRAM_FILES);
141     defconstant ("CSIDL_MYPICTURES", CSIDL_MYPICTURES);
142     defconstant ("CSIDL_PROFILE", CSIDL_PROFILE);
143     defconstant ("CSIDL_SYSTEMX86", CSIDL_SYSTEMX86);
144     defconstant ("CSIDL_PROGRAM_FILESX86", CSIDL_PROGRAM_FILESX86);
145     defconstant ("CSIDL_PROGRAM_FILES_COMMON", CSIDL_PROGRAM_FILES_COMMON);
146     defconstant ("CSIDL_PROGRAM_FILES_COMMONX86", CSIDL_PROGRAM_FILES_COMMONX86);
147     defconstant ("CSIDL_COMMON_TEMPLATES", CSIDL_COMMON_TEMPLATES);
148     defconstant ("CSIDL_COMMON_DOCUMENTS", CSIDL_COMMON_DOCUMENTS);
149     defconstant ("CSIDL_COMMON_ADMINTOOLS", CSIDL_COMMON_ADMINTOOLS);
150     defconstant ("CSIDL_ADMINTOOLS", CSIDL_ADMINTOOLS);
151     defconstant ("CSIDL_CONNECTIONS", CSIDL_CONNECTIONS);
152     defconstant ("CSIDL_COMMON_MUSIC", CSIDL_COMMON_MUSIC);
153     defconstant ("CSIDL_COMMON_PICTURES", CSIDL_COMMON_PICTURES);
154     defconstant ("CSIDL_COMMON_VIDEO", CSIDL_COMMON_VIDEO);
155     defconstant ("CSIDL_RESOURCES", CSIDL_RESOURCES);
156     defconstant ("CSIDL_RESOURCES_LOCALIZED", CSIDL_RESOURCES_LOCALIZED);
157     defconstant ("CSIDL_COMMON_OEM_LINKS", CSIDL_COMMON_OEM_LINKS);
158     defconstant ("CSIDL_CDBURN_AREA", CSIDL_CDBURN_AREA);
159     defconstant ("CSIDL_COMPUTERSNEARME", CSIDL_COMPUTERSNEARME);
160     defconstant ("CSIDL_FLAG_DONT_VERIFY", CSIDL_FLAG_DONT_VERIFY);
161     defconstant ("CSIDL_FLAG_CREATE", CSIDL_FLAG_CREATE);
162     defconstant ("CSIDL_FLAG_MASK", CSIDL_FLAG_MASK);
163
164     printf(";;; Exception codes\n");
165     defconstant("+exception-access-violation+", EXCEPTION_ACCESS_VIOLATION);
166     defconstant("+exception-array-bounds-exceeded+", EXCEPTION_ARRAY_BOUNDS_EXCEEDED);
167     defconstant("+exception-breakpoint+", EXCEPTION_BREAKPOINT);
168     defconstant("+exception-datatype-misalignment+", EXCEPTION_DATATYPE_MISALIGNMENT);
169     defconstant("+exception-flt-denormal-operand+", EXCEPTION_FLT_DENORMAL_OPERAND);
170     defconstant("+exception-flt-divide-by-zero+", EXCEPTION_FLT_DIVIDE_BY_ZERO);
171     defconstant("+exception-flt-inexact-result+", EXCEPTION_FLT_INEXACT_RESULT);
172     defconstant("+exception-flt-invalid-operation+", EXCEPTION_FLT_INVALID_OPERATION);
173     defconstant("+exception-flt-overflow+", EXCEPTION_FLT_OVERFLOW);
174     defconstant("+exception-flt-stack-check+", EXCEPTION_FLT_STACK_CHECK);
175     defconstant("+exception-flt-underflow+", EXCEPTION_FLT_UNDERFLOW);
176     defconstant("+exception-illegal-instruction+", EXCEPTION_ILLEGAL_INSTRUCTION);
177     defconstant("+exception-in-page-error+", EXCEPTION_IN_PAGE_ERROR);
178     defconstant("+exception-int-divide-by-zero+", EXCEPTION_INT_DIVIDE_BY_ZERO);
179     defconstant("+exception-int-overflow+", EXCEPTION_INT_OVERFLOW);
180     defconstant("+exception-invalid-disposition+", EXCEPTION_INVALID_DISPOSITION);
181     defconstant("+exception-noncontinuable-exception+", EXCEPTION_NONCONTINUABLE_EXCEPTION);
182     defconstant("+exception-priv-instruction+", EXCEPTION_PRIV_INSTRUCTION);
183     defconstant("+exception-single-step+", EXCEPTION_SINGLE_STEP);
184     defconstant("+exception-stack-overflow+", EXCEPTION_STACK_OVERFLOW);
185
186     printf(";;; FormatMessage\n");
187
188     defconstant ("FORMAT_MESSAGE_ALLOCATE_BUFFER", FORMAT_MESSAGE_ALLOCATE_BUFFER);
189     defconstant ("FORMAT_MESSAGE_FROM_SYSTEM", FORMAT_MESSAGE_FROM_SYSTEM);
190
191     printf(";;; Errors\n");
192
193     defconstant ("ERROR_ENVVAR_NOT_FOUND", ERROR_ENVVAR_NOT_FOUND);
194
195     printf(";;; GetComputerName\n");
196
197     defconstant ("MAX_COMPUTERNAME_LENGTH", MAX_COMPUTERNAME_LENGTH);
198     defconstant ("ERROR_BUFFER_OVERFLOW", ERROR_BUFFER_OVERFLOW);
199
200     printf(";;; Windows Types\n");
201     DEFTYPE("int-ptr", INT_PTR);
202     DEFTYPE("dword",   DWORD);
203     DEFTYPE("bool",    BOOL);
204     DEFTYPE("uint",    UINT);
205     DEFTYPE("ulong",   ULONG);
206
207     /* FIXME: SB-UNIX and SB-WIN32 really need to be untangled. */
208     printf("(in-package \"SB!UNIX\")\n\n");
209     printf(";;; Unix-like constants and types on Windows\n");
210     defconstant("o_rdonly", _O_RDONLY);
211     defconstant("o_wronly", _O_WRONLY);
212     defconstant("o_rdwr",   _O_RDWR);
213     defconstant("o_creat",  _O_CREAT);
214     defconstant("o_trunc",  _O_TRUNC);
215     defconstant("o_append", _O_APPEND);
216     defconstant("o_excl",   _O_EXCL);
217     defconstant("o_binary", _O_BINARY);
218
219     defconstant("enoent", ENOENT);
220     defconstant("eexist", EEXIST);
221     defconstant("eintr", EINTR);
222     defconstant("eagain", EAGAIN);
223
224     defconstant("s-ifmt",  S_IFMT);
225     defconstant("s-ifdir", S_IFDIR);
226     defconstant("s-ifreg", S_IFREG);
227
228     DEFTYPE("ino-t",  ino_t);
229     DEFTYPE("time-t", time_t);
230     DEFTYPE("off-t",  off_t);
231     DEFTYPE("size-t", size_t);
232     DEFTYPE("mode-t", mode_t);
233
234     DEFTYPE("wst-dev-t", wst_dev_t);
235     DEFTYPE("wst-off-t", wst_off_t);
236     DEFTYPE("wst-blksize-t", wst_blksize_t);
237     DEFTYPE("wst-blkcnt-t", wst_blkcnt_t);
238     DEFTYPE("wst-nlink-t", wst_nlink_t);
239     DEFTYPE("wst-uid-t", wst_uid_t);
240     DEFTYPE("wst-gid-t", wst_gid_t);
241     printf("\n");
242 #else
243     printf("(in-package \"SB!ALIEN\")\n\n");
244
245     printf (";;;flags for dlopen()\n");
246
247     defconstant ("rtld-lazy", RTLD_LAZY);
248     defconstant ("rtld-now", RTLD_NOW);
249     defconstant ("rtld-global", RTLD_GLOBAL);
250
251     printf("(in-package \"SB!UNIX\")\n\n");
252
253     printf(";;; langinfo\n");
254     defconstant("codeset", CODESET);
255
256     printf(";;; types, types, types\n");
257     DEFTYPE("clock-t", clock_t);
258     DEFTYPE("dev-t",   dev_t);
259     DEFTYPE("gid-t",   gid_t);
260     DEFTYPE("ino-t",   ino_t);
261     DEFTYPE("mode-t",  mode_t);
262     DEFTYPE("nlink-t", nlink_t);
263     DEFTYPE("off-t",   off_t);
264     DEFTYPE("size-t",  size_t);
265     DEFTYPE("time-t",  time_t);
266 #if !defined(LISP_FEATURE_OS_PROVIDES_SUSECONDS_T)
267     /* Similar kludge in sb-posix. */
268     DEFTYPE("suseconds-t", long);
269 #else
270     DEFTYPE("suseconds-t", suseconds_t);
271 #endif
272     DEFTYPE("uid-t",   uid_t);
273     printf(";; Types in src/runtime/wrap.h. See that file for explantion.\n");
274     printf(";; Don't use these types for anything other than the stat wrapper.\n");
275     DEFTYPE("wst-dev-t", wst_dev_t);
276     DEFTYPE("wst-off-t", wst_off_t);
277     DEFTYPE("wst-blksize-t", wst_blksize_t);
278     DEFTYPE("wst-blkcnt-t", wst_blkcnt_t);
279     DEFTYPE("wst-nlink-t", wst_nlink_t);
280     DEFTYPE("wst-uid-t", wst_uid_t);
281     DEFTYPE("wst-gid-t", wst_gid_t);
282     printf("\n");
283
284     printf(";;; fcntl.h (or unistd.h on OpenBSD and NetBSD)\n");
285     defconstant("r_ok", R_OK);
286     defconstant("w_ok", W_OK);
287     defconstant("x_ok", X_OK);
288     defconstant("f_ok", F_OK);
289     printf("\n");
290
291     printf(";;; fcntlbits.h\n");
292     defconstant("o_rdonly",  O_RDONLY);
293     defconstant("o_wronly",  O_WRONLY);
294     defconstant("o_rdwr",    O_RDWR);
295     defconstant("o_accmode", O_ACCMODE);
296     defconstant("o_creat",   O_CREAT);
297     defconstant("o_excl",    O_EXCL);
298     defconstant("o_noctty",  O_NOCTTY);
299     defconstant("o_trunc",   O_TRUNC);
300     defconstant("o_append",  O_APPEND);
301 #ifdef LISP_FEATURE_LARGEFILE
302     defconstant("o_largefile", O_LARGEFILE);
303 #endif
304
305     printf(";;;\n");
306     defconstant("s-ifmt",  S_IFMT);
307     defconstant("s-ififo", S_IFIFO);
308     defconstant("s-ifchr", S_IFCHR);
309     defconstant("s-ifdir", S_IFDIR);
310     defconstant("s-ifblk", S_IFBLK);
311     defconstant("s-ifreg", S_IFREG);
312     printf("\n");
313
314     defconstant("s-iflnk",  S_IFLNK);
315     defconstant("s-ifsock", S_IFSOCK);
316     printf("\n");
317
318     printf(";;; error numbers\n");
319     deferrno("ebadf", EBADF);
320     deferrno("enoent", ENOENT);
321     deferrno("eintr", EINTR);
322     deferrno("eagain", EAGAIN);
323     deferrno("eio", EIO);
324     deferrno("eexist", EEXIST);
325     deferrno("eloop", ELOOP);
326     deferrno("espipe", ESPIPE);
327     deferrno("ewouldblock", EWOULDBLOCK);
328     printf("\n");
329
330     printf(";;; for wait3(2) in run-program.lisp\n");
331     defconstant("wnohang", WNOHANG);
332     defconstant("wuntraced", WUNTRACED);
333     printf("\n");
334
335     printf(";;; various ioctl(2) flags\n");
336     defconstant("tiocgpgrp",  TIOCGPGRP);
337     defconstant("tiocspgrp",  TIOCSPGRP);
338     defconstant("tiocgwinsz", TIOCGWINSZ);
339     defconstant("tiocswinsz", TIOCSWINSZ);
340     /* KLUDGE: These are referenced by old CMUCL-derived code, but
341      * Linux doesn't define them.
342      *
343      * I think these are the BSD names, but I don't know what the
344      * corresponding SysV/Linux names are. As a point of reference,
345      * CMUCL doesn't have these defined either (although the defining
346      * forms *do* exist in src/code/unix.lisp), so I don't feel nearly
347      * so bad about not hunting them down. Insight into renamed
348      * obscure ioctl(2) flags appreciated. --njf, 2002-08-26
349      *
350      * I note that the first one I grepped for, TIOCSIGSEND, is
351      * referenced in SBCL conditional on #+HPUX. Maybe the porters of
352      * Oxbridge know more about things like that? And even if they
353      * don't, one benefit of the Rhodes crusade to heal the worthy
354      * ports should be that afterwards, if we grep for something like
355      * this in CVS and it's not there, we can lightheartedly nuke it.
356      * -- WHN 2002-08-30 */
357     /*
358       defconstant("tiocsigsend", TIOCSIGSEND);
359       defconstant("tiocflush", TIOCFLUSH);
360       defconstant("tiocgetp", TIOCGETP);
361       defconstant("tiocsetp", TIOCSETP);
362       defconstant("tiocgetc", TIOCGETC);
363       defconstant("tiocsetc", TIOCSETC);
364       defconstant("tiocgltc", TIOCGLTC);
365       defconstant("tiocsltc", TIOCSLTC);
366     */
367     printf("\n");
368
369     printf(";;; signals\n");
370     defconstant("sig-dfl", (unsigned long)SIG_DFL);
371     defconstant("sig-ign", (unsigned long)SIG_IGN);
372
373     defsignal("sigalrm", SIGALRM);
374     defsignal("sigbus", SIGBUS);
375     defsignal("sigchld", SIGCHLD);
376     defsignal("sigcont", SIGCONT);
377 #ifdef SIGEMT
378     defsignal("sigemt", SIGEMT);
379 #endif
380     defsignal("sigfpe", SIGFPE);
381     defsignal("sighup", SIGHUP);
382     defsignal("sigill", SIGILL);
383     defsignal("sigint", SIGINT);
384     defsignal("sigio", SIGIO);
385     defsignal("sigiot", SIGIOT);
386     defsignal("sigkill", SIGKILL);
387     defsignal("sigpipe", SIGPIPE);
388     defsignal("sigprof", SIGPROF);
389     defsignal("sigquit", SIGQUIT);
390     defsignal("sigsegv", SIGSEGV);
391 #ifdef SIGSTKFLT
392     defsignal("sigstkflt", SIGSTKFLT);
393 #endif
394     defsignal("sigstop", SIGSTOP);
395 #ifdef SIGSYS
396     defsignal("sigsys", SIGSYS);
397 #endif
398     defsignal("sigterm", SIGTERM);
399     defsignal("sigtrap", SIGTRAP);
400     defsignal("sigtstp", SIGTSTP);
401     defsignal("sigttin", SIGTTIN);
402     defsignal("sigttou", SIGTTOU);
403     defsignal("sigurg", SIGURG);
404     defsignal("sigusr1", SIGUSR1);
405     defsignal("sigusr2", SIGUSR2);
406     defsignal("sigvtalrm", SIGVTALRM);
407 #ifdef SIGWAITING
408     defsignal("sigwaiting", SIGWAITING);
409 #endif
410     defsignal("sigwinch", SIGWINCH);
411 #ifdef SIGXCPU
412     defsignal("sigxcpu", SIGXCPU);
413 #endif
414 #ifdef SIGXFSZ
415     defsignal("sigxfsz", SIGXFSZ);
416 #endif
417
418    /* Floating point exception codes. Some of these
419     * are missing on Darwin. */
420 #ifdef FPE_INTOVF
421     defconstant("fpe-intovf", FPE_INTOVF);
422 #else
423     defconstant("fpe-intovf", -1);
424 #endif
425 #ifdef FPE_INTDIV
426     defconstant("fpe-intdiv", FPE_INTDIV);
427 #else
428     defconstant("fpe-intdiv", -1);
429 #endif
430     defconstant("fpe-fltdiv", FPE_FLTDIV);
431     defconstant("fpe-fltovf", FPE_FLTOVF);
432     defconstant("fpe-fltund", FPE_FLTUND);
433     defconstant("fpe-fltres", FPE_FLTRES);
434     defconstant("fpe-fltinv", FPE_FLTINV);
435 #ifdef FPE_FLTSUB
436     defconstant("fpe-fltsub", FPE_FLTSUB);
437 #else
438     defconstant("fpe-fltsub", -1);
439 #endif
440 #endif // !WIN32
441     printf("\n");
442
443 #ifdef LISP_FEATURE_BSD
444     printf(";;; sysctl(3) names\n");
445     printf("(in-package \"SB!IMPL\")\n");
446     defconstant("ctl-kern", CTL_KERN);
447     defconstant("ctl-hw", CTL_HW);
448     defconstant("ctl-maxname", CTL_MAXNAME);
449     defconstant("kern-ostype", KERN_OSTYPE);
450     defconstant("kern-osrelease", KERN_OSRELEASE);
451     defconstant("hw-model", HW_MODEL);
452     defconstant("hw-pagesize", HW_PAGESIZE);
453     printf("\n");
454 #endif
455
456 #ifdef LISP_FEATURE_GENCGC
457     printf(";;; GENCGC related\n");
458     printf("(in-package \"SB!KERNEL\")\n");
459     DEFTYPE("page-index-t", page_index_t);
460     DEFTYPE("generation-index-t", generation_index_t);
461     printf("\n");
462 #endif
463
464     return 0;
465 }