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