blob: c45fb2e9da5f30970fce4abe521ba74b26d37e93 [file] [log] [blame]
Diana Picus72189fd2016-05-23 19:32:46 +03001#!/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).
11clean_exit="trap '' ERR; return"
12
13trap "$clean_exit" ERR
14
15. llvm-common
16
17# Check that we have actually been sourced
18if [ -z "$PS1" ]; then
19 echo "This script must be sourced. You might want to create an alias llvm-env=\". llvm-env\""
20 exit 1
21fi
22
Diana Picusdb95ab22016-06-23 14:44:19 +030023list_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 Picus72189fd2016-05-23 19:32:46 +030054if [ "$1" = "" ]; then
55 if [ -z "$LLVM_SRC" ]; then
Diana Picusdb95ab22016-06-23 14:44:19 +030056 list_worktrees
57 echo "You haven't set up an env (LLVM_SRC is empty)"
Diana Picus72189fd2016-05-23 19:32:46 +030058 eval $clean_exit
59 fi
60
61 worktree=`basename $(readlink -m $LLVM_SRC/../)`
Diana Picusdb95ab22016-06-23 14:44:19 +030062
63 list_worktrees $LLVM_SRC
64
Diana Picus72189fd2016-05-23 19:32:46 +030065 eval $clean_exit
66elif [ "$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
78fi
79
80branch=$1
81shift
82
83if [[ $branch = -* ]]; then
84 echo "Invalid branch name $branch"
85 eval $clean_exit
86fi
87
88if [ "$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
93fi
94
95clean=false
96if [ "$1" = "-cleanup" ]; then
97 clean=true
98 shift
99fi
100
101debug_build=false
102OPTIND=0
103while getopts "d" opt; do
104 if [ "$opt" = "d" ]; then
105 debug_build=true
106 else
107 eval $clean_exit
108 fi
109 shift
110done
111
112if [ "$1" != "" ]; then
113 echo "Too many args?"
114 eval $clean_exit
115fi
116
117llvm_dir=$LLVM_ROOT/repos/llvm
118llvm_worktree_dir=$LLVM_ROOT/$branch/llvm
119
120llvm_build_dir=$LLVM_ROOT/$branch/build
121if $debug_build; then
122 llvm_build_dir=$LLVM_ROOT/$branch/debug
123fi
124
125if [ "$clean" = true ]; then
126 .llvm-env-remove $llvm_dir $llvm_worktree_dir
127else
128 .llvm-env-add $branch $llvm_dir $llvm_worktree_dir
129fi
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
134if [ "$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
144fi
145
146export LLVM_SRC=$llvm_worktree_dir
147export LLVM_BLD=$llvm_build_dir
148export 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
156path_to_add_to=$PATH
157if [ ! -z "$LLVM_OLD_PATH" ]; then
158 path_to_add_to=$LLVM_OLD_PATH
159fi
160
161export LLVM_OLD_PATH=$path_to_add_to
162export PATH=$llvm_build_dir/bin:$path_to_add_to
163
164eval $clean_exit