Skip to content

Commit

Permalink
f: add everything except the eap-create implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
apljungquist committed Nov 5, 2024
1 parent fbe6377 commit 47ab350
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 40 deletions.
74 changes: 69 additions & 5 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ serde = "1.0.204"
serde_json = "1.0.120"
syslog = "6.1.1"
tar = "0.4.40"
tempdir = "0.3.7"
tempfile = "3.10.1"
thiserror = "1.0.61"
tokio = "1.38.1"
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ crates/%-sys/src/bindings.rs: target-$(AXIS_DEVICE_ARCH)/acap/_envoy
target-$(AXIS_DEVICE_ARCH)/acap/_envoy: target/debug/cargo-acap-build $(patsubst %/,%/LICENSE,$(wildcard apps/*/))
rm -r $(@D) ||:
cargo build --bin cargo-acap-build
ACAP_BUILD_RUST=1 \
CARGO_TARGET_DIR=target-$(AXIS_DEVICE_ARCH) \
./target/debug/cargo-acap-build \
--target $(AXIS_DEVICE_ARCH) \
Expand Down
12 changes: 7 additions & 5 deletions crates/acap-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ version = "0.0.0"
edition = "2021"

[dependencies]

anyhow = { workspace = true }
clap = { workspace = true, features = ["derive", "env"] }
dirs = { workspace = true }
env_logger = { workspace = true }
glob = { workspace = true }
log = { workspace = true }
semver = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["preserve_order"] }
tempfile = { workspace = true }
regex = { workspace = true }
tempdir = { workspace = true }

[dev-dependencies]
clap = { workspace = true, features = ["derive", "env"] }
env_logger = { workspace = true }
glob = { workspace = true }
[features]
rust = []
157 changes: 157 additions & 0 deletions crates/acap-build/src/bin/acap-build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
//! Wrapper over the lib exposing backwards compatible interface
// Having this increases the probability of upstreaming this rewrite, which I think should be a
// prerequisite for using it; we don't want to maintain our own fork of non-trivial tools that
// exist also in the official ACAP SDK.
use std::{
env,
fmt::{Display, Formatter},
fs,
fs::File,
path::{Path, PathBuf},
};

use acap_build::{manifest::Manifest, AppBuilder, Architecture};
use anyhow::Context;
use clap::{Parser, ValueEnum};
use log::debug;

#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq, ValueEnum)]
#[clap(rename_all = "kebab-case")]
enum BuildOption {
#[default]
Make,
/// Note: this is experimental
Meson,
NoBuild,
}

impl Display for BuildOption {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Make => write!(f, "make"),
Self::Meson => write!(f, "meson"),
Self::NoBuild => write!(f, "no-build"),
}
}
}

#[derive(Clone, Debug, Parser)]
#[clap(verbatim_doc_comment)]
struct Cli {
#[clap(default_value_t, long, short)]
build: BuildOption,
// TODO: Look into mimicking the original interface exactly.
/// Note: can be used more than once.
#[clap(long)]
meson_cross_file: Vec<PathBuf>,
#[clap(long, short)]
manifest: Option<PathBuf>,
/// Note: can be used more than once.
#[clap(long, short)]
additional_file: Vec<PathBuf>,
#[clap(long)]
disable_manifest_validation: bool,
#[clap(long)]
disable_package_creation: bool,
path: PathBuf,
}

impl Cli {
fn read_app_name(manifest: &Path) -> anyhow::Result<String> {
let manifest = fs::read_to_string(manifest)?;
let manifest: Manifest = serde_json::from_str(&manifest)?;
Ok(manifest.acap_package_conf.setup.app_name)
}
fn exec(self) -> anyhow::Result<()> {
let arch: Architecture = env::var("OECORE_TARGET_ARCH")?.parse()?;
// let staging_dir = TempDir::new("acap-build")?;
let staging_dir = env::current_dir().unwrap().with_extension("tmp");
if staging_dir.exists() {
fs::remove_dir_all(&staging_dir)?;
}
fs::create_dir(&staging_dir)?;
let manifest = match self.manifest {
None => self.path.join("manifest.json"),
Some(m) => self.path.join(m),
};
let app_name = Self::read_app_name(&manifest)?;
let exe = self.path.join(&app_name);
let license = self.path.join("LICENSE");
let mut builder = AppBuilder::new(
// staging_dir.path().to_path_buf(),
staging_dir,
arch,
&app_name,
&manifest,
&exe,
&license,
)?;

let lib = self.path.join("lib");
if lib.exists() {
builder.lib(&lib)?;
}

let html = self.path.join("html");
if html.exists() {
builder.html(&html)?;
}

for additional_file in self.additional_file {
builder.additional_file(&self.path.join(additional_file))?;
}

let path = builder.build()?;

Self::copy_artifacts(path.parent().unwrap(), &env::current_dir().unwrap(), &path)
}

fn copy_artifacts(src: &Path, dst: &Path, eap: &Path) -> anyhow::Result<()> {
let (prefix, _) = eap
.file_name()
.unwrap()
.to_str()
.unwrap()
.rsplit_once('_')
.unwrap();
let license = format!("{prefix}_LICENSE.txt");
for file_name in [
eap.file_name().unwrap().to_str().unwrap(),
license.as_str(),
"package.conf",
"package.conf.orig",
"param.conf",
] {
fs::copy(src.join(file_name), dst.join(file_name))
.with_context(|| format!("{file_name}: {src:?} -> {dst:?}"))?;
}
Ok(())
}
}

fn main() -> anyhow::Result<()> {
let log_file = if env::var_os("RUST_LOG").is_none() {
let dir = dirs::runtime_dir().unwrap_or(env::temp_dir());
let path = dir.join("cargo-acap-sdk.log");
let target = env_logger::Target::Pipe(Box::new(File::create(&path)?));
let mut builder = env_logger::Builder::from_env(env_logger::Env::default());
builder.target(target).filter_level(log::LevelFilter::Debug);
builder.init();
Some(path)
} else {
env_logger::init();
None
};
debug!("Logging initialized");

match Cli::parse().exec() {
Ok(()) => Ok(()),
Err(e) => {
if let Some(log_file) = log_file {
Err(e.context(format!("A detailed log has been saved to {log_file:?}")))
} else {
Err(e)
}
}
}
}
Loading

0 comments on commit 47ab350

Please sign in to comment.