Skip to content

Commit 9eee276

Browse files
committed
handle new ld options in a nicer way, and launch the older ld if targetting macOS <= 10.6
1 parent 2117574 commit 9eee276

File tree

2 files changed

+107
-39
lines changed

2 files changed

+107
-39
lines changed

README.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,39 @@ Here are the latest versions of Xcode that are known to /run/ on each OS X versi
8989

9090
More information about the compilers included in each version of Xcode can be found on the [MacPorts Wiki](https://trac.macports.org/wiki/XcodeVersionInfo).
9191

92-
### Linking for ppc on Xcode 7.3 and later
92+
### Linking for x86_64 on Xcode 4.4 and later
9393

94-
The following error may appear when linking a program using the older compilers from Xcode (command-line and Makefile-based builds should not be affected):
94+
If targetting 10.6, the following error may appear:
9595
```
96-
Running ld for ppc ...
97-
ld: unknown option: -object_path_lto
96+
For architecture x86_64: Undefinedsymbols
97+
98+
"_objc_retain", from: referenced
99+
100+
In libarclite_macosx.a ___ARCLite__load (arclite.o)
101+
102+
(youmeant: _objc_retainedObject maybe)
103+
104+
Symbol not (s) found for architecture x86_64 ld:
105+
106+
Error: linker command failed with exit code use 1 (-v to seeinvocation clang:)
98107
```
99-
or
108+
109+
Solution: in the Build Setting of the Project (not for the Target), set the setting "Implicitly Link Objective-C Runtime Support" to NO.
110+
111+
### Linking for ppc on Xcode 7.3 and later
112+
113+
Recent versions of Xcode and ld added several options. These are taken care of by the stub ld script (notably -object_path_lto xxx, -no_deduplicate, -dependency_info xxx), but after an Xcode upgrade new errors may appear, like:
114+
100115
```
101116
Running ld for ppc ...
102-
ld: unknown option: -no_deduplicate
117+
ld: unknown option: -object_path_lto
103118
```
104-
The reason is that the newer versions of the linker introduced the options `-object_path_lto` and `-no_deduplicate`, which Xcode adds by default. To disable this, add the following two User-Defined build setting in Xcode (by clicking on the `+` at the to of the right column in the Build Settings panel):
105119

106-
- `LD_LTO_OBJECT_FILE` with an empty value
107-
- `LD_DONT_RUN_DEDUPLICATION` with value set to `NO`.
120+
There are two possible solutions:
121+
122+
- check in the file `/Applications/Xcode.app/Contents/PlugIns/Xcode3Core.ideplugin/Contents/SharedSupport/Developer/Library/Xcode/Plug-ins/CoreBuildTasks.xcplugin/Contents/Resources/Ld.xcspec` if there is an Xcode setting to disable that option (`LD_LTO_OBJECT_FILE` in the above case)
123+
- edit `/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld` to prune the culprid option (and its argument)
108124

109-
For future reference, other settings that control the link options are located in: `/Applications/Xcode.app/Contents/PlugIns/Xcode3Core.ideplugin/Contents/SharedSupport/Developer/Library/Xcode/Plug-ins/CoreBuildTasks.xcplugin/Contents/Resources/Ld.xcspec`
110125

111126
Known bugs (and fixes) in OS X SDKs
112127
-----------------------------------

XcodeLegacy.sh

Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -497,18 +497,21 @@ EOF
497497
mkdir -p "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc7400"
498498
mkdir -p "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc970"
499499
mkdir -p "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc64"
500+
mkdir -p "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/i386"
501+
mkdir -p "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/x86_64"
500502
ln -sf "$GCCDIR/usr/libexec/gcc/darwin/ppc/ld" "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc/ld"
501503
ln -sf "$GCCDIR/usr/libexec/gcc/darwin/ppc/ld" "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc7400/ld"
502504
ln -sf "$GCCDIR/usr/libexec/gcc/darwin/ppc/ld" "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc970/ld"
503505
ln -sf "$GCCDIR/usr/libexec/gcc/darwin/ppc64/ld" "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc64/ld"
504-
# Xcode 8's ld fails to link i386 for OSX 10.5: https://github.com/devernay/xcodelegacy/issues/30
505-
# Since this ld is from Xcode 3.2.6 for OSX 10.6, this should be OK in most cases,
506-
# but it may pose a problem if linking i386 with MACOSX_DEPLOYMENT_TARGET or -mmacosx-version-min > 10.6
507-
# (but who would do such a thing?)
508-
mkdir -p "$GCCDIR/usr/libexec/gcc/darwin/i386"
509-
ln "$GCCDIR/usr/libexec/gcc/darwin/ppc/ld" "$GCCDIR/usr/libexec/gcc/darwin/i386/ld"
510-
mkdir -p "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/i386"
511-
ln -sf "$GCCDIR/usr/libexec/gcc/darwin/i386/ld" "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/i386/ld"
506+
# Xcode 8's ld fails to link i386 and x86_64 for OSX 10.5: https://github.com/devernay/xcodelegacy/issues/30
507+
# Since this ld is from Xcode 3.2.6 for OSX 10.6, this should be OK if the target OS is < 10.6
508+
# (which is checked by the stub ld script)
509+
for arch in i386 x86_64; do
510+
mkdir -p "$GCCDIR/usr/libexec/gcc/darwin/$arch"
511+
ln "$GCCDIR/usr/libexec/gcc/darwin/ppc/ld" "$GCCDIR/usr/libexec/gcc/darwin/$arch/ld"
512+
mkdir -p "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/$arch"
513+
ln -sf "$GCCDIR/usr/libexec/gcc/darwin/$arch/ld" "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/$arch/ld"
514+
done
512515
# prevent overwriting the original ld if the script is run twice
513516
if [ ! -f "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld-original" ]; then
514517
mv "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld-original"
@@ -522,13 +525,43 @@ for var in "\$@"
522525
do
523526
if [ "\$ARCH_FOUND" -eq '1' ]; then
524527
ARCH=\$var
528+
ARCH_FOUND=2
525529
break
526-
elif [ "\$var" = '-arch' ]; then
527-
ARCH_FOUND=1
528-
fi
530+
else
531+
case "\$var" in
532+
-mmacosx-version-min=10.[0-6])
533+
MACOSX_DEPLOYMENT_TARGET=\$( echo \$var | sed -e s/-mmacosx-version-min=// )
534+
;;
535+
-arch)
536+
if [ "\$ARCH_FOUND" -ne '0' ]; then
537+
echo "Warning: ld: multiple -arch flags"
538+
fi
539+
ARCH_FOUND=1
540+
;;
541+
esac
542+
fi
543+
529544
done
530545
531-
echo "Running ld for \$ARCH ..."
546+
# use the old (Snow Leopard 10.6) ld only if ppc arch or the target macOS is <= 10.6
547+
USE_OLD_LD=0
548+
case "\$ARCH" in
549+
ppc*) #ppc ppc7400 ppc970 ppc64
550+
USE_OLD_LD=1
551+
;;
552+
esac
553+
554+
if [ -n \${MACOSX_DEPLOYMENT_TARGET+x} ]; then
555+
# MACOSX_DEPLOYMENT_TARGET can either be set externally as an env variable,
556+
# or as an ld option using -mmacosx-version-min=10.x
557+
case "\${MACOSX_DEPLOYMENT_TARGET}" in
558+
10.[0-6])
559+
USE_OLD_LD=1
560+
;;
561+
esac
562+
fi
563+
564+
#echo "Running ld for \$ARCH ..."
532565
533566
LD_DIR=\`dirname "\$0"\`
534567
if [ -x "\$LD_DIR/ld-original" ]; then
@@ -542,30 +575,43 @@ else
542575
exit 1
543576
fi
544577
LD_RESULT=255
545-
if [ "\$ARCH" = 'ppc' ] || [ "\$ARCH" = 'ppc7400' ] || [ "\$ARCH" = 'ppc970' ] || [ "\$ARCH" = 'ppc64' ] || [ "\$ARCH" = 'i386' ]; then
578+
if [ "\$USE_OLD_LD" -eq '1' ]; then
546579
ARGS=()
580+
# strip the -dependency_info xxx, -object_path_lto xxx, -no_deduplicate flags
547581
DEPINFO_FOUND=0
582+
OBJECT_PATH_LTO_FOUND=0
548583
for var in "\$@"; do
549584
if [ "\$DEPINFO_FOUND" -eq '1' ]; then
550585
DEPINFO_FOUND=0
551586
continue
587+
elif [ "\$OBJECT_PATH_LTO_FOUND" -eq '1' ]; then
588+
OBJECT_PATH_LTO_FOUND=0
589+
continue
552590
elif [ "\$var" = '-dependency_info' ]; then
553591
DEPINFO_FOUND=1
554592
continue
593+
elif [ "\$var" = '-object_path_lto' ]; then
594+
OBJECT_PATH_LTO_FOUND=1
595+
continue
596+
elif [ "\$var" = '-no_deduplicate' ]; then
597+
continue
555598
fi
556599
557600
ARGS+=("\$var")
558601
done
559-
if [ -x "\$LD_DIR/../libexec/ld/\$ARCH/ld" ]; then
560-
LD="\$LD_DIR/../libexec/ld/\$ARCH/ld"
561-
elif [ -x "\$LD_DIR/../../../libexec/ld/\$ARCH/ld" ]; then
562-
LD="\$LD_DIR/../../../libexec/ld/\$ARCH/ld"
563-
elif [ -x "\$LD_DIR/../../../../libexec/ld/\$ARCH/ld" ]; then
564-
LD="\$LD_DIR/../../../../libexec/ld/\$ARCH/ld"
565-
elif [ -x "\$LD_DIR/../../../../../libexec/ld/\$ARCH/ld" ]; then
566-
LD="\$LD_DIR/../../../../../libexec/ld/\$ARCH/ld"
602+
# the old ld is put in the ppc dir so as not to disturb more recent archs (i386, x86_64)
603+
# works with ppc ppc7400 ppc970 ppc64 i386 x86_64
604+
LDARCHDIR=ppc
605+
if [ -x "\$LD_DIR/../libexec/ld/\$LDARCHDIR/ld" ]; then
606+
LD="\$LD_DIR/../libexec/ld/\$LDARCHDIR/ld"
607+
elif [ -x "\$LD_DIR/../../../libexec/ld/\$LDARCHDIR/ld" ]; then
608+
LD="\$LD_DIR/../../../libexec/ld/\$LDARCHDIR/ld"
609+
elif [ -x "\$LD_DIR/../../../../libexec/ld/\$LDARCHDIR/ld" ]; then
610+
LD="\$LD_DIR/../../../../libexec/ld/\$LDARCHDIR/ld"
611+
elif [ -x "\$LD_DIR/../../../../../libexec/ld/\$LDARCHDIR/ld" ]; then
612+
LD="\$LD_DIR/../../../../../libexec/ld/\$LDARCHDIR/ld"
567613
else
568-
echo "Error: cannot find ld for \$ARCH in \$LD_DIR/../libexec/ld/\$ARCH \$LD_DIR/../../../libexec/ld/\$ARCH \$LD_DIR/../../../../libexec/ld/\$ARCH or \$LD_DIR/../../../../../libexec/ld/\$ARCH"
614+
echo "Error: cannot find ld for \$ARCH in \$LD_DIR/../libexec/ld/\$LDARCHDIR \$LD_DIR/../../../libexec/ld/\$LDARCHDIR \$LD_DIR/../../../../libexec/ld/\$LDARCHDIR or \$LD_DIR/../../../../../libexec/ld/\$LDARCHDIR"
569615
exit 1
570616
fi
571617
@@ -838,13 +884,20 @@ SPEC_EOF
838884
if [ -f "$PLUGINDIR/LLVM GCC 4.2.xcplugin/legacy" ]; then
839885
rm -rf "$PLUGINDIR/LLVM GCC 4.2.xcplugin"
840886
fi
841-
rm -rf "$GCCDIR/usr/libexec/gcc/darwin/ppc" "$GCCDIR/usr/libexec/gcc/darwin/ppc64"
842-
rm -rf "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/as/ppc"
843-
rm -rf "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/as/ppc64"
844-
rm -rf "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc"
845-
rm -rf "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc7400"
846-
rm -rf "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc970"
847-
rm -rf "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc64"
887+
for f in "$GCCDIR/usr/libexec/gcc/darwin/ppc" \
888+
"$GCCDIR/usr/libexec/gcc/darwin/ppc64" \
889+
"$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/as/ppc" \
890+
"$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/as/ppc64" \
891+
"$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc" \
892+
"$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc7400" \
893+
"$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc970" \
894+
"$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/ppc64" \
895+
"$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/i386" \
896+
"$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/libexec/ld/x86_64"; do
897+
if [ -e "$f" ]; then
898+
rm -rf "$f"
899+
fi
900+
done
848901
if [ -f "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld-original" ]; then
849902
rm "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld"
850903
mv -f "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld-original" "$GCCDIR/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld"

0 commit comments

Comments
 (0)