From 67d3a0b1a2c2f046376f3ede12f631c1ef734893 Mon Sep 17 00:00:00 2001 From: Andrei Alexeyev Date: Wed, 27 Sep 2023 11:43:23 +0200 Subject: [PATCH 1/2] linkers: basic support for the 'zig cc' linker --- docs/markdown/Reference-tables.md | 1 + docs/markdown/snippets/zig_ld.md | 18 ++++++++++++++++++ mesonbuild/linkers/detect.py | 3 +++ mesonbuild/linkers/linkers.py | 7 +++++++ 4 files changed, 29 insertions(+) create mode 100644 docs/markdown/snippets/zig_ld.md diff --git a/docs/markdown/Reference-tables.md b/docs/markdown/Reference-tables.md index 67eccb4a2465..426a9c9773e9 100644 --- a/docs/markdown/Reference-tables.md +++ b/docs/markdown/Reference-tables.md @@ -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 | diff --git a/docs/markdown/snippets/zig_ld.md b/docs/markdown/snippets/zig_ld.md new file mode 100644 index 000000000000..8835c38ca4f7 --- /dev/null +++ b/docs/markdown/snippets/zig_ld.md @@ -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'] +``` diff --git a/mesonbuild/linkers/detect.py b/mesonbuild/linkers/detect.py index cd3c7b2efd0f..3238c557e803 100644 --- a/mesonbuild/linkers/detect.py +++ b/mesonbuild/linkers/detect.py @@ -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 diff --git a/mesonbuild/linkers/linkers.py b/mesonbuild/linkers/linkers.py index c3750ccc372f..5b3fa9bf1d56 100644 --- a/mesonbuild/linkers/linkers.py +++ b/mesonbuild/linkers/linkers.py @@ -936,6 +936,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.""" From 97f06cae90ffa4c8c185bb3760b645c5a03ea8b9 Mon Sep 17 00:00:00 2001 From: Tristan Ross Date: Tue, 30 Jul 2024 18:50:19 -0700 Subject: [PATCH 2/2] linkers: Disable -rpath-link with ld.zigcc Zig issue https://github.com/ziglang/zig/issues/18713 --- mesonbuild/linkers/linkers.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mesonbuild/linkers/linkers.py b/mesonbuild/linkers/linkers.py index 5b3fa9bf1d56..a6b54a35fc23 100644 --- a/mesonbuild/linkers/linkers.py +++ b/mesonbuild/linkers/linkers.py @@ -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