configurator.c 9.5 KB

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