summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Larsson <alexl@redhat.com>2015-06-13 10:22:48 -0700
committerDan Nicholson <dbn.lists@gmail.com>2016-01-29 14:32:24 -0800
commitae0a8b1ad5e40f68fc09ffedac215e038a50f070 (patch)
treebb8c37ac58dce6a6224b0f7b8db4cc8a6c7c0ea2
parent9ef2d3475df0eba199427e9c47d9e502bd45b116 (diff)
Allow overriding package variables with env vars
pkg-config allows a way to override package variables through the --define-prefix interface, but this is very cumbersome to do in a global way since it always needs to be passed on the command line and the override cannot be scoped to a single packge. Allow overriding package variables using environment variables of the form PKG_CONFIG_$PACKAGE_$VARIABLE. For example, setting PKG_CONFIG_GLADEUI_2_0_CATALOGDIR will override the variable "catalogdir" in the "gladeui-2.0" package. https://bugs.freedesktop.org/show_bug.cgi?id=90917
-rw-r--r--check/Makefile.am4
-rwxr-xr-xcheck/check-variable-override35
-rw-r--r--pkg-config.17
-rw-r--r--pkg.c36
4 files changed, 80 insertions, 2 deletions
diff --git a/check/Makefile.am b/check/Makefile.am
index 34d0e34..4f92e0d 100644
--- a/check/Makefile.am
+++ b/check/Makefile.am
@@ -26,7 +26,9 @@ TESTS = \
check-debug \
check-gtk \
check-tilde \
- check-relocatable
+ check-relocatable \
+ check-variable-override \
+ $(NULL)
EXTRA_DIST = \
$(TESTS) \
diff --git a/check/check-variable-override b/check/check-variable-override
new file mode 100755
index 0000000..e3f6cbd
--- /dev/null
+++ b/check/check-variable-override
@@ -0,0 +1,35 @@
+#! /bin/sh
+
+set -e
+
+. ${srcdir}/common
+
+# Check the normal behavior
+RESULT="/usr"
+run_test --variable=prefix simple
+RESULT="/usr/lib"
+run_test --variable=libdir simple
+
+# Override prefix with correct environment variable
+export PKG_CONFIG_SIMPLE_PREFIX="/foo"
+RESULT="/foo"
+run_test --variable=prefix simple
+RESULT="/foo/lib"
+run_test --variable=libdir simple
+RESULT="-I/foo/include"
+run_test --cflags simple
+unset PKG_CONFIG_SIMPLE_PREFIX
+
+# Override prefix with incorrect environment variable case. On Windows
+# this will have no effect as environment variables are case
+# insensitive.
+if [ "$native_win32" != yes ]; then
+ export PKG_CONFIG_SIMPLE_prefix="/foo"
+ RESULT="/usr"
+ run_test --variable=prefix simple
+ RESULT="/usr/lib"
+ run_test --variable=libdir simple
+ RESULT=""
+ run_test --cflags simple
+ unset PKG_CONFIG_SIMPLE_prefix
+fi
diff --git a/pkg-config.1 b/pkg-config.1
index 11e6292..f7daa81 100644
--- a/pkg-config.1
+++ b/pkg-config.1
@@ -338,6 +338,13 @@ Replaces the default
.I pkg-config
search directory, usually
.IR /usr/lib/pkgconfig : /usr/share/pkgconfig .
+.TP
+.I "PKG_CONFIG_$PACKAGE_$VARIABLE"
+Overrides the variable VARIABLE in the package PACKAGE. The environment
+variable should have the package name and package variable upper cased
+with non-alphanumeric characters converted to underscores. For example,
+setting PKG_CONFIG_GLADEUI_2_0_CATALOGDIR will override the variable
+"catalogdir" in the "gladeui-2.0" package.
.\"
.SH PKG-CONFIG DERIVED VARIABLES
.I pkg-config
diff --git a/pkg.c b/pkg.c
index c8bac4d..786ea4c 100644
--- a/pkg.c
+++ b/pkg.c
@@ -1021,6 +1021,24 @@ define_global_variable (const char *varname,
}
char *
+var_to_env_var (const char *pkg, const char *var)
+{
+ char *new = g_strconcat ("PKG_CONFIG_", pkg, "_", var, NULL);
+ char *p;
+ for (p = new; *p != 0; p++)
+ {
+ char c = g_ascii_toupper (*p);
+
+ if (!g_ascii_isalnum (c))
+ c = '_';
+
+ *p = c;
+ }
+
+ return new;
+}
+
+char *
package_get_var (Package *pkg,
const char *var)
{
@@ -1028,7 +1046,23 @@ package_get_var (Package *pkg,
if (globals)
varval = g_strdup (g_hash_table_lookup (globals, var));
-
+
+ /* Allow overriding specific variables using an environment variable of the
+ * form PKG_CONFIG_$PACKAGENAME_$VARIABLE
+ */
+ if (pkg->key)
+ {
+ char *env_var = var_to_env_var (pkg->key, var);
+ const char *env_var_content = g_getenv (env_var);
+ g_free (env_var);
+ if (env_var_content)
+ {
+ debug_spew ("Overriding variable '%s' from environment\n", var);
+ return g_strdup (env_var_content);
+ }
+ }
+
+
if (varval == NULL && pkg->vars)
varval = g_strdup (g_hash_table_lookup (pkg->vars, var));