Skip to content

Commit

Permalink
Replace ad hoc build system
Browse files Browse the repository at this point in the history
This is modelled after the corresponding changes in `acap-rs` and the
motivation is largely the same; in summary it makes it easier to share
improvements since we don't need to patch `Makefile`s that may have
diverged, and allows more sophisticated integration with Cargo such as
getting the name of test binaries build or using build scripts to
prepare additional files dynamically.

Enumerate workflows in readme

This gives potential users a preview of what is available without
downloading or installing anything.
  • Loading branch information
apljungquist committed Jun 11, 2024
1 parent a925a57 commit 569c65c
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 85 deletions.
27 changes: 0 additions & 27 deletions Cross.toml

This file was deleted.

60 changes: 60 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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 \
curl \
g++-aarch64-linux-gnu \
g++-arm-linux-gnueabihf \
inetutils-ping \
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}"
94 changes: 37 additions & 57 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,9 @@ PASS ?= pass
# Prevent pesky default rules from creating unexpected dependency graphs.
.SUFFIXES: ;

# Rebuild targets when marking them as phony directly is not enough.
FORCE:;
.PHONY: FORCE

DOCKER_RUN = docker run \
--volume ${CURDIR}/target/$(ARCH)/$(PACKAGE)/:/opt/app \
--user $(shell id -u):$(shell id -g) \
axisecp/acap-native-sdk:1.12-$(ARCH)-ubuntu22.04
EAP_INSTALL = cd ${CURDIR}/target/$(ARCH)/$(PACKAGE)/ \
&& cargo-acap-sdk containerize --docker-file $(CURDIR)/Dockerfile -- \
sh -c ". /opt/axis/acapsdk/environment-setup-* && eap-install.sh $(DEVICE_IP) $(PASS) $@"


## Verbs
Expand All @@ -54,40 +49,56 @@ build: target/aarch64/$(PACKAGE)/_envoy target/armv7hf/$(PACKAGE)/_envoy

## Install app on <DEVICE_IP> using password <PASS> and assuming architecture <ARCH>
install:
@ $(DOCKER_RUN) sh -c ". /opt/axis/acapsdk/environment-setup-* && eap-install.sh $(DEVICE_IP) $(PASS) install" \
cargo-acap-sdk install --docker-file ${CURDIR}/Dockerfile --address $(DEVICE_IP) --password $(PASS) --target $(ARCH) \
| grep -v '^to start your application type$$' \
| grep -v '^ eap-install.sh start$$'

## Remove app from <DEVICE_IP> using password <PASS> and assuming architecture <ARCH>
remove:
@ $(DOCKER_RUN) sh -c ". /opt/axis/acapsdk/environment-setup-* && eap-install.sh $(DEVICE_IP) $(PASS) remove"
@ $(EAP_INSTALL)

## Start app on <DEVICE_IP> using password <PASS> and assuming architecture <ARCH>
start:
@ $(DOCKER_RUN) sh -c ". /opt/axis/acapsdk/environment-setup-* && eap-install.sh $(DEVICE_IP) $(PASS) start" \
| grep -v '^to stop your application type$$' \
| grep -v '^ eap-install.sh stop$$'
@ # Don't match the line endings because docker replace LF with CR + LF when given `--tty`
@ $(EAP_INSTALL) \
| grep -v '^to stop your application type' \
| grep -v '^ eap-install.sh stop'

## Stop app on <DEVICE_IP> using password <PASS> and assuming architecture <ARCH>
stop:
@ $(DOCKER_RUN) sh -c ". /opt/axis/acapsdk/environment-setup-* && eap-install.sh $(DEVICE_IP) $(PASS) stop"
@ $(EAP_INSTALL)

## Build and run app directly on <DEVICE_IP> assuming architecture <ARCH>
##
## Forwards the following environment variables to the remote process:
## Prerequisites:
##
## * `RUST_LOG`
## * `RUST_LOG_STYLE`
## * The app is installed on the device.
## * The app is stopped.
## * The device has SSH enabled the ssh user root configured.
## * The device is added to `knownhosts`.
run:
cargo-acap-sdk run \
--docker-file $(CURDIR)/Dockerfile \
--password $(PASS) \
--target $(ARCH) \
--address $(DEVICE_IP) \
--package $(PACKAGE)

## Build and execute unit tests on <DEVICE_IP> assuming architecture <ARCH>
##
## Prerequisites:
##
## * The app is installed on the device.
## * The app is stopped.
## * The device has SSH enabled the ssh user root configured.
run: target/$(ARCH)/$(PACKAGE)/$(PACKAGE)
scp $< root@$(DEVICE_IP):/usr/local/packages/$(PACKAGE)/$(PACKAGE)
ssh root@$(DEVICE_IP) \
"cd /usr/local/packages/$(PACKAGE) && su - acap-$(PACKAGE) -s /bin/sh --preserve-environment -c '$(if $(RUST_LOG_STYLE),RUST_LOG_STYLE=$(RUST_LOG_STYLE) )$(if $(RUST_LOG),RUST_LOG=$(RUST_LOG) )./$(PACKAGE)'"
## * The device is added to `knownhosts`.
test:
cargo-acap-sdk test \
--docker-file $(CURDIR)/Dockerfile \
--password $(PASS) \
--target $(ARCH) \
--address $(DEVICE_IP) \
--package $(PACKAGE)

