mpc: Add source checksum
[termux-packages] / build-package.sh
index 9d0c8c8..c6d768c 100755 (executable)
@@ -12,6 +12,14 @@ termux_error_exit() {
 termux_download() {
        local URL="$1"
        local DESTINATION="$2"
+
+       if [ -f "$DESTINATION" ] && [ $# = 3 ] && [ -n "$3" ]; then
+               # Keep existing file if checksum matches.
+               local EXISTING_CHECKSUM
+               EXISTING_CHECKSUM=$(sha256sum "$DESTINATION" | cut -f 1 -d ' ')
+               if [ "$EXISTING_CHECKSUM" = "$3" ]; then return; fi
+       fi
+
        local TMPFILE
        TMPFILE=$(mktemp "$TERMUX_PKG_TMPDIR/download.$TERMUX_PKG_NAME.XXXXXXXXX")
        echo "Downloading ${URL}"
@@ -62,7 +70,7 @@ termux_setup_golang() {
                termux_error_exit "Unsupported arch: $TERMUX_ARCH"
        fi
 
-       local TERMUX_GO_VERSION=go1.8rc3
+       local TERMUX_GO_VERSION=go1.8
        local TERMUX_GO_PLATFORM=linux-amd64
 
        local TERMUX_BUILDGO_FOLDER=$TERMUX_COMMON_CACHEDIR/${TERMUX_GO_VERSION}
@@ -75,7 +83,7 @@ termux_setup_golang() {
        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" \
-                       0ff3faba02ac83920a65b453785771e75f128fbf9ba4ad1d5e72c044103f9c7a
+                       53ab94104ee3923e228a2cb2116e5e462ad3ebaeea06ff04463479d7f12d27ca
        ( cd "$TERMUX_COMMON_CACHEDIR"; tar xf "$TERMUX_BUILDGO_TAR"; mv go "$TERMUX_BUILDGO_FOLDER"; rm "$TERMUX_BUILDGO_TAR" )
 }
 
@@ -112,15 +120,17 @@ termux_step_handle_arguments() {
            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 "  -f Force build even if package has already been built."
            echo "  -s Skip dependency check."
            exit 1
        }
-       while getopts :a:hdDs option; do
+       while getopts :a:hdDfs option; do
                case "$option" in
                a) TERMUX_ARCH="$OPTARG";;
                h) _show_usage;;
                d) TERMUX_DEBUG=true;;
                D) local TERMUX_IS_DISABLED=true;;
+               f) TERMUX_FORCE_BUILD=true;;
                s) export TERMUX_SKIP_DEPCHECK=true;;
                ?) termux_error_exit "./build-package.sh: illegal option -$OPTARG";;
                esac
