-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
166 lines (139 loc) · 3.71 KB
/
entrypoint.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
166
#!/bin/bash
#
# SPDX-License-Identifier: Apache-2.0
if [ "$DEBUG_MODE" -eq 1 ]; then
set +x
fi
export ARCH="$(uname -m)"
export HOME="/home/firefox/"
export MOZDIR="mozilla-unified"
export DEBUG_MODE="$DEBUG_MODE"
rc=0
drop_to_shell() {
/bin/bash
exit $rc
}
do_exit() {
rc=$1
if [ "$DEBUG_MODE" -eq 1 ]; then
drop_to_shell $rc
else
exit $rc
fi
}
cd "$HOME"
#
# Try to run firefox in case this container is being re-used
#
echo "Checking if Firefox already exists in this container... "
if [ -d "$MOZDIR" ]; then
echo "Found something! Trying it out..."
cd "$MOZDIR"
if ! ./mach run ; then
do_exit 1
else
echo "Firefox found, but cannot run. This container may be corrupted."
do_exit 1
fi
fi
echo "Nope, proceeding to build Firefox."
#
# Clone or copy the repo
#
if ! which hg >/dev/null ; then
echo "Installing Mercurial"
python3 -m pip install --user mercurial
fi
source ${HOME}/.bashrc
# Get firefox sources
if [ -n "$FIREFOX_SRC" ]; then
echo "Copying Firefox sources from $FIREFOX_SRC to ${MOZDIR}/"
# use --delete in case we're re-using an old firefox dir (e.g. in debug mode)
rsync -a --delete "${FIREFOX_SRC}/" "${MOZDIR}/"
else
echo "Cloning Firefox from upstream"
sh clone_firefox.sh
fi
#
# Install build dependencies
#
if ! which node >/dev/null ; then
echo "Installing Node"
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
nvm install --lts
fi
if ! which rustc >/dev/null ; then
echo "Installing Rust packages"
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o rustup.sh
sh rustup.sh -y
source "$HOME/.cargo/env"
rustup target add wasm32-wasi
cargo install just
cargo install --force cbindgen
fi
#
# Pull, patch, and build.
#
cd "${MOZDIR}/"
# Copy and modify mozconfig
cp "${HOME}/mozconfigs/mozconfig-${ARCH}" mozconfig
# wasi sysroot
echo "wasi_sysroot=${HOME}/.mozbuild/sysroot-wasm32-wasi/" >> mozconfig
# Set level of parallelism. Assume ~1GB per build job.
# Parallel build jobs. To avoid running out of memory (leading to confusing
# build failures) or making the system unresponsive, pick the lesser of:
# - One-half the available memory in GiB (assume up to 2GiB per job)
# - Number of CPU threads minus 1
if [ -z "$JOBS" ]; then
mem=$(($(grep 'MemAvailable' /proc/meminfo | awk '{ print $2 }') / 1024 / 1024 / 2))
num_threads=$(nproc --ignore=1)
echo "Available memory (GiB) / 2: $mem"
echo "Number of threads: $num_threads"
if [ "$mem" -lt "$num_threads" ]; then
JOBS="$mem"
else
JOBS="$num_threads"
fi
fi
if [ "$JOBS" -eq 0 ]; then
echo "Not enough resources to continue."
do_exit 1
fi
echo "Parallel build Jobs: $JOBS"
echo "export CARGO_BUILD_JOBS=${JOBS}" >> mozconfig
echo "mk_add_options MOZ_MAKE_FLAGS=-j${JOBS}" >> mozconfig
# Bootstrapping needs to be done before patching since some patches touch build/
echo "Bootstrapping firefox"
hg update -r "$FIREFOX_RELEASE"
# .mozbuild directory needs to exist or `mach bootstrap` will prompt for a
# location to store build state
mkdir "${HOME}/.mozbuild"
./mach bootstrap --no-system-changes --application-choice=browser
if [ -n "$PATCHES" ]; then
patchdir="${HOME}/${PATCHES}"
if [ ! -d "$patchdir" ]; then
echo "Patches directory \"$patchdir\" does not exist"
do_exit 1
fi
echo "Applying patches from $patchdir"
for p in ${patchdir}/*.{patch,diff} ; do
echo "Applying $p"
patch -p1 < "$p"
done
fi
echo "Configuring firefox"
./mach configure
if [ $? -ne 0 ]; then
do_exit $?
fi
echo "Building firefox"
./mach build
if [ $? -ne 0 ]; then
do_exit $?
fi
# At last!
./mach run
do_exit $?