This commit is contained in:
Your Name 2025-05-26 20:36:29 +12:00
parent 037b690bce
commit f9f07a3bb8
8 changed files with 51 additions and 236 deletions

View File

@ -1,159 +0,0 @@
# syntax=docker/dockerfile:1
########################
# Stage 1: build deps #
########################
FROM alpine:latest AS builder
ARG B_TARGET_ARCH
ARG B_TARGET_TRIPLE
ARG B_CC
ARG B_CXX
ENV TARGET_ARCH=$B_TARGET_ARCH
ENV TARGET_TRIPLE=$B_TARGET_TRIPLE
ENV CC=$B_CC
ENV CXX=$B_CXX
# Enable static builds with musl
ENV CFLAGS="-static -O2" \
CXXFLAGS="-static -O2" \
LDFLAGS="-static"
# Install build dependencies
RUN apk update && \
apk add --no-cache \
build-base \
make \
cmake \
ninja \
git \
openssl-dev \
zlib-static \
bash \
wget \
curl \
linux-headers \
ccache \
mold \
musl-dev \
gcc-cross-embedded \
g++-cross-embedded \
gcc \
g++
RUN apk add --no-cache \
zlib-dev \
python3 \
bison \
flex \
texinfo \
gperf \
help2man \
gettext-dev \
autoconf \
automake \
libtool \
patch \
gawk \
xz-dev
# Configure mold as the default linker
ENV LD=mold
########################################
# nlohmann/json (compiled static .a) #
########################################
RUN git clone --depth 1 https://github.com/nlohmann/json.git /tmp/json
RUN cmake -S /tmp/json -B /tmp/json/build \
-DJSON_BuildTests=OFF \
-DJSON_MultipleHeaders=OFF \
-DBUILD_SHARED_LIBS=OFF && \
cmake --build /tmp/json/build --config Release && \
cmake --install /tmp/json/build
#############################
# cpphttplib (headeronly) #
#############################
RUN wget -q https://raw.githubusercontent.com/yhirose/cpp-httplib/master/httplib.h \
-O /usr/local/include/httplib.h
##################################
# libassert (build static .a) #
##################################
ARG LIBASSERT_VERSION=v2.1.5
RUN git clone --depth 1 --branch ${LIBASSERT_VERSION} https://github.com/jeremy-rifkin/libassert.git /tmp/libassert && \
cmake -S /tmp/libassert -B /tmp/libassert/build \
-DLIBASSERT_BUILD_TESTS=OFF \
-DLIBASSERT_BUILD_EXAMPLES=OFF \
-DLIBASSERT_BUILD_STATIC=ON && \
cmake --build /tmp/libassert/build --config Release && \
cmake --install /tmp/libassert/build
#####################
# zlib (static .a) #
#####################
ARG ZLIB_VERSION=1.3.1
RUN wget -q https://zlib.net/zlib-${ZLIB_VERSION}.tar.gz && \
tar xzf zlib-${ZLIB_VERSION}.tar.gz && \
cd zlib-${ZLIB_VERSION} && \
./configure --static && \
make -j$(nproc) && \
make install && \
cd .. && rm -rf zlib-${ZLIB_VERSION}*
##############################
# Reduce size of .a archives #
##############################
RUN strip --strip-unneeded /usr/local/lib/*.a
############################
# Stage 2: final build kit #
############################
FROM alpine:3.19
ARG B_TARGET_ARCH
ARG B_TARGET_TRIPLE
ARG B_CC
ARG B_CXX
ENV TARGET_ARCH=$B_TARGET_ARCH
ENV TARGET_TRIPLE=$B_TARGET_TRIPLE
ENV CC=$B_CC
ENV CXX=$B_CXX
# Install core toolchain for building apps
RUN apk update && \
apk add --no-cache \
build-base \
make \
cmake \
ninja \
git \
openssl-dev \
zlib-static \
bash \
wget \
curl \
linux-headers \
ccache \
mold \
musl-dev \
gcc-cross-embedded \
g++-cross-embedded \
gcc \
g++
# Configure mold as the default linker
ENV LD=mold
# Copy prebuilt static libs + headers
COPY --from=builder /usr/local /usr/local
ENV CFLAGS="-static" \
CXXFLAGS="-static" \
LDFLAGS="-static"
WORKDIR /workspace
ENTRYPOINT ["/bin/bash"]

View File

@ -1,27 +0,0 @@
#!/bin/bash
set -e
docker build \
--build-arg B_TARGET_ARCH=arm64 \
--build-arg B_TARGET_TRIPLE=aarch64-linux-musl \
--build-arg B_CC=aarch64-linux-musl-gcc \
--build-arg B_CXX=aarch64-linux-musl-g++ \
-t cpp-httplib-builder:arm64 \
.
docker build \
--build-arg B_TARGET_ARCH=amd64 \
--build-arg B_TARGET_TRIPLE=x86_64-linux-musl \
--build-arg B_CC=x86_64-alpine-linux-musl-gcc \
--build-arg B_CXX=x86_64-alpine-linux-musl-g++ \
-t cpp-httplib-builder:amd64 \
.
docker tag cpp-httplib-builder:arm64 gitea.jde.nz/public/cpp-httplib-builder:arm64
docker tag cpp-httplib-builder:amd64 gitea.jde.nz/public/cpp-httplib-builder:amd64
docker tag cpp-httplib-builder:amd64 gitea.jde.nz/public/cpp-httplib-builder:latest
docker push gitea.jde.nz/public/cpp-httplib-builder:arm64
docker push gitea.jde.nz/public/cpp-httplib-builder:amd64
docker push gitea.jde.nz/public/cpp-httplib-builder:latest

22
install_toolchain.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/bash
targets=(
"linux-x86_64-full"
"linux-arm64-full"
)
for target in ${targets[@]}; do
docker pull dockcross/${target}
done
BIN_DIR=${HOME}/.local/bin
mkdir -p ${BIN_DIR}
for target in ${targets[@]}; do
docker run dockcross/$target > ${BIN_DIR}/dockcross-$target && chmod u+x ${BIN_DIR}/dockcross-$target
echo "Installed ${BIN_DIR}/dockcross-$target"
done

View File

@ -0,0 +1,7 @@
This dockerfile is designed to allow c++ programs to be statically built using dockcross,
natively for x86_64 and cross-compiling for arm64 linux architectures (for raspberry pi's and quest 3 headsets), and link statically to these libraries built in the docker container:
- nlohmann/json https://github.com/nlohmann/json
- libassert from https://github.com/jeremy-rifkin/libassert
plus has the header for httplib ( https://github.com/yhirose/cpp-httplib ) in /usr/local/include.

View File

@ -2,6 +2,8 @@
set -e
SCRIPT_DIR=$(dirname $(realpath $0))
PROJECT=ipdemo
function title() {
@ -25,39 +27,33 @@ function build() {
docker volume create cppbuild-cache-$ARCH
# Directory to copy the executable to
OUTDIR="output"
OUTDIR="$SCRIPT_DIR/output"
WORKDIR="$SCRIPT_DIR/build/${ARCH}"
mkdir -p "$OUTDIR"
mkdir -p "$WORKDIR"
mkdir -p "$WORKDIR/.ccache"
echo "WORKDIR: $WORKDIR"
echo "OUTDIR: $OUTDIR"
echo "SCRIPT_DIR: $SCRIPT_DIR"
BUILD_IMAGE="gitea.jde.nz/public/cpp-httplib-builder:$ARCH"
# Build command to run in the container
BUILD_COMMAND="mkdir -p /app/build && \
cd /app/build && \
cmake -G Ninja /app/src && \
ninja -j$(nproc) && \
mkdir -p /app/src/$OUTDIR && \
cp /app/build/$PROJECT /app/src/$OUTDIR/$PROJECT.$ARCH && \
chown -R $(id -u):$(id -g) /app/src/$OUTDIR"
case $ARCH in
"arm64") export TARGET_TRIPLE="aarch64-linux-musl";;
"amd64") export TARGET_TRIPLE="x86_64-linux-musl";;
"arm64") export DOCKCROSS="dockcross-linux-arm64-full";;
"amd64") export DOCKCROSS="dockcross-linux-x86_64-full";;
*) echo "Unsupported architecture $ARCH"; exit 1;;
esac
export CC="ccache ${TARGET_TRIPLE}-gcc"
export CXX="ccache ${TARGET_TRIPLE}-g++"
# Run the build in the container
docker run --rm \
-v cppbuild-cache:/app/build \
-v $(pwd):/app/src \
-w /app/src \
-e CCACHE_DIR=/app/build/.ccache \
-e LD=mold \
-e CMAKE_BUILD_PARALLEL_LEVEL=$(nproc) \
-e NINJA_STATUS="[%f/%t] " \
$BUILD_IMAGE \
-c "$BUILD_COMMAND"
export CC="ccache gcc"
export CXX="ccache g++"
export LD="mold"
export CCACHE_DIR=${WORKDIR}/.ccache
cd $SCRIPT_DIR
$DOCKCROSS bash -c "cmake -B$WORKDIR . -GNinja"
$DOCKCROSS bash -c "ninja -j$(nproc) -Cbuild/${ARCH}"
cp "${WORKDIR}/$PROJECT" "${OUTDIR}/$PROJECT.$ARCH"
# Run the executable if it exists
if [ ! -f $OUTDIR/$PROJECT.$ARCH ]; then

View File

@ -1,21 +0,0 @@
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
# Specify the cross compilers
set(CMAKE_C_COMPILER /opt/cross/bin/aarch64-unknown-linux-musl-gcc)
set(CMAKE_CXX_COMPILER /opt/cross/bin/aarch64-unknown-linux-musl-g++)
# Where is the target environment
set(CMAKE_FIND_ROOT_PATH /opt/cross/aarch64-unknown-linux-musl)
# Search for programs in the build host directories
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# Search for libraries and headers in the target directories
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
# Set compiler flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8-a")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a")

View File

@ -1,3 +0,0 @@
#docker run --entrypoint "/bin/sh" -it --rm cpp-httplib-builder:latest -c "/bin/sh"
docker run -it --rm cpp-httplib-builder:latest -c "/bin/bash"