Skip to content

Commit

Permalink
chore: Revamp build system to overcome Cross issues
Browse files Browse the repository at this point in the history
This commit completely refactor the build system for akri:
 - Rust components are now built within docker using XX for
   cross-compilation
 - Made samples clear in a separate Makefile to ease with them getting
   out of the main repo soon
 - Remove cross intermediate image
 - Use buildx to build for multiple architectures
 - Merge largely similar Dockerfiles
 - Upgrade opcua-monitoring-broker to .NET 6
 - Upgrade opencv intermediate image to .NET 6 (the onvif broker still
   needs migration)

Signed-off-by: Nicolas Belouin <[email protected]>
  • Loading branch information
diconico07 committed Oct 12, 2023
1 parent a233713 commit 15743ae
Show file tree
Hide file tree
Showing 33 changed files with 258 additions and 810 deletions.
89 changes: 10 additions & 79 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,80 +1,11 @@

# Ignore everything
**

# The anomaly detection app is built using a `docker build` command,
# so the source code needs to be available to the container
# build
!samples/apps/anomaly-detection-app

# ONVIF broker is built using a `docker build` command, so
# the source code needs to be available to the DotNet container
# build
!samples/brokers/onvif-video-broker

# OPC UA broker is built using a `docker build` command, so
# the source code needs to be available to the DotNet container
# build
!samples/brokers/opcua-monitoring-broker

# The streaming app is built using a `docker build` command, so
# the source code needs to be available to the container
# build
!samples/apps/video-streaming-app

# The Rust binaries are not built with a `docker build`
# command (they are built using Cargo Cross, which I think
# uses a docker run). Because of this, the Rust src and
# Cargo.toml files can be ignored by docker.


# Cross-build binaries need to be available
# It is not clear to me why !target/*/*/controller
# does not work here, but it doesn't seem to. So
# for now, explicitly specifying each cross-build
# target and configuration path.
!target/x86_64-unknown-linux-gnu/debug/controller
!target/x86_64-unknown-linux-gnu/release/controller
!target/x86_64-unknown-linux-gnu/debug/agent
!target/x86_64-unknown-linux-gnu/release/agent
!target/x86_64-unknown-linux-gnu/debug/agent-full
!target/x86_64-unknown-linux-gnu/release/agent-full
!target/x86_64-unknown-linux-gnu/debug/udev-video-broker
!target/x86_64-unknown-linux-gnu/release/udev-video-broker
!target/x86_64-unknown-linux-gnu/debug/webhook-configuration
!target/x86_64-unknown-linux-gnu/release/webhook-configuration
!target/x86_64-unknown-linux-gnu/release/udev-discovery-handler
!target/x86_64-unknown-linux-gnu/release/debug-echo-discovery-handler
!target/x86_64-unknown-linux-gnu/release/onvif-discovery-handler
!target/x86_64-unknown-linux-gnu/release/opcua-discovery-handler
!target/aarch64-unknown-linux-gnu/debug/controller
!target/aarch64-unknown-linux-gnu/release/controller
!target/aarch64-unknown-linux-gnu/debug/agent
!target/aarch64-unknown-linux-gnu/release/agent
!target/aarch64-unknown-linux-gnu/debug/agent-full
!target/aarch64-unknown-linux-gnu/release/agent-full
!target/aarch64-unknown-linux-gnu/debug/udev-video-broker
!target/aarch64-unknown-linux-gnu/release/udev-video-broker
!target/aarch64-unknown-linux-gnu/debug/webhook-configuration
!target/aarch64-unknown-linux-gnu/release/webhook-configuration
!target/aarch64-unknown-linux-gnu/release/udev-discovery-handler
!target/aarch64-unknown-linux-gnu/release/debug-echo-discovery-handler
!target/aarch64-unknown-linux-gnu/release/onvif-discovery-handler
!target/aarch64-unknown-linux-gnu/release/opcua-discovery-handler
!target/armv7-unknown-linux-gnueabihf/debug/controller
!target/armv7-unknown-linux-gnueabihf/release/controller
!target/armv7-unknown-linux-gnueabihf/debug/agent
!target/armv7-unknown-linux-gnueabihf/release/agent
!target/armv7-unknown-linux-gnueabihf/debug/agent-full
!target/armv7-unknown-linux-gnueabihf/release/agent-full
!target/armv7-unknown-linux-gnueabihf/debug/udev-video-broker
!target/armv7-unknown-linux-gnueabihf/release/udev-video-broker
!target/armv7-unknown-linux-gnueabihf/debug/webhook-configuration
!target/armv7-unknown-linux-gnueabihf/release/webhook-configuration
!target/armv7-unknown-linux-gnueabihf/release/udev-discovery-handler
!target/armv7-unknown-linux-gnueabihf/release/debug-echo-discovery-handler
!target/armv7-unknown-linux-gnueabihf/release/onvif-discovery-handler
!target/armv7-unknown-linux-gnueabihf/release/opcua-discovery-handler

