#################################################################
# Important: this build file has been tested with Android NDK 11
# It may or may not work with other releases of the NDK. Please notify
# us if you find a newer NDK for which this does not work.
#################################################################

import os
import re
import sys
import platform

# we need to know when the NDK is
ANDROID_NDK_ROOT=os.getenv('ANDROID_NDK_ROOT')
if not ANDROID_NDK_ROOT:
    raise Exception('ANDROID_NDK_ROOT environment variable not set')

# detect the host system on which we're running
if env.has_key('android_host_system') and env['android_host_system']:
	ANDROID_HOST_SYSTEM = env['android_host_system']
else:
	PLATFORM_TO_TARGET_MAP = {
    	'linux-i386' : 'linux-x86',
    	'linux2'     : 'linux-x86',
    	'win32'      : 'windows',
    	'cygwin'     : 'windows',
    	'darwin'     : 'darwin-x86_64'
	}
	if sys.platform in PLATFORM_TO_TARGET_MAP:
		ANDROID_HOST_SYSTEM = PLATFORM_TO_TARGET_MAP[sys.platform]
	else:
		raise Exception('Android Host Platform cannot be determined')

# set defaults
ANDROID_ARCH         = 'arm'
ANDROID_PLATFORM     = 'android-3'
ANDROID_TOOLCHAIN    = 'arm-linux-androideabi-4.9'
ANDROID_CROSS_PREFIX = 'arm-linux-androideabi'

if not os.path.exists(os.path.join(ANDROID_NDK_ROOT, 'platforms', ANDROID_PLATFORM)):
    ANDROID_PLATFORM = None
    for platform in range(3,100):
        platform_string = 'android-'+str(platform)
        if os.path.exists(os.path.join(ANDROID_NDK_ROOT, 'platforms', platform_string)):
            ANDROID_PLATFORM = platform_string
            print 'Auto-selected first available platform:', ANDROID_PLATFORM
            break

if not os.path.exists(os.path.join(ANDROID_NDK_ROOT, 'toolchains', ANDROID_TOOLCHAIN)):
    toolchain_dirs = os.listdir(ANDROID_NDK_ROOT+'/toolchains')
    for toolchain_dir in toolchain_dirs:
        if os.path.exists(os.path.join(ANDROID_NDK_ROOT, 'toolchains', toolchain_dir, 'prebuilt', ANDROID_HOST_SYSTEM)):
            ANDROID_TOOLCHAIN=toolchain_dir
            suffix_pos = toolchain_dir.rfind('-')
            if (suffix_pos >= 0):
                ANDROID_CROSS_PREFIX = ANDROID_TOOLCHAIN[:suffix_pos]
            print "Auto-selecting toolchain:", ANDROID_TOOLCHAIN
            break

if not os.path.exists(os.path.join(ANDROID_NDK_ROOT, 'toolchains', ANDROID_TOOLCHAIN)):
    raise Exception('No valid Android toolchain found')

# override defaults from command line args
if ARGUMENTS.get('android_toolchain'):
    ANDROID_TOOLCHAIN=ARGUMENTS.get('android_toolchain')

if ARGUMENTS.get('android_cross_prefix'):
    ANDROID_CROSS_PREFIX=ARGUMENTS.get('android_cross_prefix')

if ARGUMENTS.get('android_platform'):
    ANDROID_PLATFORM=ARGUMENTS.get('android_platform')

if ARGUMENTS.get('android_arch'):
    ANDROID_ARCH=ARGUMENTS.get('android_arch')

print 'Building for Android: '
print 'ANDROID_HOST_SYSTEM =', ANDROID_HOST_SYSTEM
print 'ANDROID_TOOLCHAIN   =', ANDROID_TOOLCHAIN
print 'ANDROID_PLATFORM    =', ANDROID_PLATFORM
print 'ANDROID_ARCH        =', ANDROID_ARCH

if not os.path.exists(os.path.join(ANDROID_NDK_ROOT, 'platforms', ANDROID_PLATFORM)):
    raise Exception('Android platform '+ANDROID_PLATFORM+' not found')
    
ANDROID_TOOLCHAIN_BIN = ANDROID_NDK_ROOT+'/toolchains/'+ANDROID_TOOLCHAIN+'/prebuilt/'+ANDROID_HOST_SYSTEM+'/bin'
if not os.path.exists(ANDROID_TOOLCHAIN_BIN) and ANDROID_HOST_SYSTEM is 'darwin-x86':
    ANDROID_HOST_SYSTEM = 'darwin-x86_64'
    ANDROID_TOOLCHAIN_BIN = ANDROID_NDK_ROOT+'/toolchains/'+ANDROID_TOOLCHAIN+'/prebuilt/'+ANDROID_HOST_SYSTEM+'/bin'
ANDROID_SYSROOT = ANDROID_NDK_ROOT+'/platforms/'+ANDROID_PLATFORM+'/arch-'+ANDROID_ARCH

### add the tools to the path
env.PrependENVPath('PATH', ANDROID_TOOLCHAIN_BIN)

### special C Runtime startup for executables
env['AP4_EXTRA_EXECUTABLE_OBJECTS'] = [ANDROID_SYSROOT+'/usr/lib/crtbegin_static.o',
                                       ANDROID_SYSROOT+'/usr/lib/crtend_android.o']
env['AP4_EXTRA_LIBS'] = ['gcc']

### Load the tools
LoadTool('gcc-generic', env, gcc_cross_prefix=ANDROID_CROSS_PREFIX, gcc_strict=False)
env.AppendUnique(CCFLAGS = ['-I'+ANDROID_NDK_ROOT+'/sources/cxx-stl/system/include' ,
                            '--sysroot', ANDROID_SYSROOT,
                            '-msoft-float',
                            '-fpic',
                            '-mthumb-interwork',
                            '-ffunction-sections',
                            '-funwind-tables',
                            '-fstack-protector',
                            '-fno-short-enums'])
env.AppendUnique(CXXFLAGS = ['-fno-exceptions', '-fno-rtti'])
env.AppendUnique(CPPDEFINES = ['ANDROID', 'NPT_CONFIG_HAVE_SYSTEM_LOG_CONFIG'])
env.AppendUnique(LINKFLAGS = ['-Bdynamic',
                			  '-Wl,--no-undefined',
                			  '-Wl,-rpath=/system/lib',
                              '-Wl,--dynamic-linker=/system/bin/linker',
                              '-Wl,-rpath-link=' + ANDROID_SYSROOT + '/usr/lib',
                              '-nostdlib',
                			  '-L'+ANDROID_SYSROOT+'/usr/lib',
                			  '-lc',
                			  '-lstdc++',
                			  '-lm',
                			  '-llog',
                			  '-ldl',
                              '-pie'])

### Don't build a shared library
env['AP4_BUILD_CONFIG_NO_SHARED_LIB'] = True

### System specific choices
env['AP4_SYSTEM_SOURCES'] = {'System/Android':['*.cpp'], 'System/Posix':['*.cpp']}
