configurator.c 16 KB

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