xref: /freebsd/sys/contrib/libsodium/dist-build/android-build.sh (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1#! /bin/sh
2
3if [ -z "$NDK_PLATFORM" ]; then
4  export NDK_PLATFORM="android-16"
5fi
6export NDK_PLATFORM_COMPAT="${NDK_PLATFORM_COMPAT:-${NDK_PLATFORM}}"
7export NDK_API_VERSION=$(echo "$NDK_PLATFORM" | sed 's/^android-//')
8export NDK_API_VERSION_COMPAT=$(echo "$NDK_PLATFORM_COMPAT" | sed 's/^android-//')
9
10if [ -z "$ANDROID_NDK_HOME" ]; then
11  echo "You should probably set ANDROID_NDK_HOME to the directory containing"
12  echo "the Android NDK"
13  exit
14fi
15
16if [ ! -f ./configure ]; then
17  echo "Can't find ./configure. Wrong directory or haven't run autogen.sh?" >&2
18  exit 1
19fi
20
21if [ "x$TARGET_ARCH" = 'x' ] || [ "x$ARCH" = 'x' ] || [ "x$HOST_COMPILER" = 'x' ]; then
22  echo "You shouldn't use android-build.sh directly, use android-[arch].sh instead" >&2
23  exit 1
24fi
25
26export MAKE_TOOLCHAIN="${ANDROID_NDK_HOME}/build/tools/make_standalone_toolchain.py"
27
28export PREFIX="$(pwd)/libsodium-android-${TARGET_ARCH}"
29export TOOLCHAIN_DIR="$(pwd)/android-toolchain-${TARGET_ARCH}"
30export PATH="${PATH}:${TOOLCHAIN_DIR}/bin"
31
32export CC=${CC:-"${HOST_COMPILER}-clang"}
33
34rm -rf "${TOOLCHAIN_DIR}" "${PREFIX}"
35
36echo
37if [ "$NDK_PLATFORM" != "$NDK_PLATFORM_COMPAT" ]; then
38  echo "Building for platform [${NDK_PLATFORM}], retaining compatibility with platform [${NDK_PLATFORM_COMPAT}]"
39else
40  echo "Building for platform [${NDK_PLATFORM}]"
41fi
42echo
43
44env - PATH="$PATH" \
45    "$MAKE_TOOLCHAIN" --force --api="$NDK_API_VERSION_COMPAT" \
46    --arch="$ARCH" --install-dir="$TOOLCHAIN_DIR" || exit 1
47
48if [ -z "$LIBSODIUM_FULL_BUILD" ]; then
49  export LIBSODIUM_ENABLE_MINIMAL_FLAG="--enable-minimal"
50else
51  export LIBSODIUM_ENABLE_MINIMAL_FLAG=""
52fi
53
54./configure \
55    --disable-soname-versions \
56    ${LIBSODIUM_ENABLE_MINIMAL_FLAG} \
57    --host="${HOST_COMPILER}" \
58    --prefix="${PREFIX}" \
59    --with-sysroot="${TOOLCHAIN_DIR}/sysroot" || exit 1
60
61if [ "$NDK_PLATFORM" != "$NDK_PLATFORM_COMPAT" ]; then
62  egrep '^#define ' config.log | sort -u > config-def-compat.log
63  echo
64  echo "Configuring again for platform [${NDK_PLATFORM}]"
65  echo
66  env - PATH="$PATH" \
67      "$MAKE_TOOLCHAIN" --force --api="$NDK_API_VERSION" \
68      --arch="$ARCH" --install-dir="$TOOLCHAIN_DIR" || exit 1
69
70  ./configure \
71      --disable-soname-versions \
72      ${LIBSODIUM_ENABLE_MINIMAL_FLAG} \
73      --host="${HOST_COMPILER}" \
74      --prefix="${PREFIX}" \
75      --with-sysroot="${TOOLCHAIN_DIR}/sysroot" || exit 1
76
77  egrep '^#define ' config.log | sort -u > config-def.log
78  if ! cmp config-def.log config-def-compat.log; then
79    echo "Platform [${NDK_PLATFORM}] is not backwards-compatible with [${NDK_PLATFORM_COMPAT}]" >&2
80    diff -u config-def.log config-def-compat.log >&2
81    exit 1
82  fi
83  rm -f config-def.log config-def-compat.log
84fi
85
86
87NPROCESSORS=$(getconf NPROCESSORS_ONLN 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null)
88PROCESSORS=${NPROCESSORS:-3}
89
90make clean && \
91make -j${PROCESSORS} install && \
92echo "libsodium has been installed into ${PREFIX}"
93