# Cross toml file needs to be available for making the cross build containers
!Cross.toml
!Cargo.toml
!Cargo.lock
!agent
!controller
!discovery-handler-modules
!discovery-handlers
!discovery-utils
!samples
!shared
!webhooks
53 changes: 32 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
BUILD_AMD64 ?= 1
BUILD_ARM32 ?= 1
BUILD_ARM64 ?= 1
BUILDX_ARGS =
EXTRA_CARGO_ARGS =

PUSH ?=
LOAD ?=
LOCAL_ARCH = $(shell uname -m)
ifeq ($(LOAD), 1)
PLATFORMS ?= LOCAL_ARCH
$(intcmp 1,$(words $(PLATFORMS)), $(error Cannot load for more than one platform))
else
PLATFORMS ?= amd64 arm64 arm/v7
endif

null :=
space := $(null) #
comma := ,

DOCKER_PLATFORMS = $(subst $(space),$(comma),$(strip $(addprefix linux/, $(PLATFORMS))))

# Specify flag to build optimized release version of rust components.
# Set to be empty to use debug builds.
BUILD_RELEASE_FLAG ?= 1

# Space separated list of rust packages to not build such as the following to not build
# the udev discovery handler library or module: "akri-udev udev-discovery-handler"
PACKAGES_TO_EXCLUDE ?=

# Incremental compilation causes rustc to save additional information to disk which will be
# reused when recompiling the crate, improving re-compile times.
# The additional information is stored in the target directory.
# By default for cargo builds, it is enabled in debug mode and disabled in release mode.
CARGO_INCREMENTAL ?= 0

BUILD_SLIM_AGENT ?= 1
FULL_AGENT_EXECUTABLE_NAME ?= agent-full
# Specify which features of the Agent to build, namely which Discovery Handlers
# should be embedded if any. The "agent-full" feature must be enabled to use the embedded
# Discovery Handlers. IE: AGENT_FEATURES="agent-full onvif-feat opcua-feat udev-feat"
# should be embedded if any. IE: AGENT_FEATURES="onvif-feat opcua-feat udev-feat"
AGENT_FEATURES ?=

REGISTRY ?= devcaptest.azurecr.io
Expand All @@ -47,12 +49,21 @@ AMD64_SUFFIX = amd64
ARM32V7_SUFFIX = arm32v7
ARM64V8_SUFFIX = arm64v8

AMD64_TARGET = x86_64-unknown-linux-gnu
ARM32V7_TARGET = armv7-unknown-linux-gnueabihf
ARM64V8_TARGET = aarch64-unknown-linux-gnu

COMMON_DOCKER_BUILD_ARGS = $(if $(LOAD), --load) $(if $(PUSH), --push) --platform=$(DOCKER_PLATFORMS)
# Intermediate container defines
include build/intermediate-containers.mk

