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