1 /* Simple tool to create config.h.
2 * Would be much easier with ccan modules, but deliberately standalone. */
14 #define DEFAULT_COMPILER "cc"
15 #define DEFAULT_FLAGS "-g -Wall -Wundef -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes -Wold-style-definition"
17 #define OUTPUT_FILE "configurator.out"
18 #define INPUT_FILE "configuratortest.c"
26 DEFINES_EVERYTHING = 0x8,
27 MAY_NOT_COMPILE = 0x10,
33 enum test_style style;
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"
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) {"
53 " if (asprintf(&p, \"%u\", x) == -1) p = NULL;"
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"
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,
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"
110 { "HAVE_FOR_LOOP_DECLARATION", INSIDE_MAIN, NULL,
111 "for (int i = 0; i < argc; i++) { return 0; };\n"
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 "#define _GNU_SOURCE\n"
120 "#include <ctype.h>\n"
121 "static int func(void) { return isblank(' '); }" },
122 { "HAVE_LITTLE_ENDIAN", INSIDE_MAIN|EXECUTE, NULL,
123 "union { int i; char c[sizeof(int)]; } u;\n"
124 "u.i = 0x01020304;\n"
125 "return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;" },
126 { "HAVE_MEMMEM", DEFINES_FUNC, NULL,
127 "#define _GNU_SOURCE\n"
128 "#include <string.h>\n"
129 "static void *func(void *h, size_t hl, void *n, size_t nl) {\n"
130 "return memmem(h, hl, n, nl);"
132 { "HAVE_MMAP", DEFINES_FUNC, NULL,
133 "#include <sys/mman.h>\n"
134 "static void *func(int fd) {\n"
135 " return mmap(0, 65536, PROT_READ, MAP_SHARED, fd, 0);\n"
137 { "HAVE_QSORT_R_PRIVATE_LAST",
138 DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE, NULL,
139 "#define _GNU_SOURCE 1\n"
140 "#include <stdlib.h>\n"
141 "static int cmp(const void *lp, const void *rp, void *priv) {\n"
142 " *(unsigned int *)priv = 1;\n"
143 " return *(const int *)lp - *(const int *)rp; }\n"
145 " int array[] = { 9, 2, 5 };\n"
146 " unsigned int called = 0;\n"
147 " qsort_r(array, 3, sizeof(int), cmp, &called);\n"
148 " return called && array[0] == 2 && array[1] == 5 && array[2] == 9 ? 0 : 1;\n"
150 { "HAVE_STACK_GROWS_UPWARDS", DEFINES_EVERYTHING|EXECUTE, NULL,
151 "static long nest(const void *base, unsigned int i)\n"
154 " return (const char *)&i - (const char *)base;\n"
155 " return nest(base, i-1);\n"
157 "int main(int argc, char *argv[]) {\n"
158 " return (nest(&argc, argc) > 0) ? 0 : 1\n;"
160 { "HAVE_STATEMENT_EXPR", INSIDE_MAIN, NULL,
161 "return ({ int x = argc; x == argc ? 0 : 1; });" },
162 { "HAVE_TYPEOF", INSIDE_MAIN, NULL,
163 "__typeof__(argc) i; i = argc; return i == argc ? 0 : 1;" },
164 { "HAVE_UTIME", DEFINES_FUNC, NULL,
165 "#include <sys/types.h>\n"
166 "#include <utime.h>\n"
167 "static int func(const char *filename) {\n"
168 " struct utimbuf times = { 0 };\n"
169 " return utime(filename, ×);\n"
171 { "HAVE_WARN_UNUSED_RESULT", DEFINES_FUNC, NULL,
172 "#include <sys/types.h>\n"
173 "#include <utime.h>\n"
174 "static __attribute__((warn_unused_result)) int func(int i) {\n"
179 static char *grab_fd(int fd)
182 size_t max, size = 0;
186 buffer = malloc(max+1);
187 while ((ret = read(fd, buffer + size, max - size)) > 0) {
190 buffer = realloc(buffer, max *= 2);
193 err(1, "reading from command");
198 static char *run(const char *cmd, int *exitstatus)
206 err(1, "creating pipe");
213 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
214 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
216 || close(STDIN_FILENO) != 0
217 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
220 status = system(cmd);
221 if (WIFEXITED(status))
222 exit(WEXITSTATUS(status));
223 /* Here's a hint... */
224 exit(128 + WTERMSIG(status));
229 /* This shouldn't fail... */
230 if (waitpid(pid, &status, 0) != pid)
231 err(1, "Failed to wait for child");
233 if (WIFEXITED(status))
234 *exitstatus = WEXITSTATUS(status);
236 *exitstatus = -WTERMSIG(status);
240 static char *connect_args(const char *argv[], const char *extra)
242 unsigned int i, len = strlen(extra) + 1;
245 for (i = 1; argv[i]; i++)
246 len += 1 + strlen(argv[i]);
250 for (i = 1; argv[i]; i++) {
251 strcpy(ret + len, argv[i]);
252 len += strlen(argv[i]);
255 strcpy(ret + len, extra);
259 static struct test *find_test(const char *name)
263 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
264 if (strcmp(tests[i].name, name) == 0)
270 #define PRE_BOILERPLATE "/* Test program generated by configurator. */\n"
271 #define MAIN_START_BOILERPLATE "int main(int argc, char *argv[]) {\n"
272 #define USE_FUNC_BOILERPLATE "(void)func;\n"
273 #define MAIN_BODY_BOILERPLATE "return 0;\n"
274 #define MAIN_END_BOILERPLATE "}\n"
276 static bool run_test(const char *cmd, struct test *test)
285 if (test->depends && !run_test(cmd, find_test(test->depends))) {
286 test->answer = false;
291 outf = fopen(INPUT_FILE, "w");
293 err(1, "creating %s", INPUT_FILE);
295 fprintf(outf, "%s", PRE_BOILERPLATE);
296 switch (test->style & ~(EXECUTE|MAY_NOT_COMPILE)) {
298 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
299 fprintf(outf, "%s", test->fragment);
300 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
303 fprintf(outf, "%s", test->fragment);
304 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
305 fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
306 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
309 fprintf(outf, "%s", test->fragment);
310 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
311 fprintf(outf, "%s", USE_FUNC_BOILERPLATE);
312 fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
313 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
315 case DEFINES_EVERYTHING:
316 fprintf(outf, "%s", test->fragment);
325 if (system("cat " INPUT_FILE) == -1);
327 output = run(cmd, &status);
328 if (status != 0 || strstr(output, "warning")) {
330 printf("Compile %s for %s, status %i: %s\n",
331 status ? "fail" : "warning",
332 test->name, status, output);
333 if ((test->style & EXECUTE) && !(test->style & MAY_NOT_COMPILE))
334 errx(1, "Test for %s did not compile:\n%s",
336 test->answer = false;
339 /* Compile succeeded. */
341 /* We run INSIDE_MAIN tests for sanity checking. */
342 if ((test->style & EXECUTE) || (test->style & INSIDE_MAIN)) {
343 output = run("./" OUTPUT_FILE, &status);
344 if (!(test->style & EXECUTE) && status != 0)
345 errx(1, "Test for %s failed with %i:\n%s",
346 test->name, status, output);
347 if (verbose && status)
348 printf("%s exited %i\n", test->name, status);
351 test->answer = (status == 0);
357 int main(int argc, const char *argv[])
361 const char *default_args[]
362 = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL };
365 if (strcmp(argv[1], "--help") == 0) {
366 printf("Usage: configurator [-v] [<compiler> <flags>...]\n"
367 " <compiler> <flags> will have \"-o <outfile> <infile.c>\" appended\n"
369 DEFAULT_COMPILER, DEFAULT_FLAGS);
372 if (strcmp(argv[1], "-v") == 0) {
376 } else if (strcmp(argv[1], "-vv") == 0) {
386 cmd = connect_args(argv, "-o " OUTPUT_FILE " " INPUT_FILE);
387 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
388 run_test(cmd, &tests[i]);
393 cmd[strlen(cmd) - strlen(" -o " OUTPUT_FILE " " INPUT_FILE)] = '\0';
394 printf("/* Generated by CCAN configurator */\n"
395 "#ifndef CCAN_CONFIG_H\n"
396 "#define CCAN_CONFIG_H\n");
397 printf("#define _GNU_SOURCE /* Always use GNU extensions. */\n");
398 printf("#define CCAN_COMPILER \"%s\"\n", argv[1]);
399 printf("#define CCAN_CFLAGS \"%s\"\n\n", cmd + strlen(argv[1]) + 1);
400 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
401 printf("#define %s %u\n", tests[i].name, tests[i].answer);
402 printf("#endif /* CCAN_CONFIG_H */\n");