configurator.c 22 KB

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