pkg: add pkg_traverse() to walk the dependency graph

pull/1/merge
William Pitcock 2011-07-24 23:38:58 -05:00
parent f7e81402ce
commit b44ce20d29
2 changed files with 40 additions and 18 deletions

56
pkg.c
View File

@ -33,6 +33,42 @@ pkg_find(const char *name)
return parse_file(locbuf); return parse_file(locbuf);
} }
void
pkg_traverse(pkg_t *root,
void (*pkg_traverse_func)(pkg_t *package, void *data),
void *data)
{
pkg_dependency_t *node;
foreach_list_entry(root->requires, node)
{
pkg_t *pkgdep;
pkgdep = pkg_find(node->package);
if (pkgdep == NULL)
{
fprintf(stderr, "dependency '%s' is not satisfiable, see PKG_CONFIG_PATH\n", node->package);
continue;
}
pkg_traverse(pkgdep, pkg_traverse_func, data);
}
pkg_traverse_func(root, data);
}
void
print_cflags(pkg_t *pkg, void *unused)
{
printf("%s ", pkg->cflags);
}
void
print_libs(pkg_t *pkg, void *unused)
{
printf("%s ", pkg->libs);
}
int main(int argc, const char *argv[]) int main(int argc, const char *argv[])
{ {
pkg_t *pkg; pkg_t *pkg;
@ -40,23 +76,9 @@ int main(int argc, const char *argv[])
pkg = pkg_find(argv[1]); pkg = pkg_find(argv[1]);
if (pkg) if (pkg)
{ {
pkg_dependency_t *node; pkg_traverse(pkg, print_cflags, NULL);
pkg_traverse(pkg, print_libs, NULL);
foreach_list_entry(pkg->requires, node) printf("\n");
{
pkg_t *pkgdep;
pkgdep = pkg_find(node->package);
if (pkgdep == NULL)
{
printf("dependency '%s' not satisfied\n", node->package);
return -1;
}
printf("%s: {%s} {%s}\n", pkgdep->realname, pkgdep->cflags, pkgdep->libs);
}
printf("%s: {%s} {%s}\n", pkg->realname, pkg->cflags, pkg->libs);
} }
else else
{ {

2
pkg.h
View File

@ -85,7 +85,7 @@ struct pkg_ {
}; };
pkg_t *pkg_find(const char *name); pkg_t *pkg_find(const char *name);
pkg_t *pkg_find_matching_dep(pkg_dependency_t *dep); void pkg_traverse(pkg_t *root, void (*pkg_traverse_func)(pkg_t *package, void *data), void *data);
/* parse.c */ /* parse.c */
pkg_t *parse_file(const char *path); pkg_t *parse_file(const char *path);