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