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