depends.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 "tools.h"
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <err.h>
  11. #include <stdbool.h>
  12. #include <unistd.h>
  13. #include <errno.h>
  14. static char ** __attribute__((format(printf, 2, 3)))
  15. lines_from_cmd(const void *ctx, const char *format, ...)
  16. {
  17. va_list ap;
  18. char *cmd, *buffer;
  19. FILE *p;
  20. va_start(ap, format);
  21. cmd = talloc_vasprintf(ctx, format, ap);
  22. va_end(ap);
  23. p = popen(cmd, "r");
  24. if (!p)
  25. err(1, "Executing '%s'", cmd);
  26. buffer = grab_fd(ctx, fileno(p), NULL);
  27. if (!buffer)
  28. err(1, "Reading from '%s'", cmd);
  29. pclose(p);
  30. return strsplit(ctx, buffer, "\n");
  31. }
  32. /* Be careful about trying to compile over running programs (parallel make).
  33. * temp_file helps here. */
  34. static char *compile_info(const void *ctx, const char *dir)
  35. {
  36. char *info_c_file, *info, *ccandir, *compiled, *output;
  37. size_t len;
  38. int fd;
  39. /* Copy it to a file with proper .c suffix. */
  40. info = grab_file(ctx, talloc_asprintf(ctx, "%s/_info", dir), &len);
  41. if (!info)
  42. return NULL;
  43. info_c_file = temp_file(ctx, ".c", "_info");
  44. fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
  45. if (fd < 0)
  46. return NULL;
  47. if (!write_all(fd, info, len))
  48. return NULL;
  49. if (close(fd) != 0)
  50. return NULL;
  51. ccandir = talloc_dirname(ctx, dir);
  52. if (strrchr(ccandir, '/'))
  53. *strrchr(ccandir, '/') = '\0';
  54. compiled = temp_file(ctx, "", "info");
  55. if (compile_and_link(ctx, info_c_file, ccandir, "",
  56. CCAN_COMPILER, CCAN_CFLAGS " -I.", "",
  57. compiled, &output))
  58. return compiled;
  59. return NULL;
  60. }
  61. static char **get_one_deps(const void *ctx, const char *dir, char **infofile)
  62. {
  63. char **deps, *cmd;
  64. if (!*infofile) {
  65. *infofile = compile_info(ctx, dir);
  66. if (!*infofile)
  67. errx(1, "Could not compile _info for '%s'", dir);
  68. }
  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 **infofile)
  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 **infofile,
  150. char **(*get_one)(const void *, const char *, char **))
  151. {
  152. char **deps;
  153. unsigned int i;
  154. deps = get_one(ctx, dir, infofile);
  155. for (i = 0; i < talloc_array_length(deps)-1; i++) {
  156. char **newdeps;
  157. unsigned int j;
  158. char *subinfo = NULL;
  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, &subinfo);
  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. char **get_libs(const void *ctx, const char *dir,
  180. unsigned int *num, char **infofile)
  181. {
  182. char **libs, *cmd;
  183. if (!*infofile) {
  184. *infofile = compile_info(ctx, dir);
  185. if (!*infofile)
  186. errx(1, "Could not compile _info for '%s'", dir);
  187. }
  188. cmd = talloc_asprintf(ctx, "%s libs", *infofile);
  189. libs = lines_from_cmd(cmd, "%s", cmd);
  190. if (!libs)
  191. err(1, "Could not run '%s'", cmd);
  192. /* FIXME: Do we need num arg? */
  193. *num = talloc_array_length(libs) - 1;
  194. return libs;
  195. }
  196. /* FIXME: This is O(n^2), which is dumb. */
  197. static char **uniquify_deps(char **deps)
  198. {
  199. unsigned int i, j, num;
  200. num = talloc_array_length(deps) - 1;
  201. for (i = 0; i < num; i++) {
  202. for (j = i + 1; j < num; j++) {
  203. if (streq(deps[i], deps[j])) {
  204. memmove(&deps[j], &deps[j+1],
  205. (num - j - 1) * sizeof(char *));
  206. num--;
  207. }
  208. }
  209. }
  210. deps[num] = NULL;
  211. /* Make sure talloc_array_length() works */
  212. return talloc_realloc(NULL, deps, char *, num + 1);
  213. }
  214. char **get_deps(const void *ctx, const char *dir,
  215. bool recurse, char **infofile)
  216. {
  217. char *temp = NULL, **ret;
  218. if (!infofile)
  219. infofile = &temp;
  220. if (!recurse) {
  221. ret = get_one_deps(ctx, dir, infofile);
  222. } else
  223. ret = get_all_deps(ctx, dir, infofile, get_one_deps);
  224. if (infofile == &temp && temp) {
  225. unlink(temp);
  226. talloc_free(temp);
  227. }
  228. return uniquify_deps(ret);
  229. }
  230. char **get_safe_ccan_deps(const void *ctx, const char *dir,
  231. bool recurse)
  232. {
  233. char **ret;
  234. if (!recurse) {
  235. ret = get_one_safe_deps(ctx, dir, NULL);
  236. } else {
  237. ret = get_all_deps(ctx, dir, NULL, get_one_safe_deps);
  238. }
  239. return uniquify_deps(ret);
  240. }