Bug 1686610 [wpt PR 27178] - Update <link> pseudo selector WPTs, a=testonly
[gecko.git] / build / RunCbindgen.py
blob0c941eaaca2138639c697f439dcbbd3482f5f674
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 from __future__ import print_function
6 import buildconfig
7 import mozpack.path as mozpath
8 import os
9 import six
10 import subprocess
11 import pytoml
14 # Try to read the package name or otherwise assume same name as the crate path.
15 def _get_crate_name(crate_path):
16 try:
17 with open(mozpath.join(crate_path, "Cargo.toml")) as f:
18 return pytoml.load(f)["package"]["name"]
19 except Exception:
20 return mozpath.basename(crate_path)
23 CARGO_LOCK = mozpath.join(buildconfig.topsrcdir, "Cargo.lock")
24 CARGO_TOML = mozpath.join(buildconfig.topsrcdir, "Cargo.toml")
27 def _run_process(args):
28 env = os.environ.copy()
29 env["CARGO"] = str(buildconfig.substs["CARGO"])
30 env["RUSTC"] = str(buildconfig.substs["RUSTC"])
32 p = subprocess.Popen(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
34 stdout, stderr = p.communicate()
35 stdout = six.ensure_text(stdout)
36 stderr = six.ensure_text(stderr)
37 if p.returncode != 0:
38 print(stdout)
39 print(stderr)
40 return (stdout, p.returncode)
43 def generate_metadata(output, cargo_config):
44 stdout, returncode = _run_process(
46 buildconfig.substs["CARGO"],
47 "metadata",
48 "--all-features",
49 "--format-version",
50 "1",
51 "--manifest-path",
52 CARGO_TOML,
56 if returncode != 0:
57 return returncode
59 output.write(stdout)
61 # This is not quite accurate, but cbindgen only cares about a subset of the
62 # data which, when changed, causes these files to change.
63 return set([CARGO_LOCK, CARGO_TOML])
66 def generate(output, metadata_path, cbindgen_crate_path, *in_tree_dependencies):
67 stdout, returncode = _run_process(
69 buildconfig.substs["CBINDGEN"],
70 buildconfig.topsrcdir,
71 "--lockfile",
72 CARGO_LOCK,
73 "--crate",
74 _get_crate_name(cbindgen_crate_path),
75 "--metadata",
76 metadata_path,
77 "--cpp-compat",
81 if returncode != 0:
82 return returncode
84 output.write(stdout)
86 deps = set()
87 deps.add(CARGO_LOCK)
88 deps.add(mozpath.join(cbindgen_crate_path, "cbindgen.toml"))
89 for directory in in_tree_dependencies + (cbindgen_crate_path,):
90 for path, dirs, files in os.walk(directory):
91 for file in files:
92 if os.path.splitext(file)[1] == ".rs":
93 deps.add(mozpath.join(path, file))
95 return deps