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 # Copy binary stage
FROM --platform=$BUILDPLATFORM alpine:3.17.3 AS ui-build FROM --platform=$BUILDPLATFORM alpine:3.18.0 as binary
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
ARG TARGETPLATFORM ARG TARGETPLATFORM
RUN mkdir -p /release/$TARGETPLATFORM
COPY . $PROJECT_PATH COPY target/x86_64-unknown-linux-musl/release/chirpstack /usr/bin/chirpstack-x86_64
COPY --from=ui-build $PROJECT_PATH/ui/build $PROJECT_PATH/ui/build 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 \ RUN case "$TARGETPLATFORM" in \
"linux/amd64") \ "linux/amd64") \
cd $PROJECT_PATH/chirpstack && make release-amd64; \ cp /usr/bin/chirpstack-x86_64 /usr/bin/chirpstack; \
cp $PROJECT_PATH/target/release/chirpstack /release/$TARGETPLATFORM; \
;; \ ;; \
"linux/arm/v7") \ "linux/arm/v7") \
cd $PROJECT_PATH/chirpstack && make release-armv7hf; \ cp /usr/bin/chirpstack-armv7hf /usr/bin/chirpstack; \
cp $PROJECT_PATH/target/armv7-unknown-linux-gnueabihf/release/chirpstack /release/$TARGETPLATFORM; \
;; \ ;; \
"linux/arm64") \ "linux/arm64") \
cd $PROJECT_PATH/chirpstack && make release-arm64; \ cp /usr/bin/chirpstack-aarch64 /usr/bin/chirpstack; \
cp $PROJECT_PATH/target/aarch64-unknown-linux-gnu/release/chirpstack /release/$TARGETPLATFORM; \
;; \ ;; \
esac; esac;
# Final stage # Final stage
FROM debian:buster-slim as production FROM alpine:3.18.0
RUN apt-get update && \ RUN apk --no-cache add \
apt-get install -y \ ca-certificates
ca-certificates \
libpq5 \
&& rm -rf /var/lib/apt/lists/*
ARG TARGETPLATFORM COPY --from=binary /usr/bin/chirpstack /usr/bin/chirpstack
COPY --from=rust-build /release/$TARGETPLATFORM/chirpstack /usr/bin/chirpstack
COPY --from=rust-build /chirpstack/chirpstack/configuration/* /etc/chirpstack/
USER nobody:nogroup 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 ENV PROJECT_PATH=/chirpstack
RUN mkdir -p $PROJECT_PATH WORKDIR $PROJECT_PATH
RUN dpkg --add-architecture armhf ENTRYPOINT ["nix-shell"]
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

View File

@ -2,7 +2,15 @@
# Build distributable binaries. # Build distributable binaries.
dist: 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 # Set the versions
version: version:
@ -36,7 +44,7 @@ build-ui:
# Enters the devshell for ChirpStack development. # Enters the devshell for ChirpStack development.
devshell: 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. # Enters the devshell for ChirpStack UI development.
devshell-ui: devshell-ui:
@ -44,15 +52,11 @@ devshell-ui:
# Runs the tests # Runs the tests
test: test:
docker-compose run --rm chirpstack make test cd backend && cargo test
docker-compose run --rm chirpstack make test-lrwn cd chirpstack && make test
docker-compose run --rm chirpstack make test-lrwn-filters cd lrwn && make test
cd lrwn-filters && make test
# Starts the ChirpStack server (for testing only). # Starts the ChirpStack server (for testing only).
test-server: build-ui test-server: build-ui
docker-compose run --rm --service-ports chirpstack make test-server 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

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/" maintainer-scripts = "debian/"
systemd-units = { enable = true } systemd-units = { enable = true }
[package.metadata.rpm] [package.metadata.generate-rpm]
package = "chirpstack" 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] post_install_script = '''
"configuration/" = { path = "/etc/chirpstack/" } chown -R chirpstack:chirpstack /etc/chirpstack
"chirpstack.service" = { path = "/lib/systemd/system/chirpstack.service" } chmod 750 /etc/chirpstack
chmod 640 /etc/chirpstack/*.toml
[package.metadata.rpm.cargo] sed -i "s/\$MQTT_BROKER_HOST/localhost/" /etc/chirpstack/*.toml
buildflags = ["--release"] sed -i "s/\$POSTGRESQL_HOST/localhost/" /etc/chirpstack/*.toml
sed -i "s/\$REDIS_HOST/localhost/" /etc/chirpstack/*.toml
'''
[package.metadata.rpm.targets] assets = [
chirpstack = { path = "/usr/bin/chirpstack" } { 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 .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') PKG_VERSION := $(shell cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
debug-amd64: debug-amd64:
apt-get install -y libpq-dev:amd64 cross build --target x86_64-unknown-linux-musl
cargo build
debug-armv7hf: dist:
# We can not install this by default, as only one libpq-dev version can be # Keep these in this order, as aarch64 is based on Debian Buster (older),
# installed at a time. # the others on Bullseye. For some build scripts we want to build against
apt-get install -y libpq-dev:armhf # least recent LIBC.
BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/arm-linux-gnueabihf" \ cross build --target aarch64-unknown-linux-musl --release
PKG_CONFIG_SYSROOT_DIR="/usr/arm-linux-gnueabihf" \ cross build --target x86_64-unknown-linux-musl --release
cargo build --target armv7-unknown-linux-gnueabihf cross build --target armv7-unknown-linux-musleabihf --release
debug-arm64: cargo deb --target x86_64-unknown-linux-musl --no-build --no-strip
apt-get install -y libpq-dev:arm64 cargo deb --target armv7-unknown-linux-musleabihf --no-build --no-strip
BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/aarch64-linux-gnu" \ cargo deb --target aarch64-unknown-linux-musl --no-build --no-strip
PKG_CONFIG_SYSROOT_DIR="/usr/aarch64-linux-gnu" \
cargo build --target aarch64-unknown-linux-gnu
release-amd64: cargo generate-rpm --target x86_64-unknown-linux-musl --target-dir ../target
apt-get install -y libpq-dev:amd64 cargo generate-rpm --target armv7-unknown-linux-musleabihf --target-dir ../target
cargo build --release cargo generate-rpm --target aarch64-unknown-linux-musl --target-dir ../target
release-armv7hf: mkdir -p ../dist
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
release-arm64: cp ../target/x86_64-unknown-linux-musl/debian/*.deb ../dist
apt-get install -y libpq-dev:arm64 cp ../target/armv7-unknown-linux-musleabihf/debian/*.deb ../dist
BINDGEN_EXTRA_CLANG_ARGS="--sysroot=/usr/aarch64-linux-gnu" \ cp ../target/aarch64-unknown-linux-musl/debian/*.deb ../dist
PKG_CONFIG_SYSROOT_DIR="/usr/aarch64-linux-gnu" \
cargo build --target aarch64-unknown-linux-gnu --release
dist: release-deb-amd64 \ cp ../target/x86_64-unknown-linux-musl/generate-rpm/*.rpm ../dist
release-deb-armv7hf \ cp ../target/armv7-unknown-linux-musleabihf/generate-rpm/*.rpm ../dist
release-deb-arm64 \ cp ../target/aarch64-unknown-linux-musl/generate-rpm/*.rpm ../dist
release-rpm-amd64 \
release-rpm-armv7hf \
release-rpm-arm64 \
release-targz-amd64 \
release-targz-armv7hf \
release-targz-arm64 \
release-deb-amd64: tar -czvf ../dist/chirpstack_$(PKG_VERSION)_amd64.tar.gz -C ../target/x86_64-unknown-linux-musl/release chirpstack
apt-get install -y libpq-dev:amd64 tar -czvf ../dist/chirpstack_$(PKG_VERSION)_armv7hf.tar.gz -C ../target/armv7-unknown-linux-musleabihf/release chirpstack
mkdir -p /chirpstack/dist tar -czvf ../dist/chirpstack_$(PKG_VERSION)_arm64.tar.gz -C ../target/aarch64-unknown-linux-musl/release chirpstack
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
test: test:
cargo fmt --check cargo fmt --check
cargo clippy --no-deps cargo clippy --no-deps
cargo test cargo test
test-lrwn:
cd ../lrwn && make test
test-lrwn-filters:
cd ../lrwn-filters && make test
test-server: debug-amd64 test-server: debug-amd64
../target/debug/chirpstack -c ./configuration ../target/x86_64-unknown-linux-musl/debug/chirpstack -c ./configuration
dbshell: dbshell:
psql -h postgres -U chirpstack chirpstack psql -h postgres -U chirpstack chirpstack

View File

@ -31,7 +31,6 @@ RUN echo "Building zlib" && \
make && make install && \ make && make install && \
rm -r /tmp/* rm -r /tmp/*
RUN echo "Building libpq" && \ RUN echo "Building libpq" && \
cd /tmp && \ cd /tmp && \
curl -fLO "https://ftp.postgresql.org/pub/source/v$POSTGRESQL_VERSION/postgresql-$POSTGRESQL_VERSION.tar.gz" && \ 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 && \ make && make install && \
rm -r /tmp/* rm -r /tmp/*
RUN echo "Building libpq" && \ RUN echo "Building libpq" && \
cd /tmp && \ cd /tmp && \
curl -fLO "https://ftp.postgresql.org/pub/source/v$POSTGRESQL_VERSION/postgresql-$POSTGRESQL_VERSION.tar.gz" && \ 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 dockerfile: Dockerfile-devel
volumes: volumes:
- ./:/chirpstack - ./:/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: depends_on:
- postgres - postgres
- redis - redis

View File

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