-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·165 lines (147 loc) · 5.14 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
#
# SPDX-License-Identifier: Apache-2.0
# Shared directory for downloads
FIREFOX_DOWNLOADS="./Downloads"
# Developer mode - Drop into a shell after firefox runs (or fails)
DEBUG_MODE=0
# Relative path to patches which will be applied to firefox
PATCHES="patches/$(uname -m)"
# Name of docker image and container to create
DOCKER_IMAGE="firefox-build-and-run"
DOCKER_CONTAINER="firefox-build-and-run"
# Default release to build
FIREFOX_RELEASE="central"
# Path to sources. If empty, Firefox will be cloned in the container.
FIREFOX_SRC=""
# Parallel build jobs. Some jobs will take ~1GB memory or more, so don't go
# crazy or you may end up with strange build failures caused by OOM. If no
# value is passed into this script, then we'll try to calculate a reasonable
# value based on CPU threads and available memory. The calculation will be done
# inside the container in case the user wants to limit resources via Docker.
JOBS=""
#
# Command-line parsing (loosely based on coreboot's buildgcc script)
#
myhelp()
{
printf "Options:\n"
printf " [-c|--container] name of Docker container (default: $DOCKER_CONTAINER)\n"
printf " [-d|--debug] debug mode (default: $DEBUG_MODE)\n"
printf " [-D|--downloads] path of downloads directory shared with container (default: $FIREFOX_DOWNLOADS)\n"
printf " [-h|--help] print this help menu\n"
printf " [-j|--jobs] number of concurrent build jobs to run (default: $JOBS)\n"
printf " [-p|--patches] alternative path to patches (default: $PATCHES)\n"
printf " [-r|--release] Firefox release (tag, bookmark, or commit) to build (default: $FIREFOX_RELEASE)\n"
printf " [-s|--src] path to cached Firefox sources, if desired (default: none, clone Firefox)\n"
}
# Look if we have getopt. If not, build it.
export PATH=$PATH:.
getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
# parse parameters.. try to find out whether we're running GNU getopt
getoptbrand="$(getopt -V 2>/dev/null | sed -e '1!d' -e 's,^\(......\).*,\1,')"
shortopts="c:D:dhi:j:p:s:r:"
if [ "${getoptbrand}" = "getopt" ]; then
# Detected GNU getopt that supports long options.
args=$(getopt -l container:,debug,downloads:,help,image:,jobs:,patches:,release:,src: -o "$shortopts" -- "$@")
getopt_ret=$?
eval set -- "$args"
else
# Detected non-GNU getopt
args=$(getopt "$shortopts" $*)
getopt_ret=$?
# shellcheck disable=SC2086
set -- $args
fi
if [ $getopt_ret != 0 ]; then
myhelp
exit 1
fi
while true ; do
case "$1" in
-c|--container) shift; DOCKER_CONTAINER="$1"; shift;;
-d|--debug) shift; DEBUG_MODE=1;;
-D|--downloads) shift; FIREFOX_DOWNLOADS="$1"; shift;;
-h|--help) shift; myhelp; exit 0;;
-j|--jobs) shift; JOBS="$1"; shift;;
-p|--patches) shift; PATCHES="$1"; shift;;
-r|--release) shift; FIREFOX_RELEASE="$1"; shift;;
-s|--src) shift; FIREFOX_SRC="$1"; shift;;
--) shift; break;;
*) break;;
esac
done
if [ $# -gt 0 ]; then
printf "Excessive arguments: $*\n"
myhelp
exit 1
fi
# Path to local copy of firefox repo. See README.md for more information
# about using local sources.
if [ -n "$FIREFOX_SRC" ]; then
IN_CONTAINER_SRC="/var/firefox_src"
FIREFOX_SRC_MOUNT_OPT="--mount type=bind,target=${IN_CONTAINER_SRC},src=${FIREFOX_SRC}"
FIREFOX_SRC="$IN_CONTAINER_SRC"
else
FIREFOX_SRC_MOUNT_OPT=""
fi
if [ ! -d "$PATCHES" ]; then
echo "Patches directory \"$PATCHES\" does not exist"
exit 1
fi
echo "Using the following options:"
echo "container: $DOCKER_CONTAINER"
echo "debug mode: $DEBUG_MODE"
echo "downloads: $FIREFOX_DOWNLOADS"
echo "jobs: $JOBS"
echo "patches: $PATCHES"
echo "release: $FIREFOX_RELEASE"
echo "src: $FIREFOX_SRC"
docker image inspect "$DOCKER_IMAGE" >/dev/null
if [ $? -ne 0 ]; then
echo "Building $DOCKER_IMAGE"
docker build -t "$DOCKER_IMAGE" --build-arg UID=$(id -u) --build-arg GID=$(id -g) .
fi
if [ ! -d "$FIREFOX_DOWNLOADS" ]; then
mkdir -p "$FIREFOX_DOWNLOADS"
fi
# This will be bind mounted, so it must be absolute
FIREFOX_DOWNLOADS="$(realpath $FIREFOX_DOWNLOADS)"
TZ=$(readlink -f /etc/localtime | sed 's/.*zoneinfo\/\([^"].*\)/\1/')
# X11 stuff to share with container
# The ROS wiki has good examples: http://wiki.ros.org/docker/Tutorials/GUI
#
# If /tmp/.docker.xauth has some permission issue:
# 1. Stop docker
# 2. touch /tmp/.docker.xauth
# 3. Start docker
XSOCK=/tmp/.X11-unix
XAUTH=/tmp/.docker.xauth
if [ -e "$XAUTH" ]; then
if [ ! -w "$XAUTH" ]; then
echo "xauth file \"$XAUTH\" exists but is not writeable"
exit 1
fi
else
touch "$XAUTH"
fi
xauth nlist ${DISPLAY} | sed -e 's/^..../ffff/' | uniq | xauth -f ${XAUTH} nmerge -
docker run -ti \
--name "$DOCKER_CONTAINER" \
--user firefox \
--network=host \
--mount type=bind,target=/home/firefox/Downloads,src=${FIREFOX_DOWNLOADS} \
--device /dev/dri \
--device /dev/snd \
$FIREFOX_SRC_MOUNT_OPT \
--mount type=bind,target=${XSOCK},src=${XSOCK} \
--mount type=bind,target=${XAUTH},src=${XAUTH} \
-e XAUTHORITY=${XAUTH} \
-e DISPLAY=${DISPLAY} \
-e PATCHES=${PATCHES} \
-e FIREFOX_RELEASE=${FIREFOX_RELEASE} \
-e DEBUG_MODE=${DEBUG_MODE} \
-e FIREFOX_SRC=${FIREFOX_SRC} \
-e JOBS=${JOBS} \
-e TZ="${TZ}" \
"$DOCKER_IMAGE"