Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distribute static binaries #511

Open
xbt573 opened this issue Jun 7, 2024 · 2 comments
Open

Distribute static binaries #511

xbt573 opened this issue Jun 7, 2024 · 2 comments

Comments

@xbt573
Copy link

xbt573 commented Jun 7, 2024

gonic version: v0.16.4
docker tag: latest
Actually this is irrelevant to issue, but nevermind.

  1. Building go-sqlite3 statically is very easy, just pass -ldflags '-linkmode external -extldflags "-static"' to go build

  2. taglib requires static rebuild and some code changes, but nothing very hard
    Firstly we need to build taglib as static library: pass -DBUILD_SHARED_LIBS=OFF to cmake (this is confusing, but disabling shared libraries enables static :/)
    Static zlib is also needed, which is not always bundled with system package (on alpine use zlib-static)
    Needed code change is at https://github.com/sentriz/audiotags/blob/master/audiotags.go#L25: add zlib to pkg-config

--- a/audiotags.go
+++ b/audiotags.go
@@ -22,7 +22,7 @@
 package audiotags
 
 /*
-#cgo pkg-config: taglib
+#cgo pkg-config: taglib zlib
 #cgo LDFLAGS: -lstdc++
 #include "audiotags.h"
 #include <stdlib.h>

Final build command is:

go build -ldflags '-linkmode external -extldflags "-static"' -o gonic cmd/gonic/gonic.go

ldd shows:

/lib/ld-musl-x86_64.so.1: gonic: Not a valid dynamic program

(whole process was made in alpine docker image)

I didn't send PR because this is mostly untested (at least i could build it statically)

@sentriz
Copy link
Owner

sentriz commented Jul 6, 2024

thanks! ill try this

@sentriz
Copy link
Owner

sentriz commented Sep 18, 2024

works, thanks!

for reference:

FROM alpine:edge AS builder

WORKDIR /tmp/taglib
RUN apk add --no-cache wget
RUN wget "https://github.com/taglib/taglib/releases/download/v2.0.2/taglib-2.0.2.tar.gz"
RUN tar -xzf taglib-2.0.2.tar.gz
WORKDIR /tmp/taglib/taglib-2.0.2
RUN apk add --no-cache build-base cmake zlib-static utfcpp
RUN cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF .
RUN make
RUN make install

WORKDIR /tmp/gonic
RUN apk add --no-cache go
RUN wget "https://github.com/sentriz/gonic/archive/master.tar.gz"
RUN tar -xzf master.tar.gz
WORKDIR /tmp/gonic/gonic-master
RUN apk add --no-cache pkgconf
RUN go mod download
RUN go build -ldflags '-linkmode external -extldflags "-static"' -o /bin/gonic ./cmd/gonic/...
RUN file /bin/gonic
RUN /bin/gonic -h

FROM scratch
WORKDIR /
COPY --from=builder /bin/gonic .

@sentriz sentriz changed the title Possibility for taglib static builds Distribute static builds Sep 18, 2024
@sentriz sentriz changed the title Distribute static builds Distribute static binaries Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants