From 683b18ee1ccc49a39be3b67279885086b8888c4a Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Mon, 31 Mar 2025 12:10:28 +0200 Subject: [PATCH 1/6] Bullseye build --- build/Dockerfile | 2 +- build/Dockerfile-android | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build/Dockerfile b/build/Dockerfile index aa8e567..374fd4b 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -1,5 +1,5 @@ # Stage 1: Build the static library -FROM golang:1.24.1-bookworm AS builder +FROM golang:1-bullseye AS builder # Install dependencies RUN apt-get update && apt-get install -y gcc binutils diff --git a/build/Dockerfile-android b/build/Dockerfile-android index f2399af..2611115 100644 --- a/build/Dockerfile-android +++ b/build/Dockerfile-android @@ -1,5 +1,5 @@ # Stage 1: Build the static library -FROM golang:1 AS builder +FROM golang:1-bullseye AS builder # Install dependencies RUN apt-get update && apt search openjdk && apt-get install -y gcc musl-tools build-essential devscripts openjdk-17-jdk From 9a184f355524fcd3a79150e1a01ee83fb18b33be Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Mon, 31 Mar 2025 13:11:24 +0200 Subject: [PATCH 2/6] separate dynamic and static build on linux --- .../workflows/build_libtestoptimization.yml | 14 ++-- build/{Dockerfile => Dockerfile-dynamic} | 8 +-- build/Dockerfile-static | 64 +++++++++++++++++++ 3 files changed, 75 insertions(+), 11 deletions(-) rename build/{Dockerfile => Dockerfile-dynamic} (74%) create mode 100644 build/Dockerfile-static diff --git a/.github/workflows/build_libtestoptimization.yml b/.github/workflows/build_libtestoptimization.yml index 86132a7..576de01 100644 --- a/.github/workflows/build_libtestoptimization.yml +++ b/.github/workflows/build_libtestoptimization.yml @@ -99,8 +99,11 @@ jobs: cp -rf ./native/* external/internal/civisibility/native/ - name: Build and run linux-arm64 run: | - docker buildx build --platform linux/arm64 --build-arg GOARCH=arm64 --build-arg FILE_NAME=linux-arm64-libtestoptimization -t libtestoptimization-builder:arm64 -f ./Dockerfile ../../.. --load - docker run --rm -v ./output:/libtestoptimization libtestoptimization-builder:arm64 + docker buildx build --platform linux/arm64 --build-arg GOARCH=arm64 --build-arg FILE_NAME=linux-arm64-libtestoptimization -t libtestoptimization-builder-static:arm64 -f ./Dockerfile-static ../../.. --load + docker run --rm -v ./output:/libtestoptimization libtestoptimization-builder-static:arm64 + + docker buildx build --platform linux/arm64 --build-arg GOARCH=arm64 --build-arg FILE_NAME=linux-arm64-libtestoptimization -t libtestoptimization-builder-dynamic:arm64 -f ./Dockerfile-dynamic ../../.. --load + docker run --rm -v ./output:/libtestoptimization libtestoptimization-builder-dynamic:arm64 working-directory: external/internal/civisibility/native - name: Upload artifact uses: actions/upload-artifact@v4 @@ -130,8 +133,11 @@ jobs: cp -rf ./native/* external/internal/civisibility/native/ - name: Build and run linux-amd64 run: | - docker build --platform linux/amd64 --build-arg GOARCH=amd64 --build-arg FILE_NAME=linux-x64-libtestoptimization -t libtestoptimization-builder:amd64 -f ./Dockerfile ../../.. - docker run --rm -v ./output:/libtestoptimization libtestoptimization-builder:amd64 + docker build --platform linux/amd64 --build-arg GOARCH=amd64 --build-arg FILE_NAME=linux-x64-libtestoptimization -t libtestoptimization-builder-static:amd64 -f ./Dockerfile-static ../../.. + docker run --rm -v ./output:/libtestoptimization libtestoptimization-builder-static:amd64 + + docker build --platform linux/amd64 --build-arg GOARCH=amd64 --build-arg FILE_NAME=linux-x64-libtestoptimization -t libtestoptimization-builder-dynamic:amd64 -f ./Dockerfile-dynamic ../../.. + docker run --rm -v ./output:/libtestoptimization libtestoptimization-builder-dynamic:amd64 working-directory: external/internal/civisibility/native - name: Build and run android-arm64 run: | diff --git a/build/Dockerfile b/build/Dockerfile-dynamic similarity index 74% rename from build/Dockerfile rename to build/Dockerfile-dynamic index 374fd4b..febc892 100644 --- a/build/Dockerfile +++ b/build/Dockerfile-dynamic @@ -23,8 +23,6 @@ COPY . . WORKDIR /app/internal/civisibility/native # Build the library -RUN go build -tags civisibility_native -buildmode=c-archive -ldflags="-s -w" -gcflags="all=-l" -o ./output/static/libtestoptimization.a *.go -RUN strip --strip-unneeded ./output/static/libtestoptimization.a RUN go build -tags civisibility_native -buildmode=c-shared -ldflags="-s -w" -gcflags="all=-l" -o ./output/dynamic/libtestoptimization.so *.go RUN strip --strip-unneeded ./output/dynamic/libtestoptimization.so @@ -41,19 +39,15 @@ RUN apk add --no-cache zip RUN mkdir -p /output # Copy the static library and header from the builder stage -COPY --from=builder /app/internal/civisibility/native/output/static/libtestoptimization.a /output/static/libtestoptimization.a -COPY --from=builder /app/internal/civisibility/native/output/static/libtestoptimization.h /output/static/libtestoptimization.h COPY --from=builder /app/internal/civisibility/native/output/dynamic/libtestoptimization.so /output/dynamic/libtestoptimization.so COPY --from=builder /app/internal/civisibility/native/output/dynamic/libtestoptimization.h /output/dynamic/libtestoptimization.h # Compress both files into zip archives -RUN cd /output/static && zip -j -9 ../${FILE_NAME}-static.zip *.* RUN cd /output/dynamic && zip -j -9 ../${FILE_NAME}-dynamic.zip *.* # Create a SHA256 checksum file for the archive -RUN sha256sum /output/${FILE_NAME}-static.zip > /output/${FILE_NAME}-static.zip.sha256sum RUN sha256sum /output/${FILE_NAME}-dynamic.zip > /output/${FILE_NAME}-dynamic.zip.sha256sum RUN rm -r /output/static /output/dynamic # Command to run when the container starts -ENTRYPOINT ["sh", "-c", "ls /output && cp /output/*.* /libtestoptimization && echo 'Static library copied.'"] +ENTRYPOINT ["sh", "-c", "ls /output && cp /output/*.* /libtestoptimization && echo 'Dynamic library copied.'"] diff --git a/build/Dockerfile-static b/build/Dockerfile-static new file mode 100644 index 0000000..e7f37fd --- /dev/null +++ b/build/Dockerfile-static @@ -0,0 +1,64 @@ +# Stage 1: Build the static library using musl (Alpine) +FROM golang:1-alpine AS builder + +# let's install the dependencies for the static build with musl and cgo +RUN apk add --no-cache gcc musl-dev build-base + +# Allow to change the architecture during the build if needed +ARG GOARCH=amd64 + +# let's configure the environment variables for the compilation +ENV CGO_ENABLED=1 \ + GOOS=linux \ + GOARCH=$GOARCH \ + # use gcc with the -static option to force the static linking + CC="gcc -static" \ + CGO_CFLAGS="-O2 -Os -s -DNDEBUG -fdata-sections -ffunction-sections" \ + # indicate ld to use the static approach + CGO_LDFLAGS="-static -s -Wl,--gc-sections" + +# Working directory +WORKDIR /app + +# Copy all code to the container +COPY . . + +# let's move to the directory where the code to compile is located +WORKDIR /app/internal/civisibility/native + +# let's compile the static library using cgo in c-archive mode and forcing static linking +RUN go build -tags civisibility_native -buildmode=c-archive \ + -ldflags="-extldflags '-static' -s -w" \ + -gcflags="all=-l" \ + -o ./output/static/libtestoptimization.a *.go + +# let's optimize the binary by removing unnecessary symbols +RUN strip --strip-unneeded ./output/static/libtestoptimization.a + +# Stage 2: let's package the library +FROM alpine:latest + +# let's rename the final file using build args +ARG FILE_NAME=libtestoptimization + +# let's install zip to compress the files +RUN apk add --no-cache zip + +# let's create the output directory +RUN mkdir -p /output/static + +# let's copy the library and the header from the builder +COPY --from=builder /app/internal/civisibility/native/output/static/libtestoptimization.a /output/static/libtestoptimization.a +COPY --from=builder /app/internal/civisibility/native/output/static/libtestoptimization.h /output/static/libtestoptimization.h + +# let's compress the library and the header in a zip +RUN cd /output/static && zip -j -9 ../${FILE_NAME}-static.zip *.* + +# let's create a file with the SHA256 checksum of the zip to verify the integrity +RUN sha256sum /output/${FILE_NAME}-static.zip > /output/${FILE_NAME}-static.zip.sha256sum + +# let's clean up the intermediate files +RUN rm -r /output/static + +# when the container starts, list the content and copy the files to a destination directory +ENTRYPOINT ["sh", "-c", "ls /output && cp /output/*.* /libtestoptimization && echo 'Static library copied.'"] From 0e9a4b23974bf433314425d62fdee8efb4a2799a Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Mon, 31 Mar 2025 13:14:07 +0200 Subject: [PATCH 3/6] fix --- build/Dockerfile-dynamic | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/Dockerfile-dynamic b/build/Dockerfile-dynamic index febc892..7091a30 100644 --- a/build/Dockerfile-dynamic +++ b/build/Dockerfile-dynamic @@ -47,7 +47,7 @@ RUN cd /output/dynamic && zip -j -9 ../${FILE_NAME}-dynamic.zip *.* # Create a SHA256 checksum file for the archive RUN sha256sum /output/${FILE_NAME}-dynamic.zip > /output/${FILE_NAME}-dynamic.zip.sha256sum -RUN rm -r /output/static /output/dynamic +RUN rm -r /output/dynamic # Command to run when the container starts ENTRYPOINT ["sh", "-c", "ls /output && cp /output/*.* /libtestoptimization && echo 'Dynamic library copied.'"] From d25616a0316e0059f7c2f57b4cd057dfe6e3c9a7 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Mon, 31 Mar 2025 13:56:37 +0200 Subject: [PATCH 4/6] changes --- build/Dockerfile-static | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/build/Dockerfile-static b/build/Dockerfile-static index e7f37fd..558842c 100644 --- a/build/Dockerfile-static +++ b/build/Dockerfile-static @@ -27,14 +27,10 @@ COPY . . WORKDIR /app/internal/civisibility/native # let's compile the static library using cgo in c-archive mode and forcing static linking -RUN go build -tags civisibility_native -buildmode=c-archive \ +RUN go build -tags "civisibility_native netgo" -buildmode=c-archive \ -ldflags="-extldflags '-static' -s -w" \ - -gcflags="all=-l" \ -o ./output/static/libtestoptimization.a *.go -# let's optimize the binary by removing unnecessary symbols -RUN strip --strip-unneeded ./output/static/libtestoptimization.a - # Stage 2: let's package the library FROM alpine:latest From 9ca4c24dacfe3203b430d29dd1790c175e082f06 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Mon, 31 Mar 2025 14:49:48 +0200 Subject: [PATCH 5/6] changes --- build/Dockerfile-static | 55 ++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/build/Dockerfile-static b/build/Dockerfile-static index 558842c..f6ac88f 100644 --- a/build/Dockerfile-static +++ b/build/Dockerfile-static @@ -1,60 +1,71 @@ # Stage 1: Build the static library using musl (Alpine) FROM golang:1-alpine AS builder -# let's install the dependencies for the static build with musl and cgo +# Install dependencies for the static build with musl and cgo RUN apk add --no-cache gcc musl-dev build-base -# Allow to change the architecture during the build if needed +# Allow changing the architecture during the build if needed ARG GOARCH=amd64 -# let's configure the environment variables for the compilation +# Configure environment variables for compilation ENV CGO_ENABLED=1 \ GOOS=linux \ GOARCH=$GOARCH \ - # use gcc with the -static option to force the static linking + # Use gcc with the -static option to force static linking CC="gcc -static" \ - CGO_CFLAGS="-O2 -Os -s -DNDEBUG -fdata-sections -ffunction-sections" \ - # indicate ld to use the static approach + CGO_CFLAGS="-O2 -s -DNDEBUG -fdata-sections -ffunction-sections" \ CGO_LDFLAGS="-static -s -Wl,--gc-sections" -# Working directory +# Set working directory WORKDIR /app # Copy all code to the container COPY . . -# let's move to the directory where the code to compile is located +# Change to the directory where the code to compile is located WORKDIR /app/internal/civisibility/native -# let's compile the static library using cgo in c-archive mode and forcing static linking -RUN go build -tags "civisibility_native netgo" -buildmode=c-archive \ - -ldflags="-extldflags '-static' -s -w" \ - -o ./output/static/libtestoptimization.a *.go - -# Stage 2: let's package the library +# Compile the static library using cgo in c-archive mode, forcing static linking, +# removing the -gcflags flag, adding the "netgo" tag, y para arm64 agregar -lresolv. +RUN if [ "$GOARCH" = "arm64" ]; then \ + echo "Compilando para arm64 con linkeo explícito de libresolv"; \ + go build -tags "civisibility_native netgo" -buildmode=c-archive \ + -ldflags="-extldflags '-static -lresolv' -s -w" \ + -o ./output/static/libtestoptimization.a *.go; \ + else \ + echo "Compilando para amd64"; \ + go build -tags "civisibility_native netgo" -buildmode=c-archive \ + -ldflags="-extldflags '-static' -s -w" \ + -o ./output/static/libtestoptimization.a *.go; \ + fi + +# Optimize the binary by removing unnecessary symbols +RUN strip --strip-unneeded ./output/static/libtestoptimization.a + +# Stage 2: Package the library FROM alpine:latest -# let's rename the final file using build args +# Rename the final file using build args ARG FILE_NAME=libtestoptimization -# let's install zip to compress the files +# Install zip to compress the files RUN apk add --no-cache zip -# let's create the output directory +# Create the output directory RUN mkdir -p /output/static -# let's copy the library and the header from the builder +# Copy the library and header from the builder stage COPY --from=builder /app/internal/civisibility/native/output/static/libtestoptimization.a /output/static/libtestoptimization.a COPY --from=builder /app/internal/civisibility/native/output/static/libtestoptimization.h /output/static/libtestoptimization.h -# let's compress the library and the header in a zip +# Compress the library and header in a zip archive RUN cd /output/static && zip -j -9 ../${FILE_NAME}-static.zip *.* -# let's create a file with the SHA256 checksum of the zip to verify the integrity +# Create a file with the SHA256 checksum of the zip to verify integrity RUN sha256sum /output/${FILE_NAME}-static.zip > /output/${FILE_NAME}-static.zip.sha256sum -# let's clean up the intermediate files +# Clean up intermediate files RUN rm -r /output/static -# when the container starts, list the content and copy the files to a destination directory +# When the container starts, list the contents and copy the files to a destination directory ENTRYPOINT ["sh", "-c", "ls /output && cp /output/*.* /libtestoptimization && echo 'Static library copied.'"] From 21485cbec910856cdaef641f3de3387d3db54ca4 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Mon, 31 Mar 2025 15:25:41 +0200 Subject: [PATCH 6/6] changes --- build/Dockerfile-static | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/build/Dockerfile-static b/build/Dockerfile-static index f6ac88f..36908d5 100644 --- a/build/Dockerfile-static +++ b/build/Dockerfile-static @@ -6,12 +6,14 @@ RUN apk add --no-cache gcc musl-dev build-base # Allow changing the architecture during the build if needed ARG GOARCH=amd64 +# Option to link libresolv explicitly for arm64, set to "true" or "false" at build time. +ARG LINK_LIBRESOLV=false # Configure environment variables for compilation ENV CGO_ENABLED=1 \ GOOS=linux \ GOARCH=$GOARCH \ - # Use gcc with the -static option to force static linking + # Default CC for amd64 (para arm64 se cambiará más abajo) CC="gcc -static" \ CGO_CFLAGS="-O2 -s -DNDEBUG -fdata-sections -ffunction-sections" \ CGO_LDFLAGS="-static -s -Wl,--gc-sections" @@ -25,12 +27,18 @@ COPY . . # Change to the directory where the code to compile is located WORKDIR /app/internal/civisibility/native -# Compile the static library using cgo in c-archive mode, forcing static linking, -# removing the -gcflags flag, adding the "netgo" tag, y para arm64 agregar -lresolv. +# Conditional compilation based on architecture and linking option RUN if [ "$GOARCH" = "arm64" ]; then \ - echo "Compilando para arm64 con linkeo explícito de libresolv"; \ + echo "Compilando para arm64 usando musl-gcc -static"; \ + export CC="musl-gcc -static"; \ + if [ "$LINK_LIBRESOLV" = "true" ]; then \ + EXTRA="-lresolv"; \ + else \ + EXTRA=""; \ + fi; \ + echo "Flags extra del linker: $EXTRA"; \ go build -tags "civisibility_native netgo" -buildmode=c-archive \ - -ldflags="-extldflags '-static -lresolv' -s -w" \ + -ldflags="-extldflags '-static $EXTRA' -s -w" \ -o ./output/static/libtestoptimization.a *.go; \ else \ echo "Compilando para amd64"; \ @@ -39,7 +47,7 @@ RUN if [ "$GOARCH" = "arm64" ]; then \ -o ./output/static/libtestoptimization.a *.go; \ fi -# Optimize the binary by removing unnecessary symbols +# Optimizar la librería eliminando símbolos innecesarios RUN strip --strip-unneeded ./output/static/libtestoptimization.a # Stage 2: Package the library @@ -61,11 +69,11 @@ COPY --from=builder /app/internal/civisibility/native/output/static/libtestoptim # Compress the library and header in a zip archive RUN cd /output/static && zip -j -9 ../${FILE_NAME}-static.zip *.* -# Create a file with the SHA256 checksum of the zip to verify integrity +# Create a SHA256 checksum file for the zip to verify integrity RUN sha256sum /output/${FILE_NAME}-static.zip > /output/${FILE_NAME}-static.zip.sha256sum # Clean up intermediate files RUN rm -r /output/static -# When the container starts, list the contents and copy the files to a destination directory +# When the container starts, list the content and copy the files to a destination directory ENTRYPOINT ["sh", "-c", "ls /output && cp /output/*.* /libtestoptimization && echo 'Static library copied.'"]