Skip to content

Commit

Permalink
[nix] fix VCS daidir write permission issue
Browse files Browse the repository at this point in the history
Signed-off-by: Avimitin <dev@avimit.in>
  • Loading branch information
Avimitin authored and unlsycn committed Sep 11, 2024
1 parent a578a38 commit 71315ad
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
42 changes: 42 additions & 0 deletions templates/chisel/nix/gcd/vcs-wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!@shell@

_EXTRA_ARGS="$@"

if ((${VERBOSE:-0})); then
set -x
fi

_DATE_BIN=@dateBin@
_VCS_SIM_BIN=@vcsSimBin@
_VCS_SIM_DAIDIR=@vcsSimDaidir@
_VCS_FHS_ENV=@vcsFhsEnv@

_NOW=$("$_DATE_BIN" "+%Y-%m-%d-%H-%M-%S")
_GCD_SIM_RESULT_DIR=${GCD_SIM_RESULT_DIR:-"gcd-sim-result"}
_CURRENT="$_GCD_SIM_RESULT_DIR"/all/"$_NOW"
mkdir -p "$_CURRENT"
ln -sfn "all/$_NOW" "$_GCD_SIM_RESULT_DIR/result"

cp "$_VCS_SIM_BIN" "$_CURRENT/"
cp -r "$_VCS_SIM_DAIDIR" "$_CURRENT/"

chmod -R +w "$_CURRENT"

pushd "$_CURRENT" >/dev/null

_emu_name=$(basename "$_VCS_SIM_BIN")
_daidir=$(basename "$_VCS_SIM_DAIDIR")

export LD_LIBRARY_PATH="$PWD/$_daidir:$LD_LIBRARY_PATH"

"$_VCS_FHS_ENV" -c "./$_emu_name $_EXTRA_ARGS" &> >(tee vcs-emu-journal.log)

if ((${DATA_ONLY:-0})); then
rm -f "./$_emu_name"
fi

set -e _emu_name _daidir

popd >/dev/null

echo "VCS emulator finished, result saved in $_GCD_SIM_RESULT_DIR/result"
22 changes: 12 additions & 10 deletions templates/chisel/nix/gcd/vcs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ stdenv.mkDerivation (finalAttr: {
inherit rtl;

tests.simple-sim = runCommand "${binName}-test" { __noChroot = true; } ''
# Combine stderr and stdout and redirect them to tee
# So that we can have log saving to output and also printing to stdout
${finalAttr.finalPackage}/bin/${binName} &> >(tee $out)
export GCD_SIM_RESULT_DIR="$(mktemp -d)"
export DATA_ONLY=1
${finalAttr.finalPackage}/bin/${binName}
mkdir -p "$out"
cp -vr "$GCD_SIM_RESULT_DIR"/result/* "$out/"
'';
};

Expand All @@ -76,13 +79,12 @@ stdenv.mkDerivation (finalAttr: {
cp ${binName} $out/lib
cp -r ${binName}.daidir $out/lib
# We need to carefully handle string escape here, so don't use makeWrapper
tee $out/bin/${binName} <<EOF
#!${bash}/bin/bash
export LD_LIBRARY_PATH="$out/lib/${binName}.daidir:\$LD_LIBRARY_PATH"
_argv="\$@"
${vcs-fhs-env}/bin/vcs-fhs-env -c "$out/lib/${binName} \$_argv"
EOF
substitute ${./vcs-wrapper.sh} $out/bin/${binName} \
--subst-var-by shell "${bash}/bin/bash" \
--subst-var-by dateBin "$(command -v date)" \
--subst-var-by vcsSimBin "$out/lib/${binName}" \
--subst-var-by vcsSimDaidir "$out/lib/${binName}.daidir" \
--subst-var-by vcsFhsEnv "${vcs-fhs-env}/bin/vcs-fhs-env"
chmod +x $out/bin/${binName}
runHook postInstall
Expand Down

0 comments on commit 71315ad

Please sign in to comment.