Skip to content

Commit

Permalink
feat(gcli): macro for looking up binaries in primary package
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Dec 14, 2023
1 parent 569acd7 commit c9ab66c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
8 changes: 7 additions & 1 deletion examples/ping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ license.workspace = true
homepage.workspace = true
repository.workspace = true

[[bin]]
name = "demo_ping"
path = "src/bin/ping.rs"
required-features = ["gcli"]

[dependencies]
gstd.workspace = true
gcli = { workspace = true, optional = true }
Expand All @@ -17,4 +22,5 @@ gear-wasm-builder.workspace = true
[features]
debug = ["gstd/debug"]
default = ["std"]
std = ["gcli"]
std = []
gcli = ["gcli/embed"]
7 changes: 0 additions & 7 deletions examples/ping/bin/ping.rs

This file was deleted.

22 changes: 22 additions & 0 deletions examples/ping/src/bin/ping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// This file is part of Gear.
//
// Copyright (C) 2021-2023 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#![cfg(feature = "gcli")]

fn main() {
println!("{:?}", gcli::lookup!());
}
1 change: 1 addition & 0 deletions gcli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ vara-runtime = { workspace = true, features = [ "std", "dev" ] }
which.workspace = true

[features]
embed = []
node-key = [ "libp2p" ]

[package.metadata.docs.rs]
Expand Down
54 changes: 49 additions & 5 deletions gcli/src/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,55 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Utils for embedded commands.
#![cfg(feature = "embed")]

use crate::result::Result;
use std::path::PathBuf;

/// Look up `*.opt.wasm` file from the primary package.
pub fn lookup_opt() -> Result<Vec<u8>> {
println!("primary package: {}", env!("CARGO_MANIFEST_DIR"));
Ok(Default::default())
/// This macro is used to lookup the artifact from the `OUT_DIR`.
#[macro_export]
macro_rules! lookup {
() => {{
::gcli::embed::Artifact::from_out_dir(env!("OUT_DIR"))
}};
}

/// The length of the suffix of the output folder.
///
/// Example: `[gcli]-1234567890abcdef`
const OUT_SUFFIX_LENGTH: usize = 17;

/// Program info for embedded commands.
#[derive(Debug)]
pub struct Artifact {
/// Path of the optitmized WASM binary.
pub opt: PathBuf,
}

impl Artifact {
/// Parse the artifact from the `OUT_DIR`
/// environment variable.
pub fn from_out_dir(out: &str) -> Option<Self> {
let out_dir = PathBuf::from(out);
let mut ancestors = out_dir.ancestors();

let [name, profile, target] = [
ancestors
.nth(1)?
.file_name()?
.to_str()
.map(|name| name.get(..name.len().checked_sub(OUT_SUFFIX_LENGTH)?))
.flatten()?,
(ancestors.nth(1)?.file_name()?.to_str()? == "debug")
.then(|| "debug")
.unwrap_or("release"),
ancestors.next()?.to_str()?,
];

let opt = PathBuf::from(format!(
"{target}/wasm32-unknown-unknown/{profile}/{}.opt.wasm",
name.replace('-', "_")
));

opt.exists().then(|| Self { opt })
}
}

0 comments on commit c9ab66c

Please sign in to comment.