blob: d90c76739b582c30050bffaeba8d986895af49bf [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
23if [ "$1" = "" ]; then
24 if [ -z "$LLVM_SRC" ]; then
25 echo "You haven't set up a worktree"
26 eval $clean_exit
27 fi
28
29 worktree=`basename $(readlink -m $LLVM_SRC/../)`
30 echo $worktree
31 eval $clean_exit
32elif [ "$1" = "-h" ]; then
33 echo "Usage: $0 <branch> [-cleanup] [-d]"
34 echo " <branch> : the name of the branch you want to set the env for;"
35 echo " it will be created if it does not already exist."
36 echo " -cleanup : will clean up the environment corresponding to <branch>;"
37 echo " this includes the worktrees for all the subprojects, as"
38 echo " well as any directories in that environment (especially the"
39 echo " build dirs!)"
40 echo " it will not clean up any branches (you should use"
41 echo " llvm-branch for that)"
42 echo " -d : build the debug version."
43 eval $clean_exit
44fi
45
46branch=$1
47shift
48
49if [[ $branch = -* ]]; then
50 echo "Invalid branch name $branch"
51 eval $clean_exit
52fi
53
54if [ "$branch" = "master" ]; then
55 echo "Building master directly is not supported."
56 echo "You can instead run 'llvm-env TrackMaster'"
57 echo "to create an environment that tracks master."
58 eval $clean_exit
59fi
60
61clean=false
62if [ "$1" = "-cleanup" ]; then
63 clean=true
64 shift
65fi
66
67debug_build=false
68OPTIND=0
69while getopts "d" opt; do
70 if [ "$opt" = "d" ]; then
71 debug_build=true
72 else
73 eval $clean_exit
74 fi
75 shift
76done
77
78if [ "$1" != "" ]; then
79 echo "Too many args?"
80 eval $clean_exit
81fi
82
83llvm_dir=$LLVM_ROOT/repos/llvm
84llvm_worktree_dir=$LLVM_ROOT/$branch/llvm
85
86llvm_build_dir=$LLVM_ROOT/$branch/build
87if $debug_build; then
88 llvm_build_dir=$LLVM_ROOT/$branch/debug
89fi
90
91if [ "$clean" = true ]; then
92 .llvm-env-remove $llvm_dir $llvm_worktree_dir
93else
94 .llvm-env-add $branch $llvm_dir $llvm_worktree_dir
95fi
96
97# Changes to the environment should be confined to the end of the script, to
98# make sure we don't change half the environment and then fail out
99
100if [ "$clean" = true ]; then
101 # Clean up the environment to make sure the other scripts error out
102 # accordingly instead of trying anything stupid
103 export LLVM_SRC=
104 export LLVM_BLD=
105 export LLVM_DEBUG=
106 if [ ! -z "$LLVM_OLD_PATH" ]; then
107 export PATH=$LLVM_OLD_PATH
108 fi
109 eval $clean_exit
110fi
111
112export LLVM_SRC=$llvm_worktree_dir
113export LLVM_BLD=$llvm_build_dir
114export LLVM_DEBUG=$debug_build # For llvm-build to know
115
116# Make it possible to undo changes to the PATH: we export an LLVM_OLD_PATH, and
117# instead of appending the binary dir to $PATH, we append to $LLVM_OLD_PATH (if
118# it exists)
119# This is intended to support scenarios where you want to switch between a
120# release and debug build of the same branch in the same shell, without growing
121# a huge PATH
122path_to_add_to=$PATH
123if [ ! -z "$LLVM_OLD_PATH" ]; then
124 path_to_add_to=$LLVM_OLD_PATH
125fi
126
127export LLVM_OLD_PATH=$path_to_add_to
128export PATH=$llvm_build_dir/bin:$path_to_add_to
129
130eval $clean_exit