Bug 1269513 - Implement PKG_CHECK_MODULES equivalent in Python configure. r=glandium
MozReview-Commit-ID: mhFMEG0UXz
--- a/build/autoconf/pkg.m4
+++ b/build/autoconf/pkg.m4
@@ -1,23 +1,21 @@
dnl This Source Code Form is subject to the terms of the Mozilla Public
dnl License, v. 2.0. If a copy of the MPL was not distributed with this
dnl file, You can obtain one at http://mozilla.org/MPL/2.0/.
# PKG_CHECK_MODULES(GSTUFF, gtk+-2.0 >= 1.3 glib = 1.3.4, action-if, action-not)
# defines GSTUFF_LIBS, GSTUFF_CFLAGS, see pkg-config man page
# also defines GSTUFF_PKG_ERRORS on error
+# PKG_CONFIG is set by Python configure, if it is empty here it could not
+# be found.
AC_DEFUN([PKG_CHECK_MODULES],
[succeeded=no
if test -z "$PKG_CONFIG"; then
- AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
- fi
-
- if test "$PKG_CONFIG" = "no" ; then
echo "*** The pkg-config script could not be found. Make sure it is"
echo "*** in your path, or set the PKG_CONFIG environment variable"
echo "*** to the full path to pkg-config."
echo "*** Or see http://www.freedesktop.org/software/pkgconfig to get pkg-config."
else
PKG_CONFIG_MIN_VERSION=0.9.0
if $PKG_CONFIG --atleast-pkgconfig-version $PKG_CONFIG_MIN_VERSION; then
AC_MSG_CHECKING(for $2)
--- a/build/moz.configure/init.configure
+++ b/build/moz.configure/init.configure
@@ -765,9 +765,13 @@ def js_option(*args, **kwargs):
@depends(opt.option, build_project)
def js_option(value, build_project):
if build_project != 'js':
return value.format(opt.option)
add_old_configure_arg(js_option)
+include('pkg.configure')
+# Make this assignment here rather than in pkg.configure to avoid
+# requiring this file in unit tests.
+add_old_configure_assignment('PKG_CONFIG', pkg_config)
include(include_project_configure)
new file mode 100644
--- /dev/null
+++ b/build/moz.configure/pkg.configure
@@ -0,0 +1,86 @@
+# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+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())
+
+# 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.
+# - `condition` a depends function that will determine whether to perform
+# any checks (default is to always perform checks).
+# - `allow_missing` If set, failure to fulfill the package description
+# will not result in an error or logged message, and any error message
+# will be returned to the caller.
+# Returns `True` when the package description is fulfilled.
+@template
+@imports(_from='mozbuild.configure', _import='DependsFunction')
+def pkg_check_modules(var, package_desc,
+ condition=depends('--help')(lambda _: True),
+ allow_missing=False):
+ if isinstance(package_desc, (tuple, list)):
+ package_desc = ' '.join(package_desc)
+ if not isinstance(package_desc, DependsFunction):
+ package_desc = depends('--help')(lambda _: package_desc)
+
+ @depends_when(pkg_config, pkg_config_version, when=condition)
+ def check_pkg_config(pkg_config, version):
+ min_version = '0.9.0'
+ 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=condition)
+ @imports('subprocess')
+ @imports('sys')
+ @imports(_from='mozbuild.configure.util', _import='LineIO')
+ def package(pkg_config, package_desc):
+ # 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])
+ 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)
+ @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)
+ return tuple(flags.split())
+
+ @depends_when(pkg_config, package_desc, 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)
+ # 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/python/mozbuild/mozbuild/test/configure/test_checks_configure.py
+++ b/python/mozbuild/mozbuild/test/configure/test_checks_configure.py
@@ -648,10 +648,145 @@ class TestChecksConfigure(unittest.TestC
self.assertEqual(out, textwrap.dedent('''\
checking for java... %s
checking for javah... %s
checking for jar... %s
checking for jarsigner... not found
ERROR: The program jarsigner was not found. Set $JAVA_HOME to your Java SDK directory or use '--with-java-bin-path={java-bin-dir}'
''' % (java, javah, jar)))
+ def test_pkg_check_modules(self):
+ mock_pkg_config_version = '0.10.0'
+ mock_pkg_config_path = mozpath.abspath('/usr/bin/pkg-config')
+
+ def mock_pkg_config(_, args):
+ if args[0:2] == ['--errors-to-stdout', '--print-errors']:
+ assert len(args) == 3
+ package = args[2]
+ if package == 'unknown':
+ return (1, "Package unknown was not found in the pkg-config search path.\n"
+ "Perhaps you should add the directory containing `unknown.pc'\n"
+ "to the PKG_CONFIG_PATH environment variable\n"
+ "No package 'unknown' found", '')
+ if package == 'valid':
+ return 0, '', ''
+ if package == 'new > 1.1':
+ return 1, "Requested 'new > 1.1' but version of new is 1.1", ''
+ if args[0] == '--cflags':
+ assert len(args) == 2
+ return 0, '-I/usr/include/%s' % args[1], ''
+ if args[0] == '--libs':
+ assert len(args) == 2
+ return 0, '-l%s' % args[1], ''
+ if args[0] == '--version':
+ return 0, mock_pkg_config_version, ''
+ self.fail("Unexpected arguments to mock_pkg_config: %s" % args)
+
+ extra_paths = {
+ mock_pkg_config_path: mock_pkg_config,
+ }
+ includes = ('util.configure', 'checks.configure', 'pkg.configure')
+
+ config, output, status = self.get_result("pkg_check_modules('MOZ_VALID', 'valid')",
+ includes=includes)
+ self.assertEqual(status, 1)
+ self.assertEqual(output, textwrap.dedent('''\
+ checking for pkg_config... not found
+ ERROR: *** The pkg-config script could not be found. Make sure it is
+ *** in your path, or set the PKG_CONFIG environment variable
+ *** to the full path to pkg-config.
+ '''))
+
+
+ config, output, status = self.get_result("pkg_check_modules('MOZ_VALID', 'valid')",
+ extra_paths=extra_paths,
+ includes=includes)
+ self.assertEqual(status, 0)
+ self.assertEqual(output, textwrap.dedent('''\
+ checking for pkg_config... %s
+ checking for pkg-config version... %s
+ checking for valid... yes
+ checking MOZ_VALID_CFLAGS... -I/usr/include/valid
+ checking MOZ_VALID_LIBS... -lvalid
+ ''' % (mock_pkg_config_path, mock_pkg_config_version)))
+ self.assertEqual(config, {
+ 'PKG_CONFIG': mock_pkg_config_path,
+ 'MOZ_VALID_CFLAGS': ('-I/usr/include/valid',),
+ 'MOZ_VALID_LIBS': ('-lvalid',),
+ })
+
+ config, output, status = self.get_result("pkg_check_modules('MOZ_UKNOWN', 'unknown')",
+ extra_paths=extra_paths,
+ includes=includes)
+ self.assertEqual(status, 1)
+ self.assertEqual(output, textwrap.dedent('''\
+ checking for pkg_config... %s
+ checking for pkg-config version... %s
+ checking for unknown... no
+ ERROR: Package unknown was not found in the pkg-config search path.
+ ERROR: Perhaps you should add the directory containing `unknown.pc'
+ ERROR: to the PKG_CONFIG_PATH environment variable
+ ERROR: No package 'unknown' found
+ ''' % (mock_pkg_config_path, mock_pkg_config_version)))
+ self.assertEqual(config, {
+ 'PKG_CONFIG': mock_pkg_config_path,
+ })
+
+ config, output, status = self.get_result("pkg_check_modules('MOZ_NEW', 'new > 1.1')",
+ extra_paths=extra_paths,
+ includes=includes)
+ self.assertEqual(status, 1)
+ self.assertEqual(output, textwrap.dedent('''\
+ checking for pkg_config... %s
+ checking for pkg-config version... %s
+ checking for new > 1.1... no
+ ERROR: Requested 'new > 1.1' but version of new is 1.1
+ ''' % (mock_pkg_config_path, mock_pkg_config_version)))
+ self.assertEqual(config, {
+ 'PKG_CONFIG': mock_pkg_config_path,
+ })
+
+ # allow_missing makes missing packages non-fatal.
+ cmd = textwrap.dedent('''\
+ have_new_module = pkg_check_modules('MOZ_NEW', 'new > 1.1', allow_missing=True)
+ @depends(have_new_module)
+ def log_new_module_error(mod):
+ if mod is not True:
+ log.info('Module not found.')
+ ''')
+
+ config, output, status = self.get_result(cmd,
+ extra_paths=extra_paths,
+ includes=includes)
+ self.assertEqual(status, 0)
+ self.assertEqual(output, textwrap.dedent('''\
+ checking for pkg_config... %s
+ checking for pkg-config version... %s
+ checking for new > 1.1... no
+ WARNING: Requested 'new > 1.1' but version of new is 1.1
+ Module not found.
+ ''' % (mock_pkg_config_path, mock_pkg_config_version)))
+ self.assertEqual(config, {
+ 'PKG_CONFIG': mock_pkg_config_path,
+ })
+
+ def mock_old_pkg_config(_, args):
+ if args[0] == '--version':
+ return 0, '0.8.10', ''
+ self.fail("Unexpected arguments to mock_old_pkg_config: %s" % args)
+
+ extra_paths = {
+ mock_pkg_config_path: mock_old_pkg_config,
+ }
+
+ config, output, status = self.get_result("pkg_check_modules('MOZ_VALID', 'valid')",
+ extra_paths=extra_paths,
+ includes=includes)
+ self.assertEqual(status, 1)
+ self.assertEqual(output, textwrap.dedent('''\
+ checking for pkg_config... %s
+ checking for pkg-config version... 0.8.10
+ ERROR: *** Your version of pkg-config is too old. You need version 0.9.0 or newer.
+ ''' % mock_pkg_config_path))
+
+
if __name__ == '__main__':
main()