Skip to content

Instantly share code, notes, and snippets.

@fritschy
Created March 23, 2015 14:09
Show Gist options
  • Save fritschy/d26add68d5dfa3fe3782 to your computer and use it in GitHub Desktop.
Save fritschy/d26add68d5dfa3fe3782 to your computer and use it in GitHub Desktop.
Build clang, doing self-hosting
#! /bin/zsh
# Get llvm, clang and compiler-rt from these locations:
#INSTALL#http://llvm.org/git/llvm.git#llvm
#INSTALL#http://llvm.org/git/clang.git#llvm/tools/clang
#INSTALL#http://llvm.org/git/lldb.git#llvm/tools/lldb
#INSTALL#http://llvm.org/git/compiler-rt.git#llvm/projects/compiler-rt
if [ "$1" = "get" ] || [ "$1" = "install" ]
then
grep '^#INSTALL#' $0|while IFS='#' read u u url dst
do
git clone $url $dst
done
cp -alf $0 llvm/ 2>/dev/null || cp $0 llvm/
exit
fi
llvm_targets="ARM;CppBackend;X86"
build_type=Release
MAKE_FLAGS=-j8
PREFIX=$HOME/opt/llvm
make-stage()
{
stage=$1
prefix=$2
(
test -f ${stage}-stamp && return
test -d $stage && rm -rf $stage
mkdir $stage &&
cd $stage &&
cmake \
-DCMAKE_CXX_COMPILER:FILEPATH=$CXX \
-DCMAKE_BUILD_TYPE:STRING=$build_type \
-DLLVM_TARGETS_TO_BUILD:STRING=$llvm_targets \
-DCMAKE_C_COMPILER:FILEPATH=$CC \
-DCMAKE_INSTALL_PREFIX:PATH=$prefix \
-DCMAKE_C_FLAGS:STRING=$CFLAGS \
-DCMAKE_CXX_FLAGS:STRING=$CXXFLAGS \
-DBUILD_SHARED_LIBS:BOOL=ON \
-DLIBCLANG_BUILD_STATIC:BOOL=ON \
-G "Ninja" \
../.. &&
ninja &&
ninja check-all &&
ninja check-sanitizer &&
ninja check-asan &&
ninja install
)
}
P=$PATH
L=$LD_LIBRARY_PATH
[ -d build ] || mkdir build
cd build
if [ ! -f stage1-stamp ]
then
export CC=${CC:=clang}
export CXX=${CXX:=clang++}
make-stage stage1 $PWD/stage1/install || { echo "Stage 1 failed..."; exit 1; }
touch stage1-stamp
fi
last=1
for i in {1..$last}
do
if [ ! -f stage2.${i}-stamp ]
then
export PATH=$PWD/stage${last}/install/bin:$P
export LD_LIBRARY_PATH=$PWD/stage${last}/install/lib:$L
export CC=$PWD/stage${last}/install/bin/clang
export CXX=$PWD/stage${last}/install/bin/clang++
make-stage stage2.${i} $PWD/stage2.${i}/install || { echo "Stage 2.${i} failed..."; exit 1; }
touch stage2.${i}-stamp
fi
last=2.$i
done
if [ ! -f stage3-stamp ]
then
export PATH=$PWD/stage${last}/install/bin:$P
export LD_LIBRARY_PATH=$PWD/stage${last}/install/lib:$L
export CC=$PWD/stage${last}/install/bin/clang
export CXX=$PWD/stage${last}/install/bin/clang++
make-stage stage3 $PREFIX || { echo "Stage 3 failed..."; exit 1; }
touch stage3-stamp
fi
echo All done...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment