depends.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include <ccan/str/str.h>
  2. #include <ccan/talloc/talloc.h>
  3. #include <ccan/grab_file/grab_file.h>
  4. #include <ccan/str_talloc/str_talloc.h>
  5. #include <ccan/read_write_all/read_write_all.h>
  6. #include <ccan/compiler/compiler.h>
  7. #include "tools.h"
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <err.h>
  12. #include <stdbool.h>
  13. #include <unistd.h>
  14. #include <errno.h>
  15. static char ** PRINTF_FMT(2, 3)
  16. lines_from_cmd(const void *ctx, const char *format, ...)
  17. {
  18. va_list ap;
  19. char *cmd, *buffer;
  20. FILE *p;
  21. va_start(ap, format);
  22. cmd = talloc_vasprintf(ctx, format, ap);
  23. va_end(ap);
  24. p = popen(cmd, "r");
  25. if (!p)
  26. err(1, "Executing '%s'", cmd);
  27. buffer = grab_fd(ctx, fileno(p), NULL);
  28. if (!buffer)
  29. err(1, "Reading from '%s'", cmd);
  30. pclose(p);
  31. return strsplit(ctx, buffer, "\n");
  32. }
  33. /* Be careful about trying to compile over running programs (parallel make).
  34. * temp_file helps here. */
  35. static char *compile_info(const void *ctx, const char *dir)
  36. {
  37. char *info_c_file, *info, *ccandir, *compiled, *output;
  38. size_t len;
  39. int fd;
  40. /* Copy it to a file with proper .c suffix. */
  41. info = grab_file(ctx, talloc_asprintf(ctx, "%s/_info", dir), &len);
  42. if (!info)
  43. return NULL;
  44. info_c_file = temp_file(ctx, ".c", "_info");
  45. fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
  46. if (fd < 0)
  47. return NULL;
  48. if (!write_all(fd, info, len))
  49. return NULL;
  50. if (close(fd) != 0)
  51. return NULL;
  52. ccandir = talloc_dirname(ctx, dir);
  53. if (strrchr(ccandir, '/'))
  54. *strrchr(ccandir, '/') = '\0';
  55. compiled = temp_file(ctx, "", "info");
  56. if (compile_and_link(ctx, info_c_file, ccandir, "",
  57. CCAN_COMPILER, CCAN_CFLAGS " -I.", "",
  58. compiled, &output))
  59. return compiled;
  60. return NULL;
  61. }
  62. static char **get_one_deps(const void *ctx, const char *dir, char **infofile)
  63. {
  64. char **deps, *cmd;
  65. if (!*infofile) {
  66. *infofile = compile_info(ctx, dir);
  67. if (!*infofile)
  68. errx(1, "Could not compile _info for '%s'", dir);
  69. }
  70. cmd = talloc_asprintf(ctx, "%s depends", *infofile);
  71. deps = lines_from_cmd(cmd, "%s", cmd);
  72. if (!deps)
  73. err(1, "Could not run '%s'", cmd);
  74. return deps;
  75. }
  76. /* Make copy of src, replacing "from" with "to". */
  77. static char *replace(const void *ctx, const char *src,
  78. const char *from, const char *to)
  79. {
  80. char *ret = talloc_strdup(ctx, "");
  81. unsigned int rlen, len, add;
  82. rlen = len = 0;
  83. for (;;) {
  84. const char *next = strstr(src+len, from);
  85. if (!next)
  86. add = strlen(src+len) + 1;
  87. else
  88. add = next - (src+len);
  89. ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
  90. memcpy(ret+rlen, src+len, add);
  91. if (!next)
  92. return ret;
  93. len += add;
  94. rlen += add;
  95. strcpy(ret+rlen, to);
  96. rlen += strlen(to);
  97. len += strlen(from);
  98. }
  99. }
  100. /* This is a terrible hack. We scan for ccan/ strings. */
  101. static char **get_one_safe_deps(const void *ctx,
  102. const char *dir,
  103. char **infofile)
  104. {
  105. char **deps, **lines, *raw, *fname;
  106. unsigned int i, n;
  107. fname = talloc_asprintf(ctx, "%s/_info", dir);
  108. raw = grab_file(fname, fname, NULL);
  109. if (!raw)
  110. errx(1, "Could not open %s", fname);
  111. /* Replace \n by actual line breaks, and split it. */
  112. lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n");
  113. deps = talloc_array(ctx, char *, talloc_array_length(lines));
  114. for (n = i = 0; lines[i]; i++) {
  115. char *str;
  116. unsigned int len;
  117. /* Ignore lines starting with # (e.g. #include) */
  118. if (lines[i][0] == '#')
  119. continue;
  120. /* Start of line, or after ". */
  121. if (strstarts(lines[i], "ccan/"))
  122. str = lines[i];
  123. else {
  124. str = strstr(lines[i], "\"ccan/");
  125. if (!str)
  126. continue;
  127. str++;
  128. }
  129. len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
  130. if (len == 5)
  131. continue;
  132. deps[n++] = talloc_strndup(deps, str, len);
  133. }
  134. deps[n] = NULL;
  135. talloc_free(fname);
  136. /* Make sure talloc_array_length() works */
  137. return talloc_realloc(NULL, deps, char *, n + 1);
  138. }
  139. static bool have_dep(char **deps, const char *dep)
  140. {
  141. unsigned int i;
  142. for (i = 0; deps[i]; i++)
  143. if (streq(deps[i], dep))
  144. return true;
  145. return false;
  146. }
  147. /* Gets all the dependencies, recursively. */
  148. static char **
  149. get_all_deps(const void *ctx, const char *dir,
  150. char **infofile,
  151. char **(*get_one)(const void *, const char *, char **))
  152. {
  153. char **deps;
  154. unsigned int i;
  155. deps = get_one(ctx, dir, infofile);
  156. for (i = 0; i < talloc_array_length(deps)-1; i++) {
  157. char **newdeps;
  158. unsigned int j;
  159. char *subinfo = NULL;
  160. char *subdir;
  161. if (!strstarts(deps[i], "ccan/"))
  162. continue;
  163. subdir = talloc_asprintf(ctx, "%s/%s",
  164. talloc_dirname(ctx, dir),
  165. deps[i] + strlen("ccan/"));
  166. newdeps = get_one(ctx, subdir, &subinfo);
  167. /* Should be short, so brute-force out dups. */
  168. for (j = 0; j < talloc_array_length(newdeps)-1; j++) {
  169. unsigned int num;
  170. if (have_dep(deps, newdeps[j]))
  171. continue;
  172. num = talloc_array_length(deps)-1;
  173. deps = talloc_realloc(NULL, deps, char *, num + 2);
  174. deps[num] = newdeps[j];
  175. deps[num+1] = NULL;
  176. }
  177. }
  178. return deps;
  179. }
  180. char **get_libs(const void *ctx, const char *dir,
  181. unsigned int *num, char **infofile)
  182. {
  183. char **libs, *cmd;
  184. if (!*infofile) {
  185. *infofile = compile_info(ctx, dir);
  186. if (!*infofile)
  187. errx(1, "Could not compile _info for '%s'", dir);
  188. }
  189. cmd = talloc_asprintf(ctx, "%s libs", *infofile);
  190. libs = lines_from_cmd(cmd, "%s", cmd);
  191. if (!libs)
  192. err(1, "Could not run '%s'", cmd);
  193. /* FIXME: Do we need num arg? */
  194. *num = talloc_array_length(libs) - 1;
  195. return libs;
  196. }
  197. /* FIXME: This is O(n^2), which is dumb. */
  198. static char **uniquify_deps(char **deps)
  199. {
  200. unsigned int i, j, num;
  201. num = talloc_array_length(deps) - 1;
  202. for (i = 0; i < num; i++) {
  203. for (j = i + 1; j < num; j++) {
  204. if (streq(deps[i], deps[j])) {
  205. memmove(&deps[j], &deps[j+1],
  206. (num - j - 1) * sizeof(char *));
  207. num--;
  208. }
  209. }
  210. }
  211. deps[num] = NULL;
  212. /* Make sure talloc_array_length() works */
  213. return talloc_realloc(NULL, deps, char *, num + 1);
  214. }
  215. char **get_deps(const void *ctx, const char *dir,
  216. bool recurse, char **infofile)
  217. {
  218. char *temp = NULL, **ret;
  219. if (!infofile)
  220. infofile = &temp;
  221. if (!recurse) {
  222. ret = get_one_deps(ctx, dir, infofile);
  223. } else
  224. ret = get_all_deps(ctx, dir, infofile, get_one_deps);
  225. if (infofile == &temp && temp) {
  226. unlink(temp);
  227. talloc_free(temp);
  228. }
  229. return uniquify_deps(ret);
  230. }
  231. char **get_safe_ccan_deps(const void *ctx, const char *dir,
  232. bool recurse)
  233. {
  234. char **ret;
  235. if (!recurse) {
  236. ret = get_one_safe_deps(ctx, dir, NULL);
  237. } else {
  238. ret = get_all_deps(ctx, dir, NULL, get_one_safe_deps);
  239. }
  240. return uniquify_deps(ret);
  241. }