ldc: update to 1.5 release (#1731)
[termux-packages] / build-package.sh
CommitLineData
59f0d218
FF
1#!/bin/bash
2
59f0d218
FF
3set -e -o pipefail -u
4
7b1f1bd3
FF
5# Utility function to log an error message and exit with an error code.
6termux_error_exit() {
7 echo "ERROR: $*" 1>&2
a0057943 8 exit 1
7b1f1bd3 9}
2c1aa558 10
7b1f1bd3 11# Utility function to download a resource, optionally checking against a checksum.
19fb617a 12termux_download() {
7b1f1bd3
FF
13 local URL="$1"
14 local DESTINATION="$2"
c9d4e74f
FF
15
16 if [ -f "$DESTINATION" ] && [ $# = 3 ] && [ -n "$3" ]; then
17 # Keep existing file if checksum matches.
18 local EXISTING_CHECKSUM
19 EXISTING_CHECKSUM=$(sha256sum "$DESTINATION" | cut -f 1 -d ' ')
20 if [ "$EXISTING_CHECKSUM" = "$3" ]; then return; fi
21 fi
22
7b1f1bd3
FF
23 local TMPFILE
24 TMPFILE=$(mktemp "$TERMUX_PKG_TMPDIR/download.$TERMUX_PKG_NAME.XXXXXXXXX")
e239964b 25 echo "Downloading ${URL}"
7b1f1bd3 26 local TRYMAX=6
6ad2991c 27 for try in $(seq 1 $TRYMAX); do
e239964b 28 if curl -L --fail --retry 2 -o "$TMPFILE" "$URL"; then
7b1f1bd3
FF
29 local ACTUAL_CHECKSUM
30 ACTUAL_CHECKSUM=$(sha256sum "$TMPFILE" | cut -f 1 -d ' ')
31 if [ $# = 3 ] && [ -n "$3" ]; then
e239964b 32 # Optional checksum argument:
aea30235 33 local EXPECTED=$3
7b1f1bd3 34 if [ "$EXPECTED" != "$ACTUAL_CHECKSUM" ]; then
a75313ee
FF
35 >&2 printf "Wrong checksum for %s:\nExpected: %s\nActual: %s\n" \
36 "$URL" "$EXPECTED" "$ACTUAL_CHECKSUM"
aea30235
FF
37 exit 1
38 fi
e239964b 39 else
a75313ee
FF
40 printf "No validation of checksum for %s:\nActual: %s\n" \
41 "$URL" "$ACTUAL_CHECKSUM"
e239964b
FF
42 fi
43 mv "$TMPFILE" "$DESTINATION"
44 return
45 else
7b1f1bd3 46 echo "Download of $URL failed (attempt $try/$TRYMAX)" 1>&2
e239964b
FF
47 sleep 45
48 fi
49 done
50
7b1f1bd3
FF
51 termux_error_exit "Failed to download $URL"
52}
53
54# Utility function for golang-using packages to setup a go toolchain.
01e699d9 55termux_setup_golang() {
7b1f1bd3
FF
56 export GOOS=android
57 export CGO_ENABLED=1
58 export GO_LDFLAGS="-extldflags=-pie"
59 if [ "$TERMUX_ARCH" = "arm" ]; then
60 export GOARCH=arm
61 export GOARM=7
62 elif [ "$TERMUX_ARCH" = "i686" ]; then
63 export GOARCH=386
64 export GO386=sse2
65 elif [ "$TERMUX_ARCH" = "aarch64" ]; then
66 export GOARCH=arm64
67 elif [ "$TERMUX_ARCH" = "x86_64" ]; then
68 export GOARCH=amd64
69 else
70 termux_error_exit "Unsupported arch: $TERMUX_ARCH"
71 fi
72
e23b8a1f 73 local TERMUX_GO_VERSION=go1.9.1
7b1f1bd3 74 local TERMUX_GO_PLATFORM=linux-amd64
7b1f1bd3 75
538812ae 76 local TERMUX_BUILDGO_FOLDER=$TERMUX_COMMON_CACHEDIR/${TERMUX_GO_VERSION}
7b1f1bd3
FF
77 export GOROOT=$TERMUX_BUILDGO_FOLDER
78 export PATH=$GOROOT/bin:$PATH
79
80 if [ -d "$TERMUX_BUILDGO_FOLDER" ]; then return; fi
81
82 local TERMUX_BUILDGO_TAR=$TERMUX_COMMON_CACHEDIR/${TERMUX_GO_VERSION}.${TERMUX_GO_PLATFORM}.tar.gz
83 rm -Rf "$TERMUX_COMMON_CACHEDIR/go" "$TERMUX_BUILDGO_FOLDER"
84 termux_download https://storage.googleapis.com/golang/${TERMUX_GO_VERSION}.${TERMUX_GO_PLATFORM}.tar.gz \
8c9b8a9a 85 "$TERMUX_BUILDGO_TAR" \
e23b8a1f 86 07d81c6b6b4c2dcf1b5ef7c27aaebd3691cdb40548500941f92b221147c5d9c7
36c4069c 87
7b1f1bd3
FF
88 ( cd "$TERMUX_COMMON_CACHEDIR"; tar xf "$TERMUX_BUILDGO_TAR"; mv go "$TERMUX_BUILDGO_FOLDER"; rm "$TERMUX_BUILDGO_TAR" )
89}
90
f0c1439b
FF
91# Utility function for cmake-built packages to setup a current ninja.
92termux_setup_ninja() {
5f03e364 93 local NINJA_VERSION=1.8.2
f0c1439b
FF
94 local NINJA_FOLDER=$TERMUX_COMMON_CACHEDIR/ninja-$NINJA_VERSION
95 if [ ! -x $NINJA_FOLDER/ninja ]; then
96 mkdir -p $NINJA_FOLDER
97 local NINJA_ZIP_FILE=$TERMUX_PKG_TMPDIR/ninja-$NINJA_VERSION.zip
98 termux_download https://github.com/ninja-build/ninja/releases/download/v$NINJA_VERSION/ninja-linux.zip \
99 $NINJA_ZIP_FILE \
5f03e364 100 d2fea9ff33b3ef353161ed906f260d565ca55b8ca0568fa07b1d2cab90a84a07
f0c1439b
FF
101 unzip $NINJA_ZIP_FILE -d $NINJA_FOLDER
102 fi
103 export PATH=$NINJA_FOLDER:$PATH
104}
105
106# Utility function for cmake-built packages to setup a current meson.
107termux_setup_meson() {
108 termux_setup_ninja
f2d612ab 109 local MESON_VERSION=0.43.0
f0c1439b
FF
110 local MESON_FOLDER=$TERMUX_COMMON_CACHEDIR/meson-$MESON_VERSION
111 if [ ! -d "$MESON_FOLDER" ]; then
112 local MESON_TAR_NAME=meson-$MESON_VERSION.tar.gz
113 local MESON_TAR_FILE=$TERMUX_PKG_TMPDIR/$MESON_TAR_NAME
114 termux_download \
115 https://github.com/mesonbuild/meson/releases/download/$MESON_VERSION/meson-$MESON_VERSION.tar.gz \
116 $MESON_TAR_FILE \
f2d612ab 117 c513eca90e0d70bf14cd1eaafea2fa91cf40a73326a7ff61f08a005048057340
f0c1439b
FF
118 tar xf "$MESON_TAR_FILE" -C "$TERMUX_COMMON_CACHEDIR"
119 (cd $MESON_FOLDER && patch -p1 < $TERMUX_SCRIPTDIR/scripts/meson-android.patch)
120 fi
121 TERMUX_MESON="$MESON_FOLDER/meson.py"
f2d612ab 122 TERMUX_MESON_CROSSFILE=$TERMUX_COMMON_CACHEDIR/meson-crossfile-$TERMUX_ARCH-v2.txt
f0c1439b
FF
123 if [ ! -f $TERMUX_MESON_CROSSFILE ]; then
124 local MESON_CPU MESON_CPU_FAMILY
125 if [ $TERMUX_ARCH = "arm" ]; then
126 MESON_CPU_FAMILY="arm"
127 MESON_CPU="armv7"
128 elif [ $TERMUX_ARCH = "i686" ]; then
129 MESON_CPU_FAMILY="x86"
130 MESON_CPU="i686"
131 elif [ $TERMUX_ARCH = "x86_64" ]; then
132 MESON_CPU_FAMILY="x86_64"
133 MESON_CPU="x86_64"
134 elif [ $TERMUX_ARCH = "aarch64" ]; then
135 MESON_CPU_FAMILY="arm"
136 MESON_CPU="aarch64"
137 else
138 termux_error_exit "Unsupported arch: $TERMUX_ARCH"
139 fi
140
141 cat > $TERMUX_MESON_CROSSFILE <<-HERE
142 [binaries]
143 ar = '$AR'
144 c = '$CC'
145 cpp = '$CXX'
146 ld = '$LD'
f2d612ab 147 pkgconfig = '$PKG_CONFIG'
f0c1439b
FF
148 strip = '$STRIP'
149 [properties]
150 needs_exe_wrapper = true
151 [host_machine]
152 cpu_family = '$MESON_CPU_FAMILY'
153 cpu = '$MESON_CPU'
154 endian = 'little'
155 system = 'android'
156 HERE
157 fi
158}
159
bb8baaa2
FF
160# Utility function for cmake-built packages to setup a current cmake.
161termux_setup_cmake() {
d946e67c 162 local TERMUX_CMAKE_MAJORVESION=3.9
71d21065 163 local TERMUX_CMAKE_MINORVERSION=4
bb8baaa2
FF
164 local TERMUX_CMAKE_VERSION=$TERMUX_CMAKE_MAJORVESION.$TERMUX_CMAKE_MINORVERSION
165 local TERMUX_CMAKE_TARNAME=cmake-${TERMUX_CMAKE_VERSION}-Linux-x86_64.tar.gz
166 local TERMUX_CMAKE_TARFILE=$TERMUX_PKG_TMPDIR/$TERMUX_CMAKE_TARNAME
167 local TERMUX_CMAKE_FOLDER=$TERMUX_COMMON_CACHEDIR/cmake-$TERMUX_CMAKE_VERSION
7048a1f2 168 if [ ! -d "$TERMUX_CMAKE_FOLDER" ]; then
bb8baaa2 169 termux_download https://cmake.org/files/v$TERMUX_CMAKE_MAJORVESION/$TERMUX_CMAKE_TARNAME \
7048a1f2 170 "$TERMUX_CMAKE_TARFILE" \
71d21065 171 6d8573377fc7fca86ed1952b76f62880e2159a3de6508761dd0d619a8e96551b
7048a1f2
FF
172 rm -Rf "$TERMUX_PKG_TMPDIR/cmake-${TERMUX_CMAKE_VERSION}-Linux-x86_64"
173 tar xf "$TERMUX_CMAKE_TARFILE" -C "$TERMUX_PKG_TMPDIR"
174 mv "$TERMUX_PKG_TMPDIR/cmake-${TERMUX_CMAKE_VERSION}-Linux-x86_64" \
175 "$TERMUX_CMAKE_FOLDER"
bb8baaa2
FF
176 fi
177 export PATH=$TERMUX_CMAKE_FOLDER/bin:$PATH
16087461 178 export CMAKE_INSTALL_ALWAYS=1
bb8baaa2
FF
179}
180
7b1f1bd3
FF
181# First step is to handle command-line arguments. Not to be overridden by packages.
182termux_step_handle_arguments() {
183 # shellcheck source=/dev/null
184 test -f "$HOME/.termuxrc" && source "$HOME/.termuxrc"
185
186 # Handle command-line arguments:
187 _show_usage () {
cf575909 188 echo "Usage: ./build-package.sh [-a ARCH] [-d] [-D] [-f] [-q] [-s] PACKAGE"
7b1f1bd3
FF
189 echo "Build a package by creating a .deb file in the debs/ folder."
190 echo " -a The architecture to build for: aarch64(default), arm, i686, x86_64 or all."
191 echo " -d Build with debug symbols."
192 echo " -D Build a disabled package in disabled-packages/."
bc0560aa 193 echo " -f Force build even if package has already been built."
cf575909 194 echo " -q Quiet build"
7b1f1bd3
FF
195 echo " -s Skip dependency check."
196 exit 1
197 }
cf575909 198 while getopts :a:hdDfqs option; do
7b1f1bd3
FF
199 case "$option" in
200 a) TERMUX_ARCH="$OPTARG";;
201 h) _show_usage;;
202 d) TERMUX_DEBUG=true;;
203 D) local TERMUX_IS_DISABLED=true;;
bc0560aa 204 f) TERMUX_FORCE_BUILD=true;;
cf575909 205 q) export TERMUX_QUIET_BUILD=true;;
7b1f1bd3
FF
206 s) export TERMUX_SKIP_DEPCHECK=true;;
207 ?) termux_error_exit "./build-package.sh: illegal option -$OPTARG";;
208 esac
209 done
210 shift $((OPTIND-1))
211
212 if [ "$#" -ne 1 ]; then _show_usage; fi
213 unset -f _show_usage
214
215 # Handle 'all' arch:
216 if [ -n "${TERMUX_ARCH+x}" ] && [ "${TERMUX_ARCH}" = 'all' ]; then
adc0d548 217 for arch in 'aarch64' 'arm' 'i686' 'x86_64'; do
bc0560aa 218 ./build-package.sh ${TERMUX_FORCE_BUILD+-f} -a $arch "$1"
7b1f1bd3
FF
219 done
220 exit
221 fi
222
223 # Check the package to build:
224 TERMUX_PKG_NAME=$(basename "$1")
225 export TERMUX_SCRIPTDIR
226 TERMUX_SCRIPTDIR=$(cd "$(dirname "$0")"; pwd)
227 if [[ $1 == *"/"* ]]; then
228 # Path to directory which may be outside this repo:
229 if [ ! -d "$1" ]; then termux_error_exit "'$1' seems to be a path but is not a directory"; fi
230 export TERMUX_PKG_BUILDER_DIR
231 TERMUX_PKG_BUILDER_DIR=$(realpath "$1")
06b7f2ca
FF
232 # Skip depcheck for external package:
233 TERMUX_SKIP_DEPCHECK=true
7b1f1bd3
FF
234 else
235 # Package name:
236 if [ -n "${TERMUX_IS_DISABLED=""}" ]; then
237 export TERMUX_PKG_BUILDER_DIR=$TERMUX_SCRIPTDIR/disabled-packages/$TERMUX_PKG_NAME
238 else
239 export TERMUX_PKG_BUILDER_DIR=$TERMUX_SCRIPTDIR/packages/$TERMUX_PKG_NAME
240 fi
241 fi
242 TERMUX_PKG_BUILDER_SCRIPT=$TERMUX_PKG_BUILDER_DIR/build.sh
243 if test ! -f "$TERMUX_PKG_BUILDER_SCRIPT"; then
244 termux_error_exit "No build.sh script at package dir $TERMUX_PKG_BUILDER_DIR!"
245 fi
246}
247
248# Setup variables used by the build. Not to be overridden by packages.
249termux_step_setup_variables() {
250 : "${ANDROID_HOME:="${HOME}/lib/android-sdk"}"
251 : "${NDK:="${HOME}/lib/android-ndk"}"
4428ae3c 252 : "${TERMUX_MAKE_PROCESSES:="$(nproc)"}"
7b1f1bd3
FF
253 : "${TERMUX_TOPDIR:="$HOME/.termux-build"}"
254 : "${TERMUX_ARCH:="aarch64"}" # arm, aarch64, i686 or x86_64.
255 : "${TERMUX_PREFIX:="/data/data/com.termux/files/usr"}"
256 : "${TERMUX_ANDROID_HOME:="/data/data/com.termux/files/home"}"
257 : "${TERMUX_DEBUG:=""}"
272404e1 258 : "${TERMUX_PKG_API_LEVEL:="21"}"
56601c84 259 : "${TERMUX_ANDROID_BUILD_TOOLS_VERSION:="26.0.1"}"
829516f1 260 : "${TERMUX_NDK_VERSION:="15.2"}"
7b1f1bd3 261
10b023b8 262 if [ "x86_64" = "$TERMUX_ARCH" ] || [ "aarch64" = "$TERMUX_ARCH" ]; then
7b1f1bd3
FF
263 TERMUX_ARCH_BITS=64
264 else
265 TERMUX_ARCH_BITS=32
266 fi
267
268 TERMUX_HOST_PLATFORM="${TERMUX_ARCH}-linux-android"
269 if [ "$TERMUX_ARCH" = "arm" ]; then TERMUX_HOST_PLATFORM="${TERMUX_HOST_PLATFORM}eabi"; fi
270
271 if [ ! -d "$NDK" ]; then
272 termux_error_exit 'NDK not pointing at a directory!'
273 fi
274 if ! grep -s -q "Pkg.Revision = $TERMUX_NDK_VERSION" "$NDK/source.properties"; then
275 termux_error_exit "Wrong NDK version - we need $TERMUX_NDK_VERSION"
276 fi
277
2b0e9dc9
FF
278 # The build tuple that may be given to --build configure flag:
279 TERMUX_BUILD_TUPLE=$(sh "$TERMUX_SCRIPTDIR/scripts/config.guess")
280
7b1f1bd3
FF
281 # We do not put all of build-tools/$TERMUX_ANDROID_BUILD_TOOLS_VERSION/ into PATH
282 # to avoid stuff like arm-linux-androideabi-ld there to conflict with ones from
283 # the standalone toolchain.
284 TERMUX_DX=$ANDROID_HOME/build-tools/$TERMUX_ANDROID_BUILD_TOOLS_VERSION/dx
7b1f1bd3
FF
285
286 TERMUX_COMMON_CACHEDIR="$TERMUX_TOPDIR/_cache"
287 TERMUX_DEBDIR="$TERMUX_SCRIPTDIR/debs"
288 TERMUX_ELF_CLEANER=$TERMUX_COMMON_CACHEDIR/termux-elf-cleaner
289
7b1f1bd3
FF
290 export prefix=${TERMUX_PREFIX}
291 export PREFIX=${TERMUX_PREFIX}
7b1f1bd3
FF
292
293 TERMUX_PKG_BUILDDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/build
294 TERMUX_PKG_CACHEDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/cache
295 TERMUX_PKG_MASSAGEDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/massage
296 TERMUX_PKG_PACKAGEDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/package
297 TERMUX_PKG_SRCDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/src
298 TERMUX_PKG_SHA256=""
299 TERMUX_PKG_TMPDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/tmp
300 TERMUX_PKG_HOSTBUILD_DIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/host-build
301 TERMUX_PKG_PLATFORM_INDEPENDENT=""
302 TERMUX_PKG_NO_DEVELSPLIT=""
af4dc416 303 TERMUX_PKG_REVISION="0" # http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version
7b1f1bd3
FF
304 TERMUX_PKG_EXTRA_CONFIGURE_ARGS=""
305 TERMUX_PKG_EXTRA_HOSTBUILD_CONFIGURE_ARGS=""
306 TERMUX_PKG_EXTRA_MAKE_ARGS=""
307 TERMUX_PKG_BUILD_IN_SRC=""
308 TERMUX_PKG_RM_AFTER_INSTALL=""
9ec05091 309 TERMUX_PKG_BREAKS="" # https://www.debian.org/doc/debian-policy/ch-relationships.html#s-binarydeps
7b1f1bd3 310 TERMUX_PKG_DEPENDS=""
45d85e7f 311 TERMUX_PKG_BUILD_DEPENDS=""
7b1f1bd3
FF
312 TERMUX_PKG_HOMEPAGE=""
313 TERMUX_PKG_DESCRIPTION="FIXME:Add description"
7b1f1bd3
FF
314 TERMUX_PKG_KEEP_STATIC_LIBRARIES="false"
315 TERMUX_PKG_ESSENTIAL=""
316 TERMUX_PKG_CONFLICTS="" # https://www.debian.org/doc/debian-policy/ch-relationships.html#s-conflicts
5f217847 317 TERMUX_PKG_RECOMMENDS="" # https://www.debian.org/doc/debian-policy/ch-relationships.html#s-binarydeps
7b1f1bd3
FF
318 TERMUX_PKG_REPLACES=""
319 TERMUX_PKG_CONFFILES=""
320 TERMUX_PKG_INCLUDE_IN_DEVPACKAGE=""
321 TERMUX_PKG_DEVPACKAGE_DEPENDS=""
322 # Set if a host build should be done in TERMUX_PKG_HOSTBUILD_DIR:
323 TERMUX_PKG_HOSTBUILD=""
324 TERMUX_PKG_MAINTAINER="Fredrik Fornwall @fornwall"
2f0de587 325 TERMUX_PKG_CLANG=yes # does nothing for cmake based packages. clang is chosen by cmake
58f7a21c 326 TERMUX_PKG_FORCE_CMAKE=no # if the package has autotools as well as cmake, then set this to prefer cmake
7b1f1bd3
FF
327
328 unset CFLAGS CPPFLAGS LDFLAGS CXXFLAGS
329}
330
331# Save away and restore build setups which may change between builds.
332termux_step_handle_buildarch() {
333 # If $TERMUX_PREFIX already exists, it may have been built for a different arch
334 local TERMUX_ARCH_FILE=/data/TERMUX_ARCH
335 if [ -f "${TERMUX_ARCH_FILE}" ]; then
336 local TERMUX_PREVIOUS_ARCH
337 TERMUX_PREVIOUS_ARCH=$(cat $TERMUX_ARCH_FILE)
338 if [ "$TERMUX_PREVIOUS_ARCH" != "$TERMUX_ARCH" ]; then
339 local TERMUX_DATA_BACKUPDIRS=$TERMUX_TOPDIR/_databackups
340 mkdir -p "$TERMUX_DATA_BACKUPDIRS"
341 local TERMUX_DATA_PREVIOUS_BACKUPDIR=$TERMUX_DATA_BACKUPDIRS/$TERMUX_PREVIOUS_ARCH
342 local TERMUX_DATA_CURRENT_BACKUPDIR=$TERMUX_DATA_BACKUPDIRS/$TERMUX_ARCH
343 # Save current /data (removing old backup if any)
344 if test -e "$TERMUX_DATA_PREVIOUS_BACKUPDIR"; then
345 termux_error_exit "Directory already exists"
346 fi
d7310004
FF
347 if [ -d /data/data ]; then
348 mv /data/data "$TERMUX_DATA_PREVIOUS_BACKUPDIR"
349 fi
7b1f1bd3
FF
350 # Restore new one (if any)
351 if [ -d "$TERMUX_DATA_CURRENT_BACKUPDIR" ]; then
352 mv "$TERMUX_DATA_CURRENT_BACKUPDIR" /data/data
353 fi
354 fi
355 fi
356
357 # Keep track of current arch we are building for.
358 echo "$TERMUX_ARCH" > $TERMUX_ARCH_FILE
19fb617a
FF
359}
360
7b1f1bd3
FF
361# Source the package build script and start building. No to be overridden by packages.
362termux_step_start_build() {
363 # shellcheck source=/dev/null
364 source "$TERMUX_PKG_BUILDER_SCRIPT"
19fb617a 365
28c3a74a
FF
366 TERMUX_STANDALONE_TOOLCHAIN="$TERMUX_TOPDIR/_lib/${TERMUX_NDK_VERSION}-${TERMUX_ARCH}-${TERMUX_PKG_API_LEVEL}"
367 # Bump the below version if a change is made in toolchain setup to ensure
368 # that everyone gets an updated toolchain:
b461954b 369 TERMUX_STANDALONE_TOOLCHAIN+="-v14"
28c3a74a 370
7b1f1bd3
FF
371 if [ -n "${TERMUX_PKG_BLACKLISTED_ARCHES:=""}" ] && [ "$TERMUX_PKG_BLACKLISTED_ARCHES" != "${TERMUX_PKG_BLACKLISTED_ARCHES/$TERMUX_ARCH/}" ]; then
372 echo "Skipping building $TERMUX_PKG_NAME for arch $TERMUX_ARCH"
373 exit 0
374 fi
375
376 if [ -z "${TERMUX_SKIP_DEPCHECK:=""}" ]; then
93ae139b
FF
377 local p TERMUX_ALL_DEPS
378 TERMUX_ALL_DEPS=$(./scripts/buildorder.py "$TERMUX_PKG_NAME")
379 for p in $TERMUX_ALL_DEPS; do
7b1f1bd3
FF
380 if [ "$p" != "$TERMUX_PKG_NAME" ]; then
381 echo "Building dependency $p if necessary..."
382 ./build-package.sh -a $TERMUX_ARCH -s "$p"
383 fi
384 done
385 fi
2c1aa558 386
7b1f1bd3 387 TERMUX_PKG_FULLVERSION=$TERMUX_PKG_VERSION
af4dc416 388 if [ "$TERMUX_PKG_REVISION" != "0" ] || [ "$TERMUX_PKG_FULLVERSION" != "${TERMUX_PKG_FULLVERSION/-/}" ]; then
7b1f1bd3 389 # "0" is the default revision, so only include it if the upstream versions contains "-" itself
af4dc416 390 TERMUX_PKG_FULLVERSION+="-$TERMUX_PKG_REVISION"
7b1f1bd3 391 fi
59f0d218 392
bc0560aa
FF
393 if [ -z "$TERMUX_DEBUG" ] &&
394 [ -z "${TERMUX_FORCE_BUILD+x}" ] &&
395 [ -e "/data/data/.built-packages/$TERMUX_PKG_NAME" ]; then
7b1f1bd3
FF
396 if [ "$(cat "/data/data/.built-packages/$TERMUX_PKG_NAME")" = "$TERMUX_PKG_FULLVERSION" ]; then
397 echo "$TERMUX_PKG_NAME@$TERMUX_PKG_FULLVERSION built - skipping (rm /data/data/.built-packages/$TERMUX_PKG_NAME to force rebuild)"
398 exit 0
399 fi
400 fi
401
402 # Cleanup old state:
403 rm -Rf "$TERMUX_PKG_BUILDDIR" \
404 "$TERMUX_PKG_PACKAGEDIR" \
405 "$TERMUX_PKG_SRCDIR" \
406 "$TERMUX_PKG_TMPDIR" \
407 "$TERMUX_PKG_MASSAGEDIR"
408
409 # Ensure folders present (but not $TERMUX_PKG_SRCDIR, it will be created in build)
410 mkdir -p "$TERMUX_COMMON_CACHEDIR" \
411 "$TERMUX_DEBDIR" \
412 "$TERMUX_PKG_BUILDDIR" \
413 "$TERMUX_PKG_PACKAGEDIR" \
414 "$TERMUX_PKG_TMPDIR" \
415 "$TERMUX_PKG_CACHEDIR" \
416 "$TERMUX_PKG_MASSAGEDIR" \
7b1f1bd3
FF
417 $TERMUX_PREFIX/{bin,etc,lib,libexec,share,tmp,include}
418
419 # Make $TERMUX_PREFIX/bin/sh executable on the builder, so that build
420 # scripts can assume that it works on both builder and host later on:
421 ln -f -s /bin/sh "$TERMUX_PREFIX/bin/sh"
422
bd54a46a
FF
423 local TERMUX_ELF_CLEANER_SRC=$TERMUX_COMMON_CACHEDIR/termux-elf-cleaner.cpp
424 local TERMUX_ELF_CLEANER_VERSION=$(bash -c ". $TERMUX_SCRIPTDIR/packages/termux-elf-cleaner/build.sh; echo \$TERMUX_PKG_VERSION")
425 termux_download \
426 https://raw.githubusercontent.com/termux/termux-elf-cleaner/v$TERMUX_ELF_CLEANER_VERSION/termux-elf-cleaner.cpp \
427 $TERMUX_ELF_CLEANER_SRC \
428 11a38372f4d0e36b7556382c7ecffecae35cee8b68daaee2dbee025f758e17ee
7b1f1bd3
FF
429 if [ "$TERMUX_ELF_CLEANER_SRC" -nt "$TERMUX_ELF_CLEANER" ]; then
430 g++ -std=c++11 -Wall -Wextra -pedantic -Os "$TERMUX_ELF_CLEANER_SRC" -o "$TERMUX_ELF_CLEANER"
431 fi
432
433 if [ -n "$TERMUX_PKG_BUILD_IN_SRC" ]; then
434 echo "Building in src due to TERMUX_PKG_BUILD_IN_SRC being set" > "$TERMUX_PKG_BUILDDIR/BUILDING_IN_SRC.txt"
435 TERMUX_PKG_BUILDDIR=$TERMUX_PKG_SRCDIR
436 fi
437
438 echo "termux - building $TERMUX_PKG_NAME for arch $TERMUX_ARCH..."
439 test -t 1 && printf "\033]0;%s...\007" "$TERMUX_PKG_NAME"
440
0dc0e842
FF
441 # Avoid exporting PKG_CONFIG_LIBDIR until after termux_step_host_build.
442 export TERMUX_PKG_CONFIG_LIBDIR=$TERMUX_PREFIX/lib/pkgconfig
443 # Add a pkg-config file for the system zlib.
b833ad7a 444 mkdir -p "$TERMUX_PKG_CONFIG_LIBDIR"
0dc0e842 445 cat > "$TERMUX_PKG_CONFIG_LIBDIR/zlib.pc" <<-HERE
7b1f1bd3
FF
446 Name: zlib
447 Description: zlib compression library
448 Version: 1.2.8
449 Requires:
450 Libs: -lz
451 HERE
452
453 # Keep track of when build started so we can see what files have been created.
454 # We start by sleeping so that any generated files above (such as zlib.pc) get
455 # an older timestamp than the TERMUX_BUILD_TS_FILE.
456 sleep 1
457 TERMUX_BUILD_TS_FILE=$TERMUX_PKG_TMPDIR/timestamp_$TERMUX_PKG_NAME
458 touch "$TERMUX_BUILD_TS_FILE"
459}
460
461# Run just after sourcing $TERMUX_PKG_BUILDER_SCRIPT. May be overridden by packages.
01e699d9 462termux_step_extract_package() {
7b1f1bd3
FF
463 if [ -z "${TERMUX_PKG_SRCURL:=""}" ]; then
464 mkdir -p "$TERMUX_PKG_SRCDIR"
465 return
466 fi
467 cd "$TERMUX_PKG_TMPDIR"
468 local filename
469 filename=$(basename "$TERMUX_PKG_SRCURL")
470 local file="$TERMUX_PKG_CACHEDIR/$filename"
c9d4e74f 471 termux_download "$TERMUX_PKG_SRCURL" "$file" "$TERMUX_PKG_SHA256"
e239964b 472
1b3f3ebd
FF
473 local folder
474 set +o pipefail
7b1f1bd3 475 if [ "${file##*.}" = zip ]; then
1b3f3ebd
FF
476 folder=`unzip -qql "$file" | head -n1 | tr -s ' ' | cut -d' ' -f5-`
477 rm -Rf $folder
7b1f1bd3 478 unzip -q "$file"
59f0d218 479 else
bdd74219 480 folder=`tar tf "$file" | head -1 | sed 's/^.\///' | sed -e 's/\/.*//'`
1b3f3ebd 481 rm -Rf $folder
2f122eed 482 tar xf "$file"
59f0d218 483 fi
1b3f3ebd 484 set -o pipefail
7b1f1bd3 485 mv $folder "$TERMUX_PKG_SRCDIR"
59f0d218
FF
486}
487
7b1f1bd3 488# Hook for packages to act just after the package has been extracted.
508fa73c 489# Invoked in $TERMUX_PKG_SRCDIR.
01e699d9 490termux_step_post_extract_package() {
59f0d218
FF
491 return
492}
493
7b1f1bd3
FF
494# Optional host build. Not to be overridden by packages.
495termux_step_handle_hostbuild() {
496 if [ "x$TERMUX_PKG_HOSTBUILD" = "x" ]; then return; fi
93ae139b 497
7b1f1bd3
FF
498 cd "$TERMUX_PKG_SRCDIR"
499 for patch in $TERMUX_PKG_BUILDER_DIR/*.patch.beforehostbuild; do
500 test -f "$patch" && sed "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" "$patch" | patch --silent -p1
501 done
93ae139b
FF
502
503 local TERMUX_HOSTBUILD_MARKER="$TERMUX_PKG_HOSTBUILD_DIR/TERMUX_BUILT_FOR_$TERMUX_PKG_VERSION"
504 if [ ! -f "$TERMUX_HOSTBUILD_MARKER" ]; then
505 rm -Rf "$TERMUX_PKG_HOSTBUILD_DIR"
506 mkdir -p "$TERMUX_PKG_HOSTBUILD_DIR"
7b1f1bd3
FF
507 cd "$TERMUX_PKG_HOSTBUILD_DIR"
508 termux_step_host_build
93ae139b 509 touch "$TERMUX_HOSTBUILD_MARKER"
7b1f1bd3
FF
510 fi
511}
512
59f0d218
FF
513# Perform a host build. Will be called in $TERMUX_PKG_HOSTBUILD_DIR.
514# After termux_step_post_extract_package() and before termux_step_patch_package()
01e699d9 515termux_step_host_build() {
7b1f1bd3 516 "$TERMUX_PKG_SRCDIR/configure" ${TERMUX_PKG_EXTRA_HOSTBUILD_CONFIGURE_ARGS}
4663ac70 517 make -j $TERMUX_MAKE_PROCESSES
59f0d218
FF
518}
519
7b1f1bd3
FF
520# Setup a standalone Android NDK toolchain. Not to be overridden by packages.
521termux_step_setup_toolchain() {
522 # We put this after system PATH to avoid picking up toolchain stripped python
523 export PATH=$PATH:$TERMUX_STANDALONE_TOOLCHAIN/bin
524
272404e1 525 export CFLAGS=""
619b9664
FF
526 export LDFLAGS="-L${TERMUX_PREFIX}/lib"
527
7b1f1bd3
FF
528 if [ "$TERMUX_PKG_CLANG" = "no" ]; then
529 export AS=${TERMUX_HOST_PLATFORM}-gcc
530 export CC=$TERMUX_HOST_PLATFORM-gcc
531 export CXX=$TERMUX_HOST_PLATFORM-g++
619b9664
FF
532 LDFLAGS+=" -specs=$TERMUX_SCRIPTDIR/termux.spec"
533 CFLAGS+=" -specs=$TERMUX_SCRIPTDIR/termux.spec"
7b1f1bd3 534 else
2f0de587 535 export AS=${TERMUX_HOST_PLATFORM}-clang
7b1f1bd3
FF
536 export CC=$TERMUX_HOST_PLATFORM-clang
537 export CXX=$TERMUX_HOST_PLATFORM-clang++
7b1f1bd3 538 fi
619b9664
FF
539
540 export AR=$TERMUX_HOST_PLATFORM-ar
7b1f1bd3
FF
541 export CPP=${TERMUX_HOST_PLATFORM}-cpp
542 export CC_FOR_BUILD=gcc
543 export LD=$TERMUX_HOST_PLATFORM-ld
544 export OBJDUMP=$TERMUX_HOST_PLATFORM-objdump
545 # Setup pkg-config for cross-compiling:
546 export PKG_CONFIG=$TERMUX_STANDALONE_TOOLCHAIN/bin/${TERMUX_HOST_PLATFORM}-pkg-config
547 export RANLIB=$TERMUX_HOST_PLATFORM-ranlib
548 export READELF=$TERMUX_HOST_PLATFORM-readelf
549 export STRIP=$TERMUX_HOST_PLATFORM-strip
550
7b1f1bd3
FF
551 # Android 7 started to support DT_RUNPATH (but not DT_RPATH), so we may want
552 # LDFLAGS+="-Wl,-rpath=$TERMUX_PREFIX/lib -Wl,--enable-new-dtags"
553 # and no longer remove DT_RUNPATH in termux-elf-cleaner.
554
555 if [ "$TERMUX_ARCH" = "arm" ]; then
c9e68e0d
FF
556 # https://developer.android.com/ndk/guides/standalone_toolchain.html#abi_compatibility:
557 # "We recommend using the -mthumb compiler flag to force the generation of 16-bit Thumb-2 instructions".
558 # With r13 of the ndk ruby 2.4.0 segfaults when built on arm with clang without -mthumb.
559 CFLAGS+=" -march=armv7-a -mfpu=neon -mfloat-abi=softfp -mthumb"
a5cdd56d 560 LDFLAGS+=" -march=armv7-a"
7b1f1bd3
FF
561 elif [ "$TERMUX_ARCH" = "i686" ]; then
562 # From $NDK/docs/CPU-ARCH-ABIS.html:
563 CFLAGS+=" -march=i686 -msse3 -mstackrealign -mfpmath=sse"
564 elif [ "$TERMUX_ARCH" = "aarch64" ]; then
0d1dde5f 565 :
7b1f1bd3
FF
566 elif [ "$TERMUX_ARCH" = "x86_64" ]; then
567 :
568 else
569 termux_error_exit "Invalid arch '$TERMUX_ARCH' - support arches are 'arm', 'i686', 'aarch64', 'x86_64'"
570 fi
571
572 if [ -n "$TERMUX_DEBUG" ]; then
573 CFLAGS+=" -g3 -O1 -fstack-protector --param ssp-buffer-size=4 -D_FORTIFY_SOURCE=2"
574 else
fbf60305 575 if [ "$TERMUX_PKG_CLANG" = "no" ]; then
430eef3b
FF
576 CFLAGS+=" -Os"
577 else
fbf60305
FF
578 if [ "$TERMUX_PKG_NAME" = "ruby" -a "$TERMUX_ARCH" = arm ]; then
579 # This exception is to avoid a broken ruby on 32-bit arm
580 # with NDK r15c and ruby 2.4.2 - see #1520.
581 CFLAGS+=" -O1"
582 else
583 # -Oz seems good for clang, see https://github.com/android-ndk/ndk/issues/133
584 CFLAGS+=" -Oz"
585 fi
430eef3b 586 fi
7b1f1bd3
FF
587 fi
588
589 export CXXFLAGS="$CFLAGS"
590 export CPPFLAGS="-I${TERMUX_PREFIX}/include"
591
592 if [ "$TERMUX_PKG_DEPENDS" != "${TERMUX_PKG_DEPENDS/libandroid-support/}" ]; then
593 # If using the android support library, link to it and include its headers as system headers:
594 CPPFLAGS+=" -isystem $TERMUX_PREFIX/include/libandroid-support"
595 LDFLAGS+=" -landroid-support"
596 fi
597
598 export ac_cv_func_getpwent=no
599 export ac_cv_func_getpwnam=no
600 export ac_cv_func_getpwuid=no
2173f730 601 export ac_cv_func_sigsetmask=no
7b1f1bd3
FF
602
603 if [ ! -d $TERMUX_STANDALONE_TOOLCHAIN ]; then
604 # Do not put toolchain in place until we are done with setup, to avoid having a half setup
605 # toolchain left in place if something goes wrong (or process is just aborted):
2f0de587 606 local _TERMUX_TOOLCHAIN_TMPDIR=${TERMUX_STANDALONE_TOOLCHAIN}-tmp
7b1f1bd3
FF
607 rm -Rf $_TERMUX_TOOLCHAIN_TMPDIR
608
609 local _NDK_ARCHNAME=$TERMUX_ARCH
610 if [ "$TERMUX_ARCH" = "aarch64" ]; then
611 _NDK_ARCHNAME=arm64
612 elif [ "$TERMUX_ARCH" = "i686" ]; then
613 _NDK_ARCHNAME=x86
614 fi
2f0de587 615
7b1f1bd3 616 "$NDK/build/tools/make_standalone_toolchain.py" \
272404e1 617 --api "$TERMUX_PKG_API_LEVEL" \
7b1f1bd3 618 --arch $_NDK_ARCHNAME \
d946e67c 619 --stl=libc++ \
7b1f1bd3
FF
620 --install-dir $_TERMUX_TOOLCHAIN_TMPDIR
621
d946e67c
FF
622 # Remove android-support header wrapping not needed on android-21:
623 rm -Rf $_TERMUX_TOOLCHAIN_TMPDIR/sysroot/usr/local
624
07884eae
FF
625 local wrapped plusplus CLANG_TARGET=$TERMUX_HOST_PLATFORM
626 if [ $TERMUX_ARCH = arm ]; then CLANG_TARGET=${CLANG_TARGET/arm-/armv7a-}; fi
627 for wrapped in ${TERMUX_HOST_PLATFORM}-clang clang; do
628 for plusplus in "" "++"; do
629 local FILE_TO_REPLACE=$_TERMUX_TOOLCHAIN_TMPDIR/bin/${wrapped}${plusplus}
630 if [ ! -f $FILE_TO_REPLACE ]; then
631 termux_error_exit "No toolchain file to override: $FILE_TO_REPLACE"
632 fi
7048a1f2 633 cp "$TERMUX_SCRIPTDIR/scripts/clang-pie-wrapper" $FILE_TO_REPLACE
58e38b47 634 sed -i "s/COMPILER/clang50$plusplus/" $FILE_TO_REPLACE
07884eae
FF
635 sed -i "s/CLANG_TARGET/$CLANG_TARGET/" $FILE_TO_REPLACE
636 done
2f0de587
FF
637 done
638
d946e67c 639 if [ "$TERMUX_ARCH" = "aarch64" ]; then
0d1dde5f
FF
640 # Use gold by default to work around https://github.com/android-ndk/ndk/issues/148
641 cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/aarch64-linux-android-ld.gold \
642 $_TERMUX_TOOLCHAIN_TMPDIR/bin/aarch64-linux-android-ld
c8d1a650
FF
643 cp $_TERMUX_TOOLCHAIN_TMPDIR/aarch64-linux-android/bin/ld.gold \
644 $_TERMUX_TOOLCHAIN_TMPDIR/aarch64-linux-android/bin/ld
7b1f1bd3
FF
645 fi
646
d1f566f7
FF
647 if [ "$TERMUX_ARCH" = "arm" ]; then
648 # Linker wrapper script to add '--exclude-libs libgcc.a', see
649 # https://github.com/android-ndk/ndk/issues/379
650 # https://android-review.googlesource.com/#/c/389852/
651 local linker
652 for linker in ld ld.bfd ld.gold; do
653 local wrap_linker=$_TERMUX_TOOLCHAIN_TMPDIR/$TERMUX_HOST_PLATFORM/bin/$linker
654 local real_linker=$_TERMUX_TOOLCHAIN_TMPDIR/$TERMUX_HOST_PLATFORM/bin/$linker.real
655 cp $wrap_linker $real_linker
656 echo '#!/bin/bash' > $wrap_linker
657 echo -n '`dirname $0`/' >> $wrap_linker
658 echo -n $linker.real >> $wrap_linker
659 echo ' --exclude-libs libgcc.a "$@"' >> $wrap_linker
660 done
661 fi
662
7b1f1bd3
FF
663 cd $_TERMUX_TOOLCHAIN_TMPDIR/sysroot
664
d3e5452e 665 for f in $TERMUX_SCRIPTDIR/ndk-patches/*.patch; do
7b1f1bd3
FF
666 sed "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" "$f" | \
667 sed "s%\@TERMUX_HOME\@%${TERMUX_ANDROID_HOME}%g" | \
668 patch --silent -p1;
669 done
42562d13
FF
670 # elf.h: Taken from glibc since the elf.h in the NDK is lacking.
671 # sysexits.h: Header-only and used by a few programs.
672 # ifaddrs.h: Added in android-24 unified headers, use a inline implementation for now.
50ed3357 673 cp "$TERMUX_SCRIPTDIR"/ndk-patches/{elf.h,sysexits.h,ifaddrs.h,libintl.h} usr/include
7b1f1bd3 674
67d2d615
FF
675 # Remove <sys/shm.h> from the NDK in favour of that from the libandroid-shmem.
676 # Also remove <sys/sem.h> as it doesn't work for non-root.
d04dc139
FF
677 rm usr/include/sys/{shm.h,sem.h}
678
272404e1 679 sed -i "s/define __ANDROID_API__ __ANDROID_API_FUTURE__/define __ANDROID_API__ $TERMUX_PKG_API_LEVEL/" \
d04dc139 680 usr/include/android/api-level.h
f39b87c2 681
b26283d4
FF
682 local _LIBDIR=usr/lib
683 if [ $TERMUX_ARCH = x86_64 ]; then _LIBDIR+=64; fi
684 $TERMUX_ELF_CLEANER $_LIBDIR/*.so
7b1f1bd3
FF
685
686 # zlib is really version 1.2.8 in the Android platform (at least
687 # starting from Android 5), not older as the NDK headers claim.
688 for file in zconf.h zlib.h; do
d04dc139 689 curl -o usr/include/$file \
7b1f1bd3
FF
690 https://raw.githubusercontent.com/madler/zlib/v1.2.8/$file
691 done
692 unset file
aa1865fe 693 cd $_TERMUX_TOOLCHAIN_TMPDIR/include/c++/4.9.x
694 sed "s%\@TERMUX_HOST_PLATFORM\@%${TERMUX_HOST_PLATFORM}%g" $TERMUX_SCRIPTDIR/ndk-patches/*.cpppatch | patch -p1
7b1f1bd3
FF
695 mv $_TERMUX_TOOLCHAIN_TMPDIR $TERMUX_STANDALONE_TOOLCHAIN
696 fi
697
d946e67c 698 local _STL_LIBFILE_NAME=libc++_shared.so
d1f566f7
FF
699 if [ ! -f $TERMUX_PREFIX/lib/libstdc++.so ]; then
700 # Setup libgnustl_shared.so in $PREFIX/lib and libstdc++.so as a link to it,
7b1f1bd3
FF
701 # so that other C++ using packages links to it instead of the default android
702 # C++ library which does not support exceptions or STL:
703 # https://developer.android.com/ndk/guides/cpp-support.html
704 # We do however want to avoid installing this, to avoid problems where e.g.
705 # libm.so on some i686 devices links against libstdc++.so.
706 # The libgnustl_shared.so library will be packaged in the libgnustl package
707 # which is part of the base Termux installation.
708 mkdir -p "$TERMUX_PREFIX/lib"
709 cd "$TERMUX_PREFIX/lib"
d946e67c
FF
710
711 local _STL_LIBFILE=
7b1f1bd3 712 if [ "$TERMUX_ARCH" = arm ]; then
d946e67c 713 local _STL_LIBFILE=$TERMUX_STANDALONE_TOOLCHAIN/${TERMUX_HOST_PLATFORM}/lib/armv7-a/$_STL_LIBFILE_NAME
7b1f1bd3 714 elif [ "$TERMUX_ARCH" = x86_64 ]; then
d946e67c
FF
715 local _STL_LIBFILE=$TERMUX_STANDALONE_TOOLCHAIN/${TERMUX_HOST_PLATFORM}/lib64/$_STL_LIBFILE_NAME
716 else
717 local _STL_LIBFILE=$TERMUX_STANDALONE_TOOLCHAIN/${TERMUX_HOST_PLATFORM}/lib/$_STL_LIBFILE_NAME
7b1f1bd3 718 fi
d946e67c 719
7b1f1bd3 720 cp "$_STL_LIBFILE" .
d946e67c
FF
721 $STRIP --strip-unneeded $_STL_LIBFILE_NAME
722 $TERMUX_ELF_CLEANER $_STL_LIBFILE_NAME
d1f566f7
FF
723 if [ $TERMUX_ARCH = "arm" ]; then
724 # Use a linker script to get libunwind.a.
725 echo 'INPUT(-lunwind -lc++_shared)' > libstdc++.so
726 else
727 ln -f $_STL_LIBFILE_NAME libstdc++.so
728 fi
7b1f1bd3
FF
729 fi
730
0dc0e842 731 export PKG_CONFIG_LIBDIR="$TERMUX_PKG_CONFIG_LIBDIR"
7b1f1bd3
FF
732 # Create a pkg-config wrapper. We use path to host pkg-config to
733 # avoid picking up a cross-compiled pkg-config later on.
734 local _HOST_PKGCONFIG
735 _HOST_PKGCONFIG=$(which pkg-config)
736 mkdir -p $TERMUX_STANDALONE_TOOLCHAIN/bin "$PKG_CONFIG_LIBDIR"
737 cat > "$PKG_CONFIG" <<-HERE
738 #!/bin/sh
739 export PKG_CONFIG_DIR=
740 export PKG_CONFIG_LIBDIR=$PKG_CONFIG_LIBDIR
741 exec $_HOST_PKGCONFIG "\$@"
742 HERE
743 chmod +x "$PKG_CONFIG"
744}
745
2b0e9dc9 746# Apply all *.patch files for the package. Not to be overridden by packages.
01e699d9 747termux_step_patch_package() {
7b1f1bd3 748 cd "$TERMUX_PKG_SRCDIR"
8f7dda83 749 # Suffix patch with ".patch32" or ".patch64" to only apply for these bitnesses:
7106823d 750 shopt -s nullglob
8f7dda83 751 for patch in $TERMUX_PKG_BUILDER_DIR/*.patch{$TERMUX_ARCH_BITS,}; do
7b1f1bd3 752 test -f "$patch" && sed "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" "$patch" | \
dc6476e9 753 sed "s%\@TERMUX_HOME\@%${TERMUX_ANDROID_HOME}%g" | \
a0057943 754 patch --silent -p1
59f0d218 755 done
a599e41a 756 shopt -u nullglob
7106823d 757}
59f0d218 758
7106823d
VB
759# Replace autotools build-aux/config.{sub,guess} with ours to add android targets.
760termux_step_replace_guess_scripts () {
761 cd "$TERMUX_PKG_SRCDIR"
7b1f1bd3
FF
762 find . -name config.sub -exec chmod u+w '{}' \; -exec cp "$TERMUX_SCRIPTDIR/scripts/config.sub" '{}' \;
763 find . -name config.guess -exec chmod u+w '{}' \; -exec cp "$TERMUX_SCRIPTDIR/scripts/config.guess" '{}' \;
59f0d218
FF
764}
765
7b1f1bd3 766# For package scripts to override. Called in $TERMUX_PKG_BUILDDIR.
01e699d9 767termux_step_pre_configure() {
7b1f1bd3 768 return
59f0d218
FF
769}
770
58f7a21c 771termux_step_configure_autotools () {
7b1f1bd3 772 if [ ! -e "$TERMUX_PKG_SRCDIR/configure" ]; then return; fi
59f0d218
FF
773
774 DISABLE_STATIC="--disable-static"
775 if [ "$TERMUX_PKG_EXTRA_CONFIGURE_ARGS" != "${TERMUX_PKG_EXTRA_CONFIGURE_ARGS/--enable-static/}" ]; then
776 # Do not --disable-static if package explicitly enables it (e.g. gdb needs enable-static to build)
777 DISABLE_STATIC=""
778 fi
779
780 DISABLE_NLS="--disable-nls"
781 if [ "$TERMUX_PKG_EXTRA_CONFIGURE_ARGS" != "${TERMUX_PKG_EXTRA_CONFIGURE_ARGS/--enable-nls/}" ]; then
782 # Do not --disable-nls if package explicitly enables it (for gettext itself)
783 DISABLE_NLS=""
784 fi
785
786 ENABLE_SHARED="--enable-shared"
787 if [ "$TERMUX_PKG_EXTRA_CONFIGURE_ARGS" != "${TERMUX_PKG_EXTRA_CONFIGURE_ARGS/--disable-shared/}" ]; then
788 ENABLE_SHARED=""
789 fi
790 HOST_FLAG="--host=$TERMUX_HOST_PLATFORM"
791 if [ "$TERMUX_PKG_EXTRA_CONFIGURE_ARGS" != "${TERMUX_PKG_EXTRA_CONFIGURE_ARGS/--host=/}" ]; then
792 HOST_FLAG=""
793 fi
66926ecd
VB
794 LIBEXEC_FLAG="--libexecdir=$TERMUX_PREFIX/libexec"
795 if [ "$TERMUX_PKG_EXTRA_CONFIGURE_ARGS" != "${TERMUX_PKG_EXTRA_CONFIGURE_ARGS/--libexecdir=/}" ]; then
796 LIBEXEC_FLAG=""
797 fi
cf575909
S
798 QUIET_BUILD=
799 if [ ! -z ${TERMUX_QUIET_BUILD+x} ]; then
800 QUIET_BUILD="--enable-silent-rules"
801 fi
59f0d218
FF
802
803 # Some packages provides a $PKG-config script which some configure scripts pickup instead of pkg-config:
7b1f1bd3 804 mkdir "$TERMUX_PKG_TMPDIR/config-scripts"
59f0d218 805 for f in $TERMUX_PREFIX/bin/*config; do
7b1f1bd3 806 test -f "$f" && cp "$f" "$TERMUX_PKG_TMPDIR/config-scripts"
59f0d218 807 done
59f0d218
FF
808 export PATH=$TERMUX_PKG_TMPDIR/config-scripts:$PATH
809
00d9769e
FF
810 # Avoid gnulib wrapping of functions when cross compiling. See
811 # http://wiki.osdev.org/Cross-Porting_Software#Gnulib
812 # https://gitlab.com/sortix/sortix/wikis/Gnulib
813 # https://github.com/termux/termux-packages/issues/76
814 local AVOID_GNULIB=""
0dc0e842
FF
815 AVOID_GNULIB+=" ac_cv_func_calloc_0_nonnull=yes"
816 AVOID_GNULIB+=" ac_cv_func_chown_works=yes"
817 AVOID_GNULIB+=" ac_cv_func_getgroups_works=yes"
00d9769e
FF
818 AVOID_GNULIB+=" ac_cv_func_malloc_0_nonnull=yes"
819 AVOID_GNULIB+=" ac_cv_func_realloc_0_nonnull=yes"
820 AVOID_GNULIB+=" am_cv_func_working_getline=yes"
821 AVOID_GNULIB+=" gl_cv_func_dup2_works=yes"
822 AVOID_GNULIB+=" gl_cv_func_fcntl_f_dupfd_cloexec=yes"
823 AVOID_GNULIB+=" gl_cv_func_fcntl_f_dupfd_works=yes"
542aab6b 824 AVOID_GNULIB+=" gl_cv_func_fnmatch_posix=yes"
00d9769e
FF
825 AVOID_GNULIB+=" gl_cv_func_getcwd_abort_bug=no"
826 AVOID_GNULIB+=" gl_cv_func_getcwd_null=yes"
827 AVOID_GNULIB+=" gl_cv_func_getcwd_path_max=yes"
828 AVOID_GNULIB+=" gl_cv_func_getcwd_posix_signature=yes"
829 AVOID_GNULIB+=" gl_cv_func_gettimeofday_clobber=no"
830 AVOID_GNULIB+=" gl_cv_func_gettimeofday_posix_signature=yes"
831 AVOID_GNULIB+=" gl_cv_func_link_works=yes"
832 AVOID_GNULIB+=" gl_cv_func_lstat_dereferences_slashed_symlink=yes"
542aab6b 833 AVOID_GNULIB+=" gl_cv_func_malloc_0_nonnull=yes"
00d9769e
FF
834 AVOID_GNULIB+=" gl_cv_func_memchr_works=yes"
835 AVOID_GNULIB+=" gl_cv_func_mkdir_trailing_dot_works=yes"
836 AVOID_GNULIB+=" gl_cv_func_mkdir_trailing_slash_works=yes"
0dc0e842 837 AVOID_GNULIB+=" gl_cv_func_mkfifo_works=yes"
ea41b461 838 AVOID_GNULIB+=" gl_cv_func_realpath_works=yes"
00d9769e 839 AVOID_GNULIB+=" gl_cv_func_select_detects_ebadf=yes"
542aab6b 840 AVOID_GNULIB+=" gl_cv_func_snprintf_posix=yes"
00d9769e 841 AVOID_GNULIB+=" gl_cv_func_snprintf_retval_c99=yes"
542aab6b 842 AVOID_GNULIB+=" gl_cv_func_snprintf_truncation_c99=yes"
00d9769e
FF
843 AVOID_GNULIB+=" gl_cv_func_stat_dir_slash=yes"
844 AVOID_GNULIB+=" gl_cv_func_stat_file_slash=yes"
845 AVOID_GNULIB+=" gl_cv_func_strerror_0_works=yes"
846 AVOID_GNULIB+=" gl_cv_func_symlink_works=yes"
847 AVOID_GNULIB+=" gl_cv_func_tzset_clobber=no"
848 AVOID_GNULIB+=" gl_cv_func_unlink_honors_slashes=yes"
849 AVOID_GNULIB+=" gl_cv_func_unlink_honors_slashes=yes"
542aab6b
FF
850 AVOID_GNULIB+=" gl_cv_func_vsnprintf_posix=yes"
851 AVOID_GNULIB+=" gl_cv_func_vsnprintf_zerosize_c99=yes"
00d9769e
FF
852 AVOID_GNULIB+=" gl_cv_func_wcwidth_works=yes"
853 AVOID_GNULIB+=" gl_cv_func_working_getdelim=yes"
854 AVOID_GNULIB+=" gl_cv_func_working_mkstemp=yes"
855 AVOID_GNULIB+=" gl_cv_func_working_mktime=yes"
856 AVOID_GNULIB+=" gl_cv_func_working_strerror=yes"
857 AVOID_GNULIB+=" gl_cv_header_working_fcntl_h=yes"
f7d044c2 858 AVOID_GNULIB+=" gl_cv_C_locale_sans_EILSEQ=yes"
00d9769e
FF
859
860 # NOTE: We do not want to quote AVOID_GNULIB as we want word expansion.
861 env $AVOID_GNULIB "$TERMUX_PKG_SRCDIR/configure" \
59f0d218
FF
862 --disable-dependency-tracking \
863 --prefix=$TERMUX_PREFIX \
7b1f1bd3 864 --disable-rpath --disable-rpath-hack \
59f0d218
FF
865 $HOST_FLAG \
866 $TERMUX_PKG_EXTRA_CONFIGURE_ARGS \
867 $DISABLE_NLS \
868 $ENABLE_SHARED \
869 $DISABLE_STATIC \
cf575909
S
870 $LIBEXEC_FLAG \
871 $QUIET_BUILD
59f0d218
FF
872}
873
58f7a21c
VB
874termux_step_configure_cmake () {
875 termux_setup_cmake
876
58f7a21c
VB
877 local TOOLCHAIN_ARGS="-DCMAKE_ANDROID_STANDALONE_TOOLCHAIN=$TERMUX_STANDALONE_TOOLCHAIN"
878 local BUILD_TYPE=MinSizeRel
879 test -n "$TERMUX_DEBUG" && BUILD_TYPE=Debug
880
881 local CMAKE_PROC=$TERMUX_ARCH
882 test $CMAKE_PROC == "arm" && CMAKE_PROC='armv7-a'
883
2f0de587
FF
884 # XXX: CMAKE_{AR,RANLIB} needed for at least jsoncpp build to not
885 # pick up cross compiled binutils tool in $PREFIX/bin:
7048a1f2
FF
886 cmake -G 'Unix Makefiles' "$TERMUX_PKG_SRCDIR" \
887 -DCMAKE_AR="$(which $AR)" \
d946e67c 888 -DCMAKE_UNAME="$(which uname)" \
7048a1f2 889 -DCMAKE_RANLIB="$(which $RANLIB)" \
58f7a21c
VB
890 -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
891 -DCMAKE_CROSSCOMPILING=True \
892 -DCMAKE_C_FLAGS="$CFLAGS $CPPFLAGS" \
2f0de587 893 -DCMAKE_CXX_FLAGS="$CXXFLAGS $CPPFLAGS" \
58f7a21c
VB
894 -DCMAKE_LINKER="$TERMUX_STANDALONE_TOOLCHAIN/bin/$LD $LDFLAGS" \
895 -DCMAKE_FIND_ROOT_PATH=$TERMUX_PREFIX \
896 -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY \
897 -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
898 -DCMAKE_INSTALL_PREFIX=$TERMUX_PREFIX \
899 -DCMAKE_MAKE_PROGRAM=`which make` \
900 -DCMAKE_SYSTEM_PROCESSOR=$CMAKE_PROC \
901 -DCMAKE_SYSTEM_NAME=Android \
902 -DCMAKE_SYSTEM_VERSION=21 \
903 -DCMAKE_SKIP_INSTALL_RPATH=ON \
904 -DCMAKE_USE_SYSTEM_LIBRARIES=True \
905 -DBUILD_TESTING=OFF \
906 $TERMUX_PKG_EXTRA_CONFIGURE_ARGS $TOOLCHAIN_ARGS
907}
908
f0c1439b
FF
909termux_step_configure_meson () {
910 termux_setup_meson
911 CC=gcc CXX=g++ $TERMUX_MESON \
912 $TERMUX_PKG_SRCDIR \
913 $TERMUX_PKG_BUILDDIR \
914 --cross-file $TERMUX_MESON_CROSSFILE \
915 --prefix $TERMUX_PREFIX \
916 --libdir lib \
917 --buildtype minsize \
918 --strip \
919 $TERMUX_PKG_EXTRA_CONFIGURE_ARGS
920}
921
58f7a21c 922termux_step_configure () {
2f0de587 923 if [ "$TERMUX_PKG_FORCE_CMAKE" == 'no' ] && [ -f "$TERMUX_PKG_SRCDIR/configure" ]; then
58f7a21c
VB
924 termux_step_configure_autotools
925 elif [ -f "$TERMUX_PKG_SRCDIR/CMakeLists.txt" ]; then
926 termux_step_configure_cmake
f0c1439b
FF
927 elif [ -f "$TERMUX_PKG_SRCDIR/meson.build" ]; then
928 termux_step_configure_meson
58f7a21c
VB
929 fi
930}
931
932termux_step_post_configure () {
7b1f1bd3 933 return
59f0d218
FF
934}
935
01e699d9 936termux_step_make() {
caf5d0df 937 if ls ./*akefile &> /dev/null; then
7b1f1bd3
FF
938 if [ -z "$TERMUX_PKG_EXTRA_MAKE_ARGS" ]; then
939 make -j $TERMUX_MAKE_PROCESSES
940 else
2f0de587 941 make -j $TERMUX_MAKE_PROCESSES ${TERMUX_PKG_EXTRA_MAKE_ARGS}
7b1f1bd3
FF
942 fi
943 fi
59f0d218
FF
944}
945
01e699d9 946termux_step_make_install() {
caf5d0df 947 if ls ./*akefile &> /dev/null; then
dd54dd13 948 : "${TERMUX_PKG_MAKE_INSTALL_TARGET:="install"}"
7b1f1bd3
FF
949 # Some packages have problem with parallell install, and it does not buy much, so use -j 1.
950 if [ -z "$TERMUX_PKG_EXTRA_MAKE_ARGS" ]; then
951 make -j 1 ${TERMUX_PKG_MAKE_INSTALL_TARGET}
952 else
953 make -j 1 ${TERMUX_PKG_EXTRA_MAKE_ARGS} ${TERMUX_PKG_MAKE_INSTALL_TARGET}
954 fi
f0c1439b
FF
955 elif test -f build.ninja; then
956 ninja install
7b1f1bd3 957 fi
59f0d218
FF
958}
959
7b1f1bd3 960# Hook function for package scripts to override.
01e699d9 961termux_step_post_make_install() {
7b1f1bd3 962 return
59f0d218
FF
963}
964
01e699d9 965termux_step_extract_into_massagedir() {
7b1f1bd3 966 local TARBALL_ORIG=$TERMUX_PKG_PACKAGEDIR/${TERMUX_PKG_NAME}_orig.tar.gz
59f0d218
FF
967
968 # Build diff tar with what has changed during the build:
969 cd $TERMUX_PREFIX
6b884285
FF
970 tar -N "$TERMUX_BUILD_TS_FILE" \
971 --exclude='lib/libc++_shared.so' --exclude='lib/libstdc++.so' \
972 -czf "$TARBALL_ORIG" .
59f0d218
FF
973
974 # Extract tar in order to massage it
7b1f1bd3
FF
975 mkdir -p "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX"
976 cd "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX"
2f122eed 977 tar xf "$TARBALL_ORIG"
7b1f1bd3 978 rm "$TARBALL_ORIG"
59f0d218
FF
979}
980
01e699d9 981termux_step_massage() {
7b1f1bd3 982 cd "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX"
59f0d218
FF
983
984 # Remove lib/charset.alias which is installed by gettext-using packages:
985 rm -f lib/charset.alias
a0c80d57 986
59f0d218
FF
987 # Remove non-english man pages:
988 test -d share/man && (cd share/man; for f in `ls | grep -v man`; do rm -Rf $f; done )
a0c80d57 989
7b1f1bd3 990 if [ -z "${TERMUX_PKG_KEEP_INFOPAGES+x}" ]; then
a0c80d57
FF
991 # Remove info pages:
992 rm -Rf share/info
993 fi
994
9db621c0
FF
995 # Remove locale files we're not interested in::
996 rm -Rf share/locale
7b1f1bd3 997 if [ -z "${TERMUX_PKG_KEEP_SHARE_DOC+x}" ]; then
9db621c0
FF
998 # Remove info pages:
999 rm -Rf share/doc
1000 fi
a0c80d57 1001
59f0d218
FF
1002 # Remove old kept libraries (readline):
1003 find . -name '*.old' -delete
a0c80d57
FF
1004
1005 # Remove static libraries:
59f0d218
FF
1006 if [ $TERMUX_PKG_KEEP_STATIC_LIBRARIES = "false" ]; then
1007 find . -name '*.a' -delete
1008 find . -name '*.la' -delete
1009 fi
1010
1d678bce 1011 # Move over sbin to bin:
7b1f1bd3 1012 for file in sbin/*; do if test -f "$file"; then mv "$file" bin/; fi; done
59f0d218 1013
174285d9 1014 # Remove world permissions and add write permissions.
40534bae
FF
1015 # The -f flag is used to suppress warnings about dangling symlinks (such
1016 # as ones to /system/... which may not exist on the build machine):
174285d9
FF
1017 find . -exec chmod -f u+w,g-rwx,o-rwx \{\} \;
1018
59f0d218 1019 if [ "$TERMUX_DEBUG" = "" ]; then
e5b151a0
FF
1020 # Strip binaries. file(1) may fail for certain unusual files, so disable pipefail.
1021 set +e +o pipefail
1022 find . -type f | xargs -r file | grep -E "(executable|shared object)" | grep ELF | cut -f 1 -d : | \
7b1f1bd3 1023 xargs -r "$STRIP" --strip-unneeded --preserve-dates
e5b151a0 1024 set -e -o pipefail
59f0d218 1025 fi
1d678bce 1026 # Remove DT_ entries which the android 5.1 linker warns about:
7b1f1bd3 1027 find . -type f -print0 | xargs -r -0 "$TERMUX_ELF_CLEANER"
1d678bce
FF
1028
1029 # Fix shebang paths:
6b56911d
FF
1030 while IFS= read -r -d '' file
1031 do
1032 head -c 100 "$file" | grep -E "^#\!.*\\/bin\\/.*" | grep -q -E -v "^#\! ?\\/system" && sed --follow-symlinks -i -E "1 s@^#\!(.*)/bin/(.*)@#\!$TERMUX_PREFIX/bin/\2@" "$file"
1033 done < <(find -L . -type f -print0)
59f0d218
FF
1034
1035 test ! -z "$TERMUX_PKG_RM_AFTER_INSTALL" && rm -Rf $TERMUX_PKG_RM_AFTER_INSTALL
1036
1037 find . -type d -empty -delete # Remove empty directories
1038
7b1f1bd3
FF
1039 # Sub packages:
1040 if [ -d include ] && [ -z "${TERMUX_PKG_NO_DEVELSPLIT}" ]; then
1041 # Add virtual -dev sub package if there are include files:
1042 local _DEVEL_SUBPACKAGE_FILE=$TERMUX_PKG_TMPDIR/${TERMUX_PKG_NAME}-dev.subpackage.sh
38a5614f 1043 echo TERMUX_SUBPKG_INCLUDE=\"include share/vala share/man/man3 lib/pkgconfig share/aclocal lib/cmake $TERMUX_PKG_INCLUDE_IN_DEVPACKAGE\" > "$_DEVEL_SUBPACKAGE_FILE"
dd54dd13 1044 echo "TERMUX_SUBPKG_DESCRIPTION=\"Development files for ${TERMUX_PKG_NAME}\"" >> "$_DEVEL_SUBPACKAGE_FILE"
28dc0ace 1045 if [ -n "$TERMUX_PKG_DEVPACKAGE_DEPENDS" ]; then
6b56911d 1046 echo "TERMUX_SUBPKG_DEPENDS=\"$TERMUX_PKG_NAME,$TERMUX_PKG_DEVPACKAGE_DEPENDS\"" >> "$_DEVEL_SUBPACKAGE_FILE"
28dc0ace 1047 else
01e699d9 1048 echo "TERMUX_SUBPKG_DEPENDS=\"$TERMUX_PKG_NAME\"" >> "$_DEVEL_SUBPACKAGE_FILE"
28dc0ace 1049 fi
7b1f1bd3
FF
1050 fi
1051 # Now build all sub packages
1052 rm -Rf "$TERMUX_TOPDIR/$TERMUX_PKG_NAME/subpackages"
59f0d218 1053 for subpackage in $TERMUX_PKG_BUILDER_DIR/*.subpackage.sh $TERMUX_PKG_TMPDIR/*subpackage.sh; do
7b1f1bd3
FF
1054 test ! -f "$subpackage" && continue
1055 local SUB_PKG_NAME
1056 SUB_PKG_NAME=$(basename "$subpackage" .subpackage.sh)
1057 # Default value is same as main package, but sub package may override:
1058 local TERMUX_SUBPKG_PLATFORM_INDEPENDENT=$TERMUX_PKG_PLATFORM_INDEPENDENT
1059 local SUB_PKG_DIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/subpackages/$SUB_PKG_NAME
1060 local TERMUX_SUBPKG_DEPENDS=""
1061 local TERMUX_SUBPKG_CONFLICTS=""
4f85432b 1062 local TERMUX_SUBPKG_REPLACES=""
0d1a1c8e 1063 local TERMUX_SUBPKG_CONFFILES=""
7b1f1bd3
FF
1064 local SUB_PKG_MASSAGE_DIR=$SUB_PKG_DIR/massage/$TERMUX_PREFIX
1065 local SUB_PKG_PACKAGE_DIR=$SUB_PKG_DIR/package
1066 mkdir -p "$SUB_PKG_MASSAGE_DIR" "$SUB_PKG_PACKAGE_DIR"
1067
1068 # shellcheck source=/dev/null
1069 source $subpackage
1070
1071 for includeset in $TERMUX_SUBPKG_INCLUDE; do
1072 local _INCLUDE_DIRSET
1073 _INCLUDE_DIRSET=$(dirname "$includeset")
1074 test "$_INCLUDE_DIRSET" = "." && _INCLUDE_DIRSET=""
1075 if [ -e "$includeset" ] || [ -L "$includeset" ]; then
4432ca97 1076 # Add the -L clause to handle relative symbolic links:
7b1f1bd3
FF
1077 mkdir -p "$SUB_PKG_MASSAGE_DIR/$_INCLUDE_DIRSET"
1078 mv "$includeset" "$SUB_PKG_MASSAGE_DIR/$_INCLUDE_DIRSET"
1079 fi
1080 done
59f0d218 1081
7b1f1bd3
FF
1082 local SUB_PKG_ARCH=$TERMUX_ARCH
1083 test -n "$TERMUX_SUBPKG_PLATFORM_INDEPENDENT" && SUB_PKG_ARCH=all
59f0d218 1084
7b1f1bd3
FF
1085 cd "$SUB_PKG_DIR/massage"
1086 local SUB_PKG_INSTALLSIZE
1087 SUB_PKG_INSTALLSIZE=$(du -sk . | cut -f 1)
2f122eed 1088 tar -cJf "$SUB_PKG_PACKAGE_DIR/data.tar.xz" .
59f0d218 1089
7b1f1bd3 1090 mkdir -p DEBIAN
59f0d218 1091 cd DEBIAN
7b1f1bd3
FF
1092 cat > control <<-HERE
1093 Package: $SUB_PKG_NAME
1094 Architecture: ${SUB_PKG_ARCH}
1095 Installed-Size: ${SUB_PKG_INSTALLSIZE}
1096 Maintainer: $TERMUX_PKG_MAINTAINER
1097 Version: $TERMUX_PKG_FULLVERSION
1098 Description: $TERMUX_SUBPKG_DESCRIPTION
1099 Homepage: $TERMUX_PKG_HOMEPAGE
1100 HERE
1101 test ! -z "$TERMUX_SUBPKG_DEPENDS" && echo "Depends: $TERMUX_SUBPKG_DEPENDS" >> control
1102 test ! -z "$TERMUX_SUBPKG_CONFLICTS" && echo "Conflicts: $TERMUX_SUBPKG_CONFLICTS" >> control
4f85432b 1103 test ! -z "$TERMUX_SUBPKG_REPLACES" && echo "Replaces: $TERMUX_SUBPKG_REPLACES" >> control
2f122eed 1104 tar -cJf "$SUB_PKG_PACKAGE_DIR/control.tar.xz" .
7b1f1bd3 1105
cc7bcfa2
VB
1106 for f in $TERMUX_SUBPKG_CONFFILES; do echo "$TERMUX_PREFIX/$f" >> conffiles; done
1107
7b1f1bd3
FF
1108 # Create the actual .deb file:
1109 TERMUX_SUBPKG_DEBFILE=$TERMUX_DEBDIR/${SUB_PKG_NAME}_${TERMUX_PKG_FULLVERSION}_${SUB_PKG_ARCH}.deb
1110 test ! -f "$TERMUX_COMMON_CACHEDIR/debian-binary" && echo "2.0" > "$TERMUX_COMMON_CACHEDIR/debian-binary"
1111 ar cr "$TERMUX_SUBPKG_DEBFILE" \
1112 "$TERMUX_COMMON_CACHEDIR/debian-binary" \
1113 "$SUB_PKG_PACKAGE_DIR/control.tar.xz" \
1114 "$SUB_PKG_PACKAGE_DIR/data.tar.xz"
1115
1116 # Go back to main package:
1117 cd "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX"
59f0d218
FF
1118 done
1119
1120 # .. remove empty directories (NOTE: keep this last):
1121 find . -type d -empty -delete
7b1f1bd3
FF
1122 # Make sure user can read and write all files (problem with dpkg otherwise):
1123 chmod -R u+rw .
59f0d218
FF
1124}
1125
01e699d9 1126termux_step_post_massage() {
7b1f1bd3 1127 return
59f0d218
FF
1128}
1129
7b1f1bd3
FF
1130# Create data.tar.gz with files to package. Not to be overridden by package scripts.
1131termux_step_create_datatar() {
1132 # Create data tarball containing files to package:
1133 cd "$TERMUX_PKG_MASSAGEDIR"
46fe48b3
FF
1134
1135 local HARDLINKS="$(find . -type f -links +1)"
1136 if [ -n "$HARDLINKS" ]; then
1137 termux_error_exit "Package contains hard links: $HARDLINKS"
1138 fi
1139
7b1f1bd3
FF
1140 if [ -z "${TERMUX_PKG_METAPACKAGE+x}" ] && [ "$(find . -type f)" = "" ]; then
1141 termux_error_exit "No files in package"
f6a56287 1142 fi
2f122eed 1143 tar -cJf "$TERMUX_PKG_PACKAGEDIR/data.tar.xz" .
7b1f1bd3 1144}
f6a56287 1145
01e699d9 1146termux_step_create_debscripts() {
7b1f1bd3 1147 return
3a189d89
FF
1148}
1149
7b1f1bd3
FF
1150# Create the build deb file. Not to be overridden by package scripts.
1151termux_step_create_debfile() {
1152 # Get install size. This will be written as the "Installed-Size" deb field so is measured in 1024-byte blocks:
1153 local TERMUX_PKG_INSTALLSIZE
1154 TERMUX_PKG_INSTALLSIZE=$(du -sk . | cut -f 1)
1155
1156 # From here on TERMUX_ARCH is set to "all" if TERMUX_PKG_PLATFORM_INDEPENDENT is set by the package
1157 test -n "$TERMUX_PKG_PLATFORM_INDEPENDENT" && TERMUX_ARCH=all
1158
1159 mkdir -p DEBIAN
1160 cat > DEBIAN/control <<-HERE
1161 Package: $TERMUX_PKG_NAME
1162 Architecture: ${TERMUX_ARCH}
1163 Installed-Size: ${TERMUX_PKG_INSTALLSIZE}
1164 Maintainer: $TERMUX_PKG_MAINTAINER
1165 Version: $TERMUX_PKG_FULLVERSION
1166 Description: $TERMUX_PKG_DESCRIPTION
1167 Homepage: $TERMUX_PKG_HOMEPAGE
1168 HERE
9ec05091 1169 test ! -z "$TERMUX_PKG_BREAKS" && echo "Breaks: $TERMUX_PKG_BREAKS" >> DEBIAN/control
7b1f1bd3
FF
1170 test ! -z "$TERMUX_PKG_DEPENDS" && echo "Depends: $TERMUX_PKG_DEPENDS" >> DEBIAN/control
1171 test ! -z "$TERMUX_PKG_ESSENTIAL" && echo "Essential: yes" >> DEBIAN/control
1172 test ! -z "$TERMUX_PKG_CONFLICTS" && echo "Conflicts: $TERMUX_PKG_CONFLICTS" >> DEBIAN/control
5f217847 1173 test ! -z "$TERMUX_PKG_RECOMMENDS" && echo "Recommends: $TERMUX_PKG_RECOMMENDS" >> DEBIAN/control
7b1f1bd3
FF
1174 test ! -z "$TERMUX_PKG_REPLACES" && echo "Replaces: $TERMUX_PKG_REPLACES" >> DEBIAN/control
1175
1176 # Create DEBIAN/conffiles (see https://www.debian.org/doc/debian-policy/ap-pkg-conffiles.html):
1177 for f in $TERMUX_PKG_CONFFILES; do echo "$TERMUX_PREFIX/$f" >> DEBIAN/conffiles; done
1178
1179 # Allow packages to create arbitrary control files.
1180 # XXX: Should be done in a better way without a function?
1181 cd DEBIAN
1182 termux_step_create_debscripts
1183
1184 # Create control.tar.xz
2f122eed 1185 tar -cJf "$TERMUX_PKG_PACKAGEDIR/control.tar.xz" .
7b1f1bd3
FF
1186
1187 test ! -f "$TERMUX_COMMON_CACHEDIR/debian-binary" && echo "2.0" > "$TERMUX_COMMON_CACHEDIR/debian-binary"
1188 TERMUX_PKG_DEBFILE=$TERMUX_DEBDIR/${TERMUX_PKG_NAME}_${TERMUX_PKG_FULLVERSION}_${TERMUX_ARCH}.deb
1189 # Create the actual .deb file:
1190 ar cr "$TERMUX_PKG_DEBFILE" \
1191 "$TERMUX_COMMON_CACHEDIR/debian-binary" \
1192 "$TERMUX_PKG_PACKAGEDIR/control.tar.xz" \
1193 "$TERMUX_PKG_PACKAGEDIR/data.tar.xz"
1194}
59f0d218 1195
7b1f1bd3
FF
1196# Finish the build. Not to be overridden by package scripts.
1197termux_step_finish_build() {
1198 echo "termux - build of '$TERMUX_PKG_NAME' done"
1199 test -t 1 && printf "\033]0;%s - DONE\007" "$TERMUX_PKG_NAME"
1200 mkdir -p /data/data/.built-packages
1201 echo "$TERMUX_PKG_FULLVERSION" > "/data/data/.built-packages/$TERMUX_PKG_NAME"
9a104bda 1202 exit 0
7b1f1bd3 1203}
2497d957 1204
7b1f1bd3
FF
1205termux_step_handle_arguments "$@"
1206termux_step_setup_variables
1207termux_step_handle_buildarch
1208termux_step_start_build
59f0d218 1209termux_step_extract_package
508fa73c 1210cd "$TERMUX_PKG_SRCDIR"
59f0d218 1211termux_step_post_extract_package
7b1f1bd3
FF
1212termux_step_handle_hostbuild
1213termux_step_setup_toolchain
59f0d218 1214termux_step_patch_package
7106823d 1215termux_step_replace_guess_scripts
ca7699c1 1216cd "$TERMUX_PKG_SRCDIR"
59f0d218 1217termux_step_pre_configure
7b1f1bd3 1218cd "$TERMUX_PKG_BUILDDIR"
59f0d218 1219termux_step_configure
7b1f1bd3 1220cd "$TERMUX_PKG_BUILDDIR"
59f0d218 1221termux_step_post_configure
7b1f1bd3 1222cd "$TERMUX_PKG_BUILDDIR"
59f0d218 1223termux_step_make
7b1f1bd3 1224cd "$TERMUX_PKG_BUILDDIR"
59f0d218 1225termux_step_make_install
7b1f1bd3 1226cd "$TERMUX_PKG_BUILDDIR"
59f0d218 1227termux_step_post_make_install
7b1f1bd3 1228cd "$TERMUX_PKG_MASSAGEDIR"
59f0d218 1229termux_step_extract_into_massagedir
7b1f1bd3 1230cd "$TERMUX_PKG_MASSAGEDIR"
59f0d218 1231termux_step_massage
7b1f1bd3 1232cd "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX"
59f0d218 1233termux_step_post_massage
7b1f1bd3
FF
1234termux_step_create_datatar
1235termux_step_create_debfile
1236termux_step_finish_build