Skip to content

Commit

Permalink
Merge pull request #42 from tphakala/dev
Browse files Browse the repository at this point in the history
feat(docker): add multiplatform build support
  • Loading branch information
tphakala authored Mar 3, 2024
2 parents 5e1bede + 699d673 commit 1b72ff6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
43 changes: 33 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
FROM golang:1.22.0-bookworm as build

# Use ARGs to define default build-time variables for TensorFlow version and target platform
ARG TENSORFLOW_VERSION=v2.14.0
ARG PLATFORM=linux_amd64
# Use TARGETPLATFORM arg provided by buildx to dynamically set platform
#ARG TARGETPLATFORM

FROM golang:1.22.0-bookworm as build

# Download and configure precompiled TensorFlow Lite C library
RUN curl -L \
https://github.com/tphakala/tflite_c/releases/download/${TENSORFLOW_VERSION}/tflite_c_${TENSORFLOW_VERSION}_${PLATFORM}.tar.gz | \
# Pass in ARGs after FROM to use them in build stage
ARG TENSORFLOW_VERSION
ARG TARGETPLATFORM

# Determine PLATFORM based on TARGETPLATFORM
RUN PLATFORM='unknown'; \
case "${TARGETPLATFORM}" in \
"linux/amd64") PLATFORM='linux_amd64' ;; \
"linux/arm64") PLATFORM='linux_arm64' ;; \
*) echo "Unsupported platform: '${TARGETPLATFORM}'" && exit 1 ;; \
esac; \
# Download and configure precompiled TensorFlow Lite C library for the determined platform
curl -L \
"https://github.com/tphakala/tflite_c/releases/download/${TENSORFLOW_VERSION}/tflite_c_${TENSORFLOW_VERSION}_${PLATFORM}.tar.gz" | \
tar -C "/usr/local/lib" -xz \
&& ldconfig

Expand All @@ -14,14 +27,21 @@ WORKDIR /root/src
# Download TensorFlow headers
RUN git clone --branch ${TENSORFLOW_VERSION} --depth 1 https://github.com/tensorflow/tensorflow.git

# Compile BirdNET-GO
# Compile BirdNET-Go
COPY . BirdNET-Go
RUN cd BirdNET-Go && make
RUN cd BirdNET-Go && make TARGETPLATFORM=${TARGETPLATFORM}

# Create final image
# Create final image using a multi-platform base image
FROM debian:bookworm-slim

# Install ALSA library and SOX
RUN apt-get update && apt-get install -y \
libasound2 \
sox \
&& rm -rf /var/lib/apt/lists/*

COPY --from=build /root/src/BirdNET-Go/bin /usr/bin/
COPY --from=build /usr/local/lib/libtensorflowlite_c.so /usr/local/lib/libtensorflowlite_c.so
COPY --from=build /usr/local/lib/libtensorflowlite_c.so /usr/local/lib/
RUN ldconfig

# Add symlink to /config directory where configs can be stored
Expand All @@ -31,5 +51,8 @@ RUN mkdir -p /root/.config && ln -s /config /root/.config/birdnet-go
VOLUME /data
WORKDIR /data

# Make port 8080 available to the world outside this container
EXPOSE 8080

ENTRYPOINT ["/usr/bin/birdnet-go"]
CMD ["realtime"]
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
BINARY_DIR := bin
BINARY_NAME := birdnet-go

# Extract GOOS and GOARCH from TARGETPLATFORM
ifeq ($(TARGETPLATFORM),linux/amd64)
GOOS := linux
GOARCH := amd64
endif
ifeq ($(TARGETPLATFORM),linux/arm64)
GOOS := linux
GOARCH := arm64
endif

# Common flags
CGO_FLAGS := CGO_ENABLED=1 CGO_CFLAGS="-I$(HOME)/src/tensorflow -DMA_NO_PULSEAUDIO"
LDFLAGS := -ldflags "-s -w"

# Default build for local development
build:
$(CGO_FLAGS) go build $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME)

# Build for Linux amd64
linux_amd64:
GOOS=linux GOARCH=amd64 $(CGO_FLAGS) go build $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME)

# Build for Linux arm64
linux_arm64:
GOOS=linux GOARCH=arm64 $(CGO_FLAGS) go build $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME)

# Windows build
windows:
GOOS=windows GOARCH=amd64 $(CGO_FLAGS) CC=x86_64-w64-mingw32-gcc go build $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME).exe

# macOS Intel build
macos_intel:
GOOS=darwin GOARCH=amd64 $(CGO_FLAGS) go build $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME)

# macOS ARM build
macos_arm:
GOOS=darwin GOARCH=arm64 $(CGO_FLAGS) go build $(LDFLAGS) -o $(BINARY_DIR)/$(BINARY_NAME)

Expand Down

0 comments on commit 1b72ff6

Please sign in to comment.