depends.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 -x c %s/_info",
  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", 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. /* Ignore lines starting with # (e.g. #include) */
  96. if (lines[i][0] == '#')
  97. continue;
  98. /* Start of line, or after ". */
  99. if (strstarts(lines[i], "ccan/"))
  100. str = lines[i];
  101. else {
  102. str = strstr(lines[i], "\"ccan/");
  103. if (!str)
  104. continue;
  105. str++;
  106. }
  107. len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
  108. if (len == 5)
  109. continue;
  110. deps[n++] = talloc_strndup(deps, str, len);
  111. }
  112. deps[n] = NULL;
  113. talloc_free(fname);
  114. if (num)
  115. *num = n;
  116. return deps;
  117. }
  118. static bool have_dep(char **deps, unsigned int num, const char *dep)
  119. {
  120. unsigned int i;
  121. for (i = 0; i < num; i++)
  122. if (streq(deps[i], dep))
  123. return true;
  124. return false;
  125. }
  126. /* Gets all the dependencies, recursively. */
  127. static char **
  128. get_all_deps(const void *ctx, const char *dir,
  129. char **(*get_one)(const void *, const char *, unsigned int *))
  130. {
  131. char **deps;
  132. unsigned int i, num;
  133. deps = get_one(ctx, dir, &num);
  134. for (i = 0; i < num; i++) {
  135. char **newdeps;
  136. unsigned int j, newnum;
  137. if (!strstarts(deps[i], "ccan/"))
  138. continue;
  139. newdeps = get_one(ctx, deps[i], &newnum);
  140. /* Should be short, so brute-force out dups. */
  141. for (j = 0; j < newnum; j++) {
  142. if (have_dep(deps, num, newdeps[j]))
  143. continue;
  144. deps = talloc_realloc(NULL, deps, char *, num + 2);
  145. deps[num++] = newdeps[j];
  146. deps[num] = NULL;
  147. }
  148. }
  149. return deps;
  150. }
  151. char **get_deps(const void *ctx, const char *dir, bool recurse)
  152. {
  153. if (!recurse) {
  154. unsigned int num;
  155. return get_one_deps(ctx, dir, &num);
  156. }
  157. return get_all_deps(ctx, dir, get_one_deps);
  158. }
  159. char **get_safe_ccan_deps(const void *ctx, const char *dir, bool recurse)
  160. {
  161. if (!recurse) {
  162. unsigned int num;
  163. return get_one_safe_deps(ctx, dir, &num);
  164. }
  165. return get_all_deps(ctx, dir, get_one_safe_deps);
  166. }