depends.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <ccan/talloc/talloc.h>
  2. #include <ccan/str/str.h>
  3. #include <ccan/grab_file/grab_file.h>
  4. #include <ccan/str_talloc/str_talloc.h>
  5. #include "tools.h"
  6. #include <err.h>
  7. #include <stdbool.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. static char ** __attribute__((format(printf, 3, 4)))
  11. lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...)
  12. {
  13. va_list ap;
  14. char *cmd, *buffer;
  15. FILE *p;
  16. va_start(ap, format);
  17. cmd = talloc_vasprintf(ctx, format, ap);
  18. va_end(ap);
  19. p = popen(cmd, "r");
  20. if (!p)
  21. err(1, "Executing '%s'", cmd);
  22. buffer = grab_fd(ctx, fileno(p), NULL);
  23. if (!buffer)
  24. err(1, "Reading from '%s'", cmd);
  25. pclose(p);
  26. return strsplit(ctx, buffer, "\n", num);
  27. }
  28. static int unlink_info(char *infofile)
  29. {
  30. unlink(infofile);
  31. return 0;
  32. }
  33. /* Be careful about trying to compile over running programs (parallel make) */
  34. static char *compile_info(const void *ctx, const char *dir, const char *name)
  35. {
  36. char *infofile = talloc_asprintf(ctx, "%s/info.%u", dir, getpid());
  37. char *cmd = talloc_asprintf(ctx, "cc " CFLAGS
  38. " -o %s -x c %s/%s/_info",
  39. infofile, dir, name);
  40. talloc_set_destructor(infofile, unlink_info);
  41. if (system(cmd) != 0)
  42. return NULL;
  43. return infofile;
  44. }
  45. static char **get_one_deps(const void *ctx, const char *dir,
  46. const char *name, unsigned int *num)
  47. {
  48. char **deps, *cmd, *infofile;
  49. infofile = compile_info(ctx, dir, name);
  50. if (!infofile)
  51. errx(1, "Could not compile _info for '%s'", name);
  52. cmd = talloc_asprintf(ctx, "%s depends", infofile);
  53. deps = lines_from_cmd(cmd, num, "%s", cmd);
  54. if (!deps)
  55. err(1, "Could not run '%s'", cmd);
  56. return deps;
  57. }
  58. /* Make copy of src, replacing "from" with "to". */
  59. static char *replace(const void *ctx, const char *src,
  60. const char *from, const char *to)
  61. {
  62. char *ret = talloc_strdup(ctx, "");
  63. unsigned int rlen, len, add;
  64. rlen = len = 0;
  65. for (;;) {
  66. const char *next = strstr(src+len, from);
  67. if (!next)
  68. add = strlen(src+len) + 1;
  69. else
  70. add = next - (src+len);
  71. ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
  72. memcpy(ret+rlen, src+len, add);
  73. if (!next)
  74. return ret;
  75. len += add;
  76. rlen += add;
  77. strcpy(ret+rlen, to);
  78. rlen += strlen(to);
  79. len += strlen(from);
  80. }
  81. }
  82. /* This is a terrible hack. We scan for ccan/ strings. */
  83. static char **get_one_safe_deps(const void *ctx,
  84. const char *dir, const char *name,
  85. unsigned int *num)
  86. {
  87. char **deps, **lines, *raw, *fname;
  88. unsigned int i, n = 0;
  89. fname = talloc_asprintf(ctx, "%s/%s/_info", dir, name);
  90. raw = grab_file(fname, fname, NULL);
  91. if (!raw)
  92. errx(1, "Could not open %s", fname);
  93. /* Replace \n by actual line breaks, and split it. */
  94. lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n", &n);
  95. deps = talloc_array(ctx, char *, n+1);
  96. for (n = i = 0; lines[i]; i++) {
  97. char *str;
  98. unsigned int len;
  99. /* Ignore lines starting with # (e.g. #include) */
  100. if (lines[i][0] == '#')
  101. continue;
  102. /* Start of line, or after ". */
  103. if (strstarts(lines[i], "ccan/"))
  104. str = lines[i];
  105. else {
  106. str = strstr(lines[i], "\"ccan/");
  107. if (!str)
  108. continue;
  109. str++;
  110. }
  111. len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
  112. if (len == 5)
  113. continue;
  114. deps[n++] = talloc_strndup(deps, str, len);
  115. }
  116. deps[n] = NULL;
  117. talloc_free(fname);
  118. if (num)
  119. *num = n;
  120. return deps;
  121. }
  122. static bool have_dep(char **deps, unsigned int num, const char *dep)
  123. {
  124. unsigned int i;
  125. for (i = 0; i < num; i++)
  126. if (streq(deps[i], dep))
  127. return true;
  128. return false;
  129. }
  130. /* Gets all the dependencies, recursively. */
  131. static char **
  132. get_all_deps(const void *ctx, const char *dir, const char *name,
  133. char **(*get_one)(const void *, const char *, const char *,
  134. unsigned int *))
  135. {
  136. char **deps;
  137. unsigned int i, num;
  138. deps = get_one(ctx, dir, name, &num);
  139. for (i = 0; i < num; i++) {
  140. char **newdeps;
  141. unsigned int j, newnum;
  142. if (!strstarts(deps[i], "ccan/"))
  143. continue;
  144. newdeps = get_one(ctx, dir, deps[i] + strlen("ccan/"),
  145. &newnum);
  146. /* Should be short, so brute-force out dups. */
  147. for (j = 0; j < newnum; j++) {
  148. if (have_dep(deps, num, newdeps[j]))
  149. continue;
  150. deps = talloc_realloc(NULL, deps, char *, num + 2);
  151. deps[num++] = newdeps[j];
  152. deps[num] = NULL;
  153. }
  154. }
  155. return deps;
  156. }
  157. char **get_libs(const void *ctx, const char *dir,
  158. const char *name, unsigned int *num)
  159. {
  160. char **libs, *cmd, *infofile;
  161. infofile = compile_info(ctx, dir, name);
  162. if (!infofile)
  163. errx(1, "Could not compile _info for '%s'", name);
  164. cmd = talloc_asprintf(ctx, "%s libs", infofile);
  165. libs = lines_from_cmd(cmd, num, "%s", cmd);
  166. if (!libs)
  167. err(1, "Could not run '%s'", cmd);
  168. return libs;
  169. }
  170. char **get_deps(const void *ctx, const char *dir, const char *name,
  171. bool recurse)
  172. {
  173. if (!recurse) {
  174. unsigned int num;
  175. return get_one_deps(ctx, dir, name, &num);
  176. }
  177. return get_all_deps(ctx, dir, name, get_one_deps);
  178. }
  179. char **get_safe_ccan_deps(const void *ctx, const char *dir,
  180. const char *name, bool recurse)
  181. {
  182. if (!recurse) {
  183. unsigned int num;
  184. return get_one_safe_deps(ctx, dir, name, &num);
  185. }
  186. return get_all_deps(ctx, dir, name, get_one_safe_deps);
  187. }