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

Add zig linker support #13515

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/markdown/Reference-tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ These are return values of the `get_linker_id` method in a compiler object.
| ld.mold | The fast MOLD linker |
| ld.solaris | Solaris and illumos |
| ld.wasm | emscripten's wasm-ld linker |
| ld.zigcc | The Zig linker (C/C++ frontend; GNU-like) |
| ld64 | Apple ld64 |
| ld64.lld | The LLVM linker, with the ld64 interface |
| link | MSVC linker |
Expand Down
18 changes: 18 additions & 0 deletions docs/markdown/snippets/zig_ld.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Zig 0.11 can be used as a C/C++ compiler frontend

Zig offers
[a C/C++ frontend](https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html) as a drop-in replacement for Clang. It worked fine with Meson up to Zig 0.10. Since 0.11, Zig's
dynamic linker reports itself as `zig ld`, which wasn't known to Meson. Meson now correctly handles
Zig's linker.

You can use Zig's frontend via a [machine file](Machine-files.md):

```ini
[binaries]
c = ['zig', 'cc']
cpp = ['zig', 'c++']
ar = ['zig', 'ar']
ranlib = ['zig', 'ranlib']
lib = ['zig', 'lib']
dlltool = ['zig', 'dlltool']
```
3 changes: 3 additions & 0 deletions mesonbuild/linkers/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ def guess_nix_linker(env: 'Environment', compiler: T.List[str], comp_class: T.Ty
linker = linkers.AIXDynamicLinker(
compiler, for_machine, comp_class.LINKER_PREFIX, override,
version=search_version(e))
elif o.startswith('zig ld'):
linker = linkers.ZigCCDynamicLinker(
compiler, for_machine, comp_class.LINKER_PREFIX, override, version=v)
else:
__failed_to_detect_linker(compiler, check_args, o, e)
return linker
11 changes: 10 additions & 1 deletion mesonbuild/linkers/linkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,9 @@ def build_rpath_args(self, env: 'Environment', build_dir: str, from_dir: str,
args.extend(self._apply_prefix('-rpath,' + paths))

# TODO: should this actually be "for solaris/sunos"?
if mesonlib.is_sunos():
# NOTE: Remove the zigcc check once zig support "-rpath-link"
# See https://github.com/ziglang/zig/issues/18713
if mesonlib.is_sunos() or self.id == 'ld.zigcc':
return (args, rpath_dirs_to_remove)

# Rpaths to use while linking must be absolute. These are not
Expand Down Expand Up @@ -936,6 +938,13 @@ def get_win_subsystem_args(self, value: str) -> T.List[str]:
raise mesonlib.MesonBugException(f'win_subsystem: {value} not handled in lld linker. This should not be possible.')


class ZigCCDynamicLinker(LLVMDynamicLinker):
id = 'ld.zigcc'

def get_thinlto_cache_args(self, path: str) -> T.List[str]:
return []


class WASMDynamicLinker(GnuLikeDynamicLinkerMixin, PosixDynamicLinkerMixin, DynamicLinker):

"""Emscripten's wasm-ld."""
Expand Down
Loading