configurator.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* Simple tool to create config.h.
  2. * Would be much easier with ccan modules, but deliberately standalone.
  3. *
  4. * Copyright 2011 Rusty Russell <rusty@rustcorp.com.au>. MIT license.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include <stdio.h>
  25. #include <stdbool.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <err.h>
  29. #include <sys/types.h>
  30. #include <sys/wait.h>
  31. #include <sys/stat.h>
  32. #include <fcntl.h>
  33. #include <string.h>
  34. #define DEFAULT_COMPILER "cc"
  35. #define DEFAULT_FLAGS "-g3 -ggdb -Wall -Wundef -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes -Wold-style-definition"
  36. #define OUTPUT_FILE "configurator.out"
  37. #define INPUT_FILE "configuratortest.c"
  38. static int verbose;
  39. enum test_style {
  40. OUTSIDE_MAIN = 0x1,
  41. DEFINES_FUNC = 0x2,
  42. INSIDE_MAIN = 0x4,
  43. DEFINES_EVERYTHING = 0x8,
  44. MAY_NOT_COMPILE = 0x10,
  45. EXECUTE = 0x8000
  46. };
  47. struct test {
  48. const char *name;
  49. enum test_style style;
  50. const char *depends;
  51. const char *fragment;
  52. bool done;
  53. bool answer;
  54. };
  55. static struct test tests[] = {
  56. { "HAVE_32BIT_OFF_T", DEFINES_EVERYTHING|EXECUTE, NULL,
  57. "#include <sys/types.h>\n"
  58. "int main(int argc, char *argv[]) {\n"
  59. " return sizeof(off_t) == 4 ? 0 : 1;\n"
  60. "}\n" },
  61. { "HAVE_ALIGNOF", INSIDE_MAIN, NULL,
  62. "return __alignof__(double) > 0 ? 0 : 1;" },
  63. { "HAVE_ASPRINTF", DEFINES_FUNC, NULL,
  64. "#define _GNU_SOURCE\n"
  65. "#include <stdio.h>\n"
  66. "static char *func(int x) {"
  67. " char *p;\n"
  68. " if (asprintf(&p, \"%u\", x) == -1) p = NULL;"
  69. " return p;\n"
  70. "}" },
  71. { "HAVE_ATTRIBUTE_COLD", DEFINES_FUNC, NULL,
  72. "static int __attribute__((cold)) func(int x) { return x; }" },
  73. { "HAVE_ATTRIBUTE_CONST", DEFINES_FUNC, NULL,
  74. "static int __attribute__((const)) func(int x) { return x; }" },
  75. { "HAVE_ATTRIBUTE_MAY_ALIAS", OUTSIDE_MAIN, NULL,
  76. "typedef short __attribute__((__may_alias__)) short_a;" },
  77. { "HAVE_ATTRIBUTE_NORETURN", DEFINES_FUNC, NULL,
  78. "#include <stdlib.h>\n"
  79. "static void __attribute__((noreturn)) func(int x) { exit(x); }" },
  80. { "HAVE_ATTRIBUTE_PRINTF", DEFINES_FUNC, NULL,
  81. "static void __attribute__((format(__printf__, 1, 2))) func(const char *fmt, ...) { }" },
  82. { "HAVE_ATTRIBUTE_UNUSED", OUTSIDE_MAIN, NULL,
  83. "static int __attribute__((unused)) func(int x) { return x; }" },
  84. { "HAVE_ATTRIBUTE_USED", OUTSIDE_MAIN, NULL,
  85. "static int __attribute__((used)) func(int x) { return x; }" },
  86. { "HAVE_BACKTRACE", DEFINES_FUNC, NULL,
  87. "#include <execinfo.h>\n"
  88. "static int func(int x) {"
  89. " void *bt[10];\n"
  90. " return backtrace(bt, 10) < x;\n"
  91. "}" },
  92. { "HAVE_BIG_ENDIAN", INSIDE_MAIN|EXECUTE, NULL,
  93. "union { int i; char c[sizeof(int)]; } u;\n"
  94. "u.i = 0x01020304;\n"
  95. "return u.c[0] == 0x01 && u.c[1] == 0x02 && u.c[2] == 0x03 && u.c[3] == 0x04 ? 0 : 1;" },
  96. { "HAVE_BSWAP_64", DEFINES_FUNC, "HAVE_BYTESWAP_H",
  97. "#include <byteswap.h>\n"
  98. "static int func(int x) { return bswap_64(x); }" },
  99. { "HAVE_BUILTIN_CHOOSE_EXPR", INSIDE_MAIN, NULL,
  100. "return __builtin_choose_expr(1, 0, \"garbage\");" },
  101. { "HAVE_BUILTIN_CLZ", INSIDE_MAIN, NULL,
  102. "return __builtin_clz(1) == (sizeof(int)*8 - 1) ? 0 : 1;" },
  103. { "HAVE_BUILTIN_CLZL", INSIDE_MAIN, NULL,
  104. "return __builtin_clzl(1) == (sizeof(long)*8 - 1) ? 0 : 1;" },
  105. { "HAVE_BUILTIN_CLZLL", INSIDE_MAIN, NULL,
  106. "return __builtin_clzll(1) == (sizeof(long long)*8 - 1) ? 0 : 1;" },
  107. { "HAVE_BUILTIN_CONSTANT_P", INSIDE_MAIN, NULL,
  108. "return __builtin_constant_p(1) ? 0 : 1;" },
  109. { "HAVE_BUILTIN_EXPECT", INSIDE_MAIN, NULL,
  110. "return __builtin_expect(argc == 1, 1) ? 0 : 1;" },
  111. { "HAVE_BUILTIN_FFSL", INSIDE_MAIN, NULL,
  112. "return __builtin_ffsl(0L) == 0 ? 0 : 1;" },
  113. { "HAVE_BUILTIN_FFSLL", INSIDE_MAIN, NULL,
  114. "return __builtin_ffsll(0LL) == 0 ? 0 : 1;" },
  115. { "HAVE_BUILTIN_POPCOUNTL", INSIDE_MAIN, NULL,
  116. "return __builtin_popcountl(255L) == 8 ? 0 : 1;" },
  117. { "HAVE_BUILTIN_TYPES_COMPATIBLE_P", INSIDE_MAIN, NULL,
  118. "return __builtin_types_compatible_p(char *, int) ? 1 : 0;" },
  119. { "HAVE_BYTESWAP_H", OUTSIDE_MAIN, NULL,
  120. "#include <byteswap.h>\n" },
  121. { "HAVE_COMPOUND_LITERALS", INSIDE_MAIN, NULL,
  122. "int *foo = (int[]) { 1, 2, 3, 4 };\n"
  123. "return foo[0] ? 0 : 1;" },
  124. { "HAVE_ERR_H", DEFINES_FUNC, NULL,
  125. "#include <err.h>\n"
  126. "static void func(int arg) {\n"
  127. " if (arg == 0)\n"
  128. " err(1, \"err %u\", arg);\n"
  129. " if (arg == 1)\n"
  130. " errx(1, \"err %u\", arg);\n"
  131. " if (arg == 3)\n"
  132. " warn(\"warn %u\", arg);\n"
  133. " if (arg == 4)\n"
  134. " warnx(\"warn %u\", arg);\n"
  135. "}\n" },
  136. { "HAVE_FILE_OFFSET_BITS", DEFINES_EVERYTHING|EXECUTE,
  137. "HAVE_32BIT_OFF_T",
  138. "#define _FILE_OFFSET_BITS 64\n"
  139. "#include <sys/types.h>\n"
  140. "int main(int argc, char *argv[]) {\n"
  141. " return sizeof(off_t) == 8 ? 0 : 1;\n"
  142. "}\n" },
  143. { "HAVE_FOR_LOOP_DECLARATION", INSIDE_MAIN, NULL,
  144. "for (int i = 0; i < argc; i++) { return 0; };\n"
  145. "return 1;" },
  146. { "HAVE_FLEXIBLE_ARRAY_MEMBER", OUTSIDE_MAIN, NULL,
  147. "struct foo { unsigned int x; int arr[]; };" },
  148. { "HAVE_GETPAGESIZE", DEFINES_FUNC, NULL,
  149. "#include <unistd.h>\n"
  150. "static int func(void) { return getpagesize(); }" },
  151. { "HAVE_ISBLANK", DEFINES_FUNC, NULL,
  152. "#define _GNU_SOURCE\n"
  153. "#include <ctype.h>\n"
  154. "static int func(void) { return isblank(' '); }" },
  155. { "HAVE_LITTLE_ENDIAN", INSIDE_MAIN|EXECUTE, NULL,
  156. "union { int i; char c[sizeof(int)]; } u;\n"
  157. "u.i = 0x01020304;\n"
  158. "return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;" },
  159. { "HAVE_MEMMEM", DEFINES_FUNC, NULL,
  160. "#define _GNU_SOURCE\n"
  161. "#include <string.h>\n"
  162. "static void *func(void *h, size_t hl, void *n, size_t nl) {\n"
  163. "return memmem(h, hl, n, nl);"
  164. "}\n", },
  165. { "HAVE_MMAP", DEFINES_FUNC, NULL,
  166. "#include <sys/mman.h>\n"
  167. "static void *func(int fd) {\n"
  168. " return mmap(0, 65536, PROT_READ, MAP_SHARED, fd, 0);\n"
  169. "}" },
  170. { "HAVE_PROC_SELF_MAPS", DEFINES_EVERYTHING|EXECUTE, NULL,
  171. "#include <sys/types.h>\n"
  172. "#include <sys/stat.h>\n"
  173. "#include <fcntl.h>\n"
  174. "int main(void) {\n"
  175. " return open(\"/proc/self/maps\", O_RDONLY) != -1 ? 0 : 1;\n"
  176. "}\n" },
  177. { "HAVE_QSORT_R_PRIVATE_LAST",
  178. DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE, NULL,
  179. "#define _GNU_SOURCE 1\n"
  180. "#include <stdlib.h>\n"
  181. "static int cmp(const void *lp, const void *rp, void *priv) {\n"
  182. " *(unsigned int *)priv = 1;\n"
  183. " return *(const int *)lp - *(const int *)rp; }\n"
  184. "int main(void) {\n"
  185. " int array[] = { 9, 2, 5 };\n"
  186. " unsigned int called = 0;\n"
  187. " qsort_r(array, 3, sizeof(int), cmp, &called);\n"
  188. " return called && array[0] == 2 && array[1] == 5 && array[2] == 9 ? 0 : 1;\n"
  189. "}\n" },
  190. { "HAVE_SECTION_START_STOP",
  191. DEFINES_FUNC, NULL,
  192. "static void *__attribute__((__section__(\"mysec\"))) p = &p;\n"
  193. "static int func(void) {\n"
  194. " extern void *__start_mysec[], *__stop_mysec[];\n"
  195. " return __stop_mysec - __start_mysec;\n"
  196. "}\n" },
  197. { "HAVE_STACK_GROWS_UPWARDS", DEFINES_EVERYTHING|EXECUTE, NULL,
  198. "static long nest(const void *base, unsigned int i)\n"
  199. "{\n"
  200. " if (i == 0)\n"
  201. " return (const char *)&i - (const char *)base;\n"
  202. " return nest(base, i-1);\n"
  203. "}\n"
  204. "int main(int argc, char *argv[]) {\n"
  205. " return (nest(&argc, argc) > 0) ? 0 : 1\n;"
  206. "}\n" },
  207. { "HAVE_STATEMENT_EXPR", INSIDE_MAIN, NULL,
  208. "return ({ int x = argc; x == argc ? 0 : 1; });" },
  209. { "HAVE_SYS_FILIO_H", OUTSIDE_MAIN, NULL, /* Solaris needs this for FIONREAD */
  210. "#include <sys/filio.h>\n" },
  211. { "HAVE_TYPEOF", INSIDE_MAIN, NULL,
  212. "__typeof__(argc) i; i = argc; return i == argc ? 0 : 1;" },
  213. { "HAVE_UTIME", DEFINES_FUNC, NULL,
  214. "#include <sys/types.h>\n"
  215. "#include <utime.h>\n"
  216. "static int func(const char *filename) {\n"
  217. " struct utimbuf times = { 0 };\n"
  218. " return utime(filename, &times);\n"
  219. "}" },
  220. { "HAVE_WARN_UNUSED_RESULT", DEFINES_FUNC, NULL,
  221. "#include <sys/types.h>\n"
  222. "#include <utime.h>\n"
  223. "static __attribute__((warn_unused_result)) int func(int i) {\n"
  224. " return i + 1;\n"
  225. "}" },
  226. };
  227. static char *grab_fd(int fd)
  228. {
  229. int ret;
  230. size_t max, size = 0;
  231. char *buffer;
  232. max = 16384;
  233. buffer = malloc(max+1);
  234. while ((ret = read(fd, buffer + size, max - size)) > 0) {
  235. size += ret;
  236. if (size == max)
  237. buffer = realloc(buffer, max *= 2);
  238. }
  239. if (ret < 0)
  240. err(1, "reading from command");
  241. buffer[size] = '\0';
  242. return buffer;
  243. }
  244. static char *run(const char *cmd, int *exitstatus)
  245. {
  246. pid_t pid;
  247. int p[2];
  248. char *ret;
  249. int status;
  250. if (pipe(p) != 0)
  251. err(1, "creating pipe");
  252. pid = fork();
  253. if (pid == -1)
  254. err(1, "forking");
  255. if (pid == 0) {
  256. if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
  257. || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
  258. || close(p[0]) != 0
  259. || close(STDIN_FILENO) != 0
  260. || open("/dev/null", O_RDONLY) != STDIN_FILENO)
  261. exit(128);
  262. status = system(cmd);
  263. if (WIFEXITED(status))
  264. exit(WEXITSTATUS(status));
  265. /* Here's a hint... */
  266. exit(128 + WTERMSIG(status));
  267. }
  268. close(p[1]);
  269. ret = grab_fd(p[0]);
  270. /* This shouldn't fail... */
  271. if (waitpid(pid, &status, 0) != pid)
  272. err(1, "Failed to wait for child");
  273. close(p[0]);
  274. if (WIFEXITED(status))
  275. *exitstatus = WEXITSTATUS(status);
  276. else
  277. *exitstatus = -WTERMSIG(status);
  278. return ret;
  279. }
  280. static char *connect_args(const char *argv[], const char *extra)
  281. {
  282. unsigned int i, len = strlen(extra) + 1;
  283. char *ret;
  284. for (i = 1; argv[i]; i++)
  285. len += 1 + strlen(argv[i]);
  286. ret = malloc(len);
  287. len = 0;
  288. for (i = 1; argv[i]; i++) {
  289. strcpy(ret + len, argv[i]);
  290. len += strlen(argv[i]);
  291. ret[len++] = ' ';
  292. }
  293. strcpy(ret + len, extra);
  294. return ret;
  295. }
  296. static struct test *find_test(const char *name)
  297. {
  298. unsigned int i;
  299. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
  300. if (strcmp(tests[i].name, name) == 0)
  301. return &tests[i];
  302. }
  303. abort();
  304. }
  305. #define PRE_BOILERPLATE "/* Test program generated by configurator. */\n"
  306. #define MAIN_START_BOILERPLATE "int main(int argc, char *argv[]) {\n"
  307. #define USE_FUNC_BOILERPLATE "(void)func;\n"
  308. #define MAIN_BODY_BOILERPLATE "return 0;\n"
  309. #define MAIN_END_BOILERPLATE "}\n"
  310. static bool run_test(const char *cmd, struct test *test)
  311. {
  312. char *output;
  313. FILE *outf;
  314. int status;
  315. if (test->done)
  316. return test->answer;
  317. if (test->depends && !run_test(cmd, find_test(test->depends))) {
  318. test->answer = false;
  319. test->done = true;
  320. return test->answer;
  321. }
  322. outf = fopen(INPUT_FILE, "w");
  323. if (!outf)
  324. err(1, "creating %s", INPUT_FILE);
  325. fprintf(outf, "%s", PRE_BOILERPLATE);
  326. switch (test->style & ~(EXECUTE|MAY_NOT_COMPILE)) {
  327. case INSIDE_MAIN:
  328. fprintf(outf, "%s", MAIN_START_BOILERPLATE);
  329. fprintf(outf, "%s", test->fragment);
  330. fprintf(outf, "%s", MAIN_END_BOILERPLATE);
  331. break;
  332. case OUTSIDE_MAIN:
  333. fprintf(outf, "%s", test->fragment);
  334. fprintf(outf, "%s", MAIN_START_BOILERPLATE);
  335. fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
  336. fprintf(outf, "%s", MAIN_END_BOILERPLATE);
  337. break;
  338. case DEFINES_FUNC:
  339. fprintf(outf, "%s", test->fragment);
  340. fprintf(outf, "%s", MAIN_START_BOILERPLATE);
  341. fprintf(outf, "%s", USE_FUNC_BOILERPLATE);
  342. fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
  343. fprintf(outf, "%s", MAIN_END_BOILERPLATE);
  344. break;
  345. case DEFINES_EVERYTHING:
  346. fprintf(outf, "%s", test->fragment);
  347. break;
  348. default:
  349. abort();
  350. }
  351. fclose(outf);
  352. if (verbose > 1)
  353. if (system("cat " INPUT_FILE) == -1);
  354. output = run(cmd, &status);
  355. if (status != 0 || strstr(output, "warning")) {
  356. if (verbose)
  357. printf("Compile %s for %s, status %i: %s\n",
  358. status ? "fail" : "warning",
  359. test->name, status, output);
  360. if ((test->style & EXECUTE) && !(test->style & MAY_NOT_COMPILE))
  361. errx(1, "Test for %s did not compile:\n%s",
  362. test->name, output);
  363. test->answer = false;
  364. free(output);
  365. } else {
  366. /* Compile succeeded. */
  367. free(output);
  368. /* We run INSIDE_MAIN tests for sanity checking. */
  369. if ((test->style & EXECUTE) || (test->style & INSIDE_MAIN)) {
  370. output = run("./" OUTPUT_FILE, &status);
  371. if (!(test->style & EXECUTE) && status != 0)
  372. errx(1, "Test for %s failed with %i:\n%s",
  373. test->name, status, output);
  374. if (verbose && status)
  375. printf("%s exited %i\n", test->name, status);
  376. free(output);
  377. }
  378. test->answer = (status == 0);
  379. }
  380. test->done = true;
  381. return test->answer;
  382. }
  383. int main(int argc, const char *argv[])
  384. {
  385. char *cmd;
  386. unsigned int i;
  387. const char *default_args[]
  388. = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL };
  389. if (argc > 1) {
  390. if (strcmp(argv[1], "--help") == 0) {
  391. printf("Usage: configurator [-v] [<compiler> <flags>...]\n"
  392. " <compiler> <flags> will have \"-o <outfile> <infile.c>\" appended\n"
  393. "Default: %s %s\n",
  394. DEFAULT_COMPILER, DEFAULT_FLAGS);
  395. exit(0);
  396. }
  397. if (strcmp(argv[1], "-v") == 0) {
  398. argc--;
  399. argv++;
  400. verbose = 1;
  401. } else if (strcmp(argv[1], "-vv") == 0) {
  402. argc--;
  403. argv++;
  404. verbose = 2;
  405. }
  406. }
  407. if (argc == 1)
  408. argv = default_args;
  409. cmd = connect_args(argv, "-o " OUTPUT_FILE " " INPUT_FILE);
  410. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
  411. run_test(cmd, &tests[i]);
  412. unlink(OUTPUT_FILE);
  413. unlink(INPUT_FILE);
  414. cmd[strlen(cmd) - strlen(" -o " OUTPUT_FILE " " INPUT_FILE)] = '\0';
  415. printf("/* Generated by CCAN configurator */\n"
  416. "#ifndef CCAN_CONFIG_H\n"
  417. "#define CCAN_CONFIG_H\n");
  418. printf("#ifndef _GNU_SOURCE\n");
  419. printf("#define _GNU_SOURCE /* Always use GNU extensions. */\n");
  420. printf("#endif\n");
  421. printf("#define CCAN_COMPILER \"%s\"\n", argv[1]);
  422. printf("#define CCAN_CFLAGS \"%s\"\n\n", cmd + strlen(argv[1]) + 1);
  423. /* This one implies "#include <ccan/..." works, eg. for tdb2.h */
  424. printf("#define HAVE_CCAN 1\n");
  425. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
  426. printf("#define %s %u\n", tests[i].name, tests[i].answer);
  427. printf("#endif /* CCAN_CONFIG_H */\n");
  428. return 0;
  429. }