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