# Akri container defines
include build/akri-containers.mk
include build/samples.mk

.PHONY: all
all: akri samples opencv-base

.PHONY: push
push: PUSH = 1
push: all

.PHONY: load
load: LOAD = 1
load: all
95 changes: 8 additions & 87 deletions build/akri-containers.mk
Original file line number Diff line number Diff line change
@@ -1,38 +1,3 @@
USE_OPENCV_BASE_VERSION = 0.0.11

# Akri container defines
include build/akri-rust-containers.mk
include build/akri-dotnet-containers.mk
include build/akri-python-containers.mk

#
# Functions for building Agent with or without Discovery Handlers
#
# Build the Agent without any Discovery Handlers embedded
define agent_build_slim
CARGO_INCREMENTAL=$(CARGO_INCREMENTAL) PKG_CONFIG_ALLOW_CROSS=1 cross build $(if $(BUILD_RELEASE_FLAG), --release) --target=$(1) --manifest-path agent/Cargo.toml
endef

# Build the Agent with features that embed Discovery Handlers and rename the executable in case subsequently
# building a slim Agent
define agent_build_with_features
CARGO_INCREMENTAL=$(CARGO_INCREMENTAL) PKG_CONFIG_ALLOW_CROSS=1 cross build $(if $(BUILD_RELEASE_FLAG), --release) --target=$(1) --manifest-path agent/Cargo.toml \
--features "${AGENT_FEATURES}"
mv target/$(1)/$(if $(BUILD_RELEASE_FLAG),release,debug)/agent target/$(1)/$(if $(BUILD_RELEASE_FLAG),release,debug)/${FULL_AGENT_EXECUTABLE_NAME}
endef

#
#
# INSTALL-CROSS: install cargo cross building tool:
#
# `make install-cross`
#
#
.PHONY: install-cross
install-cross:
cargo install cross


#
#
# AKRI: make and push the images for akri:
Expand All @@ -46,58 +11,14 @@ install-cross:
# To make a slim and full Agent, with full agent executable renamed agent-full: `AGENT_FEATURES="agent-full onvif-feat opcua-feat udev-feat" BUILD_SLIM_AGENT=1 make akri-agent`
#
.PHONY: akri
akri: akri-build akri-docker-all
akri-build: install-cross akri-cross-build
akri-docker-all: akri-docker-controller akri-docker-agent akri-docker-udev akri-docker-onvif akri-docker-streaming akri-docker-opcua-monitoring akri-docker-anomaly-detection akri-docker-webhook-configuration akri-docker-debug-echo-discovery akri-docker-onvif-discovery akri-docker-opcua-discovery akri-docker-udev-discovery
akri: akri-agent akri-agent-full akri-controller akri-webhook-configuration akri-debug-echo-discovery-handler akri-onvif-discovery-handler akri-opcua-discovery-handler akri-udev-discovery-handler

