configurator.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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. * c12r_err, c12r_errx functions copied from ccan/err/err.c
  7. * Copyright Rusty Russell <rusty@rustcorp.com.au>. CC0 (Public domain) License.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #define _POSIX_C_SOURCE 200809L /* For pclose, popen, strdup */
  28. #include <errno.h>
  29. #include <stdio.h>
  30. #include <stdarg.h>
  31. #include <stdbool.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #ifdef _MSC_VER
  35. #define popen _popen
  36. #define pclose _pclose
  37. #endif
  38. #define DEFAULT_COMPILER "cc"
  39. #define DEFAULT_FLAGS "-g3 -ggdb -Wall -Wundef -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes -Wold-style-definition"
  40. #define DEFAULT_OUTPUT_EXE_FLAG "-o"
  41. #define OUTPUT_FILE "configurator.out"
  42. #define INPUT_FILE "configuratortest.c"
  43. #ifdef _WIN32
  44. #define DIR_SEP "\\"
  45. #else
  46. #define DIR_SEP "/"
  47. #endif
  48. static const char *progname = "";
  49. static int verbose;
  50. enum test_style {
  51. OUTSIDE_MAIN = 0x1,
  52. DEFINES_FUNC = 0x2,
  53. INSIDE_MAIN = 0x4,
  54. DEFINES_EVERYTHING = 0x8,
  55. MAY_NOT_COMPILE = 0x10,
  56. EXECUTE = 0x8000
  57. };
  58. struct test {
  59. const char *name;
  60. enum test_style style;
  61. const char *depends;
  62. const char *link;
  63. const char *fragment;
  64. const char *flags;
  65. const char *overrides; /* On success, force this to '1' */
  66. bool done;
  67. bool answer;
  68. };
  69. static struct test tests[] = {
  70. { "HAVE_32BIT_OFF_T", DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE, NULL, NULL,
  71. "#include <sys/types.h>\n"
  72. "int main(void) {\n"
  73. " return sizeof(off_t) == 4 ? 0 : 1;\n"
  74. "}\n" },
  75. { "HAVE_ALIGNOF", INSIDE_MAIN, NULL, NULL,
  76. "return __alignof__(double) > 0 ? 0 : 1;" },
  77. { "HAVE_ASPRINTF", DEFINES_FUNC, NULL, NULL,
  78. "#ifndef _GNU_SOURCE\n"
  79. "#define _GNU_SOURCE\n"
  80. "#endif\n"
  81. "#include <stdio.h>\n"
  82. "static char *func(int x) {"
  83. " char *p;\n"
  84. " if (asprintf(&p, \"%u\", x) == -1) p = NULL;"
  85. " return p;\n"
  86. "}" },
  87. { "HAVE_ATTRIBUTE_COLD", DEFINES_FUNC, NULL, NULL,
  88. "static int __attribute__((cold)) func(int x) { return x; }" },
  89. { "HAVE_ATTRIBUTE_CONST", DEFINES_FUNC, NULL, NULL,
  90. "static int __attribute__((const)) func(int x) { return x; }" },
  91. { "HAVE_ATTRIBUTE_PURE", DEFINES_FUNC, NULL, NULL,
  92. "static int __attribute__((pure)) func(int x) { return x; }" },
  93. { "HAVE_ATTRIBUTE_MAY_ALIAS", OUTSIDE_MAIN, NULL, NULL,
  94. "typedef short __attribute__((__may_alias__)) short_a;" },
  95. { "HAVE_ATTRIBUTE_NORETURN", DEFINES_FUNC, NULL, NULL,
  96. "#include <stdlib.h>\n"
  97. "static void __attribute__((noreturn)) func(int x) { exit(x); }" },
  98. { "HAVE_ATTRIBUTE_PRINTF", DEFINES_FUNC, NULL, NULL,
  99. "static void __attribute__((format(__printf__, 1, 2))) func(const char *fmt, ...) { (void)fmt; }" },
  100. { "HAVE_ATTRIBUTE_UNUSED", OUTSIDE_MAIN, NULL, NULL,
  101. "static int __attribute__((unused)) func(int x) { return x; }" },
  102. { "HAVE_ATTRIBUTE_USED", OUTSIDE_MAIN, NULL, NULL,
  103. "static int __attribute__((used)) func(int x) { return x; }" },
  104. { "HAVE_BACKTRACE", DEFINES_FUNC, NULL, NULL,
  105. "#include <execinfo.h>\n"
  106. "static int func(int x) {"
  107. " void *bt[10];\n"
  108. " return backtrace(bt, 10) < x;\n"
  109. "}" },
  110. { "HAVE_BIG_ENDIAN", INSIDE_MAIN|EXECUTE, NULL, NULL,
  111. "union { int i; char c[sizeof(int)]; } u;\n"
  112. "u.i = 0x01020304;\n"
  113. "return u.c[0] == 0x01 && u.c[1] == 0x02 && u.c[2] == 0x03 && u.c[3] == 0x04 ? 0 : 1;" },
  114. { "HAVE_BSWAP_64", DEFINES_FUNC, "HAVE_BYTESWAP_H", NULL,
  115. "#include <byteswap.h>\n"
  116. "static int func(int x) { return bswap_64(x); }" },
  117. { "HAVE_BUILTIN_CHOOSE_EXPR", INSIDE_MAIN, NULL, NULL,
  118. "return __builtin_choose_expr(1, 0, \"garbage\");" },
  119. { "HAVE_BUILTIN_CLZ", INSIDE_MAIN, NULL, NULL,
  120. "return __builtin_clz(1) == (sizeof(int)*8 - 1) ? 0 : 1;" },
  121. { "HAVE_BUILTIN_CLZL", INSIDE_MAIN, NULL, NULL,
  122. "return __builtin_clzl(1) == (sizeof(long)*8 - 1) ? 0 : 1;" },
  123. { "HAVE_BUILTIN_CLZLL", INSIDE_MAIN, NULL, NULL,
  124. "return __builtin_clzll(1) == (sizeof(long long)*8 - 1) ? 0 : 1;" },
  125. { "HAVE_BUILTIN_CTZ", INSIDE_MAIN, NULL, NULL,
  126. "return __builtin_ctz(1 << (sizeof(int)*8 - 1)) == (sizeof(int)*8 - 1) ? 0 : 1;" },
  127. { "HAVE_BUILTIN_CTZL", INSIDE_MAIN, NULL, NULL,
  128. "return __builtin_ctzl(1UL << (sizeof(long)*8 - 1)) == (sizeof(long)*8 - 1) ? 0 : 1;" },
  129. { "HAVE_BUILTIN_CTZLL", INSIDE_MAIN, NULL, NULL,
  130. "return __builtin_ctzll(1ULL << (sizeof(long long)*8 - 1)) == (sizeof(long long)*8 - 1) ? 0 : 1;" },
  131. { "HAVE_BUILTIN_CONSTANT_P", INSIDE_MAIN, NULL, NULL,
  132. "return __builtin_constant_p(1) ? 0 : 1;" },
  133. { "HAVE_BUILTIN_EXPECT", INSIDE_MAIN, NULL, NULL,
  134. "return __builtin_expect(argc == 1, 1) ? 0 : 1;" },
  135. { "HAVE_BUILTIN_FFS", INSIDE_MAIN, NULL, NULL,
  136. "return __builtin_ffs(0) == 0 ? 0 : 1;" },
  137. { "HAVE_BUILTIN_FFSL", INSIDE_MAIN, NULL, NULL,
  138. "return __builtin_ffsl(0L) == 0 ? 0 : 1;" },
  139. { "HAVE_BUILTIN_FFSLL", INSIDE_MAIN, NULL, NULL,
  140. "return __builtin_ffsll(0LL) == 0 ? 0 : 1;" },
  141. { "HAVE_BUILTIN_POPCOUNTL", INSIDE_MAIN, NULL, NULL,
  142. "return __builtin_popcountl(255L) == 8 ? 0 : 1;" },
  143. { "HAVE_BUILTIN_TYPES_COMPATIBLE_P", INSIDE_MAIN, NULL, NULL,
  144. "return __builtin_types_compatible_p(char *, int) ? 1 : 0;" },
  145. { "HAVE_ICCARM_INTRINSICS", DEFINES_FUNC, NULL, NULL,
  146. "#include <intrinsics.h>\n"
  147. "int func(int v) {\n"
  148. " return __CLZ(__RBIT(v));\n"
  149. "}" },
  150. { "HAVE_BYTESWAP_H", OUTSIDE_MAIN, NULL, NULL,
  151. "#include <byteswap.h>\n" },
  152. { "HAVE_CLOCK_GETTIME",
  153. DEFINES_FUNC, "HAVE_STRUCT_TIMESPEC", NULL,
  154. "#include <time.h>\n"
  155. "static struct timespec func(void) {\n"
  156. " struct timespec ts;\n"
  157. " clock_gettime(CLOCK_REALTIME, &ts);\n"
  158. " return ts;\n"
  159. "}\n" },
  160. { "HAVE_CLOCK_GETTIME_IN_LIBRT",
  161. DEFINES_FUNC,
  162. "HAVE_STRUCT_TIMESPEC !HAVE_CLOCK_GETTIME",
  163. "-lrt",
  164. "#include <time.h>\n"
  165. "static struct timespec func(void) {\n"
  166. " struct timespec ts;\n"
  167. " clock_gettime(CLOCK_REALTIME, &ts);\n"
  168. " return ts;\n"
  169. "}\n",
  170. /* This means HAVE_CLOCK_GETTIME, too */
  171. "HAVE_CLOCK_GETTIME" },
  172. { "HAVE_COMPOUND_LITERALS", INSIDE_MAIN, NULL, NULL,
  173. "int *foo = (int[]) { 1, 2, 3, 4 };\n"
  174. "return foo[0] ? 0 : 1;" },
  175. { "HAVE_FCHDIR", DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE, NULL, NULL,
  176. "#include <sys/types.h>\n"
  177. "#include <sys/stat.h>\n"
  178. "#include <fcntl.h>\n"
  179. "#include <unistd.h>\n"
  180. "int main(void) {\n"
  181. " int fd = open(\"..\", O_RDONLY);\n"
  182. " return fchdir(fd) == 0 ? 0 : 1;\n"
  183. "}\n" },
  184. { "HAVE_ERR_H", DEFINES_FUNC, NULL, NULL,
  185. "#include <err.h>\n"
  186. "static void func(int arg) {\n"
  187. " if (arg == 0)\n"
  188. " err(1, \"err %u\", arg);\n"
  189. " if (arg == 1)\n"
  190. " errx(1, \"err %u\", arg);\n"
  191. " if (arg == 3)\n"
  192. " warn(\"warn %u\", arg);\n"
  193. " if (arg == 4)\n"
  194. " warnx(\"warn %u\", arg);\n"
  195. "}\n" },
  196. { "HAVE_FILE_OFFSET_BITS", DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE,
  197. "HAVE_32BIT_OFF_T", NULL,
  198. "#define _FILE_OFFSET_BITS 64\n"
  199. "#include <sys/types.h>\n"
  200. "int main(void) {\n"
  201. " return sizeof(off_t) == 8 ? 0 : 1;\n"
  202. "}\n" },
  203. { "HAVE_FOR_LOOP_DECLARATION", INSIDE_MAIN, NULL, NULL,
  204. "int ret = 1;\n"
  205. "for (int i = 0; i < argc; i++) { ret = 0; };\n"
  206. "return ret;" },
  207. { "HAVE_FLEXIBLE_ARRAY_MEMBER", OUTSIDE_MAIN, NULL, NULL,
  208. "struct foo { unsigned int x; int arr[]; };" },
  209. { "HAVE_GETPAGESIZE", DEFINES_FUNC, NULL, NULL,
  210. "#include <unistd.h>\n"
  211. "static int func(void) { return getpagesize(); }" },
  212. { "HAVE_ISBLANK", DEFINES_FUNC, NULL, NULL,
  213. "#ifndef _GNU_SOURCE\n"
  214. "#define _GNU_SOURCE\n"
  215. "#endif\n"
  216. "#include <ctype.h>\n"
  217. "static int func(void) { return isblank(' '); }" },
  218. { "HAVE_LITTLE_ENDIAN", INSIDE_MAIN|EXECUTE, NULL, NULL,
  219. "union { int i; char c[sizeof(int)]; } u;\n"
  220. "u.i = 0x01020304;\n"
  221. "return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;" },
  222. { "HAVE_MEMMEM", DEFINES_FUNC, NULL, NULL,
  223. "#ifndef _GNU_SOURCE\n"
  224. "#define _GNU_SOURCE\n"
  225. "#endif\n"
  226. "#include <string.h>\n"
  227. "static void *func(void *h, size_t hl, void *n, size_t nl) {\n"
  228. "return memmem(h, hl, n, nl);"
  229. "}\n", },
  230. { "HAVE_MEMRCHR", DEFINES_FUNC, NULL, NULL,
  231. "#ifndef _GNU_SOURCE\n"
  232. "#define _GNU_SOURCE\n"
  233. "#endif\n"
  234. "#include <string.h>\n"
  235. "static void *func(void *s, int c, size_t n) {\n"
  236. "return memrchr(s, c, n);"
  237. "}\n", },
  238. { "HAVE_MMAP", DEFINES_FUNC, NULL, NULL,
  239. "#include <sys/mman.h>\n"
  240. "static void *func(int fd) {\n"
  241. " return mmap(0, 65536, PROT_READ, MAP_SHARED, fd, 0);\n"
  242. "}" },
  243. { "HAVE_PROC_SELF_MAPS", DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE, NULL, NULL,
  244. "#include <sys/types.h>\n"
  245. "#include <sys/stat.h>\n"
  246. "#include <fcntl.h>\n"
  247. "int main(void) {\n"
  248. " return open(\"/proc/self/maps\", O_RDONLY) != -1 ? 0 : 1;\n"
  249. "}\n" },
  250. { "HAVE_QSORT_R_PRIVATE_LAST",
  251. DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE, NULL, NULL,
  252. "#ifndef _GNU_SOURCE\n"
  253. "#define _GNU_SOURCE\n"
  254. "#endif\n"
  255. "#include <stdlib.h>\n"
  256. "static int cmp(const void *lp, const void *rp, void *priv) {\n"
  257. " *(unsigned int *)priv = 1;\n"
  258. " return *(const int *)lp - *(const int *)rp; }\n"
  259. "int main(void) {\n"
  260. " int array[] = { 9, 2, 5 };\n"
  261. " unsigned int called = 0;\n"
  262. " qsort_r(array, 3, sizeof(int), cmp, &called);\n"
  263. " return called && array[0] == 2 && array[1] == 5 && array[2] == 9 ? 0 : 1;\n"
  264. "}\n" },
  265. { "HAVE_STRUCT_TIMESPEC",
  266. DEFINES_FUNC, NULL, NULL,
  267. "#include <time.h>\n"
  268. "static void func(void) {\n"
  269. " struct timespec ts;\n"
  270. " ts.tv_sec = ts.tv_nsec = 1;\n"
  271. "}\n" },
  272. { "HAVE_SECTION_START_STOP",
  273. DEFINES_FUNC, NULL, NULL,
  274. "static void *__attribute__((__section__(\"mysec\"))) p = &p;\n"
  275. "static int func(void) {\n"
  276. " extern void *__start_mysec[], *__stop_mysec[];\n"
  277. " return __stop_mysec - __start_mysec;\n"
  278. "}\n" },
  279. { "HAVE_STACK_GROWS_UPWARDS", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
  280. "#include <stddef.h>\n"
  281. "static ptrdiff_t nest(const void *base, unsigned int i)\n"
  282. "{\n"
  283. " if (i == 0)\n"
  284. " return (const char *)&i - (const char *)base;\n"
  285. " return nest(base, i-1);\n"
  286. "}\n"
  287. "int main(int argc, char *argv[]) {\n"
  288. " (void)argv;\n"
  289. " return (nest(&argc, argc) > 0) ? 0 : 1;\n"
  290. "}\n" },
  291. { "HAVE_STATEMENT_EXPR", INSIDE_MAIN, NULL, NULL,
  292. "return ({ int x = argc; x == argc ? 0 : 1; });" },
  293. { "HAVE_SYS_FILIO_H", OUTSIDE_MAIN, NULL, NULL, /* Solaris needs this for FIONREAD */
  294. "#include <sys/filio.h>\n" },
  295. { "HAVE_SYS_TERMIOS_H", OUTSIDE_MAIN, NULL, NULL,
  296. "#include <sys/termios.h>\n" },
  297. { "HAVE_TYPEOF", INSIDE_MAIN, NULL, NULL,
  298. "__typeof__(argc) i; i = argc; return i == argc ? 0 : 1;" },
  299. { "HAVE_UNALIGNED_ACCESS", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
  300. "#include <string.h>\n"
  301. "int main(int argc, char *argv[]) {\n"
  302. " (void)argc;\n"
  303. " char pad[sizeof(int *) * 1];\n"
  304. " strncpy(pad, argv[0], sizeof(pad));\n"
  305. " int *x = (int *)pad, *y = (int *)(pad + 1);\n"
  306. " return *x == *y;\n"
  307. "}\n" },
  308. { "HAVE_UTIME", DEFINES_FUNC, NULL, NULL,
  309. "#include <sys/types.h>\n"
  310. "#include <utime.h>\n"
  311. "static int func(const char *filename) {\n"
  312. " struct utimbuf times = { 0 };\n"
  313. " return utime(filename, &times);\n"
  314. "}" },
  315. { "HAVE_WARN_UNUSED_RESULT", DEFINES_FUNC, NULL, NULL,
  316. "#include <sys/types.h>\n"
  317. "#include <utime.h>\n"
  318. "static __attribute__((warn_unused_result)) int func(int i) {\n"
  319. " return i + 1;\n"
  320. "}" },
  321. { "HAVE_OPENMP", INSIDE_MAIN, NULL, NULL,
  322. "int i;\n"
  323. "#pragma omp parallel for\n"
  324. "for(i = 0; i < 0; i++) {};\n"
  325. "return 0;\n",
  326. "-Werror -fopenmp" },
  327. { "HAVE_VALGRIND_MEMCHECK_H", OUTSIDE_MAIN, NULL, NULL,
  328. "#include <valgrind/memcheck.h>\n" },
  329. { "HAVE_UCONTEXT", DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE,
  330. NULL, NULL,
  331. "#include <ucontext.h>\n"
  332. "static int x = 0;\n"
  333. "static char stack[2048];\n"
  334. "static ucontext_t a, b;\n"
  335. "static void fn(void) {\n"
  336. " x |= 2;\n"
  337. " setcontext(&b);\n"
  338. " x |= 4;\n"
  339. "}\n"
  340. "int main(void) {\n"
  341. " x |= 1;\n"
  342. " getcontext(&a);\n"
  343. " a.uc_stack.ss_sp = stack;\n"
  344. " a.uc_stack.ss_size = sizeof(stack);\n"
  345. " makecontext(&a, fn, 0);\n"
  346. " swapcontext(&b, &a);\n"
  347. " return (x == 3) ? 0 : 1;\n"
  348. "}\n"
  349. },
  350. { "HAVE_POINTER_SAFE_MAKECONTEXT", DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE,
  351. "HAVE_UCONTEXT", NULL,
  352. "#include <stddef.h>\n"
  353. "#include <ucontext.h>\n"
  354. "static int worked = 0;\n"
  355. "static char stack[1024];\n"
  356. "static ucontext_t a, b;\n"
  357. "static void fn(void *p, void *q) {\n"
  358. " void *cp = &worked;\n"
  359. " void *cq = (void *)(~((ptrdiff_t)cp));\n"
  360. " if ((p == cp) && (q == cq))\n"
  361. " worked = 1;\n"
  362. " setcontext(&b);\n"
  363. "}\n"
  364. "int main(void) {\n"
  365. " void *ap = &worked;\n"
  366. " void *aq = (void *)(~((ptrdiff_t)ap));\n"
  367. " getcontext(&a);\n"
  368. " a.uc_stack.ss_sp = stack;\n"
  369. " a.uc_stack.ss_size = sizeof(stack);\n"
  370. " makecontext(&a, (void (*)(void))fn, 2, ap, aq);\n"
  371. " swapcontext(&b, &a);\n"
  372. " return worked ? 0 : 1;\n"
  373. "}\n"
  374. },
  375. };
  376. static void c12r_err(int eval, const char *fmt, ...)
  377. {
  378. int err_errno = errno;
  379. va_list ap;
  380. fprintf(stderr, "%s: ", progname);
  381. va_start(ap, fmt);
  382. vfprintf(stderr, fmt, ap);
  383. va_end(ap);
  384. fprintf(stderr, ": %s\n", strerror(err_errno));
  385. exit(eval);
  386. }
  387. static void c12r_errx(int eval, const char *fmt, ...)
  388. {
  389. va_list ap;
  390. fprintf(stderr, "%s: ", progname);
  391. va_start(ap, fmt);
  392. vfprintf(stderr, fmt, ap);
  393. va_end(ap);
  394. fprintf(stderr, "\n");
  395. exit(eval);
  396. }
  397. static size_t fcopy(FILE *fsrc, FILE *fdst)
  398. {
  399. char buffer[BUFSIZ];
  400. size_t rsize, wsize;
  401. size_t copied = 0;
  402. while ((rsize = fread(buffer, 1, BUFSIZ, fsrc)) > 0) {
  403. wsize = fwrite(buffer, 1, rsize, fdst);
  404. copied += wsize;
  405. if (wsize != rsize)
  406. break;
  407. }
  408. return copied;
  409. }
  410. static char *grab_stream(FILE *file)
  411. {
  412. size_t max, ret, size = 0;
  413. char *buffer;
  414. max = BUFSIZ;
  415. buffer = malloc(max);
  416. while ((ret = fread(buffer+size, 1, max - size, file)) == max - size) {
  417. size += ret;
  418. buffer = realloc(buffer, max *= 2);
  419. }
  420. size += ret;
  421. if (ferror(file))
  422. c12r_err(1, "reading from command");
  423. buffer[size] = '\0';
  424. return buffer;
  425. }
  426. static char *run(const char *cmd, int *exitstatus)
  427. {
  428. static const char redir[] = " 2>&1";
  429. size_t cmdlen;
  430. char *cmdredir;
  431. FILE *cmdout;
  432. char *ret;
  433. cmdlen = strlen(cmd);
  434. cmdredir = malloc(cmdlen + sizeof(redir));
  435. memcpy(cmdredir, cmd, cmdlen);
  436. memcpy(cmdredir + cmdlen, redir, sizeof(redir));
  437. cmdout = popen(cmdredir, "r");
  438. if (!cmdout)
  439. c12r_err(1, "popen \"%s\"", cmdredir);
  440. free(cmdredir);
  441. ret = grab_stream(cmdout);
  442. *exitstatus = pclose(cmdout);
  443. return ret;
  444. }
  445. static char *connect_args(const char *argv[], const char *outflag,
  446. const char *files)
  447. {
  448. unsigned int i;
  449. char *ret;
  450. size_t len = strlen(outflag) + strlen(files) + 1;
  451. for (i = 1; argv[i]; i++)
  452. len += 1 + strlen(argv[i]);
  453. ret = malloc(len);
  454. len = 0;
  455. for (i = 1; argv[i]; i++) {
  456. strcpy(ret + len, argv[i]);
  457. len += strlen(argv[i]);
  458. if (argv[i+1] || *outflag)
  459. ret[len++] = ' ';
  460. }
  461. strcpy(ret + len, outflag);
  462. len += strlen(outflag);
  463. strcpy(ret + len, files);
  464. return ret;
  465. }
  466. static struct test *find_test(const char *name)
  467. {
  468. unsigned int i;
  469. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
  470. if (strcmp(tests[i].name, name) == 0)
  471. return &tests[i];
  472. }
  473. abort();
  474. }
  475. #define PRE_BOILERPLATE "/* Test program generated by configurator. */\n"
  476. #define MAIN_START_BOILERPLATE \
  477. "int main(int argc, char *argv[]) {\n" \
  478. " (void)argc;\n" \
  479. " (void)argv;\n"
  480. #define USE_FUNC_BOILERPLATE "(void)func;\n"
  481. #define MAIN_BODY_BOILERPLATE "return 0;\n"
  482. #define MAIN_END_BOILERPLATE "}\n"
  483. static bool run_test(const char *cmd, struct test *test)
  484. {
  485. char *output, *newcmd;
  486. FILE *outf;
  487. int status;
  488. if (test->done)
  489. return test->answer;
  490. if (test->depends) {
  491. size_t len;
  492. const char *deps = test->depends;
  493. char *dep;
  494. /* Space-separated dependencies, could be ! for inverse. */
  495. while ((len = strcspn(deps, " ")) != 0) {
  496. bool positive = true;
  497. if (deps[len]) {
  498. dep = strdup(deps);
  499. dep[len] = '\0';
  500. } else {
  501. dep = (char *)deps;
  502. }
  503. if (dep[0] == '!') {
  504. dep++;
  505. positive = false;
  506. }
  507. if (run_test(cmd, find_test(dep)) != positive) {
  508. test->answer = false;
  509. test->done = true;
  510. return test->answer;
  511. }
  512. if (deps[len])
  513. free(dep);
  514. deps += len;
  515. deps += strspn(deps, " ");
  516. }
  517. }
  518. outf = fopen(INPUT_FILE, verbose > 1 ? "w+" : "w");
  519. if (!outf)
  520. c12r_err(1, "creating %s", INPUT_FILE);
  521. fprintf(outf, "%s", PRE_BOILERPLATE);
  522. switch (test->style & ~(EXECUTE|MAY_NOT_COMPILE)) {
  523. case INSIDE_MAIN:
  524. fprintf(outf, "%s", MAIN_START_BOILERPLATE);
  525. fprintf(outf, "%s", test->fragment);
  526. fprintf(outf, "%s", MAIN_END_BOILERPLATE);
  527. break;
  528. case OUTSIDE_MAIN:
  529. fprintf(outf, "%s", test->fragment);
  530. fprintf(outf, "%s", MAIN_START_BOILERPLATE);
  531. fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
  532. fprintf(outf, "%s", MAIN_END_BOILERPLATE);
  533. break;
  534. case DEFINES_FUNC:
  535. fprintf(outf, "%s", test->fragment);
  536. fprintf(outf, "%s", MAIN_START_BOILERPLATE);
  537. fprintf(outf, "%s", USE_FUNC_BOILERPLATE);
  538. fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
  539. fprintf(outf, "%s", MAIN_END_BOILERPLATE);
  540. break;
  541. case DEFINES_EVERYTHING:
  542. fprintf(outf, "%s", test->fragment);
  543. break;
  544. default:
  545. abort();
  546. }
  547. if (verbose > 1) {
  548. fseek(outf, 0, SEEK_SET);
  549. fcopy(outf, stdout);
  550. }
  551. fclose(outf);
  552. newcmd = strdup(cmd);
  553. if (test->flags) {
  554. newcmd = realloc(newcmd, strlen(newcmd) + strlen(" ")
  555. + strlen(test->flags) + 1);
  556. strcat(newcmd, " ");
  557. strcat(newcmd, test->flags);
  558. if (verbose > 1)
  559. printf("Extra flags line: %s", newcmd);
  560. }
  561. if (test->link) {
  562. newcmd = realloc(newcmd, strlen(newcmd) + strlen(" ")
  563. + strlen(test->link) + 1);
  564. strcat(newcmd, " ");
  565. strcat(newcmd, test->link);
  566. if (verbose > 1)
  567. printf("Extra link line: %s", newcmd);
  568. }
  569. output = run(newcmd, &status);
  570. free(newcmd);
  571. if (status != 0 || strstr(output, "warning")) {
  572. if (verbose)
  573. printf("Compile %s for %s, status %i: %s\n",
  574. status ? "fail" : "warning",
  575. test->name, status, output);
  576. if ((test->style & EXECUTE) && !(test->style & MAY_NOT_COMPILE))
  577. c12r_errx(1, "Test for %s did not compile:\n%s",
  578. test->name, output);
  579. test->answer = false;
  580. free(output);
  581. } else {
  582. /* Compile succeeded. */
  583. free(output);
  584. /* We run INSIDE_MAIN tests for sanity checking. */
  585. if ((test->style & EXECUTE) || (test->style & INSIDE_MAIN)) {
  586. output = run("." DIR_SEP OUTPUT_FILE, &status);
  587. if (!(test->style & EXECUTE) && status != 0)
  588. c12r_errx(1, "Test for %s failed with %i:\n%s",
  589. test->name, status, output);
  590. if (verbose && status)
  591. printf("%s exited %i\n", test->name, status);
  592. free(output);
  593. }
  594. test->answer = (status == 0);
  595. }
  596. test->done = true;
  597. if (test->answer && test->overrides) {
  598. struct test *override = find_test(test->overrides);
  599. override->done = true;
  600. override->answer = true;
  601. }
  602. return test->answer;
  603. }
  604. int main(int argc, const char *argv[])
  605. {
  606. char *cmd;
  607. unsigned int i;
  608. const char *default_args[]
  609. = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL };
  610. const char *outflag = DEFAULT_OUTPUT_EXE_FLAG;
  611. if (argc > 0)
  612. progname = argv[0];
  613. while (argc > 1) {
  614. if (strcmp(argv[1], "--help") == 0) {
  615. printf("Usage: configurator [-v] [-O<outflag>] [<compiler> <flags>...]\n"
  616. " <compiler> <flags> will have \"<outflag> <outfile> <infile.c>\" appended\n"
  617. "Default: %s %s %s\n",
  618. DEFAULT_COMPILER, DEFAULT_FLAGS,
  619. DEFAULT_OUTPUT_EXE_FLAG);
  620. exit(0);
  621. }
  622. if (strncmp(argv[1], "-O", 2) == 0) {
  623. argc--;
  624. argv++;
  625. outflag = argv[1] + 2;
  626. if (!*outflag) {
  627. fprintf(stderr,
  628. "%s: option requires an argument -- O\n",
  629. argv[0]);
  630. exit(1);
  631. }
  632. } else if (strcmp(argv[1], "-v") == 0) {
  633. argc--;
  634. argv++;
  635. verbose++;
  636. } else if (strcmp(argv[1], "-vv") == 0) {
  637. argc--;
  638. argv++;
  639. verbose += 2;
  640. } else {
  641. break;
  642. }
  643. }
  644. if (argc == 1)
  645. argv = default_args;
  646. cmd = connect_args(argv, outflag, OUTPUT_FILE " " INPUT_FILE);
  647. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
  648. run_test(cmd, &tests[i]);
  649. free(cmd);
  650. remove(OUTPUT_FILE);
  651. remove(INPUT_FILE);
  652. printf("/* Generated by CCAN configurator */\n"
  653. "#ifndef CCAN_CONFIG_H\n"
  654. "#define CCAN_CONFIG_H\n");
  655. printf("#ifndef _GNU_SOURCE\n");
  656. printf("#define _GNU_SOURCE /* Always use GNU extensions. */\n");
  657. printf("#endif\n");
  658. printf("#define CCAN_COMPILER \"%s\"\n", argv[1]);
  659. cmd = connect_args(argv + 1, "", "");
  660. printf("#define CCAN_CFLAGS \"%s\"\n", cmd);
  661. free(cmd);
  662. printf("#define CCAN_OUTPUT_EXE_CFLAG \"%s\"\n\n", outflag);
  663. /* This one implies "#include <ccan/..." works, eg. for tdb2.h */
  664. printf("#define HAVE_CCAN 1\n");
  665. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
  666. printf("#define %s %u\n", tests[i].name, tests[i].answer);
  667. printf("#endif /* CCAN_CONFIG_H */\n");
  668. return 0;
  669. }