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