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

chore: add skip of sub-build on abi generation to circumvent cargo-near#287 #212

Open
wants to merge 3 commits into
base: refactor/upgraded-reproducible-config
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 134 additions & 18 deletions treasury-factory/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,62 @@
use base64::{engine::general_purpose, Engine as _};
use cargo_near_build::extended::*;
use cargo_near_build::BuildOpts;
use cargo_near_build::{bon, camino, extended};
use std::env;
use std::fs;
use std::io::Write;
use std::path::Path;
use std::str::FromStr;

struct ChildContract {
workdir: &'static str,
manifest: camino::Utf8PathBuf,
nep330_contract_path: &'static str,
}

impl ChildContract {
fn get() -> Self {
// unix path to target sub-contract's crate from root of the repo
let nep330_contract_path = "web4/treasury-web4";
let workdir = "../web4/treasury-web4";
let manifest = camino::Utf8PathBuf::from_str(workdir)
.expect("pathbuf from str")
.join("Cargo.toml");

Self {
workdir,
nep330_contract_path,
manifest,
}
}

// returns absolute path, relative to parent contract
fn sub_build_target_dir() -> String {
let path = Path::new("./target/build-rs-treasury-web4-for-treasury-factory");

std::fs::create_dir_all(&path).unwrap_or_else(|err| panic!(
"create sub-build target dir {}: {:#?}", path.to_string_lossy(), err
));

let path = path.canonicalize()
.unwrap_or_else(|err| panic!("canonicalize() path: {:#?}", err));

path.to_str()
.unwrap_or_else(|| panic!("valid unicode path expected {}", path.to_string_lossy()))
.to_owned()
}

// by the way, BASE64("") = ""
const STUB_PATH: &str = "./target/treasury-web4-stub.bin";

fn intermediate_result_env_key() -> &'static str {
"BUILD_RS_SUB_BUILD_ARTIFACT_RAW_WASM"
}

const ENCODED_RESULT_PATH: &str = "./target/treasury_web4.wasm.base64.txt";
fn final_result_env_key() -> &'static str {
"BUILD_RS_SUB_BUILD_ARTIFACT_BASE64_ENCODED_WASM"
}
}

fn main() {
// Change working directory to the directory of the script (similar to process.chdir)
Expand All @@ -30,32 +82,96 @@ fn main() {
.write_all(index_html_base64.as_bytes())
.expect("Failed to write to output file");

let web4_wasm_path = "../web4/treasury-web4/target/near/treasury_web4.wasm";
if !fs::exists(web4_wasm_path).unwrap() {
let child_contract = ChildContract::get();
let build_artifact = {
let build_opts = BuildOpts::builder()
.manifest_path("../web4/treasury-web4/Cargo.toml".into())
.manifest_path(child_contract.manifest)
.override_nep330_contract_path(child_contract.nep330_contract_path)
.override_cargo_target_dir(ChildContract::sub_build_target_dir())
Copy link
Author

@dj8yfo dj8yfo Jan 14, 2025

Choose a reason for hiding this comment

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

override_cargo_target_dir can probably be made computed automatically too and attempted to be removed from public api, but that has to happen after nep330_contract_path and stub_path iterations

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Author

@dj8yfo dj8yfo Jan 14, 2025

Choose a reason for hiding this comment

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

good candidate for this looks to be

    fn sub_build_target_dir(sub_build_crate_name: &str, current_crate_name: &str) -> String {
        let out_dir_env = std::env::var("OUT_DIR").expect("OUT_DIR is always set in build scripts");
        let out_dir = Path::new(&out_dir_env);

        let path = out_dir.join(format!(
            "target-{}-for-{}",
            sub_build_crate_name, current_crate_name
        ));

        path.to_str()
            .unwrap_or_else(|| panic!("valid unicode path expected {}", path.to_string_lossy()))
            .to_owned()
    }

which results in

warning: [email protected]: Build artifact path: /home/jerrick/Documents/code/neardevhub-treasury-dashboard/treasury-factory/target/wasm32-unknown-unknown/release/build/treasury-factory-2b4e19c8a0b12f8f/out/target-treasury-web4-for-treasury-factory/near/treasury_web4.wasm

Copy link
Author

@dj8yfo dj8yfo Jan 14, 2025

Choose a reason for hiding this comment

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

this has to be tested with how this principle looks on more than 1 level of nesting in near-devhub contract

(comment with an output of tested nested build-scripts pending in this thread)

Copy link
Author

Choose a reason for hiding this comment

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

it looks like above sub_build_target_dir fn is a good default for an override_cargo_target_dir option:

  • community built
    • wasm : target/near/devhub_community/devhub_community.wasm
    • sub-build
      • stub : target/debug/build/devhub-community-9df08e869c916c9b/out/discussions-stub.bin
      • wasm : target/wasm32-unknown-unknown/release/build/devhub-community-1515b1be94ff0e90/out/target-discussions-for-community/near/devhub_discussions/devhub_discussions.wasm
  • community-factory built
    • wasm : target/near/devhub_community_factory/devhub_community_factory.wasm
    • sub-build
      • stub : target/debug/build/devhub-community-factory-d7fa895f926988cc/out/community-stub.bin
      • wasm : target/wasm32-unknown-unknown/release/build/devhub-community-factory-25fc80ffdbb5bdec/out/target-community-for-community-factory/near/devhub_community/devhub_community.wasm
      • sub-build
        • stub : target/wasm32-unknown-unknown/release/build/devhub-community-factory-25fc80ffdbb5bdec/out/target-community-for-community-factory/debug/build/devhub-community-9df08e869c916c9b/out/discussions-stub.bin
        • wasm : target/wasm32-unknown-unknown/release/build/devhub-community-factory-25fc80ffdbb5bdec/out/target-community-for-community-factory/wasm32-unknown-unknown/release/build/devhub-community-1515b1be94ff0e90/out/target-discussions-for-community/near/devhub_discussions/devhub_discussions.wasm

.build();
let build_script_opts = BuildScriptOpts::builder().build();
let build_opts_extended = BuildOptsExtended::builder()
let build_script_opts = extended::BuildScriptOpts::builder()
.rerun_if_changed_list(bon::vec![child_contract.workdir])
.build_skipped_when_env_is(vec![
// shorter build for `cargo check`
("PROFILE", "debug"),
(cargo_near_build::env_keys::BUILD_RS_ABI_STEP_HINT, "true"),
])
.stub_path(ChildContract::STUB_PATH)
Copy link
Author

Choose a reason for hiding this comment

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

stub_path can probably be computed automatically and not be an option on builder, needed to be configured

Copy link
Author

Choose a reason for hiding this comment

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

good candidate for this looks to be

    fn sub_build_stub_result_path(sub_build_crate_name: &str) -> String {
        let out_dir_env = std::env::var("OUT_DIR").expect("OUT_DIR is always set in build scripts");
        let out_dir = Path::new(&out_dir_env);

        let path = out_dir.join(format!("{}-stub.bin", sub_build_crate_name));
        path.to_str().expect("valid unicode").to_owned()
    }

which is

warning: [email protected]: Build empty artifact stub-file written to: `/home/user/Documents/code/neardevhub-treasury-dashboard/treasury-factory/target/debug/build/treasury-factory-566aa3a029070e78/out/treasury-web4-stub.bin`

as a result

.result_env_key(ChildContract::intermediate_result_env_key())
.build();

let build_opts_extended = extended::BuildOptsExtended::builder()
.build_opts(build_opts)
.build_script_opts(build_script_opts)
.build();

let build_artifact = build(build_opts_extended).expect("Building web4 contract failed");
extended::build(build_opts_extended).unwrap_or_else(|err| {
panic!(
"Building `{}` contract failed: {:#?}",
child_contract.workdir, err
)
})
};

let web4_wasm_base64_path = base64_encode_wasm(&build_artifact);

let web4_wasm = fs::read(build_artifact.path)
.expect(format!("Failed to read {}", web4_wasm_path).as_str());
export_result(web4_wasm_base64_path);
}

let web4_wasm_base64 = general_purpose::STANDARD.encode(&web4_wasm);
fn base64_encode_wasm(build_artifact: &cargo_near_build::BuildArtifact) -> std::path::PathBuf {
let web4_wasm_base64 = {
let web4_wasm = fs::read(&build_artifact.path)
.unwrap_or_else(|err| panic!("Failed to read {:?}: {:#?}", build_artifact.path, err));
general_purpose::STANDARD.encode(&web4_wasm)
};

let web4_wasm_base64_path =
Path::new(env!("CARGO_MANIFEST_DIR")).join("treasury_web4.wasm.base64.txt");
let web4_wasm_base64_path = Path::new(ChildContract::ENCODED_RESULT_PATH);

let mut output_file =
fs::File::create(web4_wasm_base64_path).expect("Failed to create output file");
let mut output_file =
fs::File::create(web4_wasm_base64_path).unwrap_or_else(|err| {
panic!(
"Failed to create output file {:?}, {:#?}",
web4_wasm_base64_path, err
)
});

output_file
.write_all(web4_wasm_base64.as_bytes())
.expect("Failed to write to output file");
}
output_file
.write_all(web4_wasm_base64.as_bytes())
.unwrap_or_else(|err| {
panic!(
"Failed to write to output file {:?}, {:#?}",
web4_wasm_base64_path, err
)
});
web4_wasm_base64_path
.canonicalize()
.unwrap_or_else(|err| panic!("canonicalize() path: {:#?}", err))
}

fn export_result(web4_wasm_base64_path: std::path::PathBuf) {
// this function doesn't return result
// result becomes available to crate's code (src/lib.rs)
// via `cargo::rustc-env` export of `result_env_key` variable
let _result_env_key = {
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
let result_env_key = ChildContract::final_result_env_key();

let path = web4_wasm_base64_path.to_str().unwrap_or_else(|| {
panic!(
"valid unicode path expected {}",
web4_wasm_base64_path.to_string_lossy()
)
});
println!(
"cargo::warning={}",
format!("Path to base64 encoded artifact: {}", path,)
);
println!(
"cargo::warning={}",
format!(
"Path to base64-encoded wasm artifact is exported to `{}`",
result_env_key,
)
);
println!("cargo::rustc-env={}={}", result_env_key, path);
};
}
2 changes: 1 addition & 1 deletion treasury-factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Contract {
"new_account_id": new_instance_contract_id.clone(),
"options": {
"full_access_keys": [env::signer_account_pk(),admin_full_access_public_key],
"contract_bytes_base64": include_str!("../treasury_web4.wasm.base64.txt")
"contract_bytes_base64": include_str!(env!("BUILD_RS_SUB_BUILD_ARTIFACT_BASE64_ENCODED_WASM"))
}
})
.to_string()
Expand Down
Loading