1 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
2 # vim: set filetype=python:
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 @depends(toolchain_prefix, when=compile_environment)
9 def pkg_config(prefixes):
10 return tuple("{}pkg-config".format(p) for p in (prefixes or ()) + ("",))
13 pkg_config = check_prog("PKG_CONFIG", pkg_config, allow_missing=True)
16 @depends_if(pkg_config)
17 @checking("for pkg-config version")
18 def pkg_config_version(pkg_config):
19 return Version(check_cmd_output(pkg_config, "--version").rstrip())
22 @depends(sysroot_path, multiarch_dir, when=pkg_config)
23 @imports(_from="os", _import="environ")
24 def pkg_config_vars(sysroot_path, multiarch_dir):
27 PKG_CONFIG_SYSROOT_DIR=sysroot_path,
28 PKG_CONFIG_LIBDIR=":".join(
29 os.path.join(sysroot_path, d)
32 "usr/lib/{}/pkgconfig".format(multiarch_dir),
33 "usr/share/pkgconfig",
39 @depends(pkg_config_vars)
40 @imports(_from="os", _import="environ")
41 def pkg_config_env(vars):
44 env["PKG_CONFIG_SYSROOT_DIR"] = vars.PKG_CONFIG_SYSROOT_DIR
45 env["PKG_CONFIG_LIBDIR"] = vars.PKG_CONFIG_LIBDIR
49 set_config("PKG_CONFIG_SYSROOT_DIR", pkg_config_vars.PKG_CONFIG_SYSROOT_DIR)
50 set_config("PKG_CONFIG_LIBDIR", pkg_config_vars.PKG_CONFIG_LIBDIR)
51 add_old_configure_assignment(
52 "PKG_CONFIG_SYSROOT_DIR", pkg_config_vars.PKG_CONFIG_SYSROOT_DIR
54 add_old_configure_assignment("PKG_CONFIG_LIBDIR", pkg_config_vars.PKG_CONFIG_LIBDIR)
57 # Locates the given module using pkg-config.
58 # - `var` determines the name of variables to set when the package is found.
59 # <var>_CFLAGS and <var>_LIBS are set with corresponding values.
60 # - `package_desc` package name and version requirement string, list of
61 # strings describing packages to locate, or depends function that will
62 # resolve to such a string or list of strings.
63 # - `when` a depends function that will determine whether to perform
64 # any checks (default is to always perform checks).
65 # - `allow_missing` If set, failure to fulfill the package description
66 # will not result in an error or logged message, and any error message
67 # will be returned to the caller.
68 # Returns `True` when the package description is fulfilled.
70 def pkg_check_modules(var, package_desc, when=always, allow_missing=False, config=True):
71 if isinstance(package_desc, (tuple, list)):
72 package_desc = " ".join(package_desc)
73 package_desc = dependable(package_desc)
74 allow_missing = dependable(allow_missing)
76 @depends(when, "--enable-compile-environment")
77 def when_and_compile_environment(when, compile_environment):
78 return when and compile_environment
80 @depends(pkg_config, pkg_config_version, when=when_and_compile_environment)
81 def check_pkg_config(pkg_config, version):
83 if pkg_config is None:
85 "*** The pkg-config script could not be found. Make sure it is\n"
86 "*** in your path, or set the PKG_CONFIG environment variable\n"
87 "*** to the full path to pkg-config."
89 if version < min_version:
91 "*** Your version of pkg-config is too old. You need version %s or newer.",
100 when=when_and_compile_environment,
103 @imports(_from="mozbuild.configure.util", _import="LineIO")
104 def package(pkg_config, env, package_desc, allow_missing):
105 # package_desc may start as a depends function, so we can't use
107 log.info("checking for %s... " % package_desc)
108 retcode, stdout, stderr = get_cmd_output(
110 "--errors-to-stdout",
119 log_writer = log.warning if allow_missing else log.error
120 with LineIO(lambda l: log_writer(l)) as o:
122 if not allow_missing:
125 @depends(pkg_config, pkg_config_env, package_desc, when=package)
126 @checking("%s_CFLAGS" % var, callback=lambda t: " ".join(t))
127 def pkg_cflags(pkg_config, env, package_desc):
128 flags = check_cmd_output(pkg_config, "--cflags", package_desc, env=env)
129 return tuple(flags.split())
131 @depends(pkg_config, pkg_config_env, package_desc, when=package)
132 @checking("%s_LIBS" % var, callback=lambda t: " ".join(t))
133 def pkg_libs(pkg_config, env, package_desc):
134 libs = check_cmd_output(pkg_config, "--libs", package_desc, env=env)
135 # Remove evil flags like -Wl,--export-dynamic
136 return tuple(libs.replace("-Wl,--export-dynamic", "").split())
138 @depends(pkg_cflags, pkg_libs, when=package)
139 def pkg_info(cflags, libs):
140 return namespace(cflags=cflags, libs=libs)
143 set_config("%s_CFLAGS" % var, pkg_cflags)
144 set_config("%s_LIBS" % var, pkg_libs)