configurator.c 9.7 KB

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