Merge "There are Two modifications. 1. Restucturing Notification Manager Class ...
[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 = ['-Wa,--noexecstack'])
49 env.AppendUnique(CXXFLAGS = ['-Wa,--noexecstack', '-fstack-protector'])
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
84 # and platform version to make sure the newest version is used.
85
86 # Detect toolchain version
87 for tc_ver in ['4.9', '4.8', '4.7', '']:
88         if os.path.exists(android_ndk + '/sources/cxx-stl/gnu-libstdc++/' + tc_ver):
89                 break
90
91 # Detect platform version.
92 platform_ver = ''
93 for pf_ver in range(0, 100): # 100 should be big enough :)
94         if os.path.exists(android_ndk + '/platforms/android-%d' % pf_ver):
95                 platform_ver = "%d" % pf_ver
96
97 cmd = [ndk_build_cmd]
98 cmd.append('APP_ABI=' + target_arch)
99 cmd.append('APP_STL=gnustl_static')
100 if env.get('RELEASE'):
101         cmd.append('APP_OPTIM=release')
102 else:
103         cmd.append('APP_OPTIM=debug')
104 if tc_ver != '':
105         cmd.append('NDK_TOOLCHAIN_VERSION=' + tc_ver)
106 else:
107         print '''
108 *************************************** Warning *******************************
109 *   To support C++11, the toolchain should be >= 4.7, please make sure your   *
110 * android NDK is at least r8e!                                                *
111 *******************************************************************************
112 '''
113
114 if platform_ver != '':
115         cmd.append('APP_PLATFORM=android-' + platform_ver)
116 cmd.append('-n')
117
118 p = subprocess.Popen(cmd, stdout = subprocess.PIPE)
119
120 for flags in p.stdout.readlines():
121         if cmp(flags[0:10], 'TC_PREFIX=') == 0: # toolchain prefix (include path)
122                 prefix = flags[10:].strip()
123                 env.Replace(CC = prefix + 'gcc')
124                 env.Replace(CXX = prefix + 'g++')
125                 env.Replace(AR = prefix + 'ar')
126                 env.Replace(RANLIB = prefix + 'ranlib')
127
128         elif cmp(flags[0:7], 'CFLAGS=') == 0:
129                 env.AppendUnique(CFLAGS = Split(flags[7:]))
130
131         elif cmp(flags[0:9], 'CXXFLAGS=') == 0:
132                 env.AppendUnique(CXXFLAGS = Split(flags[9:]))
133
134         elif cmp(flags[0:8], 'CPPPATH=') == 0:
135                 env.AppendUnique(CPPPATH = Split(flags[8:]))
136
137         elif cmp(flags[0:8], 'SYSROOT=') == 0:
138                 sysroot = flags[8:].strip()
139                 env.AppendUnique(LINKFLAGS = ['--sysroot=' + sysroot])
140                 env.AppendUnique(LIBPATH = [sysroot + '/usr/lib'])
141                 # To fix android NDK issue
142                 # Some functions, e.g. rand, srand. strtof ... are static in static inline
143                 # prior to android-L. so libc.so before android-L doesn't have them. If build
144                 # build on android-L and the function(s) is used, should link libc.a
145                 if platform_ver == '' or int(platform_ver) > 20:
146                         env.AppendUnique(LIBS = File(sysroot + '/usr/lib/libc.a'))
147
148         elif cmp(flags[0:8], 'LDFLAGS=') == 0:
149                 env.AppendUnique(LINKFLAGS = Split(flags[8:]))
150
151         elif cmp(flags[0:7], 'TC_VER=') == 0:  # set gnustl library path
152                 ver = flags[7:].strip()
153                 env.AppendUnique(LIBPATH = [android_ndk + '/sources/cxx-stl/gnu-libstdc++/'
154                                 + ver + '/libs/' + target_arch])
155
156 ######################################################################
157 # Set release/debug flags
158 ######################################################################
159 if env.get('RELEASE'):
160         env.AppendUnique(CCFLAGS = ['-Os'])
161         env.AppendUnique(CPPDEFINES = ['NDEBUG'])
162         env.AppendUnique(LINKFLAGS = ['-s'])
163 else:
164         env.AppendUnique(CCFLAGS = ['-g'])