#!/bin/sh
set -x
HOSTS="$1"
TARGETS="$2"
ORIG_SRC_DIR="$3"
DEST_ROOT="$4"
DEST_DIR="$5"
SYM_DIR="$6"
LLVM_ASSERTIONS="$7"
LLVM_OPTIMIZED="$8"
INSTALL_LIBLTO="$9"
LLVM_SUBMIT_VERSION="${10}"
LLVM_SUBMIT_SUBVERSION="${11}"
DIR=`pwd`
DARWIN_VERS=`uname -r | sed 's/\..*//'`
echo DARWIN_VERS = $DARWIN_VERS
if [ "x$RC_ProjectName" = "xllvmCore_Embedded" ]; then
DEST_DIR="$DEST_DIR/Developer/Platforms/iPhoneOS.platform"
mkdir -p "$DEST_DIR"
fi
DEVELOPER_DIR="${DEVELOPER_DIR-Developer}"
if [ "x$RC_ProjectName" = "xllvmCore_EmbeddedHosted" ]; then
DT_HOME="$DEST_DIR/usr"
HOST_SDKROOT=$SDKROOT
else
DT_HOME="$DEST_DIR/$DEVELOPER_DIR/usr"
fi
DEST_ROOT="/$DEVELOPER_DIR$DEST_ROOT"
SRC_DIR=$DIR/src
rm -rf $SRC_DIR || exit 1
mkdir $SRC_DIR || exit 1
ln -s $ORIG_SRC_DIR/* $SRC_DIR/ || exit 1
rm $SRC_DIR/Makefile || exit 1
sed -e '/[Aa]pple-style/d' -e '/include.*GNUmakefile/d' $ORIG_SRC_DIR/Makefile > $SRC_DIR/Makefile || exit 1
mkdir -p $DIR/obj-llvm || exit 1
cd $DIR/obj-llvm || exit 1
if [ "x$RC_ProjectName" = "xllvmCore_EmbeddedHosted" ]; then
rm -rf $DIR/bin || exit 1
mkdir $DIR/bin || exit 1
for prog in ar nm ranlib strip lipo ld as ; do
P=$DIR/bin/arm-apple-darwin$DARWIN_VERS-${prog}
T=`xcrun -sdk $SDKROOT -find ${prog}`
echo ' echo 'exec '$T' "$@"' >> $P || exit 1
chmod a+x $P || exit 1
done
for prog in gcc g++ ; do
P=$DIR/bin/arm-apple-darwin$DARWIN_VERS-${prog}
T=`xcrun -find llvm-${prog}`
if [ "x$T" = "x" ] ; then
T=`xcrun -sdk $SDKROOT -find ${prog}`
fi
echo ' echo 'exec '$T' -arch armv6 -isysroot '${SDKROOT}' "$@"' >> $P || exit 1
chmod a+x $P || exit 1
done
PATH=$DIR/bin:$PATH
elif [ $DARWIN_VERS -gt 9 ]; then
savedPATH=$PATH ; PATH="/Developer/usr/bin:$PATH"
XTMPCC=$(which llvm-gcc)
if [ x$CC = x -a x$XTMPCC != x ] ; then export CC=$XTMPCC ; fi
XTMPCC=$(which llvm-g++)
if [ x$CXX = x -a x$XTMPCC != x ] ; then export CXX=$XTMPCC ; fi
PATH=$savedPATH
unset XTMPCC savedPATH
fi
if [ "x$RC_ProjectName" = "xllvmCore_EmbeddedHosted" ]; then
if [ \! -f Makefile.config ]; then
$SRC_DIR/configure --prefix=$DT_HOME \
--enable-targets=arm \
--host=arm-apple-darwin10 \
--target=arm-apple-darwin10 \
--build=i686-apple-darwin10 \
--enable-assertions=$LLVM_ASSERTIONS \
--enable-optimized=$LLVM_OPTIMIZED \
--disable-bindings \
|| exit 1
fi
else
if [ \! -f Makefile.config ]; then
$SRC_DIR/configure --prefix=$DT_HOME/local \
--enable-targets=arm,x86,cbe \
--enable-assertions=$LLVM_ASSERTIONS \
--enable-optimized=$LLVM_OPTIMIZED \
--disable-bindings \
|| exit 1
fi
fi
SUBVERSION=`echo $RC_ProjectSourceVersion | sed -e 's/[^.]*\.\([0-9]*\).*/\1/'`
if [ "x$SUBVERSION" != "x$RC_ProjectSourceVersion" ]; then
LLVM_SUBMIT_SUBVERSION=`printf "%02d" $SUBVERSION`
RC_ProjectSourceVersion=`echo $RC_ProjectSourceVersion | sed -e 's/\..*//'`
LLVM_SUBMIT_VERSION=$RC_ProjectSourceVersion
fi
if [ "x$LLVM_SUBMIT_SUBVERSION" = "x00" -o "x$LLVM_SUBMIT_SUBVERSION" = "x0" ]; then
LLVM_VERSION="$LLVM_SUBMIT_VERSION"
else
LLVM_VERSION="$LLVM_SUBMIT_VERSION-$LLVM_SUBMIT_SUBVERSION"
fi
GCC_VER=`cc --version 2>/dev/null | sed 1q`
if echo "$GCC_VER" | grep GCC > /dev/null; then
GCC_VER=`echo $GCC_VER | sed -e 's/.*(GCC) \([0-9.][0-9.]*\).*/\1/'`
MAJ_VER=`echo $GCC_VER | sed 's/\..*//'`
MIN_VER=`echo $GCC_VER | sed 's/[^.]*\.\([0-9]*\).*/\1/'`
fi
JOBS_FLAG=""
if [ "x$MAJ_VER" != "x4" -o "x$MIN_VER" != "x0" ]; then
SYSCTL=`sysctl -n hw.activecpu`
if [ -z "$SYSCTL" ]; then
SYSCTL=`sysctl -n hw.ncpu`
fi
if [ -z "$SYSCTL" ]; then
SYSCTL=2
fi
JOBS_FLAG="-j $SYSCTL"
fi
make $JOBS_FLAG $OPTIMIZE_OPTS UNIVERSAL=1 UNIVERSAL_ARCH="$HOSTS" \
UNIVERSAL_SDK_PATH=$HOST_SDKROOT \
NO_RUNTIME_LIBS=1 \
REQUIRES_RTTI=1 \
DISABLE_EDIS=1 \
DEBUG_SYMBOLS=1 \
LLVM_SUBMIT_VERSION=$LLVM_SUBMIT_VERSION \
LLVM_SUBMIT_SUBVERSION=$LLVM_SUBMIT_SUBVERSION \
CXXFLAGS="-DLLVM_VERSION_INFO='\" Apple Build #$LLVM_VERSION\"'" \
VERBOSE=1
if [ $? != 0 ] ; then
echo "error: LLVM 'make' failed!"
exit 1
fi
cd $DEST_DIR || exit 1
rm -rf * || exit 1
cd $DIR/obj-llvm || exit 1
make $LOCAL_MAKEFLAGS $OPTIMIZE_OPTS UNIVERSAL=1 UNIVERSAL_ARCH="$HOSTS" \
NO_RUNTIME_LIBS=1 \
DISABLE_EDIS=1 \
DEBUG_SYMBOLS=1 \
LLVM_SUBMIT_VERSION=$LLVM_SUBMIT_VERSION \
LLVM_SUBMIT_SUBVERSION=$LLVM_SUBMIT_SUBVERSION \
OPTIMIZE_OPTION='-O3' VERBOSE=1 install
if ! test $? == 0 ; then
echo "error: LLVM 'make install' failed!"
exit 1
fi
LLVM_MINOR_VERSION=`echo $LLVM_SUBMIT_SUBVERSION | sed -e 's,0*\([1-9][0-9]*\),\1,'`
if [ "x$LLVM_MINOR_VERSION" = "x" ]; then
LLVM_MINOR_VERSION=0
fi
RC_ProjectSourceSubversion=`printf "%d" $LLVM_MINOR_VERSION`
echo "#define LLVM_VERSION ${RC_ProjectSourceVersion}" > $DEST_DIR$DEST_ROOT/include/llvm/Version.h
echo "#define LLVM_MINOR_VERSION ${RC_ProjectSourceSubversion}" >> $DEST_DIR$DEST_ROOT/include/llvm/Version.h
if [ "x$LLVM_DEBUG" != "x1" ]; then
strip -Sl $DEST_DIR$DEST_ROOT/lib/*.[oa]
for f in `ls $DEST_DIR$DEST_ROOT/lib/*.so`; do
strip -Sxl $f
done
fi
cp `find $DIR -name tblgen` $DT_HOME/local/bin
cd $DEST_DIR$DEST_ROOT
rm -f bin/.dir etc/llvm/.dir lib/.dir
cd $DEST_DIR$DEST_ROOT/bin
if [ $MACOSX_DEPLOYMENT_TARGET = "10.4" ]; then
find . -perm 755 -type f \! \( -name '*gccas' -o -name '*gccld' -o -name llvm-config \) \
-exec lipo -extract ppc -extract i386 {} -output {} \;
elif [ $MACOSX_DEPLOYMENT_TARGET = "10.5" ]; then
find . -perm 755 -type f \! \( -name '*gccas' -o -name '*gccld' -o -name llvm-config \) \
-exec lipo -extract ppc7400 -extract i386 {} -output {} \;
else
find . -perm 755 -type f \! \( -name '*gccas' -o -name '*gccld' -o -name llvm-config \) \
-exec lipo -extract i386 -extract x86_64 {} -output {} \;
fi
rm $DEST_DIR$DEST_ROOT/lib/*LLVMHello.dylib
MDIR=$DEST_DIR$DEST_ROOT/share/man/man1
gzip -f $MDIR/*
SYSCTL=`sysctl -n hw.activecpu`
if [ -z "$SYSCTL" ]; then
SYSCTL=`sysctl -n hw.ncpu`
fi
if [ -z "$SYSCTL" ]; then
SYSCTL=2
fi
cd $SYM_DIR || exit 1
rm -rf * || exit 1
find $DEST_DIR -perm -0111 -type f \
! \( -name '*.la' -o -name gccas -o -name gccld -o -name llvm-config -o -name '*.a' \) \
-print | xargs -n 1 -P ${SYSCTL} dsymutil
cd $DEST_DIR || exit 1
find . \( -path \*.dSYM/\* -or -name \*.a \) -print \
| cpio -pdml $SYM_DIR || exit 1
mkdir $SYM_DIR/src || exit 1
cd $DIR || exit 1
find obj-* -name \*.\[chy\] -o -name \*.cpp -print \
| cpio -pdml $SYM_DIR/src || exit 1
cd $DEST_DIR$DEST_ROOT
if [ "$INSTALL_LIBLTO" = "yes" ]; then
mkdir -p $DT_HOME/lib
mv lib/libLTO.dylib $DT_HOME/lib/libLTO.dylib
strip -arch all -Sl $DT_HOME/lib/libLTO.dylib
fi
rm -f lib/libLTO.a lib/libLTO.la
find $DEST_DIR$DEST_ROOT -name lto.h -delete
cd $DIR || exit 1
find $DEST_DIR -name \*.a -print | xargs ranlib || exit 1
find $DEST_DIR -name \*.dSYM -print | xargs rm -r || exit 1
find $DEST_DIR -perm -0111 -type f \
! \( -name '*.la' -o -name gccas -o -name gccld -o -name llvm-config \) \
-print | xargs -n 1 -P ${SYSCTL} strip -arch all -Sl
chgrp -h -R wheel $DEST_DIR
chgrp -R wheel $DEST_DIR
find $DEST_DIR -name html.tar.gz -exec rm {} \;
if [ "$INSTALL_LIBLTO" = "yes" ]; then
mkdir -p $DEST_DIR/usr/lib/
cd $DEST_DIR/usr/lib && \
ln -s ../../$DEVELOPER_DIR/usr/lib/libLTO.dylib ./libLTO.dylib
fi
exit 0