Migrate tauri from portmaster-ui to desktop/tauri. Update build system

This commit is contained in:
Patrick Pacher
2024-03-22 11:45:18 +01:00
parent ac23ce32a1
commit d524bce166
35 changed files with 10960 additions and 42 deletions

261
Earthfile
View File

@@ -1,10 +1,23 @@
VERSION --arg-scope-and-set 0.8
VERSION --arg-scope-and-set --global-cache 0.8
ARG --global go_version = 1.21
ARG --global distro = alpine3.18
ARG --global node_version = 18
ARG --global outputDir = "./dist"
# The list of rust targets we support. They will be automatically converted
# to GOOS, GOARCH and GOARM when building go binaries. See the +RUST_TO_GO_ARCH_STRING
# helper method at the bottom of the file.
ARG --global architectures = "x86_64-unknown-linux-gnu" \
"aarch64-unknown-linux-gnu" \
"armv7-unknown-linux-gnueabihf" \
"arm-unknown-linux-gnueabi" \
"x86_64-pc-windows-gnu"
# Import the earthly rust lib since it already provides some useful
# build-targets and methods to initialize the rust toolchain.
IMPORT github.com/earthly/lib/rust:3.0.2 AS rust
go-deps:
FROM golang:${go_version}-${distro}
WORKDIR /go-workdir
@@ -24,7 +37,6 @@ go-deps:
COPY go.sum .
RUN go mod download
go-base:
FROM +go-deps
@@ -42,6 +54,15 @@ go-base:
# ./assets
COPY assets ./assets
# updates all go dependencies and runs go mod tidy, saving go.mod and go.sum locally.
update-go-deps:
FROM +go-base
RUN go get -u ./..
RUN go mod tidy
SAVE ARTIFACT go.mod AS LOCAL go.mod
SAVE ARTIFACT --if-exists go.sum AS LOCAL go.sum
# mod-tidy runs 'go mod tidy', saving go.mod and go.sum locally.
mod-tidy:
FROM +go-base
@@ -61,6 +82,9 @@ build-go:
ARG GOARM
ARG CMDS=portmaster-start portmaster-core hub notifier
# Get the current version
DO +GET_VERSION
CACHE --sharing shared "$GOCACHE"
CACHE --sharing shared "$GOMODCACHE"
@@ -73,20 +97,17 @@ build-go:
# Build all go binaries from the specified in CMDS
FOR bin IN $CMDS
RUN go build -o "/tmp/build/" ./cmds/${bin}
RUN go build -o "/tmp/build/" ./cmds/${bin}
END
LET NAME = ""
FOR bin IN $(ls -1 "/tmp/build/")
SET NAME = "${outputDir}/${GOOS}_${GOARCH}/${bin}"
IF [ "${GOARM}" != "" ]
SET NAME = "${outputDir}/${GOOS}_${GOARCH}v${GOARM}/${bin}"
END
DO +GO_ARCH_STRING --goos="${GOOS}" --goarch="${GOARCH}" --goarm="${GOARM}"
SAVE ARTIFACT "/tmp/build/${bin}" AS LOCAL "${NAME}"
SAVE ARTIFACT "/tmp/build/${bin}" AS LOCAL "${outputDir}/${GO_ARCH_STRING}/${bin}"
END
SAVE ARTIFACT "/tmp/build/" ./output
# Test one or more go packages.
# Run `earthly +test-go` to test all packages
# Run `earthly +test-go --PKG="service/firewall"` to only test a specific package.
@@ -108,29 +129,19 @@ test-go:
END
test-go-all-platforms:
# Linux platforms:
BUILD +test-go --GOARCH=amd64 --GOOS=linux
BUILD +test-go --GOARCH=arm64 --GOOS=linux
BUILD +test-go --GOARCH=arm --GOOS=linux --GOARM=5
BUILD +test-go --GOARCH=arm --GOOS=linux --GOARM=6
BUILD +test-go --GOARCH=arm --GOOS=linux --GOARM=7
# Windows platforms:
BUILD +test-go --GOARCH=amd64 --GOOS=windows
BUILD +test-go --GOARCH=arm64 --GOOS=windows
LOCALLY
FOR arch IN ${architectures}
DO +RUST_TO_GO_ARCH_STRING --rustTarget="${arch}"
BUILD +test-go --GOARCH="${GOARCH}" --GOOS="${GOOS}" --GOARM="${GOARM}"
END
# Builds portmaster-start, portmaster-core, hub and notifier for all supported platforms
build-go-release:
# Linux platforms:
BUILD +build-go --GOARCH=amd64 --GOOS=linux
BUILD +build-go --GOARCH=arm64 --GOOS=linux
BUILD +build-go --GOARCH=arm --GOOS=linux --GOARM=5
BUILD +build-go --GOARCH=arm --GOOS=linux --GOARM=6
BUILD +build-go --GOARCH=arm --GOOS=linux --GOARM=7
# Windows platforms:
BUILD +build-go --GOARCH=amd64 --GOOS=windows
BUILD +build-go --GOARCH=arm64 --GOOS=windows
LOCALLY
FOR arch IN ${architectures}
DO +RUST_TO_GO_ARCH_STRING --rustTarget="${arch}"
BUILD +build-go --GOARCH="${GOARCH}" --GOOS="${GOOS}" --GOARM="${GOARM}"
END
# Builds all binaries from the cmds/ folder for linux/windows AMD64
# Most utility binaries are never needed on other platforms.
@@ -187,13 +198,199 @@ angular-project:
# Build the angular projects (portmaster-UI and tauri-builtin) in production mode
angular-release:
BUILD +angular-project --project=portmaster --dist=./dist --configuration=production --baseHref=/ui/modules/portmaster
BUILD +angular-project --project=tauri-builtin --dist=./dist/tauri-builtin --configuration=production --baseHref="/"
# Build the angular projects (portmaster-UI and tauri-builtin) in dev mode
angular-dev:
BUILD +angular-project --project=portmaster --dist=./dist --configuration=development --baseHref=/ui/modules/portmaster
BUILD +angular-project --project=tauri-builtin --dist=./dist/tauri-builtin --configuration=development --baseHref="/"
# A base target for rust to prepare the build container
rust-base:
FROM rust:1.76-bookworm
RUN apt-get update -qq
RUN apt-get install --no-install-recommends -qq \
autoconf \
autotools-dev \
libtool-bin \
clang \
cmake \
bsdmainutils \
g++-mingw-w64-x86-64 \
gcc-aarch64-linux-gnu \
gcc-arm-none-eabi \
gcc-arm-linux-gnueabi \
gcc-arm-linux-gnueabihf \
libgtk-3-dev \
libjavascriptcoregtk-4.1-dev \
libsoup-3.0-dev \
libwebkit2gtk-4.1-dev \
build-essential \
curl \
wget \
file \
libssl-dev \
libayatana-appindicator3-dev \
librsvg2-dev
# Add some required rustup components
RUN rustup component add clippy
RUN rustup component add rustfmt
# Install toolchains and targets
FOR arch IN ${architectures}
RUN rustup target add ${arch}
END
DO rust+INIT --keep_fingerprints=true
# For now we need tauri-cli 1.5 for bulding
DO rust+CARGO --args="install tauri-cli --version ^1.5.11"
tauri-src:
FROM +rust-base
WORKDIR /app/tauri
# --keep-ts is necessary to ensure that the timestamps of the source files
# are preserved such that Rust's incremental compilation works correctly.
COPY --keep-ts ./desktop/tauri/ .
COPY assets/data ./assets
COPY (+angular-project/dist/tauri-builtin --project=tauri-builtin --dist=./dist/tauri-builtin --configuration=production --baseHref="/") ./../angular/dist/tauri-builtin
build-tauri:
FROM +tauri-src
ARG --required target
ARG output="release/[^\./]+"
ARG bundle="none"
# if we want tauri to create the installer bundles we also need to provide all external binaries
# we need to do some magic here because tauri expects the binaries to include the rust target tripple.
# We already knwo that triple because it's a required argument. From that triple, we use +RUST_TO_GO_ARCH_STRING
# function from below to parse the triple and guess wich GOOS and GOARCH we need.
IF [ "${bundle}" != "none" ]
RUN mkdir /tmp/gobuild
RUN mkdir ./binaries
DO +RUST_TO_GO_ARCH_STRING --rustTarget="${target}"
RUN echo "GOOS=${GOOS} GOARCH=${GOARCH} GOARM=${GOARM} GO_ARCH_STRING=${GO_ARCH_STRING}"
COPY (+build-go/output --GOOS="${GOOS}" --CMDS="portmaster-start portmaster-core" --GOARCH="${GOARCH}" --GOARM="${GOARM}") /tmp/gobuild
LET dest=""
FOR bin IN $(ls /tmp/gobuild)
SET dest="./binaries/${bin}-${target}"
IF [ -z "${bin##*.exe}" ]
SET dest = "./binaries/${bin%.*}-${target}.exe"
END
RUN echo "Copying ${bin} to ${dest}"
RUN cp "/tmp/gobuild/${bin}" "${dest}"
END
END
# The following is exected to work but doesn't. for whatever reason cargo-sweep errors out on the windows-toolchain.
#
# DO rust+CARGO --args="tauri build --bundles none --ci --target=${target}" --output="release/[^/\.]+"
#
# For, now, we just directly mount the rust target cache and call cargo ourself.
DO rust+SET_CACHE_MOUNTS_ENV
RUN --mount=$EARTHLY_RUST_TARGET_CACHE cargo tauri build --bundles "${bundle}" --ci --target="${target}"
DO rust+COPY_OUTPUT --output="${output}"
RUN ls target
tauri-release:
LOCALLY
ARG bundle="none"
FOR arch IN ${architectures}
BUILD +build-tauri --target="${arch}" --bundle="${bundle}"
END
release:
BUILD +build-go-release
BUILD +angular-release
# Takes GOOS, GOARCH and optionally GOARM and creates a string representation for file-names.
# in the form of ${GOOS}_{GOARCH} if GOARM is empty, otherwise ${GOOS}_${GOARCH}v${GOARM}.
# Thats the same format as expected and served by our update server.
#
# The result is available as GO_ARCH_STRING environment variable in the build context.
GO_ARCH_STRING:
FUNCTION
ARG --required goos
ARG --required goarch
ARG goarm
LET result = "${goos}_${goarch}"
IF [ "${goarm}" != "" ]
SET result = "${goos}_${goarch}v${goarm}"
END
ENV GO_ARCH_STRING="${result}"
# Takes a rust target (--rustTarget) and extracts architecture and OS and arm version
# and finally calls GO_ARCH_STRING.
#
# The result is available as GO_ARCH_STRING environment variable in the build context.
# It also exports GOOS, GOARCH and GOARM environment variables.
RUST_TO_GO_ARCH_STRING:
FUNCTION
ARG --required rustTarget
LET goos=""
IF [ -z "${rustTarget##*linux*}" ]
SET goos="linux"
ELSE
SET goos="windows"
END
LET goarch=""
LET goarm=""
IF [ -z "${rustTarget##*x86_64*}" ]
SET goarch="amd64"
ELSE IF [ -z "${rustTarget##*arm*}" ]
SET goarch="arm"
SET goarm="6"
IF [ -z "${rustTarget##*v7*}" ]
SET goarm="7"
END
ELSE IF [ -z "${rustTarget##*aarch64*}" ]
SET goarch="arm64"
ELSE
RUN echo "GOARCH not detected"; \
exit 1;
END
ENV GOOS="${goos}"
ENV GOARCH="${goarch}"
ENV GOARM="${goarm}"
DO +GO_ARCH_STRING --goos="${goos}" --goarch="${goarch}" --goarm="${goarm}"
GET_VERSION:
FUNCTION
LOCALLY
LET VERSION=$(git tag --points-at)
IF [ -z "${VERSION}"]
SET VERSION=$(git describe --tags --abbrev=0)§dev§build
ELSE IF ! git diff --quite
SET VERSION="${VERSION}§dev§build"
END
RUN echo "Version is ${VERSION}"
ENV VERSION="${VERSION}"
test:
LOCALLY
DO +GET_VERSION