#include <ccan/grab_file/grab_file.h>
#include <ccan/str_talloc/str_talloc.h>
#include <ccan/read_write_all/read_write_all.h>
+#include <ccan/compiler/compiler.h>
#include "tools.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
-static char ** __attribute__((format(printf, 2, 3)))
+static char ** PRINTF_FMT(2, 3)
lines_from_cmd(const void *ctx, const char *format, ...)
{
va_list ap;
/* Be careful about trying to compile over running programs (parallel make).
* temp_file helps here. */
-static char *compile_info(const void *ctx, const char *dir)
+char *compile_info(const void *ctx, const char *dir)
{
char *info_c_file, *info, *ccandir, *compiled, *output;
size_t len;
return NULL;
}
-static char **get_one_deps(const void *ctx, const char *dir, char **infofile)
+static char **get_one_deps(const void *ctx, const char *dir,
+ char *(*get_info)(const void *ctx, const char *dir))
{
char **deps, *cmd;
+ char *infofile = get_info(ctx, dir);
- if (!*infofile) {
- *infofile = compile_info(ctx, dir);
- if (!*infofile)
- errx(1, "Could not compile _info for '%s'", dir);
- }
+ if (!infofile)
+ return NULL;
- cmd = talloc_asprintf(ctx, "%s depends", *infofile);
+ cmd = talloc_asprintf(ctx, "%s depends", infofile);
deps = lines_from_cmd(cmd, "%s", cmd);
if (!deps)
err(1, "Could not run '%s'", cmd);
/* This is a terrible hack. We scan for ccan/ strings. */
static char **get_one_safe_deps(const void *ctx,
const char *dir,
- char **infofile)
+ char *(*unused)(const void *, const char *))
{
char **deps, **lines, *raw, *fname;
unsigned int i, n;
/* Gets all the dependencies, recursively. */
static char **
get_all_deps(const void *ctx, const char *dir,
- char **infofile,
- char **(*get_one)(const void *, const char *, char **))
+ char *(*get_info)(const void *ctx, const char *dir),
+ char **(*get_one)(const void *, const char *,
+ char *(*get_info)(const void *, const char *)))
{
char **deps;
unsigned int i;
- deps = get_one(ctx, dir, infofile);
+ deps = get_one(ctx, dir, get_info);
for (i = 0; i < talloc_array_length(deps)-1; i++) {
char **newdeps;
unsigned int j;
- char *subinfo = NULL;
char *subdir;
if (!strstarts(deps[i], "ccan/"))
subdir = talloc_asprintf(ctx, "%s/%s",
talloc_dirname(ctx, dir),
deps[i] + strlen("ccan/"));
- newdeps = get_one(ctx, subdir, &subinfo);
+ newdeps = get_one(ctx, subdir, get_info);
/* Should be short, so brute-force out dups. */
for (j = 0; j < talloc_array_length(newdeps)-1; j++) {
{
unsigned int i, j, num;
+ if (!deps)
+ return NULL;
+
num = talloc_array_length(deps) - 1;
for (i = 0; i < num; i++) {
for (j = i + 1; j < num; j++) {
return talloc_realloc(NULL, deps, char *, num + 1);
}
-char **get_deps(const void *ctx, const char *dir,
- bool recurse, char **infofile)
+char **get_deps(const void *ctx, const char *dir, bool recurse,
+ char *(*get_info)(const void *ctx, const char *dir))
{
- char *temp = NULL, **ret;
- if (!infofile)
- infofile = &temp;
+ char **ret;
if (!recurse) {
- ret = get_one_deps(ctx, dir, infofile);
+ ret = get_one_deps(ctx, dir, get_info);
} else
- ret = get_all_deps(ctx, dir, infofile, get_one_deps);
+ ret = get_all_deps(ctx, dir, get_info, get_one_deps);
- if (infofile == &temp && temp) {
- unlink(temp);
- talloc_free(temp);
- }
return uniquify_deps(ret);
}