]> git.ozlabs.org Git - ccan/blobdiff - tools/depends.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / tools / depends.c
index 399b0aadf5affee6b8b814d2facc118a40150bf0..e8e0f138dc0900e2349ccf14580bd25fdd2b8b95 100644 (file)
@@ -2,6 +2,7 @@
 #include <ccan/read_write_all/read_write_all.h>
 #include <ccan/rbuf/rbuf.h>
 #include <ccan/tal/path/path.h>
+#include <ccan/tal/grab_file/grab_file.h>
 #include <ccan/compiler/compiler.h>
 #include <ccan/err/err.h>
 #include "tools.h"
@@ -19,6 +20,7 @@ lines_from_cmd(const void *ctx, const char *format, ...)
        char *cmd;
        FILE *p;
        struct rbuf in;
+       char *ret;
 
        va_start(ap, format);
        cmd = tal_vfmt(ctx, format, ap);
@@ -29,12 +31,14 @@ lines_from_cmd(const void *ctx, const char *format, ...)
                err(1, "Executing '%s'", cmd);
 
        /* FIXME: Use rbuf_read_str(&in, '\n') rather than strsplit! */
-       rbuf_init(&in, fileno(p), tal_arr(ctx, char, 0), 0);
-       if (!rbuf_read_str(&in, 0, do_tal_realloc) && errno)
+       rbuf_init(&in, fileno(p), tal_arr(ctx, char, 0), 0,
+                 tal_rbuf_enlarge);
+       ret = rbuf_read_str(&in, 0);
+       if (!ret && errno)
                err(1, "Reading from '%s'", cmd);
        pclose(p);
 
-       return tal_strsplit(ctx, in.buf, "\n", STR_EMPTY_OK);
+       return tal_strsplit(ctx, ret, "\n", STR_EMPTY_OK);
 }
 
 /* Be careful about trying to compile over running programs (parallel make).
@@ -42,11 +46,10 @@ lines_from_cmd(const void *ctx, const char *format, ...)
 char *compile_info(const void *ctx, const char *dir)
 {
        char *info_c_file, *info, *compiled, *output;
-       size_t len;
        int fd;
 
        /* Copy it to a file with proper .c suffix. */
-       info = tal_grab_file(ctx, tal_fmt(ctx, "%s/_info", dir), &len);
+       info = grab_file(ctx, tal_fmt(ctx, "%s/_info", dir));
        if (!info)
                return NULL;
 
@@ -54,8 +57,10 @@ char *compile_info(const void *ctx, const char *dir)
        fd = open(info_c_file, O_WRONLY|O_CREAT|O_EXCL, 0600);
        if (fd < 0)
                return NULL;
-       if (!write_all(fd, info, len))
+       if (!write_all(fd, info, tal_count(info)-1)) {
+               close(fd);
                return NULL;
+       }
 
        if (close(fd) != 0)
                return NULL;
@@ -126,7 +131,7 @@ static char **get_one_safe_deps(const void *ctx,
        bool correct_style = false;
 
        fname = path_join(ctx, dir, "_info");
-       raw = tal_grab_file(fname, fname, NULL);
+       raw = grab_file(fname, fname);
        if (!raw)
                errx(1, "Could not open %s", fname);
 
@@ -198,6 +203,8 @@ get_all_deps(const void *ctx, const char *dir, const char *style,
        unsigned int i;
 
        deps = get_one(ctx, dir, style, get_info);
+       if (!deps)
+               return NULL;
        for (i = 0; i < tal_count(deps)-1; i++) {
                char **newdeps;
                unsigned int j;
@@ -225,13 +232,13 @@ get_all_deps(const void *ctx, const char *dir, const char *style,
        return deps;
 }
 
-/* Can return NULL: _info may not support 'libs'. */
-static char **get_one_libs(const void *ctx, const char *dir,
+/* Can return NULL: _info may not support prop. */
+static char **get_one_prop(const void *ctx, const char *dir, const char *prop,
                           char *(*get_info)(const void *ctx, const char *dir))
 {
        char *cmd, **lines;
 
-       cmd = tal_fmt(ctx, "%s libs", get_info(ctx, dir));
+       cmd = tal_fmt(ctx, "%s %s", get_info(ctx, dir), prop);
        lines = lines_from_cmd(cmd, "%s", cmd);
        /* Strip final NULL. */
        if (lines)
@@ -239,6 +246,24 @@ static char **get_one_libs(const void *ctx, const char *dir,
        return lines;
 }
 
+static char **get_one_libs(const void *ctx, const char *dir,
+                          char *(*get_info)(const void *ctx, const char *dir))
+{
+       return get_one_prop(ctx, dir, "libs", get_info);
+}
+
+static char **get_one_cflags(const void *ctx, const char *dir,
+                          char *(*get_info)(const void *ctx, const char *dir))
+{
+       return get_one_prop(ctx, dir, "cflags", get_info);
+}
+
+static char **get_one_ccanlint(const void *ctx, const char *dir,
+                              char *(*get_info)(const void *ctx, const char *dir))
+{
+       return get_one_prop(ctx, dir, "ccanlint", get_info);
+}
+
 /* O(n^2) but n is small. */
 static char **add_deps(char **deps1, char **deps2)
 {
@@ -256,6 +281,75 @@ static char **add_deps(char **deps1, char **deps2)
        return deps1;
 }
 
+char **get_cflags(const void *ctx, const char *dir,
+        char *(*get_info)(const void *ctx, const char *dir))
+{
+       char **flags;
+       unsigned int len;
+       flags = get_one_cflags(ctx, dir, get_info);
+       len = tal_count(flags);
+       tal_resize(&flags, len + 1);
+       flags[len] = NULL;
+       return flags;
+}
+
+char **get_ccanlint(const void *ctx, const char *dir,
+                   char *(*get_info)(const void *ctx, const char *dir))
+{
+       char **ccanlint;
+       unsigned int len;
+       ccanlint = get_one_ccanlint(ctx, dir, get_info);
+       len = tal_count(ccanlint);
+       tal_resize(&ccanlint, len + 1);
+       ccanlint[len] = NULL;
+       return ccanlint;
+}
+
+static char *get_one_ported(const void *ctx, const char *dir,
+                           char *(*get_info)(const void *ctx, const char *dir))
+{
+       char **ported = get_one_prop(ctx, dir, "ported", get_info);
+
+       /* No news is good news. */
+       if (tal_count(ported) == 0)
+               return NULL;
+
+       if (tal_count(ported) != 1)
+               errx(1, "%s/_info ported gave %zu lines, not one",
+                    dir, tal_count(ported));
+
+       if (streq(ported[0], ""))
+               return NULL;
+       else
+               return ported[0];
+}
+
+char *get_ported(const void *ctx, const char *dir, bool recurse,
+               char *(*get_info)(const void *ctx, const char *dir))
+{
+       char *msg;
+
+       msg = get_one_ported(ctx, dir, get_info);
+       if (msg)
+               return msg;
+
+       if (recurse) {
+               size_t i;
+               char **deps = get_deps(ctx, dir, "depends", true, get_info);
+               for (i = 0; deps[i]; i++) {
+                       char *subdir;
+                       if (!strstarts(deps[i], "ccan/"))
+                               continue;
+
+                       subdir = path_join(ctx, find_ccan_dir(dir), deps[i]);
+                       msg = get_one_ported(ctx, subdir, get_info);
+                       if (msg)
+                               return msg;
+               }
+       }
+       return NULL;
+}
+
 char **get_libs(const void *ctx, const char *dir, const char *style,
                char *(*get_info)(const void *ctx, const char *dir))
 {