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 @depends(compile_environment, target)
14 def use_pkg_config(compile_environment, target):
15 return compile_environment and target.os not in ("WINNT", "OSX", "Android")
18 pkg_config = check_prog(
21 bootstrap=depends(when=target_sysroot.bootstrapped)(lambda: "pkgconf"),
27 @depends_if(pkg_config)
28 @checking("for pkg-config version")
29 def pkg_config_version(pkg_config):
30 return Version(check_cmd_output(pkg_config, "--version").rstrip())
33 @depends_if(pkg_config)
34 @checking("whether pkg-config is pkgconf")
35 def is_pkgconf(pkg_config):
36 return "pkgconf " in check_cmd_output(pkg_config, "--about", onerror=lambda: "")
39 @depends(is_pkgconf, pkg_config_version, target_sysroot.bootstrapped, when=pkg_config)
40 def pkg_config_base_flags(is_pkgconf, pkg_config_version, target_sysroot_bootstrapped):
41 # pkgconf 1.7.4 changed the default on Windows to use --static, but
42 # that doesn't work for us.
43 # Note: the --shared flag is not available before pkgconf 1.7
45 if is_pkgconf and pkg_config_version >= "1.7.4":
46 flags.append("--shared")
47 # When pkg-config is in /usr things work fine by default, but when
48 # it is not, it defines prefix to be something else than /usr, which
49 # won't match what the .pc files actually say, and won't work in
51 if target_sysroot_bootstrapped and (
52 (is_pkgconf and pkg_config_version >= "1.2.0")
53 or (not is_pkgconf and pkg_config_version >= "0.29.0")
55 flags.append("--dont-define-prefix")
59 @depends(target, target_sysroot.path, target_multiarch_dir, when=pkg_config)
60 @imports(_from="os", _import="environ")
61 @imports(_from="os", _import="pathsep")
62 def pkg_config_vars(target, sysroot_path, multiarch_dir):
63 if sysroot_path and target.kernel != "Darwin":
66 "usr/lib/{}/pkgconfig".format(multiarch_dir),
67 "usr/share/pkgconfig",
69 if target.bitness == 64:
70 pkgconfig_dirs.insert(0, "usr/lib64/pkgconfig")
73 PKG_CONFIG_SYSROOT_DIR=sysroot_path,
74 PKG_CONFIG_LIBDIR=pathsep.join(
75 os.path.join(sysroot_path, d) for d in pkgconfig_dirs
80 @depends(pkg_config_vars)
81 @imports(_from="os", _import="environ")
82 def pkg_config_env(vars):
85 env["PKG_CONFIG_PATH"] = vars.PKG_CONFIG_PATH
86 env["PKG_CONFIG_SYSROOT_DIR"] = vars.PKG_CONFIG_SYSROOT_DIR
87 env["PKG_CONFIG_LIBDIR"] = vars.PKG_CONFIG_LIBDIR
91 set_config("PKG_CONFIG_PATH", pkg_config_vars.PKG_CONFIG_PATH)
92 set_config("PKG_CONFIG_SYSROOT_DIR", pkg_config_vars.PKG_CONFIG_SYSROOT_DIR)
93 set_config("PKG_CONFIG_LIBDIR", pkg_config_vars.PKG_CONFIG_LIBDIR)
96 # Locates the given module using pkg-config.
97 # - `var` determines the name of variables to set when the package is found.
98 # <var>_CFLAGS and <var>_LIBS are set with corresponding values.
99 # - `package_desc` package name and version requirement string, list of
100 # strings describing packages to locate, or depends function that will
101 # resolve to such a string or list of strings.
102 # - `when` a depends function that will determine whether to perform
103 # any checks (default is to always perform checks).
104 # - `allow_missing` If set, failure to fulfill the package description
105 # will not result in an error or logged message, and any error message
106 # will be returned to the caller.
107 # Returns `True` when the package description is fulfilled.
109 def pkg_check_modules(
117 @depends(dependable(package_desc), when=when)
118 def package_desc(desc):
119 if isinstance(desc, str):
121 if not isinstance(desc, (tuple, list)):
123 "package_desc must be a string or a tuple or list of strings"
126 return " ".join(desc)
128 allow_missing = dependable(allow_missing)
130 @depends(when, when=use_pkg_config)
131 def when_and_use_pkg_config(when):
134 @depends(pkg_config, pkg_config_version, when=when_and_use_pkg_config)
135 def check_pkg_config(pkg_config, version):
136 min_version = "0.9.0"
137 if pkg_config is None:
139 "*** The pkg-config script could not be found. Make sure it is\n"
140 "*** in your path, or set the PKG_CONFIG environment variable\n"
141 "*** to the full path to pkg-config."
143 if version < min_version:
145 "*** Your version of pkg-config is too old. You need version %s or newer.",
154 when=when_and_use_pkg_config,
157 @imports(_from="mozbuild.configure.util", _import="LineIO")
158 def package(pkg_config, env, package_desc, allow_missing):
159 # package_desc may start as a depends function, so we can't use
161 log.info("checking for %s... " % package_desc)
162 retcode, stdout, stderr = get_cmd_output(
164 "--errors-to-stdout",
173 log_writer = log.warning if allow_missing else log.error
174 with LineIO(lambda l: log_writer(l)) as o:
176 if not allow_missing:
180 pkg_config, pkg_config_env, package_desc, pkg_config_base_flags, when=package
182 @checking("%s_CFLAGS" % var, callback=lambda t: " ".join(t))
183 def pkg_cflags(pkg_config, env, package_desc, base_flags):
184 args = list(base_flags) + ["--cflags", package_desc]
185 flags = check_cmd_output(pkg_config, *args, env=env)
186 return tuple(flags.split())
190 @depends(pkg_cflags, when=package)
191 def pkg_info(cflags):
192 return namespace(cflags=cflags)
200 pkg_config_base_flags,
203 @checking("%s_LIBS" % var, callback=lambda t: " ".join(t))
204 def pkg_libs(pkg_config, env, package_desc, base_flags):
205 args = list(base_flags) + ["--libs", package_desc]
206 libs = check_cmd_output(pkg_config, *args, env=env)
207 # Remove evil flags like -Wl,--export-dynamic
208 return tuple(libs.replace("-Wl,--export-dynamic", "").split())
210 @depends(pkg_cflags, pkg_libs, when=package)
211 def pkg_info(cflags, libs):
212 return namespace(cflags=cflags, libs=libs)
215 set_config("%s_CFLAGS" % var, pkg_cflags)
217 set_config("%s_LIBS" % var, pkg_libs)