Commit 37ca11df authored by Rusty Russell's avatar Rusty Russell

Proper recursive dependencies (came from libantithread work)

parent 0953f929
_info _info
.depends .depends
*~ *~
tools/ccan_depends
tools/doc_extract tools/doc_extract
tools/namespacize tools/namespacize
tools/run_tests tools/run_tests
tools/ccanlint/ccanlint tools/ccanlint/ccanlint
tools/ccanlint/generated-init-tests tools/ccanlint/generated-init-tests
inter-depends
test-depends
lib-depends
# Hacky makefile to compile everything and run the tests in some kind of sane order. # Hacky makefile to compile everything and run the tests in some kind of sane order.
# V=--verbose for verbose tests. # V=--verbose for verbose tests.
CFLAGS=-O3 -Wall -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Werror -Iccan -I. CFLAGS=-g -Wall -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Werror -Iccan -I.
ALL=$(patsubst ccan/%/test, ccan/%, $(wildcard ccan/*/test)) ALL=$(patsubst ccan/%/test, %, $(wildcard ccan/*/test))
ALL_DEPENDS=$(patsubst %, %/.depends, $(ALL)) ALL_DIRS=$(patsubst %, ccan/%, $(ALL))
ALL_DEPENDS=$(patsubst %, ccan/%/.depends, $(ALL))
ALL_LIBS=$(patsubst %, ccan/lib%.a, $(ALL))
test-all: $(ALL_DEPENDS) test: $(ALL_DIRS:%=test-%)
$(MAKE) `for f in $(ALL); do echo test-$$f test-$$f; while read d; do echo test-$$d test-$$f; done < $$f/.depends; done | tsort`
distclean: clean distclean: clean
rm -f */_info rm -f */_info
rm -f $(ALL_DEPENDS) rm -f $(ALL_DEPENDS)
$(ALL_DEPENDS): %/.depends: %/_info $(ALL_DEPENDS): $(ALL_DIRS:=/_info)
@$< depends > $@ || ( rm -f $@; exit 1 )
test-ccan/%: tools/run_tests $(ALL_DEPENDS): %/.depends: tools/ccan_depends
tools/ccan_depends $* > $@ || ( rm -f $@; exit 1 )
foo:
echo $(ALL_LIBS)
$(ALL_LIBS):
$(AR) r $@ $^
test-ccan/%: tools/run_tests ccan/lib%.a
@echo Testing $*... @echo Testing $*...
@if tools/run_tests $(V) ccan/$* | grep ^'not ok'; then exit 1; else exit 0; fi @if tools/run_tests $(V) ccan/$* | grep ^'not ok'; then exit 1; else exit 0; fi
ccanlint: tools/ccanlint/ccanlint ccanlint: tools/ccanlint/ccanlint
clean: tools-clean clean: tools-clean
rm -f `find . -name '*.o'` `find . -name '.depends'` $(RM) `find . -name '*.o'` `find . -name '.depends'` `find . -name '*.a'`
$(RM) inter-depends lib-depends test-depends
inter-depends: $(ALL_DEPENDS)
for f in $(ALL_DEPENDS); do echo test-ccan/`basename \`dirname $$f\``: `sed -n 's,ccan/\(.*\),ccan/lib\1.a,p' < $$f`; done > $@
test-depends: $(ALL_DEPENDS)
for f in $(ALL_DEPENDS); do echo test-ccan/`basename \`dirname $$f\``: `sed -n 's,ccan/\(.*\),test-ccan/\1,p' < $$f`; done > $@
lib-depends: $(foreach D,$(ALL),$(wildcard $D/*.[ch]))
for c in $(ALL); do echo ccan/lib$$c.a: `ls ccan/$$c/*.c | grep -v /_info.c | sed 's/.c$$/.o/'`; done > $@
include tools/Makefile include tools/Makefile
-include inter-depends
-include test-depends
-include lib-depends
tools/ccan_depends: tools/ccan_depends.o tools/depends.o tools/split.o tools/grab_file.o ccan/talloc/talloc.o
tools/run_tests: tools/run_tests.o tools/depends.o tools/split.o tools/grab_file.o ccan/tap/tap.o ccan/talloc/talloc.o tools/run_tests: tools/run_tests.o tools/depends.o tools/split.o tools/grab_file.o ccan/tap/tap.o ccan/talloc/talloc.o
tools/doc_extract: tools/doc_extract.c ccan/talloc/talloc.o tools/doc_extract: tools/doc_extract.c ccan/talloc/talloc.o
......
#include "tools.h"
#include <err.h>
#include <stdlib.h>
#include <stdio.h>
#include "string/string.h"
int main(int argc, char *argv[])
{
char **deps;
unsigned int i;
if (argc != 2)
errx(1, "Usage: ccan_depends <dir>\n"
"Spits out all the ccan dependencies (recursively)");
deps = get_deps(NULL, argv[1]);
for (i = 0; deps[i]; i++)
if (strstarts(deps[i], "ccan/"))
printf("%s\n", deps[i]);
return 0;
}
#include "talloc/talloc.h" #include "talloc/talloc.h"
#include "string/string.h"
#include "tools.h" #include "tools.h"
#include <err.h> #include <err.h>
#include <stdbool.h>
static char ** __attribute__((format(printf, 2, 3))) static char ** __attribute__((format(printf, 3, 4)))
lines_from_cmd(const void *ctx, char *format, ...) lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...)
{ {
va_list ap; va_list ap;
char *cmd, *buffer; char *cmd, *buffer;
...@@ -22,30 +24,55 @@ lines_from_cmd(const void *ctx, char *format, ...) ...@@ -22,30 +24,55 @@ lines_from_cmd(const void *ctx, char *format, ...)
err(1, "Reading from '%s'", cmd); err(1, "Reading from '%s'", cmd);
pclose(p); pclose(p);
return split(ctx, buffer, "\n", NULL); return split(ctx, buffer, "\n", num);
} }
static char *build_info(const void *ctx, const char *dir) static char **get_one_deps(const void *ctx, const char *dir, unsigned int *num)
{ {
char *file, *cfile, *cmd; char **deps, *cmd;
cmd = talloc_asprintf(ctx, "%s/_info depends", dir);
deps = lines_from_cmd(cmd, num, "%s", cmd);
if (!deps)
err(1, "Could not run '%s'", cmd);
return deps;
}
cfile = talloc_asprintf(ctx, "%s/%s", dir, "_info.c"); static bool have_dep(char **deps, unsigned int num, const char *dep)
file = talloc_asprintf(cfile, "%s/%s", dir, "_info"); {
cmd = talloc_asprintf(file, "gcc " CFLAGS " -o %s %s", file, cfile); unsigned int i;
if (system(cmd) != 0)
errx(1, "Failed to compile %s", file);
return file; for (i = 0; i < num; i++)
if (streq(deps[i], dep))
return true;
return false;
} }
/* Gets all the dependencies, recursively. */
char **get_deps(const void *ctx, const char *dir) char **get_deps(const void *ctx, const char *dir)
{ {
char **deps, *cmd; char **deps;
unsigned int i, num;
cmd = talloc_asprintf(ctx, "%s depends", build_info(ctx, dir)); deps = get_one_deps(ctx, dir, &num);
deps = lines_from_cmd(cmd, cmd); for (i = 0; i < num; i++) {
if (!deps) char **newdeps;
err(1, "Could not run '%s'", cmd); unsigned int j, newnum;
if (!strstarts(deps[i], "ccan/"))
continue;
newdeps = get_one_deps(ctx, deps[i], &newnum);
/* Should be short, so brute-force out dups. */
for (j = 0; j < newnum; j++) {
if (have_dep(deps, num, newdeps[j]))
continue;
deps = talloc_realloc(NULL, deps, char *, num + 2);
deps[num++] = newdeps[j];
deps[num] = NULL;
}
}
return deps; return deps;
} }
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include "string/string.h"
#include "talloc/talloc.h" #include "talloc/talloc.h"
#include "tools.h" #include "tools.h"
......
...@@ -19,7 +19,7 @@ static int verbose; ...@@ -19,7 +19,7 @@ static int verbose;
struct test_type struct test_type
{ {
const char *name; const char *name;
void (*testfn)(struct test_type *t, const char *name); void (*testfn)(const char *dir, struct test_type *t, const char *name);
}; };
struct test struct test
...@@ -98,14 +98,27 @@ static void add_obj(const char *testdir, const char *name) ...@@ -98,14 +98,27 @@ static void add_obj(const char *testdir, const char *name)
objs = obj; objs = obj;
} }
static int build(const char *name, int fail) static int build(const char *dir, const char *name, int fail)
{ {
const char *cmd; const char *cmd;
int ret; int ret;
char *externals = talloc_strdup(name, "");
char **deps;
cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s%s", for (deps = get_deps(objs, dir); *deps; deps++) {
char *end;
if (!strstarts(*deps, "ccan/"))
continue;
end = strrchr(*deps, '/') + 1;
/* ccan/foo -> ccan/libfoo.a */
externals = talloc_asprintf_append(externals,
" ccan/lib%s.a", end);
}
cmd = talloc_asprintf(name, "gcc " CFLAGS " %s -o %s %s %s%s%s",
fail ? "-DFAIL" : "", fail ? "-DFAIL" : "",
output_name(name), name, obj_list(), output_name(name), name, obj_list(), externals,
verbose ? "" : "> /dev/null 2>&1"); verbose ? "" : "> /dev/null 2>&1");
if (verbose) if (verbose)
...@@ -118,17 +131,17 @@ static int build(const char *name, int fail) ...@@ -118,17 +131,17 @@ static int build(const char *name, int fail)
return ret; return ret;
} }
static void compile_ok(struct test_type *t, const char *name) static void compile_ok(const char *dir, struct test_type *t, const char *name)
{ {
ok(build(name, 0) == 0, "%s %s", t->name, name); ok(build(dir, name, 0) == 0, "%s %s", t->name, name);
} }
static void compile_fail(struct test_type *t, const char *name) static void compile_fail(const char *dir, struct test_type *t, const char *name)
{ {
if (build(name, 0) != 0) if (build(dir, name, 0) != 0)
fail("non-FAIL build %s", name); fail("non-FAIL build %s", name);
else else
ok(build(name, 1) > 0, "%s %s", t->name, name); ok(build(dir, name, 1) > 0, "%s %s", t->name, name);
} }
static void run(const char *name) static void run(const char *name)
...@@ -188,13 +201,13 @@ int main(int argc, char *argv[]) ...@@ -188,13 +201,13 @@ int main(int argc, char *argv[])
} }
} }
plan_tests(num_tests + num_objs); plan_tests(num_tests + num_objs + (num_objs ? 1 : 0));
/* First all the extra object compilations. */ /* First all the extra object compilations. */
compile_objs(); compile_objs();
/* Do all the test compilations. */ /* Do all the test compilations. */
for (test = tests; test; test = test->next) for (test = tests; test; test = test->next)
test->type->testfn(test->type, test->name); test->type->testfn(argv[1], test->type, test->name);
cleanup_objs(); cleanup_objs();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment