Skip to content

Instantly share code, notes, and snippets.

@zchee
Last active November 28, 2021 10:37
Show Gist options
  • Save zchee/740e99acd893afeeae6d to your computer and use it in GitHub Desktop.
Save zchee/740e99acd893afeeae6d to your computer and use it in GitHub Desktop.
Build llvm for OS X
#!/bin/bash
set -e
# Building LLVM on OSX CMake setup script
#
# Required:
# - clang by Xcode6 or later
# - cmake
# - ninja
#
# Usage:
# build-llvm.bash <LLVM SOURCES ROOT> <CMAKE GENERATORS> <PYTHON VERSION> [DIST_DIR]
#
# CMake Generators: Unix Makefiles, Ninja, Xcode (Recommend is Ninja)
#
# Python Version: 2 or 3
# - for LLDB dylib
#
# Tested version:
# Apple LLVM version 7.3.0 (clang-703.0.28)
# Target: x86_64-apple-darwin15.4.0
#
#
# References:
# - llvm 3.8 currently requires Xcode 7(OS X 10.10 or 10.11) Build the new tsan support in compiler-rt or using
# - -DLLVM_BUILD_EXTERNAL_COMPILER_RT:BOOL=ON
# - Ref: https://groups.google.com/forum/m/#!topic/llvm-dev/Uxb1o83qTMI
# - Building with Clan Address Sanitizer(ASan)
# - https://trac.webkit.org/wiki/ASanWebKit
# - Not build llvm-dsymutil
# - When LLVM_ENABLE_LTO, link for llvm-dsymutil too slow...
# - github.com/apple/apple-llvm instead of
# - -DLLVM_TOOL_DSYMUTIL_BUILD:BOOL=OFF
# define install directory
if [[ $4 ]]; then
DST_DIR=$4
else
DST_DIR=/opt/llvm
fi
# define Python Frameworks path
if [[ $3 != "System" ]]; then
PYTHON_CONFIG=python$3-config
PYTHON_EXECUTABLE=$(which python"$3")
else
PYTHON_CONFIG=/usr/bin/python-config
PYTHON_EXECUTABLE=/usr/bin/python
fi
PYTHON_INCLUDE_DIR=$(find $("$PYTHON_CONFIG" --prefix)/include -name 'python*' -type d -maxdepth 1)
PYTHON_LIBRARY=$(find $("$PYTHON_CONFIG" --prefix)/lib -name 'libpython*' -maxdepth 1)
ENVIRONMENT_VARIABLE="\
\$SRC_DIR: $1
\$DST_DIR: $DST_DIR
\$PYTHON_EXECUTABLE: $PYTHON_EXECUTABLE
\$PYTHON_INCLUDE_DIR: $PYTHON_INCLUDE_DIR
\$PYTHON_LIBRARY: $PYTHON_LIBRARY"
# Usage
usage() {
USAGE_CMAKE_GENERATORS="\
Unix Makefiles
Ninja
Xcode
CodeBlocks - Ninja
CodeBlocks - Unix Makefiles
CodeLite - Ninja
CodeLite - Unix Makefiles
Eclipse CDT4 - Ninja
Eclipse CDT4 - Unix Makefiles
KDevelop3
KDevelop3 - Unix Makefiles
Kate - Ninja
Kate - Unix Makefiles
Sublime Text 2 - Ninja
Sublime Text 2 - Unix Makefiles"
USAGE_PYTHON_VERSION="\
2
3
Sytem"
echo "error: No build tool as arguments"
echo ""
echo "Usage: $0 <LLVM SOURCES ROOT> <CMake Generators> <Python Version> [\$DIST_DIR]"
echo ""
echo "CMake Generators"
echo " $USAGE_CMAKE_GENERATORS"
echo ""
echo "Python Version"
echo " $USAGE_PYTHON_VERSION"
echo ""
echo "Environment variable:"
echo " $ENVIRONMENT_VARIABLE"
exit 1
}
# Check len(args)
if [[ ! -n $3 ]]; then
usage
fi
# define dependencies packages
PYTHON="\
-DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE \
-DPYTHON_INCLUDE_DIR=$PYTHON_INCLUDE_DIR \
-DPYTHON_LIBRARY=$PYTHON_LIBRARY"
LIBXML2="\
-DLIBXML2_INCLUDE_DIR=/usr/local/opt/libxml2/include/libxml2 \
-DLIBXML2_LIBRARIES=/usr/local/opt/libxml2/lib/libxml2.dylib \
-DLIBXML2_XMLLINT_EXECUTABLE=/usr/local/opt/libxml2/bin/xmllint"
LIBFFI="\
-DFFI_INCLUDE_DIR=/usr/local/opt/libffi/lib/libffi-3.0.13/include \
-DFFI_LIBRARY_DIR=/usr/local/opt/libffi/lib/libffi.dylib"
# Warning for have not '*build*' in current dir name
if ! [[ $(readlink -f .) == *build* ]]; then
echo "Current dir needs build* name"
exit 1
fi
# if hash ccache 2>/dev/null; then
# export CC="ccache $CC"
# export CXX="ccache $CXX"
# fi
echo "CMake Generators"
echo " $2"
echo "Python Version"
echo " $3"
echo "Environment variable:"
echo " $ENVIRONMENT_VARIABLE"
echo ""
command cmake $1 -G $2 \
\
-DBUILD_SHARED_LIBS:BOOL=OFF \
\
-DCMAKE_ASM_FLAGS:STRING='-march=native' \
-DCMAKE_BUILD_TYPE:STRING=Release \
-DCMAKE_CXX_FLAGS:STRING='-march=native' \
-DCMAKE_C_FLAGS:STRING='-march=native' \
-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=ON \
-DCMAKE_FIND_FRAMEWORK=LAST \
-DCMAKE_INSTALL_PREFIX:PATH=$DST_DIR \
-DCMAKE_OSX_ARCHITECTURES:STRING=x86_64 \
-DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=10.11 \
-DCMAKE_OSX_SYSROOT:STRING="$(xcrun --show-sdk-path)" \
-DCMAKE_SKIP_INSTALL_RPATH:BOOL=NO \
-DCMAKE_SKIP_RPATH:BOOL=NO \
\
-DCLANG_DEFAULT_CXX_STDLIB:STRING=libc++ \
-DCLANG_INCLUDE_DOCS:BOOL=ON \
-DCLANG_INCLUDE_TESTS:BOOL=ON \
\
-DCURSES_CURSES_LIBRARY:FILEPATH=/usr/local/lib/libcurses.dylib \
-DCURSES_FORM_LIBRARY:FILEPATH=/usr/local/lib/libform.dylib \
-DCURSES_INCLUDE_PATH:PATH=/usr/local/include \
-DCURSES_NCURSES_LIBRARY:FILEPATH=/usr/local/lib/libncurses.dylib \
-DCURSES_PANEL_LIBRARY:FILEPATH=/usr/local/lib/libpanel.dylib \
\
-DLIBCLANG_BUILD_STATIC:BOOL=ON \
-DLIBCXX_ABI_UNSTABLE:BOOL=ON \
-DLIBCXX_CONFIGURE_IDE:BOOL=ON \
-DLIBCXXABI_ENABLE_ASSERTIONS:BOOL=OFF \
-DLIBCXXABI_SYSROOT:STRING="$(xcrun --show-sdk-path)" \
-DLIBCXXABI_USE_LLVM_UNWINDER:BOOL=OFF \
\
-DLIBCXX_ENABLE_ASSERTIONS:BOOL=OFF \
-DLIBCXX_OVERRIDE_DARWIN_INSTALL:BOOL=ON \
-DLIBCXX_SYSROOT:STRING="$(xcrun --show-sdk-path)" \
\
-DLIBOMP_OSX_ARCHITECTURES:STRING=x86_64 \
-DLIBOMP_USE_HWLOC:BOOL=ON \
\
-DLLDB_EXPORT_ALL_SYMBOLS:BOOL=ON \
\
-DLLVM_BUILD_EXTERNAL_COMPILER_RT:BOOL=ON \
-DLLVM_BUILD_GLOBAL_ISEL:BOOL=ON \
-DLLVM_BUILD_LLVM_C_DYLIB:BOOL=ON \
-DLLVM_BUILD_LLVM_DYLIB:BOOL=ON \
-DLLVM_CREATE_XCODE_TOOLCHAIN:BOOL=OFF \
-DLLVM_ENABLE_ASSERTIONS:BOOL=OFF \
-DLLVM_ENABLE_CXX1Y:BOOL=ON \
-DLLVM_ENABLE_FFI:BOOL=OFF \
-DLLVM_ENABLE_LIBCXX:BOOL=ON \
-DLLVM_ENABLE_LIBCXXABI:BOOL=ON \
-DLLVM_ENABLE_SPHINX:BOOL=OFF \
-DLLVM_EXTERNALIZE_DEBUGINFO:BOOL=OFF \
-DLLVM_INCLUDE_DOCS:BOOL=ON \
-DLLVM_LINK_LLVM_DYLIB:BOOL=OFF \
-DLLVM_OPTIMIZED_TABLEGEN:BOOL=ON \
-DLLVM_PARALLEL_COMPILE_JOBS:STRING=8 \
-DLLVM_PARALLEL_LINK_JOBS:STRING=8 \
-DLLVM_TARGET_ARCH:STRING=host \
-DLLVM_TARGETS_TO_BUILD:STRING=X86 \
-DLLVM_TOOL_DSYMUTIL_BUILD:BOOL=ON \
-DLLVM_USE_SPLIT_DWARF:BOOL=OFF \
\
-DPOLLY_ENABLE_GPGPU_CODEGEN:BOOL=ON \
\
-Wno-dev \
\
$(echo $PYTHON) \
$(echo $LIBXML2) \
\
-DLLVM_ENABLE_LTO:STRING=OFF \
-DLLVM_TOOL_LTO_BUILD:BOOL=OFF
# -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD:STRING= \
# $(echo $LIBFFI) \
# \
# -DC_INCLUDE_DIRS:STRING="$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain/usr/include:/usr/include" \
# Disable sanitizer (workaround)
# - clang: error: unsupported argument 'all' to option 'fno-sanitize='
if hash ag 2>/dev/null; then
ag -l " -fno-sanitize=all" | xargs sed -i 's/ -fno-sanitize=all//'
elif hash grep 2>/dev/null; then
grep -l " -fno-sanitize=all" $PWD | xargs sed -i 's/ -fno-sanitize=all//'
else
echo "Disable sanitizer required "
exit 1
fi
#!/bin/bash
set -e
if hash nproc 2>/dev/null; then
CUPS=$(($(nproc) + 1))
else
CUPS=$(($(sysctl -n hw.ncpu) + 1))
fi
if [[ -d "llvm" ]]; then
cd llvm
git fetch
else
git clone --depth=1 http://llvm.org/git/llvm.git
cd llvm
fi
if hash parallel 2>/dev/null; then
parallel -j $CUPS $1 'git submodule add --force --depth=1' http://llvm.org/git/{/}.git {.} ::: \
tools/clang \
projects/compiler-rt projects/libclc projects/libcxx projects/libcxxabi projects/openmp \
tools/lld tools/lldb tools/polly
else
git submodule add --force --depth=1 http://llvm.org/git/clang.git tools/clang
git submodule add --force --depth=1 http://llvm.org/git/compiler-rt.git projects/compiler-rt
git submodule add --force --depth=1 http://llvm.org/git/libclc.git projects/libclc
git submodule add --force --depth=1 http://llvm.org/git/libcxx.git projects/libcxx
git submodule add --force --depth=1 http://llvm.org/git/libcxxabi.git projects/libcxxabi
git submodule add --force --depth=1 http://llvm.org/git/openmp.git projects/openmp
git submodule add --force --depth=1 http://llvm.org/git/lld.git tools/lld
git submodule add --force --depth=1 http://llvm.org/git/lldb.git tools/lldb
git submodule add --force --depth=1 http://llvm.org/git/polly.git tools/polly
fi
cd tools/clang/tools
git submodule add --force --depth=1 http://llvm.org/git/clang-tools-extra.git extra
#!/bin/bash
set -e
if ! [[ -n $1 ]]; then
DIRECTORY=$(readlink -f .)
else
DIRECTORY=$1
fi
repository=$(find $DIRECTORY -type d -depth 1 \( ! -iname "build*" \))
echo 'Update repository'
for r in $repository
do
echo " $r"
done
for d in $repository
do
cd $d
BRANCH=$(git name-rev --name-only --no-undefined --always HEAD)
git pull origin $BRANCH
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment