configurator.c 12 KB

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