configurator.c 11 KB

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