2018-08-17 16:43:06 +00:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
2022-11-24 17:23:47 +00:00
|
|
|
|
|
|
|
import buildconfig
|
|
|
|
import mozpack.path as mozpath
|
2022-12-20 23:01:26 +00:00
|
|
|
import toml
|
2019-03-30 20:18:33 +00:00
|
|
|
|
2020-02-28 14:07:15 +00:00
|
|
|
|
2019-03-30 20:18:33 +00:00
|
|
|
# Try to read the package name or otherwise assume same name as the crate path.
|
|
|
|
def _get_crate_name(crate_path):
|
|
|
|
try:
|
2022-02-18 15:40:47 +00:00
|
|
|
with open(mozpath.join(crate_path, "Cargo.toml"), encoding="utf-8") as f:
|
2022-12-20 23:01:26 +00:00
|
|
|
return toml.load(f)["package"]["name"]
|
2020-02-28 14:07:15 +00:00
|
|
|
except Exception:
|
2019-03-30 20:18:33 +00:00
|
|
|
return mozpath.basename(crate_path)
|
2018-08-17 16:43:06 +00:00
|
|
|
|
2020-02-28 14:07:15 +00:00
|
|
|
|
2018-08-17 16:43:06 +00:00
|
|
|
CARGO_LOCK = mozpath.join(buildconfig.topsrcdir, "Cargo.lock")
|
2020-06-26 10:41:26 +00:00
|
|
|
CARGO_TOML = mozpath.join(buildconfig.topsrcdir, "Cargo.toml")
|
2018-08-17 16:43:06 +00:00
|
|
|
|
2020-02-28 14:07:15 +00:00
|
|
|
|
2020-06-26 10:41:26 +00:00
|
|
|
def _run_process(args):
|
2018-08-17 16:43:06 +00:00
|
|
|
env = os.environ.copy()
|
|
|
|
env["CARGO"] = str(buildconfig.substs["CARGO"])
|
2020-03-24 22:59:26 +00:00
|
|
|
env["RUSTC"] = str(buildconfig.substs["RUSTC"])
|
2019-01-12 23:42:48 +00:00
|
|
|
|
2024-03-13 17:37:15 +00:00
|
|
|
p = subprocess.Popen(
|
|
|
|
args, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8"
|
|
|
|
)
|
2020-06-26 09:37:16 +00:00
|
|
|
|
2020-06-26 10:08:09 +00:00
|
|
|
stdout, stderr = p.communicate()
|
|
|
|
if p.returncode != 0:
|
|
|
|
print(stdout)
|
|
|
|
print(stderr)
|
2020-06-26 10:41:26 +00:00
|
|
|
return (stdout, p.returncode)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_metadata(output, cargo_config):
|
2024-02-06 11:41:13 +00:00
|
|
|
args = [
|
|
|
|
buildconfig.substs["CARGO"],
|
|
|
|
"metadata",
|
|
|
|
"--all-features",
|
|
|
|
"--format-version",
|
|
|
|
"1",
|
|
|
|
"--manifest-path",
|
|
|
|
CARGO_TOML,
|
|
|
|
]
|
|
|
|
|
|
|
|
# The Spidermonkey library can be built from a package tarball outside the
|
|
|
|
# tree, so we want to let Cargo create lock files in this case. When built
|
|
|
|
# within a tree, the Rust dependencies have been vendored in so Cargo won't
|
|
|
|
# touch the lock file.
|
|
|
|
if not buildconfig.substs.get("JS_STANDALONE"):
|
|
|
|
args.append("--frozen")
|
|
|
|
|
|
|
|
stdout, returncode = _run_process(args)
|
2020-06-26 10:41:26 +00:00
|
|
|
|
|
|
|
if returncode != 0:
|
|
|
|
return returncode
|
|
|
|
|
2024-03-13 17:37:15 +00:00
|
|
|
if stdout:
|
|
|
|
output.write(stdout)
|
2020-06-26 10:41:26 +00:00
|
|
|
|
|
|
|
# This is not quite accurate, but cbindgen only cares about a subset of the
|
|
|
|
# data which, when changed, causes these files to change.
|
|
|
|
return set([CARGO_LOCK, CARGO_TOML])
|
|
|
|
|
|
|
|
|
|
|
|
def generate(output, metadata_path, cbindgen_crate_path, *in_tree_dependencies):
|
|
|
|
stdout, returncode = _run_process(
|
|
|
|
[
|
|
|
|
buildconfig.substs["CBINDGEN"],
|
|
|
|
buildconfig.topsrcdir,
|
|
|
|
"--lockfile",
|
|
|
|
CARGO_LOCK,
|
|
|
|
"--crate",
|
|
|
|
_get_crate_name(cbindgen_crate_path),
|
|
|
|
"--metadata",
|
|
|
|
metadata_path,
|
|
|
|
"--cpp-compat",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
if returncode != 0:
|
|
|
|
return returncode
|
2019-01-05 15:42:47 +00:00
|
|
|
|
2024-03-13 17:37:15 +00:00
|
|
|
if stdout:
|
|
|
|
output.write(stdout)
|
2018-08-17 16:43:06 +00:00
|
|
|
|
|
|
|
deps = set()
|
|
|
|
deps.add(CARGO_LOCK)
|
2019-01-12 23:42:48 +00:00
|
|
|
deps.add(mozpath.join(cbindgen_crate_path, "cbindgen.toml"))
|
|
|
|
for directory in in_tree_dependencies + (cbindgen_crate_path,):
|
|
|
|
for path, dirs, files in os.walk(directory):
|
|
|
|
for file in files:
|
|
|
|
if os.path.splitext(file)[1] == ".rs":
|
|
|
|
deps.add(mozpath.join(path, file))
|
2019-01-05 15:42:47 +00:00
|
|
|
|
2018-08-17 16:43:06 +00:00
|
|
|
return deps
|