Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame^] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # This script manages the environment needed by the helper scripts |
| 4 | # It needs to have one environment variable set in order to do its job: |
| 5 | # LLVM_ROOT - the directory under which the llvm sources and build directories |
| 6 | # will live |
| 7 | |
| 8 | # This script is meant to be sourced, so we don't want things to exit on error, |
| 9 | # because that would kill the whole shell. Instead, we trap and return from the |
| 10 | # script (which leaves the shell running, so people can see what went wrong). |
| 11 | clean_exit="trap '' ERR; return" |
| 12 | |
| 13 | trap "$clean_exit" ERR |
| 14 | |
| 15 | . llvm-common |
| 16 | |
| 17 | # Check that we have actually been sourced |
| 18 | if [ -z "$PS1" ]; then |
| 19 | echo "This script must be sourced. You might want to create an alias llvm-env=\". llvm-env\"" |
| 20 | exit 1 |
| 21 | fi |
| 22 | |
| 23 | if [ "$1" = "" ]; then |
| 24 | if [ -z "$LLVM_SRC" ]; then |
| 25 | echo "You haven't set up a worktree" |
| 26 | eval $clean_exit |
| 27 | fi |
| 28 | |
| 29 | worktree=`basename $(readlink -m $LLVM_SRC/../)` |
| 30 | echo $worktree |
| 31 | eval $clean_exit |
| 32 | elif [ "$1" = "-h" ]; then |
| 33 | echo "Usage: $0 <branch> [-cleanup] [-d]" |
| 34 | echo " <branch> : the name of the branch you want to set the env for;" |
| 35 | echo " it will be created if it does not already exist." |
| 36 | echo " -cleanup : will clean up the environment corresponding to <branch>;" |
| 37 | echo " this includes the worktrees for all the subprojects, as" |
| 38 | echo " well as any directories in that environment (especially the" |
| 39 | echo " build dirs!)" |
| 40 | echo " it will not clean up any branches (you should use" |
| 41 | echo " llvm-branch for that)" |
| 42 | echo " -d : build the debug version." |
| 43 | eval $clean_exit |
| 44 | fi |
| 45 | |
| 46 | branch=$1 |
| 47 | shift |
| 48 | |
| 49 | if [[ $branch = -* ]]; then |
| 50 | echo "Invalid branch name $branch" |
| 51 | eval $clean_exit |
| 52 | fi |
| 53 | |
| 54 | if [ "$branch" = "master" ]; then |
| 55 | echo "Building master directly is not supported." |
| 56 | echo "You can instead run 'llvm-env TrackMaster'" |
| 57 | echo "to create an environment that tracks master." |
| 58 | eval $clean_exit |
| 59 | fi |
| 60 | |
| 61 | clean=false |
| 62 | if [ "$1" = "-cleanup" ]; then |
| 63 | clean=true |
| 64 | shift |
| 65 | fi |
| 66 | |
| 67 | debug_build=false |
| 68 | OPTIND=0 |
| 69 | while getopts "d" opt; do |
| 70 | if [ "$opt" = "d" ]; then |
| 71 | debug_build=true |
| 72 | else |
| 73 | eval $clean_exit |
| 74 | fi |
| 75 | shift |
| 76 | done |
| 77 | |
| 78 | if [ "$1" != "" ]; then |
| 79 | echo "Too many args?" |
| 80 | eval $clean_exit |
| 81 | fi |
| 82 | |
| 83 | llvm_dir=$LLVM_ROOT/repos/llvm |
| 84 | llvm_worktree_dir=$LLVM_ROOT/$branch/llvm |
| 85 | |
| 86 | llvm_build_dir=$LLVM_ROOT/$branch/build |
| 87 | if $debug_build; then |
| 88 | llvm_build_dir=$LLVM_ROOT/$branch/debug |
| 89 | fi |
| 90 | |
| 91 | if [ "$clean" = true ]; then |
| 92 | .llvm-env-remove $llvm_dir $llvm_worktree_dir |
| 93 | else |
| 94 | .llvm-env-add $branch $llvm_dir $llvm_worktree_dir |
| 95 | fi |
| 96 | |
| 97 | # Changes to the environment should be confined to the end of the script, to |
| 98 | # make sure we don't change half the environment and then fail out |
| 99 | |
| 100 | if [ "$clean" = true ]; then |
| 101 | # Clean up the environment to make sure the other scripts error out |
| 102 | # accordingly instead of trying anything stupid |
| 103 | export LLVM_SRC= |
| 104 | export LLVM_BLD= |
| 105 | export LLVM_DEBUG= |
| 106 | if [ ! -z "$LLVM_OLD_PATH" ]; then |
| 107 | export PATH=$LLVM_OLD_PATH |
| 108 | fi |
| 109 | eval $clean_exit |
| 110 | fi |
| 111 | |
| 112 | export LLVM_SRC=$llvm_worktree_dir |
| 113 | export LLVM_BLD=$llvm_build_dir |
| 114 | export LLVM_DEBUG=$debug_build # For llvm-build to know |
| 115 | |
| 116 | # Make it possible to undo changes to the PATH: we export an LLVM_OLD_PATH, and |
| 117 | # instead of appending the binary dir to $PATH, we append to $LLVM_OLD_PATH (if |
| 118 | # it exists) |
| 119 | # This is intended to support scenarios where you want to switch between a |
| 120 | # release and debug build of the same branch in the same shell, without growing |
| 121 | # a huge PATH |
| 122 | path_to_add_to=$PATH |
| 123 | if [ ! -z "$LLVM_OLD_PATH" ]; then |
| 124 | path_to_add_to=$LLVM_OLD_PATH |
| 125 | fi |
| 126 | |
| 127 | export LLVM_OLD_PATH=$path_to_add_to |
| 128 | export PATH=$llvm_build_dir/bin:$path_to_add_to |
| 129 | |
| 130 | eval $clean_exit |