Replace glib threadpool usage with a 'dumb' thread implementation.
[platform/upstream/iotivity.git] / build_common / android / SConscript
1 ##
2 # This script includes android specific config (GNU GCC)
3 ##
4 import os
5 import platform
6 import subprocess
7
8 Import('env')
9
10 help_vars = Variables()
11 help_vars.Add(PathVariable('ANDROID_NDK', 'Android NDK root directory', os.environ.get('ANDROID_NDK')))
12 help_vars.Update(env)
13 Help(help_vars.GenerateHelpText(env))
14
15 android_ndk = env.get('ANDROID_NDK')
16 if not android_ndk:
17         print '''
18 *************************************** Error *********************************
19 *    Android NDK path isn't set, you can set enviornment variable ANDROID_NDK *
20 * or add it in command line as:                                               *
21 *      # scons ANDROID_NDK=<path to android NDK> ...                          *
22 *******************************************************************************
23 '''
24         Exit(1)
25
26 # Overwrite suffixes and prefixes
27 if env['HOST_OS'] == 'win32':
28         env['OBJSUFFIX'] = '.o'
29         env['SHOBJSUFFIX'] = '.os'
30         env['LIBPREFIX'] = 'lib'
31         env['LIBSUFFIX'] = '.a'
32         env['SHLIBPREFIX'] = 'lib'
33         env['SHLIBSUFFIX'] = '.so'
34         env['LIBPREFIXES'] = ['lib']
35         env['LIBSUFFIXES'] = ['.a', '.so']
36         env['PROGSUFFIX'] = ''
37 elif platform.system().lower() == 'darwin':
38         env['SHLIBSUFFIX'] = '.so'
39         env['LIBSUFFIXES'] = ['.a', '.so']
40         env['PROGSUFFIX'] = ''
41
42 ######################################################################
43 # Set common flags
44 ######################################################################
45
46 # Android build system default cofig
47 env.AppendUnique(CPPDEFINES = ['ANDROID'])
48 env.AppendUnique(CFLAGS = ['-std=c99'])
49 env.AppendUnique(SHCFLAGS = ['-Wa,--noexecstack'])
50 env.AppendUnique(LINKFLAGS = ['-Wl,--gc-sections', '-Wl,-z,nocopyreloc'])
51
52 ######################################################################
53 # Probe Android NDK default flags
54 ######################################################################
55 ndk_build_cmd = android_ndk + '/ndk-build'
56 if env['HOST_OS'] == 'win32':
57         if os.path.isfile(ndk_build_cmd + '.cmd'):
58                 ndk_build_cmd += '.cmd'
59
60 if not os.path.isfile(ndk_build_cmd):
61         print '''
62 *************************************** Error *********************************
63 *   It seems android ndk path is not set properly, please check if "%s"
64 * is the root directory of android ndk.                                       *
65 *******************************************************************************
66 ''' % android_ndk
67         Exit(1)
68
69 ANDROID_HOME = os.environ.get('ANDROID_HOME')
70 if ANDROID_HOME is not None:
71         ANDROID_HOME = os.path.abspath(ANDROID_HOME)
72
73 if ANDROID_HOME is None or not os.path.exists(ANDROID_HOME):
74         print '''
75 *************************************** Warning *******************************
76 *   Enviornment variable ANDROID_HOME is not set properly. It should be the   *
77 * root directory of android sdk. If you'd like build Java code, it's required.*
78 *******************************************************************************
79 '''
80
81 target_arch = env.get('TARGET_ARCH')
82
83 # Android ndk early version doesn't support C++11. Detect the toolchain version
84 # to make sure proper toolchain is used
85 for tc_ver in ['4.7', '4.8', '4.9', '']:
86         if os.path.exists(android_ndk + '/toolchains/x86-' + tc_ver):
87                 break
88
89 cmd = [ndk_build_cmd]
90 cmd.append('APP_ABI=' + target_arch)
91 cmd.append('APP_STL=gnustl_shared')
92 if env.get('RELEASE'):
93         cmd.append('APP_OPTIM=release')
94 else:
95         cmd.append('APP_OPTIM=debug')
96 if tc_ver != '':
97         cmd.append('NDK_TOOLCHAIN_VERSION=' + tc_ver)
98 else:
99         print '''
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 *******************************************************************************
104 '''
105
106 cmd.append('-n')
107
108 p = subprocess.Popen(cmd, stdout = subprocess.PIPE)
109
110 for flags in p.stdout.readlines():
111         if cmp(flags[0:10], 'TC_PREFIX=') == 0: # toolchain prefix (include path)
112                 prefix = flags[10:].strip()
113                 env.Replace(CC = prefix + 'gcc')
114                 env.Replace(CXX = prefix + 'g++')
115                 env.Replace(AR = prefix + 'ar')
116                 env.Replace(RANLIB = prefix + 'ranlib')
117
118         elif cmp(flags[0:7], 'CFLAGS=') == 0:
119                 env.AppendUnique(CFLAGS = Split(flags[7:]))
120
121         elif cmp(flags[0:9], 'CXXFLAGS=') == 0:
122                 env.AppendUnique(CXXFLAGS = Split(flags[9:]))
123
124         elif cmp(flags[0:8], 'CPPPATH=') == 0:
125                 env.AppendUnique(CPPPATH = Split(flags[8:]))
126
127         elif cmp(flags[0:8], 'SYSROOT=') == 0:
128                 sysroot = flags[8:].strip()
129                 env.AppendUnique(LINKFLAGS = ['--sysroot=' + sysroot])
130
131         elif cmp(flags[0:8], 'LDFLAGS=') == 0:
132                 env.AppendUnique(LINKFLAGS = Split(flags[8:]))
133
134         elif cmp(flags[0:7], 'TC_VER=') == 0:  # set gnustl library path
135                 ver = flags[7:].strip()
136                 stl_path = android_ndk + '/sources/cxx-stl/gnu-libstdc++/' + ver + '/libs/' + target_arch
137                 if target_arch in ['armeabi', 'armeabi-v7a', 'armeabi-v7a-hard']:
138                         stl_path = stl_path + '/thumb/'
139
140                 env.AppendUnique(LIBPATH = [stl_path])
141                 env.Install(env.get('BUILD_DIR'), stl_path + '/libgnustl_shared.so')
142
143         elif cmp(flags[0:9], 'PLATFORM=') == 0:  # get target platform: android-x
144                 platform_ver = flags[9+8:].strip()
145                 if not platform_ver.isdigit():
146                         platform_ver = ''
147
148
149         elif cmp(flags[0:9], 'PLATFORM=') == 0:  # get target platform: android-x
150                 platform_ver = flags[9+8:].strip()
151                 if not platform_ver.isdigit():
152                         platform_ver = ''
153
154 # Determine dependency faux SYS_ROOT
155 dep_sys_root = os.path.join(env.get('SRC_DIR'), 'dep', 'android', target_arch, 'usr')
156 dep_src_dir =  os.path.join(dep_sys_root, 'include')
157 dep_lib_dir =  os.path.join(dep_sys_root, 'lib')
158
159 env['DEP_SYS_ROOT'] = dep_sys_root
160
161 # Add external libraries including boost
162 env.AppendUnique(CPPPATH = [ dep_src_dir ])
163 env.AppendUnique(LIBPATH = [ dep_lib_dir ])
164
165 ######################################################################
166 # Set release/debug flags
167 ######################################################################
168 if env.get('RELEASE'):
169         env.AppendUnique(CCFLAGS = ['-Os'])
170         env.AppendUnique(CPPDEFINES = ['NDEBUG'])
171         env.AppendUnique(LINKFLAGS = ['-s'])
172 else:
173         env.AppendUnique(CCFLAGS = ['-g'])
174
175 if env.get('LOGGING'):
176         env.AppendUnique(CPPDEFINES = ['-DTB_LOG'])
177
178
179 env.AppendUnique(CPPDEFINES = ['WITH_POSIX', '__ANDROID__'])
180 env.AppendUnique(CCFLAGS = ['-Wall', '-fPIC'])
181 #env.AppendUnique(LINKFLAGS = ['-ldl', '-lpthread'])
182
183 env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
184 env.AppendUnique(LIBPATH = [src_dir + '/resource/csdk/connectivity/lib/android'])
185 env.AppendUnique(LIBS = ['log', 'coap'])
186
187 if env.get('SECURED') == '1':
188         env.AppendUnique(LIBS = ['tinydtls'])
189
190 # From android-5 (API > 20), all application must be built with flags '-fPIE' '-pie'.
191 # Due to the limitation of Scons, it's required to added it into the command line
192 # directly (otherwise, it will also be added when build share library)
193 env.Replace(CCCOM = '$CC -o $TARGET -c $CFLAGS $CCFLAGS $_CCCOMCOM -fPIE $SOURCES')
194 env.Replace(CXXCOM = '$CXX -o $TARGET -c $CXXFLAGS $CCFLAGS $_CCCOMCOM -fPIE $SOURCES')
195 env.Replace(LINKCOM = '$LINK -o $TARGET -pie $LINKFLAGS $__RPATH $SOURCES $_LIBDIRFLAGS $_LIBFLAGS')
196
197 # Fix android-ndk compatibility issue, make applications build on new NDK can run on old platform
198 if platform_ver == '' or int(platform_ver) > 20:
199         SConscript('compatibility/c_compat.scons')
200
201 SConscript('compatibility/cpp11_compat.scons')
202
203 # Make sure that boost for android is available
204 SConscript(env.get('SRC_DIR') + '/extlibs/boost/SConscript')