From 60c05f5621b771a288f80b1239fd0a287c58f3d4 Mon Sep 17 00:00:00 2001 From: TingPing Date: Thu, 8 Mar 2018 05:00:22 +0000 Subject: [PATCH] Improve prefix rewriting on Windows (#177) * cli: Default to rewriting prefix on Windows This matches `pkg-config` behavior * libpkgconf: Rewrite the prefix of all variables --- cli/main.c | 4 ++++ libpkgconf/pkg.c | 23 ++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/cli/main.c b/cli/main.c index d200d4c..03995dc 100644 --- a/cli/main.c +++ b/cli/main.c @@ -922,7 +922,11 @@ main(int argc, char *argv[]) if ((want_flags & PKG_NO_CACHE) == PKG_NO_CACHE) want_client_flags |= PKGCONF_PKG_PKGF_NO_CACHE; +/* On Windows we want to always redefine the prefix by default + * but allow that behavior to be manually disabled */ +#if !defined(_WIN32) && !defined(_WIN64) if ((want_flags & PKG_DEFINE_PREFIX) == PKG_DEFINE_PREFIX) +#endif want_client_flags |= PKGCONF_PKG_PKGF_REDEFINE_PREFIX; if ((want_flags & PKG_NO_UNINSTALLED) == PKG_NO_UNINSTALLED || getenv("PKG_CONFIG_DISABLE_UNINSTALLED") != NULL) diff --git a/libpkgconf/pkg.c b/libpkgconf/pkg.c index 0feb4d6..65bb3cf 100644 --- a/libpkgconf/pkg.c +++ b/libpkgconf/pkg.c @@ -287,6 +287,8 @@ pkgconf_pkg_new_from_file(pkgconf_client_t *client, const char *filename, FILE * pkgconf_pkg_t *pkg; char readbuf[PKGCONF_BUFSIZE]; char pathbuf[PKGCONF_ITEM_SIZE]; + char original_prefix[PKGCONF_ITEM_SIZE]; + char prefix[PKGCONF_ITEM_SIZE]; char *idptr; size_t lineno = 0; @@ -294,6 +296,8 @@ pkgconf_pkg_new_from_file(pkgconf_client_t *client, const char *filename, FILE * pkg->filename = strdup(filename); pkgconf_tuple_add(client, &pkg->vars, "pcfiledir", pkg_get_parent_dir(pkg, pathbuf, sizeof pathbuf), true); + original_prefix[0] = '\0'; + /* make module id */ if ((idptr = strrchr(pkg->filename, PKG_DIR_SEP_S)) != NULL) idptr++; @@ -368,7 +372,22 @@ pkgconf_pkg_new_from_file(pkgconf_client_t *client, const char *filename, FILE * pkgconf_pkg_parser_keyword_set(client, pkg, lineno, key, value); break; case '=': - if (strcmp(key, client->prefix_varname) || !(client->flags & PKGCONF_PKG_PKGF_REDEFINE_PREFIX)) + /* Some pc files will use absolute paths for all of their directories + * which is broken when redefining the prefix. We try to outsmart the + * file and rewrite any directory that starts with the same prefix. + * Note that `pkg-config` just blindly sets all of the directories + * so this is a behavioral difference but arguablly more correct. + */ + if (client->flags & PKGCONF_PKG_PKGF_REDEFINE_PREFIX && original_prefix[0] + && !strncmp(value, original_prefix, strlen(original_prefix))) + { + char newvalue[PKGCONF_ITEM_SIZE]; + + pkgconf_strlcpy(newvalue, prefix, sizeof newvalue); + pkgconf_strlcat(newvalue, value + strlen(original_prefix), sizeof newvalue); + pkgconf_tuple_add(client, &pkg->vars, key, newvalue, false); + } + else if (strcmp(key, client->prefix_varname) || !(client->flags & PKGCONF_PKG_PKGF_REDEFINE_PREFIX)) pkgconf_tuple_add(client, &pkg->vars, key, value, true); else { @@ -377,6 +396,8 @@ pkgconf_pkg_new_from_file(pkgconf_client_t *client, const char *filename, FILE * { pkgconf_tuple_add(client, &pkg->vars, "orig_prefix", value, true); pkgconf_tuple_add(client, &pkg->vars, key, relvalue, false); + pkgconf_strlcpy(original_prefix, value, sizeof original_prefix); + pkgconf_strlcpy(prefix, relvalue, sizeof original_prefix); } else pkgconf_tuple_add(client, &pkg->vars, key, value, true);