daml/dev-env/lib/dade-common
Samir Talwar 92020aa868
dev-env: Add a symlink, dev-env/jdk, to the current JDK. (#7745)
* dev-env: Add a symlink, `dev-env/jdk`, to the current JDK.

This makes configuring IntelliJ IDEA to use the correct JDK much easier.

CHANGELOG_BEGIN
CHANGELOG_END

* dev-env: Document setting up the JDK in IntelliJ IDEA.
2020-10-20 11:03:23 +00:00

154 lines
4.7 KiB
Bash

# -*- shell-script -*-
# Common development environment definitions
#
# Check if DADE_DEBUG is set. In cases when outside shell has set -u,
# and DADE_DEBUG is not set, then -z "${DADE_DEBUG}" would fail.
# This form ensures that it is not the case
if [[ -n "${DADE_DEBUG+x}" ]]; then
set -x
fi
DADE_CURRENT_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DADE_BASE_ROOT="$( cd "${DADE_CURRENT_SCRIPT_DIR}/../../" && pwd)"
errcho() {
>&2 echo "[dev-env] $@"
}
readAbsolutePath() {
perl -MCwd -e 'print Cwd::abs_path shift' $1
}
# Detect if we are being used within another working tree and bail out.
OLD_PWD="$PWD"
while [ "$PWD" != "/" ]; do
if [ -h "$PWD" ]; then # follow symlink if PWD is one
cd $(readAbsolutePath "$PWD")
fi
if [[ -e dev-env/lib/dade-common && "$PWD" != "$DADE_BASE_ROOT" ]]; then
errcho "Fatal: Using dev-env from $DADE_BASE_ROOT, but running under $PWD."
errcho "Please source the right dev-env profile:"
errcho " bash: 'source $PWD/dev-env/profile_bash.sh'"
errcho " zsh: 'source $PWD/dev-env/profile_zsh.sh'"
exit 1
fi
cd ..
done
cd "$OLD_PWD"
export DADE_REPO_ROOT="${DADE_REPO_ROOT:-$DADE_BASE_ROOT}"
export DADE_DEVENV_DIR="${DADE_BASE_ROOT}/dev-env"
export DADE_LIB_DIR="${DADE_DEVENV_DIR}/lib"
# If specified, use the outer layer, e.g. dev-env bootstrap script in another
# repository, special var director.
export DADE_VAR_DIR="${DADE_VAR_DIR:-$DADE_DEVENV_DIR/var}"
# DADE_GC_ROOTS_ROOT is used by the CI builds on shared nfs
DADE_GC_ROOTS="${DADE_GC_ROOTS_ROOT:-}${DADE_VAR_DIR}/gc-roots"
# Make sure that Nix is available
source "${DADE_CURRENT_SCRIPT_DIR}/ensure-nix"
# Compute expected dade base hash
dadeBaseHash() {
nix-hash "$DADE_DEVENV_DIR/../nix"
}
# List tools defined in dade
dadeListTools() {
cat $(nix-build $DADE_BASE_ROOT/nix -A dade.tools-list)
}
# dadeGetOutput get output of a target
#
# target: Attribute in the $DADE_BASE_ROOT/nix/default.nix tools attrset to
# realize.
# outputName: the output name of the Nix derivation, where the derivation is
# relalized into. This is necessary, since Nix's --add-root produces a
# unpredictable symlink name.
dadeGetOutput() {
local target=$1
local outputName=$2
local output="$target"
if [[ "$outputName" != "out" ]]; then
output="${output}-${outputName}"
fi
echo $output
}
# buildTool <attribute> <outputName> <force>
#
# attribute: Attribute in the $DADE_BASE_ROOT/nix/default.nix tools attrset to
# realize.
# outputName: the output name of the Nix derivation, where the derivation is
# relalized into. This is necessary, since Nix's --add-root produces a
# unpredictable symlink name.
# force: if set to 1, forces re-initialization of the tool.
# target: the output location (defaults to "$DADE_GC_ROOTS/$attribute").
buildTool() {
local attribute=$1
local outputName=$2
local force=$3
shift 3
if [[ $# -gt 0 ]]; then
local target=$1
local customTarget=1
shift 1
else
local target="$DADE_GC_ROOTS/$attribute"
local customTarget=0
fi
local hashfile="${target}.hash"
local hash="nope"
local currentHash="$(dadeBaseHash)"
local forced=""
test -e "$hashfile" && hash="$(cat "$hashfile")"
# Build the tool if no the garbage collector root exists or if the
# hash of the definitions is different.
# We depend on the behavior of nix-build to name a gc root as target-foo, for
# a non-out output.
DADE_BUILD_RESULT="$(dadeGetOutput $target $outputName)"
if [[ ! -e "$DADE_BUILD_RESULT" || "$force" -eq 1 || "$hash" != "$currentHash" ]]; then
set -e
test "$force" -eq 1 && forced=" (forced)"
errcho "Building tools.${attribute}${forced}..."
# Allow to fail, so we can capture outpath and to capture the exit code too.
set +e
outpath=$(nix-build "${DADE_BASE_ROOT}/nix/default.nix" -A "tools.${attribute}" -Q -o "${target}")
local dade_build_exit_code=$?
set -e
if [[ "$dade_build_exit_code" != "0" ]]; then
errcho "Build of tools.$attribute has failed!"
exit 1
fi
errcho "Built tools.$attribute in $outpath and linked to $DADE_BUILD_RESULT"
if [[ $customTarget -eq 0 ]]; then
printf "$currentHash" >| "$hashfile"
fi
set +e
fi
}
# execTool <attribute> <outputName> <binary>
execTool() {
local attribute=$1
local outputName=$2
local binary=$3
shift 3
[[ $PATH =~ (^|.*:)$DADE_DEVENV_DIR/bin($|:.*) ]] || \
eval "$(${DADE_DEVENV_DIR}/bin/dade assist)"
buildTool $attribute $outputName 0
exec "$(readlink "$DADE_BUILD_RESULT")/bin/$binary" "$@"
}
# linkTool <attribute> <outputName> <target>
linkTool() {
local attribute=$1
local outputName=$2
local target=$3
shift 3
buildTool $attribute $outputName 1 $target
}