blob: ba9c98f8d27a1014d39f04ed988112d713f20e24 [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."
Renato Golindb4c6a12016-08-01 17:03:26 +010078 echo " -s : build the self-hosted version (CC is set to <branch>/build)."
Diana Picus72189fd2016-05-23 19:32:46 +030079 eval $clean_exit
80fi
81
82branch=$1
83shift
84
85if [[ $branch = -* ]]; then
86 echo "Invalid branch name $branch"
87 eval $clean_exit
88fi
89
90if [ "$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
95fi
96
97clean=false
98if [ "$1" = "-cleanup" ]; then
99 clean=true
100 shift
101fi
102
103debug_build=false
Renato Golindb4c6a12016-08-01 17:03:26 +0100104selfhost_build=false
Diana Picus72189fd2016-05-23 19:32:46 +0300105OPTIND=0
Renato Golindb4c6a12016-08-01 17:03:26 +0100106while getopts "ds" opt; do
Diana Picus72189fd2016-05-23 19:32:46 +0300107 if [ "$opt" = "d" ]; then
108 debug_build=true
Renato Golindb4c6a12016-08-01 17:03:26 +0100109 elif [ "$opt" = "s" ]; then
110 selfhost_build=true
Diana Picus72189fd2016-05-23 19:32:46 +0300111 else
112 eval $clean_exit
113 fi
114 shift
115done
116
117if [ "$1" != "" ]; then
118 echo "Too many args?"
119 eval $clean_exit
120fi
121
122llvm_dir=$LLVM_ROOT/repos/llvm
123llvm_worktree_dir=$LLVM_ROOT/$branch/llvm
124
125llvm_build_dir=$LLVM_ROOT/$branch/build
126if $debug_build; then
127 llvm_build_dir=$LLVM_ROOT/$branch/debug
Renato Golindb4c6a12016-08-01 17:03:26 +0100128elif $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 Picus72189fd2016-05-23 19:32:46 +0300138fi
139
Diana Picusb2a87862016-07-20 17:14:00 +0300140llvm_install_dir=$LLVM_ROOT/$branch/install
141
Diana Picus72189fd2016-05-23 19:32:46 +0300142if [ "$clean" = true ]; then
143 .llvm-env-remove $llvm_dir $llvm_worktree_dir
144else
145 .llvm-env-add $branch $llvm_dir $llvm_worktree_dir
146fi
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
151if [ "$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
161fi
162
163export LLVM_SRC=$llvm_worktree_dir
164export LLVM_BLD=$llvm_build_dir
Diana Picusb2a87862016-07-20 17:14:00 +0300165export LLVM_INSTALL=$llvm_install_dir
Renato Golindb4c6a12016-08-01 17:03:26 +0100166# For llvm-build to know
167export LLVM_DEBUG=$debug_build
168export LLVM_SELFHOST=$selfhost_build
169
170# Self-hosted builds need to use the previously built clang, maybe LLD.
171if $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
176fi
Diana Picus72189fd2016-05-23 19:32:46 +0300177
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
184path_to_add_to=$PATH
185if [ ! -z "$LLVM_OLD_PATH" ]; then
186 path_to_add_to=$LLVM_OLD_PATH
187fi
188
189export LLVM_OLD_PATH=$path_to_add_to
190export PATH=$llvm_build_dir/bin:$path_to_add_to
191
192eval $clean_exit