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