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+ # --orfs-flow Mount pwd to /OpenROAD-flow-scripts/flow instead of /work
12+ # -p, --port SPEC Forward a port, e.g. --port 8265:8265 (repeatable)
13+ # --dry-run Print the docker command without executing it
14+ # -h|--help Show this help and exit
15+
516#
617# Method to use docker CLI to determine if we're using docker or podman
718#
@@ -20,7 +31,34 @@ get_container_engine () {
2031 fi
2132}
2233
23- DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd ) "
34+ DEFAULT_IMAGE=" openroad/orfs:latest"
35+ MOUNT_TARGET=" /work"
36+ USE_USER=false
37+ DRY_RUN=false
38+ PORTS=()
39+ IMAGE=" "
40+ CMD_ARGS=()
41+
42+ usage () {
43+ grep ' ^#' " $0 " | sed ' s/^# \{0,1\}//'
44+ exit 0
45+ }
46+
47+ while [[ $# -gt 0 ]]; do
48+ case " $1 " in
49+ -i|--image) IMAGE=" $2 " ; shift 2 ;;
50+ --user) USE_USER=true; shift ;;
51+ --orfs-flow) MOUNT_TARGET=" /OpenROAD-flow-scripts/flow" ; shift ;;
52+ -p|--port) PORTS+=(" $2 " ); shift 2 ;;
53+ --dry-run) DRY_RUN=true; shift ;;
54+ -h|--help) usage ;;
55+ --) shift ; CMD_ARGS=(" $@ " ); break ;;
56+ * ) echo " Unknown option: $1 " >&2 ; exit 1 ;;
57+ esac
58+ done
59+
60+ # Image resolution: --image > $OR_IMAGE > default
61+ IMAGE=" ${IMAGE:- ${OR_IMAGE:- $DEFAULT_IMAGE } } "
2462
2563get_container_engine
2664
@@ -31,41 +69,68 @@ KLAYOUT_CMD=${KLAYOUT_CMD:-/usr/bin/klayout}
3169
3270XSOCK=/tmp/.X11-unix
3371XAUTH=/tmp/.docker.xauth
34- xauth nlist $DISPLAY | sed -e ' s/^..../ffff/' | xauth -f $XAUTH nmerge -
35- ARGUMENTS=$@
72+ if ! $DRY_RUN ; then
73+ xauth nlist ${DISPLAY:-: 0} | sed -e ' s/^..../ffff/' | xauth -f $XAUTH nmerge -
74+ fi
3675
3776if test -t 0; then
3877 DOCKER_INTERACTIVE=-ti
78+ else
79+ DOCKER_INTERACTIVE=
3980fi
4081
41- if [[ $container_engine == " podman" ]]; then
82+ if $USE_USER ; then
83+ user_args=" --entrypoint=entrypoint.sh --user=root"
84+ user_env=" -e CUSTOM_USER=$( id --user --name) -e CUSTOM_USER_ID=$( id --user) -e CUSTOM_GROUP_ID=$( id --group) "
85+ elif [[ $container_engine == " podman" ]]; then
4286 user_args=" --privileged --userns=keep-id"
87+ user_env=
4388else
4489 user_args=" -u $( id -u ${USER} ) :$( id -g ${USER} ) "
90+ user_env=
91+ fi
92+
93+ PORT_ARGS=()
94+ for port in " ${PORTS[@]} " ; do
95+ PORT_ARGS+=(-p " $port " )
96+ done
97+
98+ CMD_STR=
99+ [[ ${# CMD_ARGS[@]} -gt 0 ]] && CMD_STR=" ${CMD_ARGS[*]} "
100+
101+ # Source env.sh only when mounting to ORFS flow dir (adds OR tools to PATH).
102+ # AutoTuner images install binaries in /usr/local/bin so env.sh is not needed
103+ # and would error out if absent.
104+ ENV_SH_CMD=
105+ [[ " $MOUNT_TARGET " == " /OpenROAD-flow-scripts/flow" ]] && ENV_SH_CMD=" . ../env.sh;"
106+
107+ # Build the command once; dry-run prints it, otherwise exec it.
108+ DOCKER_CMD=(
109+ " $container_engine " run --rm -i
110+ $DOCKER_INTERACTIVE
111+ $user_env $user_args
112+ -e LIBGL_ALWAYS_SOFTWARE=1
113+ -e QT_X11_NO_MITSHM=1
114+ -e XDG_RUNTIME_DIR=/tmp/xdg-run
115+ -e " DISPLAY=${DISPLAY:-: 0} "
116+ -e QT_XKB_CONFIG_ROOT=/usr/share/X11/xkb
117+ -v " $XSOCK :$XSOCK "
118+ -v " $XAUTH :$XAUTH "
119+ -e " XAUTHORITY=$XAUTH "
120+ -e FLOW_HOME=/OpenROAD-flow-scripts/flow/
121+ -e " YOSYS_EXE=$YOSYS_EXE "
122+ -e " OPENROAD_EXE=$OPENROAD_EXE "
123+ -e " KLAYOUT_CMD=$KLAYOUT_CMD "
124+ -v " $WORKSPACE :${MOUNT_TARGET} "
125+ --network host
126+ " ${PORT_ARGS[@]} "
127+ " $IMAGE "
128+ bash -c " set -ex; mkdir /tmp/xdg-run; cd ${MOUNT_TARGET} ; ${ENV_SH_CMD} $CMD_STR "
129+ )
130+
131+ if $DRY_RUN ; then
132+ echo " ${DOCKER_CMD[*]} "
133+ exit 0
45134fi
46135
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- "
136+ exec " ${DOCKER_CMD[@]} "
0 commit comments