Toolchain configuration script

From WickyWiki
Revision as of 20:28, 9 August 2016 by Wilbert (talk | contribs) (Created page with "<source lang=bash> #!/bin/bash # Toolchain environment configuration script, modified by me, original copyright notice: # Copyright 2014-2016 Łukasz "JustArchi" Domeradzki...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
#!/bin/bash

# Toolchain environment configuration script, modified by me, original copyright notice:

	# Copyright 2014-2016 Łukasz "JustArchi" Domeradzki
	# Contact: JustArchi@JustArchi.net
	#
	# Licensed under the Apache License, Version 2.0 (the "License");
	# you may not use this file except in compliance with the License.
	# You may obtain a copy of the License at
	#
	#    http://www.apache.org/licenses/LICENSE-2.0
	#
	# Unless required by applicable law or agreed to in writing, software
	# distributed under the License is distributed on an "AS IS" BASIS,
	# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	# See the License for the specific language governing permissions and
	# limitations under the License.

# Root of NDK, where you can fiend $NDK/ndk-build binary
NDK="$HOME/ndk"

# NDK toolchain root folder, where you can find the $NDKTC/bin directory containing the $CROSS_COMPILE binaries
NDKTC="$HOME/ndkTC"

# Optional, may help NDK in some cases
export NDK_TOOLCHAIN_VERSION=4.9
export SYSROOT="$NDKTC/sysroot"

# SPECIFIC="1" for specific target devices, makefile settings might override/conflict these settings
SPECIFIC="1"

### SPECIFIC

# Defaults
MARCH="armv7-a"
MFPU="neon"
#floating point operations hard/soft
MFLOATABI="soft"

FLTO=1
for ARG in "$@"; do
	case "$ARG" in
		--march=*) MARCH="$(echo "$ARG" | cut -d '=' -f 2-)" ;;
		--mfpu=*) MFPU="$(echo "$ARG" | cut -d '=' -f 2-)" ;;
		--mfloat-abi=*) MFLOATABI="$(echo "$ARG" | cut -d '=' -f 2-)" ;;
		--no-flto) FLTO=0 ;;
	esac
done

# Device CFLAGS, these should be taken from TARGET_GLOBAL_CFLAGS property of BoardCommonConfig.mk of your device, eventually leave them empty for generic non-device-optimized build. The -march flag comes from TARGET_ARCH_VARIANT
DEVICECFLAGS="-march=$MARCH -mfpu=$MFPU -mfloat-abi=$MFLOATABI"

# This specifies optimization level used during compilation. Usually it's a good idea to keep it on "-O2" for best results, but you may want to experiment with "-Os", "-O3" or "-Ofast"
OLEVEL="-O3"

# This specifies extra optimization flags, which are not selected by any of optimization levels chosen above
# Please notice that they're pretty EXPERIMENTAL, and if you get any compilation errors, the first step is experimenting with them or disabling them completely, you may also want to try different O level

# Main ArchiDroid Optimizations flags
OPTICFLAGS="-fgcse-las -fgcse-sm -fipa-pta -fivopts -fomit-frame-pointer -frename-registers -fsection-anchors -ftree-loop-im -ftree-loop-ivcanon -funsafe-loop-optimizations -funswitch-loops -fweb"

# Graphgite ArchiDroid Optimization flags
OPTICFLAGS+=" -fgraphite -fgraphite-identity"

# Extra ArchiDroid ICE flags
OPTICFLAGS+=" -floop-block -floop-interchange -floop-nest-optimize -floop-parallelize-all -floop-strip-mine -fmodulo-sched -fmodulo-sched-allow-regmoves"

# Extra Android flags
OPTICFLAGS+=" -ffunction-sections -fdata-sections -fvisibility=hidden -s"

# This specifies extra linker optimizations. Same as above, in case of problems this is second step for finding out the culprit
LDFLAGS="-llog -Wl,-O3 -Wl,--as-needed -Wl,--gc-sections -Wl,--relax -Wl,--sort-common"

# This specifies additional sections to strip, for extra savings on size
STRIPFLAGS="-s -R .note -R .comment -R .gnu.version -R .gnu.version_r"

# Additional definitions, which may help some binaries to work with android
DEFFLAGS="-fPIC -fPIE -pie -DNDEBUG -D__ANDROID__ -DANDROID"

if [[ "$FLTO" -eq 1 ]]; then
	OPTICFLAGS+=" -flto"
	LDFLAGS+=" -Wl,-flto"
fi

### EXPERT

# This specifies host (target) for makefiles. In some rare scenarios you may also try "--host=arm-linux-androideabi"
# In general you shouldn't change that, as you're compiling binaries for low-level ARM-EABI and not Android itself
CONFIGANDROID="--host=arm-linux-eabi --with-sysroot=$SYSROOT"

# This specifies the CROSS_COMPILE variable, again, in some rare scenarios you may also try "arm-eabi-"
# But beware, NDK doesn't even offer anything apart from arm-linux-androideabi one, however custom toolchains such as Linaro offer arm-eabi as well
CROSS_COMPILE="arm-linux-androideabi-"

# Workaround for some broken compilers with malloc problems (undefined reference to rpl_malloc and similar errors during compiling), don't uncomment unless you need it
#export ac_cv_func_malloc_0_nonnull=yes

### CORE

# You shouldn't edit anything from now on
export CROSS_COMPILE="$CROSS_COMPILE" # All makefiles depend on CROSS_COMPILE variable, this is important to set"

# If SPECIFIC is specified, we override flags used by makefiles with our optimized ones, of course if makefile allows that

#Note: makefiles may use our CFLAGS and LFLAGS, so I suggest to double-check them and eventually append them to makefile"
#Note: makefiles with configure options should maintain CC, CFLAGS and LDFLAGS, so if you're using ./configure, probably you don't need to do anything else"

if [[ "$SPECIFIC" -ne 0 ]]; then 
	CFLAGS="$OLEVEL $DEVICECFLAGS $OPTICFLAGS $DEFFLAGS --sysroot=$SYSROOT"
	# same flags for CXX
	CXXFLAGS="$CFLAGS"
	# CPP is the same as CXX, because they're both used in different makefiles/compilers
	CPPFLAGS="$CPPFLAGS"
	export CFLAGS="$CFLAGS"
	export LOCAL_CFLAGS="$CFLAGS"
	export CXXFLAGS="$CFLAGS" # We use same flags for CXX as well
	export LOCAL_CXXFLAGS="$CXXFLAGS"
	# CPP is the same as CXX, because they're both used in different makefiles/compilers
	export CPPFLAGS="$CPPFLAGS" 
	export LOCAL_CPPFLAGS="$CPPFLAGS"
	export LDFLAGS="$LDFLAGS"
	export LOCAL_LDFLAGS="$LDFLAGS"
else
	unset CFLAGS
	unset CXXFLAGS
	unset CPPFLAGS
	unset LDFLAGS
fi

# If NDK doesn't exist in the path (yet), prepend it
if [[ -n "$NDK" && -d "$NDK" ]] && ! echo "$PATH" | grep -q "$NDK"; then 
	export PATH="$NDK:$PATH"
fi

# If NDKTC doesn't exist in the path (yet), prepend it
if [[ -n "$NDKTC" && -d "$NDKTC" ]] && ! echo "$PATH" | grep -q "$NDKTC"; then
	export PATH="$NDKTC/bin:$PATH"
fi

# Common makefile references
export AS=${CROSS_COMPILE}as
export AR=${CROSS_COMPILE}ar
export CC=${CROSS_COMPILE}gcc
export CXX=${CROSS_COMPILE}g++
export CPP=${CROSS_COMPILE}cpp
export LD=${CROSS_COMPILE}ld
export NM=${CROSS_COMPILE}nm
export OBJCOPY=${CROSS_COMPILE}objcopy
export OBJDUMP=${CROSS_COMPILE}objdump
export READELF=${CROSS_COMPILE}readelf
export RANLIB=${CROSS_COMPILE}ranlib
export SIZE=${CROSS_COMPILE}size
export STRINGS=${CROSS_COMPILE}strings
export STRIP=${CROSS_COMPILE}strip

export CONFIGANDROID="$CONFIGANDROID"
export CCC="$CC $CFLAGS $LDFLAGS"
export CXX="$CXX $CXXFLAGS $LDFLAGS"
export SSTRIP="$STRIP $STRIPFLAGS"

echo
echo "CFLAGS: $CFLAGS"
echo
echo "LDFLAGS: $LDFLAGS"
echo
echo "CC: $CC and this points to $(which "$CC")"
echo
echo "Use \"\$CC\" command for calling gcc and \"\$CCC\" command for calling our optimized CC"
echo "Use \"\$CXX\" command for calling g++ and \"\$CCXX\" for calling our optimized CXX"
echo "Use \"\$STRIP\" command for calling strip and \"\$SSTRIP\" command for calling our optimized STRIP"
echo "Use \"./configure \$CONFIGANDROID\" when working with make files"
echo
echo "Example: \"\$CCC myprogram.c -o mybinary && \$SSTRIP mybinary \""