Static linking with libxml2

Alpine has added xz as a dependency of libxml2.

I added xz-dev to the dependencies (in fact it is installed automatically), but there is still a linking issue: https://github.com/the-benchmarker/web-frameworks/issues/2018

Minimal example to reproduce on Alpine Edge (you can use the jrei/crystal-alpine:edge Docker image):

echo 'require "xml"' > a.cr
crystal build --static a.cr

I think it may be an issue with pkg-config (on the Crystal side or Alpine), because I don’t see any lzma in the cc linking command.

pkg-config libxml-2.0 --libs --static returns -lxml2 -L/lib -lz -llzma -lm

Manually passing --link-flags '-lzma -lxml2' do the trick.

2 Likes

I guess we can add that to the @[Link(...)] thing on our side then, for alpine?

There is no way to use pkg-config for this? How do we know we are on Alpine, at Edge, using the official package?

Real future-proof fix to use: crystal build --static --link-flags "$(pkg-config libxml-2.0 --libs --static)" app.cr.
The solution in Crystal would be to use pkg-config too, because dependencies can change.

I don’t suppose this is something that is fixable? Something similar to https://github.com/crystal-lang/crystal/pull/9426?

\cc @jhass

I went ahead and did it. Seemed to have fixed the problem.

3 Likes