configurator.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 *link;
  52. const char *fragment;
  53. const char *flags;
  54. const char *overrides; /* On success, force this to '1' */
  55. bool done;
  56. bool answer;
  57. };
  58. static struct test tests[] = {
  59. { "HAVE_32BIT_OFF_T", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
  60. "#include <sys/types.h>\n"
  61. "int main(void) {\n"
  62. " return sizeof(off_t) == 4 ? 0 : 1;\n"
  63. "}\n" },
  64. { "HAVE_ALIGNOF", INSIDE_MAIN, NULL, NULL,
  65. "return __alignof__(double) > 0 ? 0 : 1;" },
  66. { "HAVE_ASPRINTF", DEFINES_FUNC, NULL, NULL,
  67. "#ifndef _GNU_SOURCE\n"
  68. "#define _GNU_SOURCE\n"
  69. "#endif\n"
  70. "#include <stdio.h>\n"
  71. "static char *func(int x) {"
  72. " char *p;\n"
  73. " if (asprintf(&p, \"%u\", x) == -1) p = NULL;"
  74. " return p;\n"
  75. "}" },
  76. { "HAVE_ATTRIBUTE_COLD", DEFINES_FUNC, NULL, NULL,
  77. "static int __attribute__((cold)) func(int x) { return x; }" },
  78. { "HAVE_ATTRIBUTE_CONST", DEFINES_FUNC, NULL, NULL,
  79. "static int __attribute__((const)) func(int x) { return x; }" },
  80. { "HAVE_ATTRIBUTE_PURE", DEFINES_FUNC, NULL, NULL,
  81. "static int __attribute__((pure)) func(int x) { return x; }" },
  82. { "HAVE_ATTRIBUTE_MAY_ALIAS", OUTSIDE_MAIN, NULL, NULL,
  83. "typedef short __attribute__((__may_alias__)) short_a;" },
  84. { "HAVE_ATTRIBUTE_NORETURN", DEFINES_FUNC, NULL, NULL,
  85. "#include <stdlib.h>\n"
  86. "static void __attribute__((noreturn)) func(int x) { exit(x); }" },
  87. { "HAVE_ATTRIBUTE_PRINTF", DEFINES_FUNC, NULL, NULL,
  88. "static void __attribute__((format(__printf__, 1, 2))) func(const char *fmt, ...) { (void)fmt; }" },
  89. { "HAVE_ATTRIBUTE_UNUSED", OUTSIDE_MAIN, NULL, NULL,
  90. "static int __attribute__((unused)) func(int x) { return x; }" },
  91. { "HAVE_ATTRIBUTE_USED", OUTSIDE_MAIN, NULL, NULL,
  92. "static int __attribute__((used)) func(int x) { return x; }" },
  93. { "HAVE_BACKTRACE", DEFINES_FUNC, NULL, NULL,
  94. "#include <execinfo.h>\n"
  95. "static int func(int x) {"
  96. " void *bt[10];\n"
  97. " return backtrace(bt, 10) < x;\n"
  98. "}" },
  99. { "HAVE_BIG_ENDIAN", INSIDE_MAIN|EXECUTE, NULL, NULL,
  100. "union { int i; char c[sizeof(int)]; } u;\n"
  101. "u.i = 0x01020304;\n"
  102. "return u.c[0] == 0x01 && u.c[1] == 0x02 && u.c[2] == 0x03 && u.c[3] == 0x04 ? 0 : 1;" },
  103. { "HAVE_BSWAP_64", DEFINES_FUNC, "HAVE_BYTESWAP_H", NULL,
  104. "#include <byteswap.h>\n"
  105. "static int func(int x) { return bswap_64(x); }" },
  106. { "HAVE_BUILTIN_CHOOSE_EXPR", INSIDE_MAIN, NULL, NULL,
  107. "return __builtin_choose_expr(1, 0, \"garbage\");" },
  108. { "HAVE_BUILTIN_CLZ", INSIDE_MAIN, NULL, NULL,
  109. "return __builtin_clz(1) == (sizeof(int)*8 - 1) ? 0 : 1;" },
  110. { "HAVE_BUILTIN_CLZL", INSIDE_MAIN, NULL, NULL,
  111. "return __builtin_clzl(1) == (sizeof(long)*8 - 1) ? 0 : 1;" },
  112. { "HAVE_BUILTIN_CLZLL", INSIDE_MAIN, NULL, NULL,
  113. "return __builtin_clzll(1) == (sizeof(long long)*8 - 1) ? 0 : 1;" },
  114. { "HAVE_BUILTIN_CTZ", INSIDE_MAIN, NULL, NULL,
  115. "return __builtin_ctz(1 << (sizeof(int)*8 - 1)) == (sizeof(int)*8 - 1) ? 0 : 1;" },
  116. { "HAVE_BUILTIN_CTZL", INSIDE_MAIN, NULL, NULL,
  117. "return __builtin_ctzl(1UL << (sizeof(long)*8 - 1)) == (sizeof(long)*8 - 1) ? 0 : 1;" },
  118. { "HAVE_BUILTIN_CTZLL", INSIDE_MAIN, NULL, NULL,
  119. "return __builtin_ctzll(1ULL << (sizeof(long long)*8 - 1)) == (sizeof(long long)*8 - 1) ? 0 : 1;" },
  120. { "HAVE_BUILTIN_CONSTANT_P", INSIDE_MAIN, NULL, NULL,
  121. "return __builtin_constant_p(1) ? 0 : 1;" },
  122. { "HAVE_BUILTIN_EXPECT", INSIDE_MAIN, NULL, NULL,
  123. "return __builtin_expect(argc == 1, 1) ? 0 : 1;" },
  124. { "HAVE_BUILTIN_FFS", INSIDE_MAIN, NULL, NULL,
  125. "return __builtin_ffs(0) == 0 ? 0 : 1;" },
  126. { "HAVE_BUILTIN_FFSL", INSIDE_MAIN, NULL, NULL,
  127. "return __builtin_ffsl(0L) == 0 ? 0 : 1;" },
  128. { "HAVE_BUILTIN_FFSLL", INSIDE_MAIN, NULL, NULL,
  129. "return __builtin_ffsll(0LL) == 0 ? 0 : 1;" },
  130. { "HAVE_BUILTIN_POPCOUNTL", INSIDE_MAIN, NULL, NULL,
  131. "return __builtin_popcountl(255L) == 8 ? 0 : 1;" },
  132. { "HAVE_BUILTIN_TYPES_COMPATIBLE_P", INSIDE_MAIN, NULL, NULL,
  133. "return __builtin_types_compatible_p(char *, int) ? 1 : 0;" },
  134. { "HAVE_ICCARM_INTRINSICS", DEFINES_FUNC, NULL, NULL,
  135. "#include <intrinsics.h>\n"
  136. "int func(int v) {\n"
  137. " return __CLZ(__RBIT(v));\n"
  138. "}" },
  139. { "HAVE_BYTESWAP_H", OUTSIDE_MAIN, NULL, NULL,
  140. "#include <byteswap.h>\n" },
  141. { "HAVE_CLOCK_GETTIME",
  142. DEFINES_FUNC, "HAVE_STRUCT_TIMESPEC", NULL,
  143. "#include <time.h>\n"
  144. "static struct timespec func(void) {\n"
  145. " struct timespec ts;\n"
  146. " clock_gettime(CLOCK_REALTIME, &ts);\n"
  147. " return ts;\n"
  148. "}\n" },
  149. { "HAVE_CLOCK_GETTIME_IN_LIBRT",
  150. DEFINES_FUNC,
  151. "HAVE_STRUCT_TIMESPEC !HAVE_CLOCK_GETTIME",
  152. "-lrt",
  153. "#include <time.h>\n"
  154. "static struct timespec func(void) {\n"
  155. " struct timespec ts;\n"
  156. " clock_gettime(CLOCK_REALTIME, &ts);\n"
  157. " return ts;\n"
  158. "}\n",
  159. /* This means HAVE_CLOCK_GETTIME, too */
  160. "HAVE_CLOCK_GETTIME" },
  161. { "HAVE_COMPOUND_LITERALS", INSIDE_MAIN, NULL, NULL,
  162. "int *foo = (int[]) { 1, 2, 3, 4 };\n"
  163. "return foo[0] ? 0 : 1;" },
  164. { "HAVE_FCHDIR", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
  165. "#include <sys/types.h>\n"
  166. "#include <sys/stat.h>\n"
  167. "#include <fcntl.h>\n"
  168. "#include <unistd.h>\n"
  169. "int main(void) {\n"
  170. " int fd = open(\"..\", O_RDONLY);\n"
  171. " return fchdir(fd) == 0 ? 0 : 1;\n"
  172. "}\n" },
  173. { "HAVE_ERR_H", DEFINES_FUNC, NULL, NULL,
  174. "#include <err.h>\n"
  175. "static void func(int arg) {\n"
  176. " if (arg == 0)\n"
  177. " err(1, \"err %u\", arg);\n"
  178. " if (arg == 1)\n"
  179. " errx(1, \"err %u\", arg);\n"
  180. " if (arg == 3)\n"
  181. " warn(\"warn %u\", arg);\n"
  182. " if (arg == 4)\n"
  183. " warnx(\"warn %u\", arg);\n"
  184. "}\n" },
  185. { "HAVE_FILE_OFFSET_BITS", DEFINES_EVERYTHING|EXECUTE,
  186. "HAVE_32BIT_OFF_T", NULL,
  187. "#define _FILE_OFFSET_BITS 64\n"
  188. "#include <sys/types.h>\n"
  189. "int main(void) {\n"
  190. " return sizeof(off_t) == 8 ? 0 : 1;\n"
  191. "}\n" },
  192. { "HAVE_FOR_LOOP_DECLARATION", INSIDE_MAIN, NULL, NULL,
  193. "for (int i = 0; i < argc; i++) { return 0; };\n"
  194. "return 1;" },
  195. { "HAVE_FLEXIBLE_ARRAY_MEMBER", OUTSIDE_MAIN, NULL, NULL,
  196. "struct foo { unsigned int x; int arr[]; };" },
  197. { "HAVE_GETPAGESIZE", DEFINES_FUNC, NULL, NULL,
  198. "#include <unistd.h>\n"
  199. "static int func(void) { return getpagesize(); }" },
  200. { "HAVE_ISBLANK", DEFINES_FUNC, NULL, NULL,
  201. "#ifndef _GNU_SOURCE\n"
  202. "#define _GNU_SOURCE\n"
  203. "#endif\n"
  204. "#include <ctype.h>\n"
  205. "static int func(void) { return isblank(' '); }" },
  206. { "HAVE_LITTLE_ENDIAN", INSIDE_MAIN|EXECUTE, NULL, NULL,
  207. "union { int i; char c[sizeof(int)]; } u;\n"
  208. "u.i = 0x01020304;\n"
  209. "return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;" },
  210. { "HAVE_MEMMEM", DEFINES_FUNC, NULL, NULL,
  211. "#ifndef _GNU_SOURCE\n"
  212. "#define _GNU_SOURCE\n"
  213. "#endif\n"
  214. "#include <string.h>\n"
  215. "static void *func(void *h, size_t hl, void *n, size_t nl) {\n"
  216. "return memmem(h, hl, n, nl);"
  217. "}\n", },
  218. { "HAVE_MEMRCHR", DEFINES_FUNC, NULL, NULL,
  219. "#ifndef _GNU_SOURCE\n"
  220. "#define _GNU_SOURCE\n"
  221. "#endif\n"
  222. "#include <string.h>\n"
  223. "static void *func(void *s, int c, size_t n) {\n"
  224. "return memrchr(s, c, n);"
  225. "}\n", },
  226. { "HAVE_MMAP", DEFINES_FUNC, NULL, NULL,
  227. "#include <sys/mman.h>\n"
  228. "static void *func(int fd) {\n"
  229. " return mmap(0, 65536, PROT_READ, MAP_SHARED, fd, 0);\n"
  230. "}" },
  231. { "HAVE_PROC_SELF_MAPS", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
  232. "#include <sys/types.h>\n"
  233. "#include <sys/stat.h>\n"
  234. "#include <fcntl.h>\n"
  235. "int main(void) {\n"
  236. " return open(\"/proc/self/maps\", O_RDONLY) != -1 ? 0 : 1;\n"
  237. "}\n" },
  238. { "HAVE_QSORT_R_PRIVATE_LAST",
  239. DEFINES_EVERYTHING|EXECUTE|MAY_NOT_COMPILE, NULL, NULL,
  240. "#ifndef _GNU_SOURCE\n"
  241. "#define _GNU_SOURCE\n"
  242. "#endif\n"
  243. "#include <stdlib.h>\n"
  244. "static int cmp(const void *lp, const void *rp, void *priv) {\n"
  245. " *(unsigned int *)priv = 1;\n"
  246. " return *(const int *)lp - *(const int *)rp; }\n"
  247. "int main(void) {\n"
  248. " int array[] = { 9, 2, 5 };\n"
  249. " unsigned int called = 0;\n"
  250. " qsort_r(array, 3, sizeof(int), cmp, &called);\n"
  251. " return called && array[0] == 2 && array[1] == 5 && array[2] == 9 ? 0 : 1;\n"
  252. "}\n" },
  253. { "HAVE_STRUCT_TIMESPEC",
  254. DEFINES_FUNC, NULL, NULL,
  255. "#include <time.h>\n"
  256. "static void func(void) {\n"
  257. " struct timespec ts;\n"
  258. " ts.tv_sec = ts.tv_nsec = 1;\n"
  259. "}\n" },
  260. { "HAVE_SECTION_START_STOP",
  261. DEFINES_FUNC, NULL, NULL,
  262. "static void *__attribute__((__section__(\"mysec\"))) p = &p;\n"
  263. "static int func(void) {\n"
  264. " extern void *__start_mysec[], *__stop_mysec[];\n"
  265. " return __stop_mysec - __start_mysec;\n"
  266. "}\n" },
  267. { "HAVE_STACK_GROWS_UPWARDS", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
  268. "static long nest(const void *base, unsigned int i)\n"
  269. "{\n"
  270. " if (i == 0)\n"
  271. " return (const char *)&i - (const char *)base;\n"
  272. " return nest(base, i-1);\n"
  273. "}\n"
  274. "int main(int argc, char *argv[]) {\n"
  275. " (void)argv;\n"
  276. " return (nest(&argc, argc) > 0) ? 0 : 1;\n"
  277. "}\n" },
  278. { "HAVE_STATEMENT_EXPR", INSIDE_MAIN, NULL, NULL,
  279. "return ({ int x = argc; x == argc ? 0 : 1; });" },
  280. { "HAVE_SYS_FILIO_H", OUTSIDE_MAIN, NULL, NULL, /* Solaris needs this for FIONREAD */
  281. "#include <sys/filio.h>\n" },
  282. { "HAVE_SYS_TERMIOS_H", OUTSIDE_MAIN, NULL, NULL,
  283. "#include <sys/termios.h>\n" },
  284. { "HAVE_TYPEOF", INSIDE_MAIN, NULL, NULL,
  285. "__typeof__(argc) i; i = argc; return i == argc ? 0 : 1;" },
  286. { "HAVE_UNALIGNED_ACCESS", DEFINES_EVERYTHING|EXECUTE, NULL, NULL,
  287. "#include <string.h>\n"
  288. "int main(int argc, char *argv[]) {\n"
  289. " (void)argc;\n"
  290. " char pad[sizeof(int *) * 1];\n"
  291. " strncpy(pad, argv[0], sizeof(pad));\n"
  292. " int *x = (int *)pad, *y = (int *)(pad + 1);\n"
  293. " return *x == *y;\n"
  294. "}\n" },
  295. { "HAVE_UTIME", DEFINES_FUNC, NULL, NULL,
  296. "#include <sys/types.h>\n"
  297. "#include <utime.h>\n"
  298. "static int func(const char *filename) {\n"
  299. " struct utimbuf times = { 0 };\n"
  300. " return utime(filename, &times);\n"
  301. "}" },
  302. { "HAVE_WARN_UNUSED_RESULT", DEFINES_FUNC, NULL, NULL,
  303. "#include <sys/types.h>\n"
  304. "#include <utime.h>\n"
  305. "static __attribute__((warn_unused_result)) int func(int i) {\n"
  306. " return i + 1;\n"
  307. "}" },
  308. { "HAVE_OPENMP", INSIDE_MAIN, NULL, NULL,
  309. "int i;\n"
  310. "#pragma omp parallel for\n"
  311. "for(i = 0; i < 0; i++) {};\n"
  312. "return 0;\n",
  313. "-Werror -fopenmp" },
  314. { "HAVE_VALGRIND_MEMCHECK_H", OUTSIDE_MAIN, NULL, NULL,
  315. "#include <valgrind/memcheck.h>\n" },
  316. };
  317. static char *grab_fd(int fd)
  318. {
  319. int ret;
  320. size_t max, size = 0;
  321. char *buffer;
  322. max = 16384;
  323. buffer = malloc(max+1);
  324. while ((ret = read(fd, buffer + size, max - size)) > 0) {
  325. size += ret;
  326. if (size == max)
  327. buffer = realloc(buffer, max *= 2);
  328. }
  329. if (ret < 0)
  330. err(1, "reading from command");
  331. buffer[size] = '\0';
  332. return buffer;
  333. }
  334. static char *run(const char *cmd, int *exitstatus)
  335. {
  336. pid_t pid;
  337. int p[2];
  338. char *ret;
  339. int status;
  340. if (pipe(p) != 0)
  341. err(1, "creating pipe");
  342. pid = fork();
  343. if (pid == -1)
  344. err(1, "forking");
  345. if (pid == 0) {
  346. if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
  347. || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
  348. || close(p[0]) != 0
  349. || close(STDIN_FILENO) != 0
  350. || open("/dev/null", O_RDONLY) != STDIN_FILENO)
  351. exit(128);
  352. status = system(cmd);
  353. if (WIFEXITED(status))
  354. exit(WEXITSTATUS(status));
  355. /* Here's a hint... */
  356. exit(128 + WTERMSIG(status));
  357. }
  358. close(p[1]);
  359. ret = grab_fd(p[0]);
  360. /* This shouldn't fail... */
  361. if (waitpid(pid, &status, 0) != pid)
  362. err(1, "Failed to wait for child");
  363. close(p[0]);
  364. if (WIFEXITED(status))
  365. *exitstatus = WEXITSTATUS(status);
  366. else
  367. *exitstatus = -WTERMSIG(status);
  368. return ret;
  369. }
  370. static char *connect_args(const char *argv[], const char *extra)
  371. {
  372. unsigned int i, len = strlen(extra) + 1;
  373. char *ret;
  374. for (i = 1; argv[i]; i++)
  375. len += 1 + strlen(argv[i]);
  376. ret = malloc(len);
  377. len = 0;
  378. for (i = 1; argv[i]; i++) {
  379. strcpy(ret + len, argv[i]);
  380. len += strlen(argv[i]);
  381. if (argv[i+1])
  382. ret[len++] = ' ';
  383. }
  384. strcpy(ret + len, extra);
  385. return ret;
  386. }
  387. static struct test *find_test(const char *name)
  388. {
  389. unsigned int i;
  390. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
  391. if (strcmp(tests[i].name, name) == 0)
  392. return &tests[i];
  393. }
  394. abort();
  395. }
  396. #define PRE_BOILERPLATE "/* Test program generated by configurator. */\n"
  397. #define MAIN_START_BOILERPLATE \
  398. "int main(int argc, char *argv[]) {\n" \
  399. " (void)argc;\n" \
  400. " (void)argv;\n"
  401. #define USE_FUNC_BOILERPLATE "(void)func;\n"
  402. #define MAIN_BODY_BOILERPLATE "return 0;\n"
  403. #define MAIN_END_BOILERPLATE "}\n"
  404. static bool run_test(const char *cmd, struct test *test)
  405. {
  406. char *output, *newcmd;
  407. FILE *outf;
  408. int status;
  409. if (test->done)
  410. return test->answer;
  411. if (test->depends) {
  412. size_t len;
  413. const char *deps = test->depends;
  414. char *dep;
  415. /* Space-separated dependencies, could be ! for inverse. */
  416. while ((len = strcspn(deps, " "))) {
  417. bool positive = true;
  418. if (deps[len]) {
  419. dep = strdup(deps);
  420. dep[len] = '\0';
  421. } else {
  422. dep = (char *)deps;
  423. }
  424. if (dep[0] == '!') {
  425. dep++;
  426. positive = false;
  427. }
  428. if (run_test(cmd, find_test(dep)) != positive) {
  429. test->answer = false;
  430. test->done = true;
  431. return test->answer;
  432. }
  433. if (deps[len])
  434. free(dep);
  435. deps += len;
  436. deps += strspn(deps, " ");
  437. }
  438. }
  439. outf = fopen(INPUT_FILE, "w");
  440. if (!outf)
  441. err(1, "creating %s", INPUT_FILE);
  442. fprintf(outf, "%s", PRE_BOILERPLATE);
  443. switch (test->style & ~(EXECUTE|MAY_NOT_COMPILE)) {
  444. case INSIDE_MAIN:
  445. fprintf(outf, "%s", MAIN_START_BOILERPLATE);
  446. fprintf(outf, "%s", test->fragment);
  447. fprintf(outf, "%s", MAIN_END_BOILERPLATE);
  448. break;
  449. case OUTSIDE_MAIN:
  450. fprintf(outf, "%s", test->fragment);
  451. fprintf(outf, "%s", MAIN_START_BOILERPLATE);
  452. fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
  453. fprintf(outf, "%s", MAIN_END_BOILERPLATE);
  454. break;
  455. case DEFINES_FUNC:
  456. fprintf(outf, "%s", test->fragment);
  457. fprintf(outf, "%s", MAIN_START_BOILERPLATE);
  458. fprintf(outf, "%s", USE_FUNC_BOILERPLATE);
  459. fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
  460. fprintf(outf, "%s", MAIN_END_BOILERPLATE);
  461. break;
  462. case DEFINES_EVERYTHING:
  463. fprintf(outf, "%s", test->fragment);
  464. break;
  465. default:
  466. abort();
  467. }
  468. fclose(outf);
  469. if (verbose > 1)
  470. if (system("cat " INPUT_FILE) == -1);
  471. newcmd = strdup(cmd);
  472. if (test->flags) {
  473. newcmd = realloc(newcmd, strlen(newcmd) + strlen(" ")
  474. + strlen(test->flags) + 1);
  475. strcat(newcmd, " ");
  476. strcat(newcmd, test->flags);
  477. if (verbose > 1)
  478. printf("Extra flags line: %s", newcmd);
  479. }
  480. if (test->link) {
  481. newcmd = realloc(newcmd, strlen(newcmd) + strlen(" ")
  482. + strlen(test->link) + 1);
  483. strcat(newcmd, " ");
  484. strcat(newcmd, test->link);
  485. if (verbose > 1)
  486. printf("Extra link line: %s", newcmd);
  487. }
  488. output = run(newcmd, &status);
  489. free(newcmd);
  490. if (status != 0 || strstr(output, "warning")) {
  491. if (verbose)
  492. printf("Compile %s for %s, status %i: %s\n",
  493. status ? "fail" : "warning",
  494. test->name, status, output);
  495. if ((test->style & EXECUTE) && !(test->style & MAY_NOT_COMPILE))
  496. errx(1, "Test for %s did not compile:\n%s",
  497. test->name, output);
  498. test->answer = false;
  499. free(output);
  500. } else {
  501. /* Compile succeeded. */
  502. free(output);
  503. /* We run INSIDE_MAIN tests for sanity checking. */
  504. if ((test->style & EXECUTE) || (test->style & INSIDE_MAIN)) {
  505. output = run("./" OUTPUT_FILE, &status);
  506. if (!(test->style & EXECUTE) && status != 0)
  507. errx(1, "Test for %s failed with %i:\n%s",
  508. test->name, status, output);
  509. if (verbose && status)
  510. printf("%s exited %i\n", test->name, status);
  511. free(output);
  512. }
  513. test->answer = (status == 0);
  514. }
  515. test->done = true;
  516. if (test->answer && test->overrides) {
  517. struct test *override = find_test(test->overrides);
  518. override->done = true;
  519. override->answer = true;
  520. }
  521. return test->answer;
  522. }
  523. int main(int argc, const char *argv[])
  524. {
  525. char *cmd;
  526. unsigned int i;
  527. const char *default_args[]
  528. = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL };
  529. if (argc > 1) {
  530. if (strcmp(argv[1], "--help") == 0) {
  531. printf("Usage: configurator [-v] [<compiler> <flags>...]\n"
  532. " <compiler> <flags> will have \"-o <outfile> <infile.c>\" appended\n"
  533. "Default: %s %s\n",
  534. DEFAULT_COMPILER, DEFAULT_FLAGS);
  535. exit(0);
  536. }
  537. if (strcmp(argv[1], "-v") == 0) {
  538. argc--;
  539. argv++;
  540. verbose = 1;
  541. } else if (strcmp(argv[1], "-vv") == 0) {
  542. argc--;
  543. argv++;
  544. verbose = 2;
  545. }
  546. }
  547. if (argc == 1)
  548. argv = default_args;
  549. cmd = connect_args(argv, " -o " OUTPUT_FILE " " INPUT_FILE);
  550. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
  551. run_test(cmd, &tests[i]);
  552. free(cmd);
  553. unlink(OUTPUT_FILE);
  554. unlink(INPUT_FILE);
  555. printf("/* Generated by CCAN configurator */\n"
  556. "#ifndef CCAN_CONFIG_H\n"
  557. "#define CCAN_CONFIG_H\n");
  558. printf("#ifndef _GNU_SOURCE\n");
  559. printf("#define _GNU_SOURCE /* Always use GNU extensions. */\n");
  560. printf("#endif\n");
  561. printf("#define CCAN_COMPILER \"%s\"\n", argv[1]);
  562. cmd = connect_args(argv+1, "");
  563. printf("#define CCAN_CFLAGS \"%s\"\n\n", cmd);
  564. free(cmd);
  565. /* This one implies "#include <ccan/..." works, eg. for tdb2.h */
  566. printf("#define HAVE_CCAN 1\n");
  567. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
  568. printf("#define %s %u\n", tests[i].name, tests[i].answer);
  569. printf("#endif /* CCAN_CONFIG_H */\n");
  570. return 0;
  571. }