Bug 1700051: part 30) Narrow scope of `newOffset`. r=smaug
[gecko.git] / build / moz.configure / pkg.configure
blob7ea253b80ac95abacf00a5fd22c9eefcde2d3210
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):
25     if sysroot_path:
26         return namespace(
27             PKG_CONFIG_SYSROOT_DIR=sysroot_path,
28             PKG_CONFIG_LIBDIR=":".join(
29                 os.path.join(sysroot_path, d)
30                 for d in (
31                     "usr/lib/pkgconfig",
32                     "usr/lib/{}/pkgconfig".format(multiarch_dir),
33                     "usr/share/pkgconfig",
34                 )
35             ),
36         )
39 @depends(pkg_config_vars)
40 @imports(_from="os", _import="environ")
41 def pkg_config_env(vars):
42     if vars:
43         env = dict(environ)
44         env["PKG_CONFIG_SYSROOT_DIR"] = vars.PKG_CONFIG_SYSROOT_DIR
45         env["PKG_CONFIG_LIBDIR"] = vars.PKG_CONFIG_LIBDIR
46         return env
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.
69 @template
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):
82         min_version = "0.9.0"
83         if pkg_config is None:
84             die(
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."
88             )
89         if version < min_version:
90             die(
91                 "*** Your version of pkg-config is too old. You need version %s or newer.",
92                 min_version,
93             )
95     @depends(
96         pkg_config,
97         pkg_config_env,
98         package_desc,
99         allow_missing,
100         when=when_and_compile_environment,
101     )
102     @imports("sys")
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
106         # @checking here.
107         log.info("checking for %s... " % package_desc)
108         retcode, stdout, stderr = get_cmd_output(
109             pkg_config,
110             "--errors-to-stdout",
111             "--print-errors",
112             package_desc,
113             env=env,
114         )
115         if retcode == 0:
116             log.info("yes")
117             return True
118         log.info("no")
119         log_writer = log.warning if allow_missing else log.error
120         with LineIO(lambda l: log_writer(l)) as o:
121             o.write(stdout)
122         if not allow_missing:
123             sys.exit(1)
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)
142     if config:
143         set_config("%s_CFLAGS" % var, pkg_cflags)
144         set_config("%s_LIBS" % var, pkg_libs)
146     return pkg_info