Bug 1305145 - Forward pkg-config environment variables from mozconfig to Python configure's pkg_check_modules. draft
authorChris Manchester <cmanchester@mozilla.com>
Tue, 04 Oct 2016 16:43:58 -0700
changeset 420899 ea31f841a601870f23198b807bee4e2263c1e20c
parent 420898 3b380b931e81fdc6970dc4011c870cb8a67b2b06
child 420900 11e6d6fa78e8995bd1781cfcdee09cb7d8f2769a
push id31338
push userbmo:cmanchester@mozilla.com
push dateTue, 04 Oct 2016 23:44:13 +0000
bugs1305145
milestone52.0a1
Bug 1305145 - Forward pkg-config environment variables from mozconfig to Python configure's pkg_check_modules. MozReview-Commit-ID: KDCoxxh37Um
build/moz.configure/pkg.configure
build/moz.configure/util.configure
--- a/build/moz.configure/pkg.configure
+++ b/build/moz.configure/pkg.configure
@@ -7,16 +7,23 @@
 pkg_config = check_prog('PKG_CONFIG', ('pkg-config',), allow_missing=True)
 
 @depends_if(pkg_config)
 @checking('for pkg-config version')
 @imports('subprocess')
 def pkg_config_version(pkg_config):
     return Version(check_cmd_output(pkg_config, '--version').rstrip())
 
+option(env='PKG_CONFIG_PATH', nargs=1,
+       help='Path to set for pkg-config')
+option(env='PKG_CONFIG_LIBDIR', nargs=1,
+       help='Libdir to set for pkg-config')
+option(env='PKG_CONFIG_SYSROOT_DIR', nargs=1,
+       help='Sysroot dir to set for pkg-config')
+
 # Locates the given module using pkg-config.
 # - `var` determines the name of variables to set when the package is found.
 #   <var>_CFLAGS and <var>_LIBS are set with corresponding values.
 # - `package_desc` package name and version requirement string, list of
 #   strings describing packages to locate, or depends function that will
 #   resolve to such a string or list of strings.
 # - `when` a depends function that will determine whether to perform
 #   any checks (default is to always perform checks).
@@ -37,47 +44,62 @@ def pkg_check_modules(var, package_desc,
         if pkg_config is None:
             die("*** The pkg-config script could not be found. Make sure it is\n"
                 "*** in your path, or set the PKG_CONFIG environment variable\n"
                 "*** to the full path to pkg-config.")
         if version < min_version:
             die("*** Your version of pkg-config is too old. You need version %s or newer.",
                 min_version)
 
-    @depends_when(pkg_config, package_desc, when=when)
+    @depends('PKG_CONFIG_PATH', 'PKG_CONFIG_SYSROOT_DIR', 'PKG_CONFIG_LIBDIR')
+    @imports('os')
+    def pkg_config_env(pkg_path, sysroot_dir, libdir):
+        env = os.environ.copy()
+        if pkg_path:
+            env['PKG_CONFIG_PATH'] = pkg_path[0]
+        if sysroot_dir:
+            env['PKG_CONFIG_SYSROOT_DIR'] = sysroot_dir[0]
+        if libdir:
+            env['PKG_CONFIG_LIBDIR'] = libdir[0]
+        return env
+
+    @depends_when(pkg_config, package_desc, pkg_config_env, when=when)
     @imports('subprocess')
     @imports('sys')
     @imports(_from='mozbuild.configure.util', _import='LineIO')
-    def package(pkg_config, package_desc):
+    def package(pkg_config, package_desc, pkg_env):
         # package_desc may start as a depends function, so we can't use
         # @checking here.
         log.info("checking for %s... " % package_desc)
         with log.queue_debug():
             try:
                 subprocess.check_output([pkg_config, '--errors-to-stdout',
-                                         '--print-errors', package_desc])
+                                         '--print-errors', package_desc],
+                                        env=pkg_env)
                 log.info("yes")
                 return True
             except subprocess.CalledProcessError as e:
                 log.info("no")
                 log_writer = log.warning if allow_missing else log.error
                 with LineIO(lambda l: log_writer(l)) as o:
                     o.write(e.output)
                 if not allow_missing:
                     sys.exit(1)
 
-    @depends_when(pkg_config, package_desc, when=package)
+    @depends_when(pkg_config, package_desc, pkg_config_env, when=package)
     @checking('%s_CFLAGS' % var, callback=lambda t: ' '.join(t))
-    def pkg_cflags(pkg_config, package_desc):
-        flags = check_cmd_output(pkg_config, '--cflags', package_desc)
+    def pkg_cflags(pkg_config, package_desc, pkg_env):
+        flags = check_cmd_output(pkg_config, '--cflags', package_desc,
+                                 env=pkg_env)
         return tuple(flags.split())
 
-    @depends_when(pkg_config, package_desc, when=package)
+    @depends_when(pkg_config, package_desc, pkg_config_env, when=package)
     @checking('%s_LIBS' % var, callback=lambda t: ' '.join(t))
-    def pkg_libs(pkg_config, package_desc):
-        libs = check_cmd_output(pkg_config, '--libs', package_desc)
+    def pkg_libs(pkg_config, package_desc, pkg_env):
+        libs = check_cmd_output(pkg_config, '--libs', package_desc,
+                                env=pkg_env)
         # Remove evil flags like -Wl,--export-dynamic
         return tuple(libs.replace('-Wl,--export-dynamic', '').split())
 
     set_config('%s_CFLAGS' % var, pkg_cflags)
     set_config('%s_LIBS' % var, pkg_libs)
 
     return package
--- a/build/moz.configure/util.configure
+++ b/build/moz.configure/util.configure
@@ -27,17 +27,17 @@ def configure_error(message):
 @imports(_from='mozbuild.configure.util', _import='LineIO')
 @imports(_from='mozbuild.shellutil', _import='quote')
 def check_cmd_output(*args, **kwargs):
     onerror = kwargs.pop('onerror', None)
 
     with log.queue_debug():
         log.debug('Executing: `%s`', quote(*args))
         proc = subprocess.Popen(args, stdout=subprocess.PIPE,
-                                stderr=subprocess.PIPE)
+                                stderr=subprocess.PIPE, **kwargs)
         stdout, stderr = proc.communicate()
         retcode = proc.wait()
         if retcode == 0:
             return stdout
 
         log.debug('The command returned non-zero exit status %d.',
                   retcode)
         for out, desc in ((stdout, 'output'), (stderr, 'error output')):