tools: simplify get_safe_ccan_deps
[ccan] / tools / ccan_depends.c
1 #include "tools.h"
2 #include <err.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <ccan/str/str.h>
6 #include <ccan/talloc/talloc.h>
7
8 int main(int argc, char *argv[])
9 {
10         char **deps;
11         unsigned int i;
12         bool compile = false;
13         bool recurse = true;
14         bool ccan = true;
15
16         if (argv[1] && streq(argv[1], "--direct")) {
17                 argv++;
18                 argc--;
19                 recurse = false;
20         }
21         if (argv[1] && streq(argv[1], "--compile")) {
22                 argv++;
23                 argc--;
24                 compile = true;
25         }
26         if (argv[1] && streq(argv[1], "--non-ccan")) {
27                 argv++;
28                 argc--;
29                 ccan = false;
30         }
31         if (argc != 2)
32                 errx(1, "Usage: ccan_depends [--direct] [--compile] [--non-ccan] <dir>\n"
33                         "Spits out all the ccan dependencies (recursively unless --direct)");
34
35         /* We find depends without compiling by looking for ccan/ */
36         if (!ccan && !compile)
37                 errx(1, "--non-ccan needs --compile");
38
39         if (compile)
40                 deps = get_deps(talloc_autofree_context(), argv[1],
41                                 recurse, NULL);
42         else
43                 deps = get_safe_ccan_deps(talloc_autofree_context(),
44                                           argv[1], recurse);
45
46         for (i = 0; deps[i]; i++)
47                 if (strstarts(deps[i], "ccan/") == ccan)
48                         printf("%s\n", deps[i]);
49         return 0;
50 }