@@ -133,7 +143,7 @@ termux_step_handle_arguments() {
        # Handle 'all' arch:
        if [ -n "${TERMUX_ARCH+x}" ] && [ "${TERMUX_ARCH}" = 'all' ]; then
                for arch in 'aarch64' 'arm' 'i686' 'x86_64'; do
-                       ./build-package.sh -a $arch "$1"
+                       ./build-package.sh ${TERMUX_FORCE_BUILD+-f} -a $arch "$1"
                done
                exit
        fi
@@ -172,8 +182,8 @@ termux_step_setup_variables() {
        : "${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"}"
+       : "${TERMUX_ANDROID_BUILD_TOOLS_VERSION:="25.0.1"}"
+       : "${TERMUX_NDK_VERSION:="14"}"
 
        if [ "x86_64" = "$TERMUX_ARCH" ] || [ "aarch64" = "$TERMUX_ARCH" ]; then
                TERMUX_ARCH_BITS=64
@@ -208,7 +218,7 @@ termux_step_setup_variables() {
        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+="-v10"
+       TERMUX_STANDALONE_TOOLCHAIN+="-v17"
 
        export TERMUX_TAR="tar"
        export TERMUX_TOUCH="touch"
@@ -307,7 +317,9 @@ termux_step_start_build() {
                TERMUX_PKG_FULLVERSION+="-$TERMUX_PKG_REVISION"
        fi
 
-       if [ -z "$TERMUX_DEBUG" ] && [ -e "/data/data/.built-packages/$TERMUX_PKG_NAME" ]; then
+       if [ -z "$TERMUX_DEBUG" ] &&
+          [ -z "${TERMUX_FORCE_BUILD+x}" ] &&
+          [ -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
@@ -376,7 +388,7 @@ termux_step_extract_package() {
        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"
+       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`
@@ -547,11 +559,18 @@ termux_step_setup_toolchain() {
                                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
+               # elf.h: Taken from glibc since the elf.h in the NDK is lacking.
+               # sysexits.h: Header-only and used by a few programs.
+               # ifaddrs.h: Added in android-24 unified headers, use a inline implementation for now.
+               cp "$TERMUX_SCRIPTDIR"/ndk_patches/{elf.h,sysexits.h,ifaddrs.h} $_TERMUX_TOOLCHAIN_TMPDIR/sysroot/usr/include
 
-               $TERMUX_ELF_CLEANER usr/lib/*.so
+               # Remove <sys/shm.h> from the NDK in favour of that from the libandroid-shmem.
+               # Also remove <sys/sem.h> as it doesn't work for non-root.
+               rm $_TERMUX_TOOLCHAIN_TMPDIR/sysroot/usr/include/sys/{shm.h,sem.h}
+
+               local _LIBDIR=usr/lib
+               if [ $TERMUX_ARCH = x86_64 ]; then _LIBDIR+=64; fi
+               $TERMUX_ELF_CLEANER $_LIBDIR/*.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.
@@ -582,6 +601,8 @@ termux_step_setup_toolchain() {
                        _STL_LIBFILE=$TERMUX_STANDALONE_TOOLCHAIN/${TERMUX_HOST_PLATFORM}/lib64/libgnustl_shared.so
                fi
                cp "$_STL_LIBFILE" .
+               $STRIP --strip-unneeded libgnustl_shared.so
+               $TERMUX_ELF_CLEANER libgnustl_shared.so
                ln -f -s libgnustl_shared.so libstdc++.so
        fi
 
@@ -603,12 +624,18 @@ termux_step_setup_toolchain() {
 termux_step_patch_package() {
        cd "$TERMUX_PKG_SRCDIR"
        # Suffix patch with ".patch32" or ".patch64" to only apply for these bitnesses:
+       shopt -s nullglob
        for patch in $TERMUX_PKG_BUILDER_DIR/*.patch{$TERMUX_ARCH_BITS,}; do
                test -f "$patch" && sed "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" "$patch" | \
                        sed "s%\@TERMUX_HOME\@%${TERMUX_ANDROID_HOME}%g" | \
                        patch --silent -p1
        done
+       shopt -u nullglob
+}
 
+# Replace autotools build-aux/config.{sub,guess} with ours to add android targets.
+termux_step_replace_guess_scripts () {
+       cd "$TERMUX_PKG_SRCDIR"
        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" '{}' \;
 }
@@ -649,19 +676,46 @@ termux_step_configure_autotools () {
        done
        export PATH=$TERMUX_PKG_TMPDIR/config-scripts:$PATH
 
-       # See http://wiki.buici.com/xwiki/bin/view/Programing+C+and+C%2B%2B/Autoconf+and+RPL_MALLOC
-       # about this problem which may cause linker errors in test scripts not undef:ing malloc and
-       # also cause problems with e.g. malloc interceptors such as libgc:
-       local AVOID_AUTOCONF_WRAPPERS="ac_cv_func_malloc_0_nonnull=yes ac_cv_func_realloc_0_nonnull=yes"
-       # Similarly, disable gnulib's rpl_getcwd(). It returns the wrong value, affecting zile. See
-       # <https://github.com/termux/termux-packages/issues/76>.
-       AVOID_AUTOCONF_WRAPPERS+=" gl_cv_func_getcwd_null=yes gl_cv_func_getcwd_posix_signature=yes gl_cv_func_getcwd_path_max=yes gl_cv_func_getcwd_abort_bug=no"
-       AVOID_AUTOCONF_WRAPPERS+=" gl_cv_header_working_fcntl_h=yes gl_cv_func_fcntl_f_dupfd_cloexec=yes gl_cv_func_fcntl_f_dupfd_works=yes"
-       # 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"
-
-       # NOTE: We do not want to quote AVOID_AUTOCONF_WRAPPERS as we want word expansion.
-       env $AVOID_AUTOCONF_WRAPPERS "$TERMUX_PKG_SRCDIR/configure" \
+       # Avoid gnulib wrapping of functions when cross compiling. See
+       # http://wiki.osdev.org/Cross-Porting_Software#Gnulib
+       # https://gitlab.com/sortix/sortix/wikis/Gnulib
+       # https://github.com/termux/termux-packages/issues/76
+       local AVOID_GNULIB=""
+       AVOID_GNULIB+=" ac_cv_func_malloc_0_nonnull=yes"
+       AVOID_GNULIB+=" ac_cv_func_realloc_0_nonnull=yes"
+       AVOID_GNULIB+=" am_cv_func_working_getline=yes"
+       AVOID_GNULIB+=" gl_cv_func_dup2_works=yes"
+       AVOID_GNULIB+=" gl_cv_func_fcntl_f_dupfd_cloexec=yes"
+       AVOID_GNULIB+=" gl_cv_func_fcntl_f_dupfd_works=yes"
+       AVOID_GNULIB+=" gl_cv_func_getcwd_abort_bug=no"
+       AVOID_GNULIB+=" gl_cv_func_getcwd_null=yes"
+       AVOID_GNULIB+=" gl_cv_func_getcwd_path_max=yes"
+       AVOID_GNULIB+=" gl_cv_func_getcwd_posix_signature=yes"
+       AVOID_GNULIB+=" gl_cv_func_gettimeofday_clobber=no"
+       AVOID_GNULIB+=" gl_cv_func_gettimeofday_posix_signature=yes"
+       AVOID_GNULIB+=" gl_cv_func_link_works=yes"
+       AVOID_GNULIB+=" gl_cv_func_lstat_dereferences_slashed_symlink=yes"
+       AVOID_GNULIB+=" gl_cv_func_memchr_works=yes"
+       AVOID_GNULIB+=" gl_cv_func_mkdir_trailing_dot_works=yes"
+       AVOID_GNULIB+=" gl_cv_func_mkdir_trailing_slash_works=yes"
+       AVOID_GNULIB+=" gl_cv_func_select_detects_ebadf=yes"
+       AVOID_GNULIB+=" gl_cv_func_snprintf_retval_c99=yes"
+       AVOID_GNULIB+=" gl_cv_func_stat_dir_slash=yes"
+       AVOID_GNULIB+=" gl_cv_func_stat_file_slash=yes"
+       AVOID_GNULIB+=" gl_cv_func_strerror_0_works=yes"
+       AVOID_GNULIB+=" gl_cv_func_symlink_works=yes"
+       AVOID_GNULIB+=" gl_cv_func_tzset_clobber=no"
+       AVOID_GNULIB+=" gl_cv_func_unlink_honors_slashes=yes"
+       AVOID_GNULIB+=" gl_cv_func_unlink_honors_slashes=yes"
+       AVOID_GNULIB+=" gl_cv_func_wcwidth_works=yes"
+       AVOID_GNULIB+=" gl_cv_func_working_getdelim=yes"
+       AVOID_GNULIB+=" gl_cv_func_working_mkstemp=yes"
+       AVOID_GNULIB+=" gl_cv_func_working_mktime=yes"
+       AVOID_GNULIB+=" gl_cv_func_working_strerror=yes"
+       AVOID_GNULIB+=" gl_cv_header_working_fcntl_h=yes"
+
+       # NOTE: We do not want to quote AVOID_GNULIB as we want word expansion.
+       env $AVOID_GNULIB "$TERMUX_PKG_SRCDIR/configure" \
                --disable-dependency-tracking \
                --prefix=$TERMUX_PREFIX \
                --disable-rpath --disable-rpath-hack \
@@ -907,6 +961,12 @@ termux_step_post_massage() {
 termux_step_create_datatar() {
        # Create data tarball containing files to package:
        cd "$TERMUX_PKG_MASSAGEDIR"
+
+       local HARDLINKS="$(find . -type f -links +1)"
+       if [ -n "$HARDLINKS" ]; then
+               termux_error_exit "Package contains hard links: $HARDLINKS"
+       fi
+
        if [ -z "${TERMUX_PKG_METAPACKAGE+x}" ] && [ "$(find . -type f)" = "" ]; then
                termux_error_exit "No files in package"
        fi
@@ -980,6 +1040,7 @@ termux_step_post_extract_package
 termux_step_handle_hostbuild
 termux_step_setup_toolchain
 termux_step_patch_package
+termux_step_replace_guess_scripts
 cd "$TERMUX_PKG_BUILDDIR"
 termux_step_pre_configure
 cd "$TERMUX_PKG_BUILDDIR"