2019-02-27 19:37:27 +01:00
|
|
|
termux_step_setup_toolchain() {
|
|
|
|
export CFLAGS=""
|
|
|
|
export LDFLAGS="-L${TERMUX_PREFIX}/lib"
|
|
|
|
|
2019-08-06 14:39:42 +02:00
|
|
|
export AS=$TERMUX_HOST_PLATFORM-clang
|
2019-02-27 19:37:27 +01:00
|
|
|
export CC=$TERMUX_HOST_PLATFORM-clang
|
|
|
|
export CXX=$TERMUX_HOST_PLATFORM-clang++
|
|
|
|
export AR=$TERMUX_HOST_PLATFORM-ar
|
2019-08-06 14:39:42 +02:00
|
|
|
export CPP=$TERMUX_HOST_PLATFORM-cpp
|
2019-02-27 19:37:27 +01:00
|
|
|
export LD=$TERMUX_HOST_PLATFORM-ld
|
2019-06-26 07:57:08 +02:00
|
|
|
export OBJCOPY=$TERMUX_HOST_PLATFORM-objcopy
|
2019-02-27 19:37:27 +01:00
|
|
|
export OBJDUMP=$TERMUX_HOST_PLATFORM-objdump
|
|
|
|
export RANLIB=$TERMUX_HOST_PLATFORM-ranlib
|
|
|
|
export READELF=$TERMUX_HOST_PLATFORM-readelf
|
|
|
|
export STRIP=$TERMUX_HOST_PLATFORM-strip
|
|
|
|
|
2019-08-06 14:39:42 +02:00
|
|
|
if [ -z "$TERMUX_ON_DEVICE_BUILD" ]; then
|
|
|
|
export PATH=$TERMUX_STANDALONE_TOOLCHAIN/bin:$PATH
|
|
|
|
export CC_FOR_BUILD=gcc
|
|
|
|
export PKG_CONFIG=$TERMUX_STANDALONE_TOOLCHAIN/bin/${TERMUX_HOST_PLATFORM}-pkg-config
|
|
|
|
export CCTERMUX_HOST_PLATFORM=$TERMUX_HOST_PLATFORM$TERMUX_PKG_API_LEVEL
|
|
|
|
if [ $TERMUX_ARCH = arm ]; then
|
|
|
|
CCTERMUX_HOST_PLATFORM=armv7a-linux-androideabi$TERMUX_PKG_API_LEVEL
|
|
|
|
fi
|
|
|
|
else
|
2019-08-10 16:28:08 +02:00
|
|
|
export CC_FOR_BUILD=$CC
|
2019-08-06 14:39:42 +02:00
|
|
|
# Some build scripts use environment variable 'PKG_CONFIG', so
|
|
|
|
# using this for on-device builds too.
|
|
|
|
export PKG_CONFIG=pkg-config
|
|
|
|
fi
|
|
|
|
|
2019-02-27 19:37:27 +01:00
|
|
|
if [ "$TERMUX_ARCH" = "arm" ]; then
|
|
|
|
# https://developer.android.com/ndk/guides/standalone_toolchain.html#abi_compatibility:
|
|
|
|
# "We recommend using the -mthumb compiler flag to force the generation of 16-bit Thumb-2 instructions".
|
|
|
|
# With r13 of the ndk ruby 2.4.0 segfaults when built on arm with clang without -mthumb.
|
|
|
|
CFLAGS+=" -march=armv7-a -mfpu=neon -mfloat-abi=softfp -mthumb"
|
|
|
|
LDFLAGS+=" -march=armv7-a"
|
|
|
|
elif [ "$TERMUX_ARCH" = "i686" ]; then
|
|
|
|
# From $NDK/docs/CPU-ARCH-ABIS.html:
|
|
|
|
CFLAGS+=" -march=i686 -msse3 -mstackrealign -mfpmath=sse"
|
|
|
|
elif [ "$TERMUX_ARCH" = "aarch64" ]; then
|
|
|
|
:
|
|
|
|
elif [ "$TERMUX_ARCH" = "x86_64" ]; then
|
|
|
|
:
|
|
|
|
else
|
|
|
|
termux_error_exit "Invalid arch '$TERMUX_ARCH' - support arches are 'arm', 'i686', 'aarch64', 'x86_64'"
|
|
|
|
fi
|
|
|
|
|
2019-07-29 15:43:12 +02:00
|
|
|
# Android 7 started to support DT_RUNPATH (but not DT_RPATH).
|
|
|
|
LDFLAGS+=" -Wl,-rpath=$TERMUX_PREFIX/lib,--enable-new-dtags"
|
|
|
|
|
2019-07-29 15:45:26 +02:00
|
|
|
# Avoid linking extra (unneeded) libraries.
|
|
|
|
LDFLAGS+=" -Wl,--as-needed"
|
|
|
|
|
2019-07-29 15:43:12 +02:00
|
|
|
# Basic hardening.
|
|
|
|
CFLAGS+=" -fstack-protector-strong"
|
|
|
|
LDFLAGS+=" -Wl,-z,relro,-z,now"
|
|
|
|
|
2019-02-27 19:37:27 +01:00
|
|
|
if [ -n "$TERMUX_DEBUG" ]; then
|
2019-07-29 15:43:12 +02:00
|
|
|
CFLAGS+=" -g3 -O1 -D_FORTIFY_SOURCE=2"
|
2019-02-27 19:37:27 +01:00
|
|
|
else
|
2019-07-14 20:03:07 +02:00
|
|
|
CFLAGS+=" -Oz"
|
2019-02-27 19:37:27 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
export CXXFLAGS="$CFLAGS"
|
|
|
|
export CPPFLAGS="-I${TERMUX_PREFIX}/include"
|
|
|
|
|
|
|
|
# If libandroid-support is declared as a dependency, link to it explicitly:
|
|
|
|
if [ "$TERMUX_PKG_DEPENDS" != "${TERMUX_PKG_DEPENDS/libandroid-support/}" ]; then
|
|
|
|
LDFLAGS+=" -landroid-support"
|
|
|
|
fi
|
|
|
|
|
|
|
|
export ac_cv_func_getpwent=no
|
|
|
|
export ac_cv_func_getpwnam=no
|
|
|
|
export ac_cv_func_getpwuid=no
|
|
|
|
export ac_cv_func_sigsetmask=no
|
|
|
|
export ac_cv_c_bigendian=no
|
|
|
|
|
2019-08-06 14:39:42 +02:00
|
|
|
if [ -z "$TERMUX_ON_DEVICE_BUILD" ] && [ ! -d $TERMUX_STANDALONE_TOOLCHAIN ]; then
|
2019-02-27 19:37:27 +01:00
|
|
|
# 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):
|
|
|
|
local _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
|
2019-06-12 04:09:22 +02:00
|
|
|
cp $NDK/toolchains/llvm/prebuilt/linux-x86_64 $_TERMUX_TOOLCHAIN_TMPDIR -r
|
2019-02-27 19:37:27 +01:00
|
|
|
|
|
|
|
# Remove android-support header wrapping not needed on android-21:
|
|
|
|
rm -Rf $_TERMUX_TOOLCHAIN_TMPDIR/sysroot/usr/local
|
|
|
|
|
2019-06-12 04:09:22 +02:00
|
|
|
# Use gold by default to work around https://github.com/android-ndk/ndk/issues/148
|
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/aarch64-linux-android-ld.gold \
|
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/aarch64-linux-android-ld
|
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/aarch64-linux-android/bin/ld.gold \
|
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/aarch64-linux-android/bin/ld
|
2019-02-27 19:37:27 +01:00
|
|
|
|
|
|
|
# Linker wrapper script to add '--exclude-libs libgcc.a', see
|
|
|
|
# https://github.com/android-ndk/ndk/issues/379
|
|
|
|
# https://android-review.googlesource.com/#/c/389852/
|
|
|
|
local linker
|
|
|
|
for linker in ld ld.bfd ld.gold; do
|
2019-06-12 04:09:22 +02:00
|
|
|
local wrap_linker=$_TERMUX_TOOLCHAIN_TMPDIR/arm-linux-androideabi/bin/$linker
|
|
|
|
local real_linker=$_TERMUX_TOOLCHAIN_TMPDIR/arm-linux-androideabi/bin/$linker.real
|
2019-02-27 19:37:27 +01:00
|
|
|
cp $wrap_linker $real_linker
|
|
|
|
echo '#!/bin/bash' > $wrap_linker
|
|
|
|
echo -n '$(dirname $0)/' >> $wrap_linker
|
|
|
|
echo -n $linker.real >> $wrap_linker
|
2019-05-13 01:17:36 +02:00
|
|
|
echo ' --exclude-libs libunwind.a --exclude-libs libgcc_real.a "$@"' >> $wrap_linker
|
2019-02-27 19:37:27 +01:00
|
|
|
done
|
2019-06-12 04:09:22 +02:00
|
|
|
for HOST_PLAT in aarch64-linux-android armv7a-linux-androideabi i686-linux-android x86_64-linux-android; do
|
2019-02-27 19:37:27 +01:00
|
|
|
|
|
|
|
# Setup the cpp preprocessor:
|
2019-06-12 04:09:22 +02:00
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT$TERMUX_PKG_API_LEVEL-clang \
|
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT-clang
|
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT$TERMUX_PKG_API_LEVEL-clang++ \
|
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT-clang++
|
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT$TERMUX_PKG_API_LEVEL-clang \
|
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT-cpp
|
|
|
|
sed -i 's/clang/clang -E/' \
|
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT-cpp
|
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT-clang \
|
2019-07-20 10:42:33 +02:00
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT-gcc
|
2019-06-12 04:09:22 +02:00
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT-clang++ \
|
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT-gcc
|
|
|
|
done
|
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/armv7a-linux-androideabi$TERMUX_PKG_API_LEVEL-clang \
|
2019-07-20 10:42:33 +02:00
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/arm-linux-androideabi-clang
|
2019-06-12 04:09:22 +02:00
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/armv7a-linux-androideabi$TERMUX_PKG_API_LEVEL-clang++ \
|
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/arm-linux-androideabi-clang++
|
2019-06-13 00:15:18 +02:00
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/armv7a-linux-androideabi-cpp \
|
2019-06-12 04:59:57 +02:00
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/arm-linux-androideabi-cpp
|
|
|
|
|
2019-02-27 19:37:27 +01:00
|
|
|
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
|
|
|
|
# ifaddrs.h: Added in android-24 unified headers, use a inline implementation for now.
|
2019-04-22 22:08:12 +02:00
|
|
|
# libintl.h: Inline implementation gettext functions.
|
2019-02-27 19:37:27 +01:00
|
|
|
# langinfo.h: Inline implementation of nl_langinfo().
|
2019-04-22 22:08:12 +02:00
|
|
|
cp "$TERMUX_SCRIPTDIR"/ndk-patches/{ifaddrs.h,libintl.h,langinfo.h} usr/include
|
2019-02-27 19:37:27 +01:00
|
|
|
|
2019-07-20 10:42:33 +02:00
|
|
|
# Remove <sys/capability.h> because it is provided by libcap.
|
2019-02-27 19:37:27 +01:00
|
|
|
# Remove <sys/shm.h> from the NDK in favour of that from the libandroid-shmem.
|
|
|
|
# Remove <sys/sem.h> as it doesn't work for non-root.
|
|
|
|
# Remove <glob.h> as we currently provide it from libandroid-glob.
|
2019-04-22 22:08:12 +02:00
|
|
|
# Remove <iconv.h> as it's provided by libiconv.
|
2019-02-27 19:37:27 +01:00
|
|
|
# Remove <spawn.h> as it's only for future (later than android-27).
|
2019-04-14 14:20:28 +02:00
|
|
|
# Remove <zlib.h> and <zconf.h> as we build our own zlib
|
2019-04-22 22:08:12 +02:00
|
|
|
rm usr/include/sys/{capability.h,shm.h,sem.h} usr/include/{glob.h,iconv.h,spawn.h,zlib.h,zconf.h}
|
2019-03-25 16:31:37 +01:00
|
|
|
|
2019-02-27 19:37:27 +01:00
|
|
|
sed -i "s/define __ANDROID_API__ __ANDROID_API_FUTURE__/define __ANDROID_API__ $TERMUX_PKG_API_LEVEL/" \
|
|
|
|
usr/include/android/api-level.h
|
|
|
|
|
|
|
|
$TERMUX_ELF_CLEANER usr/lib/*/*/*.so
|
|
|
|
|
|
|
|
grep -lrw $_TERMUX_TOOLCHAIN_TMPDIR/sysroot/usr/include/c++/v1 -e '<version>' | xargs -n 1 sed -i 's/<version>/\"version\"/g'
|
|
|
|
mv $_TERMUX_TOOLCHAIN_TMPDIR $TERMUX_STANDALONE_TOOLCHAIN
|
|
|
|
fi
|
|
|
|
|
2019-03-04 16:19:32 +01:00
|
|
|
# On Android 7, libutil functionality is provided by libc.
|
|
|
|
# But many programs still may search for libutil.
|
|
|
|
if [ ! -f $TERMUX_PREFIX/lib/libutil.so ]; then
|
|
|
|
mkdir -p "$TERMUX_PREFIX/lib"
|
|
|
|
echo 'INPUT(-lc)' > $TERMUX_PREFIX/lib/libutil.so
|
|
|
|
fi
|
|
|
|
|
2019-02-27 19:37:27 +01:00
|
|
|
export PKG_CONFIG_LIBDIR="$TERMUX_PKG_CONFIG_LIBDIR"
|
2019-08-06 14:39:42 +02:00
|
|
|
|
|
|
|
if [ -z "$TERMUX_ON_DEVICE_BUILD" ]; then
|
|
|
|
# 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"
|
|
|
|
fi
|
2019-02-27 19:37:27 +01:00
|
|
|
}
|