configurator.c 18 KB

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