Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Darwin's ld64 does not have longopt '--version', just '-v', so bootstrap.sh fails on Mac OS X. #1672

Closed
geoff-codes opened this Issue · 3 comments

2 participants

@geoff-codes

Um, yeah. That's it really. I guess I'll illustrate to take up a couple more lines.

☞   ld --version
ld: unknown option: --version
☞   ld -v
@(#)PROGRAM:ld  PROJECT:ld64-234.1
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 armv6m armv7m armv7em
LTO support using: LLVM version 3.4svn
@geoff-codes

Actually, I can do better that that for ya' I think. What your really after is the path of the same linker
as being used by the C compiler in ${CC}, right?

(Because they're not necessarily the same, and you want to avoid a potential linker for a different ABI.)

Here's the super-rediculously-portable way to find it, test that it works, and return the full path:

#!/usr/bin/env sh
# 'main;' is the shortest C program possible. It is compiled, linked,
# and written to /dev/null. So if the linker can't link, this returns 1.

for link in collect2 ld; do
  echo 'main;' | ${CC} -v -x c - -o /dev/null -\#\#\# 2>&1 | grep -q $link &&
  echo 'main;' | ${CC} -v -x c - -o /dev/null -\#\#\# 2>&1 | grep    $link |
  sed -e "s|\(.*$link\).*|\1|" -e 's/ //g' -e 's|"||' && break
done

# That should work on just about anything, and return an absolute path, except with ICC.
# If we want to try to get an absolute path too,  we have to:

which="$(which which 2>/dev/null 1>&1)" || which=echo # If 'which' is missing.

$which "$(for link in collect2 ld; do
  echo 'main;' | ${CC} -v -x c - -o /dev/null -\#\#\# 2>&1 | grep -q $link &&
  echo 'main;' | ${CC} -v -x c - -o /dev/null -\#\#\# 2>&1 | grep    $link |
  sed -e "s|\(.*$link\).*|\1|" -e 's/ //g' -e 's|"||' && break
done)"

Come on guy, are you for real? No what that's necessary.

It is. (On a Mac, in 2014.) With a pretty average developer's system (nothing too exceptional, i swear):

#!/usr/bin/env sh # find and test linkers

which ld; echo "...Not necessarily."; echo
for c in cc gcc clang icc; do echo $c:
  for link in collect2 ld; do
    echo 'main;' | $c -v -x c - -o /dev/null -\#\#\# 2>&1 | grep -q $link &&
    echo 'main;' | $c -v -x c - -o /dev/null -\#\#\# 2>&1 | grep    $link |
             sed -e "s|\(.*$link\).*|\1|" -e 's/ //g' -e 's|"||' && break
   done
  which="$(which which 2>/dev/null 1>&1)" || which=echo
  $which "$(for link in collect2 ld; do
    echo 'main;' | $c -v -x c - -o /dev/null -\#\#\# 2>&1 | grep -q $link &&
    echo 'main;' | $c -v -x c - -o /dev/null -\#\#\# 2>&1 | grep    $link |
   sed -e "s|\(.*$link\).*|\1|" -e 's/ //g' -e 's|"||' && break
  done)"
  echo
done

Returns:

/usr/bin/ld
...Not necessarily.

cc:
/Applications/Xcode51-Beta4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld
/Applications/Xcode51-Beta4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld

gcc:
/Users/Shared/usr/local/Cellar/gcc49/4.9-20140119/libexec/gcc/x86_64-apple-darwin13.1.0/4.9.0/collect2
/Users/Shared/usr/local/Cellar/gcc49/4.9-20140119/libexec/gcc/x86_64-apple-darwin13.1.0/4.9.0/collect2

clang:
/Applications/Xcode51-Beta4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld
/Applications/Xcode51-Beta4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld

icc:
ld
/usr/bin/ld
@23Skidoo
Collaborator

@g-nix Can you make a pull request with the proposed change?

@geoff-codes

@23Skidoo Done! (Closing in favor of discussion in PR #1673.)

@geoff-codes geoff-codes closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.