## Checks
## ------
Expand All @@ -97,12 +108,14 @@ check_all: check_build check_docs check_format check_lint
.PHONY: check_all

## Check that all crates can be built
check_build: target/aarch64/$(PACKAGE)/_envoy target/armv7hf/$(PACKAGE)/_envoy
check_build:
cargo-acap-sdk build

.PHONY: check_build

## Check that docs can be built
check_docs:
RUSTDOCFLAGS="-Dwarnings" cross doc \
cargo-acap-sdk containerize --docker-file $(CURDIR)/Dockerfile --docker-env RUSTFLAGS="-Dwarnings" -- cargo doc \
--document-private-items \
--no-deps \
--target aarch64-unknown-linux-gnu
Expand All @@ -115,7 +128,7 @@ check_format:

## Check that the code is free of lints
check_lint:
RUSTFLAGS="-Dwarnings" cross clippy \
cargo-acap-sdk containerize --docker-file $(CURDIR)/Dockerfile --docker-env RUSTFLAGS="-Dwarnings" -- cargo clippy \
--all-targets \
--no-deps \
--target aarch64-unknown-linux-gnu
Expand All @@ -137,36 +150,3 @@ fix_lint:

## Nouns
## =====

# Stage the files that will be packaged outside the source tree to avoid
# * cluttering the source tree and `.gitignore` with build artifacts, and
# * having the same file be built for different targets at different times.
# 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.
target/%/$(PACKAGE)/_envoy: ARCH=$*
target/%/$(PACKAGE)/_envoy: target/%/$(PACKAGE)/$(PACKAGE) target/%/$(PACKAGE)/manifest.json target/%/$(PACKAGE)/LICENSE
$(DOCKER_RUN) sh -c ". /opt/axis/acapsdk/environment-setup-* && acap-build --build no-build ."
touch $@

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

target/%/$(PACKAGE)/LICENSE: 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)/$(PACKAGE): target/aarch64-unknown-linux-gnu/release/$(PACKAGE)
mkdir -p $(dir $@)
cp $< $@

target/armv7hf/$(PACKAGE)/$(PACKAGE): target/thumbv7neon-unknown-linux-gnueabihf/release/$(PACKAGE)
mkdir -p $(dir $@)
cp $< $@

# Always rebuild the executable because configuring accurate cache invalidation is annoying.
target/%/release/$(PACKAGE): FORCE
cross -v build --release --target $* --package $(PACKAGE)
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Ensure global prerequisites are installed:

* Docker
* Rust e.g. [using rustup](https://www.rust-lang.org/tools/install)
* Cross e.g. like `cargo install cross`
* `cargo-acap-sdk` like `cargo install --git https://github.com/AxisCommunications/acap-rs.git --rev 452583a5898e233ec3e2a391923b8971fe7f342b cargo-acap-sdk`

Build the app and create `.eap` files in the `target/acap/` directory like:

Expand All @@ -34,6 +34,7 @@ Verbs:
start: Start app on <DEVICE_IP> using password <PASS> and assuming architecture <ARCH>
stop: Stop app on <DEVICE_IP> using password <PASS> and assuming architecture <ARCH>
run: Build and run app directly on <DEVICE_IP> assuming architecture <ARCH>
test: Build and execute unit tests on <DEVICE_IP> assuming architecture <ARCH>

Checks:
check_all: Run all other checks
Expand All @@ -47,6 +48,34 @@ Fixes:
fix_lint: Attempt to fix lints automatically
```

Yet more useful workflows can be listed like

```console
$ cargo-acap-sdk help
ACAP analog to `cargo` for building and deploying apps

Usage: cargo-acap-sdk <COMMAND>

Commands:
build Build app(s)
run Build executable for app(s) and run on the device, impersonating a user or the app
test Build test(s) and run on the device, impersonating a user or the app
install Build app(s) and install on the device
start TODO: Implement; Start app(s) on the device
stop TODO: Implement; Stop app(s) on the device
uninstall TODO: Implement; Uninstall app(s) on the device
containerize Run the provided program in a container
completions Print shell completion script for this program
help Print this message or the help of the given subcommand(s)

Options:
-h, --help Print help
-V, --version Print version
```

Note that the shell completions may not work when using the program as a cargo plugin like
`cargo acap-sdk` (note the difference between ` ` and `-`).

## License

[MIT](LICENSE)
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
14 changes: 14 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,17 @@ fn main() {
app_logging::init_logger();
info!("Hello World!");
}

#[cfg(test)]
mod tests {
#[test]
fn tautology() {
assert!(true)
}

#[test]
#[ignore]
fn contradiction() {
assert!(true)
}
}

0 comments on commit 569c65c

Please sign in to comment.