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/.
9 import mozpack
.path
as mozpath
14 # Try to read the package name or otherwise assume same name as the crate path.
15 def _get_crate_name(crate_path
):
17 with
open(mozpath
.join(crate_path
, "Cargo.toml"), encoding
="utf-8") as f
:
18 return toml
.load(f
)["package"]["name"]
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
)
40 return (stdout
, p
.returncode
)
43 def generate_metadata(output
, cargo_config
):
45 buildconfig
.substs
["CARGO"],
54 # The Spidermonkey library can be built from a package tarball outside the
55 # tree, so we want to let Cargo create lock files in this case. When built
56 # within a tree, the Rust dependencies have been vendored in so Cargo won't
57 # touch the lock file.
58 if not buildconfig
.substs
.get("JS_STANDALONE"):
59 args
.append("--frozen")
61 stdout
, returncode
= _run_process(args
)
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
])
73 def generate(output
, metadata_path
, cbindgen_crate_path
, *in_tree_dependencies
):
74 stdout
, returncode
= _run_process(
76 buildconfig
.substs
["CBINDGEN"],
77 buildconfig
.topsrcdir
,
81 _get_crate_name(cbindgen_crate_path
),
95 deps
.add(mozpath
.join(cbindgen_crate_path
, "cbindgen.toml"))
96 for directory
in in_tree_dependencies
+ (cbindgen_crate_path
,):
97 for path
, dirs
, files
in os
.walk(directory
):
99 if os
.path
.splitext(file)[1] == ".rs":
100 deps
.add(mozpath
.join(path
, file))