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