4445139c90fcfabd01989956e1e60cd8cc958e36
[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         EXECUTE,
24         OUTSIDE_MAIN,
25         DEFINES_FUNC,
26         INSIDE_MAIN
27 };
28
29 struct test {
30         const char *name;
31         enum test_style style;
32         const char *depends;
33         const char *fragment;
34         bool done;
35         bool answer;
36 };
37
38 static struct test tests[] = {
39         { "HAVE_ALIGNOF", INSIDE_MAIN, NULL,
40           "return __alignof__(double) > 0 ? 0 : 1;" },
41         { "HAVE_ATTRIBUTE_COLD", DEFINES_FUNC, NULL,
42           "static int __attribute__((cold)) func(int x) { return x; }" },
43         { "HAVE_ATTRIBUTE_CONST", DEFINES_FUNC, NULL,
44           "static int __attribute__((const)) func(int x) { return x; }" },
45         { "HAVE_ATTRIBUTE_MAY_ALIAS", OUTSIDE_MAIN, NULL,
46           "typedef short __attribute__((__may_alias__)) short_a;" },
47         { "HAVE_ATTRIBUTE_PRINTF", DEFINES_FUNC, NULL,
48           "static void __attribute__((format(__printf__, 1, 2))) func(const char *fmt, ...) { }" },
49         { "HAVE_ATTRIBUTE_UNUSED", OUTSIDE_MAIN, NULL,
50           "static int __attribute__((unused)) func(int x) { return x; }" },
51         { "HAVE_ATTRIBUTE_USED", OUTSIDE_MAIN, NULL,
52           "static int __attribute__((used)) func(int x) { return x; }" },
53         { "HAVE_BIG_ENDIAN", EXECUTE, NULL,
54           "union { int i; char c[sizeof(int)]; } u;\n"
55           "u.i = 0x01020304;\n"
56           "return u.c[0] == 0x01 && u.c[1] == 0x02 && u.c[2] == 0x03 && u.c[3] == 0x04 ? 0 : 1;" },
57         { "HAVE_BSWAP_64", DEFINES_FUNC, "HAVE_BYTESWAP_H",
58           "#include <byteswap.h>\n"
59           "static int func(int x) { return bswap_64(x); }" },
60         { "HAVE_BUILTIN_CHOOSE_EXPR", INSIDE_MAIN, NULL,
61           "return __builtin_choose_expr(1, 0, \"garbage\");" },
62         { "HAVE_BUILTIN_CLZ", INSIDE_MAIN, NULL,
63           "return __builtin_clz(1) == (sizeof(int)*8 - 1) ? 0 : 1;" },
64         { "HAVE_BUILTIN_CLZL", INSIDE_MAIN, NULL,
65           "return __builtin_clzl(1) == (sizeof(long)*8 - 1) ? 0 : 1;" },
66         { "HAVE_BUILTIN_CLZLL", INSIDE_MAIN, NULL,
67           "return __builtin_clzll(1) == (sizeof(long long)*8 - 1) ? 0 : 1;" },
68         { "HAVE_BUILTIN_CONSTANT_P", INSIDE_MAIN, NULL,
69           "return __builtin_constant_p(1) ? 0 : 1;" },
70         { "HAVE_BUILTIN_EXPECT", INSIDE_MAIN, NULL,
71           "return __builtin_expect(argc == 1, 1) ? 0 : 1;" },
72         { "HAVE_BUILTIN_FFSL", INSIDE_MAIN, NULL,
73           "return __builtin_ffsl(0L) == 0 ? 0 : 1;" },
74         { "HAVE_BUILTIN_FFSLL", INSIDE_MAIN, NULL,
75           "return __builtin_ffsll(0LL) == 0 ? 0 : 1;" },
76         { "HAVE_BUILTIN_POPCOUNTL", INSIDE_MAIN, NULL,
77           "return __builtin_popcountl(255L) == 8 ? 0 : 1;" },
78         { "HAVE_BUILTIN_TYPES_COMPATIBLE_P", INSIDE_MAIN, NULL,
79           "return __builtin_types_compatible_p(char *, int) ? 1 : 0;" },
80         { "HAVE_BYTESWAP_H", OUTSIDE_MAIN, NULL,
81           "#include <byteswap.h>\n" },
82         { "HAVE_COMPOUND_LITERALS", INSIDE_MAIN, NULL,
83           "char **foo = (char *[]) { \"x\", \"y\", \"z\" };\n"
84           "return foo[0] ? 0 : 1;" },
85         { "HAVE_FOR_LOOP_DECLARATION", INSIDE_MAIN, NULL,
86           "for (int i = 0; i < argc; i++) { return 0; };\n"
87           "return 1;" },
88         { "HAVE_FLEXIBLE_ARRAY_MEMBER", OUTSIDE_MAIN, NULL,
89           "struct foo { unsigned int x; int arr[]; };" },
90         { "HAVE_GETPAGESIZE", DEFINES_FUNC, NULL,
91           "#include <unistd.h>\n"
92           "static int func(void) { return getpagesize(); }" },
93         { "HAVE_LITTLE_ENDIAN", EXECUTE, NULL,
94           "union { int i; char c[sizeof(int)]; } u;\n"
95           "u.i = 0x01020304;\n"
96           "return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;" },
97         { "HAVE_MMAP", DEFINES_FUNC, NULL,
98           "#include <sys/mman.h>\n"
99           "static void *func(int fd) {\n"
100           "     return mmap(0, 65536, PROT_READ, MAP_SHARED, fd, 0);\n"
101           "}" },
102         { "HAVE_NESTED_FUNCTIONS", DEFINES_FUNC, NULL,
103           "static int func(int val) {\n"
104           "     auto void add(int val2);\n"
105           "     void add(int val2) { val += val2; }\n"
106           "     add(7);\n"
107           "     return val;\n"
108           "}" },
109         { "HAVE_STATEMENT_EXPR", INSIDE_MAIN, NULL,
110           "return ({ int x = argc; x == argc ? 0 : 1; });" },
111         { "HAVE_TYPEOF", INSIDE_MAIN, NULL,
112           "__typeof__(argc) i; i = argc; return i == argc ? 0 : 1;" },
113         { "HAVE_UTIME", DEFINES_FUNC, NULL,
114           "#include <sys/types.h>\n"
115           "#include <utime.h>\n"
116           "static int func(const char *filename) {\n"
117           "     struct utimbuf times = { 0 };\n"
118           "     return utime(filename, &times);\n"
119           "}" },
120         { "HAVE_WARN_UNUSED_RESULT", DEFINES_FUNC, NULL,
121           "#include <sys/types.h>\n"
122           "#include <utime.h>\n"
123           "static __attribute__((warn_unused_result)) int func(int i) {\n"
124           "     return i + 1;\n"
125           "}" },
126 };
127
128 static char *grab_fd(int fd)
129 {
130         int ret;
131         size_t max, size = 0;
132         char *buffer;
133
134         max = 16384;
135         buffer = malloc(max+1);
136         while ((ret = read(fd, buffer + size, max - size)) > 0) {
137                 size += ret;
138                 if (size == max)
139                         buffer = realloc(buffer, max *= 2);
140         }
141         if (ret < 0)
142                 err(1, "reading from command");
143         buffer[size] = '\0';
144         return buffer;
145 }
146
147 static char *run(const char *cmd, int *exitstatus)
148 {
149         pid_t pid;
150         int p[2];
151         char *ret;
152         int status;
153
154         if (pipe(p) != 0)
155                 err(1, "creating pipe");
156
157         pid = fork();
158         if (pid == -1)
159                 err(1, "forking");
160
161         if (pid == 0) {
162                 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
163                     || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
164                     || close(p[0]) != 0
165                     || close(STDIN_FILENO) != 0
166                     || open("/dev/null", O_RDONLY) != STDIN_FILENO)
167                         exit(128);
168
169                 status = system(cmd);
170                 if (WIFEXITED(status))
171                         exit(WEXITSTATUS(status));
172                 /* Here's a hint... */
173                 exit(128 + WTERMSIG(status));
174         }
175
176         close(p[1]);
177         ret = grab_fd(p[0]);
178         /* This shouldn't fail... */
179         if (waitpid(pid, &status, 0) != pid)
180                 err(1, "Failed to wait for child");
181         close(p[0]);
182         if (WIFEXITED(status))
183                 *exitstatus = WEXITSTATUS(status);
184         else
185                 *exitstatus = -WTERMSIG(status);
186         return ret;
187 }
188
189 static char *connect_args(char *argv[], const char *extra)
190 {
191         unsigned int i, len = strlen(extra) + 1;
192         char *ret;
193
194         for (i = 1; argv[i]; i++)
195                 len += 1 + strlen(argv[i]);
196
197         ret = malloc(len);
198         len = 0;
199         for (i = 1; argv[i]; i++) {
200                 strcpy(ret + len, argv[i]);
201                 len += strlen(argv[i]);
202                 ret[len++] = ' ';
203         }
204         strcpy(ret + len, extra);
205         return ret;
206 }
207
208 static struct test *find_test(const char *name)
209 {
210         unsigned int i;
211
212         for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
213                 if (strcmp(tests[i].name, name) == 0)
214                         return &tests[i];
215         }
216         abort();
217 }
218
219 #define PRE_BOILERPLATE "/* Test program generated by configurator. */\n"
220 #define MAIN_START_BOILERPLATE "int main(int argc, char *argv[]) {\n"
221 #define USE_FUNC_BOILERPLATE "(void)func;\n"
222 #define MAIN_BODY_BOILERPLATE "return 0;\n"
223 #define MAIN_END_BOILERPLATE "}\n"
224
225 static bool run_test(const char *cmd, struct test *test)
226 {
227         char *output;
228         FILE *outf;
229         int status;
230
231         if (test->done)
232                 return test->answer;
233
234         if (test->depends && !run_test(cmd, find_test(test->depends))) {
235                 test->answer = false;
236                 test->done = true;
237                 return test->answer;
238         }
239
240         outf = fopen(INPUT_FILE, "w");
241         if (!outf)
242                 err(1, "creating %s", INPUT_FILE);
243
244         fprintf(outf, "%s", PRE_BOILERPLATE);
245         switch (test->style) {
246         case EXECUTE:
247         case INSIDE_MAIN:
248                 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
249                 fprintf(outf, "%s", test->fragment);
250                 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
251                 break;
252         case OUTSIDE_MAIN:
253                 fprintf(outf, "%s", test->fragment);
254                 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
255                 fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
256                 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
257                 break;
258         case DEFINES_FUNC:
259                 fprintf(outf, "%s", test->fragment);
260                 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
261                 fprintf(outf, "%s", USE_FUNC_BOILERPLATE);
262                 fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
263                 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
264                 break;
265         }
266         fclose(outf);
267
268         if (verbose > 1)
269                 if (system("cat " INPUT_FILE) == -1);
270
271         output = run(cmd, &status);
272         if (status != 0 || strstr(output, "warning")) {
273                 if (verbose)
274                         printf("Compile %s for %s, status %i: %s\n",
275                                status ? "fail" : "warning",
276                                test->name, status, output);
277                 if (test->style == EXECUTE)
278                         errx(1, "Test for %s did not compile:\n%s",
279                              test->name, output);
280                 test->answer = false;
281                 free(output);
282         } else {
283                 /* Compile succeeded. */
284                 free(output);
285                 /* We run INSIDE_MAIN tests for sanity checking. */
286                 if (test->style == EXECUTE || test->style == INSIDE_MAIN) {
287                         output = run("./" OUTPUT_FILE, &status);
288                         if (test->style == INSIDE_MAIN && status != 0)
289                                 errx(1, "Test for %s failed with %i:\n%s",
290                                      test->name, status, output);
291                         if (verbose && status)
292                                 printf("%s exited %i\n", test->name, status);
293                         free(output);
294                 }
295                 test->answer = (status == 0);
296         }
297         test->done = true;
298         return test->answer;
299 }
300
301 int main(int argc, char *argv[])
302 {
303         char *cmd;
304         char *default_args[] = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL };
305         unsigned int i;
306
307         if (argc > 1) {
308                 if (strcmp(argv[1], "--help") == 0) {
309                         printf("Usage: configurator [-v] [<compiler> <flags>...]\n"
310                                "  <compiler> <flags> will have \"-o <outfile> <infile.c>\" appended\n"
311                                "Default: %s %s\n",
312                                DEFAULT_COMPILER, DEFAULT_FLAGS);
313                         exit(0);
314                 }
315                 if (strcmp(argv[1], "-v") == 0) {
316                         argc--;
317                         argv++;
318                         verbose = 1;
319                 } else if (strcmp(argv[1], "-vv") == 0) {
320                         argc--;
321                         argv++;
322                         verbose = 2;
323                 }
324         }
325
326         if (argc == 1)
327                 argv = default_args;
328
329         cmd = connect_args(argv, "-o " OUTPUT_FILE " " INPUT_FILE);
330         for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
331                 run_test(cmd, &tests[i]);
332
333         unlink(OUTPUT_FILE);
334         unlink(INPUT_FILE);
335
336         cmd[strlen(cmd) - strlen(" -o " OUTPUT_FILE " " INPUT_FILE)] = '\0';
337         printf("/* Generated by CCAN configurator */\n");
338         printf("#define CCAN_COMPILER \"%s\"\n", argv[1]);
339         printf("#define CCAN_CFLAGS \"%s\"\n\n", cmd + strlen(argv[1]) + 1);
340         for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
341                 printf("#define %s %u\n", tests[i].name, tests[i].answer);
342         return 0;
343 }