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