blob: 50c4c81ee4bcd02e956758770b981b6602231b76 [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
Diana Picusde3457e2016-06-23 14:44:19 +030038 if [ -d "$dir" -a -f "$dir/llvm/.git" ]; then
Diana Picusdb95ab22016-06-23 14:44:19 +030039 pushdq $dir/llvm
40 branch=`get_branch`
41 popdq
42
Diana Picusde3457e2016-06-23 14:44:19 +030043 if [ "$current_worktree" = "$LLVM_ROOT/$dir/llvm" ]; then
44 echo "[ $dir ($branch) ]"
Diana Picusdb95ab22016-06-23 14:44:19 +030045 else
Diana Picusde3457e2016-06-23 14:44:19 +030046 echo "$dir ($branch)"
Diana Picusdb95ab22016-06-23 14:44:19 +030047 fi
48 fi
49 done
50
51 popdq
Diana Picusde3457e2016-06-23 14:44:19 +030052
Diana Picusdb95ab22016-06-23 14:44:19 +030053}
54
Diana Picus72189fd2016-05-23 19:32:46 +030055if [ "$1" = "" ]; then
56 if [ -z "$LLVM_SRC" ]; then
Diana Picusdb95ab22016-06-23 14:44:19 +030057 list_worktrees
58 echo "You haven't set up an env (LLVM_SRC is empty)"
Diana Picus72189fd2016-05-23 19:32:46 +030059 eval $clean_exit
60 fi
61
62 worktree=`basename $(readlink -m $LLVM_SRC/../)`
Diana Picusdb95ab22016-06-23 14:44:19 +030063
64 list_worktrees $LLVM_SRC
65
Diana Picus72189fd2016-05-23 19:32:46 +030066 eval $clean_exit
67elif [ "$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."
78 eval $clean_exit
79fi
80
81branch=$1
82shift
83
84if [[ $branch = -* ]]; then
85 echo "Invalid branch name $branch"
86 eval $clean_exit
87fi
88
89if [ "$branch" = "master" ]; then
90 echo "Building master directly is not supported."
91 echo "You can instead run 'llvm-env TrackMaster'"
92 echo "to create an environment that tracks master."
93 eval $clean_exit
94fi
95
96clean=false
97if [ "$1" = "-cleanup" ]; then
98 clean=true
99 shift
100fi
101
102debug_build=false
103OPTIND=0
104while getopts "d" opt; do
105 if [ "$opt" = "d" ]; then
106 debug_build=true
107 else
108 eval $clean_exit
109 fi
110 shift
111done
112
113if [ "$1" != "" ]; then
114 echo "Too many args?"
115 eval $clean_exit
116fi
117
118llvm_dir=$LLVM_ROOT/repos/llvm
119llvm_worktree_dir=$LLVM_ROOT/$branch/llvm
120
121llvm_build_dir=$LLVM_ROOT/$branch/build
122if $debug_build; then
123 llvm_build_dir=$LLVM_ROOT/$branch/debug
124fi
125
Diana Picusb2a87862016-07-20 17:14:00 +0300126llvm_install_dir=$LLVM_ROOT/$branch/install
127
Diana Picus72189fd2016-05-23 19:32:46 +0300128if [ "$clean" = true ]; then
129 .llvm-env-remove $llvm_dir $llvm_worktree_dir
130else
131 .llvm-env-add $branch $llvm_dir $llvm_worktree_dir
132fi
133
134# Changes to the environment should be confined to the end of the script, to
135# make sure we don't change half the environment and then fail out
136
137if [ "$clean" = true ]; then
138 # Clean up the environment to make sure the other scripts error out
139 # accordingly instead of trying anything stupid
140 export LLVM_SRC=
141 export LLVM_BLD=
142 export LLVM_DEBUG=
143 if [ ! -z "$LLVM_OLD_PATH" ]; then
144 export PATH=$LLVM_OLD_PATH
145 fi
146 eval $clean_exit
147fi
148
149export LLVM_SRC=$llvm_worktree_dir
150export LLVM_BLD=$llvm_build_dir
Diana Picusb2a87862016-07-20 17:14:00 +0300151export LLVM_INSTALL=$llvm_install_dir
Diana Picus72189fd2016-05-23 19:32:46 +0300152export LLVM_DEBUG=$debug_build # For llvm-build to know
153
154# Make it possible to undo changes to the PATH: we export an LLVM_OLD_PATH, and
155# instead of appending the binary dir to $PATH, we append to $LLVM_OLD_PATH (if
156# it exists)
157# This is intended to support scenarios where you want to switch between a
158# release and debug build of the same branch in the same shell, without growing
159# a huge PATH
160path_to_add_to=$PATH
161if [ ! -z "$LLVM_OLD_PATH" ]; then
162 path_to_add_to=$LLVM_OLD_PATH
163fi
164
165export LLVM_OLD_PATH=$path_to_add_to
166export PATH=$llvm_build_dir/bin:$path_to_add_to
167
168eval $clean_exit