@@ -6,104 +6,194 @@ cd $ROOT
66
77CONTAINER_TAG=` cat ${ROOT} /bin/.container-tag`
88
9+ # Supported container types:
10+ #
11+ # = devcontainer
12+ #
13+ # When devcontainer is used, all commands run "natively" (no container prefix), because it's assumed
14+ # that the command itself is already in the container context
15+ #
16+ # = docker
17+ #
18+ # A docker container. All commands on the host (using ./do or ./bin/*) are run in the context of a Docker image.
19+ #
20+ # = singularity
21+ #
22+ # An apptainer/singularity container. All commands on the host (using ./do or ./bin/*) are run in the context of an Apptainer/Singularity image.
23+ #
24+ #
25+ # ALL THREE CONTAINERS ARE IDENTICAL. Which one you use is a preference of your setup and installed
26+ # software.
27+
28+
29+ # get the container type
30+ #
31+ # use, in priority order:
32+ # 1. devcontainer, if DEVCONTAINER_ENV is set in the enviornment
33+ # 2. the type stored in .container-type
34+ # 3. Docker, if DOCKER is set in the enviornment
35+ # 4. Singularity, if SINGULARITY is set in the enviornment
36+ # 5. The choice made by the user, which will be cached in .container-type
37+ if [ -v DEVCONTAINER_ENV ]; then
38+ CONTAINER_TYPE=devcontainer
39+ elif [ -f ${ROOT} /.container-type ]; then
40+ CONTAINER_TYPE=` cat ${ROOT} /.container-type`
41+ elif [ -v DOCKER ]; then
42+ CONTAINER_TYPE=docker
43+ elif [ -v SINGULARITY ]; then
44+ CONTAINER_TYPE=singularity
45+ else
46+ echo -e " UDB tools run in a container. Both Docker and Singularity/Apptainer are supported.\\ n\\ n1. Docker\\ n2. Singularity\\ n"
47+ while true ; do
48+ echo " Which would you like to use? (1/2) "
49+ read ans
50+ case $ans in
51+ [1]* ) CONTAINER_TYPE=docker; break ;;
52+ [2]* ) CONTAINER_TYPE=singularity; break ;;
53+ * ) echo -e " \\ nPlease answer 1 or 2." ;;
54+ esac
55+ done
56+ fi
57+
58+ if [ " ${CONTAINER_TYPE} " != " docker" -a " ${CONTAINER_TYPE} " != " singularity" ]; then
59+ echo " BAD CONTAINER TYPE: ${CONTAINER_TYPE} "
60+ fi
61+
62+ if [ ! -f ${ROOT} /.container-type ]; then
63+ echo ${CONTAINER_TYPE} > ${ROOT} /.container-type
64+ fi
65+
66+ print_env () {
67+ if [ " ${CONTAINER_TYPE} " == " docker" ]; then
68+ echo " Using Docker environment"
69+ else
70+ echo " Using Singularity environment"
71+ fi
72+ }
73+
974if [ -v GITHUB_ACTIONS ]; then
10- echo " ACTIONS"
11- CONTAINER_PATH=${ROOT} /.singularity/image.sif
12- HOME_PATH=${GITHUB_WORKSPACE}
13- HOME_OPT=" --home ${ROOT} /.home"
14- SINGULARITY_CACHE=--disable-cache
75+ echo " ACTIONS"
76+ CONTAINER_PATH=${ROOT} /.singularity/image.sif
77+ HOME_PATH=${GITHUB_WORKSPACE}
78+ HOME_OPT=" --home ${ROOT} /.home"
79+ SINGULARITY_CACHE=--disable-cache
80+ elif [ " ${CONTAINER_TYPE} " == " docker" ]; then
81+ print_env
82+ if ! docker images riscvintl/udb:${CONTAINER_TAG} | grep -q udb ; then
83+ # TODO: pull the image if it can be found
84+ echo " Building Docker image..."
85+ docker build -t riscvintl/udb:${CONTAINER_TAG} -f .devcontainer/Dockerfile .
86+ fi
87+ if [ -t 1 -a -t 0 ]; then
88+ DOCKER_BASE=" docker run -it -v $( ROOT) :$( ROOT) -w $( ROOT) riscvintl/udb:${CONTAINER_TAG} "
89+ else
90+ DOCKER_BASE=" docker run -v $( ROOT) :$( ROOT) -w $( ROOT) riscvintl/udb:${CONTAINER_TAG} "
91+ fi
92+ RUN=" ${DOCKER_BASE} "
1593else
16- CONTAINER_PATH=${ROOT} /.singularity/image-$CONTAINER_TAG .sif
17- HOME_PATH=${HOME}
18- HOME_OPT=" --bind ${ROOT} /.home:${HOME_PATH} --bind /local/mnt/workspace/.vscode-server:/local/mnt/workspace/.vscode-server"
19- SINGULARITY_CACHE=
94+ print_env
95+ CONTAINER_PATH=${ROOT} /.singularity/image-$CONTAINER_TAG .sif
96+ HOME_PATH=${HOME}
97+ HOME_OPT=" --bind ${ROOT} /.home:${HOME_PATH} "
98+ SINGULARITY_CACHE=
99+ if [ ! -f ${CONTAINER_PATH} ]; then
100+ echo " Fetching container..."
101+ if [ ! -d " ${ROOT} /.singularity" ]; then
102+ mkdir -p ${ROOT} /.singularity
103+ fi
104+ singularity pull ${SINGULARITY_CACHE} ${CONTAINER_PATH} oras://docker.io/riscvintl/spec-generator:$CONTAINER_TAG
105+ fi
20106fi
21107
22108if [ -f $ROOT /.git ]; then
23- # if this is a worktree, need to add the parent git repo in, too
24- GIT_PATH=` git rev-parse --git-common-dir | tr -d ' \n' | xargs dirname`
25- HOME_OPT=" ${HOME_OPT} --bind ${GIT_PATH} :${GIT_PATH} "
109+ # if this is a worktree, need to add the parent git repo in, too
110+ GIT_PATH=` git rev-parse --git-common-dir | tr -d ' \n' | xargs dirname`
111+ HOME_OPT=" ${HOME_OPT} --bind ${GIT_PATH} :${GIT_PATH} "
26112fi
27113
28114if [ -v DEVCONTAINER_ENV ]; then
29- RUN=" "
115+ RUN=" "
116+ elif [ " ${CONTAINER_TYPE} " == " docker" ]; then
117+ RUN=" ${DOCKER_BASE} "
30118else
31- RUN=" singularity run ${HOME_OPT} ${CONTAINER_PATH} "
119+ RUN=" singularity run ${HOME_OPT} ${CONTAINER_PATH} "
32120fi
33121
34122if [ ! -d $ROOT /.home ]; then
35- mkdir $ROOT /.home
36- fi
37-
38- if [ ! -v DEVCONTAINER_ENV ]; then
39- if [ ! -f ${CONTAINER_PATH} ]; then
40- echo " Fetching container..."
41- if [ ! -d " ${ROOT} /.singularity" ]; then
42- mkdir -p ${ROOT} /.singularity
43- fi
44- singularity pull ${SINGULARITY_CACHE} ${CONTAINER_PATH} oras://docker.io/riscvintl/spec-generator:$CONTAINER_TAG
45- fi
123+ mkdir $ROOT /.home
46124fi
47125
48126if [ ! -f $ROOT /.bundle/config ]; then
49- OLDDIR=$PWD
50- cd $ROOT
51- ${RUN} bundle config set --local path ${ROOT} /.home/.gems
52- ${RUN} bundle config set --local cache_path ${ROOT} /.home/.cache
53- cd $OLDDIR
127+ OLDDIR=$PWD
128+ cd $ROOT
129+ ${RUN} bundle config set --local path ${ROOT} /.home/.gems
130+ ${RUN} bundle config set --local cache_path ${ROOT} /.home/.cache
131+ cd $OLDDIR
54132fi
55133
56134if [ ! -d $ROOT /.home/.gems ]; then
57- OLDDIR=$PWD
58- cd $ROOT
59- ${RUN} bundle install
60- cd $OLDDIR
135+ OLDDIR=$PWD
136+ cd $ROOT
137+ ${RUN} bundle install
138+ cd $OLDDIR
61139fi
62140
63141if [ ! -d $ROOT /.home/.venv ]; then
64- ${RUN} /usr/bin/python3 -m venv ${ROOT} /.home/.venv
142+ ${RUN} /usr/bin/python3 -m venv ${ROOT} /.home/.venv
65143fi
144+
66145source ${ROOT} /.home/.venv/bin/activate
146+
67147if [ ! -f ${ROOT} /.home/.venv/bin/pre-commit ]; then
68- ${RUN} ${ROOT} /.home/.venv/bin/pip install -r requirements.txt
148+ ${RUN} ${ROOT} /.home/.venv/bin/pip install -r requirements.txt
69149fi
70150
71151# if [ ! -f $ROOT/ext/riscv-opcodes/README.md ]; then
72152# git submodule update --init ext/riscv-opcodes
73153# fi
74154
75155if [[ ! -z " $DEVELOPMENT " && $DEVELOPMENT -eq 1 ]]; then
76- if [ ! -d " ${ROOT} /.home/.yard/gem_index" ]; then
77- ${RUN} bundle exec --gemfile ${ROOT} /Gemfile yard config --gem-install-yri
78- ${RUN} bundle exec --gemfile ${ROOT} /Gemfile yard gems
79- touch ${ROOT} /.stamps/dev_gems
80- fi
156+ if [ ! -d " ${ROOT} /.home/.yard/gem_index" ]; then
157+ ${RUN} bundle exec --gemfile ${ROOT} /Gemfile yard config --gem-install-yri
158+ ${RUN} bundle exec --gemfile ${ROOT} /Gemfile yard gems
159+ touch ${ROOT} /.stamps/dev_gems
160+ fi
81161fi
82162
83163if [ ! -d ${ROOT} /node_modules ]; then
84- ${RUN} npm i
164+ ${RUN} npm i
85165fi
86166
87167if [ -v DEVCONTAINER_ENV ]; then
88- BUNDLE=" bundle"
89- RUBY=" bundle exec ruby"
90- RAKE=" bundle exec rake"
91- NPM=" npm"
92- NPX=" npx"
93- NODE=" node"
94- PYTHON=" ${ROOT} /.home/.venv/bin/python3"
95- PIP=" ${ROOT} /.home/.venv/bin/pip"
96- BASH=" bash"
168+ BUNDLE=" bundle"
169+ RUBY=" bundle exec ruby"
170+ RAKE=" bundle exec rake"
171+ NPM=" npm"
172+ NPX=" npx"
173+ NODE=" node"
174+ PYTHON=" ${ROOT} /.home/.venv/bin/python3"
175+ PIP=" ${ROOT} /.home/.venv/bin/pip"
176+ BASH=" bash"
177+ elif [ " ${CONTAINER_TYPE} " == " docker" ]; then
178+ BUNDLE=" ${DOCKER_BASE} bundle"
179+ RUBY=" ${DOCKER_BASE} bundle exec ruby"
180+ RAKE=" ${DOCKER_BASE} bundle exec rake"
181+ NPM=" ${DOCKER_BASE} npm"
182+ NPX=" ${DOCKER_BASE} npx"
183+ NODE=" ${DOCKER_BASE} node"
184+ PYTHON=" ${DOCKER_BASE} ${ROOT} /.home/.venv/bin/python3"
185+ PIP=" ${DOCKER_BASE} ${ROOT} /.home/.venv/bin/pip"
186+ BASH=" ${DOCKER_BASE} bash"
97187else
98- BUNDLE=" singularity run ${HOME_OPT} ${CONTAINER_PATH} bundle"
99- RUBY=" singularity run ${HOME_OPT} ${CONTAINER_PATH} bundle exec ruby"
100- RAKE=" singularity run ${HOME_OPT} ${CONTAINER_PATH} bundle exec rake"
101- NPM=" singularity run ${HOME_OPT} ${CONTAINER_PATH} npm"
102- NPX=" singularity run ${HOME_OPT} ${CONTAINER_PATH} npx"
103- NODE=" singularity run ${HOME_OPT} ${CONTAINER_PATH} node"
104- PYTHON=" singularity run ${HOME_OPT} ${CONTAINER_PATH} ${ROOT} /.home/.venv/bin/python3"
105- PIP=" singularity run ${HOME_OPT} ${CONTAINER_PATH} ${ROOT} /.home/.venv/bin/pip"
106- BASH=" singularity run ${HOME_OPT} ${CONTAINER_PATH} bash"
188+ BUNDLE=" singularity run ${HOME_OPT} ${CONTAINER_PATH} bundle"
189+ RUBY=" singularity run ${HOME_OPT} ${CONTAINER_PATH} bundle exec ruby"
190+ RAKE=" singularity run ${HOME_OPT} ${CONTAINER_PATH} bundle exec rake"
191+ NPM=" singularity run ${HOME_OPT} ${CONTAINER_PATH} npm"
192+ NPX=" singularity run ${HOME_OPT} ${CONTAINER_PATH} npx"
193+ NODE=" singularity run ${HOME_OPT} ${CONTAINER_PATH} node"
194+ PYTHON=" singularity run ${HOME_OPT} ${CONTAINER_PATH} ${ROOT} /.home/.venv/bin/python3"
195+ PIP=" singularity run ${HOME_OPT} ${CONTAINER_PATH} ${ROOT} /.home/.venv/bin/pip"
196+ BASH=" singularity run ${HOME_OPT} ${CONTAINER_PATH} bash"
107197fi
108198
109199if [ ! -f $ROOT /.git/hooks/pre-commit ]; then
@@ -125,7 +215,7 @@ if [ -x "\$INSTALL_PYTHON" ]; then
125215elif command -v pre-commit > /dev/null; then
126216 exec pre-commit "\$ {ARGS[@]}"
127217else
128- echo '` pre-commit` not found. Did you forget to activate your virtualenv?' 1>&2
218+ echo '\ ` pre-commit\ ` not found. Did you forget to activate your virtualenv?' 1>&2
129219 exit 1
130220fi
131221HOOK
0 commit comments