petervas / bsdifflib

Portable binary diff/patch library based on the binary diff/patch utilities bsdiff/bspatch
Other
18 stars 6 forks source link

Do you consider support build a static binary? e.g. use linux-musl alpine. #2

Closed zw963 closed 8 months ago

zw963 commented 9 months ago

Hi, i try build bsdiff binary use LDFLAGS=-static in the alpine 3.19, but no luck.

#11 [link_target 7/7] RUN make LDFLAGS=-static
#11 0.023 gcc -O2 -Wall -c -o bsdiff.o bsdiff.c
#11 0.043 gcc -O2 -Wall -c -o bsdifflib.o bsdifflib.c
#11 0.221 gcc bsdiff.o bsdifflib.o -static -o bsdiff
#11 0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.o: in function `bsdiff':
#11 0.237 bsdifflib.c:(.text+0x89a): undefined reference to `BZ2_bzWriteOpen'
#11 0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xce1): undefined reference to `BZ2_bzWrite'
#11 0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xd30): undefined reference to `BZ2_bzWrite'
#11 0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xd8e): undefined reference to `BZ2_bzWrite'
#11 0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xdf0): undefined reference to `BZ2_bzWriteClose'
#11 0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xe3c): undefined reference to `BZ2_bzWriteOpen'
#11 0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xe68): undefined reference to `BZ2_bzWrite'
#11 0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xe8d): undefined reference to `BZ2_bzWriteClose'
#11 0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xed8): undefined reference to `BZ2_bzWriteOpen'
#11 0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xf04): undefined reference to `BZ2_bzWrite'
#11 0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xf29): undefined reference to `BZ2_bzWriteClose'
#11 0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0x1272): undefined reference to `BZ2_bzWriteClose'
#11 0.243 collect2: error: ld returned 1 exit status
#11 0.243 make: *** [Makefile:12: bsdiff] Error 1
#11 ERROR: process "/bin/sh -c make LDFLAGS=-static" did not complete successfully: exit code: 2
------
 > [link_target 7/7] RUN make LDFLAGS=-static:
0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xdf0): undefined reference to `BZ2_bzWriteClose'
0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xe3c): undefined reference to `BZ2_bzWriteOpen'
0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xe68): undefined reference to `BZ2_bzWrite'
0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xe8d): undefined reference to `BZ2_bzWriteClose'
0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xed8): undefined reference to `BZ2_bzWriteOpen'
0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xf04): undefined reference to `BZ2_bzWrite'
0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0xf29): undefined reference to `BZ2_bzWriteClose'
0.237 /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: bsdifflib.c:(.text+0x1272): undefined reference to `BZ2_bzWriteClose'
0.243 collect2: error: ld returned 1 exit status
0.243 make: *** [Makefile:12: bsdiff] Error 1

Do you consider support this? or may i do something wrong?

Thanks!

petervas commented 9 months ago

When you override the LDFLAGS when calling make you need to preserve the original LDFLAGS from the makefile. Try make LDFLAGS="-lbz2 -static"

zw963 commented 9 months ago

When you override the LDFLAGS when calling make you need to preserve the original LDFLAGS from the makefile. Try make LDFLAGS="-lbz2 -static"

It works! thank you.

 ╰─ $ ldd bsdiff bspatch
bsdiff:
        not a dynamic executable
bspatch:
        not a dynamic executable

I you can provide a precompiled static binary in the released page with a tag (as most of command tools create by golang, rust packages does), would be very grateful, because compile dynamic, often result in running bspatch failed on remote VPS (because those machine system is older).

Following is the dockerfile for build above static bianry:

# -*- mode: dockerfile-ts; -*-

# 确保 build 和 linking 是同一个 alpine 版本
ARG alpine_version=3.19
# mirrors.ustc.edu.cn mirrors.tuna.tsinghua.edu.cn
ARG alpine_mirror=mirrors.ustc.edu.cn

# ============================== 使用目标系统架构 link binary ==============================

FROM alpine:$alpine_version as link_target

ARG alpine_mirror
RUN sed -i "s/dl-cdn.alpinelinux.org/$alpine_mirror/g" /etc/apk/repositories

# Add trusted CAs for communicating with external services and required build tooling
# 额外添加了 -dev 包,也允许编译动态版本。
# g++ 依赖 libc-dev, libc-dev 依赖 musl-dev, gcc
RUN set -eux; \
    apk add \
    --update \
    gc-dev pcre2-dev \
    libevent-dev libevent-static \
    sqlite-static openssl-libs-static \
    yaml-static \
    zlib-dev zlib-static \
    bzip2-dev bzip2-static \
    g++ make automake libtool autoconf git \
    ;

RUN addgroup -g 1000 docker && \
    adduser -u 1000 -G docker -h /home/docker -s /bin/sh -D docker

USER docker:docker

# WORKDIR 应该放在 USER 后面,因为如果 /app 不存在,需要创建这个文件夹
WORKDIR /app

COPY --chown=docker:docker . .

RUN make LDFLAGS="-lbz2 -static"

# Build a minimal docker image
FROM scratch AS mini
WORKDIR /
ENV PATH=$PATH:/

# These provide certificate chain validation where communicating with external services over TLS
ENV SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt

# This is your application
COPY --from=link_target /app /
# COPY --from=build_cross_platform /app/openapi.yml /openapi.yml

USER docker:docker

# Spider-gazelle has a built in helper for health checks (change this as desired for your applications)
# HEALTHCHECK CMD ["/app", "-c", "http://127.0.0.1:3000/"]

# Run the app binding on port 3000
# EXPOSE 3000
ENTRYPOINT ["/app/bin/app"]
# CMD ["/app", "-b", "0.0.0.0", "-p", "3000"]

Then run with:

$: docker buildx build --progress=plain --platform linux/amd64 --target mini -t build_cross_link_binary --output type=local,dest=linux/amd64 -f $dockerfile .

It can build bsdiff for other platform, e.g. linux/arm64/v8

Thanks!