e9333a73cfdf01542867980079639c82dbd9d355
[platform/upstream/iotivity.git] / service / easy-setup / sampleapp / enrollee / tizen-sdb / EnrolleeSample / build / SConscript
1 ##
2 # This script includes generic build options:
3 #    release/debug, target os, target arch, cross toolchain, build environment etc
4 ##
5 import os
6 import platform
7
8 # Map of host os and allowed target os (host: allowed target os)
9 host_target_map = {
10                 'linux': ['linux', 'android', 'arduino', 'yocto', 'tizen'],
11                 'windows': ['windows', 'android', 'arduino'],
12                 'darwin': ['darwin', 'ios', 'android', 'arduino'],
13                 }
14
15 # Map of os and allowed archs (os: allowed archs)
16 os_arch_map = {
17                 'linux': ['x86', 'x86_64', 'arm', 'arm64'],
18                 'tizen': ['x86', 'x86_64', 'arm', 'arm64'],
19                 'android': ['x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'armeabi-v7a-hard', 'arm64-v8a'],
20                 'windows': ['x86', 'amd64', 'arm'],
21                 'darwin': ['i386', 'x86_64'],
22                 'ios': ['i386', 'x86_64', 'armv7', 'armv7s', 'arm64'],
23                 'arduino': ['avr', 'arm'],
24                 'yocto': ['i586', 'x86_64', 'arm', 'powerpc', 'powerpc64', 'mips', 'mipsel'],
25                 }
26
27 es_role_map = {
28                 'enrollee', 'mediator'
29                 }
30
31 es_target_enrollee_map = {
32                 'arduino', 'tizen', 'linux'
33                 }
34
35 es_softap_mode_map = {
36                 'ENROLLEE_SOFTAP', 'MEDIATOR_SOFTAP'
37                 }
38
39 host = platform.system().lower()
40
41 if not host_target_map.has_key(host):
42         print "\nError: Current system (%s) isn't supported\n" % host
43         Exit(1)
44
45 ######################################################################
46 # Get build options (the optins from command line)
47 ######################################################################
48 target_os = ARGUMENTS.get('TARGET_OS', host).lower() # target os
49
50 if target_os not in host_target_map[host]:
51         print "\nError: Unknown target os: %s (Allow values: %s)\n" % (target_os, host_target_map[host])
52         Exit(1)
53
54 default_arch = platform.machine()
55 if default_arch not in os_arch_map[target_os]:
56         default_arch = os_arch_map[target_os][0].lower()
57
58 target_arch = ARGUMENTS.get('TARGET_ARCH', default_arch) # target arch
59
60 # True if binary needs to be installed on board. (Might need root permissions)
61 # set to 'no', 'false' or 0 for only compilation
62 require_upload = ARGUMENTS.get('UPLOAD', True)
63
64 # Get the device name
65 device_name = ARGUMENTS.get('DEVICE_NAME', "OIC-DEVICE")
66
67 # Get es_role
68 es_role = ARGUMENTS.get('ES_ROLE')
69
70 if es_role not in es_role_map:
71         print "\nError: Unknown ES_ROLE: %s (Allow values: %s)\n" % (es_role, es_role_map)
72         Exit(1)
73
74 # Get es_target_enrollee
75 es_target_enrollee = ARGUMENTS.get('ES_TARGET_ENROLLEE')
76
77 if es_target_enrollee not in es_target_enrollee_map:
78         print "\nError: Unknown ES_TARGET_ENROLLEE: %s (Allow values: %s)\n" % (es_target_enrollee, es_target_enrollee_map)
79         Exit(1)
80
81 # Get es_soft_mode
82 es_soft_mode = ARGUMENTS.get('ES_SOFTAP_MODE')
83
84 if es_soft_mode not in es_softap_mode_map:
85         print "\nError: Unknown ES_SOFTAP_MODE: %s (Allow values: %s)\n" % (es_soft_mode, es_softap_mode_map)
86         Exit(1)
87
88 ######################################################################
89 # Common build options (release, target os, target arch)
90 ######################################################################
91 help_vars = Variables()
92 help_vars.Add(BoolVariable('RELEASE', 'Build for release?', True)) # set to 'no', 'false' or 0 for debug
93 help_vars.Add(BoolVariable('LOGGING', 'Enable stack logging', False))
94 help_vars.Add(EnumVariable('TARGET_OS', 'Target platform', host, host_target_map[host]))
95 help_vars.Add(ListVariable('TARGET_TRANSPORT', 'Target transport', 'ALL', ['ALL', 'BT', 'BLE', 'IP', 'TCP']))
96 help_vars.Add(EnumVariable('TARGET_ARCH', 'Target architecture', default_arch, os_arch_map[target_os]))
97 help_vars.Add(EnumVariable('SECURED', 'Build with DTLS', '0', allowed_values=('0', '1')))
98 help_vars.Add(BoolVariable('UPLOAD', 'Upload binary ? (For Arduino)', require_upload))
99 help_vars.Add(EnumVariable('ROUTING', 'Enable routing', 'EP', allowed_values=('GW', 'EP')))
100 help_vars.Add(EnumVariable('BUILD_SAMPLE', 'Build with sample', 'ON', allowed_values=('ON', 'OFF')))
101
102 help_vars.AddVariables(('DEVICE_NAME', 'Network display name for device', 'OIC-DEVICE', None, None),)
103
104 #ES_TARGET_ENROLLEE is for specifying what is our target enrollee (Arduino or rest of platforms which support Multicast)
105 help_vars.Add(EnumVariable('ES_TARGET_ENROLLEE', 'Target Enrollee', 'arduino', allowed_values=('arduino', 'tizen', 'linux')))
106 #ES_ROLE is for specifying the role (Enrollee or Mediator) for which scons is being executed
107 help_vars.Add(EnumVariable('ES_ROLE', 'Target build mode', 'mediator', allowed_values=('mediator', 'enrollee')))
108 #ES_SOFT_MODE is for specifying MODE (Mode 1 : Enrollee with  Soft AP or Mode 2  : Mediator with Soft AP)
109 help_vars.Add(EnumVariable('ES_SOFTAP_MODE', 'Target build mode', 'ENROLLEE_SOFTAP', allowed_values=('ENROLLEE_SOFTAP', 'MEDIATOR_SOFTAP')))
110
111 AddOption('--prefix',
112                   dest='prefix',
113                   type='string',
114                   nargs=1,
115                   action='store',
116                   metavar='DIR',
117                   help='installation prefix')
118
119 ######################################################################
120 # Platform(build target) specific options: SDK/NDK & toolchain
121 ######################################################################
122 targets_support_cc = ['linux', 'arduino', 'tizen']
123
124 if target_os in targets_support_cc:
125         # Set cross compile toolchain
126         help_vars.Add('TC_PREFIX', "Toolchain prefix (Generally only be required for cross-compiling)", os.environ.get('TC_PREFIX'))
127         help_vars.Add(PathVariable('TC_PATH',
128                         'Toolchain path (Generally only be required for cross-compiling)',
129                         os.environ.get('TC_PATH')))
130
131 if target_os in ['android', 'arduino']: # Android/Arduino always uses GNU compiler regardless of the host
132         env = Environment(variables = help_vars,
133                         tools = ['gnulink', 'gcc', 'g++', 'ar', 'as']
134                         )
135 else:
136         env = Environment(variables = help_vars, TARGET_ARCH = target_arch, TARGET_OS = target_os, ES_ROLE = es_role, ES_TARGET_ENROLLEE = es_target_enrollee, ES_SOFTAP_MODE = es_soft_mode, ESPREFIX = GetOption('prefix'))
137
138 Help(help_vars.GenerateHelpText(env))
139
140 # Set device name to __OIC_DEVICE_NAME__
141 env.AppendUnique(CPPDEFINES = ['-D__OIC_DEVICE_NAME__=' + "\'\"" + device_name + "\"\'"])
142
143 tc_set_msg = '''
144 ************************************ Warning **********************************
145 *   Enviornment variable TC_PREFIX/TC_PATH is set. It will change the default *
146 * toolchain, if it isn't what you expect you should unset it, otherwise it may*
147 * cause inexplicable errors.                                                  *
148 *******************************************************************************
149 '''
150
151 if target_os in targets_support_cc:
152         prefix = env.get('TC_PREFIX')
153         tc_path = env.get('TC_PATH')
154         if prefix:
155                 env.Replace(CC = prefix + 'gcc')
156                 env.Replace(CXX = prefix + 'g++')
157                 env.Replace(AR = prefix + 'ar')
158                 env.Replace(AS = prefix + 'as')
159                 env.Replace(LINK = prefix + 'ld')
160                 env.Replace(RANLIB = prefix + 'ranlib')
161
162         if tc_path:
163                 env.PrependENVPath('PATH', tc_path)
164                 sys_root = os.path.abspath(tc_path + '/../')
165                 env.AppendUnique(CCFLAGS = ['--sysroot=' + sys_root])
166                 env.AppendUnique(LINKFLAGS = ['--sysroot=' + sys_root])
167
168         if prefix or tc_path:
169                 print tc_set_msg
170
171 # Ensure scons be able to change its working directory
172 env.SConscriptChdir(1)
173
174 # Set the source directory and build directory
175 #   Source directory: 'dir'
176 #   Build directory: 'dir'/out/<target_os>/<target_arch>/<release or debug>/
177 #
178 # You can get the directory as following:
179 #   env.get('SRC_DIR')
180 #   env.get('BUILD_DIR')
181
182 def __set_dir(env, dir):
183         if not os.path.exists(dir + '/SConstruct'):
184                 print '''
185 *************************************** Error *********************************
186 * The directory(%s) seems isn't a source code directory, no SConstruct file is
187 * found. *
188 *******************************************************************************
189 ''' % dir
190                 Exit(1)
191
192         if env.get('RELEASE'):
193                 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/release/'
194         else:
195                 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/debug/'
196         env.VariantDir(build_dir, dir, duplicate=0)
197
198         env.Replace(BUILD_DIR = build_dir)
199         env.Replace(SRC_DIR = dir)
200
201 def __src_to_obj(env, src, home = ''):
202         obj = env.get('BUILD_DIR') + src.replace(home, '')
203         if env.get('OBJSUFFIX'):
204                 obj += env.get('OBJSUFFIX')
205         return env.Object(obj, src)
206
207 def __install(ienv, targets, name):
208         i_n = ienv.Install(env.get('BUILD_DIR'), targets)
209         Alias(name, i_n)
210         env.AppendUnique(TS = [name])
211
212 def __installlib(ienv, targets, name):
213         user_prefix = env.get('PREFIX')
214         if user_prefix:
215                 i_n = ienv.Install(user_prefix + '/lib', targets)
216         else:
217                 i_n = ienv.Install(env.get('BUILD_DIR'), targets)
218         ienv.Alias("install", i_n)
219
220 def __installbin(ienv, targets, name):
221         user_prefix = env.get('PREFIX')
222         if user_prefix:
223                 i_n = ienv.Install(user_prefix + '/bin', targets)
224         else:
225                 i_n = ienv.Install(env.get('BUILD_DIR'), targets)
226         ienv.Alias("install", i_n)
227
228 def __append_target(ienv, target):
229         env.AppendUnique(TS = [target])
230
231 def __print_targets(env):
232         Help('''
233 ===============================================================================
234 Targets:\n    ''')
235         for t in env.get('TS'):
236                 Help(t + ' ')
237         Help('''
238 \nDefault all targets will be built. You can specify the target to build:
239
240     $ scons [options] [target]
241 ===============================================================================
242 ''')
243
244 env.AddMethod(__set_dir, 'SetDir')
245 env.AddMethod(__print_targets, 'PrintTargets')
246 env.AddMethod(__src_to_obj, 'SrcToObj')
247 env.AddMethod(__append_target, 'AppendTarget')
248 env.AddMethod(__install, 'InstallTarget')
249 env.AddMethod(__installlib, 'UserInstallTargetLib')
250 env.AddMethod(__installbin, 'UserInstallTargetBin')
251 env.SetDir(env.GetLaunchDir())
252 env['ROOT_DIR']=env.GetLaunchDir()+'/..'
253
254 Export('env')
255
256 # Delete the temp files of configuration
257 if env.GetOption('clean'):
258         dir = env.get('SRC_DIR')
259
260         if os.path.exists(dir + '/config.log'):
261                 Execute(Delete(dir + '/config.log'))
262                 Execute(Delete(dir + '/.sconsign.dblite'))
263                 Execute(Delete(dir + '/.sconf_temp'))
264
265 Return('env')