depends.c 5.7 KB

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