cmake: Avoid overwriting PKG_CONFIG_PATH env var
authorClemens Lang <clemens.lang@bmw-carit.de>
Fri, 8 Feb 2019 14:48:15 +0000 (15:48 +0100)
committerSimon McVittie <smcv@collabora.com>
Mon, 13 May 2019 09:36:11 +0000 (10:36 +0100)
The CMake config file installed by DBus will run in the context of other
projects. Consequently, changing the value of the PKG_CONFIG_DIR,
PKG_CONFIG_PATH or PKG_CONFIG_LIBDIR environment variables will affect
any further calls to pkg-config made by such projects, which can cause
problems.

A common case of this happening are pkg-config files installed in
usr/share/pkgconfig for .pc files that are architecture-independent, as
for example systemd does.

Avoid clobbering the environment variables by saving and restoring their
values. Note that for some of the variables, setting them to an empty
string is different from not setting them at all.

Signed-off-by: Clemens Lang <clemens.lang@bmw-carit.de>
(cherry picked from commit 3525cc045d4c683dfc6048f5be795cc372c323a3)
Closes: dbus#267

cmake/DBus1Config.pkgconfig.in

index 7e090aa..8c0a25e 100644 (file)
 
 get_filename_component(DBus1_PKGCONFIG_DIR "${CMAKE_CURRENT_LIST_DIR}/../../pkgconfig" ABSOLUTE)
 find_package(PkgConfig)
+if(DEFINED ENV{PKG_CONFIG_DIR})
+    set(_dbus_pkgconfig_dir "$ENV{PKG_CONFIG_DIR}")
+endif()
+if(DEFINED ENV{PKG_CONFIG_PATH})
+    set(_dbus_pkgconfig_path "$ENV{PKG_CONFIG_PATH}")
+endif()
+if(DEFINED ENV{PKG_CONFIG_LIBDIR})
+    set(_dbus_pkgconfig_libdir "$ENV{PKG_CONFIG_LIBDIR}")
+endif()
 set(ENV{PKG_CONFIG_DIR})
 set(ENV{PKG_CONFIG_PATH} ${DBus1_PKGCONFIG_DIR})
 set(ENV{PKG_CONFIG_LIBDIR} ${DBus1_PKGCONFIG_DIR})
 # for debugging
 #set(ENV{PKG_CONFIG_DEBUG_SPEW} 1)
 pkg_check_modules(PC_DBUS1 QUIET dbus-1)
+if(DEFINED _dbus_pkgconfig_dir)
+    set(ENV{PKG_CONFIG_DIR} "${_dbus_pkgconfig_dir}")
+else()
+    unset(ENV{PKG_CONFIG_DIR})
+endif()
+if(DEFINED _dbus_pkgconfig_path)
+    set(ENV{PKG_CONFIG_PATH} "${_dbus_pkgconfig_path}")
+else()
+    unset(ENV{PKG_CONFIG_PATH})
+endif()
+if(DEFINED _dbus_pkgconfig_libdir)
+    set(ENV{PKG_CONFIG_LIBDIR} "${_dbus_pkgconfig_libdir}")
+else()
+    unset(ENV{PKG_CONFIG_LIBDIR})
+endif()
+unset(_dbus_pkgconfig_dir)
+unset(_dbus_pkgconfig_path)
+unset(_dbus_pkgconfig_libdir)
 set(DBus1_DEFINITIONS ${PC_DBUS1_CFLAGS_OTHER})
 
 # find the real stuff and use pkgconfig variables as hints