From d0b8896189f5b2fb472d54ed389681dff97d907b Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 15 Feb 2023 18:23:11 +0000 Subject: [PATCH] Allow building the build system using cargo Rust's build system only handles cargo, not rustc. --- .cirrus.yml | 4 ++-- .github/workflows/abi-cafe.yml | 6 +++--- .github/workflows/main.yml | 22 ++++++++++----------- .github/workflows/rustc.yml | 4 ++-- .gitignore | 1 + .vscode/settings.json | 3 ++- Cargo.toml | 8 -------- Readme.md | 8 ++++---- build_system/Cargo.lock | 7 +++++++ build_system/Cargo.toml | 13 +++++++++++++ build_system/build_sysroot.rs | 4 ++-- build_system/{mod.rs => main.rs} | 10 +++++++--- build_system/usage.txt | 16 ++++++++-------- clean_all.sh | 2 +- docs/usage.md | 2 +- scripts/rustup.sh | 2 +- scripts/setup_rust_fork.sh | 2 +- test.sh | 2 +- y.rs | 33 ++------------------------------ y.sh | 6 ++++++ 20 files changed, 75 insertions(+), 80 deletions(-) create mode 100644 build_system/Cargo.lock create mode 100644 build_system/Cargo.toml rename build_system/{mod.rs => main.rs} (98%) create mode 100755 y.sh diff --git a/.cirrus.yml b/.cirrus.yml index 1405ea74903..8b4efd4e394 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -10,7 +10,7 @@ task: folder: target prepare_script: - . $HOME/.cargo/env - - ./y.rs prepare + - ./y.sh prepare test_script: - . $HOME/.cargo/env - - ./y.rs test + - ./y.sh test diff --git a/.github/workflows/abi-cafe.yml b/.github/workflows/abi-cafe.yml index 3c40555669c..12aa69d3c79 100644 --- a/.github/workflows/abi-cafe.yml +++ b/.github/workflows/abi-cafe.yml @@ -46,12 +46,12 @@ jobs: run: rustup set default-host x86_64-pc-windows-gnu - name: Prepare dependencies - run: ./y.rs prepare + run: ./y.sh prepare - name: Build - run: ./y.rs build --sysroot none + run: ./y.sh build --sysroot none - name: Test abi-cafe env: TARGET_TRIPLE: ${{ matrix.env.TARGET_TRIPLE }} - run: ./y.rs abi-cafe + run: ./y.sh abi-cafe diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index abcd1affdee..8e6c1e8ade0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -19,7 +19,7 @@ jobs: - name: Rustfmt run: | cargo fmt --check - rustfmt --check build_system/mod.rs + rustfmt --check build_system/main.rs rustfmt --check example/* @@ -91,15 +91,15 @@ jobs: sudo apt-get install -y gcc-s390x-linux-gnu qemu-user - name: Prepare dependencies - run: ./y.rs prepare + run: ./y.sh prepare - name: Build - run: ./y.rs build --sysroot none + run: ./y.sh build --sysroot none - name: Test env: TARGET_TRIPLE: ${{ matrix.env.TARGET_TRIPLE }} - run: ./y.rs test + run: ./y.sh test - name: Install LLVM standard library run: rustup target add ${{ matrix.env.TARGET_TRIPLE }} @@ -111,7 +111,7 @@ jobs: if: matrix.os != 'windows-latest' || matrix.env.TARGET_TRIPLE != 'x86_64-pc-windows-gnu' env: TARGET_TRIPLE: ${{ matrix.env.TARGET_TRIPLE }} - run: ./y.rs test --sysroot llvm --no-unstable-features + run: ./y.sh test --sysroot llvm --no-unstable-features # This job doesn't use cg_clif in any way. It checks that all cg_clif tests work with cg_llvm too. @@ -165,13 +165,13 @@ jobs: run: cargo install hyperfine || true - name: Prepare dependencies - run: ./y.rs prepare + run: ./y.sh prepare - name: Build - run: CI_OPT=1 ./y.rs build --sysroot none + run: CI_OPT=1 ./y.sh build --sysroot none - name: Benchmark - run: CI_OPT=1 ./y.rs bench + run: CI_OPT=1 ./y.sh bench dist: @@ -224,13 +224,13 @@ jobs: sudo apt-get install -y gcc-mingw-w64-x86-64 wine-stable - name: Prepare dependencies - run: ./y.rs prepare + run: ./y.sh prepare - name: Build backend - run: CI_OPT=1 ./y.rs build --sysroot none + run: CI_OPT=1 ./y.sh build --sysroot none - name: Build sysroot - run: CI_OPT=1 ./y.rs build + run: CI_OPT=1 ./y.sh build - name: Package prebuilt cg_clif run: tar cvfJ cg_clif.tar.xz dist diff --git a/.github/workflows/rustc.yml b/.github/workflows/rustc.yml index b2f772c4fc4..b49dc3aff7a 100644 --- a/.github/workflows/rustc.yml +++ b/.github/workflows/rustc.yml @@ -18,7 +18,7 @@ jobs: key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }} - name: Prepare dependencies - run: ./y.rs prepare + run: ./y.sh prepare - name: Test run: ./scripts/test_bootstrap.sh @@ -38,7 +38,7 @@ jobs: key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }} - name: Prepare dependencies - run: ./y.rs prepare + run: ./y.sh prepare - name: Test run: ./scripts/test_rustc_tests.sh diff --git a/.gitignore b/.gitignore index e5d10a937ae..e6ac8c8408d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /target +/build_system/target **/*.rs.bk *.rlib *.o diff --git a/.vscode/settings.json b/.vscode/settings.json index 7c8703cba50..60cb51d5663 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,9 +6,10 @@ "rust-analyzer.imports.granularity.enforce": true, "rust-analyzer.imports.granularity.group": "module", "rust-analyzer.imports.prefix": "crate", - "rust-analyzer.cargo.features": ["unstable-features", "__check_build_system_using_ra"], + "rust-analyzer.cargo.features": ["unstable-features"], "rust-analyzer.linkedProjects": [ "./Cargo.toml", + "./build_system/Cargo.toml", { "crates": [ { diff --git a/Cargo.toml b/Cargo.toml index 145b1150423..1c1f2d8577b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,13 +3,6 @@ name = "rustc_codegen_cranelift" version = "0.1.0" edition = "2021" -[[bin]] -# This is used just to teach rust-analyzer how to check the build system. required-features is used -# to disable it for regular builds. -name = "y" -path = "./y.rs" -required-features = ["__check_build_system_using_ra"] - [lib] crate-type = ["dylib"] @@ -45,7 +38,6 @@ smallvec = "1.8.1" unstable-features = ["jit", "inline_asm"] jit = ["cranelift-jit", "libloading"] inline_asm = [] -__check_build_system_using_ra = [] [package.metadata.rust-analyzer] rustc_private = true diff --git a/Readme.md b/Readme.md index 7e9945578a5..9469feea0cb 100644 --- a/Readme.md +++ b/Readme.md @@ -10,8 +10,8 @@ If not please open an issue. ```bash $ git clone https://github.com/bjorn3/rustc_codegen_cranelift $ cd rustc_codegen_cranelift -$ ./y.rs prepare -$ ./y.rs build +$ ./y.sh prepare +$ ./y.sh build ``` To run the test suite replace the last command with: @@ -20,7 +20,7 @@ To run the test suite replace the last command with: $ ./test.sh ``` -For more docs on how to build and test see [build_system/usage.txt](build_system/usage.txt) or the help message of `./y.rs`. +For more docs on how to build and test see [build_system/usage.txt](build_system/usage.txt) or the help message of `./y.sh`. ## Precompiled builds @@ -35,7 +35,7 @@ If you want to use `cargo clif build` instead of having to specify the full path rustc_codegen_cranelift can be used as a near-drop-in replacement for `cargo build` or `cargo run` for existing projects. -Assuming `$cg_clif_dir` is the directory you cloned this repo into and you followed the instructions (`y.rs prepare` and `y.rs build` or `test.sh`). +Assuming `$cg_clif_dir` is the directory you cloned this repo into and you followed the instructions (`y.sh prepare` and `y.sh build` or `test.sh`). In the directory with your project (where you can do the usual `cargo build`), run: diff --git a/build_system/Cargo.lock b/build_system/Cargo.lock new file mode 100644 index 00000000000..86268e19160 --- /dev/null +++ b/build_system/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "y" +version = "0.1.0" diff --git a/build_system/Cargo.toml b/build_system/Cargo.toml new file mode 100644 index 00000000000..f47b9bc5540 --- /dev/null +++ b/build_system/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "y" +version = "0.1.0" +edition = "2021" + +[[bin]] +name = "y" +path = "main.rs" + +[features] +unstable-features = [] # for rust-analyzer + +# Do not add any dependencies diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index dab9c77d1a4..2ed6272b2c5 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -225,7 +225,7 @@ fn build_clif_sysroot_for_triple( match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) { Err(e) => { eprintln!("Failed to get rustc version for patched sysroot source: {}", e); - eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source"); + eprintln!("Hint: Try `./y.sh prepare` to patch the sysroot source"); process::exit(1); } Ok(source_version) => { @@ -234,7 +234,7 @@ fn build_clif_sysroot_for_triple( eprintln!("The patched sysroot source is outdated"); eprintln!("Source version: {}", source_version.trim()); eprintln!("Rustc version: {}", rustc_version.trim()); - eprintln!("Hint: Try `./y.rs prepare` to update the patched sysroot source"); + eprintln!("Hint: Try `./y.sh prepare` to update the patched sysroot source"); process::exit(1); } } diff --git a/build_system/mod.rs b/build_system/main.rs similarity index 98% rename from build_system/mod.rs rename to build_system/main.rs index e1b46c3120e..06395eb141c 100644 --- a/build_system/mod.rs +++ b/build_system/main.rs @@ -1,3 +1,7 @@ +#![warn(rust_2018_idioms)] +#![warn(unused_lifetimes)] +#![warn(unreachable_pub)] + use std::env; use std::path::PathBuf; use std::process; @@ -37,19 +41,19 @@ enum Command { } #[derive(Copy, Clone, Debug)] -pub(crate) enum SysrootKind { +enum SysrootKind { None, Clif, Llvm, } #[derive(Clone, Debug)] -pub(crate) enum CodegenBackend { +enum CodegenBackend { Local(PathBuf), Builtin(String), } -pub(crate) fn main() { +fn main() { if env::var("RUST_BACKTRACE").is_err() { env::set_var("RUST_BACKTRACE", "1"); } diff --git a/build_system/usage.txt b/build_system/usage.txt index 98726ae5af1..9d20cdca6a7 100644 --- a/build_system/usage.txt +++ b/build_system/usage.txt @@ -1,11 +1,11 @@ The build system of cg_clif. USAGE: - ./y.rs prepare [--out-dir DIR] [--download-dir DIR] - ./y.rs build [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--download-dir DIR] [--no-unstable-features] [--frozen] - ./y.rs test [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--download-dir DIR] [--no-unstable-features] [--frozen] - ./y.rs abi-cafe [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--download-dir DIR] [--no-unstable-features] [--frozen] - ./y.rs bench [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--download-dir DIR] [--no-unstable-features] [--frozen] + ./y.sh prepare [--out-dir DIR] [--download-dir DIR] + ./y.sh build [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--download-dir DIR] [--no-unstable-features] [--frozen] + ./y.sh test [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--download-dir DIR] [--no-unstable-features] [--frozen] + ./y.sh abi-cafe [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--download-dir DIR] [--no-unstable-features] [--frozen] + ./y.sh bench [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--download-dir DIR] [--no-unstable-features] [--frozen] OPTIONS: --debug @@ -40,7 +40,7 @@ REQUIREMENTS: * Rustup: By default rustup is used to install the right nightly version. If you don't want to use rustup, you can manually install the nightly version indicated by rust-toolchain.toml and point the CARGO, RUSTC and RUSTDOC env vars to the right executables. - * Git: `./y.rs prepare` uses git for applying patches and on Windows for downloading test repos. - * Curl and tar (non-Windows only): Used by `./y.rs prepare` to download a single commit for + * Git: `./y.sh prepare` uses git for applying patches and on Windows for downloading test repos. + * Curl and tar (non-Windows only): Used by `./y.sh prepare` to download a single commit for repos. Git will be used to clone the whole repo when using Windows. - * [Hyperfine](https://github.com/sharkdp/hyperfine/): Used for benchmarking with `./y.rs bench`. + * [Hyperfine](https://github.com/sharkdp/hyperfine/): Used for benchmarking with `./y.sh bench`. diff --git a/clean_all.sh b/clean_all.sh index cdfc2e143e6..19405a53d1c 100755 --- a/clean_all.sh +++ b/clean_all.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -e -rm -rf target/ download/ build/ dist/ y.bin y.bin.dSYM y.exe y.pdb +rm -rf target/ build_system/target download/ build/ dist/ y.bin y.bin.dSYM y.exe y.pdb # Kept for now in case someone updates their checkout of cg_clif before running clean_all.sh # FIXME remove at some point in the future diff --git a/docs/usage.md b/docs/usage.md index 4c2b0fa1704..c6210f958d6 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -2,7 +2,7 @@ rustc_codegen_cranelift can be used as a near-drop-in replacement for `cargo build` or `cargo run` for existing projects. -Assuming `$cg_clif_dir` is the directory you cloned this repo into and you followed the instructions (`y.rs prepare` and `y.rs build` or `test.sh`). +Assuming `$cg_clif_dir` is the directory you cloned this repo into and you followed the instructions (`y.sh prepare` and `y.sh build` or `test.sh`). ## Cargo diff --git a/scripts/rustup.sh b/scripts/rustup.sh index 3cbeb6375de..6b446aafb7b 100755 --- a/scripts/rustup.sh +++ b/scripts/rustup.sh @@ -32,7 +32,7 @@ case $1 in ./clean_all.sh - ./y.rs prepare + ./y.sh prepare (cd download/sysroot && cargo update && cargo fetch && cp Cargo.lock ../../build_sysroot/) ;; diff --git a/scripts/setup_rust_fork.sh b/scripts/setup_rust_fork.sh index abb09775d21..15b16b42be5 100644 --- a/scripts/setup_rust_fork.sh +++ b/scripts/setup_rust_fork.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -e -./y.rs build --no-unstable-features +./y.sh build --no-unstable-features echo "[SETUP] Rust fork" git clone https://github.com/rust-lang/rust.git || true diff --git a/test.sh b/test.sh index 13e7784539d..6357eebf026 100755 --- a/test.sh +++ b/test.sh @@ -1,2 +1,2 @@ #!/usr/bin/env bash -exec ./y.rs test "$@" +exec ./y.sh test "$@" diff --git a/y.rs b/y.rs index a68a10500f5..e806a64d943 100755 --- a/y.rs +++ b/y.rs @@ -1,35 +1,6 @@ #!/usr/bin/env bash #![deny(unsafe_code)] /*This line is ignored by bash # This block is ignored by rustc -set -e -echo "[BUILD] y.rs" 1>&2 -rustc $0 -o ${0/.rs/.bin} -Cdebuginfo=1 --edition 2021 -exec ${0/.rs/.bin} $@ +echo "Warning: y.rs is a deprecated alias for y.sh" 1>&2 +exec ./y.sh "$@" */ - -#![warn(rust_2018_idioms)] -#![warn(unused_lifetimes)] -#![warn(unreachable_pub)] - -//! The build system for cg_clif -//! -//! # Manual compilation -//! -//! If your system doesn't support shell scripts you can manually compile and run this file using -//! for example: -//! -//! ```shell -//! $ rustc y.rs -o y.bin -//! $ ./y.bin -//! ``` -//! -//! # Naming -//! -//! The name `y.rs` was chosen to not conflict with rustc's `x.py`. - -#[path = "build_system/mod.rs"] -mod build_system; - -fn main() { - build_system::main(); -} diff --git a/y.sh b/y.sh new file mode 100755 index 00000000000..bc925a23e2a --- /dev/null +++ b/y.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +set -e +echo "[BUILD] build system" 1>&2 +rustc build_system/main.rs -o y.bin -Cdebuginfo=1 --edition 2021 +exec ./y.bin "$@"