1 /* Simple tool to create config.h.
2 * Would be much easier with ccan modules, but deliberately standalone.
4 * Copyright 2011 Rusty Russell <rusty@rustcorp.com.au>. MIT license.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 #include <sys/types.h>
35 #define DEFAULT_COMPILER "cc"
36 #define DEFAULT_FLAGS "-g3 -ggdb -Wall -Wundef -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes -Wold-style-definition"
38 #define OUTPUT_FILE "configurator.out"
39 #define INPUT_FILE "configuratortest.c"
47 DEFINES_EVERYTHING = 0x8,
48 MAY_NOT_COMPILE = 0x10,
54 enum test_style style;
62 static struct test tests[] = {
63 { "HAVE_32BIT_OFF_T", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
64 "#include <sys/types.h>\n"
65 "int main(int argc, char *argv[]) {\n"
66 " return sizeof(off_t) == 4 ? 0 : 1;\n"
68 { "HAVE_ALIGNOF", INSIDE_MAIN, NULL, NULL,
69 "return __alignof__(double) > 0 ? 0 : 1;" },
70 { "HAVE_ASPRINTF", DEFINES_FUNC, NULL, NULL,
71 "#define _GNU_SOURCE\n"
72 "#include <stdio.h>\n"
73 "static char *func(int x) {"
75 " if (asprintf(&p, \"%u\", x) == -1) p = NULL;"
78 { "HAVE_ATTRIBUTE_COLD", DEFINES_FUNC, NULL, NULL,
79 "static int __attribute__((cold)) func(int x) { return x; }" },
80 { "HAVE_ATTRIBUTE_CONST", DEFINES_FUNC, NULL, NULL,
81 "static int __attribute__((const)) func(int x) { return x; }" },
82 { "HAVE_ATTRIBUTE_MAY_ALIAS", OUTSIDE_MAIN, NULL, NULL,
83 "typedef short __attribute__((__may_alias__)) short_a;" },
84 { "HAVE_ATTRIBUTE_NORETURN", DEFINES_FUNC, NULL, NULL,
85 "#include <stdlib.h>\n"
86 "static void __attribute__((noreturn)) func(int x) { exit(x); }" },
87 { "HAVE_ATTRIBUTE_PRINTF", DEFINES_FUNC, NULL, NULL,
88 "static void __attribute__((format(__printf__, 1, 2))) func(const char *fmt, ...) { }" },
89 { "HAVE_ATTRIBUTE_UNUSED", OUTSIDE_MAIN, NULL, NULL,
90 "static int __attribute__((unused)) func(int x) { return x; }" },
91 { "HAVE_ATTRIBUTE_USED", OUTSIDE_MAIN, NULL, NULL,
92 "static int __attribute__((used)) func(int x) { return x; }" },
93 { "HAVE_BACKTRACE", DEFINES_FUNC, NULL, NULL,
94 "#include <execinfo.h>\n"
95 "static int func(int x) {"
97 " return backtrace(bt, 10) < x;\n"
99 { "HAVE_BIG_ENDIAN", INSIDE_MAIN|EXECUTE, NULL, NULL,
100 "union { int i; char c[sizeof(int)]; } u;\n"
101 "u.i = 0x01020304;\n"
102 "return u.c[0] == 0x01 && u.c[1] == 0x02 && u.c[2] == 0x03 && u.c[3] == 0x04 ? 0 : 1;" },
103 { "HAVE_BSWAP_64", DEFINES_FUNC, "HAVE_BYTESWAP_H", NULL,
104 "#include <byteswap.h>\n"
105 "static int func(int x) { return bswap_64(x); }" },
106 { "HAVE_BUILTIN_CHOOSE_EXPR", INSIDE_MAIN, NULL, NULL,
107 "return __builtin_choose_expr(1, 0, \"garbage\");" },
108 { "HAVE_BUILTIN_CLZ", INSIDE_MAIN, NULL, NULL,
109 "return __builtin_clz(1) == (sizeof(int)*8 - 1) ? 0 : 1;" },
110 { "HAVE_BUILTIN_CLZL", INSIDE_MAIN, NULL, NULL,
111 "return __builtin_clzl(1) == (sizeof(long)*8 - 1) ? 0 : 1;" },
112 { "HAVE_BUILTIN_CLZLL", INSIDE_MAIN, NULL, NULL,
113 "return __builtin_clzll(1) == (sizeof(long long)*8 - 1) ? 0 : 1;" },
114 { "HAVE_BUILTIN_CONSTANT_P", INSIDE_MAIN, NULL, NULL,
115 "return __builtin_constant_p(1) ? 0 : 1;" },
116 { "HAVE_BUILTIN_EXPECT", INSIDE_MAIN, NULL, NULL,
117 "return __builtin_expect(argc == 1, 1) ? 0 : 1;" },
118 { "HAVE_BUILTIN_FFSL", INSIDE_MAIN, NULL, NULL,
119 "return __builtin_ffsl(0L) == 0 ? 0 : 1;" },
120 { "HAVE_BUILTIN_FFSLL", INSIDE_MAIN, NULL, NULL,
121 "return __builtin_ffsll(0LL) == 0 ? 0 : 1;" },
122 { "HAVE_BUILTIN_POPCOUNTL", INSIDE_MAIN, NULL, NULL,
123 "return __builtin_popcountl(255L) == 8 ? 0 : 1;" },
124 { "HAVE_BUILTIN_TYPES_COMPATIBLE_P", INSIDE_MAIN, NULL, NULL,
125 "return __builtin_types_compatible_p(char *, int) ? 1 : 0;" },
126 { "HAVE_BYTESWAP_H", OUTSIDE_MAIN, NULL, NULL,
127 "#include <byteswap.h>\n" },
128 { "HAVE_CLOCK_GETTIME",
129 DEFINES_FUNC, "HAVE_STRUCT_TIMESPEC", NULL,
130 "#include <time.h>\n"
131 "static struct timespec func(void) {\n"
132 " struct timespec ts;\n"
133 " clock_gettime(CLOCK_REALTIME, &ts);\n"
136 { "HAVE_CLOCK_GETTIME_IN_LIBRT",
138 "HAVE_STRUCT_TIMESPEC !HAVE_CLOCK_GETTIME",
140 "#include <time.h>\n"
141 "static struct timespec func(void) {\n"
142 " struct timespec ts;\n"
143 " clock_gettime(CLOCK_REALTIME, &ts);\n"
146 { "HAVE_COMPOUND_LITERALS", INSIDE_MAIN, NULL, NULL,
147 "int *foo = (int[]) { 1, 2, 3, 4 };\n"
148 "return foo[0] ? 0 : 1;" },
149 { "HAVE_FCHDIR", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
150 "#include <sys/types.h>\n"
151 "#include <sys/stat.h>\n"
152 "#include <fcntl.h>\n"
153 "#include <unistd.h>\n"
155 " int fd = open(\"..\", O_RDONLY);\n"
156 " return fchdir(fd) == 0 ? 0 : 1;\n"
158 { "HAVE_ERR_H", DEFINES_FUNC, NULL, NULL,
160 "static void func(int arg) {\n"
162 " err(1, \"err %u\", arg);\n"
164 " errx(1, \"err %u\", arg);\n"
166 " warn(\"warn %u\", arg);\n"
168 " warnx(\"warn %u\", arg);\n"
170 { "HAVE_FILE_OFFSET_BITS", DEFINES_EVERYTHING|EXECUTE,
171 "HAVE_32BIT_OFF_T", NULL,
172 "#define _FILE_OFFSET_BITS 64\n"
173 "#include <sys/types.h>\n"
174 "int main(int argc, char *argv[]) {\n"
175 " return sizeof(off_t) == 8 ? 0 : 1;\n"
177 { "HAVE_FOR_LOOP_DECLARATION", INSIDE_MAIN, NULL, NULL,
178 "for (int i = 0; i < argc; i++) { return 0; };\n"
180 { "HAVE_FLEXIBLE_ARRAY_MEMBER", OUTSIDE_MAIN, NULL, NULL,
181 "struct foo { unsigned int x; int arr[]; };" },
182 { "HAVE_GETPAGESIZE", DEFINES_FUNC, NULL, NULL,
183 "#include <unistd.h>\n"
184 "static int func(void) { return getpagesize(); }" },
185 { "HAVE_ISBLANK", DEFINES_FUNC, NULL, NULL,
186 "#define _GNU_SOURCE\n"
187 "#include <ctype.h>\n"
188 "static int func(void) { return isblank(' '); }" },
189 { "HAVE_LITTLE_ENDIAN", INSIDE_MAIN|EXECUTE, NULL, NULL,
190 "union { int i; char c[sizeof(int)]; } u;\n"
191 "u.i = 0x01020304;\n"
192 "return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;" },
193 { "HAVE_MEMMEM", DEFINES_FUNC, NULL, NULL,
194 "#define _GNU_SOURCE\n"
195 "#include <string.h>\n"
196 "static void *func(void *h, size_t hl, void *n, size_t nl) {\n"
197 "return memmem(h, hl, n, nl);"
199 { "HAVE_MMAP", DEFINES_FUNC, NULL, NULL,
200 "#include <sys/mman.h>\n"
201 "static void *func(int fd) {\n"
202 " return mmap(0, 65536, PROT_READ, MAP_SHARED, fd, 0);\n"
204 { "HAVE_PROC_SELF_MAPS", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
205 "#include <sys/types.h>\n"
206 "#include <sys/stat.h>\n"
207 "#include <fcntl.h>\n"
209 " return open(\"/proc/self/maps\", O_RDONLY) != -1 ? 0 : 1;\n"
211 { "HAVE_QSORT_R_PRIVATE_LAST",
212 DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE, NULL, NULL,
213 "#define _GNU_SOURCE 1\n"
214 "#include <stdlib.h>\n"
215 "static int cmp(const void *lp, const void *rp, void *priv) {\n"
216 " *(unsigned int *)priv = 1;\n"
217 " return *(const int *)lp - *(const int *)rp; }\n"
219 " int array[] = { 9, 2, 5 };\n"
220 " unsigned int called = 0;\n"
221 " qsort_r(array, 3, sizeof(int), cmp, &called);\n"
222 " return called && array[0] == 2 && array[1] == 5 && array[2] == 9 ? 0 : 1;\n"
224 { "HAVE_STRUCT_TIMESPEC",
225 DEFINES_FUNC, NULL, NULL,
226 "#include <time.h>\n"
227 "static void func(void) {\n"
228 " struct timespec ts;\n"
229 " ts.tv_sec = ts.tv_nsec = 1;\n"
231 { "HAVE_SECTION_START_STOP",
232 DEFINES_FUNC, NULL, NULL,
233 "static void *__attribute__((__section__(\"mysec\"))) p = &p;\n"
234 "static int func(void) {\n"
235 " extern void *__start_mysec[], *__stop_mysec[];\n"
236 " return __stop_mysec - __start_mysec;\n"
238 { "HAVE_STACK_GROWS_UPWARDS", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
239 "static long nest(const void *base, unsigned int i)\n"
242 " return (const char *)&i - (const char *)base;\n"
243 " return nest(base, i-1);\n"
245 "int main(int argc, char *argv[]) {\n"
246 " return (nest(&argc, argc) > 0) ? 0 : 1\n;"
248 { "HAVE_STATEMENT_EXPR", INSIDE_MAIN, NULL, NULL,
249 "return ({ int x = argc; x == argc ? 0 : 1; });" },
250 { "HAVE_SYS_FILIO_H", OUTSIDE_MAIN, NULL, NULL, /* Solaris needs this for FIONREAD */
251 "#include <sys/filio.h>\n" },
252 { "HAVE_TYPEOF", INSIDE_MAIN, NULL, NULL,
253 "__typeof__(argc) i; i = argc; return i == argc ? 0 : 1;" },
254 { "HAVE_UTIME", DEFINES_FUNC, NULL, NULL,
255 "#include <sys/types.h>\n"
256 "#include <utime.h>\n"
257 "static int func(const char *filename) {\n"
258 " struct utimbuf times = { 0 };\n"
259 " return utime(filename, ×);\n"
261 { "HAVE_WARN_UNUSED_RESULT", DEFINES_FUNC, NULL, NULL,
262 "#include <sys/types.h>\n"
263 "#include <utime.h>\n"
264 "static __attribute__((warn_unused_result)) int func(int i) {\n"
269 static char *grab_fd(int fd)
272 size_t max, size = 0;
276 buffer = malloc(max+1);
277 while ((ret = read(fd, buffer + size, max - size)) > 0) {
280 buffer = realloc(buffer, max *= 2);
283 err(1, "reading from command");
288 static char *run(const char *cmd, int *exitstatus)
296 err(1, "creating pipe");
303 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
304 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
306 || close(STDIN_FILENO) != 0
307 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
310 status = system(cmd);
311 if (WIFEXITED(status))
312 exit(WEXITSTATUS(status));
313 /* Here's a hint... */
314 exit(128 + WTERMSIG(status));
319 /* This shouldn't fail... */
320 if (waitpid(pid, &status, 0) != pid)
321 err(1, "Failed to wait for child");
323 if (WIFEXITED(status))
324 *exitstatus = WEXITSTATUS(status);
326 *exitstatus = -WTERMSIG(status);
330 static char *connect_args(const char *argv[], const char *extra)
332 unsigned int i, len = strlen(extra) + 1;
335 for (i = 1; argv[i]; i++)
336 len += 1 + strlen(argv[i]);
340 for (i = 1; argv[i]; i++) {
341 strcpy(ret + len, argv[i]);
342 len += strlen(argv[i]);
345 strcpy(ret + len, extra);
349 static struct test *find_test(const char *name)
353 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
354 if (strcmp(tests[i].name, name) == 0)
360 #define PRE_BOILERPLATE "/* Test program generated by configurator. */\n"
361 #define MAIN_START_BOILERPLATE "int main(int argc, char *argv[]) {\n"
362 #define USE_FUNC_BOILERPLATE "(void)func;\n"
363 #define MAIN_BODY_BOILERPLATE "return 0;\n"
364 #define MAIN_END_BOILERPLATE "}\n"
366 static bool run_test(const char *cmd, struct test *test)
377 const char *deps = test->depends;
380 /* Space-separated dependencies, could be ! for inverse. */
381 while ((len = strcspn(deps, " "))) {
382 bool positive = true;
394 if (run_test(cmd, find_test(dep)) != positive) {
395 test->answer = false;
400 deps += strspn(deps, " ");
404 outf = fopen(INPUT_FILE, "w");
406 err(1, "creating %s", INPUT_FILE);
408 fprintf(outf, "%s", PRE_BOILERPLATE);
409 switch (test->style & ~(EXECUTE|MAY_NOT_COMPILE)) {
411 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
412 fprintf(outf, "%s", test->fragment);
413 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
416 fprintf(outf, "%s", test->fragment);
417 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
418 fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
419 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
422 fprintf(outf, "%s", test->fragment);
423 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
424 fprintf(outf, "%s", USE_FUNC_BOILERPLATE);
425 fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
426 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
428 case DEFINES_EVERYTHING:
429 fprintf(outf, "%s", test->fragment);
438 if (system("cat " INPUT_FILE) == -1);
442 newcmd = malloc(strlen(cmd) + strlen(" ")
443 + strlen(test->link) + 1);
444 sprintf(newcmd, "%s %s", cmd, test->link);
446 printf("Extra link line: %s", newcmd);
450 output = run(cmd, &status);
451 if (status != 0 || strstr(output, "warning")) {
453 printf("Compile %s for %s, status %i: %s\n",
454 status ? "fail" : "warning",
455 test->name, status, output);
456 if ((test->style & EXECUTE) && !(test->style & MAY_NOT_COMPILE))
457 errx(1, "Test for %s did not compile:\n%s",
459 test->answer = false;
462 /* Compile succeeded. */
464 /* We run INSIDE_MAIN tests for sanity checking. */
465 if ((test->style & EXECUTE) || (test->style & INSIDE_MAIN)) {
466 output = run("./" OUTPUT_FILE, &status);
467 if (!(test->style & EXECUTE) && status != 0)
468 errx(1, "Test for %s failed with %i:\n%s",
469 test->name, status, output);
470 if (verbose && status)
471 printf("%s exited %i\n", test->name, status);
474 test->answer = (status == 0);
480 int main(int argc, const char *argv[])
484 const char *default_args[]
485 = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL };
488 if (strcmp(argv[1], "--help") == 0) {
489 printf("Usage: configurator [-v] [<compiler> <flags>...]\n"
490 " <compiler> <flags> will have \"-o <outfile> <infile.c>\" appended\n"
492 DEFAULT_COMPILER, DEFAULT_FLAGS);
495 if (strcmp(argv[1], "-v") == 0) {
499 } else if (strcmp(argv[1], "-vv") == 0) {
509 cmd = connect_args(argv, "-o " OUTPUT_FILE " " INPUT_FILE);
510 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
511 run_test(cmd, &tests[i]);
516 cmd[strlen(cmd) - strlen(" -o " OUTPUT_FILE " " INPUT_FILE)] = '\0';
517 printf("/* Generated by CCAN configurator */\n"
518 "#ifndef CCAN_CONFIG_H\n"
519 "#define CCAN_CONFIG_H\n");
520 printf("#ifndef _GNU_SOURCE\n");
521 printf("#define _GNU_SOURCE /* Always use GNU extensions. */\n");
523 printf("#define CCAN_COMPILER \"%s\"\n", argv[1]);
524 printf("#define CCAN_CFLAGS \"%s\"\n\n", cmd + strlen(argv[1]) + 1);
525 /* This one implies "#include <ccan/..." works, eg. for tdb2.h */
526 printf("#define HAVE_CCAN 1\n");
527 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
528 printf("#define %s %u\n", tests[i].name, tests[i].answer);
529 printf("#endif /* CCAN_CONFIG_H */\n");