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

Fix static linking error for LLVM-17 #167

Closed
wants to merge 2 commits into from
Closed

Fix static linking error for LLVM-17 #167

wants to merge 2 commits into from

Conversation

callum-hopkins-dev
Copy link

Foreword: this is my first public pr in quite a while, apologies if something is not quite right.

When specifying the static build feature, this crate attempts to link to the library -lLLVM-17. However, LLVM does not provide a single static library since it would be too large (500MB!). Therefore, to successfully link against static LLVM, one must append all of the individual libraries, of which there are many.

This PR uses the llvm-config tool's --link-static flag, which causes it to output each individual static archive. Additionally, the LLVM library uses symbols from zstd, which this PR also appends to the linker.

@callum-hopkins-dev callum-hopkins-dev closed this by deleting the head repository Feb 12, 2024
@Dirreke
Copy link

Dirreke commented Mar 3, 2024

Hey! I met the similar Issue. And I want to know if you resolve it and how? Thanks!
@callum-hopkins-dev

@callum-hopkins-dev
Copy link
Author

Hey! I met the similar Issue. And I want to know if you resolve it and how? Thanks! @callum-hopkins-dev

It really depends on what your build environment is. This patch was one of a series of patches through the rust-bindgen project to fix static compilation on alpine Linux specifically.

You could try the environment variable RUSTFLAGS="-C target-feature=-crt-static" which also solves the issue, hence why I scrapped this fix.

@Dirreke
Copy link

Dirreke commented Mar 3, 2024

Actually I still have this problem.

note: /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lLLVM-17: No such file or directory

I couldn't compile rust-bindgen with the only feature static. Is there anything else I should notice?

@callum-hopkins-dev
Copy link
Author

Actually I still have this problem.

note: /usr/lib/gcc/x86_64-alpine-linux-musl/13.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lLLVM-17: No such file or directory

I couldn't compile rust-bindgen with the only feature static. Is there anything else I should notice?

Ah, yes that's caused by clang-sys not linking with the correct libraries under the static feature.

Is there any particular reason you need to statically link the compiler like this? Remember the method of linking is only used at compile time and doesn't determine whether the bindgen itself is linked statically / dynamically.

@Dirreke
Copy link

Dirreke commented Mar 3, 2024

I'm not familiar with apline. I'm trying to compile my program in the docker rust: alpine. It seems that I cann't load dynamical library. Maybe I should try to cross comopile it on a gnu env.
Thanks.

@callum-hopkins-dev
Copy link
Author

Try compiling with export RUSTFLAGS="-C target-feature=-crt-static".

That disables static musl linking which should fix the issue.

@Dirreke
Copy link

Dirreke commented Mar 3, 2024

I have tried that on alpine. And I have no idea why It doesn't work. I will try again tomorrow. Thanks very much

@Dirreke
Copy link

Dirreke commented Mar 4, 2024

Should I use feature runtime or static?

@Dirreke
Copy link

Dirreke commented Mar 4, 2024

@callum-hopkins-dev
Copy link
Author

callum-hopkins-dev commented Mar 4, 2024

I'm not entirely sure how you're cross-compiling, but if you have the ability to use a container engine such as docker, I would take full advantage of it's QEMU emulation.

I do a lot of "cross-compiling" for arm64 and use this particular Containerfile:

FROM alpine:edge AS build

ENV RUSTFLAGS="-C target-feature=-crt-static"
ENV PATH=${PATH}:/root/.cargo/bin

WORKDIR /src

# install any dependencies here
RUN apk add alpine-sdk curl llvm-dev clang-dev

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile=minimal

COPY . .

RUN cargo build --release

FROM alpine:edge

# install any dependencies here, libgcc is needed since we disabled crt-static
RUN apk add libgcc

COPY --from=build /src/target/release/BINARY /usr/local/bin/BINARY

ENTRYPOINT [ "/usr/local/bin/BINARY" ]

In this scenario, you should enable the runtime bindgen feature and build the docker / podman image with the following command: podman build --platform linux/arm64 . Note the --platform flag, which allows us to compile the entire container under a different architecture without messing around with cross-compilation toolchains.

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