mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.63k stars 1.63k forks source link

"Unable to detect linker for compiler" error in an osx cross-compile environment #12238

Closed sezero closed 1 year ago

sezero commented 1 year ago

Ran into it while trying to build dav1d project for macos/x64, using meson-1.0.2 with python 3.7 on i686-linux. The following copied from meson-log.txt:

-----
Detecting compiler via: /tmp/cc.sh --version
compiler returned <subprocess.Popen object at 0xb70d6e6c>
compiler stdout:
clang version 3.4.2 (tags/RELEASE_34/dot2-final)
Target: x86_64-apple-darwin9
Thread model: posix

compiler stderr:

Running command: /tmp/cc.sh -E -dM -
-----
Detecting linker via: /tmp/cc.sh -Wl,--version
linker returned <subprocess.Popen object at 0xb68dafec>
linker stdout:

linker stderr:
ld: unknown option: --version
clang: error: linker command failed with exit code 1 (use -v to see invocation)

-----
Detecting Apple linker via: /tmp/cc.sh -Wl,-v
linker stdout:

linker stderr:
241.9
configured to support archs: armv4t armv5 armv6 armv7 armv7f armv7k armv7s armv6m armv7m armv7em armv8 arm64 arm64v8 i386 x86_64 x86_64h
Library search paths:
    /opt/MacOSX10.8.sdk/usr/lib
    /opt/MacOSX10.8.sdk/usr/local/lib
Framework search paths:
    /opt/MacOSX10.8.sdk/System/Library/Frameworks/
Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

../meson.build:25:0: ERROR: Unable to detect linker for compiler `/tmp/cc.sh -Wl,--version`
stdout: 
stderr: ld: unknown option: --version
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The cross-build file is like this:

[binaries]
c = '/tmp/cc.sh'
cpp = '/tmp/cc.sh'
ld = 'x86_64-apple-darwin9-ld'
ar = 'x86_64-apple-darwin9-ar'
ranlib = 'x86_64-apple-darwin9-ranlib'
strip = 'x86_64-apple-darwin9-strip'

[host_machine]
system = 'darwin'
cpu_family = 'x86_64'
cpu = 'x86_64'
endian = 'little'

The x86_64-apple-darwin9-XXX tools are built from cctools-port project. And, cc.sh is simply a wrapper around clang:

#!/bin/sh

exec clang -target x86_64-apple-darwin9 -arch x86_64 -mlinker-version=341.9 -isysroot /opt/MacOSX10.8.sdk -B /opt/cctools-port/bin -mmacosx-version-min=10.6 "$@"

Is there a way around this?

Thanks.

sezero commented 1 year ago

Issue persists if I use meson from git master instead of 1.0.2 release.

sezero commented 1 year ago

I guess the linker entries in the cross file was wrong. If change them to like:

c_ld = 'x86_64-apple-darwin9-ld'
cpp_ld = 'x86_64-apple-darwin9-ld'

... then I get the following error itself:

meson.build:25:0: ERROR: Unsupported linker, only bfd, gold, and lld are supported, not x86_64-apple-darwin9-ld.

If I symlink x86_64-apple-darwin9-ld as lld and change those entries in the cross file to lld, then the initially reported issue repeats itself.

tristan957 commented 1 year ago

Can you print the --version output of the linker? You might have to debug this and submit a PR. Not too many contributors regularly using Mac. The good part is that the PR will probably be pretty small

sezero commented 1 year ago

Apple's ld doesn't do --version, it responds to -v :

This from an older version:

$  x86_64-apple-darwin9-ld --version
ld: unknown option: --version

$ x86_64-apple-darwin9-ld -v
241.9
configured to support archs: armv4t armv5 armv6 armv7 armv7f armv7k armv7s armv6m armv7m armv7em armv8 arm64 arm64v8 i386 x86_64 x86_64h
LTO support using: LLVM version 3.4.2

This from a much newer version:

$ x86_64-apple-darwin9-ld --version
ld: warning: directory not found for option '-F/System/Library/Frameworks/'
ld: unknown option: --version

$ x86_64-apple-darwin9-ld -v
@(#)PROGRAM:ld  PROJECT:ld64-609
BUILD 10:34:07 Jun 26 2021
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
LTO support using: LLVM version 11.1.0 (static support for 27, runtime is 27)
TAPI support using: TAPI version 11.0.0 (1100.0.11)
sezero commented 1 year ago

.. And the following is from natively running on an old Mac (so that no one can blame me for my cross-environment :))

