pkg: handle PKG_DEFAULT_PATH and PKG_CONFIG_PATH envvar more cleanly

feature/tap-sh
William Pitcock 2012-02-17 15:35:23 -06:00
parent d0639b3353
commit 1e93bac383
2 changed files with 46 additions and 28 deletions

View File

@ -3,6 +3,7 @@ SRCS = main.c parse.c pkg.c
include buildsys.mk include buildsys.mk
CFLAGS += -DPKG_DEFAULT_PATH=\"${libdir}/pkgconfig\"
LIBS = -lpopt LIBS = -lpopt
include .deps include .deps

41
pkg.c
View File

@ -2,7 +2,7 @@
* pkg.c * pkg.c
* higher-level dependency graph compilation, management and manipulation * higher-level dependency graph compilation, management and manipulation
* *
* Copyright (c) 2011 William Pitcock <nenolod@dereferenced.org>. * Copyright (c) 2011, 2012 William Pitcock <nenolod@dereferenced.org>.
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -23,22 +23,41 @@
#include "pkg.h" #include "pkg.h"
#define PKG_CONFIG_EXT ".pc"
#define PKG_CONFIG_PATH_SZ (65535)
static inline const char *
pkg_get_pkgconfig_path(void)
{
static bool computed = false;
static char path[PKG_CONFIG_PATH_SZ];
char *env_path;
if (computed)
return path;
strncpy(path, PKG_DEFAULT_PATH, sizeof path);
env_path = getenv("PKG_CONFIG_PATH");
if (env_path != NULL)
{
strncat(path, ":", sizeof path);
strncat(path, env_path, sizeof path);
}
return path;
}
pkg_t * pkg_t *
pkg_find(const char *name) pkg_find(const char *name)
{ {
char locbuf[BUFSIZ]; char locbuf[BUFSIZ];
char path[BUFSIZ]; char path[BUFSIZ];
char *env_path; const char *env_path;
int count = 0, pcount = 0; int count = 0, pcount = 0;
FILE *f; FILE *f;
snprintf(locbuf, sizeof locbuf, "/usr/lib/pkgconfig/%s.pc", name); env_path = pkg_get_pkgconfig_path();
if (!(f = fopen(locbuf, "r")))
{
env_path = getenv("PKG_CONFIG_PATH");
if (env_path == NULL)
return NULL;
while (env_path[count] != '\0') while (env_path[count] != '\0')
{ {
if (env_path[count] != ':') if (env_path[count] != ':')
@ -46,8 +65,7 @@ pkg_find(const char *name)
path[pcount] = env_path[count]; path[pcount] = env_path[count];
pcount++; pcount++;
} }
else
if (env_path[count] == ':')
{ {
snprintf(locbuf, sizeof locbuf, "%s/%s.pc", path, name); snprintf(locbuf, sizeof locbuf, "%s/%s.pc", path, name);
if (f = fopen(locbuf, "r")) if (f = fopen(locbuf, "r"))
@ -64,7 +82,6 @@ pkg_find(const char *name)
snprintf(locbuf, sizeof locbuf, "%s/%s.pc", path, name); snprintf(locbuf, sizeof locbuf, "%s/%s.pc", path, name);
f = fopen(locbuf, "r"); f = fopen(locbuf, "r");
} }
}
return parse_file(locbuf, f); return parse_file(locbuf, f);
} }