Refactor build scripts for cross-rs based cross compiling.

This commit is contained in:
Orne Brocaar 2023-05-29 14:41:30 +01:00
parent cd3594fd96
commit 1f89f30878
12 changed files with 83 additions and 260 deletions

View File

@ -1,79 +1,30 @@
# UI build stage
FROM --platform=$BUILDPLATFORM alpine:3.17.3 AS ui-build
ENV PROJECT_PATH=/chirpstack
RUN apk add --no-cache make git bash build-base nodejs npm yarn
RUN mkdir -p $PROJECT_PATH
COPY ./api/grpc-web $PROJECT_PATH/api/grpc-web
COPY ./ui $PROJECT_PATH/ui
RUN cd $PROJECT_PATH/ui && \
yarn install --network-timeout 600000 && \
yarn build
# ChirpStack build stage
FROM --platform=$BUILDPLATFORM rust:1.68.2-buster AS rust-build
ENV PROJECT_PATH=/chirpstack
RUN mkdir -p $PROJECT_PATH
RUN dpkg --add-architecture armhf
RUN dpkg --add-architecture arm64
RUN apt-get update && \
apt-get install -y \
make \
cmake \
git \
libpq-dev \
clang \
libclang-dev \
jq \
protobuf-compiler \
gcc-arm-linux-gnueabihf \
g++-arm-linux-gnueabihf \
gcc-aarch64-linux-gnu \
g++-aarch64-linux-gnu \
zlib1g-dev:armhf \
zlib1g-dev:arm64
# Copy binary stage
FROM --platform=$BUILDPLATFORM alpine:3.18.0 as binary
ARG TARGETPLATFORM
RUN mkdir -p /release/$TARGETPLATFORM
COPY . $PROJECT_PATH
COPY --from=ui-build $PROJECT_PATH/ui/build $PROJECT_PATH/ui/build
COPY target/x86_64-unknown-linux-musl/release/chirpstack /usr/bin/chirpstack-x86_64
COPY target/armv7-unknown-linux-musleabihf/release/chirpstack /usr/bin/chirpstack-armv7hf
COPY target/aarch64-unknown-linux-musl/release/chirpstack /usr/bin/chirpstack-aarch64
RUN case "$TARGETPLATFORM" in \
"linux/amd64") \
cd $PROJECT_PATH/chirpstack && make release-amd64; \
cp $PROJECT_PATH/target/release/chirpstack /release/$TARGETPLATFORM; \
cp /usr/bin/chirpstack-x86_64 /usr/bin/chirpstack; \
;; \
"linux/arm/v7") \
cd $PROJECT_PATH/chirpstack && make release-armv7hf; \
cp $PROJECT_PATH/target/armv7-unknown-linux-gnueabihf/release/chirpstack /release/$TARGETPLATFORM; \
cp /usr/bin/chirpstack-armv7hf /usr/bin/chirpstack; \
;; \
"linux/arm64") \
cd $PROJECT_PATH/chirpstack && make release-arm64; \
cp $PROJECT_PATH/target/aarch64-unknown-linux-gnu/release/chirpstack /release/$TARGETPLATFORM; \
cp /usr/bin/chirpstack-aarch64 /usr/bin/chirpstack; \
;; \
esac;
# Final stage
FROM debian:buster-slim as production
FROM alpine:3.18.0
RUN apt-get update && \
apt-get install -y \
ca-certificates \
libpq5 \
&& rm -rf /var/lib/apt/lists/*
RUN apk --no-cache add \
ca-certificates
ARG TARGETPLATFORM
COPY --from=rust-build /release/$TARGETPLATFORM/chirpstack /usr/bin/chirpstack
COPY --from=rust-build /chirpstack/chirpstack/configuration/* /etc/chirpstack/
COPY --from=binary /usr/bin/chirpstack /usr/bin/chirpstack
USER nobody:nogroup
ENTRYPOINT ["/usr/bin/chirpstack"]
ENTRYPOINT ["/usr/bin/chirpstack"]

View File

@ -1,38 +1,6 @@
FROM rust:1.68.2-buster
FROM nixos/nix
ENV PROJECT_PATH=/chirpstack
RUN mkdir -p $PROJECT_PATH
WORKDIR $PROJECT_PATH
RUN dpkg --add-architecture armhf
RUN dpkg --add-architecture arm64
RUN apt-get update && \
apt-get install -y \
make \
cmake \
git \
bash \
screen \
postgresql-client \
libpq-dev \
mosquitto-clients \
redis-tools \
rpm \
clang \
libclang-dev \
golang-cfssl \
jq \
protobuf-compiler \
gcc-arm-linux-gnueabihf \
g++-arm-linux-gnueabihf \
gcc-aarch64-linux-gnu \
g++-aarch64-linux-gnu \
zlib1g-dev:armhf \
zlib1g-dev:arm64
RUN cargo install diesel_cli --version 2.0.0 --no-default-features --features postgres
RUN cargo install cargo-deb
RUN cargo install cargo-rpm
RUN cargo install cargo-bitbake
WORKDIR $PROJECT_PATH/chirpstack
ENTRYPOINT ["nix-shell"]

View File

@ -2,7 +2,15 @@
# Build distributable binaries.
dist:
docker-compose run --rm --no-deps chirpstack make dist
cd chirpstack && make dist
# Install dev dependencies
dev-dependencies:
cargo install cross --version 0.2.5
cargo install diesel_cli --version 2.0.0 --no-default-features --features postgres
cargo install cargo-deb --version 1.43.1
cargo install cargo-bitbake --version 0.3.16
cargo install cargo-generate-rpm --version 0.11.0
# Set the versions
version:
@ -36,7 +44,7 @@ build-ui:
# Enters the devshell for ChirpStack development.
devshell:
docker-compose run --rm --service-ports --name chirpstack chirpstack bash
docker-compose run --rm --service-ports --name chirpstack chirpstack
# Enters the devshell for ChirpStack UI development.
devshell-ui:
@ -44,15 +52,11 @@ devshell-ui:
# Runs the tests
test:
docker-compose run --rm chirpstack make test
docker-compose run --rm chirpstack make test-lrwn
docker-compose run --rm chirpstack make test-lrwn-filters
cd backend && cargo test
cd chirpstack && make test
cd lrwn && make test
cd lrwn-filters && make test
# Starts the ChirpStack server (for testing only).
test-server: build-ui
docker-compose run --rm --service-ports chirpstack make test-server
# Update the Docker development images
update-images:
docker-compose build chirpstack
docker-compose build chirpstack-ui
docker-compose run --rm --service-ports chirpstack make test-server

View File

@ -1,37 +0,0 @@
%define __spec_install_post %{nil}
%define __os_install_post %{_dbpath}/brp-compress
%define debug_package %{nil}
Name: chirpstack
Summary: ChirpStack is an open-source LoRaWAN(TM) Network Server
Version: @@VERSION@@
Release: @@RELEASE@@%{?dist}
License: MIT
Group: Applications/System
Source0: %{name}-%{version}.tar.gz
URL: https://www.chirpstack.io/
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
%description
%{summary}
%prep
%setup -q
%install
rm -rf %{buildroot}
mkdir -p %{buildroot}
cp -a * %{buildroot}
sed -i "s/\$MQTT_BROKER_HOST/localhost/" %{buildroot}/etc/chirpstack/*.toml
sed -i "s/\$POSTGRESQL_HOST/localhost/" %{buildroot}/etc/chirpstack/*.toml
sed -i "s/\$REDIS_HOST/localhost/" %{buildroot}/etc/chirpstack/*.toml
%clean
rm -rf %{buildroot}
%files
%defattr(-,root,root,-)
%{_bindir}/*
/lib/systemd/system/chirpstack.service
%config /etc/chirpstack/*

View File

@ -1 +0,0 @@
../configuration

View File

@ -181,15 +181,26 @@ suggests = "postgresql, mosquitto, redis"
maintainer-scripts = "debian/"
systemd-units = { enable = true }
[package.metadata.rpm]
package = "chirpstack"
[package.metadata.generate-rpm]
pre_install_script = '''
id chirpstack &>/dev/null
if [[ $? -ne 0 ]]; then
useradd --system -U -M chirpstack -s /bin/false -d /etc/chirpstack
fi
'''
[package.metadata.rpm.files]
"configuration/" = { path = "/etc/chirpstack/" }
"chirpstack.service" = { path = "/lib/systemd/system/chirpstack.service" }
post_install_script = '''
chown -R chirpstack:chirpstack /etc/chirpstack
chmod 750 /etc/chirpstack
chmod 640 /etc/chirpstack/*.toml
[package.metadata.rpm.cargo]
buildflags = ["--release"]
sed -i "s/\$MQTT_BROKER_HOST/localhost/" /etc/chirpstack/*.toml
sed -i "s/\$POSTGRESQL_HOST/localhost/" /etc/chirpstack/*.toml
sed -i "s/\$REDIS_HOST/localhost/" /etc/chirpstack/*.toml
'''
[package.metadata.rpm.targets]
chirpstack = { path = "/usr/bin/chirpstack" }
assets = [
{ source = "target/release/chirpstack", dest = "/usr/bin/chirpstack", mode = "755" },
{ source = "configuration/*", dest = "/etc/chirpstack"},
{ source = "rpm/chirpstack.service", dest = "/lib/systemd/system/chirpstack.service"},
]

View File

@ -1,121 +1,47 @@
.PHONY: dist
TARGET_ARCH := $(shell rustc --print cfg |grep target_arch |sed 's/target_arch="\(.*\)"/\1/')
PKG_VERSION := $(shell cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
debug-amd64:
apt-get install -y libpq-dev:amd64
cargo build
cross build --target x86_64-unknown-linux-musl
debug-armv7hf:
# We can not install this by default, as only one libpq-dev version can be
# installed at a time.
apt-get install -y libpq-dev:armhf
BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/arm-linux-gnueabihf" \
PKG_CONFIG_SYSROOT_DIR="/usr/arm-linux-gnueabihf" \
cargo build --target armv7-unknown-linux-gnueabihf
dist:
# Keep these in this order, as aarch64 is based on Debian Buster (older),
# the others on Bullseye. For some build scripts we want to build against
# least recent LIBC.
cross build --target aarch64-unknown-linux-musl --release
cross build --target x86_64-unknown-linux-musl --release
cross build --target armv7-unknown-linux-musleabihf --release
debug-arm64:
apt-get install -y libpq-dev:arm64
BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/aarch64-linux-gnu" \
PKG_CONFIG_SYSROOT_DIR="/usr/aarch64-linux-gnu" \
cargo build --target aarch64-unknown-linux-gnu
cargo deb --target x86_64-unknown-linux-musl --no-build --no-strip
cargo deb --target armv7-unknown-linux-musleabihf --no-build --no-strip
cargo deb --target aarch64-unknown-linux-musl --no-build --no-strip
release-amd64:
apt-get install -y libpq-dev:amd64
cargo build --release
cargo generate-rpm --target x86_64-unknown-linux-musl --target-dir ../target
cargo generate-rpm --target armv7-unknown-linux-musleabihf --target-dir ../target
cargo generate-rpm --target aarch64-unknown-linux-musl --target-dir ../target
release-armv7hf:
apt-get install -y libpq-dev:armhf
BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/arm-linux-gnueabihf" \
PKG_CONFIG_SYSROOT_DIR="/usr/arm-linux-gnueabihf" \
cargo build --target armv7-unknown-linux-gnueabihf --release
mkdir -p ../dist
release-arm64:
apt-get install -y libpq-dev:arm64
BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/aarch64-linux-gnu" \
PKG_CONFIG_SYSROOT_DIR="/usr/aarch64-linux-gnu" \
cargo build --target aarch64-unknown-linux-gnu --release
cp ../target/x86_64-unknown-linux-musl/debian/*.deb ../dist
cp ../target/armv7-unknown-linux-musleabihf/debian/*.deb ../dist
cp ../target/aarch64-unknown-linux-musl/debian/*.deb ../dist
dist: release-deb-amd64 \
release-deb-armv7hf \
release-deb-arm64 \
release-rpm-amd64 \
release-rpm-armv7hf \
release-rpm-arm64 \
release-targz-amd64 \
release-targz-armv7hf \
release-targz-arm64 \
cp ../target/x86_64-unknown-linux-musl/generate-rpm/*.rpm ../dist
cp ../target/armv7-unknown-linux-musleabihf/generate-rpm/*.rpm ../dist
cp ../target/aarch64-unknown-linux-musl/generate-rpm/*.rpm ../dist
release-deb-amd64:
apt-get install -y libpq-dev:amd64
mkdir -p /chirpstack/dist
cargo deb
cp ../target/debian/*.deb /chirpstack/dist
release-deb-armv7hf:
apt-get install -y libpq-dev:armhf
mkdir -p /chirpstack/dist
BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/arm-linux-gnueabihf" \
PKG_CONFIG_SYSROOT_DIR="/usr/arm-linux-gnueabihf" \
cargo deb --target armv7-unknown-linux-gnueabihf
cp ../target/armv7-unknown-linux-gnueabihf/debian/*.deb /chirpstack/dist
release-deb-arm64:
apt-get install -y libpq-dev:arm64
mkdir -p /chirpstack/dist
BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/aarch64-linux-gnu" \
PKG_CONFIG_SYSROOT_DIR="/usr/aarch64-linux-gnu" \
cargo deb --target aarch64-unknown-linux-gnu
cp ../target/aarch64-unknown-linux-gnu/debian/*.deb /chirpstack/dist
release-rpm-amd64:
apt-get install -y libpq-dev:amd64
mkdir -p /chirpstack/dist
cargo rpm build
find ../target/release/rpmbuild/RPMS -type f -exec cp {} /chirpstack/dist \;
release-rpm-armv7hf:
apt-get install -y libpq-dev:armhf
mkdir -p /chirpstack/dist
BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/arm-linux-gnueabihf" \
PKG_CONFIG_SYSROOT_DIR="/usr/arm-linux-gnueabihf" \
cargo rpm build --target armv7-unknown-linux-gnueabihf
find ../target/armv7-unknown-linux-gnueabihf/release/rpmbuild/RPMS -type f -exec cp {} /chirpstack/dist \;
release-rpm-arm64:
apt-get install -y libpq-dev:arm64
mkdir -p /chirpstack/dist
BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/aarch64-linux-gnu" \
PKG_CONFIG_SYSROOT_DIR="/usr/aarch64-linux-gnu" \
cargo rpm build --target aarch64-unknown-linux-gnu
find ../target/aarch64-unknown-linux-gnu/release/rpmbuild/RPMS -type f -exec cp {} /chirpstack/dist \;
release-targz-amd64: release-amd64
mkdir -p /chirpstack/dist
tar -czvf /chirpstack/dist/chirpstack_$(PKG_VERSION)_amd64.tar.gz -C ../target/release chirpstack
release-targz-armv7hf: release-armv7hf
mkdir -p /chirpstack/dist
tar -czvf /chirpstack/dist/chirpstack_$(PKG_VERSION)_armv7hf.tar.gz -C ../target/armv7-unknown-linux-gnueabihf/release chirpstack
release-targz-arm64: release-arm64
mkdir -p /chirpstack/dist
tar -czvf /chirpstack/dist/chirpstack_$(PKG_VERSION)_arm64.tar.gz -C ../target/aarch64-unknown-linux-gnu/release chirpstack
tar -czvf ../dist/chirpstack_$(PKG_VERSION)_amd64.tar.gz -C ../target/x86_64-unknown-linux-musl/release chirpstack
tar -czvf ../dist/chirpstack_$(PKG_VERSION)_armv7hf.tar.gz -C ../target/armv7-unknown-linux-musleabihf/release chirpstack
tar -czvf ../dist/chirpstack_$(PKG_VERSION)_arm64.tar.gz -C ../target/aarch64-unknown-linux-musl/release chirpstack
test:
cargo fmt --check
cargo clippy --no-deps
cargo test
test-lrwn:
cd ../lrwn && make test
test-lrwn-filters:
cd ../lrwn-filters && make test
test-server: debug-amd64
../target/debug/chirpstack -c ./configuration
../target/x86_64-unknown-linux-musl/debug/chirpstack -c ./configuration
dbshell:
psql -h postgres -U chirpstack chirpstack

View File

@ -31,7 +31,6 @@ RUN echo "Building zlib" && \
make && make install && \
rm -r /tmp/*
RUN echo "Building libpq" && \
cd /tmp && \
curl -fLO "https://ftp.postgresql.org/pub/source/v$POSTGRESQL_VERSION/postgresql-$POSTGRESQL_VERSION.tar.gz" && \

View File

@ -29,7 +29,6 @@ RUN echo "Building zlib" && \
make && make install && \
rm -r /tmp/*
RUN echo "Building libpq" && \
cd /tmp && \
curl -fLO "https://ftp.postgresql.org/pub/source/v$POSTGRESQL_VERSION/postgresql-$POSTGRESQL_VERSION.tar.gz" && \

View File

@ -5,10 +5,6 @@ services:
dockerfile: Dockerfile-devel
volumes:
- ./:/chirpstack
- ./.rust/target:/chirpstack/target
- ./.rust/.cargo/registry/index:/usr/local/cargo/registry/index
- ./.rust/.cargo/registry/cache:/usr/local/cargo/registry/cache
- ./.rust/.cargo/git/db:/usr/local/cargo/git/db
depends_on:
- postgres
- redis

View File

@ -1,6 +1,9 @@
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-22.11.tar.gz") {} }:
pkgs.mkShell {
nativeBuildInputs = [
pkgs.pkg-config
];
buildInputs = [
pkgs.cacert
pkgs.rustup
@ -9,7 +12,11 @@ pkgs.mkShell {
pkgs.cmake
pkgs.clang
pkgs.postgresql
pkgs.openssl
pkgs.docker
];
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
BINDGEN_EXTRA_CLANG_ARGS = "-I${pkgs.llvmPackages.libclang.lib}/lib/clang/${pkgs.llvmPackages.libclang.version}/include";
DOCKER_BUILDKIT = "1";
NIX_STORE = "/nix/store";
}