]> git.ozlabs.org Git - ccan/blob - tools/configurator/configurator.c
configurator: Add test for ucontext.h
[ccan] / tools / configurator / configurator.c
1 /* Simple tool to create config.h.
2  * Would be much easier with ccan modules, but deliberately standalone.
3  *
4  * Copyright 2011 Rusty Russell <rusty@rustcorp.com.au>.  MIT license.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  */
24 #include <stdio.h>
25 #include <stdbool.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <err.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <string.h>
34
35 #define DEFAULT_COMPILER "cc"
36 #define DEFAULT_FLAGS "-g3 -ggdb -Wall -Wundef -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes -Wold-style-definition"
37
38 #define OUTPUT_FILE "configurator.out"
39 #define INPUT_FILE "configuratortest.c"
40
41 static int verbose;
42
43 enum test_style {
44         OUTSIDE_MAIN            = 0x1,
45         DEFINES_FUNC            = 0x2,
46         INSIDE_MAIN             = 0x4,
47         DEFINES_EVERYTHING      = 0x8,
48         MAY_NOT_COMPILE         = 0x10,
49         EXECUTE                 = 0x8000
50 };
51
52 struct test {
53         const char *name;
54         enum test_style style;
55         const char *depends;
56         const char *link;
57         const char *fragment;
58         const char *flags;
59         const char *overrides; /* On success, force this to '1' */
60         bool done;
61         bool answer;
62 };
63
64 static struct test tests[] = {
65         { "HAVE_32BIT_OFF_T", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
66           "#include <sys/types.h>\n"
67           "int main(void) {\n"
68           "     return sizeof(off_t) == 4 ? 0 : 1;\n"
69           "}\n" },
70         { "HAVE_ALIGNOF", INSIDE_MAIN, NULL, NULL,
71           "return __alignof__(double) > 0 ? 0 : 1;" },
72         { "HAVE_ASPRINTF", DEFINES_FUNC, NULL, NULL,
73           "#ifndef _GNU_SOURCE\n"
74           "#define _GNU_SOURCE\n"
75           "#endif\n"
76           "#include <stdio.h>\n"
77           "static char *func(int x) {"
78           "     char *p;\n"
79           "     if (asprintf(&p, \"%u\", x) == -1) p = NULL;"
80           "     return p;\n"
81           "}" },
82         { "HAVE_ATTRIBUTE_COLD", DEFINES_FUNC, NULL, NULL,
83           "static int __attribute__((cold)) func(int x) { return x; }" },
84         { "HAVE_ATTRIBUTE_CONST", DEFINES_FUNC, NULL, NULL,
85           "static int __attribute__((const)) func(int x) { return x; }" },
86         { "HAVE_ATTRIBUTE_PURE", DEFINES_FUNC, NULL, NULL,
87           "static int __attribute__((pure)) func(int x) { return x; }" },
88         { "HAVE_ATTRIBUTE_MAY_ALIAS", OUTSIDE_MAIN, NULL, NULL,
89           "typedef short __attribute__((__may_alias__)) short_a;" },
90         { "HAVE_ATTRIBUTE_NORETURN", DEFINES_FUNC, NULL, NULL,
91           "#include <stdlib.h>\n"
92           "static void __attribute__((noreturn)) func(int x) { exit(x); }" },
93         { "HAVE_ATTRIBUTE_PRINTF", DEFINES_FUNC, NULL, NULL,
94           "static void __attribute__((format(__printf__, 1, 2))) func(const char *fmt, ...) { (void)fmt; }" },
95         { "HAVE_ATTRIBUTE_UNUSED", OUTSIDE_MAIN, NULL, NULL,
96           "static int __attribute__((unused)) func(int x) { return x; }" },
97         { "HAVE_ATTRIBUTE_USED", OUTSIDE_MAIN, NULL, NULL,
98           "static int __attribute__((used)) func(int x) { return x; }" },
99         { "HAVE_BACKTRACE", DEFINES_FUNC, NULL, NULL,
100           "#include <execinfo.h>\n"
101           "static int func(int x) {"
102           "     void *bt[10];\n"
103           "     return backtrace(bt, 10) < x;\n"
104           "}" },
105         { "HAVE_BIG_ENDIAN", INSIDE_MAIN|EXECUTE, NULL, NULL,
106           "union { int i; char c[sizeof(int)]; } u;\n"
107           "u.i = 0x01020304;\n"
108           "return u.c[0] == 0x01 && u.c[1] == 0x02 && u.c[2] == 0x03 && u.c[3] == 0x04 ? 0 : 1;" },
109         { "HAVE_BSWAP_64", DEFINES_FUNC, "HAVE_BYTESWAP_H", NULL,
110           "#include <byteswap.h>\n"
111           "static int func(int x) { return bswap_64(x); }" },
112         { "HAVE_BUILTIN_CHOOSE_EXPR", INSIDE_MAIN, NULL, NULL,
113           "return __builtin_choose_expr(1, 0, \"garbage\");" },
114         { "HAVE_BUILTIN_CLZ", INSIDE_MAIN, NULL, NULL,
115           "return __builtin_clz(1) == (sizeof(int)*8 - 1) ? 0 : 1;" },
116         { "HAVE_BUILTIN_CLZL", INSIDE_MAIN, NULL, NULL,
117           "return __builtin_clzl(1) == (sizeof(long)*8 - 1) ? 0 : 1;" },
118         { "HAVE_BUILTIN_CLZLL", INSIDE_MAIN, NULL, NULL,
119           "return __builtin_clzll(1) == (sizeof(long long)*8 - 1) ? 0 : 1;" },
120         { "HAVE_BUILTIN_CTZ", INSIDE_MAIN, NULL, NULL,
121           "return __builtin_ctz(1 << (sizeof(int)*8 - 1)) == (sizeof(int)*8 - 1) ? 0 : 1;" },
122         { "HAVE_BUILTIN_CTZL", INSIDE_MAIN, NULL, NULL,
123           "return __builtin_ctzl(1UL << (sizeof(long)*8 - 1)) == (sizeof(long)*8 - 1) ? 0 : 1;" },
124         { "HAVE_BUILTIN_CTZLL", INSIDE_MAIN, NULL, NULL,
125           "return __builtin_ctzll(1ULL << (sizeof(long long)*8 - 1)) == (sizeof(long long)*8 - 1) ? 0 : 1;" },
126         { "HAVE_BUILTIN_CONSTANT_P", INSIDE_MAIN, NULL, NULL,
127           "return __builtin_constant_p(1) ? 0 : 1;" },
128         { "HAVE_BUILTIN_EXPECT", INSIDE_MAIN, NULL, NULL,
129           "return __builtin_expect(argc == 1, 1) ? 0 : 1;" },
130         { "HAVE_BUILTIN_FFS", INSIDE_MAIN, NULL, NULL,
131           "return __builtin_ffs(0) == 0 ? 0 : 1;" },
132         { "HAVE_BUILTIN_FFSL", INSIDE_MAIN, NULL, NULL,
133           "return __builtin_ffsl(0L) == 0 ? 0 : 1;" },
134         { "HAVE_BUILTIN_FFSLL", INSIDE_MAIN, NULL, NULL,
135           "return __builtin_ffsll(0LL) == 0 ? 0 : 1;" },
136         { "HAVE_BUILTIN_POPCOUNTL", INSIDE_MAIN, NULL, NULL,
137           "return __builtin_popcountl(255L) == 8 ? 0 : 1;" },
138         { "HAVE_BUILTIN_TYPES_COMPATIBLE_P", INSIDE_MAIN, NULL, NULL,
139           "return __builtin_types_compatible_p(char *, int) ? 1 : 0;" },
140         { "HAVE_ICCARM_INTRINSICS", DEFINES_FUNC, NULL, NULL,
141           "#include <intrinsics.h>\n"
142           "int func(int v) {\n"
143           "     return __CLZ(__RBIT(v));\n"
144           "}" },
145         { "HAVE_BYTESWAP_H", OUTSIDE_MAIN, NULL, NULL,
146           "#include <byteswap.h>\n" },
147         { "HAVE_CLOCK_GETTIME",
148           DEFINES_FUNC, "HAVE_STRUCT_TIMESPEC", NULL,
149           "#include <time.h>\n"
150           "static struct timespec func(void) {\n"
151           "     struct timespec ts;\n"
152           "     clock_gettime(CLOCK_REALTIME, &ts);\n"
153           "     return ts;\n"
154           "}\n" },
155         { "HAVE_CLOCK_GETTIME_IN_LIBRT",
156           DEFINES_FUNC,
157           "HAVE_STRUCT_TIMESPEC !HAVE_CLOCK_GETTIME",
158           "-lrt",
159           "#include <time.h>\n"
160           "static struct timespec func(void) {\n"
161           "     struct timespec ts;\n"
162           "     clock_gettime(CLOCK_REALTIME, &ts);\n"
163           "     return ts;\n"
164           "}\n",
165           /* This means HAVE_CLOCK_GETTIME, too */
166           "HAVE_CLOCK_GETTIME" },
167         { "HAVE_COMPOUND_LITERALS", INSIDE_MAIN, NULL, NULL,
168           "int *foo = (int[]) { 1, 2, 3, 4 };\n"
169           "return foo[0] ? 0 : 1;" },
170         { "HAVE_FCHDIR", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
171           "#include <sys/types.h>\n"
172           "#include <sys/stat.h>\n"
173           "#include <fcntl.h>\n"
174           "#include <unistd.h>\n"
175           "int main(void) {\n"
176           "     int fd = open(\"..\", O_RDONLY);\n"
177           "     return fchdir(fd) == 0 ? 0 : 1;\n"
178           "}\n" },
179         { "HAVE_ERR_H", DEFINES_FUNC, NULL, NULL,
180           "#include <err.h>\n"
181           "static void func(int arg) {\n"
182           "     if (arg == 0)\n"
183           "             err(1, \"err %u\", arg);\n"
184           "     if (arg == 1)\n"
185           "             errx(1, \"err %u\", arg);\n"
186           "     if (arg == 3)\n"
187           "             warn(\"warn %u\", arg);\n"
188           "     if (arg == 4)\n"
189           "             warnx(\"warn %u\", arg);\n"
190           "}\n" },
191         { "HAVE_FILE_OFFSET_BITS", DEFINES_EVERYTHING|EXECUTE,
192           "HAVE_32BIT_OFF_T", NULL,
193           "#define _FILE_OFFSET_BITS 64\n"
194           "#include <sys/types.h>\n"
195           "int main(void) {\n"
196           "     return sizeof(off_t) == 8 ? 0 : 1;\n"
197           "}\n" },
198         { "HAVE_FOR_LOOP_DECLARATION", INSIDE_MAIN, NULL, NULL,
199           "for (int i = 0; i < argc; i++) { return 0; };\n"
200           "return 1;" },
201         { "HAVE_FLEXIBLE_ARRAY_MEMBER", OUTSIDE_MAIN, NULL, NULL,
202           "struct foo { unsigned int x; int arr[]; };" },
203         { "HAVE_GETPAGESIZE", DEFINES_FUNC, NULL, NULL,
204           "#include <unistd.h>\n"
205           "static int func(void) { return getpagesize(); }" },
206         { "HAVE_ISBLANK", DEFINES_FUNC, NULL, NULL,
207           "#ifndef _GNU_SOURCE\n"
208           "#define _GNU_SOURCE\n"
209           "#endif\n"
210           "#include <ctype.h>\n"
211           "static int func(void) { return isblank(' '); }" },
212         { "HAVE_LITTLE_ENDIAN", INSIDE_MAIN|EXECUTE, NULL, NULL,
213           "union { int i; char c[sizeof(int)]; } u;\n"
214           "u.i = 0x01020304;\n"
215           "return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;" },
216         { "HAVE_MEMMEM", DEFINES_FUNC, NULL, NULL,
217           "#ifndef _GNU_SOURCE\n"
218           "#define _GNU_SOURCE\n"
219           "#endif\n"
220           "#include <string.h>\n"
221           "static void *func(void *h, size_t hl, void *n, size_t nl) {\n"
222           "return memmem(h, hl, n, nl);"
223           "}\n", },
224         { "HAVE_MEMRCHR", DEFINES_FUNC, NULL, NULL,
225           "#ifndef _GNU_SOURCE\n"
226           "#define _GNU_SOURCE\n"
227           "#endif\n"
228           "#include <string.h>\n"
229           "static void *func(void *s, int c, size_t n) {\n"
230           "return memrchr(s, c, n);"
231           "}\n", },
232         { "HAVE_MMAP", DEFINES_FUNC, NULL, NULL,
233           "#include <sys/mman.h>\n"
234           "static void *func(int fd) {\n"
235           "     return mmap(0, 65536, PROT_READ, MAP_SHARED, fd, 0);\n"
236           "}" },
237         { "HAVE_PROC_SELF_MAPS", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
238           "#include <sys/types.h>\n"
239           "#include <sys/stat.h>\n"
240           "#include <fcntl.h>\n"
241           "int main(void) {\n"
242           "     return open(\"/proc/self/maps\", O_RDONLY) != -1 ? 0 : 1;\n"
243           "}\n" },
244         { "HAVE_QSORT_R_PRIVATE_LAST",
245           DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE, NULL, NULL,
246           "#ifndef _GNU_SOURCE\n"
247           "#define _GNU_SOURCE\n"
248           "#endif\n"
249           "#include <stdlib.h>\n"
250           "static int cmp(const void *lp, const void *rp, void *priv) {\n"
251           " *(unsigned int *)priv = 1;\n"
252           " return *(const int *)lp - *(const int *)rp; }\n"
253           "int main(void) {\n"
254           " int array[] = { 9, 2, 5 };\n"
255           " unsigned int called = 0;\n"
256           " qsort_r(array, 3, sizeof(int), cmp, &called);\n"
257           " return called && array[0] == 2 && array[1] == 5 && array[2] == 9 ? 0 : 1;\n"
258           "}\n" },
259         { "HAVE_STRUCT_TIMESPEC",
260           DEFINES_FUNC, NULL, NULL,
261           "#include <time.h>\n"
262           "static void func(void) {\n"
263           "     struct timespec ts;\n"
264           "     ts.tv_sec = ts.tv_nsec = 1;\n"
265           "}\n" },
266         { "HAVE_SECTION_START_STOP",
267           DEFINES_FUNC, NULL, NULL,
268           "static void *__attribute__((__section__(\"mysec\"))) p = &p;\n"
269           "static int func(void) {\n"
270           "     extern void *__start_mysec[], *__stop_mysec[];\n"
271           "     return __stop_mysec - __start_mysec;\n"
272           "}\n" },
273         { "HAVE_STACK_GROWS_UPWARDS", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
274           "static long nest(const void *base, unsigned int i)\n"
275           "{\n"
276           "     if (i == 0)\n"
277           "             return (const char *)&i - (const char *)base;\n"
278           "     return nest(base, i-1);\n"
279           "}\n"
280           "int main(int argc, char *argv[]) {\n"
281           "     (void)argv;\n"
282           "     return (nest(&argc, argc) > 0) ? 0 : 1;\n"
283           "}\n" },
284         { "HAVE_STATEMENT_EXPR", INSIDE_MAIN, NULL, NULL,
285           "return ({ int x = argc; x == argc ? 0 : 1; });" },
286         { "HAVE_SYS_FILIO_H", OUTSIDE_MAIN, NULL, NULL, /* Solaris needs this for FIONREAD */
287           "#include <sys/filio.h>\n" },
288         { "HAVE_SYS_TERMIOS_H", OUTSIDE_MAIN, NULL, NULL,
289           "#include <sys/termios.h>\n" },
290         { "HAVE_TYPEOF", INSIDE_MAIN, NULL, NULL,
291           "__typeof__(argc) i; i = argc; return i == argc ? 0 : 1;" },
292         { "HAVE_UNALIGNED_ACCESS", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
293           "#include <string.h>\n"
294           "int main(int argc, char *argv[]) {\n"
295           "     (void)argc;\n"
296           "     char pad[sizeof(int *) * 1];\n"
297           "     strncpy(pad, argv[0], sizeof(pad));\n"
298           "     int *x = (int *)pad, *y = (int *)(pad + 1);\n"
299           "     return *x == *y;\n"
300           "}\n" },
301         { "HAVE_UTIME", DEFINES_FUNC, NULL, NULL,
302           "#include <sys/types.h>\n"
303           "#include <utime.h>\n"
304           "static int func(const char *filename) {\n"
305           "     struct utimbuf times = { 0 };\n"
306           "     return utime(filename, &times);\n"
307           "}" },
308         { "HAVE_WARN_UNUSED_RESULT", DEFINES_FUNC, NULL, NULL,
309           "#include <sys/types.h>\n"
310           "#include <utime.h>\n"
311           "static __attribute__((warn_unused_result)) int func(int i) {\n"
312           "     return i + 1;\n"
313           "}" },
314         { "HAVE_OPENMP", INSIDE_MAIN, NULL, NULL,
315           "int i;\n"
316           "#pragma omp parallel for\n"
317           "for(i = 0; i < 0; i++) {};\n"
318           "return 0;\n",
319           "-Werror -fopenmp" },
320         { "HAVE_VALGRIND_MEMCHECK_H", OUTSIDE_MAIN, NULL, NULL,
321           "#include <valgrind/memcheck.h>\n" },
322         { "HAVE_UCONTEXT", DEFINES_EVERYTHING|EXECUTE,
323           NULL, NULL,
324           "#include <ucontext.h>\n"
325           "static int x = 0;\n"
326           "static char stack[1024];\n"
327           "static ucontext_t a, b;\n"
328           "static void fn(void) {\n"
329           "     x |= 2;\n"
330           "     setcontext(&b);\n"
331           "     x |= 4;\n"
332           "}\n"
333           "int main(int argc, char *argv[]) {\n"
334           "     x |= 1;\n"
335           "     getcontext(&a);\n"
336           "     a.uc_stack.ss_sp = stack;\n"
337           "     a.uc_stack.ss_size = sizeof(stack);\n"
338           "     makecontext(&a, fn, 0);\n"
339           "     swapcontext(&b, &a);\n"
340           "     return (x == 3) ? 0 : 1;\n"
341           "}\n"
342         },
343         { "HAVE_POINTER_SAFE_MAKECONTEXT", DEFINES_EVERYTHING|EXECUTE,
344           "HAVE_UCONTEXT", NULL,
345           "#include <stddef.h>\n"
346           "#include <ucontext.h>\n"
347           "static int worked = 0;\n"
348           "static char stack[1024];\n"
349           "static ucontext_t a, b;\n"
350           "static void fn(void *p, void *q) {\n"
351           "     void *cp = &worked;\n"
352           "     void *cq = (void *)(~((ptrdiff_t)cp));\n"
353           "     if ((p == cp) && (q == cq))\n"
354           "             worked = 1;\n"
355           "     setcontext(&b);\n"
356           "}\n"
357           "int main(int argc, char *argv[]) {\n"
358           "     void *ap = &worked;\n"
359           "     void *aq = (void *)(~((ptrdiff_t)ap));\n"
360           "     getcontext(&a);\n"
361           "     a.uc_stack.ss_sp = stack;\n"
362           "     a.uc_stack.ss_size = sizeof(stack);\n"
363           "     makecontext(&a, (void (*)(void))fn, 2, ap, aq);\n"
364           "     swapcontext(&b, &a);\n"
365           "     return worked ? 0 : 1;\n"
366           "}\n"
367         },
368 };
369
370 static char *grab_fd(int fd)
371 {
372         int ret;
373         size_t max, size = 0;
374         char *buffer;
375
376         max = 16384;
377         buffer = malloc(max+1);
378         while ((ret = read(fd, buffer + size, max - size)) > 0) {
379                 size += ret;
380                 if (size == max)
381                         buffer = realloc(buffer, max *= 2);
382         }
383         if (ret < 0)
384                 err(1, "reading from command");
385         buffer[size] = '\0';
386         return buffer;
387 }
388
389 static char *run(const char *cmd, int *exitstatus)
390 {
391         pid_t pid;
392         int p[2];
393         char *ret;
394         int status;
395
396         if (pipe(p) != 0)
397                 err(1, "creating pipe");
398
399         pid = fork();
400         if (pid == -1)
401                 err(1, "forking");
402
403         if (pid == 0) {
404                 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
405                     || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
406                     || close(p[0]) != 0
407                     || close(STDIN_FILENO) != 0
408                     || open("/dev/null", O_RDONLY) != STDIN_FILENO)
409                         exit(128);
410
411                 status = system(cmd);
412                 if (WIFEXITED(status))
413                         exit(WEXITSTATUS(status));
414                 /* Here's a hint... */
415                 exit(128 + WTERMSIG(status));
416         }
417
418         close(p[1]);
419         ret = grab_fd(p[0]);
420         /* This shouldn't fail... */
421         if (waitpid(pid, &status, 0) != pid)
422                 err(1, "Failed to wait for child");
423         close(p[0]);
424         if (WIFEXITED(status))
425                 *exitstatus = WEXITSTATUS(status);
426         else
427                 *exitstatus = -WTERMSIG(status);
428         return ret;
429 }
430
431 static char *connect_args(const char *argv[], const char *extra)
432 {
433         unsigned int i, len = strlen(extra) + 1;
434         char *ret;
435
436         for (i = 1; argv[i]; i++)
437                 len += 1 + strlen(argv[i]);
438
439         ret = malloc(len);
440         len = 0;
441         for (i = 1; argv[i]; i++) {
442                 strcpy(ret + len, argv[i]);
443                 len += strlen(argv[i]);
444                 if (argv[i+1])
445                         ret[len++] = ' ';
446         }
447         strcpy(ret + len, extra);
448         return ret;
449 }
450
451 static struct test *find_test(const char *name)
452 {
453         unsigned int i;
454
455         for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
456                 if (strcmp(tests[i].name, name) == 0)
457                         return &tests[i];
458         }
459         abort();
460 }
461
462 #define PRE_BOILERPLATE "/* Test program generated by configurator. */\n"
463 #define MAIN_START_BOILERPLATE \
464         "int main(int argc, char *argv[]) {\n" \
465         "       (void)argc;\n" \
466         "       (void)argv;\n"
467 #define USE_FUNC_BOILERPLATE "(void)func;\n"
468 #define MAIN_BODY_BOILERPLATE "return 0;\n"
469 #define MAIN_END_BOILERPLATE "}\n"
470
471 static bool run_test(const char *cmd, struct test *test)
472 {
473         char *output, *newcmd;
474         FILE *outf;
475         int status;
476
477         if (test->done)
478                 return test->answer;
479
480         if (test->depends) {
481                 size_t len;
482                 const char *deps = test->depends;
483                 char *dep;
484
485                 /* Space-separated dependencies, could be ! for inverse. */
486                 while ((len = strcspn(deps, " "))) {
487                         bool positive = true;
488                         if (deps[len]) {
489                                 dep = strdup(deps);
490                                 dep[len] = '\0';
491                         } else {
492                                 dep = (char *)deps;
493                         }
494
495                         if (dep[0] == '!') {
496                                 dep++;
497                                 positive = false;
498                         }
499                         if (run_test(cmd, find_test(dep)) != positive) {
500                                 test->answer = false;
501                                 test->done = true;
502                                 return test->answer;
503                         }
504                         if (deps[len])
505                                 free(dep);
506
507                         deps += len;
508                         deps += strspn(deps, " ");
509                 }
510         }
511
512         outf = fopen(INPUT_FILE, "w");
513         if (!outf)
514                 err(1, "creating %s", INPUT_FILE);
515
516         fprintf(outf, "%s", PRE_BOILERPLATE);
517         switch (test->style & ~(EXECUTE|MAY_NOT_COMPILE)) {
518         case INSIDE_MAIN:
519                 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
520                 fprintf(outf, "%s", test->fragment);
521                 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
522                 break;
523         case OUTSIDE_MAIN:
524                 fprintf(outf, "%s", test->fragment);
525                 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
526                 fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
527                 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
528                 break;
529         case DEFINES_FUNC:
530                 fprintf(outf, "%s", test->fragment);
531                 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
532                 fprintf(outf, "%s", USE_FUNC_BOILERPLATE);
533                 fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
534                 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
535                 break;
536         case DEFINES_EVERYTHING:
537                 fprintf(outf, "%s", test->fragment);
538                 break;
539         default:
540                 abort();
541
542         }
543         fclose(outf);
544
545         if (verbose > 1)
546                 if (system("cat " INPUT_FILE) == -1)
547                         ;
548
549         newcmd = strdup(cmd);
550
551         if (test->flags) {
552                 newcmd = realloc(newcmd, strlen(newcmd) + strlen(" ")
553                                 + strlen(test->flags) + 1);
554                 strcat(newcmd, " ");
555                 strcat(newcmd, test->flags);
556                 if (verbose > 1)
557                         printf("Extra flags line: %s", newcmd);
558         }
559
560         if (test->link) {
561                 newcmd = realloc(newcmd, strlen(newcmd) + strlen(" ")
562                                 + strlen(test->link) + 1);
563                 strcat(newcmd, " ");
564                 strcat(newcmd, test->link);
565                 if (verbose > 1)
566                         printf("Extra link line: %s", newcmd);
567         }
568
569         output = run(newcmd, &status);
570
571         free(newcmd);
572
573         if (status != 0 || strstr(output, "warning")) {
574                 if (verbose)
575                         printf("Compile %s for %s, status %i: %s\n",
576                                status ? "fail" : "warning",
577                                test->name, status, output);
578                 if ((test->style & EXECUTE) && !(test->style & MAY_NOT_COMPILE))
579                         errx(1, "Test for %s did not compile:\n%s",
580                              test->name, output);
581                 test->answer = false;
582                 free(output);
583         } else {
584                 /* Compile succeeded. */
585                 free(output);
586                 /* We run INSIDE_MAIN tests for sanity checking. */
587                 if ((test->style & EXECUTE) || (test->style & INSIDE_MAIN)) {
588                         output = run("./" OUTPUT_FILE, &status);
589                         if (!(test->style & EXECUTE) && status != 0)
590                                 errx(1, "Test for %s failed with %i:\n%s",
591                                      test->name, status, output);
592                         if (verbose && status)
593                                 printf("%s exited %i\n", test->name, status);
594                         free(output);
595                 }
596                 test->answer = (status == 0);
597         }
598         test->done = true;
599
600         if (test->answer && test->overrides) {
601                 struct test *override = find_test(test->overrides);
602                 override->done = true;
603                 override->answer = true;
604         }
605         return test->answer;
606 }
607
608 int main(int argc, const char *argv[])
609 {
610         char *cmd;
611         unsigned int i;
612         const char *default_args[]
613                 = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL };
614
615         if (argc > 1) {
616                 if (strcmp(argv[1], "--help") == 0) {
617                         printf("Usage: configurator [-v] [<compiler> <flags>...]\n"
618                                "  <compiler> <flags> will have \"-o <outfile> <infile.c>\" appended\n"
619                                "Default: %s %s\n",
620                                DEFAULT_COMPILER, DEFAULT_FLAGS);
621                         exit(0);
622                 }
623                 if (strcmp(argv[1], "-v") == 0) {
624                         argc--;
625                         argv++;
626                         verbose = 1;
627                 } else if (strcmp(argv[1], "-vv") == 0) {
628                         argc--;
629                         argv++;
630                         verbose = 2;
631                 }
632         }
633
634         if (argc == 1)
635                 argv = default_args;
636
637         cmd = connect_args(argv, " -o " OUTPUT_FILE " " INPUT_FILE);
638         for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
639                 run_test(cmd, &tests[i]);
640         free(cmd);
641
642         unlink(OUTPUT_FILE);
643         unlink(INPUT_FILE);
644
645         printf("/* Generated by CCAN configurator */\n"
646                "#ifndef CCAN_CONFIG_H\n"
647                "#define CCAN_CONFIG_H\n");
648         printf("#ifndef _GNU_SOURCE\n");
649         printf("#define _GNU_SOURCE /* Always use GNU extensions. */\n");
650         printf("#endif\n");
651         printf("#define CCAN_COMPILER \"%s\"\n", argv[1]);
652         cmd = connect_args(argv+1, "");
653         printf("#define CCAN_CFLAGS \"%s\"\n\n", cmd);
654         free(cmd);
655         /* This one implies "#include <ccan/..." works, eg. for tdb2.h */
656         printf("#define HAVE_CCAN 1\n");
657         for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
658                 printf("#define %s %u\n", tests[i].name, tests[i].answer);
659         printf("#endif /* CCAN_CONFIG_H */\n");
660         return 0;
661 }