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(
16 bootstrap=depends(when=target_sysroot.bootstrapped)(lambda: "pkgconf"),
18 when=compile_environment
19 & depends(target.os)(lambda os: os not in ("WINNT", "OSX", "Android")),
23 @depends_if(pkg_config)
24 @checking("for pkg-config version")
25 def pkg_config_version(pkg_config):
26 return Version(check_cmd_output(pkg_config, "--version").rstrip())
29 @depends_if(pkg_config)
30 @checking("whether pkg-config is pkgconf")
31 def is_pkgconf(pkg_config):
32 return "pkgconf " in check_cmd_output(pkg_config, "--about", onerror=lambda: "")
35 @depends(is_pkgconf, pkg_config_version, target_sysroot.bootstrapped, when=pkg_config)
36 def pkg_config_base_flags(is_pkgconf, pkg_config_version, target_sysroot_bootstrapped):
37 # pkgconf 1.7.4 changed the default on Windows to use --static, but
38 # that doesn't work for us.
39 # Note: the --shared flag is not available before pkgconf 1.7
41 if is_pkgconf and pkg_config_version >= "1.7.4":
42 flags.append("--shared")
43 # When pkg-config is in /usr things work fine by default, but when
44 # it is not, it defines prefix to be something else than /usr, which
45 # won't match what the .pc files actually say, and won't work in
47 if target_sysroot_bootstrapped and (
48 (is_pkgconf and pkg_config_version >= "1.2.0")
49 or (not is_pkgconf and pkg_config_version >= "0.29.0")
51 flags.append("--dont-define-prefix")
55 @depends(target, target_sysroot.path, target_multiarch_dir, when=pkg_config)
56 @imports(_from="os", _import="environ")
57 @imports(_from="os", _import="pathsep")
58 def pkg_config_vars(target, sysroot_path, multiarch_dir):
59 if sysroot_path and target.kernel != "Darwin":
62 "usr/lib/{}/pkgconfig".format(multiarch_dir),
63 "usr/share/pkgconfig",
65 if target.bitness == 64:
66 pkgconfig_dirs.insert(0, "usr/lib64/pkgconfig")
69 PKG_CONFIG_SYSROOT_DIR=sysroot_path,
70 PKG_CONFIG_LIBDIR=pathsep.join(
71 os.path.join(sysroot_path, d) for d in pkgconfig_dirs
76 @depends(pkg_config_vars)
77 @imports(_from="os", _import="environ")
78 def pkg_config_env(vars):
81 env["PKG_CONFIG_PATH"] = vars.PKG_CONFIG_PATH
82 env["PKG_CONFIG_SYSROOT_DIR"] = vars.PKG_CONFIG_SYSROOT_DIR
83 env["PKG_CONFIG_LIBDIR"] = vars.PKG_CONFIG_LIBDIR
87 set_config("PKG_CONFIG_PATH", pkg_config_vars.PKG_CONFIG_PATH)
88 set_config("PKG_CONFIG_SYSROOT_DIR", pkg_config_vars.PKG_CONFIG_SYSROOT_DIR)
89 set_config("PKG_CONFIG_LIBDIR", pkg_config_vars.PKG_CONFIG_LIBDIR)
92 # Locates the given module using pkg-config.
93 # - `var` determines the name of variables to set when the package is found.
94 # <var>_CFLAGS and <var>_LIBS are set with corresponding values.
95 # - `package_desc` package name and version requirement string, list of
96 # strings describing packages to locate, or depends function that will
97 # resolve to such a string or list of strings.
98 # - `when` a depends function that will determine whether to perform
99 # any checks (default is to always perform checks).
100 # - `allow_missing` If set, failure to fulfill the package description
101 # will not result in an error or logged message, and any error message
102 # will be returned to the caller.
103 # Returns `True` when the package description is fulfilled.
105 def pkg_check_modules(
106 var, package_desc, when=always, allow_missing=False, config=True, cflags_only=False
108 @depends(dependable(package_desc), when=when)
109 def package_desc(desc):
110 if isinstance(desc, str):
112 if not isinstance(desc, (tuple, list)):
114 "package_desc must be a string or a tuple or list of strings"
117 return " ".join(desc)
119 allow_missing = dependable(allow_missing)
121 @depends(when, "--enable-compile-environment")
122 def when_and_compile_environment(when, compile_environment):
123 return when and compile_environment
125 @depends(pkg_config, pkg_config_version, when=when_and_compile_environment)
126 def check_pkg_config(pkg_config, version):
127 min_version = "0.9.0"
128 if pkg_config is None:
130 "*** The pkg-config script could not be found. Make sure it is\n"
131 "*** in your path, or set the PKG_CONFIG environment variable\n"
132 "*** to the full path to pkg-config."
134 if version < min_version:
136 "*** Your version of pkg-config is too old. You need version %s or newer.",
145 when=when_and_compile_environment,
148 @imports(_from="mozbuild.configure.util", _import="LineIO")
149 def package(pkg_config, env, package_desc, allow_missing):
150 # package_desc may start as a depends function, so we can't use
152 log.info("checking for %s... " % package_desc)
153 retcode, stdout, stderr = get_cmd_output(
155 "--errors-to-stdout",
164 log_writer = log.warning if allow_missing else log.error
165 with LineIO(lambda l: log_writer(l)) as o:
167 if not allow_missing:
171 pkg_config, pkg_config_env, package_desc, pkg_config_base_flags, when=package
173 @checking("%s_CFLAGS" % var, callback=lambda t: " ".join(t))
174 def pkg_cflags(pkg_config, env, package_desc, base_flags):
175 args = list(base_flags) + ["--cflags", package_desc]
176 flags = check_cmd_output(pkg_config, *args, env=env)
177 return tuple(flags.split())
181 @depends(pkg_cflags, when=package)
182 def pkg_info(cflags):
183 return namespace(cflags=cflags)
191 pkg_config_base_flags,
194 @checking("%s_LIBS" % var, callback=lambda t: " ".join(t))
195 def pkg_libs(pkg_config, env, package_desc, base_flags):
196 args = list(base_flags) + ["--libs", package_desc]
197 libs = check_cmd_output(pkg_config, *args, env=env)
198 # Remove evil flags like -Wl,--export-dynamic
199 return tuple(libs.replace("-Wl,--export-dynamic", "").split())
201 @depends(pkg_cflags, pkg_libs, when=package)
202 def pkg_info(cflags, libs):
203 return namespace(cflags=cflags, libs=libs)
206 set_config("%s_CFLAGS" % var, pkg_cflags)
208 set_config("%s_LIBS" % var, pkg_libs)