Repository: mesonbuild/meson

Issue:
Meson static linking with "thin libraries" is not supported on illumos
**Describe the bug**
The feature introduced in https://mesonbuild.com/Release-notes-for-0-60-0.html#more-efficient-static-linking-of-uninstalled-libraries does not check if so-called "thin libraries" are supported on the system.
Therefore static linking may be broken at least in some configurations.

**To Reproduce**
Building gst-plugins-bad1 fails with

```
[657/820] Linking target tests/check/elements_h263parse
FAILED: tests/check/elements_h263parse
/usr/gcc/7/bin/gcc  -o tests/check/elements_h263parse
tests/check/elements_h263parse.p/elements_h263parse.c.o
-L/usr/lib/libjpeg8-turbo/lib/amd64 -I/usr/X11/include/mesa
-I/usr/X11/include -I/usr/include/libjpeg8-turbo -Wl,-z -Wl,ignore -z
defs -m64 -R/usr/lib/libjpeg8-turbo/lib/amd64 -m64 -O2
-Wl,-L/usr/lib/libjpeg8-turbo/lib/amd64
-Wl,-R/usr/lib/libjpeg8-turbo/lib/amd64 -m64
'-Wl,-rpath,$ORIGIN/../../gst-libs/gst/basecamerabinsrc:$ORIGIN/../../gst-libs/gst/interfaces:$ORIGIN/../../gst-libs/gst/uridownloader:$ORIGIN/../../gst-libs/gst/codecparsers'
-Wl,--start-group
gst-libs/gst/basecamerabinsrc/libgstbasecamerabinsrc-1.0.so.0.1805.0
gst-libs/gst/interfaces/libgstphotography-1.0.so.0.1805.0
gst-libs/gst/uridownloader/libgsturidownloader-1.0.so.0.1805.0
tests/check/libparser.a
gst-libs/gst/codecparsers/libgstcodecparsers-1.0.so.0.1805.0 -lm
/usr/lib/amd64/libgstreamer-1.0.so /usr/lib/amd64/libgobject-2.0.so
/usr/lib/amd64/libglib-2.0.so /usr/lib/amd64/libgstapp-1.0.so
/usr/lib/amd64/libgstbase-1.0.so /usr/lib/amd64/libgstpbutils-1.0.so
/usr/lib/amd64/libgstaudio-1.0.so /usr/lib/amd64/libgstvideo-1.0.so
/usr/lib/amd64/libgsttag-1.0.so /usr/lib/amd64/libgstcontroller-1.0.so
/usr/lib/amd64/libgstrtp-1.0.so /usr/lib/amd64/libgstcheck-1.0.so -lrt
/usr/lib/amd64/libgio-2.0.so -Wl,--end-group
ld: fatal: file tests/check/libparser.a: unknown file type
ld: fatal: file processing errors. No output written to
tests/check/elements_h263parse
collect2: error: ld returned 1 exit status
```

**Expected behavior**
No breakage :)

As a workaround I opened a PR against our userland component:

https://github.com/OpenIndiana/oi-userland/pull/7666

The patch disables "thin libraries" as for OSX:

https://github.com/OpenIndiana/oi-userland/blob/0d4dfb90167020df15aabd4e767b1f84b00dcdb2/components/developer/meson/patches/014-thin-libraries-breakage.patch

Thank you

**system parameters**
* Native build
* OpenIndiana x86_64 (illumos)
* Python version: 3.9.10
* meson version: 0.61.1
* ninja version: 1.10.2



Discussion:
@eli-schwartz, I thought we reverted this?
Probably #10823 is a duplicate of this issue (or anyway related)?
I've hit this as well on Solaris when upgrading our bundled meson from 0.59 to 0.63.

If `$PATH` has `/usr/gnu/bin` before `/usr/bin`, and `$AR` is not set, then meson detects `/usr/gnu/bin/ar` can create thin static archives, and enables them, but the Solaris linker doesn't support linking with them.

If `$PATH` has `/usr/bin` before` /usr/gnu/bin` or `$AR` is set to `/usr/bin/ar`, then meson detects that `/usr/bin/ar` cannot create thin archives, and doesn't try to use them, and everything is good.   This leaves running meson with `AR=/usr/bin/ar` as a simple workaround.

I verified this by running `ninja -v` to do the build to see what command is used to build the archives - with `/usr/gnu/bin/ar` it runs `ar csrDT ...`, but with `/usr/bin/ar` it runs `ar csr ...`

I suspect the simplest fix would be to add a check for `is_sunos()` alongside the current one for Darwin at:
https://github.com/mesonbuild/meson/blob/6860e42c065a61cee2f31615fab99af1e20c8e50/mesonbuild/linkers/linkers.py#L210

A more complete fix would add a test that the chosen linker can link with thin archives to the check that ar can generate them.
(Just noticed my suggested simple fix is the same as what @alarcher came up with months ago in the OpenIndiana patch he linked to, so should be credited there.)

Resolving patch:
diff --git a/mesonbuild/linkers/linkers.py b/mesonbuild/linkers/linkers.py
--- a/mesonbuild/linkers/linkers.py
+++ b/mesonbuild/linkers/linkers.py
@@ -208,8 +208,12 @@ def can_linker_accept_rsp(self) -> bool:
         return self.can_rsp
 
     def get_std_link_args(self, env: 'Environment', is_thin: bool) -> T.List[str]:
-        # FIXME: osx ld rejects this: "file built for unknown-unsupported file format"
-        if is_thin and not env.machines[self.for_machine].is_darwin():
+        # Thin archives are a GNU extension not supported by the system linkers
+        # on Mac OS X, Solaris, or illumos, so don't build them on those OSes.
+        # OS X ld rejects with: "file built for unknown-unsupported file format"
+        # illumos/Solaris ld rejects with: "unknown file type"
+        if is_thin and not env.machines[self.for_machine].is_darwin() \
+          and not env.machines[self.for_machine].is_sunos():
             return self.std_thin_args
         else:
             return self.std_args


Test patch: