2018-05-20 03:38:02 +08:00
#!/usr/bin/env bash
2022-09-16 02:38:20 +08:00
# FireSim initial setup script.
2020-05-06 14:29:58 +08:00
2018-05-20 03:38:02 +08:00
# exit script if any command fails
set -e
set -o pipefail
unamestr = $( uname)
RDIR = $( pwd )
FASTINSTALL = false
2019-05-28 06:49:44 +08:00
IS_LIBRARY = false
2020-05-06 14:29:58 +08:00
SKIP_TOOLCHAIN = false
2022-02-15 11:38:40 +08:00
SKIP_VALIDATE = false
2022-09-10 02:47:13 +08:00
TOOLCHAIN = riscv-tools
USE_PINNED_DEPS = true
2019-05-28 06:49:44 +08:00
function usage
{
2022-09-16 02:38:20 +08:00
echo "usage: build-setup.sh [OPTIONS]"
2022-09-10 02:47:13 +08:00
echo "options:"
2022-09-14 13:57:27 +08:00
echo " --skip-toolchain-extra: if set, skips building extra RISC-V toolchain collateral i.e Spike,"
2022-09-10 02:47:13 +08:00
echo " PK, RISC-V tests, libgloss and installing it to $RISCV (including cloning or building). "
echo " --library: if set, initializes submodules assuming FireSim is being used"
2022-09-14 13:57:27 +08:00
echo " as a library submodule. Implies --skip-toolchain-extra"
2022-09-10 02:47:13 +08:00
echo " --skip-validate: if set, skips checking if user is on release tagged branch"
echo " --unpinned-deps: if set, use unpinned conda package dependencies"
2019-05-28 06:49:44 +08:00
}
2019-07-02 08:16:16 +08:00
if [ " $1 " = = "--help" -o " $1 " = = "-h" -o " $1 " = = "-H" ] ; then
2019-05-28 06:49:44 +08:00
usage
exit 3
fi
while test $# -gt 0
do
case " $1 " in
--library)
IS_LIBRARY = true;
2020-05-06 14:29:58 +08:00
SKIP_TOOLCHAIN = true;
2019-05-28 06:49:44 +08:00
; ;
2022-09-14 13:57:27 +08:00
--skip-toolchain-extra)
2020-05-06 14:29:58 +08:00
SKIP_TOOLCHAIN = true;
2019-05-28 06:49:44 +08:00
; ;
2022-02-15 11:38:40 +08:00
--skip-validate)
SKIP_VALIDATE = true;
; ;
2022-09-10 02:47:13 +08:00
--unpinned-deps)
USE_PINNED_DEPS = false;
; ;
2019-05-28 06:49:44 +08:00
-h | -H | --help)
usage
exit
; ;
--*) echo " ERROR: bad option $1 "
usage
exit 1
; ;
*) echo " ERROR: bad argument $1 "
usage
exit 2
; ;
esac
shift
done
2022-02-15 11:38:40 +08:00
# before doing anything verify that you are on a release branch/tag
set +e
tag = $( git describe --exact-match --tags)
2022-09-16 02:38:20 +08:00
tag_ret_code = " $? "
2022-02-15 11:38:40 +08:00
set -e
2022-09-16 02:38:20 +08:00
if [ " $tag_ret_code " -ne 0 ] ; then
2022-02-15 11:38:40 +08:00
if [ " $SKIP_VALIDATE " = false ] ; then
2023-04-21 05:47:21 +08:00
printf '\033[2J' # clear screen
2022-02-16 14:14:07 +08:00
read -p "WARNING: You are not on an official release of FireSim." $'\n' "Type \"y\" to continue if this is intended, otherwise see https://docs.fires.im/en/stable/Initial-Setup/Setting-up-your-Manager-Instance.html#setting-up-the-firesim-repo: " validate
2022-09-16 02:38:20 +08:00
[ [ " $validate " = = [ yY] ] ] || exit 5
2022-02-15 11:38:40 +08:00
echo "Setting up non-official FireSim release"
fi
else
echo " Setting up official FireSim release: $tag "
fi
2022-09-10 07:10:21 +08:00
if [ " $IS_LIBRARY " = true ] ; then
if [ -z " $RISCV " ] ; then
echo "ERROR: You must set the RISCV environment variable before running."
exit 4
else
echo " Using existing RISCV toolchain at $RISCV "
fi
2020-05-06 14:29:58 +08:00
fi
# Remove and backup the existing env.sh if it exists
# The existing of env.sh implies this script completely correctly
if [ -f env.sh ] ; then
mv -f env.sh env.sh.backup
fi
2021-11-30 15:19:59 +08:00
# This will be flushed out into a complete env.sh which will be written out
# upon completion.
env_string = " # This file was generated by $0 "
2020-05-06 14:29:58 +08:00
function env_append {
env_string += $( printf " \n $1 " )
}
2021-11-30 15:19:59 +08:00
# Initially, create a env.sh that suggests build.sh did not run correctly.
bad_env = " ${ env_string }
echo \" ERROR: build-setup.sh did not execute correctly or was terminated prematurely.\"
echo \" Please review build-setup-log for more information.\"
return 1"
echo " $bad_env " > env.sh
2022-03-23 01:00:25 +08:00
env_append "export FIRESIM_ENV_SOURCED=1"
2020-05-06 14:29:58 +08:00
2022-09-16 02:38:20 +08:00
# Conda Setup
2022-10-29 02:58:05 +08:00
# Provide a sourceable snippet that can be used in subshells that may not have
# inhereted conda functions that would be brought in under a login shell that
# has run conda init (e.g., VSCode, CI)
read -r -d '\0' CONDA_ACTIVATE_PREAMBLE <<'END_CONDA_ACTIVATE '
if ! type conda >& /dev/null; then
echo "::ERROR:: you must have conda in your environment first"
return 1 # don't want to exit here because this file is sourced
fi
# if we're sourcing this in a sub process that has conda in the PATH but not as a function, init it again
conda activate --help >& /dev/null || source $( conda info --base) /etc/profile.d/conda.sh
\0
END_CONDA_ACTIVATE
2022-09-16 02:38:20 +08:00
if [ " $IS_LIBRARY " = true ] ; then
# the chipyard conda environment should be installed already and be sufficient
if [ -z " ${ CONDA_DEFAULT_ENV +x } " ] ; then
echo "ERROR: No conda environment detected. If using Chipyard, did you source 'env.sh'."
exit 5
fi
else
# note: lock file must end in .conda-lock.yml - see https://github.com/conda-incubator/conda-lock/issues/154
if [ " $USE_PINNED_DEPS " = false ] ; then
# auto-gen the lockfile
2023-03-23 06:01:17 +08:00
./scripts/generate-conda-lockfile.sh
2022-09-16 02:38:20 +08:00
fi
2023-03-24 03:51:06 +08:00
LOCKFILE = " $( find $RDIR /conda-reqs/*.conda-lock.yml) "
2022-09-16 02:38:20 +08:00
conda-lock install -p $RDIR /.conda-env $LOCKFILE
source $RDIR /.conda-env/etc/profile.d/conda.sh
conda activate $RDIR /.conda-env
2022-10-29 02:58:05 +08:00
env_append " $CONDA_ACTIVATE_PREAMBLE "
2022-09-16 02:38:20 +08:00
env_append " conda activate $RDIR /.conda-env "
fi
2019-06-28 13:11:06 +08:00
git config submodule.target-design/chipyard.update none
2019-05-29 23:34:26 +08:00
git submodule update --init --recursive #--jobs 8
2022-09-16 02:38:20 +08:00
# Chipyard setup
2019-05-29 23:34:26 +08:00
if [ " $IS_LIBRARY " = false ] ; then
2020-01-30 06:33:29 +08:00
# This checks if firemarshal has already been configured by someone. If
# not, we will provide our own config. This must be checked before calling
# init-submodules-no-riscv-tools.sh because that will configure
# firemarshal.
2022-09-16 02:38:20 +08:00
marshal_cfg = " $RDIR /target-design/chipyard/software/firemarshal/marshal-config.yaml "
if [ ! -f " $marshal_cfg " ] ; then
2020-01-29 06:53:59 +08:00
first_init = true
else
first_init = false
fi
2020-01-29 05:25:01 +08:00
2019-06-28 13:11:06 +08:00
git config --unset submodule.target-design/chipyard.update
git submodule update --init target-design/chipyard
cd $RDIR /target-design/chipyard
2022-05-24 06:00:04 +08:00
2022-09-16 02:38:20 +08:00
SKIP_TOOLCHAIN_ARG = ""
if [ " $SKIP_TOOLCHAIN " = true ] ; then
2023-01-27 02:11:44 +08:00
SKIP_TOOLCHAIN_ARG = "-s 3"
2022-09-16 02:38:20 +08:00
fi
# default to normal riscv-tools toolchain
2023-01-28 02:44:09 +08:00
./build-setup.sh --force $SKIP_TOOLCHAIN_ARG -s 1 -s 4 -s 5 -s 6 -s 7 -s 8 -s 9
2022-09-16 02:38:20 +08:00
2022-07-27 00:32:56 +08:00
# Deinitialize Chipyard's FireSim submodule so that fuzzy finders, IDEs,
# etc., don't get confused by source duplication.
git submodule deinit sims/firesim
2019-05-29 23:34:26 +08:00
cd $RDIR
2020-01-28 09:47:32 +08:00
2020-01-29 05:25:01 +08:00
# Configure firemarshal to know where our firesim installation is.
# If this is a fresh init of chipyard, we can safely overwrite the marshal
# config, otherwise we have to assume the user might have changed it
2020-01-29 06:53:59 +08:00
if [ $first_init = true ] ; then
2020-02-14 05:15:23 +08:00
echo "firesim-dir: '../../../../'" > $marshal_cfg
2020-01-28 09:47:32 +08:00
fi
2020-05-06 14:29:58 +08:00
env_append "export FIRESIM_STANDALONE=1"
2019-05-28 06:49:44 +08:00
2022-09-18 07:39:54 +08:00
# FireMarshal setup
2022-09-16 02:38:20 +08:00
target_chipyard_dir = " $RDIR /target-design/chipyard "
2019-05-28 06:49:44 +08:00
2020-02-21 04:09:02 +08:00
# setup marshal symlink
2020-05-06 14:29:58 +08:00
ln -sf ../target-design/chipyard/software/firemarshal $RDIR /sw/firesim-software
2022-09-10 02:47:13 +08:00
2022-09-13 06:31:30 +08:00
env_append " export PATH= $RDIR /sw/firesim-software:\$PATH "
2022-09-18 07:39:54 +08:00
2022-10-17 10:46:34 +08:00
env_append " source $RDIR /scripts/fix-open-files.sh "
2022-09-18 07:39:54 +08:00
else
# FireMarshal setup
target_chipyard_dir = " $RDIR /../.. "
# setup marshal symlink
ln -sf ../../../software/firemarshal $RDIR /sw/firesim-software
# Source CY env.sh in library-mode
env_append " source $target_chipyard_dir /env.sh "
2022-09-13 02:38:21 +08:00
fi
2022-09-16 02:38:20 +08:00
cd " $RDIR "
2019-05-28 06:49:44 +08:00
2018-05-20 03:38:02 +08:00
# commands to run only on EC2
# see if the instance info page exists. if not, we are not on ec2.
# this is one of the few methods that works without sudo
if wget -T 1 -t 3 -O /dev/null http://169.254.169.254/; then
2019-05-23 09:48:38 +08:00
2022-03-30 04:48:17 +08:00
(
# ensure that we're using the system toolchain to build the kernel modules
# newer gcc has --enable-default-pie and older kernels think the compiler
# is broken unless you pass -fno-pie but then I was encountering a weird
# error about string.h not being found
export PATH = /usr/bin:$PATH
cd " $RDIR /platforms/f1/aws-fpga/sdk/linux_kernel_drivers/xdma "
make
2019-05-23 09:48:38 +08:00
2022-03-30 04:48:17 +08:00
# the only ones missing are libguestfs-tools
sudo yum install -y libguestfs-tools bc
# Setup for using qcow2 images
2022-09-16 02:38:20 +08:00
cd " $RDIR "
2022-03-30 04:48:17 +08:00
./scripts/install-nbd-kmod.sh
)
2019-11-08 06:38:57 +08:00
2022-03-19 23:59:47 +08:00
(
if [ [ " ${ CPPFLAGS :- zzz } " != "zzz" ] ] ; then
# don't set it if it isn't already set but strip out -DNDEBUG because
# the sdk software has assertion-only variable usage that will end up erroring
# under NDEBUG with -Wall and -Werror
export CPPFLAGS = " ${ CPPFLAGS /-DNDEBUG/ } "
fi
# Source {sdk,hdk}_setup.sh once on this machine to build aws libraries and
# pull down some IP, so we don't have to waste time doing it each time on
# worker instances
2022-09-16 02:38:20 +08:00
AWSFPGA = " $RDIR /platforms/f1/aws-fpga "
cd " $AWSFPGA "
2022-03-19 23:59:47 +08:00
bash -c "source ./sdk_setup.sh"
bash -c "source ./hdk_setup.sh"
)
2022-04-01 09:02:19 +08:00
2018-05-20 03:38:02 +08:00
fi
2022-09-16 02:38:20 +08:00
cd " $RDIR "
2020-05-06 14:29:58 +08:00
set +e
2018-09-23 03:36:39 +08:00
./gen-tags.sh
2020-05-06 14:29:58 +08:00
set -e
2022-03-22 12:54:26 +08:00
read -r -d '\0' NDEBUG_CHECK <<'END_NDE BUG'
# Ensure that we don't have -DNDEBUG anywhere in our environment
# check and fixup the known place where conda will put it
if [ [ " $CPPFLAGS " = = *"-DNDEBUG" * ] ] ; then
echo "::INFO:: removing '-DNDEBUG' from CPPFLAGS as we prefer to leave assertions in place"
export CPPFLAGS = " ${ CPPFLAGS /-DNDEBUG/ } "
fi
# check for any other occurances and warn the user
2022-03-30 04:48:17 +08:00
env | grep -v 'CONDA_.*_BACKUP' | grep -- -DNDEBUG && echo "::WARNING:: you still seem to have -DNDEBUG in your environment. This is known to cause problems."
2022-03-22 21:11:04 +08:00
true # ensure env.sh exits 0
2022-03-22 12:54:26 +08:00
\0
END_NDEBUG
env_append " $NDEBUG_CHECK "
2020-05-06 14:29:58 +08:00
# Write out the generated env.sh indicating successful completion.
echo " $env_string " > env.sh
2018-09-23 03:36:39 +08:00
2018-05-20 03:38:02 +08:00
echo "Setup complete!"
2022-09-29 12:58:13 +08:00
echo "To generate simulator RTL and run metasimulation simulation, source env.sh"
2019-05-28 06:49:44 +08:00
echo "To use the manager to deploy builds/simulations on EC2, source sourceme-f1-manager.sh to setup your environment."
2018-05-20 03:38:02 +08:00
echo "To run builds/simulations manually on this machine, source sourceme-f1-full.sh to setup your environment."
2022-09-29 12:58:13 +08:00
echo "For more information, see docs at https://docs.fires.im/."