Bug 1642744 [wpt PR 23920] - [ScrollTimeline] Update compositor timeline from blink...
[gecko.git] / build / RunCbindgen.py
blobed1b37b274a378999e964d937c64a72d8e87542f
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")
26 def _generate(output, cbindgen_crate_path, metadata_crate_path,
27 in_tree_dependencies):
28 env = os.environ.copy()
29 env['CARGO'] = str(buildconfig.substs['CARGO'])
30 env['RUSTC'] = str(buildconfig.substs['RUSTC'])
32 p = subprocess.Popen([
33 buildconfig.substs['CBINDGEN'],
34 metadata_crate_path,
35 "--lockfile",
36 CARGO_LOCK,
37 "--crate",
38 _get_crate_name(cbindgen_crate_path),
39 "--cpp-compat"
40 ], env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
42 stdout, stderr = p.communicate()
43 stdout = six.ensure_text(stdout)
44 stderr = six.ensure_text(stderr)
45 if p.returncode != 0:
46 print(stdout)
47 print(stderr)
48 return p.returncode
50 output.write(stdout)
52 deps = set()
53 deps.add(CARGO_LOCK)
54 deps.add(mozpath.join(cbindgen_crate_path, "cbindgen.toml"))
55 for directory in in_tree_dependencies + (cbindgen_crate_path,):
56 for path, dirs, files in os.walk(directory):
57 for file in files:
58 if os.path.splitext(file)[1] == ".rs":
59 deps.add(mozpath.join(path, file))
61 return deps
64 def generate(output, cbindgen_crate_path, *in_tree_dependencies):
65 metadata_crate_path = mozpath.join(buildconfig.topsrcdir,
66 "toolkit", "library", "rust")
67 return _generate(output, cbindgen_crate_path, metadata_crate_path,
68 in_tree_dependencies)
71 # Use the binding's crate directory instead of toolkit/library/rust as
72 # the metadata crate directory.
74 # This is necessary for the bindings inside SpiderMonkey, given that
75 # SpiderMonkey tarball doesn't contain toolkit/library/rust and its
76 # dependencies.
77 def generate_with_same_crate(output, cbindgen_crate_path,
78 *in_tree_dependencies):
79 return _generate(output, cbindgen_crate_path, cbindgen_crate_path,
80 in_tree_dependencies)