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

Include Features and Package Flags in Cargo Stylus Commands #127

Merged
merged 12 commits into from
Dec 12, 2024
Merged
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
ARG BUILD_PLATFORM=linux/amd64
ARG RUST_VERSION=1.80
ARG CARGO_STYLUS_VERSION=0.5.6

FROM --platform=${BUILD_PLATFORM} rust:${RUST_VERSION} AS builder
RUN apt-get update && apt-get install -y git

RUN rustup target add x86_64-unknown-linux-gnu
ARG CARGO_STYLUS_VERSION
RUN test -n "$CARGO_STYLUS_VERSION"
RUN git clone --branch v$CARGO_STYLUS_VERSION https://github.com/offchainlabs/cargo-stylus.git

# Copy the entire workspace
COPY . /cargo-stylus/
WORKDIR /cargo-stylus

# Build the project using the workspace member
RUN cargo build --release --manifest-path main/Cargo.toml

FROM --platform=${BUILD_PLATFORM} rust:${RUST_VERSION} AS cargo-stylus-base
Expand Down
29 changes: 0 additions & 29 deletions Makefile

This file was deleted.

3 changes: 2 additions & 1 deletion main/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ impl CheckConfig {
let toolchain_file_path = PathBuf::from(".").as_path().join(TOOLCHAIN_FILE_NAME);
let toolchain_channel = extract_toolchain_channel(&toolchain_file_path)?;
let rust_stable = !toolchain_channel.contains("nightly");
let cfg = BuildConfig::new(rust_stable);
let mut cfg = BuildConfig::new(rust_stable);
cfg.features = self.common_cfg.features.clone();
let wasm = project::build_dylib(cfg.clone())?;
let project_hash =
project::hash_project(self.common_cfg.source_files_for_project_hash.clone(), cfg)?;
Expand Down
28 changes: 24 additions & 4 deletions main/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ struct CommonConfig {
#[arg(long)]
/// Optional max fee per gas in gwei units.
max_fee_per_gas_gwei: Option<u128>,
/// Specifies the features to use when building the Stylus binary.
#[arg(long)]
features: Option<String>,
}

#[derive(Subcommand, Clone, Debug)]
Expand Down Expand Up @@ -258,6 +261,12 @@ struct ReplayArgs {
/// Whether to use stable Rust. Note that nightly is needed to expand macros.
#[arg(short, long)]
stable_rust: bool,
/// Any features that should be passed to cargo build.
#[arg(short, long)]
features: Option<Vec<String>>,
/// Which specific package to build during replay, if any.
#[arg(long)]
package: Option<String>,
/// Whether this process is the child of another.
#[arg(short, long, hide(true))]
child: bool,
Expand Down Expand Up @@ -681,7 +690,7 @@ async fn replay(args: ReplayArgs) -> Result<()> {
let provider = sys::new_provider(&args.trace.endpoint)?;
let trace = Trace::new(provider, args.trace.tx, args.trace.use_native_tracer).await?;

build_shared_library(&args.trace.project)?;
build_shared_library(&args.trace.project, args.package, args.features)?;
let library_extension = if macos { ".dylib" } else { ".so" };
let shared_library = find_shared_library(&args.trace.project, library_extension)?;

Expand All @@ -704,12 +713,23 @@ async fn replay(args: ReplayArgs) -> Result<()> {
Ok(())
}

pub fn build_shared_library(path: &Path) -> Result<()> {
pub fn build_shared_library(
path: &Path,
package: Option<String>,
features: Option<Vec<String>>,
) -> Result<()> {
let mut cargo = sys::new_command("cargo");

cargo.current_dir(path).arg("build");

if let Some(f) = features {
cargo.arg("--features").arg(f.join(","));
}
if let Some(p) = package {
cargo.arg("--package").arg(p);
}

cargo
.current_dir(path)
.arg("build")
.arg("--lib")
.arg("--locked")
.arg("--target")
Expand Down
5 changes: 5 additions & 0 deletions main/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum OptLevel {
pub struct BuildConfig {
pub opt_level: OptLevel,
pub stable: bool,
pub features: Option<String>,
}

impl BuildConfig {
Expand Down Expand Up @@ -74,6 +75,10 @@ pub fn build_dylib(cfg: BuildConfig) -> Result<PathBuf> {
cmd.arg("--lib");
cmd.arg("--locked");

if let Some(features) = cfg.features {
cmd.arg(format!("--features={}", features.clone()));
}

if !cfg.stable {
cmd.arg("-Z");
cmd.arg("build-std=std,panic_abort");
Expand Down
1 change: 1 addition & 0 deletions main/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub async fn verify(cfg: VerifyConfig) -> eyre::Result<()> {
let build_cfg = project::BuildConfig {
opt_level: project::OptLevel::default(),
stable: rust_stable,
features: cfg.common_cfg.features.clone(),
};
let wasm_file: PathBuf = project::build_dylib(build_cfg.clone())
.map_err(|e| eyre!("could not build project to WASM: {e}"))?;
Expand Down
Loading