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 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")) as f
:
18 return pytoml
.load(f
)["package"]["name"]
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'],
38 _get_crate_name(cbindgen_crate_path
),
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
)
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
):
58 if os
.path
.splitext(file)[1] == ".rs":
59 deps
.add(mozpath
.join(path
, file))
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
,
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
77 def generate_with_same_crate(output
, cbindgen_crate_path
,
78 *in_tree_dependencies
):
79 return _generate(output
, cbindgen_crate_path
, cbindgen_crate_path
,