daml/dev-env/lib/dade-common
Stefano Baghino d999a21ac3
Replace cd .. && pwd with readlink -f (#17152)
The output of `cd` might be altered by `CDPATH` set on a
system, leading to `dade` being broken because it cannot
read the current path. Instead, using `readlink -f` seem
to get the job done without side effects.
2023-07-19 12:54:33 +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="$( dirname $( readlink -f "${BASH_SOURCE[0]}" ) )"
DADE_BASE_ROOT="$( readlink -f "${DADE_CURRENT_SCRIPT_DIR}/../../" )"
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 --no-out-link $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
}