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

Build backend using static linking #36

Merged
merged 1 commit into from
Dec 24, 2024
Merged

Conversation

mtlynch
Copy link
Contributor

@mtlynch mtlynch commented Dec 22, 2024

By default, Go builds with dynamic linking. This has the benefit of allowing the compiled binary to re-use existing libraries on the system, but the drawback is that the binary is less portable, as it won't run on systems that lack the libraries it expects. For example, the precompiled Fusion binary does not run on my NixOS 24.11 system.

Switching to static linking means that fusion can basically run on any Linux system regardless of what libraries are installed. It also will allow us to change the base Docker image from debian to alpine, resulting in an image that's about 80% smaller:

REPOSITORY              TAG       IMAGE ID       CREATED        SIZE
fusion                  latest    5284da0a4f3c   45 years ago   177MB
fusion-alpine           latest    cc517d82d800   45 years ago   36.2MB

@0x2E
Copy link
Owner

0x2E commented Dec 23, 2024

Awesome. But it doesn't work on macOS because the SQLite driver is using CGO, which makes it hard to create a static build on macOS (Darwin).

I just opened #37 for a new CGO-Free drive. Could you test it on your machine? It works great here. I'll merge these two PRs if everything is fine.

Error:

❯   go build -v -ldflags '-extldflags "-static"' -o ./build/fusion ./cmd/server/* 
# command-line-arguments
/opt/homebrew/Cellar/go/1.23.4/libexec/pkg/tool/darwin_arm64/link: running cc failed: exit status 1
/usr/bin/cc -arch arm64 -Wl,-headerpad,1144 -o $WORK/b001/exe/a.out -Qunused-arguments /var/folders/z5/1_qfr0f55x97c63p412hprzw0000gn/T/go-link-2226424412/go.o /var/folders/z5/1_qfr0f55x97c63p412hprzw0000gn/T/go-link-2226424412/000000.o ... /var/folders/z5/1_qfr0f55x97c63p412hprzw0000gn/T/go-link-2226424412/000022.o -lresolv -O2 -g -framework CoreFoundation -framework Security -O2 -g -framework CoreFoundation -static
ld: library not found for -lcrt0.o

@0x2E 0x2E merged commit 472a41d into 0x2E:main Dec 24, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants