configurator.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* Simple tool to create config.h.
  2. * Would be much easier with ccan modules, but deliberately standalone. */
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <err.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <string.h>
  13. #define DEFAULT_COMPILER "cc"
  14. #define DEFAULT_FLAGS "-g -Wall -Wundef -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes -Wold-style-definition"
  15. #define OUTPUT_FILE "configurator.out"
  16. #define INPUT_FILE "configuratortest.c"
  17. static int verbose;
  18. enum test_style {
  19. EXECUTE,
  20. OUTSIDE_MAIN,
  21. DEFINES_FUNC,
  22. INSIDE_MAIN
  23. };
  24. struct test {
  25. const char *name;
  26. enum test_style style;
  27. const char *depends;
  28. const char *fragment;
  29. bool done;
  30. bool answer;
  31. };
  32. static struct test tests[] = {
  33. { "HAVE_ALIGNOF", INSIDE_MAIN, NULL,
  34. "return __alignof__(double) > 0 ? 0 : 1;" },
  35. { "HAVE_ASPRINTF", DEFINES_FUNC, NULL,
  36. "#define _GNU_SOURCE\n"
  37. "#include <stdio.h>\n"
  38. "static char *func(int x) {"
  39. " char *p;\n"
  40. " if (asprintf(&p, \"%u\", x) == -1) p = NULL;"
  41. " return p;\n"
  42. "}" },
  43. { "HAVE_ATTRIBUTE_COLD", DEFINES_FUNC, NULL,
  44. "static int __attribute__((cold)) func(int x) { return x; }" },
  45. { "HAVE_ATTRIBUTE_CONST", DEFINES_FUNC, NULL,
  46. "static int __attribute__((const)) func(int x) { return x; }" },
  47. { "HAVE_ATTRIBUTE_MAY_ALIAS", OUTSIDE_MAIN, NULL,
  48. "typedef short __attribute__((__may_alias__)) short_a;" },
  49. { "HAVE_ATTRIBUTE_NORETURN", DEFINES_FUNC, NULL,
  50. "#include <stdlib.h>\n"
  51. "static void __attribute__((noreturn)) func(int x) { exit(x); }" },
  52. { "HAVE_ATTRIBUTE_PRINTF", DEFINES_FUNC, NULL,
  53. "static void __attribute__((format(__printf__, 1, 2))) func(const char *fmt, ...) { }" },
  54. { "HAVE_ATTRIBUTE_UNUSED", OUTSIDE_MAIN, NULL,
  55. "static int __attribute__((unused)) func(int x) { return x; }" },
  56. { "HAVE_ATTRIBUTE_USED", OUTSIDE_MAIN, NULL,
  57. "static int __attribute__((used)) func(int x) { return x; }" },
  58. { "HAVE_BIG_ENDIAN", EXECUTE, NULL,
  59. "union { int i; char c[sizeof(int)]; } u;\n"
  60. "u.i = 0x01020304;\n"
  61. "return u.c[0] == 0x01 && u.c[1] == 0x02 && u.c[2] == 0x03 && u.c[3] == 0x04 ? 0 : 1;" },
  62. { "HAVE_BSWAP_64", DEFINES_FUNC, "HAVE_BYTESWAP_H",
  63. "#include <byteswap.h>\n"
  64. "static int func(int x) { return bswap_64(x); }" },
  65. { "HAVE_BUILTIN_CHOOSE_EXPR", INSIDE_MAIN, NULL,
  66. "return __builtin_choose_expr(1, 0, \"garbage\");" },
  67. { "HAVE_BUILTIN_CLZ", INSIDE_MAIN, NULL,
  68. "return __builtin_clz(1) == (sizeof(int)*8 - 1) ? 0 : 1;" },
  69. { "HAVE_BUILTIN_CLZL", INSIDE_MAIN, NULL,
  70. "return __builtin_clzl(1) == (sizeof(long)*8 - 1) ? 0 : 1;" },
  71. { "HAVE_BUILTIN_CLZLL", INSIDE_MAIN, NULL,
  72. "return __builtin_clzll(1) == (sizeof(long long)*8 - 1) ? 0 : 1;" },
  73. { "HAVE_BUILTIN_CONSTANT_P", INSIDE_MAIN, NULL,
  74. "return __builtin_constant_p(1) ? 0 : 1;" },
  75. { "HAVE_BUILTIN_EXPECT", INSIDE_MAIN, NULL,
  76. "return __builtin_expect(argc == 1, 1) ? 0 : 1;" },
  77. { "HAVE_BUILTIN_FFSL", INSIDE_MAIN, NULL,
  78. "return __builtin_ffsl(0L) == 0 ? 0 : 1;" },
  79. { "HAVE_BUILTIN_FFSLL", INSIDE_MAIN, NULL,
  80. "return __builtin_ffsll(0LL) == 0 ? 0 : 1;" },
  81. { "HAVE_BUILTIN_POPCOUNTL", INSIDE_MAIN, NULL,
  82. "return __builtin_popcountl(255L) == 8 ? 0 : 1;" },
  83. { "HAVE_BUILTIN_TYPES_COMPATIBLE_P", INSIDE_MAIN, NULL,
  84. "return __builtin_types_compatible_p(char *, int) ? 1 : 0;" },
  85. { "HAVE_BYTESWAP_H", OUTSIDE_MAIN, NULL,
  86. "#include <byteswap.h>\n" },
  87. { "HAVE_COMPOUND_LITERALS", INSIDE_MAIN, NULL,
  88. "char **foo = (char *[]) { \"x\", \"y\", \"z\" };\n"
  89. "return foo[0] ? 0 : 1;" },
  90. { "HAVE_FOR_LOOP_DECLARATION", INSIDE_MAIN, NULL,
  91. "for (int i = 0; i < argc; i++) { return 0; };\n"
  92. "return 1;" },
  93. { "HAVE_FLEXIBLE_ARRAY_MEMBER", OUTSIDE_MAIN, NULL,
  94. "struct foo { unsigned int x; int arr[]; };" },
  95. { "HAVE_GETPAGESIZE", DEFINES_FUNC, NULL,
  96. "#include <unistd.h>\n"
  97. "static int func(void) { return getpagesize(); }" },
  98. { "HAVE_LITTLE_ENDIAN", EXECUTE, NULL,
  99. "union { int i; char c[sizeof(int)]; } u;\n"
  100. "u.i = 0x01020304;\n"
  101. "return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;" },
  102. { "HAVE_MMAP", DEFINES_FUNC, NULL,
  103. "#include <sys/mman.h>\n"
  104. "static void *func(int fd) {\n"
  105. " return mmap(0, 65536, PROT_READ, MAP_SHARED, fd, 0);\n"
  106. "}" },
  107. { "HAVE_NESTED_FUNCTIONS", DEFINES_FUNC, NULL,
  108. "void (*func(int val))(int);\n"
  109. "void (*func(int val))(int) {\n"
  110. " auto void add(int val2);\n"
  111. " void add(int val2) { val += val2; }\n"
  112. " return add;\n"
  113. "}\n" },
  114. { "HAVE_STATEMENT_EXPR", INSIDE_MAIN, NULL,
  115. "return ({ int x = argc; x == argc ? 0 : 1; });" },
  116. { "HAVE_TYPEOF", INSIDE_MAIN, NULL,
  117. "__typeof__(argc) i; i = argc; return i == argc ? 0 : 1;" },
  118. { "HAVE_UTIME", DEFINES_FUNC, NULL,
  119. "#include <sys/types.h>\n"
  120. "#include <utime.h>\n"
  121. "static int func(const char *filename) {\n"
  122. " struct utimbuf times = { 0 };\n"
  123. " return utime(filename, &times);\n"
  124. "}" },
  125. { "HAVE_WARN_UNUSED_RESULT", DEFINES_FUNC, NULL,
  126. "#include <sys/types.h>\n"
  127. "#include <utime.h>\n"
  128. "static __attribute__((warn_unused_result)) int func(int i) {\n"
  129. " return i + 1;\n"
  130. "}" },
  131. };
  132. static char *grab_fd(int fd)
  133. {
  134. int ret;
  135. size_t max, size = 0;
  136. char *buffer;
  137. max = 16384;
  138. buffer = malloc(max+1);
  139. while ((ret = read(fd, buffer + size, max - size)) > 0) {
  140. size += ret;
  141. if (size == max)
  142. buffer = realloc(buffer, max *= 2);
  143. }
  144. if (ret < 0)
  145. err(1, "reading from command");
  146. buffer[size] = '\0';
  147. return buffer;
  148. }
  149. static char *run(const char *cmd, int *exitstatus)
  150. {
  151. pid_t pid;
  152. int p[2];
  153. char *ret;
  154. int status;
  155. if (pipe(p) != 0)
  156. err(1, "creating pipe");
  157. pid = fork();
  158. if (pid == -1)
  159. err(1, "forking");
  160. if (pid == 0) {
  161. if (dup2(p[1], STDOUT_FILENO) != STDOUT_FILENO
  162. || dup2(p[1], STDERR_FILENO) != STDERR_FILENO
  163. || close(p[0]) != 0
  164. || close(STDIN_FILENO) != 0
  165. || open("/dev/null", O_RDONLY) != STDIN_FILENO)
  166. exit(128);
  167. status = system(cmd);
  168. if (WIFEXITED(status))
  169. exit(WEXITSTATUS(status));
  170. /* Here's a hint... */
  171. exit(128 + WTERMSIG(status));
  172. }
  173. close(p[1]);
  174. ret = grab_fd(p[0]);
  175. /* This shouldn't fail... */
  176. if (waitpid(pid, &status, 0) != pid)
  177. err(1, "Failed to wait for child");
  178. close(p[0]);
  179. if (WIFEXITED(status))
  180. *exitstatus = WEXITSTATUS(status);
  181. else
  182. *exitstatus = -WTERMSIG(status);
  183. return ret;
  184. }
  185. static char *connect_args(char *argv[], const char *extra)
  186. {
  187. unsigned int i, len = strlen(extra) + 1;
  188. char *ret;
  189. for (i = 1; argv[i]; i++)
  190. len += 1 + strlen(argv[i]);
  191. ret = malloc(len);
  192. len = 0;
  193. for (i = 1; argv[i]; i++) {
  194. strcpy(ret + len, argv[i]);
  195. len += strlen(argv[i]);
  196. ret[len++] = ' ';
  197. }
  198. strcpy(ret + len, extra);
  199. return ret;
  200. }
  201. static struct test *find_test(const char *name)
  202. {
  203. unsigned int i;
  204. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
  205. if (strcmp(tests[i].name, name) == 0)
  206. return &tests[i];
  207. }
  208. abort();
  209. }
  210. #define PRE_BOILERPLATE "/* Test program generated by configurator. */\n"
  211. #define MAIN_START_BOILERPLATE "int main(int argc, char *argv[]) {\n"
  212. #define USE_FUNC_BOILERPLATE "(void)func;\n"
  213. #define MAIN_BODY_BOILERPLATE "return 0;\n"
  214. #define MAIN_END_BOILERPLATE "}\n"
  215. static bool run_test(const char *cmd, struct test *test)
  216. {
  217. char *output;
  218. FILE *outf;
  219. int status;
  220. if (test->done)
  221. return test->answer;
  222. if (test->depends && !run_test(cmd, find_test(test->depends))) {
  223. test->answer = false;
  224. test->done = true;
  225. return test->answer;
  226. }
  227. outf = fopen(INPUT_FILE, "w");
  228. if (!outf)
  229. err(1, "creating %s", INPUT_FILE);
  230. fprintf(outf, "%s", PRE_BOILERPLATE);
  231. switch (test->style) {
  232. case EXECUTE:
  233. case INSIDE_MAIN:
  234. fprintf(outf, "%s", MAIN_START_BOILERPLATE);
  235. fprintf(outf, "%s", test->fragment);
  236. fprintf(outf, "%s", MAIN_END_BOILERPLATE);
  237. break;
  238. case OUTSIDE_MAIN:
  239. fprintf(outf, "%s", test->fragment);
  240. fprintf(outf, "%s", MAIN_START_BOILERPLATE);
  241. fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
  242. fprintf(outf, "%s", MAIN_END_BOILERPLATE);
  243. break;
  244. case DEFINES_FUNC:
  245. fprintf(outf, "%s", test->fragment);
  246. fprintf(outf, "%s", MAIN_START_BOILERPLATE);
  247. fprintf(outf, "%s", USE_FUNC_BOILERPLATE);
  248. fprintf(outf, "%s", MAIN_BODY_BOILERPLATE);
  249. fprintf(outf, "%s", MAIN_END_BOILERPLATE);
  250. break;
  251. }
  252. fclose(outf);
  253. if (verbose > 1)
  254. if (system("cat " INPUT_FILE) == -1);
  255. output = run(cmd, &status);
  256. if (status != 0 || strstr(output, "warning")) {
  257. if (verbose)
  258. printf("Compile %s for %s, status %i: %s\n",
  259. status ? "fail" : "warning",
  260. test->name, status, output);
  261. if (test->style == EXECUTE)
  262. errx(1, "Test for %s did not compile:\n%s",
  263. test->name, output);
  264. test->answer = false;
  265. free(output);
  266. } else {
  267. /* Compile succeeded. */
  268. free(output);
  269. /* We run INSIDE_MAIN tests for sanity checking. */
  270. if (test->style == EXECUTE || test->style == INSIDE_MAIN) {
  271. output = run("./" OUTPUT_FILE, &status);
  272. if (test->style == INSIDE_MAIN && status != 0)
  273. errx(1, "Test for %s failed with %i:\n%s",
  274. test->name, status, output);
  275. if (verbose && status)
  276. printf("%s exited %i\n", test->name, status);
  277. free(output);
  278. }
  279. test->answer = (status == 0);
  280. }
  281. test->done = true;
  282. return test->answer;
  283. }
  284. int main(int argc, char *argv[])
  285. {
  286. char *cmd;
  287. char *default_args[] = { "", DEFAULT_COMPILER, DEFAULT_FLAGS, NULL };
  288. unsigned int i;
  289. if (argc > 1) {
  290. if (strcmp(argv[1], "--help") == 0) {
  291. printf("Usage: configurator [-v] [<compiler> <flags>...]\n"
  292. " <compiler> <flags> will have \"-o <outfile> <infile.c>\" appended\n"
  293. "Default: %s %s\n",
  294. DEFAULT_COMPILER, DEFAULT_FLAGS);
  295. exit(0);
  296. }
  297. if (strcmp(argv[1], "-v") == 0) {
  298. argc--;
  299. argv++;
  300. verbose = 1;
  301. } else if (strcmp(argv[1], "-vv") == 0) {
  302. argc--;
  303. argv++;
  304. verbose = 2;
  305. }
  306. }
  307. if (argc == 1)
  308. argv = default_args;
  309. cmd = connect_args(argv, "-o " OUTPUT_FILE " " INPUT_FILE);
  310. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
  311. run_test(cmd, &tests[i]);
  312. unlink(OUTPUT_FILE);
  313. unlink(INPUT_FILE);
  314. cmd[strlen(cmd) - strlen(" -o " OUTPUT_FILE " " INPUT_FILE)] = '\0';
  315. printf("/* Generated by CCAN configurator */\n"
  316. "#ifndef CCAN_CONFIG_H\n"
  317. "#define CCAN_CONFIG_H\n");
  318. printf("#define _GNU_SOURCE /* Always use GNU extensions. */\n");
  319. printf("#define CCAN_COMPILER \"%s\"\n", argv[1]);
  320. printf("#define CCAN_CFLAGS \"%s\"\n\n", cmd + strlen(argv[1]) + 1);
  321. for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
  322. printf("#define %s %u\n", tests[i].name, tests[i].answer);
  323. printf("#endif /* CCAN_CONFIG_H */\n");
  324. return 0;
  325. }