Fix building bundled sm-91 with clang-16.
[0ad.git] / libraries / source / spidermonkey / build.sh
blob44c852081a50decf3f08d0e256d797c2e92dcc8c
1 #!/bin/sh
2 # This script is called by update-workspaces.sh / build-osx-libraries.sh
3 set -e
5 # This should match the version in config/milestone.txt
6 FOLDER="mozjs-91.13.1"
7 # If same-version changes are needed, increment this.
8 LIB_VERSION="91.13.1+2"
9 LIB_NAME="mozjs91-ps"
11 # Since this script is called by update-workspaces.sh, we want to quickly
12 # avoid doing any work if SpiderMonkey is already built and up-to-date.
13 # Running SM's Makefile is a bit slow and noisy, so instead we'll make a
14 # special file and only rebuild if the build.sh version differs.
15 if [ -e .already-built ] && [ "$(cat .already-built)" = "${LIB_VERSION}" ]
16 then
17 echo "SpiderMonkey is already up to date."
18 exit
21 echo "Building SpiderMonkey..."
22 echo
24 # Prevent the SpiderMonkey build system from reading dependencies from
25 # user-installed python packages.
26 PYTHONNOUSERSITE=true
28 OS="${OS:=$(uname -s)}"
30 # Use Mozilla make on Windows
31 if [ "${OS}" = "Windows_NT" ]
32 then
33 MAKE="mozmake"
34 else
35 MAKE=${MAKE:="make"}
38 MAKE_OPTS="${JOBS}"
40 # Standalone SpiderMonkey can not use jemalloc (see https://bugzilla.mozilla.org/show_bug.cgi?id=1465038)
41 # Jitspew doesn't compile on VS17 in the zydis disassembler - since we don't use it, deactivate it.
42 # Trace-logging doesn't compile for now.
43 CONF_OPTS="--disable-tests
44 --disable-jemalloc
45 --disable-js-shell
46 --without-intl-api
47 --enable-shared-js
48 --disable-jitspew"
50 if [ -n "$PROFILE" ]
51 then
52 CONF_OPTS="$CONF_OPTS --enable-profiling
53 --enable-perf
54 --enable-instruments
55 --enable-jitspew
56 --with-jitreport-granularity=3"
59 if [ "${OS}" = "Windows_NT" ]
60 then
61 CONF_OPTS="${CONF_OPTS} --target=i686"
62 elif [ "${OS}" = "Darwin" ]
63 then
64 # Unless we are forcing an architecture, switch between ARM / x86 based on platform.
65 if [ -z "${ARCH}" ]; then
66 if [ "$(uname -m)" == "arm64" ]
67 then
68 ARCH="aarch64"
69 else
70 ARCH="x86_64"
73 CONF_OPTS="${CONF_OPTS} --target=$ARCH-apple-darwin"
75 # Link to custom-built zlib
76 export PKG_CONFIG_PATH="=${ZLIB_DIR}:${PKG_CONFIG_PATH}"
77 CONF_OPTS="${CONF_OPTS} --with-system-zlib"
78 # Specify target versions and SDK
79 if [ "${MIN_OSX_VERSION}" ] && [ "${MIN_OSX_VERSION-_}" ]; then
80 CONF_OPTS="${CONF_OPTS} --enable-macos-target=$MIN_OSX_VERSION"
82 if [ "${SYSROOT}" ] && [ "${SYSROOT-_}" ]; then
83 CONF_OPTS="${CONF_OPTS} --with-macos-sdk=${SYSROOT}"
87 LLVM_OBJDUMP=${LLVM_OBJDUMP:=$(command -v llvm-objdump || command -v objdump)}
89 # Quick sanity check to print explicit error messages
90 # (Don't run this on windows as it would likely fail spuriously)
91 if [ "${OS}" != "Windows_NT" ]
92 then
93 [ -n "$(command -v rustc)" ] || (echo "Error: rustc is not available. Install the rust toolchain (rust + cargo) before proceeding." && exit 1)
94 [ -n "${LLVM_OBJDUMP}" ] || (echo "Error: LLVM objdump is not available. Install it (likely via LLVM-clang) before proceeding." && exit 1)
97 # If Valgrind looks like it's installed, then set up SM to support it
98 # (else the JITs will interact poorly with it)
99 if [ -e /usr/include/valgrind/valgrind.h ]
100 then
101 CONF_OPTS="${CONF_OPTS} --enable-valgrind"
104 # We need to be able to override CHOST in case it is 32bit userland on 64bit kernel
105 CONF_OPTS="${CONF_OPTS} \
106 ${CBUILD:+--build=${CBUILD}} \
107 ${CHOST:+--host=${CHOST}} \
108 ${CTARGET:+--target=${CTARGET}}"
110 echo "SpiderMonkey build options: ${CONF_OPTS}"
112 # It can occasionally be useful to not rebuild everything, but don't do this by default.
113 REBUILD=${REBUILD:=true}
114 if [ $REBUILD = true ]
115 then
116 # Delete the existing directory to avoid conflicts and extract the tarball
117 rm -rf "$FOLDER"
118 if [ ! -e "${FOLDER}.tar.xz" ]
119 then
120 # The tarball is committed to svn, but it's useful to let jenkins download it (when testing upgrade scripts).
121 download="$(command -v wget || echo "curl -L -o "${FOLDER}.tar.xz"")"
122 $download "https://github.com/wraitii/spidermonkey-tarballs/releases/download/${FOLDER}/${FOLDER}.tar.xz"
124 tar xfJ "${FOLDER}.tar.xz"
126 # Clean up header files that may be left over by earlier versions of SpiderMonkey
127 rm -rf include-unix-debug
128 rm -rf include-unix-release
130 cd "$FOLDER"
132 # Prevent complaining that configure is outdated.
133 touch ./js/src/configure
135 # Apply patches
136 . ../patch.sh
137 else
138 cd "$FOLDER"
141 # Debug version of SM is broken on FreeBSD.
142 if [ "${OS}" != "FreeBSD" ]
143 then
144 mkdir -p build-debug
145 cd build-debug
146 # llvm-objdump is searched for with the complete name, not simply 'objdump', account for that.
147 CXXFLAGS="${CXXFLAGS}" ../js/src/configure \
148 LLVM_OBJDUMP="${LLVM_OBJDUMP}" \
149 ${CONF_OPTS} \
150 --enable-debug \
151 --disable-optimize \
152 --enable-gczeal
153 ${MAKE} ${MAKE_OPTS}
154 cd ..
157 mkdir -p build-release
158 cd build-release
159 CXXFLAGS="${CXXFLAGS}" ../js/src/configure \
160 LLVM_OBJDUMP="${LLVM_OBJDUMP}" \
161 ${CONF_OPTS} \
162 --enable-optimize
163 ${MAKE} ${MAKE_OPTS}
164 cd ..
166 cd ..
168 if [ "${OS}" = "Windows_NT" ]
169 then
170 INCLUDE_DIR_DEBUG=include-win32-debug
171 INCLUDE_DIR_RELEASE=include-win32-release
172 LIB_PREFIX=
173 LIB_SUFFIX=.dll
174 STATIC_LIB_SUFFIX=.lib
175 else
176 INCLUDE_DIR_DEBUG=include-unix-debug
177 INCLUDE_DIR_RELEASE=include-unix-release
178 LIB_PREFIX=lib
179 LIB_SUFFIX=.so
180 STATIC_LIB_SUFFIX=.a
181 if [ "${OS}" = "OpenBSD" ];
182 then
183 LIB_SUFFIX=.so.1.0
184 elif [ "${OS}" = "Darwin" ];
185 then
186 LIB_SUFFIX=.a
190 if [ "${OS}" = "Windows_NT" ]
191 then
192 # Bug #776126
193 # SpiderMonkey uses a tweaked zlib when building, and it wrongly copies its own files to include dirs
194 # afterwards, so we have to remove them to not have them conflicting with the regular zlib
195 pushd "${FOLDER}/build-release/dist/include"
196 rm -f mozzconf.h zconf.h zlib.h
197 popd
198 pushd "${FOLDER}/build-debug/dist/include"
199 rm -f mozzconf.h zconf.h zlib.h
200 popd
203 # Copy files into the necessary locations for building and running the game
205 # js-config.h is different for debug and release builds, so we need different include directories for both
206 mkdir -p "${INCLUDE_DIR_RELEASE}"
207 cp -R -L "${FOLDER}"/build-release/dist/include/* "${INCLUDE_DIR_RELEASE}/"
209 if [ "${OS}" != "FreeBSD" ]; then
210 mkdir -p "${INCLUDE_DIR_DEBUG}"
211 cp -R -L "${FOLDER}"/build-debug/dist/include/* "${INCLUDE_DIR_DEBUG}/"
214 # These align the ligns below, making it easier to check for mistakes.
215 DEB="debug"
216 REL="release"
218 mkdir -p lib/
220 # Fetch the jsrust static library. Path is grepped from the build file as it varies by rust toolset.
221 rust_path=$(grep jsrust < "${FOLDER}/build-release/js/src/build/backend.mk" | cut -d = -f 2 | cut -c2-)
222 cp -L "${rust_path}" "lib/${LIB_PREFIX}${LIB_NAME}-rust${STATIC_LIB_SUFFIX}"
224 if [ "${OS}" = "Darwin" ]
225 then
226 # On MacOS, copy the static libraries only.
227 cp -L "${FOLDER}/build-${DEB}/js/src/build/${LIB_PREFIX}js_static${LIB_SUFFIX}" "lib/${LIB_PREFIX}${LIB_NAME}-${DEB}${LIB_SUFFIX}"
228 cp -L "${FOLDER}/build-${REL}/js/src/build/${LIB_PREFIX}js_static${LIB_SUFFIX}" "lib/${LIB_PREFIX}${LIB_NAME}-${REL}${LIB_SUFFIX}"
229 elif [ "${OS}" = "Windows_NT" ]
230 then
231 # Windows needs DLLs to binaries/, static stubs to lib/ and debug symbols
232 cp -L "${FOLDER}/build-${DEB}/js/src/build/${LIB_PREFIX}${LIB_NAME}-${DEB}${LIB_SUFFIX}" "../../../binaries/system/${LIB_PREFIX}${LIB_NAME}-${DEB}${LIB_SUFFIX}"
233 cp -L "${FOLDER}/build-${REL}/js/src/build/${LIB_PREFIX}${LIB_NAME}-${REL}${LIB_SUFFIX}" "../../../binaries/system/${LIB_PREFIX}${LIB_NAME}-${REL}${LIB_SUFFIX}"
234 cp -L "${FOLDER}/build-${DEB}/js/src/build/${LIB_PREFIX}${LIB_NAME}-${DEB}${STATIC_LIB_SUFFIX}" "lib/${LIB_PREFIX}${LIB_NAME}-${DEB}${STATIC_LIB_SUFFIX}"
235 cp -L "${FOLDER}/build-${REL}/js/src/build/${LIB_PREFIX}${LIB_NAME}-${REL}${STATIC_LIB_SUFFIX}" "lib/${LIB_PREFIX}${LIB_NAME}-${REL}${STATIC_LIB_SUFFIX}"
236 # Copy debug symbols as well.
237 cp -L "${FOLDER}/build-${DEB}/js/src/build/${LIB_PREFIX}${LIB_NAME}-${DEB}.pdb" "../../../binaries/system/${LIB_PREFIX}${LIB_NAME}-${DEB}.pdb"
238 cp -L "${FOLDER}/build-${REL}/js/src/build/${LIB_PREFIX}${LIB_NAME}-${REL}.pdb" "../../../binaries/system/${LIB_PREFIX}${LIB_NAME}-${REL}.pdb"
239 # Copy the debug jsrust library.
240 rust_path=$(grep jsrust < "${FOLDER}/build-debug/js/src/build/backend.mk" | cut -d = -f 2 | cut -c2-)
241 cp -L "${rust_path}" "lib/${LIB_PREFIX}${LIB_NAME}-rust-debug${STATIC_LIB_SUFFIX}"
242 else
243 # Copy shared libs to both lib/ and binaries/ so the compiler and executable (resp.) can find them.
244 cp -L "${FOLDER}/build-${REL}/js/src/build/${LIB_PREFIX}${LIB_NAME}-${REL}${LIB_SUFFIX}" "lib/${LIB_PREFIX}${LIB_NAME}-${REL}${LIB_SUFFIX}"
245 cp -L "${FOLDER}/build-${REL}/js/src/build/${LIB_PREFIX}${LIB_NAME}-${REL}${LIB_SUFFIX}" "../../../binaries/system/${LIB_PREFIX}${LIB_NAME}-${REL}${LIB_SUFFIX}"
246 if [ "${OS}" != "FreeBSD" ]; then
247 cp -L "${FOLDER}/build-${DEB}/js/src/build/${LIB_PREFIX}${LIB_NAME}-${DEB}${LIB_SUFFIX}" "../../../binaries/system/${LIB_PREFIX}${LIB_NAME}-${DEB}${LIB_SUFFIX}"
248 cp -L "${FOLDER}/build-${DEB}/js/src/build/${LIB_PREFIX}${LIB_NAME}-${DEB}${LIB_SUFFIX}" "lib/${LIB_PREFIX}${LIB_NAME}-${DEB}${LIB_SUFFIX}"
252 # Flag that it's already been built successfully so we can skip it next time
253 echo "${LIB_VERSION}" > .already-built