Skip to content

Commit

Permalink
revive: Switch to 64bit RISC-V
Browse files Browse the repository at this point in the history
  • Loading branch information
athei committed Nov 20, 2024
1 parent 70b6c7b commit 438d74c
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 230 deletions.
1 change: 0 additions & 1 deletion .github/workflows/checks-quick.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ jobs:
--exclude
"substrate/frame/contracts/fixtures/build"
"substrate/frame/contracts/fixtures/contracts/common"
"substrate/frame/revive/fixtures/build"
"substrate/frame/revive/fixtures/contracts/common"
- name: deny git deps
run: python3 .github/scripts/deny-git-deps.py .
Expand Down
72 changes: 37 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion substrate/frame/revive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
environmental = { workspace = true }
paste = { workspace = true }
polkavm = { version = "0.13.0", default-features = false }
polkavm = { version = "0.17.0", default-features = false }
bitflags = { workspace = true }
codec = { features = ["derive", "max-encoded-len"], workspace = true }
scale-info = { features = ["derive"], workspace = true }
Expand Down
4 changes: 1 addition & 3 deletions substrate/frame/revive/fixtures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ anyhow = { workspace = true, default-features = true, optional = true }
log = { workspace = true }

[build-dependencies]
parity-wasm = { workspace = true }
tempfile = { workspace = true }
toml = { workspace = true }
polkavm-linker = { version = "0.14.0" }
polkavm-linker = { version = "0.17.0" }
anyhow = { workspace = true, default-features = true }

[features]
Expand Down
95 changes: 66 additions & 29 deletions substrate/frame/revive/fixtures/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use anyhow::Result;

