depends.c 4.0 KB

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