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