akri-cross-build: akri-cross-build-amd64 akri-cross-build-arm32 akri-cross-build-arm64
akri-cross-build-amd64:
ifeq (1, $(BUILD_AMD64))
CARGO_INCREMENTAL=$(CARGO_INCREMENTAL) PKG_CONFIG_ALLOW_CROSS=1 cross build $(if $(BUILD_RELEASE_FLAG), --release) --target=$(AMD64_TARGET) --workspace --exclude agent $(foreach package,$(wordlist 1, 100, $(PACKAGES_TO_EXCLUDE)),--exclude $(package))
ifneq ($(AGENT_FEATURES),)
$(call agent_build_with_features,$(AMD64_TARGET))
endif
ifeq (1, $(BUILD_SLIM_AGENT))
$(call agent_build_slim,$(AMD64_TARGET))
endif
endif
akri-cross-build-arm32:
ifeq (1, $(BUILD_ARM32))
CARGO_INCREMENTAL=$(CARGO_INCREMENTAL) PKG_CONFIG_ALLOW_CROSS=1 cross build $(if $(BUILD_RELEASE_FLAG), --release) --target=$(ARM32V7_TARGET) --workspace --exclude agent $(foreach package,$(wordlist 1, 100, $(PACKAGES_TO_EXCLUDE)),--exclude $(package))
ifneq ($(AGENT_FEATURES),)
$(call agent_build_with_features,$(ARM32V7_TARGET))
endif
ifeq (1, $(BUILD_SLIM_AGENT))
$(call agent_build_slim,$(ARM32V7_TARGET))
endif
endif
akri-cross-build-arm64:
ifeq (1, ${BUILD_ARM64})
CARGO_INCREMENTAL=$(CARGO_INCREMENTAL) PKG_CONFIG_ALLOW_CROSS=1 cross build $(if $(BUILD_RELEASE_FLAG), --release) --target=$(ARM64V8_TARGET) --workspace --exclude agent $(foreach package,$(wordlist 1, 100, $(PACKAGES_TO_EXCLUDE)),--exclude $(package))
ifneq ($(AGENT_FEATURES),)
$(call agent_build_with_features,$(ARM64V8_TARGET))
endif
ifeq (1, $(BUILD_SLIM_AGENT))
$(call agent_build_slim,$(ARM64V8_TARGET))
endif
endif
akri-%:
docker buildx build $(COMMON_DOCKER_BUILD_ARGS) --build-arg AKRI_COMPONENT=$* --tag "$(PREFIX)/$(subst -handler,,$*):$(LABEL_PREFIX)" --build-arg EXTRA_CARGO_ARGS="$(if $(BUILD_RELEASE_FLAG), --release)" --file $(DOCKERFILE_DIR)/Dockerfile.rust .

# Rust targets
$(eval $(call add_rust_targets,controller,controller))
$(eval $(call add_rust_targets,agent,agent))
$(eval $(call add_rust_targets,agent-full,agent-full))
$(eval $(call add_rust_targets,udev,udev-video-broker))
$(eval $(call add_rust_targets,webhook-configuration,webhook-configuration))
$(eval $(call add_rust_targets,debug-echo-discovery,debug-echo-discovery))
$(eval $(call add_rust_targets,onvif-discovery,onvif-discovery))
$(eval $(call add_rust_targets,opcua-discovery,opcua-discovery))
$(eval $(call add_rust_targets,udev-discovery,udev-discovery))

# .NET targets
$(eval $(call add_onvif_target,onvif,onvif-video-broker))
$(eval $(call add_opcua_target,opcua-monitoring,opcua-monitoring-broker))

# Python targets
$(eval $(call add_python_target,anomaly-detection,anomaly-detection-app))
$(eval $(call add_python_target,streaming,video-streaming-app))
.PHONY: akri-agent-full
akri-agent-full:
ifneq (,$(strip $(AGENT_FEATURES)))
docker buildx build $(COMMON_DOCKER_BUILD_ARGS) --build-arg AKRI_COMPONENT=agent --build-arg EXTRA_CARGO_ARGS="$(if $(BUILD_RELEASE_FLAG), --release) -F agent-full,$(subst $(space),$(comma),$(AGENT_FEATURES))" --tag "$(PREFIX)/agent-full:$(LABEL_PREFIX)" --file $(DOCKERFILE_DIR).rust .
endif

51 changes: 1 addition & 50 deletions build/akri-dotnet-containers.mk
Original file line number Diff line number Diff line change
Expand Up @@ -44,53 +44,4 @@ define add_onvif_target
$(1)-docker-multi-arch-push:
$(ENABLE_DOCKER_MANIFEST) docker manifest push $(PREFIX)/$(2):$(LABEL_PREFIX)

endef

