Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pallet-revive: Use custom target to build test fixtures #6266

Merged
merged 13 commits into from
Oct 29, 2024
36 changes: 14 additions & 22 deletions substrate/frame/revive/fixtures/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,50 +116,41 @@ mod build {
fs::write(output_dir.join("Cargo.toml"), cargo_toml).map_err(Into::into)
}

fn invoke_build(current_dir: &Path) -> Result<()> {
fn invoke_build(target: &Path, current_dir: &Path) -> Result<()> {
let encoded_rustflags = [
"-Dwarnings",
"-Ctarget-feature=+lui-addi-fusion",
"-Crelocation-model=pie",
"-Clink-arg=--emit-relocs",
"-Clink-arg=--unique",
]
.join("\x1f");

let toolchain = std::env::var(OVERRIDE_RUSTUP_TOOLCHAIN_ENV_VAR)
.ok()
.unwrap_or(String::from("rve-nightly"));

let build_res = Command::new(env::var("CARGO")?)
let mut build_command = Command::new(env::var("CARGO")?);
build_command
.current_dir(current_dir)
.env_clear()
.env("PATH", env::var("PATH").unwrap_or_default())
.env("CARGO_ENCODED_RUSTFLAGS", encoded_rustflags)
.env("RUSTUP_TOOLCHAIN", &toolchain)
.env("RUSTC_BOOTSTRAP", "1")
.env("RUSTUP_HOME", env::var("RUSTUP_HOME").unwrap_or_default())
.args([
"build",
"--release",
"--target=riscv32ema-unknown-none-elf",
"-Zbuild-std=core",
"-Zbuild-std-features=panic_immediate_abort",
])
.output()
.expect("failed to execute process");
.arg("--target")
jarkkojs marked this conversation as resolved.
Show resolved Hide resolved
.arg(target);

if let Ok(toolchain) = env::var(OVERRIDE_RUSTUP_TOOLCHAIN_ENV_VAR) {
build_command.env("RUSTUP_TOOLCHAIN", &toolchain);
}

let build_res = build_command.output().expect("failed to execute process");

if build_res.status.success() {
return Ok(())
}

let stderr = String::from_utf8_lossy(&build_res.stderr);

if stderr.contains("'rve-nightly' is not installed") {
eprintln!("RISC-V toolchain is not installed.\nDownload and install toolchain from https://github.com/paritytech/rustc-rv32e-toolchain.");
eprintln!("{}", stderr);
} else {
eprintln!("{}", stderr);
}
eprintln!("{}", stderr);

bail!("Failed to build contracts");
}
Expand Down Expand Up @@ -195,6 +186,7 @@ mod build {
let fixtures_dir: PathBuf = env::var("CARGO_MANIFEST_DIR")?.into();
let contracts_dir = fixtures_dir.join("contracts");
let out_dir: PathBuf = env::var("OUT_DIR")?.into();
let target = fixtures_dir.join("riscv32emac-unknown-none-polkavm.json");

println!("cargo::rerun-if-env-changed={OVERRIDE_RUSTUP_TOOLCHAIN_ENV_VAR}");
println!("cargo::rerun-if-env-changed={OVERRIDE_STRIP_ENV_VAR}");
Expand All @@ -216,7 +208,7 @@ mod build {
let tmp_dir_path = tmp_dir.path();

create_cargo_toml(&fixtures_dir, entries.iter(), tmp_dir.path())?;
invoke_build(tmp_dir_path)?;
invoke_build(&target, tmp_dir_path)?;

write_output(tmp_dir_path, &out_dir, entries)?;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"arch": "riscv32",
"cpu": "generic-rv32",
"crt-objects-fallback": "false",
"data-layout": "e-m:e-p:32:32-i64:64-n32-S32",
"eh-frame-header": false,
"emit-debug-gdb-scripts": false,
"features": "+e,+m,+a,+c,+lui-addi-fusion,+fast-unaligned-access,+xtheadcondmov",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"features": "+e,+m,+a,+c,+lui-addi-fusion,+fast-unaligned-access,+xtheadcondmov",
"features": "+e,+m,+a,+c,+lui-addi-fusion,+unaligned-scalar-mem,+xtheadcondmov",

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+fast-unaligned-access isn't a feature for the target and it won't have any effect. +fast-unaligned-access seems to be an older artifact.

https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/RISCV/RISCVFeatures.td#L1358-L1361

Copy link
Member

@xermicus xermicus Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay it still called +fast-unaligned-access in LLVM 18.X. Rust 1.82.0 (latest stable) however uses LLVM 19:

rustc -vV
rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1

What toolchain do we expect?

Copy link
Member Author

@athei athei Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It uses whatever you build the repository with unless you override it. Our CI is on 1.81.

"linker": "rust-lld",
"linker-flavor": "ld.lld",
"llvm-abiname": "ilp32e",
"llvm-target": "riscv32",
"max-atomic-width": 32,
"panic-strategy": "abort",
"relocation-model": "pie",
"target-pointer-width": "32",
"singlethread": true,
"pre-link-args": {
"ld": [
"--emit-relocs",
"--unique",
"--relocatable"
]
},
"env": "polkavm"
}