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