depends.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. 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,
  63. char *(*get_info)(const void *ctx, const char *dir))
  64. {
  65. char **deps, *cmd;
  66. char *infofile = get_info(ctx, dir);
  67. if (!infofile)
  68. return NULL;
  69. cmd = talloc_asprintf(ctx, "%s depends", infofile);
  70. deps = lines_from_cmd(cmd, "%s", cmd);
  71. if (!deps)
  72. err(1, "Could not run '%s'", cmd);
  73. return deps;
  74. }
  75. /* Make copy of src, replacing "from" with "to". */
  76. static char *replace(const void *ctx, const char *src,
  77. const char *from, const char *to)
  78. {
  79. char *ret = talloc_strdup(ctx, "");
  80. unsigned int rlen, len, add;
  81. rlen = len = 0;
  82. for (;;) {
  83. const char *next = strstr(src+len, from);
  84. if (!next)
  85. add = strlen(src+len) + 1;
  86. else
  87. add = next - (src+len);
  88. ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
  89. memcpy(ret+rlen, src+len, add);
  90. if (!next)
  91. return ret;
  92. len += add;
  93. rlen += add;
  94. strcpy(ret+rlen, to);
  95. rlen += strlen(to);
  96. len += strlen(from);
  97. }
  98. }
  99. /* This is a terrible hack. We scan for ccan/ strings. */
  100. static char **get_one_safe_deps(const void *ctx,
  101. const char *dir,
  102. char *(*unused)(const void *, const char *))
  103. {
  104. char **deps, **lines, *raw, *fname;
  105. unsigned int i, n;
  106. fname = talloc_asprintf(ctx, "%s/_info", dir);
  107. raw = grab_file(fname, fname, NULL);
  108. if (!raw)
  109. errx(1, "Could not open %s", fname);
  110. /* Replace \n by actual line breaks, and split it. */
  111. lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n");
  112. deps = talloc_array(ctx, char *, talloc_array_length(lines));
  113. for (n = i = 0; lines[i]; i++) {
  114. char *str;
  115. unsigned int len;
  116. /* Ignore lines starting with # (e.g. #include) */
  117. if (lines[i][0] == '#')
  118. continue;
  119. /* Start of line, or after ". */
  120. if (strstarts(lines[i], "ccan/"))
  121. str = lines[i];
  122. else {
  123. str = strstr(lines[i], "\"ccan/");
  124. if (!str)
  125. continue;
  126. str++;
  127. }
  128. len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
  129. if (len == 5)
  130. continue;
  131. deps[n++] = talloc_strndup(deps, str, len);
  132. }
  133. deps[n] = NULL;
  134. talloc_free(fname);
  135. /* Make sure talloc_array_length() works */
  136. return talloc_realloc(NULL, deps, char *, n + 1);
  137. }
  138. static bool have_dep(char **deps, const char *dep)
  139. {
  140. unsigned int i;
  141. for (i = 0; deps[i]; i++)
  142. if (streq(deps[i], dep))
  143. return true;
  144. return false;
  145. }
  146. /* Gets all the dependencies, recursively. */
  147. static char **
  148. get_all_deps(const void *ctx, const char *dir,
  149. char *(*get_info)(const void *ctx, const char *dir),
  150. char **(*get_one)(const void *, const char *,
  151. char *(*get_info)(const void *, const char *)))
  152. {
  153. char **deps;
  154. unsigned int i;
  155. deps = get_one(ctx, dir, get_info);
  156. for (i = 0; i < talloc_array_length(deps)-1; i++) {
  157. char **newdeps;
  158. unsigned int j;
  159. char *subdir;
  160. if (!strstarts(deps[i], "ccan/"))
  161. continue;
  162. subdir = talloc_asprintf(ctx, "%s/%s",
  163. talloc_dirname(ctx, dir),
  164. deps[i] + strlen("ccan/"));
  165. newdeps = get_one(ctx, subdir, get_info);
  166. /* Should be short, so brute-force out dups. */
  167. for (j = 0; j < talloc_array_length(newdeps)-1; j++) {
  168. unsigned int num;
  169. if (have_dep(deps, newdeps[j]))
  170. continue;
  171. num = talloc_array_length(deps)-1;
  172. deps = talloc_realloc(NULL, deps, char *, num + 2);
  173. deps[num] = newdeps[j];
  174. deps[num+1] = NULL;
  175. }
  176. }
  177. return deps;
  178. }
  179. /* Can return NULL: _info may not support 'libs'. */
  180. static char **get_one_libs(const void *ctx, const char *dir,
  181. char *(*get_info)(const void *ctx, const char *dir))
  182. {
  183. char *cmd, **lines;
  184. cmd = talloc_asprintf(ctx, "%s libs", get_info(ctx, dir));
  185. lines = lines_from_cmd(cmd, "%s", cmd);
  186. /* Strip final NULL. */
  187. if (lines)
  188. lines = talloc_realloc(NULL, lines, char *,
  189. talloc_array_length(lines)-1);
  190. return lines;
  191. }
  192. char **get_libs(const void *ctx, const char *dir, bool recurse,
  193. char *(*get_info)(const void *ctx, const char *dir))
  194. {
  195. char **deps, **libs;
  196. unsigned int i, len;
  197. libs = get_one_libs(ctx, dir, get_info);
  198. len = talloc_array_length(libs);
  199. if (recurse) {
  200. deps = get_deps(ctx, dir, true, get_info);
  201. for (i = 0; deps[i]; i++) {
  202. char **newlibs, *subdir;
  203. size_t newlen;
  204. if (!strstarts(deps[i], "ccan/"))
  205. continue;
  206. subdir = talloc_asprintf(ctx, "%s/%s",
  207. talloc_dirname(ctx, dir),
  208. deps[i] + strlen("ccan/"));
  209. newlibs = get_one_libs(ctx, subdir, get_info);
  210. newlen = talloc_array_length(newlibs);
  211. libs = talloc_realloc(ctx, libs, char *, len + newlen);
  212. memcpy(&libs[len], newlibs,
  213. sizeof(newlibs[0])*newlen);
  214. len += newlen;
  215. }
  216. }
  217. /* Append NULL entry. */
  218. libs = talloc_realloc(ctx, libs, char *, len + 1);
  219. libs[len] = NULL;
  220. return libs;
  221. }
  222. /* FIXME: This is O(n^2), which is dumb. */
  223. static char **uniquify_deps(char **deps)
  224. {
  225. unsigned int i, j, num;
  226. if (!deps)
  227. return NULL;
  228. num = talloc_array_length(deps) - 1;
  229. for (i = 0; i < num; i++) {
  230. for (j = i + 1; j < num; j++) {
  231. if (streq(deps[i], deps[j])) {
  232. memmove(&deps[j], &deps[j+1],
  233. (num - j - 1) * sizeof(char *));
  234. num--;
  235. }
  236. }
  237. }
  238. deps[num] = NULL;
  239. /* Make sure talloc_array_length() works */
  240. return talloc_realloc(NULL, deps, char *, num + 1);
  241. }
  242. char **get_deps(const void *ctx, const char *dir, bool recurse,
  243. char *(*get_info)(const void *ctx, const char *dir))
  244. {
  245. char **ret;
  246. if (!recurse) {
  247. ret = get_one_deps(ctx, dir, get_info);
  248. } else
  249. ret = get_all_deps(ctx, dir, get_info, get_one_deps);
  250. return uniquify_deps(ret);
  251. }
  252. char **get_safe_ccan_deps(const void *ctx, const char *dir,
  253. bool recurse)
  254. {
  255. char **ret;
  256. if (!recurse) {
  257. ret = get_one_safe_deps(ctx, dir, NULL);
  258. } else {
  259. ret = get_all_deps(ctx, dir, NULL, get_one_safe_deps);
  260. }
  261. return uniquify_deps(ret);
  262. }