2011-07-25 02:03:17 +00:00
|
|
|
/*
|
|
|
|
* pkg.c
|
2011-07-25 04:46:10 +00:00
|
|
|
* higher-level dependency graph compilation, management and manipulation
|
2011-07-25 02:03:17 +00:00
|
|
|
*
|
2012-02-17 21:35:23 +00:00
|
|
|
* Copyright (c) 2011, 2012 William Pitcock <nenolod@dereferenced.org>.
|
2011-07-25 02:03:17 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pkg.h"
|
2012-04-30 06:06:13 +00:00
|
|
|
#include "bsdstubs.h"
|
2011-07-25 02:03:17 +00:00
|
|
|
|
2012-02-17 21:35:23 +00:00
|
|
|
#define PKG_CONFIG_EXT ".pc"
|
|
|
|
#define PKG_CONFIG_PATH_SZ (65535)
|
|
|
|
|
2012-05-02 09:24:58 +00:00
|
|
|
/* pkg-config uses ';' on win32 as ':' is part of path */
|
2012-05-02 19:07:38 +00:00
|
|
|
#ifdef _WIN32
|
2012-05-02 09:24:58 +00:00
|
|
|
#define PKG_CONFIG_PATH_SEP_S ";"
|
|
|
|
#else
|
|
|
|
#define PKG_CONFIG_PATH_SEP_S ":"
|
|
|
|
#endif
|
|
|
|
|
2012-05-02 19:06:32 +00:00
|
|
|
static inline size_t
|
|
|
|
path_split(const char *text, char ***parv)
|
2012-05-02 18:38:31 +00:00
|
|
|
{
|
2012-05-02 19:06:32 +00:00
|
|
|
size_t count = 0;
|
|
|
|
char *workbuf, *p, *iter;
|
2012-05-02 18:38:31 +00:00
|
|
|
|
|
|
|
if (text == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*parv = malloc(sizeof (void *));
|
|
|
|
|
2012-05-02 19:06:32 +00:00
|
|
|
iter = workbuf = strdup(text);
|
2012-05-02 19:07:38 +00:00
|
|
|
while ((p = strtok(iter, PKG_CONFIG_PATH_SEP_S)) != NULL)
|
2012-05-02 18:38:31 +00:00
|
|
|
{
|
2012-05-02 19:31:30 +00:00
|
|
|
(*parv)[count] = strdup(p);
|
2012-05-02 19:06:32 +00:00
|
|
|
count++, iter = NULL;
|
|
|
|
|
2012-05-02 18:38:31 +00:00
|
|
|
*parv = realloc(*parv, sizeof (void *) * count);
|
|
|
|
}
|
2012-05-02 19:06:32 +00:00
|
|
|
free(workbuf);
|
2012-05-02 18:38:31 +00:00
|
|
|
|
2012-05-02 19:06:32 +00:00
|
|
|
return count + 1;
|
2012-05-02 18:38:31 +00:00
|
|
|
}
|
|
|
|
|
2011-07-25 02:03:17 +00:00
|
|
|
pkg_t *
|
2012-05-02 23:14:53 +00:00
|
|
|
pkg_find(const char *name, unsigned int flags)
|
2011-07-25 02:03:17 +00:00
|
|
|
{
|
2012-05-02 18:38:31 +00:00
|
|
|
char locbuf[PKG_CONFIG_PATH_SZ];
|
2012-05-02 19:06:32 +00:00
|
|
|
char **path;
|
2012-05-03 17:15:45 +00:00
|
|
|
size_t count, iter = 0;
|
2012-02-17 21:35:23 +00:00
|
|
|
const char *env_path;
|
2012-01-16 01:03:54 +00:00
|
|
|
FILE *f;
|
2011-07-25 02:03:17 +00:00
|
|
|
|
2012-05-02 13:55:13 +00:00
|
|
|
/* PKG_CONFIG_PATH has to take precedence */
|
|
|
|
env_path = getenv("PKG_CONFIG_PATH");
|
|
|
|
if (env_path)
|
2012-01-16 01:03:54 +00:00
|
|
|
{
|
2012-05-02 19:06:32 +00:00
|
|
|
count = path_split(env_path, &path);
|
2012-05-01 01:27:19 +00:00
|
|
|
|
2012-05-02 19:06:32 +00:00
|
|
|
while (iter < count)
|
|
|
|
{
|
2012-05-02 19:11:20 +00:00
|
|
|
snprintf(locbuf, sizeof locbuf, "%s/%s" PKG_CONFIG_EXT, path[iter], name);
|
2012-05-02 19:35:45 +00:00
|
|
|
free(path[iter]);
|
|
|
|
|
2012-05-03 17:15:45 +00:00
|
|
|
if ((f = fopen(locbuf, "r")) != NULL)
|
2012-05-02 19:06:32 +00:00
|
|
|
return parse_file(locbuf, f);
|
2012-05-02 19:35:45 +00:00
|
|
|
|
|
|
|
iter++;
|
2012-05-02 13:55:13 +00:00
|
|
|
}
|
2012-05-02 19:35:45 +00:00
|
|
|
|
|
|
|
free(path);
|
2012-02-17 21:35:23 +00:00
|
|
|
}
|
|
|
|
|
2012-05-02 23:16:32 +00:00
|
|
|
if (!(flags & PKGF_ENV_ONLY))
|
|
|
|
{
|
|
|
|
snprintf(locbuf, sizeof locbuf, "%s/%s" PKG_CONFIG_EXT, PKG_DEFAULT_PATH, name);
|
2012-05-03 17:15:45 +00:00
|
|
|
if ((f = fopen(locbuf, "r")) != NULL)
|
2012-05-02 23:16:32 +00:00
|
|
|
return parse_file(locbuf, f);
|
|
|
|
}
|
2012-05-02 19:06:32 +00:00
|
|
|
|
2012-05-02 09:40:38 +00:00
|
|
|
return NULL;
|
2011-07-25 02:03:17 +00:00
|
|
|
}
|
|
|
|
|
2011-07-26 16:56:59 +00:00
|
|
|
/*
|
|
|
|
* pkg_compare_version(a, b)
|
|
|
|
*
|
|
|
|
* compare versions using RPM version comparison rules as described in the LSB.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pkg_compare_version(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
char oldch1, oldch2;
|
|
|
|
char *str1, *str2;
|
|
|
|
char *one, *two;
|
|
|
|
int ret;
|
|
|
|
bool isnum;
|
|
|
|
|
|
|
|
/* optimization: if version matches then it's the same version. */
|
|
|
|
if (!strcasecmp(a, b))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
str1 = LOCAL_COPY(a);
|
|
|
|
str2 = LOCAL_COPY(b);
|
|
|
|
|
|
|
|
one = str1;
|
|
|
|
two = str2;
|
|
|
|
|
|
|
|
while (*one && *two)
|
|
|
|
{
|
|
|
|
while (*one && !isalnum(*one))
|
|
|
|
one++;
|
|
|
|
while (*two && !isalnum(*two))
|
2011-07-26 17:05:14 +00:00
|
|
|
two++;
|
2011-07-26 16:56:59 +00:00
|
|
|
|
|
|
|
if (!(*one && *two))
|
|
|
|
break;
|
|
|
|
|
|
|
|
str1 = one;
|
|
|
|
str2 = two;
|
|
|
|
|
|
|
|
if (isdigit(*str1))
|
|
|
|
{
|
|
|
|
while (*str1 && isdigit(*str1))
|
|
|
|
str1++;
|
|
|
|
|
|
|
|
while (*str2 && isdigit(*str2))
|
|
|
|
str2++;
|
|
|
|
|
|
|
|
isnum = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (*str1 && isalpha(*str1))
|
|
|
|
str1++;
|
|
|
|
|
|
|
|
while (*str2 && isalpha(*str2))
|
|
|
|
str2++;
|
|
|
|
|
|
|
|
isnum = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
oldch1 = *str1;
|
|
|
|
oldch2 = *str2;
|
|
|
|
|
|
|
|
*str1 = '\0';
|
|
|
|
*str2 = '\0';
|
|
|
|
|
|
|
|
if (one == str1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (two == str2)
|
|
|
|
return (isnum ? 1 : -1);
|
|
|
|
|
|
|
|
if (isnum)
|
|
|
|
{
|
|
|
|
int onelen, twolen;
|
|
|
|
|
|
|
|
while (*one == '0')
|
|
|
|
one++;
|
|
|
|
|
|
|
|
while (*two == '0')
|
|
|
|
two++;
|
|
|
|
|
|
|
|
onelen = strlen(one);
|
|
|
|
twolen = strlen(two);
|
|
|
|
|
2011-07-26 17:05:14 +00:00
|
|
|
if (onelen > twolen)
|
2011-07-26 16:56:59 +00:00
|
|
|
return 1;
|
2011-07-26 17:05:14 +00:00
|
|
|
else if (twolen > onelen)
|
2011-07-26 16:56:59 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = strcmp(one, two);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
*str1 = oldch1;
|
|
|
|
*str2 = oldch2;
|
|
|
|
|
|
|
|
one = str1;
|
|
|
|
two = str2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!*one) && (!*two))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!*one)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-07-27 00:59:46 +00:00
|
|
|
/*
|
|
|
|
* pkg_get_comparator(pkgdep)
|
|
|
|
*
|
|
|
|
* returns the comparator used in a depgraph dependency node as a string.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
pkg_get_comparator(pkg_dependency_t *pkgdep)
|
|
|
|
{
|
|
|
|
switch(pkgdep->compare)
|
|
|
|
{
|
|
|
|
case PKG_LESS_THAN:
|
|
|
|
return "<";
|
|
|
|
case PKG_GREATER_THAN:
|
|
|
|
return ">";
|
|
|
|
case PKG_LESS_THAN_EQUAL:
|
|
|
|
return "<=";
|
|
|
|
case PKG_GREATER_THAN_EQUAL:
|
|
|
|
return ">=";
|
|
|
|
case PKG_EQUAL:
|
|
|
|
return "=";
|
|
|
|
case PKG_NOT_EQUAL:
|
|
|
|
return "!=";
|
|
|
|
case PKG_ANY:
|
|
|
|
return "(any)";
|
2012-05-03 17:15:45 +00:00
|
|
|
default:
|
|
|
|
return "???";
|
2011-07-27 00:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
|
2011-07-26 17:17:45 +00:00
|
|
|
/*
|
2012-05-02 23:14:53 +00:00
|
|
|
* pkg_verify_dependency(pkgdep, flags)
|
2011-07-26 17:17:45 +00:00
|
|
|
*
|
|
|
|
* verify a pkg_dependency_t node in the depgraph. if the dependency is solvable,
|
|
|
|
* return the appropriate pkg_t object, else NULL.
|
|
|
|
*/
|
|
|
|
pkg_t *
|
2012-05-02 23:14:53 +00:00
|
|
|
pkg_verify_dependency(pkg_dependency_t *pkgdep, unsigned int flags)
|
2011-07-26 17:17:45 +00:00
|
|
|
{
|
|
|
|
pkg_t *pkg;
|
|
|
|
|
2012-05-02 23:14:53 +00:00
|
|
|
pkg = pkg_find(pkgdep->package, flags);
|
2011-07-26 17:17:45 +00:00
|
|
|
if (pkg == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2011-07-27 01:37:01 +00:00
|
|
|
pkg->id = strdup(pkgdep->package);
|
|
|
|
|
2011-07-26 17:17:45 +00:00
|
|
|
switch(pkgdep->compare)
|
|
|
|
{
|
|
|
|
case PKG_LESS_THAN:
|
2011-07-27 00:52:44 +00:00
|
|
|
if (pkg_compare_version(pkg->version, pkgdep->version) < 0)
|
2011-07-26 17:17:45 +00:00
|
|
|
return pkg;
|
|
|
|
break;
|
|
|
|
case PKG_GREATER_THAN:
|
2011-07-27 00:52:44 +00:00
|
|
|
if (pkg_compare_version(pkg->version, pkgdep->version) > 0)
|
2011-07-26 17:17:45 +00:00
|
|
|
return pkg;
|
|
|
|
break;
|
|
|
|
case PKG_LESS_THAN_EQUAL:
|
2011-07-27 00:52:44 +00:00
|
|
|
if (pkg_compare_version(pkg->version, pkgdep->version) <= 0)
|
2011-07-26 17:17:45 +00:00
|
|
|
return pkg;
|
|
|
|
break;
|
|
|
|
case PKG_GREATER_THAN_EQUAL:
|
2011-07-27 00:52:44 +00:00
|
|
|
if (pkg_compare_version(pkg->version, pkgdep->version) >= 0)
|
2011-07-26 17:17:45 +00:00
|
|
|
return pkg;
|
|
|
|
break;
|
|
|
|
case PKG_EQUAL:
|
2011-07-27 00:52:44 +00:00
|
|
|
if (pkg_compare_version(pkg->version, pkgdep->version) == 0)
|
2011-07-26 17:17:45 +00:00
|
|
|
return pkg;
|
|
|
|
break;
|
|
|
|
case PKG_NOT_EQUAL:
|
2011-07-27 00:52:44 +00:00
|
|
|
if (pkg_compare_version(pkg->version, pkgdep->version) != 0)
|
2011-07-26 17:17:45 +00:00
|
|
|
return pkg;
|
|
|
|
break;
|
|
|
|
case PKG_ANY:
|
|
|
|
default:
|
|
|
|
return pkg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-25 22:44:05 +00:00
|
|
|
/*
|
|
|
|
* pkg_verify_graph(root, depth)
|
|
|
|
*
|
|
|
|
* verify the graph dependency nodes are satisfiable by walking the tree using
|
|
|
|
* pkg_traverse().
|
|
|
|
*/
|
|
|
|
void
|
2012-05-02 23:29:35 +00:00
|
|
|
pkg_verify_graph(pkg_t *root, int depth, unsigned int flags)
|
2011-07-25 22:44:05 +00:00
|
|
|
{
|
2012-05-02 23:29:35 +00:00
|
|
|
pkg_traverse(root, NULL, NULL, depth, flags);
|
2011-07-25 22:44:05 +00:00
|
|
|
}
|
|
|
|
|
2012-05-02 21:44:58 +00:00
|
|
|
static inline void
|
2012-05-03 17:20:39 +00:00
|
|
|
pkg_walk_list(pkg_dependency_t *deplist,
|
2011-07-25 04:38:58 +00:00
|
|
|
void (*pkg_traverse_func)(pkg_t *package, void *data),
|
2011-07-25 06:22:04 +00:00
|
|
|
void *data,
|
2012-05-02 21:44:58 +00:00
|
|
|
int maxdepth,
|
|
|
|
unsigned int flags)
|
2011-07-25 02:03:17 +00:00
|
|
|
{
|
2011-07-25 04:38:58 +00:00
|
|
|
pkg_dependency_t *node;
|
2011-07-25 02:03:17 +00:00
|
|
|
|
2012-05-02 21:44:58 +00:00
|
|
|
foreach_list_entry(deplist, node)
|
2011-07-25 04:20:22 +00:00
|
|
|
{
|
2011-07-25 04:38:58 +00:00
|
|
|
pkg_t *pkgdep;
|
2011-07-25 04:20:22 +00:00
|
|
|
|
2011-07-25 06:03:13 +00:00
|
|
|
if (*node->package == '\0')
|
|
|
|
continue;
|
|
|
|
|
2012-05-02 23:14:53 +00:00
|
|
|
pkgdep = pkg_verify_dependency(node, flags);
|
2012-05-02 21:44:58 +00:00
|
|
|
if (pkgdep == NULL)
|
2011-07-25 04:20:22 +00:00
|
|
|
{
|
2011-07-25 22:03:06 +00:00
|
|
|
fprintf(stderr, "Package %s was not found in the pkg-config search path.\n", node->package);
|
|
|
|
fprintf(stderr, "Perhaps you should add the directory containing `%s.pc'\n", node->package);
|
|
|
|
fprintf(stderr, "to the PKG_CONFIG_PATH environment variable\n");
|
|
|
|
fprintf(stderr, "No package '%s' found\n", node->package);
|
2011-07-25 06:26:05 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2011-07-25 04:38:58 +00:00
|
|
|
}
|
2011-07-25 04:20:22 +00:00
|
|
|
|
2012-05-02 21:44:58 +00:00
|
|
|
pkg_traverse(pkgdep, pkg_traverse_func, data, maxdepth - 1, flags);
|
2011-07-25 04:38:58 +00:00
|
|
|
}
|
2012-05-02 21:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pkg_traverse(root, pkg_traverse_func, data, maxdepth)
|
|
|
|
*
|
|
|
|
* walk the dependency graph up to maxdepth levels. -1 means infinite recursion.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
pkg_traverse(pkg_t *root,
|
|
|
|
void (*pkg_traverse_func)(pkg_t *package, void *data),
|
|
|
|
void *data,
|
|
|
|
int maxdepth,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
if (maxdepth == 0)
|
|
|
|
return;
|
|
|
|
|
2012-05-03 17:20:39 +00:00
|
|
|
pkg_walk_list(root->requires, pkg_traverse_func, data, maxdepth - 1, flags);
|
2011-07-25 04:20:22 +00:00
|
|
|
|
2012-05-02 21:47:02 +00:00
|
|
|
if (flags & PKGF_SEARCH_PRIVATE)
|
2012-05-03 17:20:39 +00:00
|
|
|
pkg_walk_list(root->requires_private, pkg_traverse_func, data, maxdepth - 1, flags);
|
2012-05-02 21:47:02 +00:00
|
|
|
|
2011-07-25 22:44:05 +00:00
|
|
|
if (pkg_traverse_func != NULL)
|
|
|
|
pkg_traverse_func(root, data);
|
2011-07-25 04:38:58 +00:00
|
|
|
}
|