define add_opcua_target
akri-$(1): akri-build akri-docker-$(1)
akri-docker-$(1): $(1)-build $(1)-docker-per-arch $(1)-docker-multi-arch-create $(1)-docker-multi-arch-push
$(1)-build: $(1)-build-amd64 $(1)-build-arm32 $(1)-build-arm64
$(1)-docker-per-arch: $(1)-docker-per-arch-amd64 $(1)-docker-per-arch-arm32 $(1)-docker-per-arch-arm64

$(1)-build-amd64:
ifeq (1, ${BUILD_AMD64})
docker build $(CACHE_OPTION) -f $(DOCKERFILE_DIR)/Dockerfile.$(2) . -t $(PREFIX)/$(2):$(LABEL_PREFIX)-$(AMD64_SUFFIX) --build-arg OUTPUT_PLATFORM_TAG=3.1-bullseye-slim --build-arg DOTNET_PUBLISH_RUNTIME=linux-x64
endif
$(1)-build-arm32:
ifeq (1, ${BUILD_ARM32})
docker build $(CACHE_OPTION) -f $(DOCKERFILE_DIR)/Dockerfile.$(2) . -t $(PREFIX)/$(2):$(LABEL_PREFIX)-$(ARM32V7_SUFFIX) --build-arg OUTPUT_PLATFORM_TAG=3.1-bullseye-slim-$(ARM32V7_SUFFIX) --build-arg DOTNET_PUBLISH_RUNTIME=linux-arm
endif
$(1)-build-arm64:
ifeq (1, ${BUILD_ARM64})
docker build $(CACHE_OPTION) -f $(DOCKERFILE_DIR)/Dockerfile.$(2) . -t $(PREFIX)/$(2):$(LABEL_PREFIX)-$(ARM64V8_SUFFIX) --build-arg OUTPUT_PLATFORM_TAG=3.1-bullseye-slim-$(ARM64V8_SUFFIX) --build-arg DOTNET_PUBLISH_RUNTIME=linux-arm64
endif

$(1)-docker-per-arch-amd64:
ifeq (1, ${BUILD_AMD64})
docker push $(PREFIX)/$(2):$(LABEL_PREFIX)-$(AMD64_SUFFIX)
endif
$(1)-docker-per-arch-arm32:
ifeq (1, ${BUILD_ARM32})
docker push $(PREFIX)/$(2):$(LABEL_PREFIX)-$(ARM32V7_SUFFIX)
endif
$(1)-docker-per-arch-arm64:
ifeq (1, ${BUILD_ARM64})
docker push $(PREFIX)/$(2):$(LABEL_PREFIX)-$(ARM64V8_SUFFIX)
endif

$(1)-docker-multi-arch-create:
ifeq (1, ${BUILD_AMD64})
$(ENABLE_DOCKER_MANIFEST) docker manifest create --amend $(PREFIX)/$(2):$(LABEL_PREFIX) $(PREFIX)/$(2):$(LABEL_PREFIX)-$(AMD64_SUFFIX)
endif
ifeq (1, ${BUILD_ARM32})
$(ENABLE_DOCKER_MANIFEST) docker manifest create --amend $(PREFIX)/$(2):$(LABEL_PREFIX) $(PREFIX)/$(2):$(LABEL_PREFIX)-$(ARM32V7_SUFFIX)
endif
ifeq (1, ${BUILD_ARM64})
$(ENABLE_DOCKER_MANIFEST) docker manifest create --amend $(PREFIX)/$(2):$(LABEL_PREFIX) $(PREFIX)/$(2):$(LABEL_PREFIX)-$(ARM64V8_SUFFIX)
endif

$(1)-docker-multi-arch-push:
$(ENABLE_DOCKER_MANIFEST) docker manifest push $(PREFIX)/$(2):$(LABEL_PREFIX)

endef

endef
48 changes: 0 additions & 48 deletions build/akri-python-containers.mk

This file was deleted.

Loading

0 comments on commit 15743ae

Please sign in to comment.