Last active
November 2, 2022 07:41
-
-
Save WeZZard/5d847acf1c1aa197a718c57f69115fd1 to your computer and use it in GitHub Desktop.
llvm-config: config llvm build in macOS by putting the build dir beside llvm-project dir. The script must be in the same folder containing llvm-project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
PARENT_DIR="$(dirname $(cd "$(dirname "$0")"; pwd)/$(basename "$0"))" | |
CONFIG=Debug; | |
ASSERT=on; | |
CONSECUTIVE_ARG_PARSE_KIND=None; | |
ARCH=$(uname -m) | |
PLATFORM="macosx" | |
LLVM_ENABLED_PROJECTS=() | |
LLVM_ENABLED_RUNTIMES=() | |
DEFAULT_LLVM_ENABLED_PROJECTS=(clang clang-tools-extra) | |
DEFAULT_LLVM_ENABLED_RUNTIMES=(libcxx libcxxabi) | |
SUPPORTED_TARGETS="ARM;AArch64;X86" | |
function parse_arg_config() { | |
ARG=$1; | |
if [[ $ARG == "Release" ]] || [[ "$ARG" == "R" ]]; then | |
CONFIG=Release; | |
ASSERT=off; | |
fi | |
if [[ $ARG == "ReleaseAssert" ]] || [[ "$ARG" == "RA" ]]; then | |
CONFIG=Release; | |
ASSERT=on; | |
fi | |
if [[ $ARG == "Debug" ]] || [[ "$ARG" == "D" ]]; then | |
CONFIG=Debug; | |
ASSERT=off; | |
fi | |
if [[ $ARG == "DebugAssert" ]] || [[ "$ARG" == "DA" ]]; then | |
CONFIG=Debug; | |
ASSERT=on; | |
fi | |
return 0; | |
} | |
function parse_arg_arch() { | |
ARG=$1; | |
if [[ $ARG == "arm64" ]]; then | |
ARCH="arm64" | |
return 0; | |
fi | |
if [[ $ARG == "x86_64" ]]; then | |
ARCH="x86_64" | |
return 0; | |
fi | |
echo "Unrecognized architecture: $ARG."; | |
return 1 | |
} | |
function parse_arg_platform() { | |
ARG=$1; | |
if [[ $ARG == "macosx" ]]; then | |
PLATFORM="macosx" | |
fi | |
echo "Unrecognized platform: $ARG."; | |
return 1; | |
} | |
function parse_arg_llvm_enable_project() { | |
ARG=$1; | |
LLVM_ENABLED_PROJECTS+=($ARG); | |
return 0; | |
} | |
function parse_arg_llvm_enable_runtime() { | |
ARG=$1; | |
LLVM_ENABLED_RUNTIMES+=($ARG); | |
return 0; | |
} | |
function arg_parse() { | |
ARG=$1; | |
if [[ "$CONSECUTIVE_ARG_PARSE_KIND" == "--config" ]]; then | |
CONSECUTIVE_ARG_PARSE_KIND=None | |
parse_arg_config $ARG; | |
return $?; | |
fi | |
if [[ "$CONSECUTIVE_ARG_PARSE_KIND" == "--arch" ]]; then | |
CONSECUTIVE_ARG_PARSE_KIND=None | |
parse_arg_arch $ARG; | |
return $?; | |
fi | |
if [[ "$CONSECUTIVE_ARG_PARSE_KIND" == "--platform" ]]; then | |
CONSECUTIVE_ARG_PARSE_KIND=None | |
parse_arg_platform $ARG; | |
return $?; | |
fi | |
if [[ "$CONSECUTIVE_ARG_PARSE_KIND" == "--llvm-enable-project" ]]; then | |
CONSECUTIVE_ARG_PARSE_KIND=None | |
parse_arg_llvm_enable_project $ARG; | |
return $?; | |
fi | |
if [[ "$CONSECUTIVE_ARG_PARSE_KIND" == "--llvm-enable-runtime" ]]; then | |
CONSECUTIVE_ARG_PARSE_KIND=None | |
parse_arg_llvm_enable_runtime $ARG; | |
return $?; | |
fi | |
if [[ "$CONSECUTIVE_ARG_PARSE_KIND" -ne "None" ]]; then | |
echo "Invalid consecutive argument: $CONSECUTIVE_ARG_PARSE_KIND. There must be something behind it." | |
return 1; | |
fi | |
if [[ $ARG == "--config" ]]; then | |
CONSECUTIVE_ARG_PARSE_KIND="$ARG"; | |
return 0; | |
fi | |
if [[ $ARG == "--arch" ]]; then | |
CONSECUTIVE_ARG_PARSE_KIND="$ARG"; | |
return 0; | |
fi | |
if [[ $ARG == "--platform" ]]; then | |
CONSECUTIVE_ARG_PARSE_KIND="$ARG"; | |
return 0; | |
fi | |
if [[ $ARG == "--llvm-enable-project" ]]; then | |
CONSECUTIVE_ARG_PARSE_KIND="$ARG"; | |
return 0; | |
fi | |
if [[ $ARG == "--llvm-enable-runtime" ]]; then | |
CONSECUTIVE_ARG_PARSE_KIND="$ARG"; | |
return 0; | |
fi | |
echo "Unrecognized argument: $ARG."; | |
return 1 | |
} | |
for EACH_ARG in "$@"; do | |
arg_parse "$EACH_ARG" | |
RET_VAL=$? | |
if [[ $RET_VAL -ne 0 ]]; then | |
exit 1 | |
fi | |
done | |
GENERATOR=Ninja | |
if [[ "$ASSERT" == "off" ]]; then | |
CONFIG_SUFFIX="$CONFIG" | |
else | |
CONFIG_SUFFIX="$CONFIG"Assert | |
fi | |
LLVM_ENABLE_PROJECTS="" | |
if [[ "$LLVM_ENABLED_PROJECTS" == "" ]]; then | |
echo "No LLVM projects enabled. Use default instead." | |
LLVM_ENABLED_PROJECTS=${DEFAULT_LLVM_ENABLED_PROJECTS[@]}; | |
fi | |
for EACH in ${LLVM_ENABLED_PROJECTS[@]}; do | |
if [[ $LLVM_ENABLE_PROJECTS == "" ]]; then | |
LLVM_ENABLE_PROJECTS=$EACH; | |
else | |
LLVM_ENABLE_PROJECTS="$LLVM_ENABLE_PROJECTS;$EACH"; | |
fi | |
done | |
LLVM_ENABLE_RUNTIMES="" | |
if [[ "$LLVM_ENABLED_RUNTIMES" == "" ]]; then | |
echo "No LLVM runtimes enabled. Use default instead." | |
LLVM_ENABLED_RUNTIMES+=${DEFAULT_LLVM_ENABLED_RUNTIMES[@]}; | |
fi | |
for EACH in ${LLVM_ENABLED_RUNTIMES[@]}; do | |
if [[ $LLVM_ENABLE_RUNTIMES == "" ]]; then | |
LLVM_ENABLE_RUNTIMES=$EACH; | |
else | |
LLVM_ENABLE_RUNTIMES="$LLVM_ENABLE_RUNTIMES;$EACH"; | |
fi | |
done | |
SOURCE_DIR=$PARENT_DIR/llvm-project; | |
if [[ ! -d $SOURCE_DIR ]]; then | |
echo "Directory llvm-project not found. Are you putting this script in the same directory that llvm-project located at?"; | |
exit 1; | |
fi | |
BUILD_DIR=$PARENT_DIR/build/$GENERATOR-$CONFIG_SUFFIX/$PLATFORM-$ARCH/; | |
mkdir -p $BUILD_DIR; | |
pushd $BUILD_DIR; | |
cmake -G $GENERATOR -DCMAKE_BUILD_TYPE=$CONFIG \ | |
-DLLVM_ENABLE_ASSERTIONS=$ASSERT \ | |
-DLLVM_ENABLE_PROJECTS=$LLVM_ENABLE_PROJECTS \ | |
-DLLVM_ENABLE_RUNTIMES=$LLVM_ENABLE_RUNTIMES \ | |
-DLLVM_TARGETS_TO_BUILD=$SUPPORTED_TARGETS \ | |
$SOURCE_DIR/llvm | |
popd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment