xref: /freebsd-src/contrib/unbound/contrib/android/setenv_android.sh (revision e25152834cdf3b353892835a4f3b157e066a8ed4)
1*25039b37SCy Schubert#!/usr/bin/env bash
2*25039b37SCy Schubert
3*25039b37SCy Schubert# ====================================================================
4*25039b37SCy Schubert# Sets the cross compile environment for Android
5*25039b37SCy Schubert#
6*25039b37SCy Schubert# Based upon OpenSSL's setenv-android.sh by TH, JW, and SM.
7*25039b37SCy Schubert# Heavily modified by JWW for Crypto++.
8*25039b37SCy Schubert# Updated by Skycoder42 for current recommendations for Android.
9*25039b37SCy Schubert# Modified by JWW for Unbound.
10*25039b37SCy Schubert# ====================================================================
11*25039b37SCy Schubert
12*25039b37SCy Schubert#########################################
13*25039b37SCy Schubert#####        Some validation        #####
14*25039b37SCy Schubert#########################################
15*25039b37SCy Schubert
16*25039b37SCy Schubertif [ -z "$ANDROID_API" ]; then
17*25039b37SCy Schubert    echo "ANDROID_API is not set. Please set it"
18*25039b37SCy Schubert    [[ "$0" = "${BASH_SOURCE[0]}" ]] && exit 1 || return 1
19*25039b37SCy Schubertfi
20*25039b37SCy Schubert
21*25039b37SCy Schubertif [ -z "$ANDROID_CPU" ]; then
22*25039b37SCy Schubert    echo "ANDROID_CPU is not set. Please set it"
23*25039b37SCy Schubert    [[ "$0" = "${BASH_SOURCE[0]}" ]] && exit 1 || return 1
24*25039b37SCy Schubertfi
25*25039b37SCy Schubert
26*25039b37SCy Schubertif [ ! -d "$ANDROID_NDK_ROOT" ]; then
27*25039b37SCy Schubert    echo "ERROR: ANDROID_NDK_ROOT is not a valid path. Please set it."
28*25039b37SCy Schubert    echo "NDK root is $ANDROID_NDK_ROOT"
29*25039b37SCy Schubert    [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
30*25039b37SCy Schubertfi
31*25039b37SCy Schubert
32*25039b37SCy Schubert# cryptest-android.sh may run this script without sourcing.
33*25039b37SCy Schubertif [ "$0" = "${BASH_SOURCE[0]}" ]; then
34*25039b37SCy Schubert    echo "setenv-android.sh is usually sourced, but not this time."
35*25039b37SCy Schubertfi
36*25039b37SCy Schubert
37*25039b37SCy Schubert#####################################################################
38*25039b37SCy Schubert
39*25039b37SCy Schubert# Need to set THIS_HOST to darwin-x86_64, linux-x86_64,
40*25039b37SCy Schubert# windows, or windows-x86_64
41*25039b37SCy Schubert
42*25039b37SCy Schubertif [[ "$(uname -s | grep -i -c darwin)" -ne 0 ]]; then
43*25039b37SCy Schubert    THIS_HOST=darwin-x86_64
44*25039b37SCy Schubertelif [[ "$(uname -s | grep -i -c linux)" -ne 0 ]]; then
45*25039b37SCy Schubert    THIS_HOST=linux-x86_64
46*25039b37SCy Schubertelse
47*25039b37SCy Schubert    echo "ERROR: Unknown host"
48*25039b37SCy Schubert    [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
49*25039b37SCy Schubertfi
50*25039b37SCy Schubert
51*25039b37SCy SchubertANDROID_TOOLCHAIN="$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/$THIS_HOST/bin"
52*25039b37SCy SchubertANDROID_SYSROOT="$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/$THIS_HOST/sysroot"
53*25039b37SCy Schubert
54*25039b37SCy Schubert# Error checking
55*25039b37SCy Schubertif [ ! -d "$ANDROID_TOOLCHAIN" ]; then
56*25039b37SCy Schubert    echo "ERROR: ANDROID_TOOLCHAIN is not a valid path. Please set it."
57*25039b37SCy Schubert    echo "Path is $ANDROID_TOOLCHAIN"
58*25039b37SCy Schubert    [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
59*25039b37SCy Schubertfi
60*25039b37SCy Schubert
61*25039b37SCy Schubert# Error checking
62*25039b37SCy Schubertif [ ! -d "$ANDROID_SYSROOT" ]; then
63*25039b37SCy Schubert    echo "ERROR: ANDROID_SYSROOT is not a valid path. Please set it."
64*25039b37SCy Schubert    echo "Path is $ANDROID_SYSROOT"
65*25039b37SCy Schubert    [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
66*25039b37SCy Schubertfi
67*25039b37SCy Schubert
68*25039b37SCy Schubert#####################################################################
69*25039b37SCy Schubert
70*25039b37SCy SchubertTHE_ARCH=$(tr '[:upper:]' '[:lower:]' <<< "$ANDROID_CPU")
71*25039b37SCy Schubert
72*25039b37SCy Schubert# https://developer.android.com/ndk/guides/abis.html
73*25039b37SCy Schubertcase "$THE_ARCH" in
74*25039b37SCy Schubert  armv7*|armeabi*)
75*25039b37SCy Schubert    CC="armv7a-linux-androideabi$ANDROID_API-clang"
76*25039b37SCy Schubert    CXX="armv7a-linux-androideabi$ANDROID_API-clang++"
77*25039b37SCy Schubert    LD="arm-linux-androideabi-ld"
78*25039b37SCy Schubert    AS="arm-linux-androideabi-as"
79*25039b37SCy Schubert    AR="arm-linux-androideabi-ar"
80*25039b37SCy Schubert    RANLIB="arm-linux-androideabi-ranlib"
81*25039b37SCy Schubert    STRIP="arm-linux-androideabi-strip"
82*25039b37SCy Schubert
83*25039b37SCy Schubert    CFLAGS="-march=armv7-a -mthumb -mfloat-abi=softfp -funwind-tables -fexceptions"
84*25039b37SCy Schubert    CXXFLAGS="-march=armv7-a -mthumb -mfloat-abi=softfp -funwind-tables -fexceptions -frtti"
85*25039b37SCy Schubert    ;;
86*25039b37SCy Schubert
87*25039b37SCy Schubert  armv8*|aarch64|arm64*)
88*25039b37SCy Schubert    CC="aarch64-linux-android$ANDROID_API-clang"
89*25039b37SCy Schubert    CXX="aarch64-linux-android$ANDROID_API-clang++"
90*25039b37SCy Schubert    LD="aarch64-linux-android-ld"
91*25039b37SCy Schubert    AS="aarch64-linux-android-as"
92*25039b37SCy Schubert    AR="aarch64-linux-android-ar"
93*25039b37SCy Schubert    RANLIB="aarch64-linux-android-ranlib"
94*25039b37SCy Schubert    STRIP="aarch64-linux-android-strip"
95*25039b37SCy Schubert
96*25039b37SCy Schubert    CFLAGS="-funwind-tables -fexceptions"
97*25039b37SCy Schubert    CXXFLAGS="-funwind-tables -fexceptions -frtti"
98*25039b37SCy Schubert    ;;
99*25039b37SCy Schubert
100*25039b37SCy Schubert  x86)
101*25039b37SCy Schubert    CC="i686-linux-android$ANDROID_API-clang"
102*25039b37SCy Schubert    CXX="i686-linux-android$ANDROID_API-clang++"
103*25039b37SCy Schubert    LD="i686-linux-android-ld"
104*25039b37SCy Schubert    AS="i686-linux-android-as"
105*25039b37SCy Schubert    AR="i686-linux-android-ar"
106*25039b37SCy Schubert    RANLIB="i686-linux-android-ranlib"
107*25039b37SCy Schubert    STRIP="i686-linux-android-strip"
108*25039b37SCy Schubert
109*25039b37SCy Schubert    CFLAGS="-mtune=intel -mssse3 -mfpmath=sse -funwind-tables -fexceptions"
110*25039b37SCy Schubert    CXXFLAGS="-mtune=intel -mssse3 -mfpmath=sse -funwind-tables -fexceptions -frtti"
111*25039b37SCy Schubert    ;;
112*25039b37SCy Schubert
113*25039b37SCy Schubert  x86_64|x64)
114*25039b37SCy Schubert    CC="x86_64-linux-android$ANDROID_API-clang"
115*25039b37SCy Schubert    CXX="x86_64-linux-android$ANDROID_API-clang++"
116*25039b37SCy Schubert    LD="x86_64-linux-android-ld"
117*25039b37SCy Schubert    AS="x86_64-linux-android-as"
118*25039b37SCy Schubert    AR="x86_64-linux-android-ar"
119*25039b37SCy Schubert    RANLIB="x86_64-linux-android-ranlib"
120*25039b37SCy Schubert    STRIP="x86_64-linux-android-strip"
121*25039b37SCy Schubert
122*25039b37SCy Schubert    CFLAGS="-march=x86-64 -msse4.2 -mpopcnt -mtune=intel -funwind-tables -fexceptions"
123*25039b37SCy Schubert    CXXFLAGS="-march=x86-64 -msse4.2 -mpopcnt -mtune=intel -funwind-tables -fexceptions -frtti"
124*25039b37SCy Schubert    ;;
125*25039b37SCy Schubert
126*25039b37SCy Schubert  *)
127*25039b37SCy Schubert    echo "ERROR: Unknown architecture $ANDROID_CPU"
128*25039b37SCy Schubert    [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
129*25039b37SCy Schubert    ;;
130*25039b37SCy Schubertesac
131*25039b37SCy Schubert
132*25039b37SCy Schubert#####################################################################
133*25039b37SCy Schubert
134*25039b37SCy Schubert# Error checking
135*25039b37SCy Schubertif [ ! -e "$ANDROID_TOOLCHAIN/$CC" ]; then
136*25039b37SCy Schubert    echo "ERROR: Failed to find Android clang. Please edit this script."
137*25039b37SCy Schubert    [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
138*25039b37SCy Schubertfi
139*25039b37SCy Schubert
140*25039b37SCy Schubert# Error checking
141*25039b37SCy Schubertif [ ! -e "$ANDROID_TOOLCHAIN/$CXX" ]; then
142*25039b37SCy Schubert    echo "ERROR: Failed to find Android clang++. Please edit this script."
143*25039b37SCy Schubert    [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
144*25039b37SCy Schubertfi
145*25039b37SCy Schubert
146*25039b37SCy Schubert# Error checking
147*25039b37SCy Schubertif [ ! -e "$ANDROID_TOOLCHAIN/$RANLIB" ]; then
148*25039b37SCy Schubert    echo "ERROR: Failed to find Android ranlib. Please edit this script."
149*25039b37SCy Schubert    [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
150*25039b37SCy Schubertfi
151*25039b37SCy Schubert
152*25039b37SCy Schubert# Error checking
153*25039b37SCy Schubertif [ ! -e "$ANDROID_TOOLCHAIN/$AR" ]; then
154*25039b37SCy Schubert    echo "ERROR: Failed to find Android ar. Please edit this script."
155*25039b37SCy Schubert    [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
156*25039b37SCy Schubertfi
157*25039b37SCy Schubert
158*25039b37SCy Schubert# Error checking
159*25039b37SCy Schubertif [ ! -e "$ANDROID_TOOLCHAIN/$AS" ]; then
160*25039b37SCy Schubert    echo "ERROR: Failed to find Android as. Please edit this script."
161*25039b37SCy Schubert    [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
162*25039b37SCy Schubertfi
163*25039b37SCy Schubert
164*25039b37SCy Schubert# Error checking
165*25039b37SCy Schubertif [ ! -e "$ANDROID_TOOLCHAIN/$LD" ]; then
166*25039b37SCy Schubert    echo "ERROR: Failed to find Android ld. Please edit this script."
167*25039b37SCy Schubert    [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
168*25039b37SCy Schubertfi
169*25039b37SCy Schubert
170*25039b37SCy Schubert#####################################################################
171*25039b37SCy Schubert
172*25039b37SCy SchubertLENGTH=${#ANDROID_TOOLCHAIN}
173*25039b37SCy SchubertSUBSTR=${PATH:0:$LENGTH}
174*25039b37SCy Schubertif [ "$SUBSTR" != "$ANDROID_TOOLCHAIN" ]; then
175*25039b37SCy Schubert    export PATH="$ANDROID_TOOLCHAIN:$PATH"
176*25039b37SCy Schubertfi
177*25039b37SCy Schubert
178*25039b37SCy Schubert#####################################################################
179*25039b37SCy Schubert
180*25039b37SCy Schubertexport CPP CC CXX LD AS AR RANLIB STRIP
181*25039b37SCy Schubertexport ANDROID_SYSROOT="$AOSP_SYSROOT"
182*25039b37SCy Schubertexport CPPFLAGS="-D__ANDROID_API__=$ANDROID_API"
183*25039b37SCy Schubertexport CFLAGS="$CFLAGS --sysroot=$AOSP_SYSROOT"
184*25039b37SCy Schubertexport CXXFLAGS="$CXXFLAGS -stdlib=libc++ --sysroot=$AOSP_SYSROOT"
185*25039b37SCy Schubert
186*25039b37SCy Schubert#####################################################################
187*25039b37SCy Schubert
188*25039b37SCy Schubertecho "ANDROID_TOOLCHAIN: $ANDROID_TOOLCHAIN"
189*25039b37SCy Schubert
190*25039b37SCy Schubertecho "CPP: $(command -v "$CPP")"
191*25039b37SCy Schubertecho "CC: $(command -v "$CC")"
192*25039b37SCy Schubertecho "CXX: $(command -v "$CXX")"
193*25039b37SCy Schubertecho "LD: $(command -v "$LD")"
194*25039b37SCy Schubertecho "AS: $(command -v "$AS")"
195*25039b37SCy Schubertecho "AR: $(command -v "$AR")"
196*25039b37SCy Schubert
197*25039b37SCy Schubertecho "ANDROID_SYSROOT: $ANDROID_SYSROOT"
198*25039b37SCy Schubert
199*25039b37SCy Schubertecho "CPPFLAGS: $CPPFLAGS"
200*25039b37SCy Schubertecho "CFLAGS: $CFLAGS"
201*25039b37SCy Schubertecho "CXXFLAGS: $CXXFLAGS"
202*25039b37SCy Schubert
203*25039b37SCy Schubert[ "$0" = "${BASH_SOURCE[0]}" ] && exit 0 || return 0
204