aboutsummaryrefslogtreecommitdiff
path: root/helpers/llvm-env
blob: f293adb83808b69974b9c6750b7740d146ef1c5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/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

list_worktrees() {
  current_worktree=$1

  # FIXME:
  # Ideally we would just use git for this (I think git 2.7-ish should suffice),
  # but I'm currently stuck with git 2.5 on my Ubuntu and it seems to me the
  # rest of the team is on older gits too. Until we all move to a smarter git,
  # we're going to use the simplest possible implementation that can give us
  # what we want (the proper way to do this ofc would be to inspect
  # $GITDIR/worktrees/). This implementation has the disadvantage that it may
  # give false positives or false negatives if you do naughty things with your
  # $LLVM_ROOT.
  pushdq $LLVM_ROOT

  for dir in *; do
    if [ -d "$dir" -a -f "$dir/llvm/.git" ]; then
      pushdq $dir/llvm
      branch=`get_branch`
      popdq

      if [ "$current_worktree" = "$LLVM_ROOT/$dir/llvm" ]; then
        echo "[ $dir ($branch) ]"
      else
        echo "$dir ($branch)"
      fi
    fi
  done

  popdq

}

if [ "$1" = "" ]; then
  if [ -z "$LLVM_SRC" ]; then
    list_worktrees
    echo "You haven't set up an env (LLVM_SRC is empty)"
    eval $clean_exit
  fi

  worktree=`basename $(readlink -m $LLVM_SRC/../)`

  list_worktrees $LLVM_SRC

  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