Bug 1804798 - Explicitly set auto page name (and corresponding debug flag) when inser...
[gecko.git] / build / RunCbindgen.py
blob835bea5621c611a8e07ebae2ccd2ba214eb1e02a
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
7 import os
8 import subprocess
10 import buildconfig
11 import mozpack.path as mozpath
12 import pytoml
13 import six
16 # Try to read the package name or otherwise assume same name as the crate path.
17 def _get_crate_name(crate_path):
18 try:
19 with open(mozpath.join(crate_path, "Cargo.toml"), encoding="utf-8") as f:
20 return pytoml.load(f)["package"]["name"]
21 except Exception:
22 return mozpath.basename(crate_path)
25 CARGO_LOCK = mozpath.join(buildconfig.topsrcdir, "Cargo.lock")
26 CARGO_TOML = mozpath.join(buildconfig.topsrcdir, "Cargo.toml")
29 def _run_process(args):
30 env = os.environ.copy()
31 env["CARGO"] = str(buildconfig.substs["CARGO"])
32 env["RUSTC"] = str(buildconfig.substs["RUSTC"])
34 p = subprocess.Popen(args, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
36 stdout, stderr = p.communicate()
37 stdout = six.ensure_text(stdout)
38 stderr = six.ensure_text(stderr)
39 if p.returncode != 0:
40 print(stdout)
41 print(stderr)
42 return (stdout, p.returncode)
45 def generate_metadata(output, cargo_config):
46 stdout, returncode = _run_process(
48 buildconfig.substs["CARGO"],
49 "metadata",
50 "--frozen",
51 "--all-features",
52 "--format-version",
53 "1",
54 "--manifest-path",
55 CARGO_TOML,
59 if returncode != 0:
60 return returncode
62 output.write(stdout)
64 # This is not quite accurate, but cbindgen only cares about a subset of the
65 # data which, when changed, causes these files to change.
66 return set([CARGO_LOCK, CARGO_TOML])
69 def generate(output, metadata_path, cbindgen_crate_path, *in_tree_dependencies):
70 stdout, returncode = _run_process(
72 buildconfig.substs["CBINDGEN"],
73 buildconfig.topsrcdir,
74 "--lockfile",
75 CARGO_LOCK,
76 "--crate",
77 _get_crate_name(cbindgen_crate_path),
78 "--metadata",
79 metadata_path,
80 "--cpp-compat",
84 if returncode != 0:
85 return returncode
87 output.write(stdout)
89 deps = set()
90 deps.add(CARGO_LOCK)
91 deps.add(mozpath.join(cbindgen_crate_path, "cbindgen.toml"))
92 for directory in in_tree_dependencies + (cbindgen_crate_path,):
93 for path, dirs, files in os.walk(directory):
94 for file in files:
95 if os.path.splitext(file)[1] == ".rs":
96 deps.add(mozpath.join(path, file))
98 return deps