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 | |
Diana Picus | db95ab2 | 2016-06-23 14:44:19 +0300 | [diff] [blame^] | 23 | list_worktrees() { |
| 24 | current_worktree=$1 |
| 25 | |
| 26 | # FIXME: |
| 27 | # Ideally we would just use git for this (I think git 2.7-ish should suffice), |
| 28 | # but I'm currently stuck with git 2.5 on my Ubuntu and it seems to me the |
| 29 | # rest of the team is on older gits too. Until we all move to a smarter git, |
| 30 | # we're going to use the simplest possible implementation that can give us |
| 31 | # what we want (the proper way to do this ofc would be to inspect |
| 32 | # $GITDIR/worktrees/). This implementation has the disadvantage that it may |
| 33 | # give false positives or false negatives if you do naughty things with your |
| 34 | # $LLVM_ROOT. |
| 35 | pushdq $LLVM_ROOT |
| 36 | |
| 37 | for dir in *; do |
| 38 | if [ -d "$dir" -a "$dir" != "repos" -a -d "$dir/llvm" ]; then |
| 39 | pushdq $dir/llvm |
| 40 | branch=`get_branch` |
| 41 | popdq |
| 42 | |
| 43 | if [ "$current_worktree" = $LLVM_ROOT/$dir/llvm ]; then |
| 44 | echo "[ Worktree: $dir ($branch) ]" |
| 45 | else |
| 46 | echo "Wortkree: $dir ($branch)" |
| 47 | fi |
| 48 | fi |
| 49 | done |
| 50 | |
| 51 | popdq |
| 52 | } |
| 53 | |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 54 | if [ "$1" = "" ]; then |
| 55 | if [ -z "$LLVM_SRC" ]; then |
Diana Picus | db95ab2 | 2016-06-23 14:44:19 +0300 | [diff] [blame^] | 56 | list_worktrees |
| 57 | echo "You haven't set up an env (LLVM_SRC is empty)" |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 58 | eval $clean_exit |
| 59 | fi |
| 60 | |
| 61 | worktree=`basename $(readlink -m $LLVM_SRC/../)` |
Diana Picus | db95ab2 | 2016-06-23 14:44:19 +0300 | [diff] [blame^] | 62 | |
| 63 | list_worktrees $LLVM_SRC |
| 64 | |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 65 | eval $clean_exit |
| 66 | elif [ "$1" = "-h" ]; then |
| 67 | echo "Usage: $0 <branch> [-cleanup] [-d]" |
| 68 | echo " <branch> : the name of the branch you want to set the env for;" |
| 69 | echo " it will be created if it does not already exist." |
| 70 | echo " -cleanup : will clean up the environment corresponding to <branch>;" |
| 71 | echo " this includes the worktrees for all the subprojects, as" |
| 72 | echo " well as any directories in that environment (especially the" |
| 73 | echo " build dirs!)" |
| 74 | echo " it will not clean up any branches (you should use" |
| 75 | echo " llvm-branch for that)" |
| 76 | echo " -d : build the debug version." |
| 77 | eval $clean_exit |
| 78 | fi |
| 79 | |
| 80 | branch=$1 |
| 81 | shift |
| 82 | |
| 83 | if [[ $branch = -* ]]; then |
| 84 | echo "Invalid branch name $branch" |
| 85 | eval $clean_exit |
| 86 | fi |
| 87 | |
| 88 | if [ "$branch" = "master" ]; then |
| 89 | echo "Building master directly is not supported." |
| 90 | echo "You can instead run 'llvm-env TrackMaster'" |
| 91 | echo "to create an environment that tracks master." |
| 92 | eval $clean_exit |
| 93 | fi |
| 94 | |
| 95 | clean=false |
| 96 | if [ "$1" = "-cleanup" ]; then |
| 97 | clean=true |
| 98 | shift |
| 99 | fi |
| 100 | |
| 101 | debug_build=false |
| 102 | OPTIND=0 |
| 103 | while getopts "d" opt; do |
| 104 | if [ "$opt" = "d" ]; then |
| 105 | debug_build=true |
| 106 | else |
| 107 | eval $clean_exit |
| 108 | fi |
| 109 | shift |
| 110 | done |
| 111 | |
| 112 | if [ "$1" != "" ]; then |
| 113 | echo "Too many args?" |
| 114 | eval $clean_exit |
| 115 | fi |
| 116 | |
| 117 | llvm_dir=$LLVM_ROOT/repos/llvm |
| 118 | llvm_worktree_dir=$LLVM_ROOT/$branch/llvm |
| 119 | |
| 120 | llvm_build_dir=$LLVM_ROOT/$branch/build |
| 121 | if $debug_build; then |
| 122 | llvm_build_dir=$LLVM_ROOT/$branch/debug |
| 123 | fi |
| 124 | |
| 125 | if [ "$clean" = true ]; then |
| 126 | .llvm-env-remove $llvm_dir $llvm_worktree_dir |
| 127 | else |
| 128 | .llvm-env-add $branch $llvm_dir $llvm_worktree_dir |
| 129 | fi |
| 130 | |
| 131 | # Changes to the environment should be confined to the end of the script, to |
| 132 | # make sure we don't change half the environment and then fail out |
| 133 | |
| 134 | if [ "$clean" = true ]; then |
| 135 | # Clean up the environment to make sure the other scripts error out |
| 136 | # accordingly instead of trying anything stupid |
| 137 | export LLVM_SRC= |
| 138 | export LLVM_BLD= |
| 139 | export LLVM_DEBUG= |
| 140 | if [ ! -z "$LLVM_OLD_PATH" ]; then |
| 141 | export PATH=$LLVM_OLD_PATH |
| 142 | fi |
| 143 | eval $clean_exit |
| 144 | fi |
| 145 | |
| 146 | export LLVM_SRC=$llvm_worktree_dir |
| 147 | export LLVM_BLD=$llvm_build_dir |
| 148 | export LLVM_DEBUG=$debug_build # For llvm-build to know |
| 149 | |
| 150 | # Make it possible to undo changes to the PATH: we export an LLVM_OLD_PATH, and |
| 151 | # instead of appending the binary dir to $PATH, we append to $LLVM_OLD_PATH (if |
| 152 | # it exists) |
| 153 | # This is intended to support scenarios where you want to switch between a |
| 154 | # release and debug build of the same branch in the same shell, without growing |
| 155 | # a huge PATH |
| 156 | path_to_add_to=$PATH |
| 157 | if [ ! -z "$LLVM_OLD_PATH" ]; then |
| 158 | path_to_add_to=$LLVM_OLD_PATH |
| 159 | fi |
| 160 | |
| 161 | export LLVM_OLD_PATH=$path_to_add_to |
| 162 | export PATH=$llvm_build_dir/bin:$path_to_add_to |
| 163 | |
| 164 | eval $clean_exit |