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