From 503eff246f4c6fbefe74c262be97579e93a2f759 Mon Sep 17 00:00:00 2001 Message-Id: <503eff246f4c6fbefe74c262be97579e93a2f759.1434216425.git.dbn.lists@gmail.com> From: Alexander Larsson Date: Sat, 13 Jun 2015 10:22:48 -0700 Subject: [PATCH] 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 --- check/Makefile.am | 4 +++- check/check-variable-override | 26 ++++++++++++++++++++++++++ pkg-config.1 | 7 +++++++ pkg.c | 36 +++++++++++++++++++++++++++++++++++- 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100755 check/check-variable-override 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..0f11034 --- /dev/null +++ b/check/check-variable-override @@ -0,0 +1,26 @@ +#! /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 +unset PKG_CONFIG_SIMPLE_PREFIX + +# Override prefix with incorrect environment variable +export PKG_CONFIG_SIMPLE_prefix="/foo" +RESULT="/usr" +run_test --variable=prefix simple +RESULT="/usr/lib" +run_test --variable=libdir simple diff --git a/pkg-config.1 b/pkg-config.1 index 74bb70a..9693d06 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 a8980f0..398e934 100644 --- a/pkg.c +++ b/pkg.c @@ -1019,6 +1019,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) { @@ -1026,7 +1044,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)); -- 1.9.3