libpkgconf: add path matching and environment building functions

pull/100/head
William Pitcock 2016-11-30 22:32:17 -06:00
parent f4da1082cb
commit 4bb46e20e3
4 changed files with 48 additions and 45 deletions

View File

@ -238,5 +238,7 @@ void pkgconf_audit_log_dependency(const pkgconf_pkg_t *dep, const pkgconf_depend
/* path.c */ /* path.c */
void pkgconf_path_add(const char *text, pkgconf_list_t *dirlist); void pkgconf_path_add(const char *text, pkgconf_list_t *dirlist);
size_t pkgconf_path_split(const char *text, pkgconf_list_t *dirlist); size_t pkgconf_path_split(const char *text, pkgconf_list_t *dirlist);
size_t pkgconf_path_build_from_environ(const char *environ, const char *fallback, pkgconf_list_t *dirlist);
bool pkgconf_path_match_list(const char *path, pkgconf_list_t *dirlist);
#endif #endif

View File

@ -47,3 +47,34 @@ pkgconf_path_split(const char *text, pkgconf_list_t *dirlist)
return count; return count;
} }
size_t
pkgconf_path_build_from_environ(const char *environ, const char *fallback, pkgconf_list_t *dirlist)
{
const char *data;
data = getenv(environ);
if (data != NULL)
return pkgconf_path_split(data, dirlist);
if (fallback != NULL)
return pkgconf_path_split(fallback, dirlist);
/* no fallback and no environment variable, thusly no nodes added */
return 0;
}
bool
pkgconf_path_match_list(const char *path, pkgconf_list_t *dirlist)
{
pkgconf_node_t *n = NULL;
PKGCONF_FOREACH_LIST_ENTRY(dirlist->head, n)
{
pkgconf_path_t *pnode = n->data;
if (!strcmp(pnode->path, path))
return true;
}
return false;
}

View File

@ -66,20 +66,13 @@ str_has_suffix(const char *str, const char *suffix)
} }
static inline const char * static inline const char *
get_pkgconfig_path(void) get_default_pkgconfig_path(void)
{ {
const char *env_path;
#ifdef _WIN32 #ifdef _WIN32
static char outbuf[MAX_PATH]; static char outbuf[MAX_PATH];
char namebuf[MAX_PATH]; char namebuf[MAX_PATH];
char *p; char *p;
#endif
env_path = getenv("PKG_CONFIG_LIBDIR");
if (env_path != NULL)
return env_path;
#ifdef _WIN32
int sizepath = GetModuleFileName(NULL, namebuf, sizeof namebuf); int sizepath = GetModuleFileName(NULL, namebuf, sizeof namebuf);
char * winslash; char * winslash;
namebuf[sizepath] = '\0'; namebuf[sizepath] = '\0';
@ -127,21 +120,13 @@ static pkgconf_list_t pkg_dir_list = PKGCONF_LIST_INITIALIZER;
static void static void
pkgconf_pkg_dir_list_build(unsigned int flags) pkgconf_pkg_dir_list_build(unsigned int flags)
{ {
const char *env_path;
if (pkg_dir_list.head != NULL || pkg_dir_list.tail != NULL) if (pkg_dir_list.head != NULL || pkg_dir_list.tail != NULL)
return; return;
/* PKG_CONFIG_PATH has to take precedence */ pkgconf_path_build_from_environ("PKG_CONFIG_PATH", NULL, &pkg_dir_list);
env_path = getenv("PKG_CONFIG_PATH");
if (env_path)
pkgconf_path_split(env_path, &pkg_dir_list);
if (!(flags & PKGCONF_PKG_PKGF_ENV_ONLY)) if (!(flags & PKGCONF_PKG_PKGF_ENV_ONLY))
{ pkgconf_path_build_from_environ("PKG_CONFIG_LIBDIR", get_default_pkgconfig_path(), &pkg_dir_list);
env_path = get_pkgconfig_path();
pkgconf_path_split(env_path, &pkg_dir_list);
}
} }
typedef void (*pkgconf_pkg_parser_keyword_func_t)(pkgconf_pkg_t *pkg, const ptrdiff_t offset, char *value, unsigned int flags); typedef void (*pkgconf_pkg_parser_keyword_func_t)(pkgconf_pkg_t *pkg, const ptrdiff_t offset, char *value, unsigned int flags);

39
main.c
View File

@ -52,6 +52,9 @@
static unsigned int global_traverse_flags = PKGCONF_PKG_PKGF_NONE; static unsigned int global_traverse_flags = PKGCONF_PKG_PKGF_NONE;
static pkgconf_list_t filter_libdirs = PKGCONF_LIST_INITIALIZER;
static pkgconf_list_t filter_includedirs = PKGCONF_LIST_INITIALIZER;
static uint64_t want_flags; static uint64_t want_flags;
static int maximum_traverse_depth = 2000; static int maximum_traverse_depth = 2000;
@ -68,51 +71,30 @@ error_handler(const char *msg)
return true; return true;
} }
static char *
fallback_getenv(const char *envname, const char *fallback)
{
const char *data = getenv(envname);
if (data == NULL)
data = fallback;
return strdup(data);
}
static bool static bool
fragment_has_system_dir(pkgconf_fragment_t *frag) fragment_has_system_dir(pkgconf_fragment_t *frag)
{ {
int check_flags = 0; int check_flags = 0;
char *check_paths = NULL; pkgconf_list_t *check_paths = NULL;
char *save, *chunk;
bool ret = false;
switch (frag->type) switch (frag->type)
{ {
case 'L': case 'L':
check_flags = PKG_KEEP_SYSTEM_LIBS; check_flags = PKG_KEEP_SYSTEM_LIBS;
check_paths = fallback_getenv("PKG_CONFIG_SYSTEM_LIBRARY_PATH", SYSTEM_LIBDIR); check_paths = &filter_libdirs;
break; break;
case 'I': case 'I':
check_flags = PKG_KEEP_SYSTEM_CFLAGS; check_flags = PKG_KEEP_SYSTEM_CFLAGS;
check_paths = fallback_getenv("PKG_CONFIG_SYSTEM_INCLUDE_PATH", SYSTEM_INCLUDEDIR); check_paths = &filter_includedirs;
break; break;
default: default:
return false; return false;
} }
for (chunk = strtok_r(check_paths, ":", &save); chunk != NULL; chunk = strtok_r(NULL, ":", &save)) if ((want_flags & check_flags) == 0)
{ return false;
if ((want_flags & check_flags) == 0 && !strcmp(chunk, frag->data))
{
ret = true;
break;
}
}
free(check_paths); return pkgconf_path_match_list(frag->data, check_paths);
return ret;
} }
static void static void
@ -708,6 +690,9 @@ main(int argc, char *argv[])
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
}; };
pkgconf_path_build_from_environ("PKG_CONFIG_SYSTEM_LIBRARY_PATH", SYSTEM_LIBDIR, &filter_libdirs);
pkgconf_path_build_from_environ("PKG_CONFIG_SYSTEM_INCLUDE_PATH", SYSTEM_INCLUDEDIR, &filter_includedirs);
while ((ret = pkg_getopt_long_only(argc, argv, "", options, NULL)) != -1) while ((ret = pkg_getopt_long_only(argc, argv, "", options, NULL)) != -1)
{ {
switch (ret) switch (ret)