| 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/. |
| 4 |
|
| 5 |
import os |
| 6 |
import subprocess |
| 7 |
|
| 8 |
import buildconfig |
| 9 |
import mozpack.path as mozpath |
| 10 |
import toml |
| 11 |
|
| 12 |
|
| 13 |
# Try to read the package name or otherwise assume same name as the crate path. |
| 14 |
def _get_crate_name(crate_path): |
| 15 |
try: |
| 16 |
with open(mozpath.join(crate_path, "Cargo.toml"), encoding="utf-8") as f: |
| 17 |
return toml.load(f)["package"]["name"] |
| 18 |
except Exception: |
| 19 |
return mozpath.basename(crate_path) |
| 20 |
|
| 21 |
|
| 22 |
CARGO_LOCK = mozpath.join(buildconfig.topsrcdir, "Cargo.lock") |
| 23 |
CARGO_TOML = mozpath.join(buildconfig.topsrcdir, "Cargo.toml") |
| 24 |
|
| 25 |
|
| 26 |
def _run_process(args): |
| 27 |
env = os.environ.copy() |
| 28 |
env["CARGO"] = str(buildconfig.substs["CARGO"]) |
| 29 |
env["RUSTC"] = str(buildconfig.substs["RUSTC"]) |
| 30 |
|
| 31 |
p = subprocess.Popen( |
| 32 |
args, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8" |
| 33 |
) |
| 34 |
|
| 35 |
stdout, stderr = p.communicate() |
| 36 |
if p.returncode != 0: |
| 37 |
print(stdout) |
| 38 |
print(stderr) |
| 39 |
return (stdout, p.returncode) |
| 40 |
|
| 41 |
|
| 42 |
def generate_metadata(output, cargo_config): |
| 43 |
args = [ |
| 44 |
buildconfig.substs["CARGO"], |
| 45 |
"metadata", |
| 46 |
"--all-features", |
| 47 |
"--format-version", |
| 48 |
"1", |
| 49 |
"--manifest-path", |
| 50 |
CARGO_TOML, |
| 51 |
] |
| 52 |
|
| 53 |
# The Spidermonkey library can be built from a package tarball outside the |
| 54 |
# tree, so we want to let Cargo create lock files in this case. When built |
| 55 |
# within a tree, the Rust dependencies have been vendored in so Cargo won't |
| 56 |
# touch the lock file. |
| 57 |
if not buildconfig.substs.get("JS_STANDALONE"): |
| 58 |
args.append("--frozen") |
| 59 |
|
| 60 |
stdout, returncode = _run_process(args) |
| 61 |
|
| 62 |
if returncode != 0: |
| 63 |
return returncode |
| 64 |
|
| 65 |
if stdout: |
| 66 |
output.write(stdout) |
| 67 |
|
| 68 |
# This is not quite accurate, but cbindgen only cares about a subset of the |
| 69 |
# data which, when changed, causes these files to change. |
| 70 |
return set([CARGO_LOCK, CARGO_TOML]) |
| 71 |
|
| 72 |
|
| 73 |
def generate(output, metadata_path, cbindgen_crate_path, *in_tree_dependencies): |
| 74 |
stdout, returncode = _run_process( |
| 75 |
[ |
| 76 |
buildconfig.substs["CBINDGEN"], |
| 77 |
buildconfig.topsrcdir, |
| 78 |
"--lockfile", |
| 79 |
CARGO_LOCK, |
| 80 |
"--crate", |
| 81 |
_get_crate_name(cbindgen_crate_path), |
| 82 |
"--metadata", |
| 83 |
metadata_path, |
| 84 |
"--cpp-compat", |
| 85 |
] |
| 86 |
) |
| 87 |
|
| 88 |
if returncode != 0: |
| 89 |
return returncode |
| 90 |
|
| 91 |
if stdout: |
| 92 |
output.write(stdout) |
| 93 |
|
| 94 |
deps = set() |
| 95 |
deps.add(CARGO_LOCK) |
| 96 |
deps.add(mozpath.join(cbindgen_crate_path, "cbindgen.toml")) |
| 97 |
for directory in in_tree_dependencies + (cbindgen_crate_path,): |
| 98 |
for path, dirs, files in os.walk(directory): |
| 99 |
for file in files: |
| 100 |
if os.path.splitext(file)[1] == ".rs": |
| 101 |
deps.add(mozpath.join(path, file)) |
| 102 |
|
| 103 |
return deps |