depends.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <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, 3, 4)))
  15. lines_from_cmd(const void *ctx, unsigned int *num, 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", num);
  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, *errmsg, *ccandir;
  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");
  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. *strrchr(ccandir, '/') = '\0';
  53. return compile_and_link(ctx, info_c_file, ccandir, "", "", "",
  54. &errmsg);
  55. }
  56. static char **get_one_deps(const void *ctx, const char *dir,
  57. unsigned int *num, char **infofile)
  58. {
  59. char **deps, *cmd;
  60. if (!*infofile) {
  61. *infofile = compile_info(ctx, dir);
  62. if (!*infofile)
  63. errx(1, "Could not compile _info for '%s'", dir);
  64. }
  65. cmd = talloc_asprintf(ctx, "%s depends", *infofile);
  66. deps = lines_from_cmd(cmd, num, "%s", cmd);
  67. if (!deps)
  68. err(1, "Could not run '%s'", cmd);
  69. return deps;
  70. }
  71. /* Make copy of src, replacing "from" with "to". */
  72. static char *replace(const void *ctx, const char *src,
  73. const char *from, const char *to)
  74. {
  75. char *ret = talloc_strdup(ctx, "");
  76. unsigned int rlen, len, add;
  77. rlen = len = 0;
  78. for (;;) {
  79. const char *next = strstr(src+len, from);
  80. if (!next)
  81. add = strlen(src+len) + 1;
  82. else
  83. add = next - (src+len);
  84. ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
  85. memcpy(ret+rlen, src+len, add);
  86. if (!next)
  87. return ret;
  88. len += add;
  89. rlen += add;
  90. strcpy(ret+rlen, to);
  91. rlen += strlen(to);
  92. len += strlen(from);
  93. }
  94. }
  95. /* This is a terrible hack. We scan for ccan/ strings. */
  96. static char **get_one_safe_deps(const void *ctx,
  97. const char *dir,
  98. unsigned int *num,
  99. char **infofile)
  100. {
  101. char **deps, **lines, *raw, *fname;
  102. unsigned int i, n = 0;
  103. fname = talloc_asprintf(ctx, "%s/_info", dir);
  104. raw = grab_file(fname, fname, NULL);
  105. if (!raw)
  106. errx(1, "Could not open %s", fname);
  107. /* Replace \n by actual line breaks, and split it. */
  108. lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n", &n);
  109. deps = talloc_array(ctx, char *, n+1);
  110. for (n = i = 0; lines[i]; i++) {
  111. char *str;
  112. unsigned int len;
  113. /* Ignore lines starting with # (e.g. #include) */
  114. if (lines[i][0] == '#')
  115. continue;
  116. /* Start of line, or after ". */
  117. if (strstarts(lines[i], "ccan/"))
  118. str = lines[i];
  119. else {
  120. str = strstr(lines[i], "\"ccan/");
  121. if (!str)
  122. continue;
  123. str++;
  124. }
  125. len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
  126. if (len == 5)
  127. continue;
  128. deps[n++] = talloc_strndup(deps, str, len);
  129. }
  130. deps[n] = NULL;
  131. talloc_free(fname);
  132. if (num)
  133. *num = n;
  134. return deps;
  135. }
  136. static bool have_dep(char **deps, unsigned int num, const char *dep)
  137. {
  138. unsigned int i;
  139. for (i = 0; i < num; i++)
  140. if (streq(deps[i], dep))
  141. return true;
  142. return false;
  143. }
  144. /* Gets all the dependencies, recursively. */
  145. static char **
  146. get_all_deps(const void *ctx, const char *dir,
  147. char **infofile,
  148. char **(*get_one)(const void *, const char *,
  149. unsigned int *, char **))
  150. {
  151. char **deps;
  152. unsigned int i, num;
  153. deps = get_one(ctx, dir, &num, infofile);
  154. for (i = 0; i < num; i++) {
  155. char **newdeps;
  156. unsigned int j, newnum;
  157. char *subinfo = NULL;
  158. char *subdir;
  159. if (!strstarts(deps[i], "ccan/"))
  160. continue;
  161. subdir = talloc_asprintf(ctx, "%s/%s",
  162. talloc_dirname(ctx, dir),
  163. deps[i] + strlen("ccan/"));
  164. newdeps = get_one(ctx, subdir, &newnum, &subinfo);
  165. /* Should be short, so brute-force out dups. */
  166. for (j = 0; j < newnum; j++) {
  167. if (have_dep(deps, num, newdeps[j]))
  168. continue;
  169. deps = talloc_realloc(NULL, deps, char *, num + 2);
  170. deps[num++] = newdeps[j];
  171. deps[num] = NULL;
  172. }
  173. }
  174. return deps;
  175. }
  176. char **get_libs(const void *ctx, const char *dir,
  177. unsigned int *num, char **infofile)
  178. {
  179. char **libs, *cmd;
  180. if (!*infofile) {
  181. *infofile = compile_info(ctx, dir);
  182. if (!*infofile)
  183. errx(1, "Could not compile _info for '%s'", dir);
  184. }
  185. cmd = talloc_asprintf(ctx, "%s libs", *infofile);
  186. libs = lines_from_cmd(cmd, num, "%s", cmd);
  187. if (!libs)
  188. err(1, "Could not run '%s'", cmd);
  189. return libs;
  190. }
  191. char **get_deps(const void *ctx, const char *dir,
  192. bool recurse, char **infofile)
  193. {
  194. char *temp = NULL, **ret;
  195. if (!infofile)
  196. infofile = &temp;
  197. if (!recurse) {
  198. unsigned int num;
  199. ret = get_one_deps(ctx, dir, &num, infofile);
  200. } else
  201. ret = get_all_deps(ctx, dir, infofile, get_one_deps);
  202. if (infofile == &temp && temp) {
  203. unlink(temp);
  204. talloc_free(temp);
  205. }
  206. return ret;
  207. }
  208. char **get_safe_ccan_deps(const void *ctx, const char *dir,
  209. bool recurse, char **infofile)
  210. {
  211. if (!recurse) {
  212. unsigned int num;
  213. return get_one_safe_deps(ctx, dir, &num, infofile);
  214. }
  215. return get_all_deps(ctx, dir, infofile, get_one_safe_deps);
  216. }