Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into embedded_web_page
Browse files Browse the repository at this point in the history
* upstream/main:
  chore: Group and sort imports (#23)
  Demonstrate containerized builds (#17)
  Factor out `app_logging` modules to lib crate (#24)
  chore(deps): bump the default group with 2 updates (#21)
  chore(deps): bump thiserror from 1.0.59 to 1.0.60 in the default group (#20)
  chore(deps): bump the default group with 3 updates (#13)
  • Loading branch information
apljungquist committed Jun 11, 2024
2 parents 5404825 + 0a5bc56 commit 331a4b4
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 73 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!/docker/
!/rust-toolchain.toml
35 changes: 21 additions & 14 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 @@ -16,6 +16,7 @@ syslog = "6.1.1"
thiserror = "1.0.57"

licensekey = { path = "crates/licensekey" }
app-logging = { path = "crates/app-logging" }

[workspace.package]
edition = "2021"
58 changes: 58 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
ARG REPO=axisecp
ARG SDK=acap-native-sdk
ARG UBUNTU_VERSION=22.04
ARG VERSION=1.14
ARG BASE_IMAGE=debian:bookworm-20240423-slim

FROM ${REPO}/${SDK}:${VERSION}-aarch64-ubuntu${UBUNTU_VERSION} AS sdk-aarch64
FROM ${REPO}/${SDK}:${VERSION}-armv7hf-ubuntu${UBUNTU_VERSION} AS sdk-armv7hf
FROM ${BASE_IMAGE}

COPY --from=sdk-aarch64 /opt/axis/acapsdk/axis-acap-manifest-tools /opt/axis/acapsdk/axis-acap-manifest-tools
COPY --from=sdk-aarch64 /opt/axis/acapsdk/environment-setup-cortexa53-crypto-poky-linux /opt/axis/acapsdk/environment-setup-cortexa53-crypto-poky-linux
COPY --from=sdk-armv7hf /opt/axis/acapsdk/environment-setup-cortexa9hf-neon-poky-linux-gnueabi /opt/axis/acapsdk/environment-setup-cortexa9hf-neon-poky-linux-gnueabi
COPY --from=sdk-aarch64 /opt/axis/acapsdk/sysroots/aarch64 /opt/axis/acapsdk/sysroots/aarch64
COPY --from=sdk-armv7hf /opt/axis/acapsdk/sysroots/armv7hf /opt/axis/acapsdk/sysroots/armv7hf
COPY --from=sdk-aarch64 /opt/axis/acapsdk/sysroots/x86_64-pokysdk-linux /opt/axis/acapsdk/sysroots/x86_64-pokysdk-linux

RUN apt-get update \
&& apt-get install -y \
build-essential \
clang \
g++-aarch64-linux-gnu \
g++-arm-linux-gnueabihf \
pkg-config \
python3-jsonschema \
wget \
&& rm -rf /var/lib/apt/lists/*

ENV RUSTUP_HOME=/usr/local/rustup \
CARGO_HOME=/usr/local/cargo \
PATH=/usr/local/cargo/bin:$PATH

COPY docker/install_rust.sh rust-toolchain.toml ./
RUN ./install_rust.sh \
&& rustup target add \
aarch64-unknown-linux-gnu \
thumbv7neon-unknown-linux-gnueabihf \
&& rm install_rust.sh rust-toolchain.toml

ENV \
SYSROOT_AARCH64=/opt/axis/acapsdk/sysroots/aarch64 \
SYSROOT_ARMV7HF=/opt/axis/acapsdk/sysroots/armv7hf
# The above makes the below easier to read
ENV \
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER="aarch64-linux-gnu-gcc" \
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-C link-args=--sysroot=${SYSROOT_AARCH64}" \
CC_aarch64_axis_linux_gnu="aarch64-linux-gnu-gcc" \
CXX_aarch64_axis_linux_gnu="aarch64-linux-gnu-g++" \
PKG_CONFIG_LIBDIR_aarch64_unknown_linux_gnu="${SYSROOT_AARCH64}/usr/lib/pkgconfig:${SYSROOT_AARCH64}/usr/share/pkgconfig" \
PKG_CONFIG_PATH_aarch64_unknown_linux_gnu="${SYSROOT_AARCH64}/usr/lib/pkgconfig:${SYSROOT_AARCH64}/usr/share/pkgconfig" \
PKG_CONFIG_SYSROOT_DIR_aarch64_unknown_linux_gnu="${SYSROOT_AARCH64}" \
CARGO_TARGET_THUMBV7NEON_UNKNOWN_LINUX_GNUEABIHF_LINKER="arm-linux-gnueabihf-gcc" \
CARGO_TARGET_THUMBV7NEON_UNKNOWN_LINUX_GNUEABIHF_RUSTFLAGS="-C link-args=--sysroot=${SYSROOT_ARMV7HF}" \
CC_thumbv7neon_unknown_linux_gnueabihf="arm-linux-gnueabihf-gcc" \
CXX_thumbv7neon_unknown_linux_gnueabihf="arm-linux-gnueabihf-g++" \
PKG_CONFIG_LIBDIR_thumbv7neon_unknown_linux_gnueabihf="${SYSROOT_ARMV7HF}/usr/lib/pkgconfig:${SYSROOT_ARMV7HF}/usr/share/pkgconfig" \
PKG_CONFIG_PATH_thumbv7neon_unknown_linux_gnueabihf="${SYSROOT_ARMV7HF}/usr/lib/pkgconfig:${SYSROOT_ARMV7HF}/usr/share/pkgconfig" \
PKG_CONFIG_SYSROOT_DIR_thumbv7neon_unknown_linux_gnueabihf="${SYSROOT_ARMV7HF}"
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,26 @@ crates/%-sys/src/bindings.rs: FORCE
# Use the `_envoy` file as a target because
# * `.DELETE_ON_ERROR` does not work for directories, and
# * the name of the `.eap` file is annoying to predict.
# When building for all targets using a single image we cannot rely on wildcard matching.
target/aarch64/$(PACKAGE)/_envoy: ENVIRONMENT_SETUP=environment-setup-cortexa53-crypto-poky-linux
target/armv7hf/$(PACKAGE)/_envoy: ENVIRONMENT_SETUP=environment-setup-cortexa9hf-neon-poky-linux-gnueabi
target/%/$(PACKAGE)/_envoy: ARCH=$*
target/%/$(PACKAGE)/_envoy: target/%/$(PACKAGE)/$(PACKAGE) target/%/$(PACKAGE)/manifest.json target/%/$(PACKAGE)/LICENSE
ifeq (0, $(shell test -e /.dockerenv; echo $$?))
. /opt/axis/acapsdk/$(ENVIRONMENT_SETUP) && cd $(@D) && acap-build --build no-build .
else
$(DOCKER_RUN) sh -c ". /opt/axis/acapsdk/environment-setup-* && acap-build --build no-build ."
endif
touch $@

target/%/$(PACKAGE)/manifest.json: apps/$(PACKAGE)/manifest.json
mkdir -p $(dir $@)
cp $< $@

target/%/$(PACKAGE)/LICENSE: apps/$(PACKAGE)/LICENSE
mkdir -p $(dir $@)
cp $< $@

# The target triple and the name of the docker image do not match, so
# at some point we need to map one to the other. It might as well be here.
target/aarch64/$(PACKAGE)/_envoy: target/aarch64-unknown-linux-gnu/release/$(PACKAGE)
Expand All @@ -212,5 +231,9 @@ target/%/$(PACKAGE)/_envoy: apps/$(PACKAGE)/manifest.json apps/$(PACKAGE)/LICENS

# Always rebuild the executable because configuring accurate cache invalidation is annoying.
target/%/release/$(PACKAGE): FORCE
ifeq (0, $(shell test -e /.dockerenv; echo $$?))
cargo -v build --release --target $* --package $(PACKAGE)
else
cross -v build --release --target $* --package $(PACKAGE)
endif
touch $@ # This is a hack to make the `_envoy` target above always build
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ _Easy and safe [ACAP] apps using [Rust]_
## Quickstart guide

Build the `hello_world` example and create `.eap` files in the `target/acap/` directory like

```sh
docker build --tag acap-rs .
docker run \
--interactive \
--rm \
--tty \
--user $(id -u):$(id -g) \
--volume $(pwd):$(pwd) \
--workdir $(pwd) \
acap-rs \
make build PACKAGE=hello_world
```

This works with any of the [example applications](#example-applications).

## Advanced setup

Ensure global prerequisites are installed:

* Docker
Expand All @@ -22,15 +41,7 @@ source ./init_env.sh
make sync_env
```

Build the `hello_world` example and create `.eap` files in the `target/acap/` directory like

```sh
PACKAGE=hello_world make build
```

This works with any of the [example applications](#example-applications).

Other important workflows are documented in the [Makefile](./Makefile) and can be listed with `make help`.
Important workflows are documented in the [Makefile](./Makefile) and can now be listed with `make help`.

## Example applications

Expand Down
4 changes: 2 additions & 2 deletions apps/hello_world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ edition.workspace = true

[dependencies]
log = { workspace = true }
env_logger = { workspace = true }
syslog = { workspace = true }

app-logging = { workspace = true }
4 changes: 1 addition & 3 deletions apps/hello_world/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
//! A simple hello world application
//!
//! Uses some common logging crates to demonstrate
//! Uses some common app-logging crates to demonstrate
//! 1. how to use them in an application, and
//! 2. how to build and bundle them as an application.
use log::info;

mod app_logging;

fn main() {
app_logging::init_logger();
info!("Hello World!");
Expand Down
5 changes: 2 additions & 3 deletions apps/licensekey_handler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ edition.workspace = true

[dependencies]
log = { workspace = true }
env_logger = { workspace = true }
syslog = { workspace = true }

licensekey = { workspace = true }
licensekey = { workspace = true }
app-logging = { workspace = true }
32 changes: 0 additions & 32 deletions apps/licensekey_handler/src/app_logging.rs

This file was deleted.

9 changes: 5 additions & 4 deletions apps/licensekey_handler/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! A simple example application demonstrating how the licensekey crate may be used
use log::{info, warn};
use std::ffi::{CStr, CString};
use std::os::unix::ffi::OsStrExt;
use std::{
ffi::{CStr, CString},
os::unix::ffi::OsStrExt,
};

mod app_logging;
use log::{info, warn};

const APP_ID: i32 = 0;
const MAJOR_VERSION: i32 = 1;
Expand Down
9 changes: 9 additions & 0 deletions crates/app-logging/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "app-logging"
version = "0.0.0"
edition.workspace = true

[dependencies]
log = { workspace = true }
env_logger = { workspace = true }
syslog = { workspace = true }
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Utilities for managing logging in an application.
//! Utilities for managing app-logging in an application.
use std::env;
use std::io::IsTerminal;
use std::{env, io::IsTerminal};

use log::debug;

Expand All @@ -12,7 +11,7 @@ fn init_syslog() {
log::set_max_level(log::LevelFilter::Debug);
}

/// Set up logging as appropriate for the environment, then run the provided function.
/// Set up app-logging as appropriate for the environment, then run the provided function.
///
/// If stdout is a terminal, write to stderr.
/// Otherwise, write to the system logger.
Expand Down
6 changes: 4 additions & 2 deletions crates/licensekey/src/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ pub fn licensekey_get_state_string(state_code: c_int) -> Option<CString> {
#[cfg(not(target_arch = "x86_64"))]
#[cfg(test)]
mod tests {
use super::*;
use licensekey_sys::LicenseKeyState;
use std::ffi::CString;

use licensekey_sys::LicenseKeyState;

use super::*;

#[test]
fn licensekey_verify_does_not_panic() {
let app_name = CString::new("test_app").unwrap();
Expand Down
15 changes: 15 additions & 0 deletions docker/install_rust.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env sh
set -eux
wget "https://static.rust-lang.org/rustup/archive/1.26.0/x86_64-unknown-linux-gnu/rustup-init"
echo "0b2f6c8f85a3d02fde2efc0ced4657869d73fccfce59defb4e8d29233116e6db rustup-init" | sha256sum -c -
chmod +x rustup-init

./rustup-init \
--default-host x86_64-unknown-linux-gnu \
--default-toolchain $(grep "channel" rust-toolchain.toml | cut -d '"' -f 2) \
--no-modify-path \
--profile minimal \
-y

rm rustup-init
chmod -R a+w $RUSTUP_HOME $CARGO_HOME

0 comments on commit 331a4b4

Please sign in to comment.