From 7b1f1bd35dd9bd3367bf603d97be4f582f8b1b23 Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Thu, 5 Jan 2017 20:37:45 -0500 Subject: [PATCH] build-package.sh: Cleanup a bit - Put everything into shell functions. - Start fixing shellcheck warnings. - Run a hostbuild before setting up an android toolchain (fixes #657). It was quite a hasty rewrite, so things could be broken. Let me know! --- build-package.sh | 1372 +++++++++++++++-------------- disabled-packages/alpine/build.sh | 14 +- packages/colordiff/build.sh | 2 +- packages/libluajit/build.sh | 2 +- packages/openssh/build.sh | 2 +- packages/tinyscheme/build.sh | 2 +- packages/tty-clock/build.sh | 2 +- packages/zile/build.sh | 2 +- 8 files changed, 703 insertions(+), 695 deletions(-) diff --git a/build-package.sh b/build-package.sh index 1eda2eeda..1fc0ebdd5 100755 --- a/build-package.sh +++ b/build-package.sh @@ -2,190 +2,28 @@ set -e -o pipefail -u -# Read settings from .termuxrc if existing: -test -f $HOME/.termuxrc && . $HOME/.termuxrc - -# Handle command-line arguments: -show_usage () { - echo "Usage: ./build-package.sh [-a ARCH] [-d] [-D] PACKAGE" - echo "Build a package by creating a .deb file in the debs/ folder." - echo " -a The architecture to build for: aarch64(default), arm, i686, x86_64 or all." - echo " -d Build with debug symbols." - echo " -D Build a disabled package in disabled-packages/." - echo " -s Skip dependency check." - exit 1 -} -while getopts :a:hdDs option -do - case "$option" in - a) TERMUX_ARCH="$OPTARG";; - h) show_usage;; - d) TERMUX_DEBUG=true;; - D) TERMUX_IS_DISABLED=true;; - s) export TERMUX_SKIP_DEPCHECK=true;; - ?) echo "./build-package.sh: illegal option -$OPTARG"; exit 1;; - esac -done -shift $(($OPTIND-1)) -if [ "$#" -ne 1 ]; then show_usage; fi - -# Configurable settings: -: ${ANDROID_HOME:="${HOME}/lib/android-sdk"} -: ${NDK:="${HOME}/lib/android-ndk"} -: ${TERMUX_MAKE_PROCESSES:='4'} -: ${TERMUX_TOPDIR:="$HOME/.termux-build"} -: ${TERMUX_ARCH:="aarch64"} # arm, aarch64, i686 or x86_64. -if [ "x86_64" = $TERMUX_ARCH -o "aarch64" = $TERMUX_ARCH ]; then - TERMUX_ARCH_BITS=64 -else - TERMUX_ARCH_BITS=32 -fi -TERMUX_HOST_PLATFORM="${TERMUX_ARCH}-linux-android" -if [ $TERMUX_ARCH = "arm" ]; then TERMUX_HOST_PLATFORM="${TERMUX_HOST_PLATFORM}eabi"; fi -: ${TERMUX_PREFIX:='/data/data/com.termux/files/usr'} -: ${TERMUX_ANDROID_HOME:='/data/data/com.termux/files/home'} -: ${TERMUX_DEBUG:=""} -: ${TERMUX_PROCESS_DEB:=""} -: ${TERMUX_API_LEVEL:="21"} -: ${TERMUX_ANDROID_BUILD_TOOLS_VERSION:="24.0.1"} -: ${TERMUX_NDK_VERSION:="13"} -: ${TERMUX_IS_DISABLED:=""} - -# Check the NDK: -if [ ! -d "$NDK" ]; then - echo 'ERROR: $NDK not defined as pointing at a directory - define it pointing at a android NDK installation!' - exit 1 -fi -if grep -s -q "Pkg.Revision = $TERMUX_NDK_VERSION" $NDK/source.properties; then - : -else - echo "Wrong NDK version - we need $TERMUX_NDK_VERSION" - exit 1 -fi - -# Check the package to build: -TERMUX_PKG_NAME=`basename $1` -export TERMUX_SCRIPTDIR=`cd $(dirname $0); pwd` -if [[ $1 == *"/"* ]]; then - # Path to directory which may be outside this repo: - if [ ! -d $1 ]; then echo "ERROR: '$1' seems to be a path but is not a directory"; exit 1; fi - export TERMUX_PKG_BUILDER_DIR=`realpath $1` -else - # Package name: - if [ -n "$TERMUX_IS_DISABLED" ]; then - export TERMUX_PKG_BUILDER_DIR=$TERMUX_SCRIPTDIR/disabled-packages/$TERMUX_PKG_NAME - else - export TERMUX_PKG_BUILDER_DIR=$TERMUX_SCRIPTDIR/packages/$TERMUX_PKG_NAME - fi -fi -TERMUX_PKG_BUILDER_SCRIPT=$TERMUX_PKG_BUILDER_DIR/build.sh -if test ! -f $TERMUX_PKG_BUILDER_SCRIPT; then - echo "ERROR: No build.sh script at supposed package dir $TERMUX_PKG_BUILDER_DIR!" +# Utility function to log an error message and exit with an error code. +termux_error_exit() { + echo "ERROR: $*" 1>&2 exit 1 -fi - -# Handle 'all' arch: -if [ $TERMUX_ARCH = 'all' ]; then - for arch in 'arm' 'i686' 'aarch64' 'x86_64'; do - ./build-package.sh -a $arch $1 - done - exit -fi - -# We do not put all of build-tools/$TERMUX_ANDROID_BUILD_TOOLS_VERSION/ into PATH -# to avoid stuff like arm-linux-androideabi-ld there to conflict with ones from -# the standalone toolchain. -TERMUX_DX=$ANDROID_HOME/build-tools/$TERMUX_ANDROID_BUILD_TOOLS_VERSION/dx -TERMUX_JACK=$ANDROID_HOME/build-tools/$TERMUX_ANDROID_BUILD_TOOLS_VERSION/jack.jar -TERMUX_JILL=$ANDROID_HOME/build-tools/$TERMUX_ANDROID_BUILD_TOOLS_VERSION/jill.jar -# Make $TERMUX_TAR and $TERMUX_TOUCH point at gnu versions: -export TERMUX_TAR="tar" -test `uname` = "Darwin" && TERMUX_TAR=gtar -export TERMUX_TOUCH="touch" -test `uname` = "Darwin" && TERMUX_TOUCH=gtouch - -export prefix=${TERMUX_PREFIX} # prefix is used by some makefiles -export PREFIX=${TERMUX_PREFIX} # PREFIX is used by some makefiles -export TERMUX_COMMON_CACHEDIR="$TERMUX_TOPDIR/_cache" -export TERMUX_DEBDIR="$TERMUX_SCRIPTDIR/debs" -export PKG_CONFIG_LIBDIR=$TERMUX_PREFIX/lib/pkgconfig -mkdir -p $TERMUX_COMMON_CACHEDIR $TERMUX_DEBDIR - -TERMUX_PKG_BUILDDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/build -TERMUX_PKG_CACHEDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/cache -TERMUX_PKG_MASSAGEDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/massage -TERMUX_PKG_PACKAGEDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/package -TERMUX_PKG_SRCDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/src -TERMUX_PKG_SHA256="" -TERMUX_PKG_TMPDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/tmp -TERMUX_PKG_HOSTBUILD_DIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/host-build -TERMUX_PKG_PLATFORM_INDEPENDENT="" -TERMUX_PKG_NO_DEVELSPLIT="" -TERMUX_PKG_BUILD_REVISION="0" # http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version -TERMUX_PKG_EXTRA_CONFIGURE_ARGS="" -TERMUX_PKG_EXTRA_HOSTBUILD_CONFIGURE_ARGS="" -TERMUX_PKG_EXTRA_MAKE_ARGS="" -TERMUX_PKG_BUILD_IN_SRC="" -TERMUX_PKG_RM_AFTER_INSTALL="" -TERMUX_PKG_DEPENDS="" -TERMUX_PKG_HOMEPAGE="" -TERMUX_PKG_DESCRIPTION="FIXME:Add description" -TERMUX_PKG_FOLDERNAME="" -TERMUX_PKG_KEEP_STATIC_LIBRARIES="false" -TERMUX_PKG_ESSENTIAL="" -TERMUX_PKG_CONFLICTS="" # https://www.debian.org/doc/debian-policy/ch-relationships.html#s-conflicts -TERMUX_PKG_REPLACES="" -TERMUX_PKG_CONFFILES="" -TERMUX_PKG_INCLUDE_IN_DEVPACKAGE="" -TERMUX_PKG_DEVPACKAGE_DEPENDS="" -# Set if a host build should be done in TERMUX_PKG_HOSTBUILD_DIR: -TERMUX_PKG_HOSTBUILD="" -TERMUX_PKG_MAINTAINER="Fredrik Fornwall @fornwall" -TERMUX_PKG_CLANG=no - -# Cleanup old state -rm -Rf $TERMUX_PKG_BUILDDIR $TERMUX_PKG_PACKAGEDIR $TERMUX_PKG_SRCDIR $TERMUX_PKG_TMPDIR $TERMUX_PKG_MASSAGEDIR - -# If $TERMUX_PREFIX already exists, it may have been built for a different arch -TERMUX_ARCH_FILE=/data/TERMUX_ARCH -if [ -f "${TERMUX_ARCH_FILE}" ]; then - TERMUX_PREVIOUS_ARCH=`cat $TERMUX_ARCH_FILE` - if [ $TERMUX_PREVIOUS_ARCH != $TERMUX_ARCH ]; then - TERMUX_DATA_BACKUPDIRS=$TERMUX_TOPDIR/_databackups - mkdir -p $TERMUX_DATA_BACKUPDIRS - TERMUX_DATA_PREVIOUS_BACKUPDIR=$TERMUX_DATA_BACKUPDIRS/$TERMUX_PREVIOUS_ARCH - TERMUX_DATA_CURRENT_BACKUPDIR=$TERMUX_DATA_BACKUPDIRS/$TERMUX_ARCH - # Save current /data (removing old backup if any) - if test -e $TERMUX_DATA_PREVIOUS_BACKUPDIR; then - echo "ERROR: Directory already exists" - exit 1 - fi - mv /data/data $TERMUX_DATA_PREVIOUS_BACKUPDIR - # Restore new one (if any) - if [ -d $TERMUX_DATA_CURRENT_BACKUPDIR ]; then - mv $TERMUX_DATA_CURRENT_BACKUPDIR /data/data - fi - fi -fi -echo $TERMUX_ARCH > $TERMUX_ARCH_FILE - -# Ensure folders present (but not $TERMUX_PKG_SRCDIR, it will be created in build) -mkdir -p $TERMUX_PKG_BUILDDIR $TERMUX_PKG_PACKAGEDIR $TERMUX_PKG_TMPDIR $TERMUX_PKG_CACHEDIR $TERMUX_PKG_MASSAGEDIR $PKG_CONFIG_LIBDIR $TERMUX_PREFIX/{bin,etc,lib,libexec,share,tmp,include} +} +# Utility function to download a resource, optionally checking against a checksum. termux_download() { - URL="$1" - DESTINATION="$2" - - TMPFILE=`mktemp $TERMUX_PKG_TMPDIR/download.$TERMUX_PKG_NAME.XXXXXXXXX` + local URL="$1" + local DESTINATION="$2" + local TMPFILE + TMPFILE=$(mktemp "$TERMUX_PKG_TMPDIR/download.$TERMUX_PKG_NAME.XXXXXXXXX") echo "Downloading ${URL}" - TRYMAX=6 + local TRYMAX=6 for try in $(seq 1 $TRYMAX); do if curl -L --fail --retry 2 -o "$TMPFILE" "$URL"; then - local ACTUAL_CHECKSUM=`sha256sum $TMPFILE | cut -f 1 -d ' '` - if [ $# = 3 ]; then + local ACTUAL_CHECKSUM + ACTUAL_CHECKSUM=$(sha256sum "$TMPFILE" | cut -f 1 -d ' ') + if [ $# = 3 ] && [ -n "$3" ]; then # Optional checksum argument: local EXPECTED=$3 - if [ $EXPECTED != $ACTUAL_CHECKSUM ]; then + if [ "$EXPECTED" != "$ACTUAL_CHECKSUM" ]; then >&2 printf "Wrong checksum for %s:\nExpected: %s\nActual: %s\n" \ "$URL" "$EXPECTED" "$ACTUAL_CHECKSUM" exit 1 @@ -197,40 +35,323 @@ termux_download() { mv "$TMPFILE" "$DESTINATION" return else - echo "Download of $1 failed (attempt $try/$TRYMAX)" 1>&2 + echo "Download of $URL failed (attempt $try/$TRYMAX)" 1>&2 sleep 45 fi done - echo "Failed to download $1 - exiting" 1>&2 - exit 1 + termux_error_exit "Failed to download $URL" +} + +# Utility function for golang-using packages to setup a go toolchain. +termux_setup_golang () { + export GOOS=android + export CGO_ENABLED=1 + export GO_LDFLAGS="-extldflags=-pie" + if [ "$TERMUX_ARCH" = "arm" ]; then + export GOARCH=arm + export GOARM=7 + elif [ "$TERMUX_ARCH" = "i686" ]; then + export GOARCH=386 + export GO386=sse2 + elif [ "$TERMUX_ARCH" = "aarch64" ]; then + export GOARCH=arm64 + elif [ "$TERMUX_ARCH" = "x86_64" ]; then + export GOARCH=amd64 + else + termux_error_exit "Unsupported arch: $TERMUX_ARCH" + fi + + local TERMUX_GO_VERSION=go1.7.4 + local TERMUX_GO_PLATFORM=linux-amd64 + test "$(uname)" = "Darwin" && TERMUX_GO_PLATFORM=darwin-amd64 + + export TERMUX_BUILDGO_FOLDER=$TERMUX_COMMON_CACHEDIR/${TERMUX_GO_VERSION}.${TERMUX_GO_PLATFORM} + export GOROOT=$TERMUX_BUILDGO_FOLDER + export PATH=$GOROOT/bin:$PATH + + if [ -d "$TERMUX_BUILDGO_FOLDER" ]; then return; fi + + local TERMUX_BUILDGO_TAR=$TERMUX_COMMON_CACHEDIR/${TERMUX_GO_VERSION}.${TERMUX_GO_PLATFORM}.tar.gz + rm -Rf "$TERMUX_COMMON_CACHEDIR/go" "$TERMUX_BUILDGO_FOLDER" + termux_download https://storage.googleapis.com/golang/${TERMUX_GO_VERSION}.${TERMUX_GO_PLATFORM}.tar.gz \ + "$TERMUX_BUILDGO_TAR" + ( cd "$TERMUX_COMMON_CACHEDIR"; tar xf "$TERMUX_BUILDGO_TAR"; mv go "$TERMUX_BUILDGO_FOLDER"; rm "$TERMUX_BUILDGO_TAR" ) +} + +# First step is to handle command-line arguments. Not to be overridden by packages. +termux_step_handle_arguments() { + # shellcheck source=/dev/null + test -f "$HOME/.termuxrc" && source "$HOME/.termuxrc" + + # Handle command-line arguments: + _show_usage () { + echo "Usage: ./build-package.sh [-a ARCH] [-d] [-D] PACKAGE" + echo "Build a package by creating a .deb file in the debs/ folder." + echo " -a The architecture to build for: aarch64(default), arm, i686, x86_64 or all." + echo " -d Build with debug symbols." + echo " -D Build a disabled package in disabled-packages/." + echo " -s Skip dependency check." + exit 1 + } + while getopts :a:hdDs option; do + case "$option" in + a) TERMUX_ARCH="$OPTARG";; + h) _show_usage;; + d) TERMUX_DEBUG=true;; + D) local TERMUX_IS_DISABLED=true;; + s) export TERMUX_SKIP_DEPCHECK=true;; + ?) termux_error_exit "./build-package.sh: illegal option -$OPTARG";; + esac + done + shift $((OPTIND-1)) + + if [ "$#" -ne 1 ]; then _show_usage; fi + unset -f _show_usage + + # Handle 'all' arch: + if [ -n "${TERMUX_ARCH+x}" ] && [ "${TERMUX_ARCH}" = 'all' ]; then + for arch in 'arm' 'i686' 'aarch64' 'x86_64'; do + ./build-package.sh -a $arch "$1" + done + exit + fi + + # Check the package to build: + TERMUX_PKG_NAME=$(basename "$1") + export TERMUX_SCRIPTDIR + TERMUX_SCRIPTDIR=$(cd "$(dirname "$0")"; pwd) + if [[ $1 == *"/"* ]]; then + # Path to directory which may be outside this repo: + if [ ! -d "$1" ]; then termux_error_exit "'$1' seems to be a path but is not a directory"; fi + export TERMUX_PKG_BUILDER_DIR + TERMUX_PKG_BUILDER_DIR=$(realpath "$1") + else + # Package name: + if [ -n "${TERMUX_IS_DISABLED=""}" ]; then + export TERMUX_PKG_BUILDER_DIR=$TERMUX_SCRIPTDIR/disabled-packages/$TERMUX_PKG_NAME + else + export TERMUX_PKG_BUILDER_DIR=$TERMUX_SCRIPTDIR/packages/$TERMUX_PKG_NAME + fi + fi + TERMUX_PKG_BUILDER_SCRIPT=$TERMUX_PKG_BUILDER_DIR/build.sh + if test ! -f "$TERMUX_PKG_BUILDER_SCRIPT"; then + termux_error_exit "No build.sh script at package dir $TERMUX_PKG_BUILDER_DIR!" + fi +} + +# Setup variables used by the build. Not to be overridden by packages. +termux_step_setup_variables() { + : "${ANDROID_HOME:="${HOME}/lib/android-sdk"}" + : "${NDK:="${HOME}/lib/android-ndk"}" + : "${TERMUX_MAKE_PROCESSES:="4"}" + : "${TERMUX_TOPDIR:="$HOME/.termux-build"}" + : "${TERMUX_ARCH:="aarch64"}" # arm, aarch64, i686 or x86_64. + : "${TERMUX_PREFIX:="/data/data/com.termux/files/usr"}" + : "${TERMUX_ANDROID_HOME:="/data/data/com.termux/files/home"}" + : "${TERMUX_DEBUG:=""}" + : "${TERMUX_API_LEVEL:="21"}" + : "${TERMUX_ANDROID_BUILD_TOOLS_VERSION:="24.0.1"}" + : "${TERMUX_NDK_VERSION:="13"}" + + if [ "x86_64" = "$TERMUX_ARCH" -o "aarch64" = "$TERMUX_ARCH" ]; then + TERMUX_ARCH_BITS=64 + else + TERMUX_ARCH_BITS=32 + fi + + TERMUX_HOST_PLATFORM="${TERMUX_ARCH}-linux-android" + if [ "$TERMUX_ARCH" = "arm" ]; then TERMUX_HOST_PLATFORM="${TERMUX_HOST_PLATFORM}eabi"; fi + + if [ ! -d "$NDK" ]; then + termux_error_exit 'NDK not pointing at a directory!' + fi + if ! grep -s -q "Pkg.Revision = $TERMUX_NDK_VERSION" "$NDK/source.properties"; then + termux_error_exit "Wrong NDK version - we need $TERMUX_NDK_VERSION" + fi + + # We do not put all of build-tools/$TERMUX_ANDROID_BUILD_TOOLS_VERSION/ into PATH + # to avoid stuff like arm-linux-androideabi-ld there to conflict with ones from + # the standalone toolchain. + TERMUX_DX=$ANDROID_HOME/build-tools/$TERMUX_ANDROID_BUILD_TOOLS_VERSION/dx + TERMUX_JACK=$ANDROID_HOME/build-tools/$TERMUX_ANDROID_BUILD_TOOLS_VERSION/jack.jar + TERMUX_JILL=$ANDROID_HOME/build-tools/$TERMUX_ANDROID_BUILD_TOOLS_VERSION/jill.jar + + TERMUX_COMMON_CACHEDIR="$TERMUX_TOPDIR/_cache" + TERMUX_DEBDIR="$TERMUX_SCRIPTDIR/debs" + TERMUX_ELF_CLEANER=$TERMUX_COMMON_CACHEDIR/termux-elf-cleaner + + TERMUX_STANDALONE_TOOLCHAIN="$TERMUX_TOPDIR/_lib/toolchain-${TERMUX_ARCH}-ndk${TERMUX_NDK_VERSION}-api${TERMUX_API_LEVEL}" + # Bump the below version if a change is made in toolchain setup to ensure + # that everyone gets an updated toolchain: + TERMUX_STANDALONE_TOOLCHAIN+="-v3" + + export TERMUX_TAR="tar" + export TERMUX_TOUCH="touch" + test "$(uname)" = "Darwin" && TERMUX_TAR=gtar + test "$(uname)" = "Darwin" && TERMUX_TOUCH=gtouch + export prefix=${TERMUX_PREFIX} + export PREFIX=${TERMUX_PREFIX} + export PKG_CONFIG_LIBDIR=$TERMUX_PREFIX/lib/pkgconfig + + TERMUX_PKG_BUILDDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/build + TERMUX_PKG_CACHEDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/cache + TERMUX_PKG_MASSAGEDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/massage + TERMUX_PKG_PACKAGEDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/package + TERMUX_PKG_SRCDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/src + TERMUX_PKG_SHA256="" + TERMUX_PKG_TMPDIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/tmp + TERMUX_PKG_HOSTBUILD_DIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/host-build + TERMUX_PKG_PLATFORM_INDEPENDENT="" + TERMUX_PKG_NO_DEVELSPLIT="" + TERMUX_PKG_BUILD_REVISION="0" # http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version + TERMUX_PKG_EXTRA_CONFIGURE_ARGS="" + TERMUX_PKG_EXTRA_HOSTBUILD_CONFIGURE_ARGS="" + TERMUX_PKG_EXTRA_MAKE_ARGS="" + TERMUX_PKG_BUILD_IN_SRC="" + TERMUX_PKG_RM_AFTER_INSTALL="" + TERMUX_PKG_DEPENDS="" + TERMUX_PKG_HOMEPAGE="" + TERMUX_PKG_DESCRIPTION="FIXME:Add description" + TERMUX_PKG_FOLDERNAME="" + TERMUX_PKG_KEEP_STATIC_LIBRARIES="false" + TERMUX_PKG_ESSENTIAL="" + TERMUX_PKG_CONFLICTS="" # https://www.debian.org/doc/debian-policy/ch-relationships.html#s-conflicts + TERMUX_PKG_REPLACES="" + TERMUX_PKG_CONFFILES="" + TERMUX_PKG_INCLUDE_IN_DEVPACKAGE="" + TERMUX_PKG_DEVPACKAGE_DEPENDS="" + # Set if a host build should be done in TERMUX_PKG_HOSTBUILD_DIR: + TERMUX_PKG_HOSTBUILD="" + TERMUX_PKG_MAINTAINER="Fredrik Fornwall @fornwall" + TERMUX_PKG_CLANG=no + + unset CFLAGS CPPFLAGS LDFLAGS CXXFLAGS +} + +# Save away and restore build setups which may change between builds. +termux_step_handle_buildarch() { + # If $TERMUX_PREFIX already exists, it may have been built for a different arch + local TERMUX_ARCH_FILE=/data/TERMUX_ARCH + if [ -f "${TERMUX_ARCH_FILE}" ]; then + local TERMUX_PREVIOUS_ARCH + TERMUX_PREVIOUS_ARCH=$(cat $TERMUX_ARCH_FILE) + if [ "$TERMUX_PREVIOUS_ARCH" != "$TERMUX_ARCH" ]; then + local TERMUX_DATA_BACKUPDIRS=$TERMUX_TOPDIR/_databackups + mkdir -p "$TERMUX_DATA_BACKUPDIRS" + local TERMUX_DATA_PREVIOUS_BACKUPDIR=$TERMUX_DATA_BACKUPDIRS/$TERMUX_PREVIOUS_ARCH + local TERMUX_DATA_CURRENT_BACKUPDIR=$TERMUX_DATA_BACKUPDIRS/$TERMUX_ARCH + # Save current /data (removing old backup if any) + if test -e "$TERMUX_DATA_PREVIOUS_BACKUPDIR"; then + termux_error_exit "Directory already exists" + fi + mv /data/data "$TERMUX_DATA_PREVIOUS_BACKUPDIR" + # Restore new one (if any) + if [ -d "$TERMUX_DATA_CURRENT_BACKUPDIR" ]; then + mv "$TERMUX_DATA_CURRENT_BACKUPDIR" /data/data + fi + fi + fi + + # Keep track of current arch we are building for. + echo "$TERMUX_ARCH" > $TERMUX_ARCH_FILE } -# Have a debian-binary file ready for deb packaging: -test ! -f $TERMUX_COMMON_CACHEDIR/debian-binary && echo "2.0" > $TERMUX_COMMON_CACHEDIR/debian-binary -# The host tuple that may be given to --host configure flag, but normally autodetected so not needed explicitly -TERMUX_HOST_TUPLE=`sh $TERMUX_SCRIPTDIR/scripts/config.guess` +# Source the package build script and start building. No to be overridden by packages. +termux_step_start_build() { + # shellcheck source=/dev/null + source "$TERMUX_PKG_BUILDER_SCRIPT" -# Make $TERMUX_PREFIX/bin/sh executable on the builder, so that build script can assume that it works -# on both builder and host later on: -ln -f -s /bin/sh $TERMUX_PREFIX/bin/sh + if [ -n "${TERMUX_PKG_BLACKLISTED_ARCHES:=""}" ] && [ "$TERMUX_PKG_BLACKLISTED_ARCHES" != "${TERMUX_PKG_BLACKLISTED_ARCHES/$TERMUX_ARCH/}" ]; then + echo "Skipping building $TERMUX_PKG_NAME for arch $TERMUX_ARCH" + exit 0 + fi + + if [ -z "${TERMUX_SKIP_DEPCHECK:=""}" ]; then + for p in $(./scripts/buildorder.py "$TERMUX_PKG_NAME"); do + if [ "$p" != "$TERMUX_PKG_NAME" ]; then + echo "Building dependency $p if necessary..." + ./build-package.sh -a $TERMUX_ARCH -s "$p" + fi + done + fi -TERMUX_ELF_CLEANER=$TERMUX_COMMON_CACHEDIR/termux-elf-cleaner -TERMUX_ELF_CLEANER_SRC=$TERMUX_SCRIPTDIR/packages/termux-elf-cleaner/termux-elf-cleaner.cpp -if [ $TERMUX_ELF_CLEANER_SRC -nt $TERMUX_ELF_CLEANER ]; then - g++ -std=c++11 -Wall -Wextra -pedantic -Os $TERMUX_ELF_CLEANER_SRC -o $TERMUX_ELF_CLEANER -fi + TERMUX_PKG_FULLVERSION=$TERMUX_PKG_VERSION + if [ "$TERMUX_PKG_BUILD_REVISION" != "0" ] || [ "$TERMUX_PKG_FULLVERSION" != "${TERMUX_PKG_FULLVERSION/-/}" ]; then + # "0" is the default revision, so only include it if the upstream versions contains "-" itself + TERMUX_PKG_FULLVERSION+="-$TERMUX_PKG_BUILD_REVISION" + fi -# Run just after sourcing $TERMUX_PKG_BUILDER_SCRIPT + if [ -z "$TERMUX_DEBUG" ] && [ -e "/data/data/.built-packages/$TERMUX_PKG_NAME" ]; then + if [ "$(cat "/data/data/.built-packages/$TERMUX_PKG_NAME")" = "$TERMUX_PKG_FULLVERSION" ]; then + echo "$TERMUX_PKG_NAME@$TERMUX_PKG_FULLVERSION built - skipping (rm /data/data/.built-packages/$TERMUX_PKG_NAME to force rebuild)" + exit 0 + fi + fi + + # Cleanup old state: + rm -Rf "$TERMUX_PKG_BUILDDIR" \ + "$TERMUX_PKG_PACKAGEDIR" \ + "$TERMUX_PKG_SRCDIR" \ + "$TERMUX_PKG_TMPDIR" \ + "$TERMUX_PKG_MASSAGEDIR" + + # Ensure folders present (but not $TERMUX_PKG_SRCDIR, it will be created in build) + mkdir -p "$TERMUX_COMMON_CACHEDIR" \ + "$TERMUX_DEBDIR" \ + "$TERMUX_PKG_BUILDDIR" \ + "$TERMUX_PKG_PACKAGEDIR" \ + "$TERMUX_PKG_TMPDIR" \ + "$TERMUX_PKG_CACHEDIR" \ + "$TERMUX_PKG_MASSAGEDIR" \ + $PKG_CONFIG_LIBDIR \ + $TERMUX_PREFIX/{bin,etc,lib,libexec,share,tmp,include} + + # Make $TERMUX_PREFIX/bin/sh executable on the builder, so that build + # scripts can assume that it works on both builder and host later on: + ln -f -s /bin/sh "$TERMUX_PREFIX/bin/sh" + + TERMUX_ELF_CLEANER_SRC=$TERMUX_SCRIPTDIR/packages/termux-elf-cleaner/termux-elf-cleaner.cpp + if [ "$TERMUX_ELF_CLEANER_SRC" -nt "$TERMUX_ELF_CLEANER" ]; then + g++ -std=c++11 -Wall -Wextra -pedantic -Os "$TERMUX_ELF_CLEANER_SRC" -o "$TERMUX_ELF_CLEANER" + fi + + if [ -n "$TERMUX_PKG_BUILD_IN_SRC" ]; then + echo "Building in src due to TERMUX_PKG_BUILD_IN_SRC being set" > "$TERMUX_PKG_BUILDDIR/BUILDING_IN_SRC.txt" + TERMUX_PKG_BUILDDIR=$TERMUX_PKG_SRCDIR + fi + + echo "termux - building $TERMUX_PKG_NAME for arch $TERMUX_ARCH..." + test -t 1 && printf "\033]0;%s...\007" "$TERMUX_PKG_NAME" + + # Add a pkg-config file for the system zlib + cat > "$PKG_CONFIG_LIBDIR/zlib.pc" <<-HERE + Name: zlib + Description: zlib compression library + Version: 1.2.8 + Requires: + Libs: -lz + HERE + + # Keep track of when build started so we can see what files have been created. + # We start by sleeping so that any generated files above (such as zlib.pc) get + # an older timestamp than the TERMUX_BUILD_TS_FILE. + sleep 1 + TERMUX_BUILD_TS_FILE=$TERMUX_PKG_TMPDIR/timestamp_$TERMUX_PKG_NAME + touch "$TERMUX_BUILD_TS_FILE" +} + +# Run just after sourcing $TERMUX_PKG_BUILDER_SCRIPT. May be overridden by packages. termux_step_extract_package () { - if [ -z "${TERMUX_PKG_SRCURL:=""}" ]; then - mkdir -p $TERMUX_PKG_SRCDIR - return - fi - cd $TERMUX_PKG_TMPDIR - filename=`basename $TERMUX_PKG_SRCURL` - file=$TERMUX_PKG_CACHEDIR/$filename - test ! -f $file && termux_download $TERMUX_PKG_SRCURL $file $TERMUX_PKG_SHA256 + if [ -z "${TERMUX_PKG_SRCURL:=""}" ]; then + mkdir -p "$TERMUX_PKG_SRCDIR" + return + fi + cd "$TERMUX_PKG_TMPDIR" + local filename + filename=$(basename "$TERMUX_PKG_SRCURL") + local file="$TERMUX_PKG_CACHEDIR/$filename" + test ! -f "$file" && termux_download "$TERMUX_PKG_SRCURL" "$file" "$TERMUX_PKG_SHA256" if [ "x$TERMUX_PKG_FOLDERNAME" = "x" ]; then folder=`basename $filename .tar.bz2` && folder=`basename $folder .tar.gz` && folder=`basename $folder .tar.xz` && folder=`basename $folder .tar.lz` && folder=`basename $folder .tgz` && folder=`basename $folder .zip` @@ -239,47 +360,220 @@ termux_step_extract_package () { folder=$TERMUX_PKG_FOLDERNAME fi rm -Rf $folder - if [ ${file##*.} = zip ]; then - unzip -q $file + if [ "${file##*.}" = zip ]; then + unzip -q "$file" else - $TERMUX_TAR xf $file + $TERMUX_TAR xf "$file" fi - mv $folder $TERMUX_PKG_SRCDIR + mv $folder "$TERMUX_PKG_SRCDIR" } +# Hook for packages to act just after the package has been extracted. termux_step_post_extract_package () { return } +# Optional host build. Not to be overridden by packages. +termux_step_handle_hostbuild() { + if [ "x$TERMUX_PKG_HOSTBUILD" = "x" ]; then return; fi + cd "$TERMUX_PKG_SRCDIR" + for patch in $TERMUX_PKG_BUILDER_DIR/*.patch.beforehostbuild; do + test -f "$patch" && sed "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" "$patch" | patch --silent -p1 + done + mkdir -p "$TERMUX_PKG_HOSTBUILD_DIR" + if [ -f "$TERMUX_PKG_HOSTBUILD_DIR/TERMUX_BUILT_FOR_$TERMUX_PKG_VERSION" ]; then + echo "Using already built host build" + else + cd "$TERMUX_PKG_HOSTBUILD_DIR" + termux_step_host_build + touch $TERMUX_PKG_HOSTBUILD_DIR/TERMUX_BUILT_FOR_$TERMUX_PKG_VERSION + fi +} + # Perform a host build. Will be called in $TERMUX_PKG_HOSTBUILD_DIR. # After termux_step_post_extract_package() and before termux_step_patch_package() termux_step_host_build () { - $TERMUX_PKG_SRCDIR/configure ${TERMUX_PKG_EXTRA_HOSTBUILD_CONFIGURE_ARGS} + "$TERMUX_PKG_SRCDIR/configure" ${TERMUX_PKG_EXTRA_HOSTBUILD_CONFIGURE_ARGS} make -j $TERMUX_MAKE_PROCESSES } -# This should not be overridden +# Setup a standalone Android NDK toolchain. Not to be overridden by packages. +termux_step_setup_toolchain() { + # We put this after system PATH to avoid picking up toolchain stripped python + export PATH=$PATH:$TERMUX_STANDALONE_TOOLCHAIN/bin + + export AR=$TERMUX_HOST_PLATFORM-ar + if [ "$TERMUX_PKG_CLANG" = "no" ]; then + export AS=${TERMUX_HOST_PLATFORM}-gcc + export CC=$TERMUX_HOST_PLATFORM-gcc + export CXX=$TERMUX_HOST_PLATFORM-g++ + _SPECSFLAG=" -specs=$TERMUX_SCRIPTDIR/termux.spec" + else + export AS=${TERMUX_HOST_PLATFORM}-gcc + export CC=$TERMUX_HOST_PLATFORM-clang + export CXX=$TERMUX_HOST_PLATFORM-clang++ + # TODO: clang does not have specs file, how to ensure pie + # binaries gets built? + _SPECSFLAG="" + fi + export CPP=${TERMUX_HOST_PLATFORM}-cpp + export CC_FOR_BUILD=gcc + export LD=$TERMUX_HOST_PLATFORM-ld + export OBJDUMP=$TERMUX_HOST_PLATFORM-objdump + # Setup pkg-config for cross-compiling: + export PKG_CONFIG=$TERMUX_STANDALONE_TOOLCHAIN/bin/${TERMUX_HOST_PLATFORM}-pkg-config + export RANLIB=$TERMUX_HOST_PLATFORM-ranlib + export READELF=$TERMUX_HOST_PLATFORM-readelf + export STRIP=$TERMUX_HOST_PLATFORM-strip + + export CFLAGS="$_SPECSFLAG" + export LDFLAGS="$_SPECSFLAG -L${TERMUX_PREFIX}/lib" + # Android 7 started to support DT_RUNPATH (but not DT_RPATH), so we may want + # LDFLAGS+="-Wl,-rpath=$TERMUX_PREFIX/lib -Wl,--enable-new-dtags" + # and no longer remove DT_RUNPATH in termux-elf-cleaner. + + if [ "$TERMUX_ARCH" = "arm" ]; then + CFLAGS+=" -march=armv7-a -mfpu=neon -mfloat-abi=softfp" + # "first flag instructs the linker to pick libgcc.a, libgcov.a, and + # crt*.o, which are tailored for armv7-a" + # - https://developer.android.com/ndk/guides/standalone_toolchain.html + LDFLAGS+=" -march=armv7-a -Wl,--fix-cortex-a8" + elif [ "$TERMUX_ARCH" = "i686" ]; then + # From $NDK/docs/CPU-ARCH-ABIS.html: + CFLAGS+=" -march=i686 -msse3 -mstackrealign -mfpmath=sse" + elif [ "$TERMUX_ARCH" = "aarch64" ]; then + LDFLAGS+=" -Wl,-rpath-link,$TERMUX_PREFIX/lib" + LDFLAGS+=" -Wl,-rpath-link,$TERMUX_STANDALONE_TOOLCHAIN/sysroot/usr/lib" + elif [ "$TERMUX_ARCH" = "x86_64" ]; then + : + else + termux_error_exit "Invalid arch '$TERMUX_ARCH' - support arches are 'arm', 'i686', 'aarch64', 'x86_64'" + fi + + if [ -n "$TERMUX_DEBUG" ]; then + CFLAGS+=" -g3 -O1 -fstack-protector --param ssp-buffer-size=4 -D_FORTIFY_SOURCE=2" + else + CFLAGS+=" -Os" + fi + + export CXXFLAGS="$CFLAGS" + export CPPFLAGS="-I${TERMUX_PREFIX}/include" + + if [ "$TERMUX_PKG_DEPENDS" != "${TERMUX_PKG_DEPENDS/libandroid-support/}" ]; then + # If using the android support library, link to it and include its headers as system headers: + CPPFLAGS+=" -isystem $TERMUX_PREFIX/include/libandroid-support" + LDFLAGS+=" -landroid-support" + fi + + export ac_cv_func_getpwent=no + export ac_cv_func_getpwnam=no + export ac_cv_func_getpwuid=no + + if [ ! -d $TERMUX_STANDALONE_TOOLCHAIN ]; then + # Do not put toolchain in place until we are done with setup, to avoid having a half setup + # toolchain left in place if something goes wrong (or process is just aborted): + _TERMUX_TOOLCHAIN_TMPDIR=${TERMUX_STANDALONE_TOOLCHAIN}-tmp + rm -Rf $_TERMUX_TOOLCHAIN_TMPDIR + + local _NDK_ARCHNAME=$TERMUX_ARCH + if [ "$TERMUX_ARCH" = "aarch64" ]; then + _NDK_ARCHNAME=arm64 + elif [ "$TERMUX_ARCH" = "i686" ]; then + _NDK_ARCHNAME=x86 + fi + "$NDK/build/tools/make_standalone_toolchain.py" \ + --api "$TERMUX_API_LEVEL" \ + --arch $_NDK_ARCHNAME \ + --install-dir $_TERMUX_TOOLCHAIN_TMPDIR + + if [ "$TERMUX_ARCH" = "arm" ]; then + # Fix to allow e.g. to be included: + cp $_TERMUX_TOOLCHAIN_TMPDIR/include/c++/4.9.x/arm-linux-androideabi/armv7-a/bits/* \ + $_TERMUX_TOOLCHAIN_TMPDIR/include/c++/4.9.x/bits + fi + + cd $_TERMUX_TOOLCHAIN_TMPDIR/sysroot + + for f in $TERMUX_SCRIPTDIR/ndk_patches/*.patch; do + sed "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" "$f" | \ + sed "s%\@TERMUX_HOME\@%${TERMUX_ANDROID_HOME}%g" | \ + patch --silent -p1; + done + # elf.h is taken from glibc since the elf.h in the NDK is lacking. + # sysexits.h is header-only and used by a few programs. + cp "$TERMUX_SCRIPTDIR"/ndk_patches/{elf.h,sysexits.h} $_TERMUX_TOOLCHAIN_TMPDIR/sysroot/usr/include + + $TERMUX_ELF_CLEANER usr/lib/*.so + + # zlib is really version 1.2.8 in the Android platform (at least + # starting from Android 5), not older as the NDK headers claim. + for file in zconf.h zlib.h; do + curl -o $_TERMUX_TOOLCHAIN_TMPDIR/sysroot/usr/include/$file \ + https://raw.githubusercontent.com/madler/zlib/v1.2.8/$file + done + unset file + + mv $_TERMUX_TOOLCHAIN_TMPDIR $TERMUX_STANDALONE_TOOLCHAIN + fi + + if [ ! -f $TERMUX_PREFIX/lib/libstdc++.so ]; then + # Setup libgnustl_shared.so in $PREFIX/lib and libstdc++.so as a symlink to it, + # so that other C++ using packages links to it instead of the default android + # C++ library which does not support exceptions or STL: + # https://developer.android.com/ndk/guides/cpp-support.html + # We do however want to avoid installing this, to avoid problems where e.g. + # libm.so on some i686 devices links against libstdc++.so. + # The libgnustl_shared.so library will be packaged in the libgnustl package + # which is part of the base Termux installation. + mkdir -p "$TERMUX_PREFIX/lib" + cd "$TERMUX_PREFIX/lib" + _STL_LIBFILE=$TERMUX_STANDALONE_TOOLCHAIN/${TERMUX_HOST_PLATFORM}/lib/libgnustl_shared.so + if [ "$TERMUX_ARCH" = arm ]; then + _STL_LIBFILE=$TERMUX_STANDALONE_TOOLCHAIN/${TERMUX_HOST_PLATFORM}/lib/armv7-a/libgnustl_shared.so + elif [ "$TERMUX_ARCH" = x86_64 ]; then + _STL_LIBFILE=$TERMUX_STANDALONE_TOOLCHAIN/${TERMUX_HOST_PLATFORM}/lib64/libgnustl_shared.so + fi + cp "$_STL_LIBFILE" . + ln -f -s libgnustl_shared.so libstdc++.so + fi + + # Create a pkg-config wrapper. We use path to host pkg-config to + # avoid picking up a cross-compiled pkg-config later on. + local _HOST_PKGCONFIG + _HOST_PKGCONFIG=$(which pkg-config) + mkdir -p $TERMUX_STANDALONE_TOOLCHAIN/bin "$PKG_CONFIG_LIBDIR" + cat > "$PKG_CONFIG" <<-HERE + #!/bin/sh + export PKG_CONFIG_DIR= + export PKG_CONFIG_LIBDIR=$PKG_CONFIG_LIBDIR + exec $_HOST_PKGCONFIG "\$@" + HERE + chmod +x "$PKG_CONFIG" +} + +# This should not be overridden by packages. termux_step_patch_package () { - cd $TERMUX_PKG_SRCDIR + cd "$TERMUX_PKG_SRCDIR" # Suffix patch with ".patch32" or ".patch64" to only apply for these bitnesses: for patch in $TERMUX_PKG_BUILDER_DIR/*.patch{$TERMUX_ARCH_BITS,}; do - test -f $patch && sed "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" $patch | \ + test -f "$patch" && sed "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" "$patch" | \ sed "s%\@TERMUX_HOME\@%${TERMUX_ANDROID_HOME}%g" | \ patch --silent -p1 done - find . -name config.sub -exec chmod u+w '{}' \; -exec cp $TERMUX_SCRIPTDIR/scripts/config.sub '{}' \; - find . -name config.guess -exec chmod u+w '{}' \; -exec cp $TERMUX_SCRIPTDIR/scripts/config.guess '{}' \; + find . -name config.sub -exec chmod u+w '{}' \; -exec cp "$TERMUX_SCRIPTDIR/scripts/config.sub" '{}' \; + find . -name config.guess -exec chmod u+w '{}' \; -exec cp "$TERMUX_SCRIPTDIR/scripts/config.guess" '{}' \; + # The host tuple that may be given to --host configure flag, but normally autodetected so not needed explicitly + TERMUX_HOST_TUPLE=$(sh "$TERMUX_SCRIPTDIR/scripts/config.guess") } +# For package scripts to override. Called in $TERMUX_PKG_BUILDDIR. termux_step_pre_configure () { - return + return } termux_step_configure () { - if [ ! -e $TERMUX_PKG_SRCDIR/configure ]; then - return - fi + if [ ! -e "$TERMUX_PKG_SRCDIR/configure" ]; then return; fi DISABLE_STATIC="--disable-static" if [ "$TERMUX_PKG_EXTRA_CONFIGURE_ARGS" != "${TERMUX_PKG_EXTRA_CONFIGURE_ARGS/--enable-static/}" ]; then @@ -303,9 +597,9 @@ termux_step_configure () { fi # Some packages provides a $PKG-config script which some configure scripts pickup instead of pkg-config: - mkdir $TERMUX_PKG_TMPDIR/config-scripts + mkdir "$TERMUX_PKG_TMPDIR/config-scripts" for f in $TERMUX_PREFIX/bin/*config; do - test -f $f && cp $f $TERMUX_PKG_TMPDIR/config-scripts + test -f "$f" && cp "$f" "$TERMUX_PKG_TMPDIR/config-scripts" done export PATH=$TERMUX_PKG_TMPDIR/config-scripts:$PATH @@ -320,10 +614,11 @@ termux_step_configure () { # Remove rpl_gettimeofday reference when building at least coreutils: AVOID_AUTOCONF_WRAPPERS+=" gl_cv_func_tzset_clobber=no gl_cv_func_gettimeofday_clobber=no gl_cv_func_gettimeofday_posix_signature=yes" - env $AVOID_AUTOCONF_WRAPPERS $TERMUX_PKG_SRCDIR/configure \ + # NOTE: We do not want to quote AVOID_AUTOCONF_WRAPPERS as we want word expansion. + env $AVOID_AUTOCONF_WRAPPERS "$TERMUX_PKG_SRCDIR/configure" \ --disable-dependency-tracking \ --prefix=$TERMUX_PREFIX \ - --disable-rpath --disable-rpath-hack \ + --disable-rpath --disable-rpath-hack \ $HOST_FLAG \ $TERMUX_PKG_EXTRA_CONFIGURE_ARGS \ $DISABLE_NLS \ @@ -333,54 +628,52 @@ termux_step_configure () { } termux_step_post_configure () { - return -} - -termux_step_pre_make () { - return + return } termux_step_make () { - if ls *akefile &> /dev/null; then - if [ -z "$TERMUX_PKG_EXTRA_MAKE_ARGS" ]; then - make -j $TERMUX_MAKE_PROCESSES - else - make -j $TERMUX_MAKE_PROCESSES ${TERMUX_PKG_EXTRA_MAKE_ARGS} - fi - fi + if ls *akefile &> /dev/null; then + if [ -z "$TERMUX_PKG_EXTRA_MAKE_ARGS" ]; then + make -j $TERMUX_MAKE_PROCESSES + else + make -j $TERMUX_MAKE_PROCESSES ${TERMUX_PKG_EXTRA_MAKE_ARGS} + fi + fi } termux_step_make_install () { - if ls *akefile &> /dev/null; then - : ${TERMUX_PKG_MAKE_INSTALL_TARGET:="install"}: - # Some packages have problem with parallell install, and it does not buy much, so use -j 1. - if [ -z "$TERMUX_PKG_EXTRA_MAKE_ARGS" ]; then - make -j 1 ${TERMUX_PKG_MAKE_INSTALL_TARGET} - else - make -j 1 ${TERMUX_PKG_EXTRA_MAKE_ARGS} ${TERMUX_PKG_MAKE_INSTALL_TARGET} - fi - fi + if ls *akefile &> /dev/null; then + : ${TERMUX_PKG_MAKE_INSTALL_TARGET:="install"}: + # Some packages have problem with parallell install, and it does not buy much, so use -j 1. + if [ -z "$TERMUX_PKG_EXTRA_MAKE_ARGS" ]; then + make -j 1 ${TERMUX_PKG_MAKE_INSTALL_TARGET} + else + make -j 1 ${TERMUX_PKG_EXTRA_MAKE_ARGS} ${TERMUX_PKG_MAKE_INSTALL_TARGET} + fi + fi } +# Hook function for package scripts to override. termux_step_post_make_install () { - return + return } termux_step_extract_into_massagedir () { - TARBALL_ORIG=$TERMUX_PKG_PACKAGEDIR/${TERMUX_PKG_NAME}_orig.tar.gz + local TARBALL_ORIG=$TERMUX_PKG_PACKAGEDIR/${TERMUX_PKG_NAME}_orig.tar.gz # Build diff tar with what has changed during the build: cd $TERMUX_PREFIX - $TERMUX_TAR -N $TERMUX_BUILD_TS_FILE -czf $TARBALL_ORIG . + $TERMUX_TAR -N "$TERMUX_BUILD_TS_FILE" -czf "$TARBALL_ORIG" . # Extract tar in order to massage it - mkdir -p $TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX - cd $TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX - $TERMUX_TAR xf $TARBALL_ORIG + mkdir -p "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX" + cd "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX" + $TERMUX_TAR xf "$TARBALL_ORIG" + rm "$TARBALL_ORIG" } termux_step_massage () { - cd $TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX + cd "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX" # Remove lib/charset.alias which is installed by gettext-using packages: rm -f lib/charset.alias @@ -388,14 +681,14 @@ termux_step_massage () { # Remove non-english man pages: test -d share/man && (cd share/man; for f in `ls | grep -v man`; do rm -Rf $f; done ) - if [ -z ${TERMUX_PKG_KEEP_INFOPAGES+x} ]; then + if [ -z "${TERMUX_PKG_KEEP_INFOPAGES+x}" ]; then # Remove info pages: rm -Rf share/info fi # Remove locale files we're not interested in:: rm -Rf share/locale - if [ -z ${TERMUX_PKG_KEEP_SHARE_DOC+x} ]; then + if [ -z "${TERMUX_PKG_KEEP_SHARE_DOC+x}" ]; then # Remove info pages: rm -Rf share/doc fi @@ -410,7 +703,7 @@ termux_step_massage () { fi # Move over sbin to bin: - for file in sbin/*; do if test -f $file; then mv $file bin/; fi; done + for file in sbin/*; do if test -f "$file"; then mv "$file" bin/; fi; done # Remove world permissions and add write permissions. # The -f flag is used to suppress warnings about dangling symlinks (such @@ -420,11 +713,11 @@ termux_step_massage () { # Strip binaries. file(1) may fail for certain unusual files, so disable pipefail. set +e +o pipefail find . -type f | xargs -r file | grep -E "(executable|shared object)" | grep ELF | cut -f 1 -d : | \ - xargs -r $STRIP --strip-unneeded --preserve-dates + xargs -r "$STRIP" --strip-unneeded --preserve-dates set -e -o pipefail fi # Remove DT_ entries which the android 5.1 linker warns about: - find . -type f -print0 | xargs -r -0 $TERMUX_ELF_CLEANER + find . -type f -print0 | xargs -r -0 "$TERMUX_ELF_CLEANER" # Fix shebang paths: for file in `find -L . -type f`; do @@ -435,475 +728,190 @@ termux_step_massage () { find . -type d -empty -delete # Remove empty directories - # Sub packages: - if [ -d include -a -z "${TERMUX_PKG_NO_DEVELSPLIT}" ]; then - # Add virtual -dev sub package if there are include files: - _DEVEL_SUBPACKAGE_FILE=$TERMUX_PKG_TMPDIR/${TERMUX_PKG_NAME}-dev.subpackage.sh - echo TERMUX_SUBPKG_INCLUDE=\"include share/man/man3 lib/pkgconfig share/aclocal $TERMUX_PKG_INCLUDE_IN_DEVPACKAGE\" > $_DEVEL_SUBPACKAGE_FILE - echo TERMUX_SUBPKG_DESCRIPTION=\"Development files for ${TERMUX_PKG_NAME}\" >> $_DEVEL_SUBPACKAGE_FILE + # Sub packages: + if [ -d include ] && [ -z "${TERMUX_PKG_NO_DEVELSPLIT}" ]; then + # Add virtual -dev sub package if there are include files: + local _DEVEL_SUBPACKAGE_FILE=$TERMUX_PKG_TMPDIR/${TERMUX_PKG_NAME}-dev.subpackage.sh + echo TERMUX_SUBPKG_INCLUDE=\"include share/man/man3 lib/pkgconfig share/aclocal $TERMUX_PKG_INCLUDE_IN_DEVPACKAGE\" > $_DEVEL_SUBPACKAGE_FILE + echo TERMUX_SUBPKG_DESCRIPTION=\"Development files for ${TERMUX_PKG_NAME}\" >> $_DEVEL_SUBPACKAGE_FILE if [ -n "$TERMUX_PKG_DEVPACKAGE_DEPENDS" ]; then echo TERMUX_SUBPKG_DEPENDS=\"$TERMUX_PKG_NAME,$TERMUX_PKG_DEVPACKAGE_DEPENDS\" >> $_DEVEL_SUBPACKAGE_FILE else - echo TERMUX_SUBPKG_DEPENDS=\"$TERMUX_PKG_NAME\" >> $_DEVEL_SUBPACKAGE_FILE + echo TERMUX_SUBPKG_DEPENDS=\"$TERMUX_PKG_NAME\" >> "$_DEVEL_SUBPACKAGE_FILE" fi if [ x$TERMUX_PKG_CONFLICTS != x ]; then # Assume that dev packages conflicts as well. - echo "TERMUX_SUBPKG_CONFLICTS=${TERMUX_PKG_CONFLICTS}-dev" >> $_DEVEL_SUBPACKAGE_FILE + echo "TERMUX_SUBPKG_CONFLICTS=${TERMUX_PKG_CONFLICTS}-dev" >> "$_DEVEL_SUBPACKAGE_FILE" fi - fi - # Now build all sub packages - rm -Rf $TERMUX_TOPDIR/$TERMUX_PKG_NAME/subpackages + fi + # Now build all sub packages + rm -Rf "$TERMUX_TOPDIR/$TERMUX_PKG_NAME/subpackages" for subpackage in $TERMUX_PKG_BUILDER_DIR/*.subpackage.sh $TERMUX_PKG_TMPDIR/*subpackage.sh; do - test ! -f $subpackage && continue - SUB_PKG_NAME=`basename $subpackage .subpackage.sh` - # Default value is same as main package, but sub package may override: - TERMUX_SUBPKG_PLATFORM_INDEPENDENT=$TERMUX_PKG_PLATFORM_INDEPENDENT - SUB_PKG_DIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/subpackages/$SUB_PKG_NAME - TERMUX_SUBPKG_DEPENDS="" - TERMUX_SUBPKG_CONFLICTS="" - SUB_PKG_MASSAGE_DIR=$SUB_PKG_DIR/massage/$TERMUX_PREFIX - SUB_PKG_PACKAGE_DIR=$SUB_PKG_DIR/package - mkdir -p $SUB_PKG_MASSAGE_DIR $SUB_PKG_PACKAGE_DIR - - . $subpackage - - for includeset in $TERMUX_SUBPKG_INCLUDE; do - _INCLUDE_DIRSET=`dirname $includeset` - test "$_INCLUDE_DIRSET" = "." && _INCLUDE_DIRSET="" - if [ -e $includeset -o -L $includeset ]; then + test ! -f "$subpackage" && continue + local SUB_PKG_NAME + SUB_PKG_NAME=$(basename "$subpackage" .subpackage.sh) + # Default value is same as main package, but sub package may override: + local TERMUX_SUBPKG_PLATFORM_INDEPENDENT=$TERMUX_PKG_PLATFORM_INDEPENDENT + local SUB_PKG_DIR=$TERMUX_TOPDIR/$TERMUX_PKG_NAME/subpackages/$SUB_PKG_NAME + local TERMUX_SUBPKG_DEPENDS="" + local TERMUX_SUBPKG_CONFLICTS="" + local SUB_PKG_MASSAGE_DIR=$SUB_PKG_DIR/massage/$TERMUX_PREFIX + local SUB_PKG_PACKAGE_DIR=$SUB_PKG_DIR/package + mkdir -p "$SUB_PKG_MASSAGE_DIR" "$SUB_PKG_PACKAGE_DIR" + + # shellcheck source=/dev/null + source $subpackage + + for includeset in $TERMUX_SUBPKG_INCLUDE; do + local _INCLUDE_DIRSET + _INCLUDE_DIRSET=$(dirname "$includeset") + test "$_INCLUDE_DIRSET" = "." && _INCLUDE_DIRSET="" + if [ -e "$includeset" ] || [ -L "$includeset" ]; then # Add the -L clause to handle relative symbolic links: - mkdir -p $SUB_PKG_MASSAGE_DIR/$_INCLUDE_DIRSET - mv $includeset $SUB_PKG_MASSAGE_DIR/$_INCLUDE_DIRSET - fi - done + mkdir -p "$SUB_PKG_MASSAGE_DIR/$_INCLUDE_DIRSET" + mv "$includeset" "$SUB_PKG_MASSAGE_DIR/$_INCLUDE_DIRSET" + fi + done - SUB_PKG_ARCH=$TERMUX_ARCH - test -n "$TERMUX_SUBPKG_PLATFORM_INDEPENDENT" && SUB_PKG_ARCH=all + local SUB_PKG_ARCH=$TERMUX_ARCH + test -n "$TERMUX_SUBPKG_PLATFORM_INDEPENDENT" && SUB_PKG_ARCH=all - cd $SUB_PKG_DIR/massage - SUB_PKG_INSTALLSIZE=`du -sk . | cut -f 1` - $TERMUX_TAR -cJf $SUB_PKG_PACKAGE_DIR/data.tar.xz . + cd "$SUB_PKG_DIR/massage" + local SUB_PKG_INSTALLSIZE + SUB_PKG_INSTALLSIZE=$(du -sk . | cut -f 1) + $TERMUX_TAR -cJf "$SUB_PKG_PACKAGE_DIR/data.tar.xz" . - mkdir -p DEBIAN + mkdir -p DEBIAN cd DEBIAN - cat > control <> control - test ! -z "$TERMUX_SUBPKG_CONFLICTS" && echo "Conflicts: $TERMUX_SUBPKG_CONFLICTS" >> control - $TERMUX_TAR -cJf $SUB_PKG_PACKAGE_DIR/control.tar.xz . - - # Create the actual .deb file: - TERMUX_SUBPKG_DEBFILE=$TERMUX_DEBDIR/${SUB_PKG_NAME}_${TERMUX_PKG_FULLVERSION}_${SUB_PKG_ARCH}.deb - ar cr $TERMUX_SUBPKG_DEBFILE \ - $TERMUX_COMMON_CACHEDIR/debian-binary \ - $SUB_PKG_PACKAGE_DIR/control.tar.xz \ - $SUB_PKG_PACKAGE_DIR/data.tar.xz - if [ "$TERMUX_PROCESS_DEB" != "" ]; then - $TERMUX_PROCESS_DEB $TERMUX_SUBPKG_DEBFILE - fi - - # Go back to main package: - cd $TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX + cat > control <<-HERE + Package: $SUB_PKG_NAME + Architecture: ${SUB_PKG_ARCH} + Installed-Size: ${SUB_PKG_INSTALLSIZE} + Maintainer: $TERMUX_PKG_MAINTAINER + Version: $TERMUX_PKG_FULLVERSION + Description: $TERMUX_SUBPKG_DESCRIPTION + Homepage: $TERMUX_PKG_HOMEPAGE + HERE + test ! -z "$TERMUX_SUBPKG_DEPENDS" && echo "Depends: $TERMUX_SUBPKG_DEPENDS" >> control + test ! -z "$TERMUX_SUBPKG_CONFLICTS" && echo "Conflicts: $TERMUX_SUBPKG_CONFLICTS" >> control + $TERMUX_TAR -cJf "$SUB_PKG_PACKAGE_DIR/control.tar.xz" . + + # Create the actual .deb file: + TERMUX_SUBPKG_DEBFILE=$TERMUX_DEBDIR/${SUB_PKG_NAME}_${TERMUX_PKG_FULLVERSION}_${SUB_PKG_ARCH}.deb + test ! -f "$TERMUX_COMMON_CACHEDIR/debian-binary" && echo "2.0" > "$TERMUX_COMMON_CACHEDIR/debian-binary" + ar cr "$TERMUX_SUBPKG_DEBFILE" \ + "$TERMUX_COMMON_CACHEDIR/debian-binary" \ + "$SUB_PKG_PACKAGE_DIR/control.tar.xz" \ + "$SUB_PKG_PACKAGE_DIR/data.tar.xz" + + # Go back to main package: + cd "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX" done # .. remove empty directories (NOTE: keep this last): find . -type d -empty -delete - # Make sure user can read and write all files (problem with dpkg otherwise): - chmod -R u+rw . + # Make sure user can read and write all files (problem with dpkg otherwise): + chmod -R u+rw . } termux_step_post_massage () { - return -} - -termux_step_create_debscripts () { - return + return } -termux_setup_golang () { - export GOOS=android - export CGO_ENABLED=1 - export GO_LDFLAGS="-extldflags=-pie" - if [ "$TERMUX_ARCH" = "arm" ]; then - export GOARCH=arm - export GOARM=7 - elif [ "$TERMUX_ARCH" = "i686" ]; then - export GOARCH=386 - export GO386=sse2 - elif [ "$TERMUX_ARCH" = "aarch64" ]; then - export GOARCH=arm64 - elif [ "$TERMUX_ARCH" = "x86_64" ]; then - export GOARCH=amd64 - else - echo "ERROR: Unsupported arch: $TERMUX_ARCH" - exit 1 +# Create data.tar.gz with files to package. Not to be overridden by package scripts. +termux_step_create_datatar() { + # Create data tarball containing files to package: + cd "$TERMUX_PKG_MASSAGEDIR" + if [ -z "${TERMUX_PKG_METAPACKAGE+x}" ] && [ "$(find . -type f)" = "" ]; then + termux_error_exit "No files in package" fi + $TERMUX_TAR -cJf "$TERMUX_PKG_PACKAGEDIR/data.tar.xz" . +} - local TERMUX_GO_VERSION=go1.7.4 - local TERMUX_GO_PLATFORM=linux-amd64 - test `uname` = "Darwin" && TERMUX_GO_PLATFORM=darwin-amd64 - - export TERMUX_BUILDGO_FOLDER=$TERMUX_COMMON_CACHEDIR/${TERMUX_GO_VERSION}.${TERMUX_GO_PLATFORM} - export GOROOT=$TERMUX_BUILDGO_FOLDER - export PATH=$GOROOT/bin:$PATH - - if [ -d $TERMUX_BUILDGO_FOLDER ]; then return; fi - - local TERMUX_BUILDGO_TAR=$TERMUX_COMMON_CACHEDIR/${TERMUX_GO_VERSION}.${TERMUX_GO_PLATFORM}.tar.gz - rm -Rf $TERMUX_COMMON_CACHEDIR/go $TERMUX_BUILDGO_FOLDER - termux_download https://storage.googleapis.com/golang/${TERMUX_GO_VERSION}.${TERMUX_GO_PLATFORM}.tar.gz $TERMUX_BUILDGO_TAR - ( cd $TERMUX_COMMON_CACHEDIR; tar xf $TERMUX_BUILDGO_TAR; mv go $TERMUX_BUILDGO_FOLDER; rm $TERMUX_BUILDGO_TAR ) +termux_step_create_debscripts () { + return } -source $TERMUX_PKG_BUILDER_SCRIPT +# Create the build deb file. Not to be overridden by package scripts. +termux_step_create_debfile() { + # Get install size. This will be written as the "Installed-Size" deb field so is measured in 1024-byte blocks: + local TERMUX_PKG_INSTALLSIZE + TERMUX_PKG_INSTALLSIZE=$(du -sk . | cut -f 1) + + # From here on TERMUX_ARCH is set to "all" if TERMUX_PKG_PLATFORM_INDEPENDENT is set by the package + test -n "$TERMUX_PKG_PLATFORM_INDEPENDENT" && TERMUX_ARCH=all + + mkdir -p DEBIAN + cat > DEBIAN/control <<-HERE + Package: $TERMUX_PKG_NAME + Architecture: ${TERMUX_ARCH} + Installed-Size: ${TERMUX_PKG_INSTALLSIZE} + Maintainer: $TERMUX_PKG_MAINTAINER + Version: $TERMUX_PKG_FULLVERSION + Description: $TERMUX_PKG_DESCRIPTION + Homepage: $TERMUX_PKG_HOMEPAGE + HERE + test ! -z "$TERMUX_PKG_DEPENDS" && echo "Depends: $TERMUX_PKG_DEPENDS" >> DEBIAN/control + test ! -z "$TERMUX_PKG_ESSENTIAL" && echo "Essential: yes" >> DEBIAN/control + test ! -z "$TERMUX_PKG_CONFLICTS" && echo "Conflicts: $TERMUX_PKG_CONFLICTS" >> DEBIAN/control + test ! -z "$TERMUX_PKG_REPLACES" && echo "Replaces: $TERMUX_PKG_REPLACES" >> DEBIAN/control + + # Create DEBIAN/conffiles (see https://www.debian.org/doc/debian-policy/ap-pkg-conffiles.html): + for f in $TERMUX_PKG_CONFFILES; do echo "$TERMUX_PREFIX/$f" >> DEBIAN/conffiles; done + + # Allow packages to create arbitrary control files. + # XXX: Should be done in a better way without a function? + cd DEBIAN + termux_step_create_debscripts + + # Create control.tar.xz + $TERMUX_TAR -cJf "$TERMUX_PKG_PACKAGEDIR/control.tar.xz" . + + test ! -f "$TERMUX_COMMON_CACHEDIR/debian-binary" && echo "2.0" > "$TERMUX_COMMON_CACHEDIR/debian-binary" + TERMUX_PKG_DEBFILE=$TERMUX_DEBDIR/${TERMUX_PKG_NAME}_${TERMUX_PKG_FULLVERSION}_${TERMUX_ARCH}.deb + # Create the actual .deb file: + ar cr "$TERMUX_PKG_DEBFILE" \ + "$TERMUX_COMMON_CACHEDIR/debian-binary" \ + "$TERMUX_PKG_PACKAGEDIR/control.tar.xz" \ + "$TERMUX_PKG_PACKAGEDIR/data.tar.xz" +} -if [ -n "${TERMUX_PKG_BLACKLISTED_ARCHES:=""}" -a "$TERMUX_PKG_BLACKLISTED_ARCHES" != "${TERMUX_PKG_BLACKLISTED_ARCHES/$TERMUX_ARCH/}" ]; then - echo "Skipping building $TERMUX_PKG_NAME for arch $TERMUX_ARCH" +# Finish the build. Not to be overridden by package scripts. +termux_step_finish_build() { + echo "termux - build of '$TERMUX_PKG_NAME' done" + test -t 1 && printf "\033]0;%s - DONE\007" "$TERMUX_PKG_NAME" + mkdir -p /data/data/.built-packages + echo "$TERMUX_PKG_FULLVERSION" > "/data/data/.built-packages/$TERMUX_PKG_NAME" exit 0 -fi - -if [ -z "${TERMUX_SKIP_DEPCHECK:=""}" ]; then - for p in `./scripts/buildorder.py $TERMUX_PKG_NAME`; do - if [ "$p" != "$TERMUX_PKG_NAME" ]; then - echo "Building dependency $p if necessary..." - ./build-package.sh -s $p - fi - done -fi - -# Compute full version: -TERMUX_PKG_FULLVERSION=$TERMUX_PKG_VERSION -if [ "$TERMUX_PKG_BUILD_REVISION" != "0" -o "$TERMUX_PKG_FULLVERSION" != "${TERMUX_PKG_FULLVERSION/-/}" ]; then - # "0" is the default revision, so only include it if the upstream versions contains "-" itself - TERMUX_PKG_FULLVERSION+="-$TERMUX_PKG_BUILD_REVISION" -fi - -if [ -z "$TERMUX_DEBUG" -a -e "/data/data/.built-packages/$TERMUX_PKG_NAME" ]; then - if [ "`cat /data/data/.built-packages/$TERMUX_PKG_NAME`" = "$TERMUX_PKG_FULLVERSION" ]; then - echo "$TERMUX_PKG_NAME@$TERMUX_PKG_FULLVERSION built - skipping (rm /data/data/.built-packages/$TERMUX_PKG_NAME to force rebuild)" - exit 0 - fi -fi - -echo "termux - building $1 for arch $TERMUX_ARCH..." -test -t 1 && printf "\033]0;%s...\007" "$1" - -# Compute standalone toolchain dir, bitness of arch and name of host platform: -TERMUX_STANDALONE_TOOLCHAIN="$TERMUX_TOPDIR/_lib/toolchain-${TERMUX_ARCH}-ndk${TERMUX_NDK_VERSION}-api${TERMUX_API_LEVEL}" -# Bump the below version if a change is made in toolchain setup to ensure -# that everyone gets an updated toolchain: -TERMUX_STANDALONE_TOOLCHAIN+="-v3" - -# We put this after system PATH to avoid picking up toolchain stripped python -export PATH=$PATH:$TERMUX_STANDALONE_TOOLCHAIN/bin - -export AR=$TERMUX_HOST_PLATFORM-ar -if [ "$TERMUX_PKG_CLANG" = "no" ]; then - export AS=${TERMUX_HOST_PLATFORM}-gcc - export CC=$TERMUX_HOST_PLATFORM-gcc - export CXX=$TERMUX_HOST_PLATFORM-g++ - _SPECSFLAG=" -specs=$TERMUX_SCRIPTDIR/termux.spec" -else - export AS=${TERMUX_HOST_PLATFORM}-gcc - export CC=$TERMUX_HOST_PLATFORM-clang - export CXX=$TERMUX_HOST_PLATFORM-clang++ - # TODO: clang does not have specs file, how to ensure pie - # binaries gets built? - _SPECSFLAG="" -fi -export CPP=${TERMUX_HOST_PLATFORM}-cpp -export CC_FOR_BUILD=gcc -export LD=$TERMUX_HOST_PLATFORM-ld -export OBJDUMP=$TERMUX_HOST_PLATFORM-objdump -# Setup pkg-config for cross-compiling: -export PKG_CONFIG=$TERMUX_STANDALONE_TOOLCHAIN/bin/${TERMUX_HOST_PLATFORM}-pkg-config -export RANLIB=$TERMUX_HOST_PLATFORM-ranlib -export READELF=$TERMUX_HOST_PLATFORM-readelf -export STRIP=$TERMUX_HOST_PLATFORM-strip - -export CFLAGS="$_SPECSFLAG" -export LDFLAGS="$_SPECSFLAG -L${TERMUX_PREFIX}/lib" -# Android 7 started to support DT_RUNPATH (but not DT_RPATH), so we may want -# LDFLAGS+="-Wl,-rpath=$TERMUX_PREFIX/lib -Wl,--enable-new-dtags" -# and no longer remove DT_RUNPATH in termux-elf-cleaner. - -if [ "$TERMUX_ARCH" = "arm" ]; then - CFLAGS+=" -march=armv7-a -mfpu=neon -mfloat-abi=softfp" - # "first flag instructs the linker to pick libgcc.a, libgcov.a, and - # crt*.o, which are tailored for armv7-a" - # - https://developer.android.com/ndk/guides/standalone_toolchain.html - LDFLAGS+=" -march=armv7-a -Wl,--fix-cortex-a8" -elif [ $TERMUX_ARCH = "i686" ]; then - # From $NDK/docs/CPU-ARCH-ABIS.html: - CFLAGS+=" -march=i686 -msse3 -mstackrealign -mfpmath=sse" -elif [ $TERMUX_ARCH = "aarch64" ]; then - LDFLAGS+=" -Wl,-rpath-link,$TERMUX_PREFIX/lib" - LDFLAGS+=" -Wl,-rpath-link,$TERMUX_STANDALONE_TOOLCHAIN/sysroot/usr/lib" -elif [ $TERMUX_ARCH = "x86_64" ]; then - : -else - echo "Error: Invalid arch '$TERMUX_ARCH' - support arches are 'arm', 'i686', 'aarch64', 'x86_64'" - exit 1 -fi - -if [ -n "$TERMUX_DEBUG" ]; then - CFLAGS+=" -g3 -O1 -fstack-protector --param ssp-buffer-size=4 -D_FORTIFY_SOURCE=2" -else - CFLAGS+=" -Os" -fi - -export CXXFLAGS="$CFLAGS" -export CPPFLAGS="-I${TERMUX_PREFIX}/include" - -export ac_cv_func_getpwent=no -export ac_cv_func_getpwnam=no -export ac_cv_func_getpwuid=no - -if [ ! -d $TERMUX_STANDALONE_TOOLCHAIN ]; then - # Do not put toolchain in place until we are done with setup, to avoid having a half setup - # toolchain left in place if something goes wrong (or process is just aborted): - _TERMUX_TOOLCHAIN_TMPDIR=${TERMUX_STANDALONE_TOOLCHAIN}-tmp - rm -Rf $_TERMUX_TOOLCHAIN_TMPDIR - - _NDK_ARCHNAME=$TERMUX_ARCH - if [ $TERMUX_ARCH = "aarch64" ]; then - _NDK_ARCHNAME=arm64 - elif [ $TERMUX_ARCH = "i686" ]; then - _NDK_ARCHNAME=x86 - fi - $NDK/build/tools/make_standalone_toolchain.py \ - --api $TERMUX_API_LEVEL \ - --arch $_NDK_ARCHNAME \ - --install-dir $_TERMUX_TOOLCHAIN_TMPDIR - - if [ "arm" = $TERMUX_ARCH ]; then - # Fix to allow e.g. to be included: - cp $_TERMUX_TOOLCHAIN_TMPDIR/include/c++/4.9.x/arm-linux-androideabi/armv7-a/bits/* \ - $_TERMUX_TOOLCHAIN_TMPDIR/include/c++/4.9.x/bits - fi - - cd $_TERMUX_TOOLCHAIN_TMPDIR/sysroot - - for f in $TERMUX_SCRIPTDIR/ndk_patches/*.patch; do - sed "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" $f | \ - sed "s%\@TERMUX_HOME\@%${TERMUX_ANDROID_HOME}%g" | \ - patch --silent -p1; - done - # elf.h is taken from glibc since the elf.h in the NDK is lacking. - # sysexits.h is header-only and used by a few programs. - cp $TERMUX_SCRIPTDIR/ndk_patches/{elf.h,sysexits.h} $_TERMUX_TOOLCHAIN_TMPDIR/sysroot/usr/include - - $TERMUX_ELF_CLEANER usr/lib/*.so +} - # zlib is really version 1.2.8 in the Android platform (at least - # starting from Android 5), not older as the NDK headers claim. - for file in zconf.h zlib.h; do - curl -o $_TERMUX_TOOLCHAIN_TMPDIR/sysroot/usr/include/$file \ - https://raw.githubusercontent.com/madler/zlib/v1.2.8/$file - done - unset file - - mv $_TERMUX_TOOLCHAIN_TMPDIR $TERMUX_STANDALONE_TOOLCHAIN -fi - -if [ ! -f $TERMUX_PREFIX/lib/libstdc++.so ]; then - # Setup libgnustl_shared.so in $PREFIX/lib and libstdc++.so as a symlink to it, - # so that other C++ using packages links to it instead of the default android - # C++ library which does not support exceptions or STL: - # https://developer.android.com/ndk/guides/cpp-support.html - # We do however want to avoid installing this, to avoid problems # where e.g. - # libm.so on some i686 devices links against libstdc++.so. - # The libgnustl_shared.so library will be packaged in the libgnustl package - # which is part of the base Termux installation. - mkdir -p $TERMUX_PREFIX/lib - cd $TERMUX_PREFIX/lib - _STL_LIBFILE=$TERMUX_STANDALONE_TOOLCHAIN/${TERMUX_HOST_PLATFORM}/lib/libgnustl_shared.so - if [ $TERMUX_ARCH = arm ]; then - _STL_LIBFILE=$TERMUX_STANDALONE_TOOLCHAIN/${TERMUX_HOST_PLATFORM}/lib/armv7-a/libgnustl_shared.so - elif [ $TERMUX_ARCH = x86_64 ]; then - _STL_LIBFILE=$TERMUX_STANDALONE_TOOLCHAIN/${TERMUX_HOST_PLATFORM}/lib64/libgnustl_shared.so - fi - cp $_STL_LIBFILE . - ln -f -s libgnustl_shared.so libstdc++.so -fi - -# Create pkg-config wrapper. We use path to host pkg-config to -# avoid picking up a cross-compiled pkg-config later on. -_HOST_PKGCONFIG=`which pkg-config` -mkdir -p $TERMUX_STANDALONE_TOOLCHAIN/bin $PKG_CONFIG_LIBDIR -cat > $PKG_CONFIG < $PKG_CONFIG_LIBDIR/zlib.pc <> $TERMUX_PKG_BUILDDIR/BUILDING_IN_SRC.txt - TERMUX_PKG_BUILDDIR=$TERMUX_PKG_SRCDIR -fi - -cd $TERMUX_PKG_BUILDDIR +termux_step_handle_hostbuild +termux_step_setup_toolchain termux_step_patch_package -cd $TERMUX_PKG_BUILDDIR +cd "$TERMUX_PKG_BUILDDIR" termux_step_pre_configure -cd $TERMUX_PKG_BUILDDIR +cd "$TERMUX_PKG_BUILDDIR" termux_step_configure -cd $TERMUX_PKG_BUILDDIR +cd "$TERMUX_PKG_BUILDDIR" termux_step_post_configure -cd $TERMUX_PKG_BUILDDIR -termux_step_pre_make -cd $TERMUX_PKG_BUILDDIR +cd "$TERMUX_PKG_BUILDDIR" termux_step_make -cd $TERMUX_PKG_BUILDDIR +cd "$TERMUX_PKG_BUILDDIR" termux_step_make_install -cd $TERMUX_PKG_BUILDDIR +cd "$TERMUX_PKG_BUILDDIR" termux_step_post_make_install -cd $TERMUX_PKG_MASSAGEDIR +cd "$TERMUX_PKG_MASSAGEDIR" termux_step_extract_into_massagedir -cd $TERMUX_PKG_MASSAGEDIR +cd "$TERMUX_PKG_MASSAGEDIR" termux_step_massage -cd $TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX +cd "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX" termux_step_post_massage - -# Create data tarball containing files to package: -cd $TERMUX_PKG_MASSAGEDIR -if [ -z "${TERMUX_PKG_METAPACKAGE+x}" -a "`find . -type f`" = "" ]; then - echo "ERROR: No files in package" - exit 1 -fi -$TERMUX_TAR -cJf $TERMUX_PKG_PACKAGEDIR/data.tar.xz . - -# Get install size. This will be written as the "Installed-Size" deb field so is measured in 1024-byte blocks: -TERMUX_PKG_INSTALLSIZE=`du -sk . | cut -f 1` - -# Create deb package: -# NOTE: From here on TERMUX_ARCH is set to "all" if TERMUX_PKG_PLATFORM_INDEPENDENT is set by the package -test -n "$TERMUX_PKG_PLATFORM_INDEPENDENT" && TERMUX_ARCH=all - -cd $TERMUX_PKG_MASSAGEDIR - -mkdir -p DEBIAN -cat > DEBIAN/control <> DEBIAN/control -test ! -z "$TERMUX_PKG_ESSENTIAL" && echo "Essential: yes" >> DEBIAN/control -test ! -z "$TERMUX_PKG_CONFLICTS" && echo "Conflicts: $TERMUX_PKG_CONFLICTS" >> DEBIAN/control -test ! -z "$TERMUX_PKG_REPLACES" && echo "Replaces: $TERMUX_PKG_REPLACES" >> DEBIAN/control - -# Create DEBIAN/conffiles (see https://www.debian.org/doc/debian-policy/ap-pkg-conffiles.html): -for f in $TERMUX_PKG_CONFFILES; do echo $TERMUX_PREFIX/$f >> DEBIAN/conffiles; done -# Allow packages to create arbitrary control files: -cd DEBIAN -termux_step_create_debscripts - -# Create control.tar.xz -$TERMUX_TAR -cJf $TERMUX_PKG_PACKAGEDIR/control.tar.xz . -# In the .deb ar file there should be a file "debian-binary" with "2.0" as the content: -TERMUX_PKG_DEBFILE=$TERMUX_DEBDIR/${TERMUX_PKG_NAME}_${TERMUX_PKG_FULLVERSION}_${TERMUX_ARCH}.deb -# Create the actual .deb file: -ar cr "$TERMUX_PKG_DEBFILE" \ - "$TERMUX_COMMON_CACHEDIR/debian-binary" \ - "$TERMUX_PKG_PACKAGEDIR/control.tar.xz" \ - "$TERMUX_PKG_PACKAGEDIR/data.tar.xz" - -if [ "$TERMUX_PROCESS_DEB" != "" ]; then - $TERMUX_PROCESS_DEB "$TERMUX_PKG_DEBFILE" -fi - -echo "termux - build of '$1' done" -test -t 1 && printf "\033]0;%s - DONE\007" "$1" - -mkdir -p /data/data/.built-packages -echo "$TERMUX_PKG_FULLVERSION" > "/data/data/.built-packages/$TERMUX_PKG_NAME" -exit 0 +termux_step_create_datatar +termux_step_create_debfile +termux_step_finish_build diff --git a/disabled-packages/alpine/build.sh b/disabled-packages/alpine/build.sh index 2970e2255..c1c3c4582 100644 --- a/disabled-packages/alpine/build.sh +++ b/disabled-packages/alpine/build.sh @@ -4,20 +4,20 @@ TERMUX_PKG_VERSION=2.20 TERMUX_PKG_SRCURL=http://patches.freeiz.com/alpine/release/src/alpine-${TERMUX_PKG_VERSION}.tar.xz #TERMUX_PKG_DEPENDS="pcre, openssl, libuuid" TERMUX_PKG_EXTRA_CONFIGURE_ARGS=" --with-c-client-target=lnx --without-pthread --without-password-prog --without-tcl --without-ldap --without-krb5 --disable-debug --with-system-pinerc=/data/data/com.termux/files/usr/etc/pine.conf --with-password-prog=no" -export TERMUX_HOST_PLATFORM="${TERMUX_ARCH}-linux-android" -if [ $TERMUX_ARCH = "arm" ]; then export TERMUX_HOST_PLATFORM="${TERMUX_HOST_PLATFORM}eabi"; fi TERMUX_PKG_BUILD_IN_SRC=yes + termux_step_pre_configure () { -LDFLAGS+=" -lcrypt -llog" + LDFLAGS+=" -lcrypt -llog" cp $TERMUX_PKG_BUILDER_DIR/getpass.c $TERMUX_PKG_SRCDIR/include/ cp $TERMUX_PKG_BUILDER_DIR/getpass.h $TERMUX_PKG_SRCDIR/include/ cd $TERMUX_PKG_SRCDIR autoreconf -if -touch $TERMUX_PKG_SRCDIR/imap/lnxok + touch $TERMUX_PKG_SRCDIR/imap/lnxok + export TPATH=$PATH } -export TPATH=$PATH -termux_step_pre_make () { + +termux_step_post_configure() { cd pith - $CC_FOR_BUILD help_c_gen.c -o help_c_gen + $CC_FOR_BUILD help_c_gen.c -o help_c_gen $CC_FOR_BUILD help_h_gen.c -o help_h_gen } diff --git a/packages/colordiff/build.sh b/packages/colordiff/build.sh index ba5c7c2d7..dd0ec7781 100644 --- a/packages/colordiff/build.sh +++ b/packages/colordiff/build.sh @@ -7,7 +7,7 @@ TERMUX_PKG_DEPENDS="perl" TERMUX_PKG_BUILD_IN_SRC=yes TERMUX_PKG_PLATFORM_INDEPENDENT=yes -termux_step_pre_make(){ +termux_step_post_configure(){ export INSTALL_DIR=${PREFIX}/bin export MAN_DIR=${PREFIX}/share/man/man1 export ETC_DIR=${PREFIX}/etc diff --git a/packages/libluajit/build.sh b/packages/libluajit/build.sh index 598814a88..ab1df706a 100644 --- a/packages/libluajit/build.sh +++ b/packages/libluajit/build.sh @@ -8,7 +8,7 @@ TERMUX_PKG_BUILD_IN_SRC=yes TERMUX_PKG_CONFLICTS="lua,lua-dev" TERMUX_PKG_REPLACES="lua,lua-dev" -termux_step_post_extract_package() { +termux_step_pre_configure() { # luajit wants same pointer size for host and target build export HOST_CC="gcc" if [ $TERMUX_ARCH_BITS = "32" ]; then diff --git a/packages/openssh/build.sh b/packages/openssh/build.sh index ac17e5926..b056ec967 100755 --- a/packages/openssh/build.sh +++ b/packages/openssh/build.sh @@ -16,7 +16,7 @@ termux_step_pre_configure() { LDFLAGS+=" -llog" # liblog for android logging in syslog hack } -termux_step_pre_make () { +termux_step_post_configure() { # We need to remove this file before installing, since otherwise the # install leaves it alone which means no updated timestamps. rm -Rf $TERMUX_PREFIX/etc/moduli diff --git a/packages/tinyscheme/build.sh b/packages/tinyscheme/build.sh index ff0f80cfd..21dad6e68 100644 --- a/packages/tinyscheme/build.sh +++ b/packages/tinyscheme/build.sh @@ -12,7 +12,7 @@ termux_step_pre_configure () { # TODO: Add the tsx extension with file/networking (http://heras-gilsanz.com/manuel/tsx.html) # and the regexp extension (http://downloads.sourceforge.net/project/tinyscheme/tinyscheme-regex/1.3/re-1.3.tar.gz) -#termux_step_pre_make () { +#termux_step_post_configure () { #TSX_TARFILE=$TERMUX_PKG_CACHEDIR/tsx-1.1.tar.gz #test ! -f $TSX_TARFILE && curl -o $TSX_TARFILE "http://heras-gilsanz.com/manuel/tsx-1.1.tgz" #} diff --git a/packages/tty-clock/build.sh b/packages/tty-clock/build.sh index 32ffd7743..4132cfe37 100644 --- a/packages/tty-clock/build.sh +++ b/packages/tty-clock/build.sh @@ -9,7 +9,7 @@ TERMUX_PKG_FOLDERNAME=tty-clock-${_COMMIT} TERMUX_PKG_DEPENDS="ncurses" TERMUX_PKG_BUILD_IN_SRC=yes -termux_step_pre_make () { +termux_step_post_configure() { LDFLAGS+=" -lncurses" CFLAGS+=" $CPPFLAGS" } diff --git a/packages/zile/build.sh b/packages/zile/build.sh index 6d29f94e9..cdb2a1e3a 100755 --- a/packages/zile/build.sh +++ b/packages/zile/build.sh @@ -6,7 +6,7 @@ TERMUX_PKG_SRCURL=https://mirrors.kernel.org/gnu/zile/zile-${TERMUX_PKG_VERSION} TERMUX_PKG_DEPENDS="libgc, ncurses" TERMUX_PKG_BUILD_IN_SRC=yes -termux_step_pre_make () { +termux_step_post_configure() { # zile uses help2man to build the zile.1 man page, which would require # a host build. To avoid that just copy a pre-built man page. cp $TERMUX_PKG_BUILDER_DIR/zile.1 $TERMUX_PKG_BUILDDIR/doc/zile.1