depends.c 4.3 KB

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