use anyhow::{bail, Context};
use std::{
cfg, env, fs,
env, fs,
io::Write,
path::{Path, PathBuf},
process::Command,
};
Expand Down Expand Up @@ -82,7 +83,7 @@ fn create_cargo_toml<'a>(
entries: impl Iterator<Item = &'a Entry>,
output_dir: &Path,
) -> Result<()> {
let mut cargo_toml: toml::Value = toml::from_str(include_str!("./build/Cargo.toml"))?;
let mut cargo_toml: toml::Value = toml::from_str(include_str!("./build/_Cargo.toml"))?;
let mut set_dep = |name, path| -> Result<()> {
cargo_toml["dependencies"][name]["path"] = toml::Value::String(
fixtures_dir.join(path).canonicalize()?.to_str().unwrap().to_string(),
Expand All @@ -108,29 +109,32 @@ fn create_cargo_toml<'a>(
let cargo_toml = toml::to_string_pretty(&cargo_toml)?;
fs::write(output_dir.join("Cargo.toml"), cargo_toml.clone())
.with_context(|| format!("Failed to write {cargo_toml:?}"))?;
fs::copy(
fixtures_dir.join("build/_rust-toolchain.toml"),
output_dir.join("rust-toolchain.toml"),
)
.context("Failed to write toolchain file")?;
Ok(())
}

fn invoke_build(target: &Path, current_dir: &Path) -> Result<()> {
fn invoke_build(current_dir: &Path) -> Result<()> {
let encoded_rustflags = ["-Dwarnings"].join("\x1f");

let mut build_command = Command::new(env::var("CARGO")?);
let mut build_command = Command::new("cargo");
build_command
.current_dir(current_dir)
.env_clear()
.env("PATH", env::var("PATH").unwrap_or_default())
.env("CARGO_ENCODED_RUSTFLAGS", encoded_rustflags)
.env("RUSTC_BOOTSTRAP", "1")
.env("RUSTUP_HOME", env::var("RUSTUP_HOME").unwrap_or_default())
.env("RUSTUP_TOOLCHAIN", env::var("RUSTUP_TOOLCHAIN").unwrap_or_default())
.args([
"build",
"--release",
"-Zbuild-std=core",
"-Zbuild-std-features=panic_immediate_abort",
])
.arg("--target")
.arg(target);
.arg(polkavm_linker::target_json_64_path().unwrap());

if let Ok(toolchain) = env::var(OVERRIDE_RUSTUP_TOOLCHAIN_ENV_VAR) {
build_command.env("RUSTUP_TOOLCHAIN", &toolchain);
Expand Down Expand Up @@ -168,7 +172,7 @@ fn write_output(build_dir: &Path, out_dir: &Path, entries: Vec<Entry>) -> Result
for entry in entries {
post_process(
&build_dir
.join("target/riscv32emac-unknown-none-polkavm/release")
.join("target/riscv64emac-unknown-none-polkavm/release")
.join(entry.name()),
&out_dir.join(entry.out_filename()),
)?;
Expand All @@ -177,11 +181,60 @@ fn write_output(build_dir: &Path, out_dir: &Path, entries: Vec<Entry>) -> Result
Ok(())
}

/// Create a directory in the `target` as output directory
fn create_out_dir() -> Result<PathBuf> {
let temp_dir: PathBuf = env::var("OUT_DIR")?.into();

// this is set in case the user has overriden the target directory
let out_dir = if let Ok(path) = env::var("CARGO_TARGET_DIR") {
path.into()
} else {
// otherwise just traverse up from the out dir
let mut out_dir: PathBuf = temp_dir.clone();
loop {
if !out_dir.pop() {
bail!("Cannot find project root.")
}
if out_dir.join("Cargo.lock").exists() {
break;
}
}
out_dir.join("target")
}
.join("pallet-revive-fixtures");

// clean up some leftover symlink from previous versions of this script
if out_dir.exists() && !out_dir.is_dir() {
fs::remove_file(&out_dir)?;
}
fs::create_dir_all(&out_dir).context("Failed to create output directory")?;

// write the location of the out dir so it can be found later
let mut file = fs::File::create(temp_dir.join("fixture_location.rs"))
.context("Failed to create fixture_location.rs")?;
write!(
file,
r#"
const OUT_DIR: &str = "{0}";
macro_rules! fixture {{
($name: literal) => {{
include_bytes!(concat!("{0}", "/", $name, ".polkavm"))
}};
}}
"#,
out_dir.display()
)
.context("Failed to write to fixture_location.rs")?;

Ok(out_dir)
}

pub fn main() -> Result<()> {
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");
let out_dir = create_out_dir().context("Cannot determine output directory")?;
let build_dir = out_dir.join("build");
fs::create_dir_all(&build_dir).context("Failed to create build directory")?;

println!("cargo::rerun-if-env-changed={OVERRIDE_RUSTUP_TOOLCHAIN_ENV_VAR}");
println!("cargo::rerun-if-env-changed={OVERRIDE_STRIP_ENV_VAR}");
Expand All @@ -199,25 +252,9 @@ pub fn main() -> Result<()> {
return Ok(())
}

let tmp_dir = tempfile::tempdir()?;
let tmp_dir_path = tmp_dir.path();

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

write_output(tmp_dir_path, &out_dir, entries)?;

#[cfg(unix)]
if let Ok(symlink_dir) = env::var("CARGO_WORKSPACE_ROOT_DIR") {
let symlink_dir: PathBuf = symlink_dir.into();
let symlink_dir: PathBuf = symlink_dir.join("target").join("pallet-revive-fixtures");
if symlink_dir.is_symlink() {
fs::remove_file(&symlink_dir)
.with_context(|| format!("Failed to remove_file {symlink_dir:?}"))?;
}
std::os::unix::fs::symlink(&out_dir, &symlink_dir)
.with_context(|| format!("Failed to symlink {out_dir:?} -> {symlink_dir:?}"))?;
}
create_cargo_toml(&fixtures_dir, entries.iter(), &build_dir)?;
invoke_build(&build_dir)?;
write_output(&build_dir, &out_dir, entries)?;

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ publish = false
version = "1.0.0"
edition = "2021"

# Make sure this is not included into the workspace
[workspace]

# Binary targets are injected dynamically by the build script.
[[bin]]

# All paths are injected dynamically by the build script.
[dependencies]
uapi = { package = 'pallet-revive-uapi', path = "", default-features = false }
common = { package = 'pallet-revive-fixtures-common', path = "" }
polkavm-derive = { version = "0.14.0" }
polkavm-derive = { version = "0.17.0" }

[profile.release]
opt-level = 3
Expand Down
4 changes: 4 additions & 0 deletions substrate/frame/revive/fixtures/build/_rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[toolchain]
channel = "nightly-2024-11-19"
components = ["rust-src"]
profile = "minimal"
Loading

0 comments on commit 438d74c

Please sign in to comment.