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 |
Diana Picus | de3457e | 2016-06-23 14:44:19 +0300 | [diff] [blame] | 38 | if [ -d "$dir" -a -f "$dir/llvm/.git" ]; then |
Diana Picus | db95ab2 | 2016-06-23 14:44:19 +0300 | [diff] [blame] | 39 | pushdq $dir/llvm |
| 40 | branch=`get_branch` |
| 41 | popdq |
| 42 | |
Diana Picus | de3457e | 2016-06-23 14:44:19 +0300 | [diff] [blame] | 43 | if [ "$current_worktree" = "$LLVM_ROOT/$dir/llvm" ]; then |
| 44 | echo "[ $dir ($branch) ]" |
Diana Picus | db95ab2 | 2016-06-23 14:44:19 +0300 | [diff] [blame] | 45 | else |
Diana Picus | de3457e | 2016-06-23 14:44:19 +0300 | [diff] [blame] | 46 | echo "$dir ($branch)" |
Diana Picus | db95ab2 | 2016-06-23 14:44:19 +0300 | [diff] [blame] | 47 | fi |
| 48 | fi |
| 49 | done |
| 50 | |
| 51 | popdq |
Diana Picus | de3457e | 2016-06-23 14:44:19 +0300 | [diff] [blame] | 52 | |
Diana Picus | db95ab2 | 2016-06-23 14:44:19 +0300 | [diff] [blame] | 53 | } |
| 54 | |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 55 | if [ "$1" = "" ]; then |
| 56 | if [ -z "$LLVM_SRC" ]; then |
Diana Picus | db95ab2 | 2016-06-23 14:44:19 +0300 | [diff] [blame] | 57 | list_worktrees |
| 58 | echo "You haven't set up an env (LLVM_SRC is empty)" |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 59 | eval $clean_exit |
| 60 | fi |
| 61 | |
| 62 | worktree=`basename $(readlink -m $LLVM_SRC/../)` |
Diana Picus | db95ab2 | 2016-06-23 14:44:19 +0300 | [diff] [blame] | 63 | |
| 64 | list_worktrees $LLVM_SRC |
| 65 | |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 66 | eval $clean_exit |
| 67 | elif [ "$1" = "-h" ]; then |
| 68 | echo "Usage: $0 <branch> [-cleanup] [-d]" |
| 69 | echo " <branch> : the name of the branch you want to set the env for;" |
| 70 | echo " it will be created if it does not already exist." |
| 71 | echo " -cleanup : will clean up the environment corresponding to <branch>;" |
| 72 | echo " this includes the worktrees for all the subprojects, as" |
| 73 | echo " well as any directories in that environment (especially the" |
| 74 | echo " build dirs!)" |
| 75 | echo " it will not clean up any branches (you should use" |
| 76 | echo " llvm-branch for that)" |
| 77 | echo " -d : build the debug version." |
Renato Golin | db4c6a1 | 2016-08-01 17:03:26 +0100 | [diff] [blame] | 78 | echo " -s : build the self-hosted version (CC is set to <branch>/build)." |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 79 | eval $clean_exit |
| 80 | fi |
| 81 | |
| 82 | branch=$1 |
| 83 | shift |
| 84 | |
| 85 | if [[ $branch = -* ]]; then |
| 86 | echo "Invalid branch name $branch" |
| 87 | eval $clean_exit |
| 88 | fi |
| 89 | |
| 90 | if [ "$branch" = "master" ]; then |
| 91 | echo "Building master directly is not supported." |
| 92 | echo "You can instead run 'llvm-env TrackMaster'" |
| 93 | echo "to create an environment that tracks master." |
| 94 | eval $clean_exit |
| 95 | fi |
| 96 | |
| 97 | clean=false |
| 98 | if [ "$1" = "-cleanup" ]; then |
| 99 | clean=true |
| 100 | shift |
| 101 | fi |
| 102 | |
| 103 | debug_build=false |
Renato Golin | db4c6a1 | 2016-08-01 17:03:26 +0100 | [diff] [blame] | 104 | selfhost_build=false |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 105 | OPTIND=0 |
Renato Golin | db4c6a1 | 2016-08-01 17:03:26 +0100 | [diff] [blame] | 106 | while getopts "ds" opt; do |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 107 | if [ "$opt" = "d" ]; then |
| 108 | debug_build=true |
Renato Golin | db4c6a1 | 2016-08-01 17:03:26 +0100 | [diff] [blame] | 109 | elif [ "$opt" = "s" ]; then |
| 110 | selfhost_build=true |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 111 | else |
| 112 | eval $clean_exit |
| 113 | fi |
| 114 | shift |
| 115 | done |
| 116 | |
| 117 | if [ "$1" != "" ]; then |
| 118 | echo "Too many args?" |
| 119 | eval $clean_exit |
| 120 | fi |
| 121 | |
| 122 | llvm_dir=$LLVM_ROOT/repos/llvm |
| 123 | llvm_worktree_dir=$LLVM_ROOT/$branch/llvm |
| 124 | |
| 125 | llvm_build_dir=$LLVM_ROOT/$branch/build |
| 126 | if $debug_build; then |
| 127 | llvm_build_dir=$LLVM_ROOT/$branch/debug |
Renato Golin | db4c6a1 | 2016-08-01 17:03:26 +0100 | [diff] [blame] | 128 | elif $selfhost_build; then |
| 129 | if [ ! -d "$llvm_build_dir" ] || \ |
| 130 | [ ! -x "$llvm_build_dir/bin/clang" ] || \ |
| 131 | [ ! -x "$llvm_build_dir/bin/clang++" ]; then |
| 132 | echo "Stage 1 build dir not found at: $llvm_build_dir" |
| 133 | echo "Build stage 1 first, then the self-hosted" |
| 134 | eval $clean_exit |
| 135 | fi |
| 136 | llvm_stage1_dir=$llvm_build_dir |
| 137 | llvm_build_dir=$LLVM_ROOT/$branch/selfhost |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 138 | fi |
| 139 | |
Diana Picus | b2a8786 | 2016-07-20 17:14:00 +0300 | [diff] [blame] | 140 | llvm_install_dir=$LLVM_ROOT/$branch/install |
| 141 | |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 142 | if [ "$clean" = true ]; then |
| 143 | .llvm-env-remove $llvm_dir $llvm_worktree_dir |
| 144 | else |
| 145 | .llvm-env-add $branch $llvm_dir $llvm_worktree_dir |
| 146 | fi |
| 147 | |
| 148 | # Changes to the environment should be confined to the end of the script, to |
| 149 | # make sure we don't change half the environment and then fail out |
| 150 | |
| 151 | if [ "$clean" = true ]; then |
| 152 | # Clean up the environment to make sure the other scripts error out |
| 153 | # accordingly instead of trying anything stupid |
| 154 | export LLVM_SRC= |
| 155 | export LLVM_BLD= |
| 156 | export LLVM_DEBUG= |
| 157 | if [ ! -z "$LLVM_OLD_PATH" ]; then |
| 158 | export PATH=$LLVM_OLD_PATH |
| 159 | fi |
| 160 | eval $clean_exit |
| 161 | fi |
| 162 | |
| 163 | export LLVM_SRC=$llvm_worktree_dir |
| 164 | export LLVM_BLD=$llvm_build_dir |
Diana Picus | b2a8786 | 2016-07-20 17:14:00 +0300 | [diff] [blame] | 165 | export LLVM_INSTALL=$llvm_install_dir |
Renato Golin | db4c6a1 | 2016-08-01 17:03:26 +0100 | [diff] [blame] | 166 | # For llvm-build to know |
| 167 | export LLVM_DEBUG=$debug_build |
| 168 | export LLVM_SELFHOST=$selfhost_build |
| 169 | |
| 170 | # Self-hosted builds need to use the previously built clang, maybe LLD. |
| 171 | if $selfhost_build; then |
| 172 | # Clang will automatically pick LLD if available on the same dir |
| 173 | LLVM_CMAKE_FLAGS="-DCMAKE_C_COMPILER=$llvm_stage1_dir/bin/clang \ |
| 174 | -DCMAKE_CXX_COMPILER=$llvm_stage1_dir/bin/clang++" |
| 175 | export LLVM_CMAKE_FLAGS |
| 176 | fi |
Diana Picus | 72189fd | 2016-05-23 19:32:46 +0300 | [diff] [blame] | 177 | |
| 178 | # Make it possible to undo changes to the PATH: we export an LLVM_OLD_PATH, and |
| 179 | # instead of appending the binary dir to $PATH, we append to $LLVM_OLD_PATH (if |
| 180 | # it exists) |
| 181 | # This is intended to support scenarios where you want to switch between a |
| 182 | # release and debug build of the same branch in the same shell, without growing |
| 183 | # a huge PATH |
| 184 | path_to_add_to=$PATH |
| 185 | if [ ! -z "$LLVM_OLD_PATH" ]; then |
| 186 | path_to_add_to=$LLVM_OLD_PATH |
| 187 | fi |
| 188 | |
| 189 | export LLVM_OLD_PATH=$path_to_add_to |
| 190 | export PATH=$llvm_build_dir/bin:$path_to_add_to |
| 191 | |
| 192 | eval $clean_exit |