11#! /usr/bin/env bash
2-
32set -ex
43
4+ # docker_shell — unified launcher for AutoTuner / OpenROAD
5+ #
6+ # Usage: etc/docker_shell [OPTIONS] [-- CMD [ARGS...]]
7+ #
8+ # Options:
9+ # -i, --image IMAGE Docker image (overrides $OR_IMAGE and default)
10+ # --user Map host UID/GID into the container via entrypoint.sh
11+ # -p, --port SPEC Forward a port, e.g. --port 8265:8265 (repeatable)
12+ # --dry-run Print the docker command without executing it
13+ # -h|--help Show this help and exit
14+
515#
616# Method to use docker CLI to determine if we're using docker or podman
717#
@@ -20,7 +30,34 @@ get_container_engine () {
2030 fi
2131}
2232
23- DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd ) "
33+ DEFAULT_IMAGE=" openroad/orfs:latest"
34+ FLOW_DIR=" /OpenROAD-flow-scripts/flow"
35+ USE_USER=false
36+ DRY_RUN=false
37+ PORTS=()
38+ IMAGE=" "
39+ CMD_ARGS=()
40+
41+ usage () {
42+ grep ' ^#' " $0 " | sed ' s/^# \{0,1\}//'
43+ exit 0
44+ }
45+
46+ while [[ $# -gt 0 ]]; do
47+ case " $1 " in
48+ -i|--image) IMAGE=" $2 " ; shift 2 ;;
49+ --user) USE_USER=true; shift ;;
50+ -p|--port) PORTS+=(" $2 " ); shift 2 ;;
51+ --dry-run) DRY_RUN=true; shift ;;
52+ -h|--help) usage ;;
53+ --) shift ; CMD_ARGS=(" $@ " ); break ;;
54+ -* ) echo " Unknown option: $1 " >&2 ; exit 1 ;;
55+ * ) CMD_ARGS=(" $@ " ); break ;;
56+ esac
57+ done
58+
59+ # Image resolution: --image > $OR_IMAGE > default
60+ IMAGE=" ${IMAGE:- ${OR_IMAGE:- $DEFAULT_IMAGE } } "
2461
2562get_container_engine
2663
@@ -31,41 +68,65 @@ KLAYOUT_CMD=${KLAYOUT_CMD:-/usr/bin/klayout}
3168
3269XSOCK=/tmp/.X11-unix
3370XAUTH=/tmp/.docker.xauth
34- xauth nlist $DISPLAY | sed -e ' s/^..../ffff/' | xauth -f $XAUTH nmerge -
35- ARGUMENTS=$@
71+ if ! $DRY_RUN ; then
72+ xauth nlist ${DISPLAY:-: 0} | sed -e ' s/^..../ffff/' | xauth -f $XAUTH nmerge -
73+ fi
3674
3775if test -t 0; then
3876 DOCKER_INTERACTIVE=-ti
77+ else
78+ DOCKER_INTERACTIVE=
3979fi
4080
41- if [[ $container_engine == " podman" ]]; then
81+ if $USE_USER ; then
82+ user_args=" --entrypoint=entrypoint.sh --user=root"
83+ user_env=" -e CUSTOM_USER=$( id --user --name) -e CUSTOM_USER_ID=$( id --user) -e CUSTOM_GROUP_ID=$( id --group) "
84+ elif [[ $container_engine == " podman" ]]; then
4285 user_args=" --privileged --userns=keep-id"
86+ user_env=
4387else
4488 user_args=" -u $( id -u ${USER} ) :$( id -g ${USER} ) "
89+ user_env=
90+ fi
91+
92+ PORT_ARGS=()
93+ for port in " ${PORTS[@]} " ; do
94+ PORT_ARGS+=(-p " $port " )
95+ done
96+
97+ CMD_STR=
98+ [[ ${# CMD_ARGS[@]} -gt 0 ]] && CMD_STR=" ${CMD_ARGS[*]} "
99+
100+ ENV_SH_CMD=' if [ -f ../env.sh ]; then . ../env.sh; fi;'
101+
102+ # Build the command once; dry-run prints it, otherwise exec it.
103+ DOCKER_CMD=(
104+ " $container_engine " run --rm -i
105+ $DOCKER_INTERACTIVE
106+ $user_env $user_args
107+ -e LIBGL_ALWAYS_SOFTWARE=1
108+ -e QT_X11_NO_MITSHM=1
109+ -e XDG_RUNTIME_DIR=/tmp/xdg-run
110+ -e " DISPLAY=${DISPLAY:-: 0} "
111+ -e QT_XKB_CONFIG_ROOT=/usr/share/X11/xkb
112+ -v " $XSOCK :$XSOCK "
113+ -v " $XAUTH :$XAUTH "
114+ -e " XAUTHORITY=$XAUTH "
115+ -e FLOW_HOME=/OpenROAD-flow-scripts/flow/
116+ -e WORK_HOME=/work
117+ -e " YOSYS_EXE=$YOSYS_EXE "
118+ -e " OPENROAD_EXE=$OPENROAD_EXE "
119+ -e " KLAYOUT_CMD=$KLAYOUT_CMD "
120+ -v " $WORKSPACE :/work"
121+ --network host
122+ " ${PORT_ARGS[@]} "
123+ " $IMAGE "
124+ bash -c " set -ex; mkdir /tmp/xdg-run; cd ${FLOW_DIR} ; ${ENV_SH_CMD} $CMD_STR "
125+ )
126+
127+ if $DRY_RUN ; then
128+ echo " ${DOCKER_CMD[*]} "
129+ exit 0
45130fi
46131
47- # Most of these options below has to do with allowing to
48- # run the OpenROAD GUI from within Docker.
49- docker run $user_args \
50- -e LIBGL_ALWAYS_SOFTWARE=1 \
51- -e " QT_X11_NO_MITSHM=1" \
52- -e XDG_RUNTIME_DIR=/tmp/xdg-run \
53- -e DISPLAY=$DISPLAY \
54- -e QT_XKB_CONFIG_ROOT=/usr/share/X11/xkb \
55- -v $XSOCK :$XSOCK \
56- -v $XAUTH :$XAUTH \
57- -e XAUTHORITY=$XAUTH \
58- -e FLOW_HOME=/OpenROAD-flow-scripts/flow/ \
59- -e YOSYS_EXE=$YOSYS_EXE \
60- -e OPENROAD_EXE=$OPENROAD_EXE \
61- -e KLAYOUT_CMD=$KLAYOUT_CMD \
62- -v $WORKSPACE :/OpenROAD-flow-scripts/flow:Z \
63- --network host \
64- $DOCKER_INTERACTIVE \
65- ${OR_IMAGE:- openroad/ orfs: latest} \
66- bash -c " set -ex
67- mkdir /tmp/xdg-run
68- cd /OpenROAD-flow-scripts/flow
69- . ../env.sh
70- $ARGUMENTS
71- "
132+ exec " ${DOCKER_CMD[@]} "
0 commit comments