Snow-Leopard:~ macuser$ ld --version
ld: unknown option: --version
Snow-Leopard:~ macuser$ ld -v
@(#)PROGRAM:ld  PROJECT:ld64-97.17
llvm version 2.9svn, from Apple Clang 1.7 (build 77)
Snow-Leopard:~ macuser$ gcc -Wl,-v
collect2 version 4.2.1 (Apple Inc. build 5666) (dot 3) (i686 Darwin)
/usr/libexec/gcc/i686-apple-darwin10/4.2.1/ld -dynamic -arch i386 -macosx_version_min 10.6.8 -weak_reference_mismatches non-weak -o a.out -lcrt1.10.6.o -L/usr/lib/i686-apple-darwin10/4.2.1 -L/usr/lib/gcc/i686-apple-darwin10/4.2.1 -L/usr/
lib/gcc/i686-apple-darwin10/4.2.1 -L/usr/lib/gcc/i686-apple-darwin10/4.2.1/../../../i686-apple-darwin10/4.2.1 -L/usr/lib/gcc/i686-apple-darwin10/4.2.1/../../.. -v -lSystem -lgcc -lSystem
@(#)PROGRAM:ld  PROJECT:ld64-97.17
Library search paths:
        /usr/lib/i686-apple-darwin10/4.2.1
        /usr/lib/gcc/i686-apple-darwin10/4.2.1
        /usr/lib/gcc/i686-apple-darwin10/4.2.1
        /usr/lib/i686-apple-darwin10/4.2.1
        /usr/lib
        /usr/lib
        /usr/local/lib
Framework search paths:
        /Library/Frameworks/
        /System/Library/Frameworks/
Undefined symbols:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
tristan957 commented 1 year ago

Yeah your linker is just not supported. You need to check out the code that does linker detection and hook into that

sezero commented 1 year ago

Yeah your linker is just not supported.

Apple linker not supported? Huh..

You need to check out the code that does linker detection and hook into that

Directions please? (I'm no meson guy really..)

tristan957 commented 1 year ago

Apple linker not supported? Huh..

Your apple linker is from a 2009 operating system which was last updated in 2012. The Apple linker obviously has changed output since 2012. Not sure what the point of your comment is :).

https://github.com/mesonbuild/meson/blob/b51bce070eead2b65e56f087acf23829a6304ae2/mesonbuild/linkers/detect.py#L194

There is the code you need to modify.

sezero commented 1 year ago

Apple linker not supported? Huh..

Your apple linker is from a 2009 operating system which was last updated in 2012. The Apple linker obviously has changed output since 2012. Not sure what the point of your comment is :).

You can also see that I get the same error from version 609 of the linker (as I shown you above), which was released on March 2021: https://github.com/apple-opensource/ld64/tags

The point of my comment is that old or new Apple ld is failing for me, and if I'm at fault somehow I'd like to be shown how.

tristan957 commented 1 year ago

The point of my comment is that old or new Apple ld is failing for me, and if I'm at fault somehow I'd like to be shown how.

The bug is definitely in the Meson code. It just needs some sort of addendum.

You can see that the error is on https://github.com/mesonbuild/meson/blob/b51bce070eead2b65e56f087acf23829a6304ae2/mesonbuild/linkers/detect.py#L197

Your linker doesn't provide that line in its stderr.

sezero commented 1 year ago

You can see that the error is on

https://github.com/mesonbuild/meson/blob/b51bce070eead2b65e56f087acf23829a6304ae2/mesonbuild/linkers/detect.py#L197

Your linker doesn't provide that line in its stderr.

OK that helped, and figured it out. Thanks!

Closing the ticket.

tristan957 commented 1 year ago

Why close the ticket if it is a valid ticket?

sezero commented 1 year ago

Because it was the fault of the cctools-port, which didn't feed PROJECT:ld64 to stderr. After patching, things worked.

tristan957 commented 1 year ago

Thanks for the conclusion. Happy it worked for you.

ilg-ul commented 1 year ago

I just updated my system to macOS 14.1 with the latest CLT/Xcode 15 and meson (1.0.2) can no longer be used with it.

meson.build:16:0: ERROR: Unable to detect linker for compiler `gcc -Wl,--version`

The linker is:

ilg@wksi ~ % /usr/bin/ld -v       
@(#)PROGRAM:ld  PROJECT:dyld-1015.7
BUILD 18:48:43 Aug 22 2023
configured to support archs: armv6 armv7 armv7s arm64 arm64e arm64_32 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em
will use ld-classic for: armv6 armv7 armv7s arm64_32 i386 armv6m armv7k armv7m armv7em
LTO support using: LLVM version 15.0.0 (static support for 29, runtime is 29)
TAPI support using: Apple TAPI version 15.0.0 (tapi-1500.0.12.3)
Library search paths:
Framework search paths:
ilg@wksi ~ % uname -a
Darwin wksi.local 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct  9 21:27:27 PDT 2023; root:xnu-10002.41.9~6/RELEASE_X86_64 x86_64
ilg@wksi ~ % /usr/bin/ld --version
ld: unknown option: --version

Was this problem addressed in the latest release?

eli-schwartz commented 1 year ago

@ilg-ul are you using cctools? Is this:

error in an osx cross-compile environment

per the title of this PR?

eli-schwartz commented 1 year ago

I am relatively sure your issue is instead #12419

ilg-ul commented 1 year ago

You're right! Sorry for the noise.