Bug 1869043 add a main thread record of track audio outputs r=padenot
[gecko.git] / build / moz.configure / pkg.configure
blob6beed205f1502e9927d36e60198aef6eabdd86af
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(
14     "PKG_CONFIG",
15     pkg_config,
16     bootstrap=depends(when=target_sysroot.bootstrapped)(lambda: "pkgconf"),
17     allow_missing=True,
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
40     flags = []
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
46     # sysroots.
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")
50     ):
51         flags.append("--dont-define-prefix")
52     return tuple(flags)
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":
60         pkgconfig_dirs = [
61             "usr/lib/pkgconfig",
62             "usr/lib/{}/pkgconfig".format(multiarch_dir),
63             "usr/share/pkgconfig",
64         ]
65         if target.bitness == 64:
66             pkgconfig_dirs.insert(0, "usr/lib64/pkgconfig")
67         return namespace(
68             PKG_CONFIG_PATH="",
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
72             ),
73         )
76 @depends(pkg_config_vars)
77 @imports(_from="os", _import="environ")
78 def pkg_config_env(vars):
79     if vars:
80         env = dict(environ)
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
84         return env
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.
104 @template
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):
111             desc = [desc]
112         if not isinstance(desc, (tuple, list)):
113             configure_error(
114                 "package_desc must be a string or a tuple or list of strings"
115             )
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:
129             die(
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."
133             )
134         if version < min_version:
135             die(
136                 "*** Your version of pkg-config is too old. You need version %s or newer.",
137                 min_version,
138             )
140     @depends(
141         pkg_config,
142         pkg_config_env,
143         package_desc,
144         allow_missing,
145         when=when_and_compile_environment,
146     )
147     @imports("sys")
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
151         # @checking here.
152         log.info("checking for %s... " % package_desc)
153         retcode, stdout, stderr = get_cmd_output(
154             pkg_config,
155             "--errors-to-stdout",
156             "--print-errors",
157             package_desc,
158             env=env,
159         )
160         if retcode == 0:
161             log.info("yes")
162             return True
163         log.info("no")
164         log_writer = log.warning if allow_missing else log.error
165         with LineIO(lambda l: log_writer(l)) as o:
166             o.write(stdout)
167         if not allow_missing:
168             sys.exit(1)
170     @depends(
171         pkg_config, pkg_config_env, package_desc, pkg_config_base_flags, when=package
172     )
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())
179     if cflags_only:
181         @depends(pkg_cflags, when=package)
182         def pkg_info(cflags):
183             return namespace(cflags=cflags)
185     else:
187         @depends(
188             pkg_config,
189             pkg_config_env,
190             package_desc,
191             pkg_config_base_flags,
192             when=package,
193         )
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)
205     if config:
206         set_config("%s_CFLAGS" % var, pkg_cflags)
207         if not cflags_only:
208             set_config("%s_LIBS" % var, pkg_libs)
210     return pkg_info