Bug 1860146 [wpt PR 42644] - Add CSS filter string support as parameter for beginLaye...
[gecko.git] / build / RunCbindgen.py
blobf797cad385d7a634038ef49f5971f33bca37bfc8
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 import os
6 import subprocess
8 import buildconfig
9 import mozpack.path as mozpath
10 import six
11 import toml
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"), encoding="utf-8") as f:
18 return toml.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 "--frozen",
49 "--all-features",
50 "--format-version",
51 "1",
52 "--manifest-path",
53 CARGO_TOML,
57 if returncode != 0:
58 return returncode
60 output.write(stdout)
62 # This is not quite accurate, but cbindgen only cares about a subset of the
63 # data which, when changed, causes these files to change.
64 return set([CARGO_LOCK, CARGO_TOML])
67 def generate(output, metadata_path, cbindgen_crate_path, *in_tree_dependencies):
68 stdout, returncode = _run_process(
70 buildconfig.substs["CBINDGEN"],
71 buildconfig.topsrcdir,
72 "--lockfile",
73 CARGO_LOCK,
74 "--crate",
75 _get_crate_name(cbindgen_crate_path),
76 "--metadata",
77 metadata_path,
78 "--cpp-compat",
82 if returncode != 0:
83 return returncode
85 output.write(stdout)
87 deps = set()
88 deps.add(CARGO_LOCK)
89 deps.add(mozpath.join(cbindgen_crate_path, "cbindgen.toml"))
90 for directory in in_tree_dependencies + (cbindgen_crate_path,):
91 for path, dirs, files in os.walk(directory):
92 for file in files:
93 if os.path.splitext(file)[1] == ".rs":
94 deps.add(mozpath.join(path, file))
96 return deps