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