depends.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #include <ccan/str/str.h>
  2. #include <ccan/talloc/talloc.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 <ccan/compiler/compiler.h>
  7. #include <ccan/err/err.h>
  8. #include "tools.h"
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <stdbool.h>
  13. #include <unistd.h>
  14. #include <errno.h>
  15. static char ** PRINTF_FMT(2, 3)
  16. lines_from_cmd(const void *ctx, const char *format, ...)
  17. {
  18. va_list ap;
  19. char *cmd, *buffer;
  20. FILE *p;
  21. va_start(ap, format);
  22. cmd = talloc_vasprintf(ctx, format, ap);
  23. va_end(ap);
  24. p = popen(cmd, "r");
  25. if (!p)
  26. err(1, "Executing '%s'", cmd);
  27. buffer = grab_fd(ctx, fileno(p), NULL);
  28. if (!buffer)
  29. err(1, "Reading from '%s'", cmd);
  30. pclose(p);
  31. return strsplit(ctx, buffer, "\n");
  32. }
  33. /* Be careful about trying to compile over running programs (parallel make).
  34. * temp_file helps here. */
  35. char *compile_info(const void *ctx, const char *dir)
  36. {
  37. char *info_c_file, *info, *compiled, *output;
  38. size_t len;
  39. int fd;
  40. /* Copy it to a file with proper .c suffix. */
  41. info = grab_file(ctx, talloc_asprintf(ctx, "%s/_info", dir), &len);
  42. if (!info)
  43. return NULL;
  44. info_c_file = temp_file(ctx, ".c", "_info");
  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. compiled = temp_file(ctx, "", "info");
  53. if (compile_and_link(ctx, info_c_file, find_ccan_dir(dir), "",
  54. CCAN_COMPILER, CCAN_CFLAGS " -I.", "",
  55. compiled, &output))
  56. return compiled;
  57. return NULL;
  58. }
  59. static char **get_one_deps(const void *ctx, const char *dir, const char *style,
  60. char *(*get_info)(const void *ctx, const char *dir))
  61. {
  62. char **deps, *cmd;
  63. char *infofile = get_info(ctx, dir);
  64. if (!infofile)
  65. return NULL;
  66. cmd = talloc_asprintf(ctx, "%s %s", infofile, style);
  67. deps = lines_from_cmd(cmd, "%s", cmd);
  68. if (!deps) {
  69. /* You must understand depends, maybe not testdepends. */
  70. if (streq(style, "depends"))
  71. err(1, "Could not run '%s'", cmd);
  72. deps = talloc(ctx, char *);
  73. deps[0] = NULL;
  74. }
  75. return deps;
  76. }
  77. /* Make copy of src, replacing "from" with "to". */
  78. static char *replace(const void *ctx, const char *src,
  79. const char *from, const char *to)
  80. {
  81. char *ret = talloc_strdup(ctx, "");
  82. unsigned int rlen, len, add;
  83. rlen = len = 0;
  84. for (;;) {
  85. const char *next = strstr(src+len, from);
  86. if (!next)
  87. add = strlen(src+len) + 1;
  88. else
  89. add = next - (src+len);
  90. ret = talloc_realloc(ctx, ret, char, rlen + add + strlen(to)+1);
  91. memcpy(ret+rlen, src+len, add);
  92. if (!next)
  93. return ret;
  94. len += add;
  95. rlen += add;
  96. strcpy(ret+rlen, to);
  97. rlen += strlen(to);
  98. len += strlen(from);
  99. }
  100. }
  101. /* This is a terrible hack. We scan for ccan/ strings. */
  102. static char **get_one_safe_deps(const void *ctx,
  103. const char *dir,
  104. const char *style,
  105. char *(*unused)(const void *, const char *))
  106. {
  107. char **deps, **lines, *raw, *fname;
  108. unsigned int i, n;
  109. bool correct_style = false;
  110. fname = talloc_asprintf(ctx, "%s/_info", dir);
  111. raw = grab_file(fname, fname, NULL);
  112. if (!raw)
  113. errx(1, "Could not open %s", fname);
  114. /* Replace \n by actual line breaks, and split it. */
  115. lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n");
  116. deps = talloc_array(ctx, char *, talloc_array_length(lines));
  117. for (n = i = 0; lines[i]; i++) {
  118. char *str;
  119. unsigned int len;
  120. /* Ignore lines starting with # (e.g. #include) */
  121. if (lines[i][0] == '#')
  122. continue;
  123. if (strstr(lines[i], "\"testdepends\""))
  124. correct_style = streq(style, "testdepends");
  125. else if (strstr(lines[i], "\"depends\""))
  126. correct_style = streq(style, "depends");
  127. if (!correct_style)
  128. continue;
  129. /* Start of line, or after ". */
  130. if (strstarts(lines[i], "ccan/"))
  131. str = lines[i];
  132. else {
  133. str = strstr(lines[i], "\"ccan/");
  134. if (!str)
  135. continue;
  136. str++;
  137. }
  138. len = strspn(str, "/abcdefghijklmnopqrstuvxwyz12345678980_");
  139. if (len == 5)
  140. continue;
  141. deps[n++] = talloc_strndup(deps, str, len);
  142. }
  143. deps[n] = NULL;
  144. talloc_free(fname);
  145. /* Make sure talloc_array_length() works */
  146. return talloc_realloc(NULL, deps, char *, n + 1);
  147. }
  148. static bool have_dep(char **deps, const char *dep)
  149. {
  150. unsigned int i;
  151. for (i = 0; deps[i]; i++)
  152. if (streq(deps[i], dep))
  153. return true;
  154. return false;
  155. }
  156. /* Gets all the dependencies, recursively. */
  157. static char **
  158. get_all_deps(const void *ctx, const char *dir, const char *style,
  159. char *(*get_info)(const void *ctx, const char *dir),
  160. char **(*get_one)(const void *, const char *, const char *,
  161. char *(*get_info)(const void *, const char *)))
  162. {
  163. char **deps;
  164. unsigned int i;
  165. deps = get_one(ctx, dir, style, get_info);
  166. for (i = 0; i < talloc_array_length(deps)-1; i++) {
  167. char **newdeps;
  168. unsigned int j;
  169. char *subdir;
  170. if (!strstarts(deps[i], "ccan/"))
  171. continue;
  172. subdir = talloc_asprintf(ctx, "%s/%s",
  173. find_ccan_dir(dir), deps[i]);
  174. newdeps = get_one(ctx, subdir, "depends", get_info);
  175. /* Should be short, so brute-force out dups. */
  176. for (j = 0; j < talloc_array_length(newdeps)-1; j++) {
  177. unsigned int num;
  178. if (have_dep(deps, newdeps[j]))
  179. continue;
  180. num = talloc_array_length(deps)-1;
  181. deps = talloc_realloc(NULL, deps, char *, num + 2);
  182. deps[num] = newdeps[j];
  183. deps[num+1] = NULL;
  184. }
  185. }
  186. return deps;
  187. }
  188. /* Can return NULL: _info may not support 'libs'. */
  189. static char **get_one_libs(const void *ctx, const char *dir,
  190. char *(*get_info)(const void *ctx, const char *dir))
  191. {
  192. char *cmd, **lines;
  193. cmd = talloc_asprintf(ctx, "%s libs", get_info(ctx, dir));
  194. lines = lines_from_cmd(cmd, "%s", cmd);
  195. /* Strip final NULL. */
  196. if (lines)
  197. lines = talloc_realloc(NULL, lines, char *,
  198. talloc_array_length(lines)-1);
  199. return lines;
  200. }
  201. /* O(n^2) but n is small. */
  202. static char **add_deps(char **deps1, char **deps2)
  203. {
  204. unsigned int i, len;
  205. len = talloc_array_length(deps1);
  206. for (i = 0; deps2[i]; i++) {
  207. if (have_dep(deps1, deps2[i]))
  208. continue;
  209. deps1 = talloc_realloc(NULL, deps1, char *, len + 1);
  210. deps1[len-1] = talloc_steal(deps1, deps2[i]);
  211. deps1[len++] = NULL;
  212. }
  213. return deps1;
  214. }
  215. char **get_libs(const void *ctx, const char *dir, const char *style,
  216. char *(*get_info)(const void *ctx, const char *dir))
  217. {
  218. char **deps, **libs;
  219. unsigned int i, len;
  220. libs = get_one_libs(ctx, dir, get_info);
  221. len = talloc_array_length(libs);
  222. if (style) {
  223. deps = get_deps(ctx, dir, style, true, get_info);
  224. if (streq(style, "testdepends"))
  225. deps = add_deps(deps,
  226. get_deps(ctx, dir, "depends", true,
  227. get_info));
  228. for (i = 0; deps[i]; i++) {
  229. char **newlibs, *subdir;
  230. size_t newlen;
  231. if (!strstarts(deps[i], "ccan/"))
  232. continue;
  233. subdir = talloc_asprintf(ctx, "%s/%s",
  234. find_ccan_dir(dir), deps[i]);
  235. newlibs = get_one_libs(ctx, subdir, get_info);
  236. newlen = talloc_array_length(newlibs);
  237. libs = talloc_realloc(ctx, libs, char *, len + newlen);
  238. memcpy(&libs[len], newlibs,
  239. sizeof(newlibs[0])*newlen);
  240. len += newlen;
  241. }
  242. }
  243. /* Append NULL entry. */
  244. libs = talloc_realloc(ctx, libs, char *, len + 1);
  245. libs[len] = NULL;
  246. return libs;
  247. }
  248. /* FIXME: This is O(n^2), which is dumb. */
  249. static char **uniquify_deps(char **deps)
  250. {
  251. unsigned int i, j, num;
  252. if (!deps)
  253. return NULL;
  254. num = talloc_array_length(deps) - 1;
  255. for (i = 0; i < num; i++) {
  256. for (j = i + 1; j < num; j++) {
  257. if (streq(deps[i], deps[j])) {
  258. memmove(&deps[j], &deps[j+1],
  259. (num - j - 1) * sizeof(char *));
  260. num--;
  261. }
  262. }
  263. }
  264. deps[num] = NULL;
  265. /* Make sure talloc_array_length() works */
  266. return talloc_realloc(NULL, deps, char *, num + 1);
  267. }
  268. char **get_deps(const void *ctx, const char *dir, const char *style,
  269. bool recurse,
  270. char *(*get_info)(const void *ctx, const char *dir))
  271. {
  272. char **ret;
  273. if (!recurse) {
  274. ret = get_one_deps(ctx, dir, style, get_info);
  275. } else
  276. ret = get_all_deps(ctx, dir, style, get_info, get_one_deps);
  277. return uniquify_deps(ret);
  278. }
  279. char **get_safe_ccan_deps(const void *ctx, const char *dir, const char *style,
  280. bool recurse)
  281. {
  282. char **ret;
  283. if (!recurse) {
  284. ret = get_one_safe_deps(ctx, dir, style, NULL);
  285. } else {
  286. ret = get_all_deps(ctx, dir, style, NULL, get_one_safe_deps);
  287. }
  288. return uniquify_deps(ret);
  289. }