diff --git a/Cargo.lock b/Cargo.lock index 02d2e5edf2..e677d435a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -132,6 +132,12 @@ version = "3.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37ccbd214614c6783386c1af30caf03192f17891059cecc394b4fb119e363de3" +[[package]] +name = "bytes" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db" + [[package]] name = "c2rust" version = "0.16.0" @@ -238,6 +244,7 @@ dependencies = [ "lazy_static", "log", "rustc-private-link", + "toml_edit", ] [[package]] @@ -467,6 +474,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "combine" +version = "4.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a604e93b79d1808327a6fca85a6f2d69de66461e7620f5a4cbf5fb4d1d7c948" +dependencies = [ + "bytes", + "memchr", +] + [[package]] name = "console" version = "0.15.1" @@ -1335,6 +1352,17 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42657b1a6f4d817cda8e7a0ace261fe0cc946cf3a80314390b22cc61ae080792" +[[package]] +name = "toml_edit" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5376256e44f2443f8896ac012507c19a012df0fe8758b55246ae51a2279db51f" +dependencies = [ + "combine", + "indexmap", + "itertools", +] + [[package]] name = "tracing" version = "0.1.36" diff --git a/c2rust-transpile/rust-toolchain.toml b/c2rust-transpile/rust-toolchain.toml new file mode 120000 index 0000000000..43fe0ada68 --- /dev/null +++ b/c2rust-transpile/rust-toolchain.toml @@ -0,0 +1 @@ +../rust-toolchain.toml \ No newline at end of file diff --git a/c2rust-transpile/src/build_files/mod.rs b/c2rust-transpile/src/build_files/mod.rs index 47527b443d..2f9dea8afe 100644 --- a/c2rust-transpile/src/build_files/mod.rs +++ b/c2rust-transpile/src/build_files/mod.rs @@ -259,8 +259,8 @@ fn emit_lib_rs( /// If we translate variadic functions, the output will only compile /// on a nightly toolchain until the `c_variadics` feature is stable. fn emit_rust_toolchain(tcfg: &TranspilerConfig, build_dir: &Path) { - let output_path = build_dir.join("rust-toolchain"); - let output = include_str!("../../../rust-toolchain").to_string(); + let output_path = build_dir.join("rust-toolchain.toml"); + let output = include_str!("../../rust-toolchain.toml").to_string(); maybe_write_to_file(&output_path, output, tcfg.overwrite_existing); } diff --git a/c2rust/rust-toolchain.toml b/c2rust/rust-toolchain.toml new file mode 120000 index 0000000000..43fe0ada68 --- /dev/null +++ b/c2rust/rust-toolchain.toml @@ -0,0 +1 @@ +../rust-toolchain.toml \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile index 9803878e1d..b800e54898 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,6 +1,6 @@ # Docker image for c2rust CI and containerized development. # Multi-distro via optional BASE_IMAGE argument. -# RUST_VER argument should be set to the contents of the rust-toolchain file in +# RUST_VER argument should be set to the channel of the rust-toolchain.toml file in # the repo root (because dockerfiles can't access files above the location where # `docker build` is run). diff --git a/docs/README-developers.md b/docs/README-developers.md index c2ca482c95..fe4c02d553 100644 --- a/docs/README-developers.md +++ b/docs/README-developers.md @@ -27,7 +27,7 @@ Then run `./scripts/provision_mac.sh`. - `python-dev` - `python` >= 3.6 - [python dependencies](../scripts/requirements.txt) - - `rustc` [version](../rust-toolchain) + - `rustc` [version](../rust-toolchain.toml) - `rustfmt-preview` component for the above `rustc` version - `libssl` (development library, dependency of the refactoring tool) diff --git a/dynamic_instrumentation/Cargo.toml b/dynamic_instrumentation/Cargo.toml index ffeae2414d..ebd8170131 100644 --- a/dynamic_instrumentation/Cargo.toml +++ b/dynamic_instrumentation/Cargo.toml @@ -15,6 +15,9 @@ fs-err = "2" clap = { version = "3.2", features = ["derive"] } cargo_metadata = "0.15" camino = "1.0" +# Used for parsing `rust-toolchain.toml`. +# We don't need to edit at all, but `cargo` uses `toml-edit`, so we want to match it. +toml_edit = "0.14" [build-dependencies] rustc-private-link = { path = "../rustc-private-link" } diff --git a/dynamic_instrumentation/rust-toolchain b/dynamic_instrumentation/rust-toolchain deleted file mode 120000 index 9327ba4034..0000000000 --- a/dynamic_instrumentation/rust-toolchain +++ /dev/null @@ -1 +0,0 @@ -../rust-toolchain \ No newline at end of file diff --git a/dynamic_instrumentation/rust-toolchain.toml b/dynamic_instrumentation/rust-toolchain.toml new file mode 120000 index 0000000000..43fe0ada68 --- /dev/null +++ b/dynamic_instrumentation/rust-toolchain.toml @@ -0,0 +1 @@ +../rust-toolchain.toml \ No newline at end of file diff --git a/dynamic_instrumentation/src/main.rs b/dynamic_instrumentation/src/main.rs index 36be1a88b5..c12d6bfdcd 100644 --- a/dynamic_instrumentation/src/main.rs +++ b/dynamic_instrumentation/src/main.rs @@ -268,6 +268,19 @@ fn rustc_wrapper() -> anyhow::Result<()> { Ok(()) } +/// Set `$RUST_TOOLCHAIN` to the toolchain channel specified in `rust-toolchain.toml`. +/// This ensures that we use a toolchain compatible with the `rustc` private crates that we linked to. +fn set_rust_toolchain() -> anyhow::Result<()> { + let toml = include_str!("../rust-toolchain.toml"); + // Couldn't find an `include_toml!` macro to do this at compile time. + let doc = toml.parse::()?; + let channel = doc["toolchain"]["channel"].as_str(); + if let Some(toolchain) = channel { + env::set_var("RUSTUP_TOOLCHAIN", toolchain); + } + Ok(()) +} + /// Run as a `cargo` wrapper/plugin, the default invocation. fn cargo_wrapper(rustc_wrapper: &Path) -> anyhow::Result<()> { let Args { @@ -275,8 +288,7 @@ fn cargo_wrapper(rustc_wrapper: &Path) -> anyhow::Result<()> { mut cargo_args, } = Args::parse(); - // Ensure we use a toolchain compatible with the `rustc` private crates we linked to. - env::set_var("RUSTUP_TOOLCHAIN", include_str!("../rust-toolchain").trim()); + set_rust_toolchain()?; // Resolve the sysroot once in the [`cargo_wrapper`] // so that we don't need all of the [`rustc_wrapper`]s to have to do it. diff --git a/examples/robotfindskitten/repo/rust/rust-toolchain b/examples/robotfindskitten/repo/rust/rust-toolchain deleted file mode 100644 index fc40182b3c..0000000000 --- a/examples/robotfindskitten/repo/rust/rust-toolchain +++ /dev/null @@ -1 +0,0 @@ -nightly-2019-12-05 diff --git a/examples/robotfindskitten/repo/rust/rust-toolchain.toml b/examples/robotfindskitten/repo/rust/rust-toolchain.toml new file mode 100644 index 0000000000..5eb4c1c9dc --- /dev/null +++ b/examples/robotfindskitten/repo/rust/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly-2022-02-14" diff --git a/manual/rust-toolchain b/manual/rust-toolchain deleted file mode 120000 index 9327ba4034..0000000000 --- a/manual/rust-toolchain +++ /dev/null @@ -1 +0,0 @@ -../rust-toolchain \ No newline at end of file diff --git a/manual/rust-toolchain.toml b/manual/rust-toolchain.toml new file mode 120000 index 0000000000..43fe0ada68 --- /dev/null +++ b/manual/rust-toolchain.toml @@ -0,0 +1 @@ +../rust-toolchain.toml \ No newline at end of file diff --git a/rust-toolchain b/rust-toolchain deleted file mode 100644 index f24eb00eda..0000000000 --- a/rust-toolchain +++ /dev/null @@ -1 +0,0 @@ -nightly-2022-02-14 diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000000..72a896fdd2 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "nightly-2022-02-14" +components = ["rustfmt-preview", "rustc-dev", "rust-src"] diff --git a/scripts/common.py b/scripts/common.py index 647e192e8e..f61140a334 100644 --- a/scripts/common.py +++ b/scripts/common.py @@ -11,11 +11,13 @@ import platform import multiprocessing -from typing import List, Callable +from pathlib import Path +from typing import List import plumbum as pb from plumbum.machines import LocalCommand as Command +from query_toml import query_toml class Colors: @@ -99,9 +101,7 @@ class Config: CC_DB_JSON = "compile_commands.json" - # Look up rust toolchain from repo root - with open(os.path.join(ROOT_DIR, "rust-toolchain")) as fh: - CUSTOM_RUST_NAME = fh.readline().strip() + CUSTOM_RUST_NAME = query_toml(path=Path(ROOT_DIR).joinpath("rust-toolchain.toml"), query=("toolchain", "channel")) LLVM_SKIP_SIGNATURE_CHECKS = False diff --git a/scripts/docker_build.sh b/scripts/docker_build.sh index 71c761837b..26e4c08d76 100755 --- a/scripts/docker_build.sh +++ b/scripts/docker_build.sh @@ -27,9 +27,9 @@ build_image() { BASE_IMAGE=${1} IMAGE_TAG=$(echo ${BASE_IMAGE} | tr -s :/ - ) # replace colons and slashes with hyphens - # pull the rust version out of ../rust-toolchain to keep things synched - RUST_TOOLCHAIN_FILE="$SCRIPT_DIR/../rust-toolchain" - RUST_VER=$(cat $RUST_TOOLCHAIN_FILE | tr -d '\n') + # pull the rust version out of ../rust-toolchain.toml to keep things synced + RUST_TOOLCHAIN_FILE="$SCRIPT_DIR/../rust-toolchain.toml" + RUST_VER=$($SCRIPT_DIR/query_toml.py toolchain.channel $RUST_TOOLCHAIN_FILE) docker pull "$BASE_IMAGE" docker build -f $SCRIPT_DIR/../docker/Dockerfile \ diff --git a/scripts/provision_mac.sh b/scripts/provision_mac.sh index d6c1e64958..8530c20718 100755 --- a/scripts/provision_mac.sh +++ b/scripts/provision_mac.sh @@ -34,10 +34,7 @@ type -P "pip3" >/dev/null || { pip3 install --user --upgrade pip pip3 install -r "$SCRIPT_DIR/requirements.txt" --user --disable-pip-version-check -RUST_TOOLCHAIN_FILE="$SCRIPT_DIR/../rust-toolchain" -export RUST_VER=$(cat $RUST_TOOLCHAIN_FILE | tr -d '\n') - # Rust and dependencies -RUST_TOOLCHAIN_FILE="$SCRIPT_DIR/../rust-toolchain" -export RUST_VER=$(cat $RUST_TOOLCHAIN_FILE | tr -d '\n') +RUST_TOOLCHAIN_FILE="$SCRIPT_DIR/../rust-toolchain.toml" +export RUST_VER=$($SCRIPT_DIR/query_toml.py toolchain.channel $RUST_TOOLCHAIN_FILE) "$SCRIPT_DIR/provision_rust.sh" diff --git a/scripts/query_toml.py b/scripts/query_toml.py new file mode 100755 index 0000000000..ec90c6f9f0 --- /dev/null +++ b/scripts/query_toml.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +from argparse import ArgumentParser +from pathlib import Path +from typing import Any, Iterable +import toml + + +def query_toml(path: Path, query: Iterable[str]) -> Any: + result = toml.load(path) + for field in query: + if isinstance(result, list): + field = int(field) + result = result[field] + return result + + +def main(): + parser = ArgumentParser() + parser.add_argument("query", type=str, help="the TOML query, with fields separated by .") + parser.add_argument("path", type=Path, help="the path to a TOML file") + args = parser.parse_args() + print(query_toml(path=args.path, query=args.query.split("."))) + + +if __name__ == "__main__": + main() diff --git a/scripts/requirements.txt b/scripts/requirements.txt index 4a9af6ff4f..39999614fe 100644 --- a/scripts/requirements.txt +++ b/scripts/requirements.txt @@ -9,3 +9,4 @@ pygments typing;python_version<"3.5" scan-build pyyaml +toml diff --git a/scripts/test_rust_refactor.py b/scripts/test_rust_refactor.py index 6b79dd321b..20f79b2c46 100755 --- a/scripts/test_rust_refactor.py +++ b/scripts/test_rust_refactor.py @@ -91,7 +91,7 @@ def main(): setup_logging() # NOTE: it seems safe to disable this check since we now - # that we use a rust-toolchain file for rustc versioning. + # that we use a rust-toolchain.toml file for rustc versioning. # ensure_rustc_version(c.CUSTOM_RUST_RUSTC_VERSION) # TODO: update rustfmt version check once c2rust-refactor bitrot has been fixed # ensure_rustfmt_version() diff --git a/scripts/test_translator.py b/scripts/test_translator.py index 1205e508da..7a02d3adc9 100755 --- a/scripts/test_translator.py +++ b/scripts/test_translator.py @@ -422,7 +422,7 @@ def run(self) -> List[TestOutcome]: self.generated_files["rust_src"].append(self.full_path + "/src/Cargo.toml") self.generated_files["rust_src"].append(self.full_path + "/src/build.rs") self.generated_files["rust_src"].append(self.full_path + "/src/c2rust-lib.rs") - self.generated_files["rust_src"].append(self.full_path + "/src/rust-toolchain") + self.generated_files["rust_src"].append(self.full_path + "/src/rust-toolchain.toml") _, rust_file_short = os.path.split(translated_rust_file.path) extensionless_rust_file, _ = os.path.splitext(rust_file_short) @@ -657,7 +657,7 @@ def main() -> None: die(msg, errno.ENOENT) # NOTE: it seems safe to disable this check since we now - # that we use a rust-toolchain file for rustc versioning. + # that we use a rust-toolchain.toml file for rustc versioning. # ensure_rustc_version(c.CUSTOM_RUST_RUSTC_VERSION) if not test_directories: