2 # This script includes android specific config (GNU GCC)
8 Import('env', 'RELEASE_BUILD', 'TARGET_CPU_ARCH', 'ANDROID_NDK')
12 *************************************** Error *********************************
13 * Android NDK path (ANDROID_NDK) isn't set, you can set enviornment variable*
14 * ANDROID_NDK or add it in command line as: *
15 * # scons ANDROID_NDK=<path to android NDK> ... *
16 *******************************************************************************
20 # Overwrite suffixes and prefixes
21 if env['HOST_OS'] == 'win32':
22 env['OBJSUFFIX'] = '.o'
23 env['SHOBJSUFFIX'] = '.os'
24 env['LIBPREFIX'] = 'lib'
25 env['LIBSUFFIX'] = '.a'
26 env['SHLIBPREFIX'] = 'lib'
27 env['SHLIBSUFFIX'] = '.so'
28 env['LIBPREFIXES'] = ['lib']
29 env['LIBSUFFIXES'] = ['.a', '.so']
30 env['PROGSUFFIX'] = ''
31 elif platform.system().lower() == 'darwin':
32 env['SHLIBSUFFIX'] = '.so'
33 env['LIBSUFFIXES'] = ['.a', '.so']
34 env['PROGSUFFIX'] = ''
36 ######################################################################
38 ######################################################################
40 # Android build system default cofig
41 env.AppendUnique(CPPDEFINES = ['ANDROID'])
42 env.AppendUnique(CFLAGS = ['-Wa,--noexecstack'])
43 env.AppendUnique(CXXFLAGS = ['-Wa,--noexecstack', '-fstack-protector'])
44 env.AppendUnique(LINKFLAGS = ['-Wl,--gc-sections', '-Wl,-z,nocopyreloc'])
46 ######################################################################
47 # Probe Android NDK default flags
48 ######################################################################
49 ndk_build_cmd = ANDROID_NDK + '/ndk-build'
50 if env['HOST_OS'] == 'win32':
51 if os.path.isfile(ndk_build_cmd + '.cmd'):
52 ndk_build_cmd += '.cmd'
54 if not os.path.isfile(ndk_build_cmd):
56 *************************************** Error *********************************
57 * It seems android ndk path is not set properly, please check if "%s"
58 * is the root directory of android ndk. *
59 *******************************************************************************
63 ANDROID_HOME = os.environ.get('ANDROID_HOME')
64 if ANDROID_HOME is not None:
65 ANDROID_HOME = os.path.abspath(ANDROID_HOME)
67 if ANDROID_HOME is None or not os.path.exists(ANDROID_HOME):
69 *************************************** Warning *******************************
70 * Enviornment variable ANDROID_HOME is not set properly. It should be the *
71 * root directory of android sdk. If you'd like build Java code, it's required.*
72 *******************************************************************************
75 # Android ndk early version doesn't support C++11. Detect the toolchain
76 # and platform version to make sure the newest version is used.
78 # Detect toolchain version
79 for tc_ver in ['4.9', '4.8', '4.7', '']:
80 if os.path.exists(ANDROID_NDK + '/sources/cxx-stl/gnu-libstdc++/' + tc_ver):
83 # Detect platform version.
85 for pf_ver in range(0, 100): # 100 should be big enough :)
86 if os.path.exists(ANDROID_NDK + '/platforms/android-%d' % pf_ver):
87 platform_ver = "%d" % pf_ver
90 cmd.append('APP_ABI=' + TARGET_CPU_ARCH)
91 cmd.append('APP_STL=gnustl_static')
93 cmd.append('APP_OPTIM=release')
95 cmd.append('APP_OPTIM=debug')
97 cmd.append('NDK_TOOLCHAIN_VERSION=' + tc_ver)
100 *************************************** Warning *******************************
101 * To support C++11, the toolchain should be >= 4.7, please make sure your *
102 * android NDK is at least r8e! *
103 *******************************************************************************
106 if platform_ver != '':
107 cmd.append('APP_PLATFORM=android-' + platform_ver)
110 p = subprocess.Popen(cmd, stdout = subprocess.PIPE)
112 for flags in p.stdout.readlines():
113 if cmp(flags[0:10], 'TC_PREFIX=') == 0: # toolchain prefix (include path)
114 prefix = flags[10:].strip()
115 env.Replace(CC = prefix + 'gcc')
116 env.Replace(CXX = prefix + 'g++')
117 env.Replace(AR = prefix + 'ar')
118 env.Replace(RANLIB = prefix + 'ranlib')
120 elif cmp(flags[0:7], 'CFLAGS=') == 0:
121 env.AppendUnique(CFLAGS = Split(flags[7:]))
123 elif cmp(flags[0:9], 'CXXFLAGS=') == 0:
124 env.AppendUnique(CXXFLAGS = Split(flags[9:]))
126 elif cmp(flags[0:8], 'CPPPATH=') == 0:
127 env.AppendUnique(CPPPATH = Split(flags[8:]))
129 elif cmp(flags[0:8], 'SYSROOT=') == 0:
130 env.AppendUnique(LINKFLAGS = ['--sysroot=' + flags[8:].strip()])
131 env.AppendUnique(LIBPATH = [flags[8:].strip() + '/usr/lib'])
133 elif cmp(flags[0:8], 'LDFLAGS=') == 0:
134 env.AppendUnique(LINKFLAGS = Split(flags[8:]))
136 elif cmp(flags[0:7], 'TC_VER=') == 0: # set gnustl library path
137 ver = flags[7:].strip()
138 env.AppendUnique(LIBPATH = [ANDROID_NDK + '/sources/cxx-stl/gnu-libstdc++/'
139 + ver + '/libs/' + TARGET_CPU_ARCH])
141 ######################################################################
142 # Set release/debug flags
143 ######################################################################
145 env.AppendUnique(CFLAGS = ['-Os'])
146 env.AppendUnique(CXXFLAGS = ['-Os'])
147 env.AppendUnique(CPPDEFINES = ['NDEBUG'])
148 env.AppendUnique(LINKFLAGS = ['-s'])
150 env.AppendUnique(CFLAGS = ['-g'])
151 env.AppendUnique(CXXFLAGS = ['-g'])