Skip to content

Commit

Permalink
pallet-contracts-fixtures: Only build RISCV when the feature is enabl…
Browse files Browse the repository at this point in the history
…ed (#2870)

This disables building the RISCV fixtures by default. They still require
a custom build rustc version. When there are actual tests for RISCV, the
feature can be enabled in CI. Also fixes some unwraps that assume again
that people are using rustup...
  • Loading branch information
bkchr authored Jan 7, 2024
1 parent 204fe7f commit 745c02c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 4 additions & 0 deletions substrate/frame/contracts/fixtures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ toml = "0.8.2"
twox-hash = "1.6.3"
polkavm-linker = "0.3.0"
anyhow = "1.0.0"

[features]
# Enable experimental RISCV fixtures build
riscv-experimental = []
14 changes: 10 additions & 4 deletions substrate/frame/contracts/fixtures/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// limitations under the License.

//! Compile contracts to wasm and RISC-V binaries.
use anyhow::{bail, format_err, Context, Result};
use anyhow::{bail, Context, Result};
use parity_wasm::elements::{deserialize_file, serialize_to_file, Internal};
use std::{
env, fs,
Expand Down Expand Up @@ -91,6 +91,7 @@ impl Entry {
}

/// Return the name of the RISC-V polkavm file.
#[cfg(feature = "riscv-experimental")]
fn out_riscv_filename(&self) -> String {
format!("{}.polkavm", self.name())
}
Expand Down Expand Up @@ -231,6 +232,7 @@ fn post_process_wasm(input_path: &Path, output_path: &Path) -> Result<()> {
}

/// Build contracts for RISC-V.
#[cfg(feature = "riscv-experimental")]
fn invoke_riscv_build(current_dir: &Path) -> Result<()> {
let encoded_rustflags =
["-Crelocation-model=pie", "-Clink-arg=--emit-relocs", "-Clink-arg=-Tmemory.ld"]
Expand All @@ -241,10 +243,10 @@ fn invoke_riscv_build(current_dir: &Path) -> Result<()> {
let build_res = Command::new(env::var("CARGO")?)
.current_dir(current_dir)
.env_clear()
.env("PATH", env::var("PATH").unwrap())
.env("PATH", env::var("PATH").unwrap_or_default())
.env("CARGO_ENCODED_RUSTFLAGS", encoded_rustflags)
.env("RUSTUP_TOOLCHAIN", "rve-nightly")
.env("RUSTUP_HOME", env::var("RUSTUP_HOME").unwrap())
.env("RUSTUP_HOME", env::var("RUSTUP_HOME").unwrap_or_default())
.args(["build", "--release", "--target=riscv32em-unknown-none-elf"])
.output()
.expect("failed to execute process");
Expand All @@ -265,12 +267,13 @@ fn invoke_riscv_build(current_dir: &Path) -> Result<()> {
bail!("Failed to build contracts");
}
/// Post-process the compiled wasm contracts.
#[cfg(feature = "riscv-experimental")]
fn post_process_riscv(input_path: &Path, output_path: &Path) -> Result<()> {
let mut config = polkavm_linker::Config::default();
config.set_strip(true);
let orig = fs::read(input_path).with_context(|| format!("Failed to read {:?}", input_path))?;
let linked = polkavm_linker::program_from_elf(config, orig.as_ref())
.map_err(|err| format_err!("Failed to link polkavm program: {}", err))?;
.map_err(|err| anyhow::format_err!("Failed to link polkavm program: {}", err))?;
fs::write(output_path, linked.as_bytes()).map_err(Into::into)
}

Expand All @@ -283,6 +286,7 @@ fn write_output(build_dir: &Path, out_dir: &Path, entries: Vec<Entry>) -> Result
&out_dir.join(&wasm_output),
)?;

#[cfg(feature = "riscv-experimental")]
post_process_riscv(
&build_dir.join("target/riscv32em-unknown-none-elf/release").join(entry.name()),
&out_dir.join(entry.out_riscv_filename()),
Expand Down Expand Up @@ -335,6 +339,8 @@ fn main() -> Result<()> {
)?;

invoke_wasm_build(tmp_dir_path)?;

#[cfg(feature = "riscv-experimental")]
invoke_riscv_build(tmp_dir_path)?;

write_output(tmp_dir_path, &out_dir, entries)?;
Expand Down

0 comments on commit 745c02c

Please sign in to comment.