--- /dev/null
+TERMUX_PKG_HOMEPAGE=http://www.swi-prolog.org/
+TERMUX_PKG_DESCRIPTION="Comprehensive free Prolog environment"
+TERMUX_PKG_VERSION=7.3.6
+TERMUX_PKG_SRCURL=http://www.swi-prolog.org/download/devel/src/swipl-${TERMUX_PKG_VERSION}.tar.gz
+TERMUX_PKG_BUILD_IN_SRC=true
+TERMUX_PKG_HOSTBUILD=true
+TERMUX_PKG_DEPENDS="readline, libgmp"
+
+termux_step_host_build () {
+ cp -Rf $TERMUX_PKG_SRCDIR/* .
+
+ # apt install libgmp-dev:i386 libncurses5-dev:i386
+ ./configure --host=i386-linux --disable-readline #--disable-gmp
+
+ if [ $TERMUX_ARCH_BITS = 32 ]; then
+ # "Make sure that the native swipl has the same word-length (32/64 bits)
+ # and use the native swipl for creating the boot file"
+ # https://groups.google.com/forum/#!topic/swi-prolog/8lBcjb9cxuk
+ find . -name Makefile | xargs perl -p -i -e 's/CFLAGS=/CFLAGS=-m32 /'
+ find . -name Makefile | xargs perl -p -i -e 's/LDFLAGS=/LDFLAGS=-m32 /'
+ fi
+ make
+}
+
+
+termux_step_post_configure () {
+ cp $TERMUX_PKG_HOSTBUILD_DIR/src/defatom src/
+ $TERMUX_TOUCH -d "next hour" $TERMUX_PKG_BUILDDIR/src/defatom
+ #cp $TERMUX_PKG_HOSTBUILD_DIR/{defatom,swipl} $TERMUX_PKG_BUILDDIR/src/
+
+ #bdir=/home/fornwall/termux/swi-prolog/src/src
+ #PLARCH=arm-linux
+ perl -p -i -e "s|bdir=|bdir=$TERMUX_PKG_HOSTBUILD_DIR/src/ # |" */swipl.sh
+ perl -p -i -e "s|PLARCH=|PLARCH=i386-linux # |" */swipl.sh
+ perl -p -i -e "s|${TERMUX_ARCH}-linux|i386-linux|" */swipl.sh
+}
+
+termux_step_post_make_install () {
+ mv $TERMUX_PREFIX/lib/swipl-$TERMUX_PKG_VERSION/lib/arm-linux/libswipl.so* $TERMUX_PREFIX/lib/
+}
--- /dev/null
+diff -u -r ../swipl-7.3.6/src/pl-dict.c ./src/pl-dict.c
+--- ../swipl-7.3.6/src/pl-dict.c 2015-08-25 05:55:13.000000000 -0400
++++ ./src/pl-dict.c 2015-09-16 19:10:55.212457701 -0400
+@@ -113,10 +113,148 @@
+
+ #if (defined _GNU_SOURCE || defined __GNU__ || defined __linux__)
+
++#ifdef __ANDROID__
++#include <stdlib.h>
++
++typedef int cmp_t(void *, const void *, const void *);
++static inline char *med3(char *, char *, char *, cmp_t *, void *);
++static inline void swapfunc(char *, char *, int, int);
++
++#define min(a, b) (a) < (b) ? a : b
++
++/*
++ * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
++ */
++#define swapcode(TYPE, parmi, parmj, n) { \
++ long i = (n) / sizeof (TYPE); \
++ TYPE *pi = (TYPE *) (parmi); \
++ TYPE *pj = (TYPE *) (parmj); \
++ do { \
++ TYPE t = *pi; \
++ *pi++ = *pj; \
++ *pj++ = t; \
++ } while (--i > 0); \
++}
++
++#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
++ es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
++
++ static inline void
++swapfunc(char *a, char *b, int n, int swaptype)
++{
++ if(swaptype <= 1)
++ swapcode(long, a, b, n)
++ else
++ swapcode(char, a, b, n)
++}
++
++#define swap(a, b) \
++ if (swaptype == 0) { \
++ long t = *(long *)(a); \
++ *(long *)(a) = *(long *)(b); \
++ *(long *)(b) = t; \
++ } else \
++swapfunc(a, b, es, swaptype)
++
++#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
++
++#define CMP(t, x, y) (cmp((t), (x), (y)))
++
++ static inline char *
++med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk)
++{
++ return CMP(thunk, a, b) < 0 ?
++ (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
++ :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
++}
++
++void qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
++{
++ char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
++ size_t d, r;
++ int cmp_result;
++ int swaptype, swap_cnt;
++
++loop: SWAPINIT(a, es);
++ swap_cnt = 0;
++ if (n < 7) {
++ for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
++ for (pl = pm;
++ pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
++ pl -= es)
++ swap(pl, pl - es);
++ return;
++ }
++ pm = (char *)a + (n / 2) * es;
++ if (n > 7) {
++ pl = a;
++ pn = (char *)a + (n - 1) * es;
++ if (n > 40) {
++ d = (n / 8) * es;
++ pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
++ pm = med3(pm - d, pm, pm + d, cmp, thunk);
++ pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
++ }
++ pm = med3(pl, pm, pn, cmp, thunk);
++ }
++ swap(a, pm);
++ pa = pb = (char *)a + es;
++
++ pc = pd = (char *)a + (n - 1) * es;
++ for (;;) {
++ while (pb <= pc && (cmp_result = CMP(thunk, pb, a)) <= 0) {
++ if (cmp_result == 0) {
++ swap_cnt = 1;
++ swap(pa, pb);
++ pa += es;
++ }
++ pb += es;
++ }
++ while (pb <= pc && (cmp_result = CMP(thunk, pc, a)) >= 0) {
++ if (cmp_result == 0) {
++ swap_cnt = 1;
++ swap(pc, pd);
++ pd -= es;
++ }
++ pc -= es;
++ }
++ if (pb > pc)
++ break;
++ swap(pb, pc);
++ swap_cnt = 1;
++ pb += es;
++ pc -= es;
++ }
++ if (swap_cnt == 0) { /* Switch to insertion sort */
++ for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
++ for (pl = pm;
++ pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
++ pl -= es)
++ swap(pl, pl - es);
++ return;
++ }
++
++ pn = (char *)a + n * es;
++ r = min(pa - (char *)a, pb - pa);
++ vecswap(a, pb - r, r);
++ r = min(pd - pc, pn - pd - es);
++ vecswap(pb, pn - r, r);
++ if ((r = pb - pa) > es)
++ qsort_r(a, r / es, es, thunk, cmp);
++ if ((r = pd - pc) > es) {
++ /* Iterate rather than recurse to save stack space */
++ a = pn - r;
++ n = r / es;
++ goto loop;
++ }
++}
++
++# else
+ typedef int(* __compar_d_fn_t)(const void *, const void *, void *);
+ extern void qsort_r (void *__base, size_t __nmemb, size_t __size,
+ __compar_d_fn_t __compar, void *__arg)
+ __nonnull ((1, 4));
++#endif
+
+ #endif
+
--- /dev/null
+diff -u -r ../swipl-7.3.6/src/configure ./src/configure
+--- ../swipl-7.3.6/src/configure 2015-08-25 05:55:30.000000000 -0400
++++ ./src/configure 2015-09-16 17:52:41.545729451 -0400
+@@ -9372,51 +9372,7 @@
+ done
+
+
+-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for pthread support for cpu clocks" >&5
+-$as_echo_n "checking for pthread support for cpu clocks... " >&6; }
+-if test "$cross_compiling" = yes; then :
+- { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
+-$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
+-as_fn_error $? "cannot run test program while cross compiling
+-See \`config.log' for more details" "$LINENO" 5; }
+-else
+- cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+-/* end confdefs.h. */
+-
+-#include <stdio.h>
+-#include <pthread.h>
+-#include <time.h>
+-#include <stdlib.h>
+-#define ts2d(ts) \
+- ((double)(ts).tv_sec + (double)(ts).tv_nsec/(double)1000000000.0)
+-main()
+-{
+- clockid_t clock_id;
+- struct timespec ts = {1,1};
+- if ( pthread_getcpuclockid(pthread_self(), &clock_id) != 0 )
+- { perror("pthread_getcpuclockid");
+- exit(1);
+- }
+- sleep(1);
+- if ( clock_gettime(clock_id, &ts) != 0 )
+- { perror("clock_gettime");
+- exit(1);
+- }
+- fprintf(stderr, "Used %f sec\n", ts2d(ts));
+- exit (ts.tv_sec == 0 ? 0 : 1);
+-}
+-_ACEOF
+-if ac_fn_c_try_run "$LINENO"; then :
+ ac_pthread_cpuclocks="yes"
+- { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+-$as_echo "yes" >&6; }
+-else
+- { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+-$as_echo "no" >&6; }
+-fi
+-rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \
+- conftest.$ac_objext conftest.beam conftest.$ac_ext
+-fi
+
+
+ if test "x$ac_pthread_cpuclocks" = "xyes"; then