Skip to content

Add shared library link mode for LLVM/MLIR#82

Merged
raviqqe merged 2 commits intomainfrom
support-shared-libs
Mar 15, 2026
Merged

Add shared library link mode for LLVM/MLIR#82
raviqqe merged 2 commits intomainfrom
support-shared-libs

Conversation

@edg-l
Copy link
Copy Markdown
Member

@edg-l edg-l commented Mar 14, 2026

Currently build.rs hardcodes --link-static and only scans for .a archives. This breaks on distros like Gentoo and Fedora where LLVM is only packaged as shared libraries.

Auto-detect link mode in detect_link_mode() by probing llvm-config with --link-static first, then falling back to --link-shared. Set MLIR_SYS_LINK_SHARED=1 (or true) to force shared mode.

In shared mode, links MLIR and MLIR-C directly instead of enumerating .a archives. parse_archive_name is split into parse_static_lib_name and parse_shared_lib_name, with the latter handling .so, versioned .so.N, and .dylib suffixes. --ignore-libllvm is only passed for static builds since it is not valid in shared mode.

This probably prevents and fixes #64

Auto-detect whether LLVM is installed with static or shared libraries
and link accordingly. This enables mlir-sys to work on systems where
LLVM is built with shared libraries only (e.g. Gentoo, Fedora).

Detection priority:
1. MLIR_SYS_LINK_SHARED env var (explicit override)
2. llvm-config --link-static --libnames (probe for static libs)
3. llvm-config --link-shared --libnames (fallback to shared)

When shared, links MLIR, MLIR-C, and LLVM as shared libraries
instead of scanning for individual static archives.
@edg-l edg-l marked this pull request as ready for review March 14, 2026 17:27
@edg-l edg-l requested a review from raviqqe March 14, 2026 17:29
Comment thread build.rs
Comment on lines +80 to 83
if flag.is_empty() {
continue;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When can this be empty?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

llvm-config --system-libs

On my gentoo system its empty, (my gentoo ebuild builds mlir and llvm with shared linkage maybe thats why? i'm not sure)

without the is_empty() check you'd get cargo:rustc-link-lib= with no library name, which would break the build

Comment thread build.rs Outdated
Comment thread build.rs Outdated
@edg-l edg-l force-pushed the support-shared-libs branch from 28afc1c to edd905d Compare March 15, 2026 00:06
@edg-l edg-l requested a review from raviqqe March 15, 2026 00:08
@raviqqe
Copy link
Copy Markdown
Member

raviqqe commented Mar 15, 2026

Thank you for the changes!

@raviqqe raviqqe merged commit 1ed106b into main Mar 15, 2026
7 checks passed
@raviqqe raviqqe deleted the support-shared-libs branch March 15, 2026 21:07
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.

Build fails on Linux with Custom MLIR 19 build

2 participants