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
11 import mozpack
.path
as mozpath
16 # Try to read the package name or otherwise assume same name as the crate path.
17 def _get_crate_name(crate_path
):
19 with
open(mozpath
.join(crate_path
, "Cargo.toml"), encoding
="utf-8") as f
:
20 return pytoml
.load(f
)["package"]["name"]
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
)
42 return (stdout
, p
.returncode
)
45 def generate_metadata(output
, cargo_config
):
46 stdout
, returncode
= _run_process(
48 buildconfig
.substs
["CARGO"],
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
,
77 _get_crate_name(cbindgen_crate_path
),
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
):
95 if os
.path
.splitext(file)[1] == ".rs":
96 deps
.add(mozpath
.join(path
, file))