2019-02-27 19:37:27 +01:00
|
|
|
termux_step_setup_toolchain() {
|
2019-08-14 19:29:58 +02:00
|
|
|
[ "$TERMUX_PKG_METAPACKAGE" = "true" ] && return
|
|
|
|
|
2019-02-27 19:37:27 +01:00
|
|
|
export CFLAGS=""
|
2021-02-03 12:13:42 +01:00
|
|
|
export CPPFLAGS=""
|
2019-02-27 19:37:27 +01:00
|
|
|
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++
|
2019-08-06 14:39:42 +02:00
|
|
|
export CPP=$TERMUX_HOST_PLATFORM-cpp
|
2021-09-16 21:51:29 +02:00
|
|
|
export LD=ld.lld
|
2021-10-24 11:59:09 +02:00
|
|
|
export AR=llvm-ar
|
2021-09-16 21:51:29 +02:00
|
|
|
export OBJCOPY=llvm-objcopy
|
|
|
|
export OBJDUMP=llvm-objdump
|
|
|
|
export RANLIB=llvm-ranlib
|
|
|
|
export READELF=llvm-readelf
|
|
|
|
export STRIP=llvm-strip
|
2021-10-24 11:59:09 +02:00
|
|
|
export NM=llvm-nm
|
2019-02-27 19:37:27 +01:00
|
|
|
|
2019-08-12 23:16:07 +02:00
|
|
|
if [ "$TERMUX_ON_DEVICE_BUILD" = "false" ]; then
|
2019-08-06 14:39:42 +02:00
|
|
|
export PATH=$TERMUX_STANDALONE_TOOLCHAIN/bin:$PATH
|
|
|
|
export CC_FOR_BUILD=gcc
|
2021-09-17 13:40:15 +02:00
|
|
|
export PKG_CONFIG=$TERMUX_STANDALONE_TOOLCHAIN/bin/pkg-config
|
2021-10-24 08:02:56 +02:00
|
|
|
export PKGCONFIG=$PKG_CONFIG
|
2019-08-06 14:39:42 +02:00
|
|
|
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
|
2020-02-01 20:51:07 +01:00
|
|
|
LDFLAGS+=" -Wl,-rpath=$TERMUX_PREFIX/lib"
|
2019-08-06 14:39:42 +02:00
|
|
|
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
|
2021-09-17 13:40:15 +02:00
|
|
|
export PKG_CONFIG_LIBDIR="$TERMUX_PKG_CONFIG_LIBDIR"
|
2019-08-06 14:39:42 +02:00
|
|
|
|
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"
|
2020-01-03 22:39:54 +01:00
|
|
|
export GOARCH=arm
|
|
|
|
export GOARM=7
|
2019-02-27 19:37:27 +01:00
|
|
|
elif [ "$TERMUX_ARCH" = "i686" ]; then
|
|
|
|
# From $NDK/docs/CPU-ARCH-ABIS.html:
|
|
|
|
CFLAGS+=" -march=i686 -msse3 -mstackrealign -mfpmath=sse"
|
2021-08-26 09:59:19 +02:00
|
|
|
# i686 seem to explicitly require -fPIC, see
|
|
|
|
# https://github.com/termux/termux-packages/issues/7215#issuecomment-906154438
|
|
|
|
CPPFLAGS+=" -fPIC"
|
2020-01-03 22:39:54 +01:00
|
|
|
export GOARCH=386
|
|
|
|
export GO386=sse2
|
2019-02-27 19:37:27 +01:00
|
|
|
elif [ "$TERMUX_ARCH" = "aarch64" ]; then
|
2020-05-05 19:29:04 +02:00
|
|
|
export GOARCH=arm64
|
2019-02-27 19:37:27 +01:00
|
|
|
elif [ "$TERMUX_ARCH" = "x86_64" ]; then
|
2020-05-05 19:29:04 +02:00
|
|
|
export GOARCH=amd64
|
2019-02-27 19:37:27 +01:00
|
|
|
else
|
|
|
|
termux_error_exit "Invalid arch '$TERMUX_ARCH' - support arches are 'arm', 'i686', 'aarch64', 'x86_64'"
|
|
|
|
fi
|
|
|
|
|
2020-12-14 14:25:51 +01:00
|
|
|
# -static-openmp requires -fopenmp in LDFLAGS to work; hopefully this won't be problematic
|
|
|
|
# even when we don't have -fopenmp in CFLAGS / when we don't want to enable OpenMP
|
|
|
|
# We might also want to consider shipping libomp.so instead; since r21
|
|
|
|
LDFLAGS+=" -fopenmp -static-openmp"
|
|
|
|
|
2019-07-29 15:43:12 +02:00
|
|
|
# Android 7 started to support DT_RUNPATH (but not DT_RPATH).
|
2020-02-01 20:51:07 +01:00
|
|
|
LDFLAGS+=" -Wl,--enable-new-dtags"
|
2019-07-29 15:43:12 +02:00
|
|
|
|
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"
|
|
|
|
|
2021-08-21 16:50:05 +02:00
|
|
|
if [ "$TERMUX_DEBUG_BUILD" = "true" ]; then
|
2021-02-03 12:13:42 +01:00
|
|
|
CFLAGS+=" -g3 -O1"
|
|
|
|
CPPFLAGS+=" -D_FORTIFY_SOURCE=2 -D__USE_FORTIFY_LEVEL=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"
|
2021-02-03 12:13:42 +01:00
|
|
|
export CPPFLAGS+=" -I${TERMUX_PREFIX}/include"
|
2019-02-27 19:37:27 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2020-01-03 22:39:54 +01:00
|
|
|
export GOOS=android
|
|
|
|
export CGO_ENABLED=1
|
|
|
|
export GO_LDFLAGS="-extldflags=-pie"
|
2020-11-22 14:52:28 +01:00
|
|
|
export CGO_LDFLAGS="${LDFLAGS/-Wl,-z,relro,-z,now/}"
|
2020-12-28 23:42:49 +01:00
|
|
|
CGO_LDFLAGS="${LDFLAGS/-static-openmp/}"
|
2020-01-03 22:39:54 +01:00
|
|
|
export CGO_CFLAGS="-I$TERMUX_PREFIX/include"
|
2021-07-15 20:04:56 +02:00
|
|
|
export RUSTFLAGS="-C link-arg=-Wl,-rpath=$TERMUX_PREFIX/lib -C link-arg=-Wl,--enable-new-dtags"
|
2020-01-03 22:39:54 +01:00
|
|
|
|
2019-02-27 19:37:27 +01:00
|
|
|
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
|
|
|
|
|
2020-04-09 17:11:51 +02:00
|
|
|
termux_setup_standalone_toolchain
|
|
|
|
|
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
|
|
|
termux_setup_standalone_toolchain() {
|
2021-09-22 12:46:01 +02:00
|
|
|
if [ "$TERMUX_ON_DEVICE_BUILD" = "true" ] || [ -d $TERMUX_STANDALONE_TOOLCHAIN ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# 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
|
2019-02-27 19:37:27 +01:00
|
|
|
fi
|
2021-09-22 12:46:01 +02:00
|
|
|
cp $NDK/toolchains/llvm/prebuilt/linux-x86_64 $_TERMUX_TOOLCHAIN_TMPDIR -r
|
|
|
|
|
|
|
|
# Remove android-support header wrapping not needed on android-21:
|
|
|
|
rm -Rf $_TERMUX_TOOLCHAIN_TMPDIR/sysroot/usr/local
|
|
|
|
|
|
|
|
for HOST_PLAT in aarch64-linux-android armv7a-linux-androideabi i686-linux-android x86_64-linux-android; do
|
|
|
|
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 \
|
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT-gcc
|
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT-clang++ \
|
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/$HOST_PLAT-g++
|
|
|
|
done
|
|
|
|
|
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/armv7a-linux-androideabi$TERMUX_PKG_API_LEVEL-clang \
|
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/arm-linux-androideabi-clang
|
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/armv7a-linux-androideabi$TERMUX_PKG_API_LEVEL-clang++ \
|
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/arm-linux-androideabi-clang++
|
|
|
|
cp $_TERMUX_TOOLCHAIN_TMPDIR/bin/armv7a-linux-androideabi-cpp \
|
|
|
|
$_TERMUX_TOOLCHAIN_TMPDIR/bin/arm-linux-androideabi-cpp
|
|
|
|
|
2021-09-17 13:40:15 +02:00
|
|
|
# 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 "$PKG_CONFIG_LIBDIR"
|
|
|
|
cat > $_TERMUX_TOOLCHAIN_TMPDIR/bin/pkg-config <<-HERE
|
|
|
|
#!/bin/sh
|
|
|
|
export PKG_CONFIG_DIR=
|
|
|
|
export PKG_CONFIG_LIBDIR=$PKG_CONFIG_LIBDIR
|
|
|
|
exec $_HOST_PKGCONFIG "\$@"
|
|
|
|
HERE
|
|
|
|
chmod +x $_TERMUX_TOOLCHAIN_TMPDIR/bin/pkg-config
|
|
|
|
|
2021-10-23 15:57:11 +02:00
|
|
|
# Fix broken as symlinks in r23b (which prevents building with
|
|
|
|
# the -fno-integrated-as flag)
|
|
|
|
for PLATFORM in aarch64-linux-android arm-linux-androideabi i686-linux-android x86_64-linux-android; do
|
|
|
|
unlink $_TERMUX_TOOLCHAIN_TMPDIR/$PLATFORM/bin/as
|
|
|
|
ln -s ../../bin/$PLATFORM-as $_TERMUX_TOOLCHAIN_TMPDIR/$PLATFORM/bin/as
|
|
|
|
done
|
|
|
|
|
2021-09-22 12:46:01 +02:00
|
|
|
cd $_TERMUX_TOOLCHAIN_TMPDIR/sysroot
|
|
|
|
for f in $TERMUX_SCRIPTDIR/ndk-patches/*.patch; do
|
|
|
|
echo "Applying ndk-patch: $(basename $f)"
|
|
|
|
sed "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" "$f" | \
|
|
|
|
sed "s%\@TERMUX_HOME\@%${TERMUX_ANDROID_HOME}%g" | \
|
|
|
|
patch --silent -p1;
|
|
|
|
done
|
|
|
|
# libintl.h: Inline implementation gettext functions.
|
|
|
|
# langinfo.h: Inline implementation of nl_langinfo().
|
|
|
|
cp "$TERMUX_SCRIPTDIR"/ndk-patches/{libintl.h,langinfo.h} usr/include
|
|
|
|
|
|
|
|
# Remove <sys/capability.h> because it is provided by libcap.
|
|
|
|
# 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.
|
|
|
|
# Remove <iconv.h> as it's provided by libiconv.
|
|
|
|
# Remove <spawn.h> as it's only for future (later than android-27).
|
|
|
|
# Remove <zlib.h> and <zconf.h> as we build our own zlib.
|
|
|
|
# Remove unicode headers provided by libicu.
|
|
|
|
rm usr/include/{sys/{capability,shm,sem},{glob,iconv,spawn,zlib,zconf}}.h
|
|
|
|
rm usr/include/unicode/{char16ptr,platform,ptypes,putil,stringoptions,ubidi,ubrk,uchar,uconfig,ucpmap,udisplaycontext,uenum,uldnames,ulocdata,uloc,umachine,unorm2,urename,uscript,ustring,utext,utf16,utf8,utf,utf_old,utypes,uvernum,uversion}.h
|
|
|
|
|
|
|
|
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
|
|
|
|
for dir in usr/lib/*; do
|
|
|
|
# This seem to be needed when building rust
|
|
|
|
# packages
|
|
|
|
echo 'INPUT(-lunwind)' > $dir/libgcc.a
|
|
|
|
done
|
|
|
|
|
2021-10-23 15:57:11 +02:00
|
|
|
grep -lrw $_TERMUX_TOOLCHAIN_TMPDIR/sysroot/usr/include/c++/v1 -e '<version>' | xargs -n 1 sed -i 's/<version>/\"version\"/g'
|
2021-09-22 12:46:01 +02:00
|
|
|
mv $_TERMUX_TOOLCHAIN_TMPDIR $TERMUX_STANDALONE_TOOLCHAIN
|
2019-02-27 19:37:27 +01:00
|
|
|
}
|