configurator.c 12 KB

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