aboutsummaryrefslogtreecommitdiff
path: root/helpers/llvm-env
diff options
context:
space:
mode:
Diffstat (limited to 'helpers/llvm-env')
-rwxr-xr-xhelpers/llvm-env130
1 files changed, 130 insertions, 0 deletions
diff --git a/helpers/llvm-env b/helpers/llvm-env
new file mode 100755
index 0000000..d90c767
--- /dev/null
+++ b/helpers/llvm-env
@@ -0,0 +1,130 @@
+#!/usr/bin/env bash
+
+# This script manages the environment needed by the helper scripts
+# It needs to have one environment variable set in order to do its job:
+# LLVM_ROOT - the directory under which the llvm sources and build directories
+# will live
+
+# This script is meant to be sourced, so we don't want things to exit on error,
+# because that would kill the whole shell. Instead, we trap and return from the
+# script (which leaves the shell running, so people can see what went wrong).
+clean_exit="trap '' ERR; return"
+
+trap "$clean_exit" ERR
+
+. llvm-common
+
+# Check that we have actually been sourced
+if [ -z "$PS1" ]; then
+ echo "This script must be sourced. You might want to create an alias llvm-env=\". llvm-env\""
+ exit 1
+fi
+
+if [ "$1" = "" ]; then
+ if [ -z "$LLVM_SRC" ]; then
+ echo "You haven't set up a worktree"
+ eval $clean_exit
+ fi
+
+ worktree=`basename $(readlink -m $LLVM_SRC/../)`
+ echo $worktree
+ eval $clean_exit
+elif [ "$1" = "-h" ]; then
+ echo "Usage: $0 <branch> [-cleanup] [-d]"
+ echo " <branch> : the name of the branch you want to set the env for;"
+ echo " it will be created if it does not already exist."
+ echo " -cleanup : will clean up the environment corresponding to <branch>;"
+ echo " this includes the worktrees for all the subprojects, as"
+ echo " well as any directories in that environment (especially the"
+ echo " build dirs!)"
+ echo " it will not clean up any branches (you should use"
+ echo " llvm-branch for that)"
+ echo " -d : build the debug version."
+ eval $clean_exit
+fi
+
+branch=$1
+shift
+
+if [[ $branch = -* ]]; then
+ echo "Invalid branch name $branch"
+ eval $clean_exit
+fi
+
+if [ "$branch" = "master" ]; then
+ echo "Building master directly is not supported."
+ echo "You can instead run 'llvm-env TrackMaster'"
+ echo "to create an environment that tracks master."
+ eval $clean_exit
+fi
+
+clean=false
+if [ "$1" = "-cleanup" ]; then
+ clean=true
+ shift
+fi
+
+debug_build=false
+OPTIND=0
+while getopts "d" opt; do
+ if [ "$opt" = "d" ]; then
+ debug_build=true
+ else
+ eval $clean_exit
+ fi
+ shift
+done
+
+if [ "$1" != "" ]; then
+ echo "Too many args?"
+ eval $clean_exit
+fi
+
+llvm_dir=$LLVM_ROOT/repos/llvm
+llvm_worktree_dir=$LLVM_ROOT/$branch/llvm
+
+llvm_build_dir=$LLVM_ROOT/$branch/build
+if $debug_build; then
+ llvm_build_dir=$LLVM_ROOT/$branch/debug
+fi
+
+if [ "$clean" = true ]; then
+ .llvm-env-remove $llvm_dir $llvm_worktree_dir
+else
+ .llvm-env-add $branch $llvm_dir $llvm_worktree_dir
+fi
+
+# Changes to the environment should be confined to the end of the script, to
+# make sure we don't change half the environment and then fail out
+
+if [ "$clean" = true ]; then
+ # Clean up the environment to make sure the other scripts error out
+ # accordingly instead of trying anything stupid
+ export LLVM_SRC=
+ export LLVM_BLD=
+ export LLVM_DEBUG=
+ if [ ! -z "$LLVM_OLD_PATH" ]; then
+ export PATH=$LLVM_OLD_PATH
+ fi
+ eval $clean_exit
+fi
+
+export LLVM_SRC=$llvm_worktree_dir
+export LLVM_BLD=$llvm_build_dir
+export LLVM_DEBUG=$debug_build # For llvm-build to know
+
+# Make it possible to undo changes to the PATH: we export an LLVM_OLD_PATH, and
+# instead of appending the binary dir to $PATH, we append to $LLVM_OLD_PATH (if
+# it exists)
+# This is intended to support scenarios where you want to switch between a
+# release and debug build of the same branch in the same shell, without growing
+# a huge PATH
+path_to_add_to=$PATH
+if [ ! -z "$LLVM_OLD_PATH" ]; then
+ path_to_add_to=$LLVM_OLD_PATH
+fi
+
+export LLVM_OLD_PATH=$path_to_add_to
+export PATH=$llvm_build_dir/bin:$path_to_add_to
+
+eval $clean_exit