configurator: Remove the now unneeded test for nested functions.
[ccan] / tools / configurator / configurator.c
1 /* Simple tool to create config.h.
2  * Would be much easier with ccan modules, but deliberately standalone. */
3 #include <stdio.h>
4 #include <stdbool.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <err.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <string.h>
13
14 #define DEFAULT_COMPILER "cc"
15 #define DEFAULT_FLAGS "-g -Wall -Wundef -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes -Wold-style-definition"
16
17 #define OUTPUT_FILE "configurator.out"
18 #define INPUT_FILE "configuratortest.c"
19
20 static int verbose;
21
22 enum test_style {
23         OUTSIDE_MAIN            = 0x1,
24         DEFINES_FUNC            = 0x2,
25         INSIDE_MAIN             = 0x4,
26         DEFINES_EVERYTHING      = 0x8,
27         MAY_NOT_COMPILE         = 0x10,
28         EXECUTE                 = 0x8000
29 };
30
31 struct test {
32         const char *name;
33         enum test_style style;
34         const char *depends;
35         const char *fragment;
36         bool done;
37         bool answer;
38 };
39
40 static struct test tests[] = {
41         { "HAVE_ALIGNOF", INSIDE_MAIN, NULL,
42           "return __alignof__(double) > 0 ? 0 : 1;" },
43         { "HAVE_ASPRINTF", DEFINES_FUNC, NULL,
44           "#define _GNU_SOURCE\n"
45           "#include <stdio.h>\n"
46           "static char *func(int x) {"
47           "     char *p;\n"
48           "     if (asprintf(&p, \"%u\", x) == -1) p = NULL;"
49           "     return p;\n"
50           "}" },
51         { "HAVE_ATTRIBUTE_COLD", DEFINES_FUNC, NULL,
52           "static int __attribute__((cold)) func(int x) { return x; }" },
53         { "HAVE_ATTRIBUTE_CONST", DEFINES_FUNC, NULL,
54           "static int __attribute__((const)) func(int x) { return x; }" },
55         { "HAVE_ATTRIBUTE_MAY_ALIAS", OUTSIDE_MAIN, NULL,
56           "typedef short __attribute__((__may_alias__)) short_a;" },
57         { "HAVE_ATTRIBUTE_NORETURN", DEFINES_FUNC, NULL,
58           "#include <stdlib.h>\n"
59           "static void __attribute__((noreturn)) func(int x) { exit(x); }" },
60         { "HAVE_ATTRIBUTE_PRINTF", DEFINES_FUNC, NULL,
61           "static void __attribute__((format(__printf__, 1, 2))) func(const char *fmt, ...) { }" },
62         { "HAVE_ATTRIBUTE_UNUSED", OUTSIDE_MAIN, NULL,
63           "static int __attribute__((unused)) func(int x) { return x; }" },
64         { "HAVE_ATTRIBUTE_USED", OUTSIDE_MAIN, NULL,
65           "static int __attribute__((used)) func(int x) { return x; }" },
66         { "HAVE_BIG_ENDIAN", INSIDE_MAIN|EXECUTE, NULL,
67           "union { int i; char c[sizeof(int)]; } u;\n"
68           "u.i = 0x01020304;\n"
69           "return u.c[0] == 0x01 && u.c[1] == 0x02 && u.c[2] == 0x03 && u.c[3] == 0x04 ? 0 : 1;" },
70         { "HAVE_BSWAP_64", DEFINES_FUNC, "HAVE_BYTESWAP_H",
71           "#include <byteswap.h>\n"
72           "static int func(int x) { return bswap_64(x); }" },
73         { "HAVE_BUILTIN_CHOOSE_EXPR", INSIDE_MAIN, NULL,
74           "return __builtin_choose_expr(1, 0, \"garbage\");" },
75         { "HAVE_BUILTIN_CLZ", INSIDE_MAIN, NULL,
76           "return __builtin_clz(1) == (sizeof(int)*8 - 1) ? 0 : 1;" },
77         { "HAVE_BUILTIN_CLZL", INSIDE_MAIN, NULL,
78           "return __builtin_clzl(1) == (sizeof(long)*8 - 1) ? 0 : 1;" },
79         { "HAVE_BUILTIN_CLZLL", INSIDE_MAIN, NULL,
80           "return __builtin_clzll(1) == (sizeof(long long)*8 - 1) ? 0 : 1;" },
81         { "HAVE_BUILTIN_CONSTANT_P", INSIDE_MAIN, NULL,
82           "return __builtin_constant_p(1) ? 0 : 1;" },
83         { "HAVE_BUILTIN_EXPECT", INSIDE_MAIN, NULL,
84           "return __builtin_expect(argc == 1, 1) ? 0 : 1;" },
85         { "HAVE_BUILTIN_FFSL", INSIDE_MAIN, NULL,
86           "return __builtin_ffsl(0L) == 0 ? 0 : 1;" },
87         { "HAVE_BUILTIN_FFSLL", INSIDE_MAIN, NULL,
88           "return __builtin_ffsll(0LL) == 0 ? 0 : 1;" },
89         { "HAVE_BUILTIN_POPCOUNTL", INSIDE_MAIN, NULL,
90           "return __builtin_popcountl(255L) == 8 ? 0 : 1;" },
91         { "HAVE_BUILTIN_TYPES_COMPATIBLE_P", INSIDE_MAIN, NULL,
92           "return __builtin_types_compatible_p(char *, int) ? 1 : 0;" },
93         { "HAVE_BYTESWAP_H", OUTSIDE_MAIN, NULL,
94           "#include <byteswap.h>\n" },
95         { "HAVE_COMPOUND_LITERALS", INSIDE_MAIN, NULL,
96           "int *foo = (int[]) { 1, 2, 3, 4 };\n"
97           "return foo[0] ? 0 : 1;" },
98         { "HAVE_FOR_LOOP_DECLARATION", INSIDE_MAIN, NULL,
99           "for (int i = 0; i < argc; i++) { return 0; };\n"
100           "return 1;" },
101         { "HAVE_FLEXIBLE_ARRAY_MEMBER", OUTSIDE_MAIN, NULL,
102           "struct foo { unsigned int x; int arr[]; };" },
103         { "HAVE_GETPAGESIZE", DEFINES_FUNC, NULL,
104           "#include <unistd.h>\n"
105           "static int func(void) { return getpagesize(); }" },
106         { "HAVE_ISBLANK", DEFINES_FUNC, NULL,
107           "#include <ctype.h>\n"
108           "static int func(void) { return isblank(' '); }" },
109         { "HAVE_LITTLE_ENDIAN", INSIDE_MAIN|EXECUTE, NULL,
110           "union { int i; char c[sizeof(int)]; } u;\n"
111           "u.i = 0x01020304;\n"
112           "return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;" },
113         { "HAVE_MEMMEM", DEFINES_FUNC, NULL,
114           "#define _GNU_SOURCE\n"
115           "#include <string.h>\n"
116           "static void *func(void *h, size_t hl, void *n, size_t nl) {\n"
117           "return memmem(h, hl, n, nl);"
118           "}\n", },
119         { "HAVE_MMAP", DEFINES_FUNC, NULL,
120           "#include <sys/mman.h>\n"
121           "static void *func(int fd) {\n"
122           "     return mmap(0, 65536, PROT_READ, MAP_SHARED, fd, 0);\n"
123           "}" },
124         { "HAVE_QSORT_R_PRIVATE_LAST",
125           DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE, NULL,
126           "#define _GNU_SOURCE 1\n"
127           "#include <stdlib.h>\n"
128           "static int cmp(const void *lp, const void *rp, void *priv) {\n"
129           " *(unsigned int *)priv = 1;\n"
130           " return *(const int *)lp - *(const int *)rp; }\n"
131           "int main(void) {\n"
132           " int array[] = { 9, 2, 5 };\n"
133           " unsigned int called = 0;\n"
134           " qsort_r(array, 3, sizeof(int), cmp, &called);\n"
135           " return called && array[0] == 2 && array[1] == 5 && array[2] == 9 ? 0 : 1;\n"
136           "}\n" },
137         { "HAVE_STACK_GROWS_UPWARDS", DEFINES_EVERYTHING|EXECUTE, NULL,
138           "static long nest(const void *base, unsigned int i)\n"
139           "{\n"
140           "     if (i == 0)\n"
141           "             return (const char *)&i - (const char *)base;\n"
142           "     return nest(base, i-1);\n"
143           "}\n"
144           "int main(int argc, char *argv[]) {\n"
145           "     return (nest(&argc, argc) > 0) ? 0 : 1\n;"
146           "}\n" },
147         { "HAVE_STATEMENT_EXPR", INSIDE_MAIN, NULL,
148           "return ({ int x = argc; x == argc ? 0 : 1; });" },
149         { "HAVE_TYPEOF", INSIDE_MAIN, NULL,
150           "__typeof__(argc) i; i = argc; return i == argc ? 0 : 1;" },
151         { "HAVE_UTIME", DEFINES_FUNC, NULL,
152           "#include <sys/types.h>\n"
153           "#include <utime.h>\n"
154           "static int func(const char *filename) {\n"
155           "     struct utimbuf times = { 0 };\n"
156           "     return utime(filename, &times);\n"
157           "}" },
158         { "HAVE_WARN_UNUSED_RESULT", DEFINES_FUNC, NULL,
159           "#include <sys/types.h>\n"
160           "#include <utime.h>\n"
161           "static __attribute__((warn_unused_result)) int func(int i) {\n"
162           "     return i + 1;\n"
163           "}" },
164 };
165
166 static char *grab_fd(int fd)
167 {
168         int ret;
169         size_t max, size = 0;
170         char *buffer;
171
172         max = 16384;
173         buffer = malloc(max+1);
174         while ((ret = read(fd, buffer + size, max - size)) > 0) {
175                 size += ret;
176                 if (size == max)
177                         buffer = realloc(buffer, max *= 2);
178         }
179         if (ret < 0)
180                 err(1, "reading from command");
181         buffer[size] = '\0';
182         return buffer;
183 }
184
185 static char *run(const char *cmd, int *exitstatus)
186 {
187         pid_t pid;
188         int p[2];
189         char *ret;
190         int status;
191
192         if (pipe(p) != 0)
193                 err(1, "creating pipe");
194
195         pid = fork();
196         if (pid == -1)
197                 err(1, "forking");
198
199         if (pid == 0) {
200                 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
201                     || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
202                     || close(p[0]) != 0
203                     || close(STDIN_FILENO) != 0
204                     || open("/dev/null", O_RDONLY) != STDIN_FILENO)
205                         exit(128);
206
207                 status = system(cmd);
208                 if (WIFEXITED(status))
209                         exit(WEXITSTATUS(status));
210                 /* Here's a hint... */
211                 exit(128 + WTERMSIG(status));
212         }
213
214         close(p[1]);
215         ret = grab_fd(p[0]);
216         /* This shouldn't fail... */
217         if (waitpid(pid, &status, 0) != pid)
218                 err(1, "Failed to wait for child");
219         close(p[0]);
220         if (WIFEXITED(status))
221                 *exitstatus = WEXITSTATUS(status);
222         else
223                 *exitstatus = -WTERMSIG(status);
224         return ret;
225 }
226
227 static char *connect_args(const char *argv[], const char *extra)
228 {
229         unsigned int i, len = strlen(extra) + 1;
230         char *ret;
231
232         for (i = 1; argv[i]; i++)
233                 len += 1 + strlen(argv[i]);
234
235         ret = malloc(len);
236         len = 0;
237         for (i = 1; argv[i]; i++) {
238                 strcpy(ret + len, argv[i]);
239                 len += strlen(argv[i]);
240                 ret[len++] = ' ';
241         }
242         strcpy(ret + len, extra);
243         return ret;
244 }
245
246 static struct test *find_test(const char *name)
247 {
248         unsigned int i;
249
250         for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
251                 if (strcmp(tests[i].name, name) == 0)
252                         return &tests[i];
253         }
254         abort();
255 }
256
257 #define PRE_BOILERPLATE "/* Test program generated by configurator. */\n"
258 #define MAIN_START_BOILERPLATE "int main(int argc, char *argv[]) {\n"
259 #define USE_FUNC_BOILERPLATE "(void)func;\n"
260 #define MAIN_BODY_BOILERPLATE "return 0;\n"
261 #define MAIN_END_BOILERPLATE "}\n"
262
263 static bool run_test(const char *cmd, struct test *test)
264 {
265         char *output;
266         FILE *outf;
267         int status;
268
269         if (test->done)
270                 return test->answer;
271
272         if (test->depends && !run_test(cmd, find_test(test->depends))) {
273                 test->answer = false;
274                 test->done = true;
275                 return test->answer;
276         }
277
278         outf = fopen(INPUT_FILE, "w");
279         if (!outf)
280                 err(1, "creating %s", INPUT_FILE);
281
282         fprintf(outf, "%s", PRE_BOILERPLATE);
283         switch (test->style & ~(EXECUTE|MAY_NOT_COMPILE)) {
284         case INSIDE_MAIN:
285                 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
286                 fprintf(outf, "%s", test->fragment);
287                 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
288                 break;
289         case OUTSIDE_MAIN:
290                 fprintf(outf, "%s", test->fragment);
291                 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
292                 fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
293                 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
294                 break;
295         case DEFINES_FUNC:
296                 fprintf(outf, "%s", test->fragment);
297                 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
298                 fprintf(outf, "%s", USE_FUNC_BOILERPLATE);
299                 fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
300                 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
301                 break;
302         case DEFINES_EVERYTHING:
303                 fprintf(outf, "%s", test->fragment);
304                 break;
305         default:
306                 abort();
307
308         }
309         fclose(outf);
310
311         if (verbose > 1)
312                 if (system("cat " INPUT_FILE) == -1);
313
314         output = run(cmd, &status);
315         if (status != 0 || strstr(output, "warning")) {
316                 if (verbose)
317                         printf("Compile %s for %s, status %i: %s\n",
318                                status ? "fail" : "warning",
319                                test->name, status, output);
320                 if ((test->style & EXECUTE) && !(test->style & MAY_NOT_COMPILE))
321                         errx(1, "Test for %s did not compile:\n%s",
322                              test->name, output);
323                 test->answer = false;
324                 free(output);
325         } else {
326                 /* Compile succeeded. */
327                 free(output);
328                 /* We run INSIDE_MAIN tests for sanity checking. */
329                 if ((test->style & EXECUTE) || (test->style & INSIDE_MAIN)) {
330                         output = run("./" OUTPUT_FILE, &status);
331                         if (!(test->style & EXECUTE) && status != 0)
332                                 errx(1, "Test for %s failed with %i:\n%s",
333                                      test->name, status, output);
334                         if (verbose && status)
335                                 printf("%s exited %i\n", test->name, status);
336                         free(output);
337                 }
338                 test->answer = (status == 0);
339         }
340         test->done = true;
341         return test->answer;
342 }
343
344 int main(int argc, const char *argv[])
345 {
346         char *cmd;
347         unsigned int i;
348         const char *default_args[]
349                 = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL };
350
351         if (argc > 1) {
352                 if (strcmp(argv[1], "--help") == 0) {
353                         printf("Usage: configurator [-v] [<compiler> <flags>...]\n"
354                                "  <compiler> <flags> will have \"-o <outfile> <infile.c>\" appended\n"
355                                "Default: %s %s\n",
356                                DEFAULT_COMPILER, DEFAULT_FLAGS);
357                         exit(0);
358                 }
359                 if (strcmp(argv[1], "-v") == 0) {
360                         argc--;
361                         argv++;
362                         verbose = 1;
363                 } else if (strcmp(argv[1], "-vv") == 0) {
364                         argc--;
365                         argv++;
366                         verbose = 2;
367                 }
368         }
369
370         if (argc == 1)
371                 argv = default_args;
372
373         cmd = connect_args(argv, "-o " OUTPUT_FILE " " INPUT_FILE);
374         for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
375                 run_test(cmd, &tests[i]);
376
377         unlink(OUTPUT_FILE);
378         unlink(INPUT_FILE);
379
380         cmd[strlen(cmd) - strlen(" -o " OUTPUT_FILE " " INPUT_FILE)] = '\0';
381         printf("/* Generated by CCAN configurator */\n"
382                "#ifndef CCAN_CONFIG_H\n"
383                "#define CCAN_CONFIG_H\n");
384         printf("#define _GNU_SOURCE /* Always use GNU extensions. */\n");
385         printf("#define CCAN_COMPILER \"%s\"\n", argv[1]);
386         printf("#define CCAN_CFLAGS \"%s\"\n\n", cmd + strlen(argv[1]) + 1);
387         for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
388                 printf("#define %s %u\n", tests[i].name, tests[i].answer);
389         printf("#endif /* CCAN_CONFIG_H */\n");
390         return 0;
391 }