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,
32 enum test_style style;
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) {"
47 " if (asprintf(&p, \"%u\", x) == -1) p = NULL;"
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"
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"
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_LITTLE_ENDIAN", INSIDE_MAIN|EXECUTE, NULL,
106 "union { int i; char c[sizeof(int)]; } u;\n"
107 "u.i = 0x01020304;\n"
108 "return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;" },
109 { "HAVE_MEMMEM", DEFINES_FUNC, NULL,
110 "#define _GNU_SOURCE\n"
111 "#include <string.h>\n"
112 "static void *func(void *h, size_t hl, void *n, size_t nl) {\n"
113 "return memmem(h, hl, n, nl);"
115 { "HAVE_MMAP", DEFINES_FUNC, NULL,
116 "#include <sys/mman.h>\n"
117 "static void *func(int fd) {\n"
118 " return mmap(0, 65536, PROT_READ, MAP_SHARED, fd, 0);\n"
120 { "HAVE_NESTED_FUNCTIONS", DEFINES_FUNC, NULL,
121 "void (*func(int val))(int);\n"
122 "void (*func(int val))(int) {\n"
123 " auto void add(int val2);\n"
124 " void add(int val2) { val += val2; }\n"
127 { "HAVE_STACK_GROWS_UPWARDS", DEFINES_EVERYTHING|EXECUTE, NULL,
128 "static long nest(const void *base, unsigned int i)\n"
131 " return (const char *)&i - (const char *)base;\n"
132 " return nest(base, i-1);\n"
134 "int main(int argc, char *argv[]) {\n"
135 " return (nest(&argc, argc) > 0) ? 0 : 1\n;"
137 { "HAVE_STATEMENT_EXPR", INSIDE_MAIN, NULL,
138 "return ({ int x = argc; x == argc ? 0 : 1; });" },
139 { "HAVE_TYPEOF", INSIDE_MAIN, NULL,
140 "__typeof__(argc) i; i = argc; return i == argc ? 0 : 1;" },
141 { "HAVE_UTIME", DEFINES_FUNC, NULL,
142 "#include <sys/types.h>\n"
143 "#include <utime.h>\n"
144 "static int func(const char *filename) {\n"
145 " struct utimbuf times = { 0 };\n"
146 " return utime(filename, ×);\n"
148 { "HAVE_WARN_UNUSED_RESULT", DEFINES_FUNC, NULL,
149 "#include <sys/types.h>\n"
150 "#include <utime.h>\n"
151 "static __attribute__((warn_unused_result)) int func(int i) {\n"
156 static char *grab_fd(int fd)
159 size_t max, size = 0;
163 buffer = malloc(max+1);
164 while ((ret = read(fd, buffer + size, max - size)) > 0) {
167 buffer = realloc(buffer, max *= 2);
170 err(1, "reading from command");
175 static char *run(const char *cmd, int *exitstatus)
183 err(1, "creating pipe");
190 if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
191 || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
193 || close(STDIN_FILENO) != 0
194 || open("/dev/null", O_RDONLY) != STDIN_FILENO)
197 status = system(cmd);
198 if (WIFEXITED(status))
199 exit(WEXITSTATUS(status));
200 /* Here's a hint... */
201 exit(128 + WTERMSIG(status));
206 /* This shouldn't fail... */
207 if (waitpid(pid, &status, 0) != pid)
208 err(1, "Failed to wait for child");
210 if (WIFEXITED(status))
211 *exitstatus = WEXITSTATUS(status);
213 *exitstatus = -WTERMSIG(status);
217 static char *connect_args(const char *argv[], const char *extra)
219 unsigned int i, len = strlen(extra) + 1;
222 for (i = 1; argv[i]; i++)
223 len += 1 + strlen(argv[i]);
227 for (i = 1; argv[i]; i++) {
228 strcpy(ret + len, argv[i]);
229 len += strlen(argv[i]);
232 strcpy(ret + len, extra);
236 static struct test *find_test(const char *name)
240 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
241 if (strcmp(tests[i].name, name) == 0)
247 #define PRE_BOILERPLATE "/* Test program generated by configurator. */\n"
248 #define MAIN_START_BOILERPLATE "int main(int argc, char *argv[]) {\n"
249 #define USE_FUNC_BOILERPLATE "(void)func;\n"
250 #define MAIN_BODY_BOILERPLATE "return 0;\n"
251 #define MAIN_END_BOILERPLATE "}\n"
253 static bool run_test(const char *cmd, struct test *test)
262 if (test->depends && !run_test(cmd, find_test(test->depends))) {
263 test->answer = false;
268 outf = fopen(INPUT_FILE, "w");
270 err(1, "creating %s", INPUT_FILE);
272 fprintf(outf, "%s", PRE_BOILERPLATE);
273 switch (test->style & ~EXECUTE) {
275 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
276 fprintf(outf, "%s", test->fragment);
277 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
280 fprintf(outf, "%s", test->fragment);
281 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
282 fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
283 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
286 fprintf(outf, "%s", test->fragment);
287 fprintf(outf, "%s", MAIN_START_BOILERPLATE);
288 fprintf(outf, "%s", USE_FUNC_BOILERPLATE);
289 fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
290 fprintf(outf, "%s", MAIN_END_BOILERPLATE);
292 case DEFINES_EVERYTHING:
293 fprintf(outf, "%s", test->fragment);
302 if (system("cat " INPUT_FILE) == -1);
304 output = run(cmd, &status);
305 if (status != 0 || strstr(output, "warning")) {
307 printf("Compile %s for %s, status %i: %s\n",
308 status ? "fail" : "warning",
309 test->name, status, output);
310 if (test->style & EXECUTE)
311 errx(1, "Test for %s did not compile:\n%s",
313 test->answer = false;
316 /* Compile succeeded. */
318 /* We run INSIDE_MAIN tests for sanity checking. */
319 if ((test->style & EXECUTE) || (test->style & INSIDE_MAIN)) {
320 output = run("./" OUTPUT_FILE, &status);
321 if (!(test->style & EXECUTE) && status != 0)
322 errx(1, "Test for %s failed with %i:\n%s",
323 test->name, status, output);
324 if (verbose && status)
325 printf("%s exited %i\n", test->name, status);
328 test->answer = (status == 0);
334 int main(int argc, const char *argv[])
338 const char *default_args[]
339 = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL };
342 if (strcmp(argv[1], "--help") == 0) {
343 printf("Usage: configurator [-v] [<compiler> <flags>...]\n"
344 " <compiler> <flags> will have \"-o <outfile> <infile.c>\" appended\n"
346 DEFAULT_COMPILER, DEFAULT_FLAGS);
349 if (strcmp(argv[1], "-v") == 0) {
353 } else if (strcmp(argv[1], "-vv") == 0) {
363 cmd = connect_args(argv, "-o " OUTPUT_FILE " " INPUT_FILE);
364 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
365 run_test(cmd, &tests[i]);
370 cmd[strlen(cmd) - strlen(" -o " OUTPUT_FILE " " INPUT_FILE)] = '\0';
371 printf("/* Generated by CCAN configurator */\n"
372 "#ifndef CCAN_CONFIG_H\n"
373 "#define CCAN_CONFIG_H\n");
374 printf("#define _GNU_SOURCE /* Always use GNU extensions. */\n");
375 printf("#define CCAN_COMPILER \"%s\"\n", argv[1]);
376 printf("#define CCAN_CFLAGS \"%s\"\n\n", cmd + strlen(argv[1]) + 1);
377 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
378 printf("#define %s %u\n", tests[i].name, tests[i].answer);
379 printf("#endif /* CCAN_CONFIG_H */\n");