diff options
author | Diana Picus <diana.picus@linaro.org> | 2016-05-23 19:32:46 +0300 |
---|---|---|
committer | Diana Picus <diana.picus@linaro.org> | 2016-06-15 12:27:43 +0300 |
commit | 72189fd702d00d45714f98857a88159097933b5c (patch) | |
tree | 285a34a287a234fcc1e558febb0f036e0aa112ba /helpers/llvm-env | |
parent | 488ec270d3bb2e34837ccf25431d73b5c5d68dc3 (diff) |
Use git worktree in the LLVM helper scripts
This commit adds a new script, llvm-env, which sets up the environment for
working with LLVM. It looks for an environment variable LLVM_ROOT and tries to
create the following hierarchy:
$LLVM_ROOT
`- repos
| `- llvm
| `- clang
| `- compiler-rt
| [...]
`- <branch1>
| `- llvm
| `- build
| `- debug
`- <branch2>
| `- llvm
| `- build
| `- debug
[...]
The $LLVM_ROOT/repos directory contains all the repositories, as checked out by
llvm-prepare, and will always track master. For other branches,
llvm-env <branch_name> will create a new directory, $LLVM_ROOT/<branch_name>,
and will add an llvm worktree directory there. If -d is passed, it will also
create a debug directory there, otherwise it will create a build directory.
Notice that these 2 can live in parallel, and we can switch between them at any
time by invoking llvm-env. It will set LLVM_SRC and LLVM_BLD accordingly, and
also modify the path to point to the binaries in LLVM_BLD.
The other scripts will now work with the LLVM_SRC and LLVM_BLD set by llvm-env
in the current shell. Because llvm-env controls whether or not we're doing a
debug build, llvm-build will no longer take a -d flag (it will instead look
after a LLVM_DEBUG environment variable, also set by llvm-env). There are
changes in llvm-projs, too, because now it no longer creates links - instead it
creates worktree directories in the corresponding $LLVM_ROOT/<branch>/llvm.
Other scripts have also been updated accordingly.
To make things easier, here are some of the changes that I had to make that are
not particularly important for the review (pretty mechanical stuff):
* Moved function has() from llvm-branch to llvm-common, so I could reuse it
* Because of this, I had to rename the has() function in llvm-projs to
has_link(), which is actually a better name for it anyway
* Disable the checks for LLVM_SRC and LLVM_BLD in llvm-common
Change-Id: I9e02f6d8e0c803e79838845013b81331dffba99c
Diffstat (limited to 'helpers/llvm-env')
-rwxr-xr-x | helpers/llvm-env | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/helpers/llvm-env b/helpers/llvm-env new file mode 100755 index 0000000..d90c767 --- /dev/null +++ b/helpers/llvm-env @@ -0,0 +1,130 @@ +#!/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 + +if [ "$1" = "" ]; then + if [ -z "$LLVM_SRC" ]; then + echo "You haven't set up a worktree" + eval $clean_exit + fi + + worktree=`basename $(readlink -m $LLVM_SRC/../)` + echo $worktree + 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 |