depends.c 8.3 KB

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