diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index d81af64..0000000 --- a/Dockerfile +++ /dev/null @@ -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 - -############################# -# cpp‑httplib (header‑only) # -############################# -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 pre‑built static libs + headers -COPY --from=builder /usr/local /usr/local - -ENV CFLAGS="-static" \ - CXXFLAGS="-static" \ - LDFLAGS="-static" - -WORKDIR /workspace - -ENTRYPOINT ["/bin/bash"] diff --git a/build.sh b/build.sh deleted file mode 100755 index 22a5e43..0000000 --- a/build.sh +++ /dev/null @@ -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 diff --git a/install_host.sh b/install_host_headers.sh similarity index 100% rename from install_host.sh rename to install_host_headers.sh diff --git a/install_toolchain.sh b/install_toolchain.sh new file mode 100755 index 0000000..15f55f5 --- /dev/null +++ b/install_toolchain.sh @@ -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 + + diff --git a/notes.txt b/notes.txt index e69de29..d59b7f2 100644 --- a/notes.txt +++ b/notes.txt @@ -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. + diff --git a/test/build.sh b/test/build.sh index c3cdf97..33a79b9 100755 --- a/test/build.sh +++ b/test/build.sh @@ -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 diff --git a/test/cmake/aarch64-linux-gnu.cmake b/test/cmake/aarch64-linux-gnu.cmake deleted file mode 100644 index 757e625..0000000 --- a/test/cmake/aarch64-linux-gnu.cmake +++ /dev/null @@ -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") \ No newline at end of file diff --git a/test/shell.sh b/test/shell.sh deleted file mode 100755 index 16179fe..0000000 --- a/test/shell.sh +++ /dev/null @@ -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" -