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