[IOT-2324]warning fix 1
[platform/upstream/iotivity.git] / build_common / SConscript
1 # -*- mode: python; python-indent-offset: 4; indent-tabs-mode: nil -*-
2 ##
3 # This script includes generic build options:
4 #    release/debug, target os, target arch, cross toolchain, build environment etc
5 ##
6 import os
7 import platform
8
9 project_version = '1.3.0'
10
11 # Map of host os and allowed target os (host: allowed target os)
12 host_target_map = {
13     'linux': ['linux', 'android', 'arduino', 'yocto', 'tizen'],
14     'windows': ['windows', 'android', 'arduino'],
15     'darwin': ['darwin', 'ios', 'android', 'arduino'],
16     'msys_nt' :['msys_nt'],
17 }
18
19 # Map of os and allowed archs (os: allowed archs)
20 os_arch_map = {
21     'linux': ['x86', 'x86_64', 'arm', 'arm-v7a', 'armeabi-v7a', 'arm64', 'mips', 'mipsel', 'mips64', 'mips64el', 'i386', 'powerpc', 'sparc', 'aarch64'],
22     'tizen': ['x86', 'x86_64', 'arm', 'arm-v7a', 'armeabi-v7a', 'arm64'],
23     'android': ['x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'armeabi-v7a-hard', 'arm64-v8a'],
24     'windows': ['x86', 'amd64', 'arm'],
25     'msys_nt':['x86', 'x86_64'],
26     'darwin': ['i386', 'x86_64'],
27     'ios': ['i386', 'x86_64', 'armv7', 'armv7s', 'arm64'],
28     'arduino': ['avr', 'arm'],
29     'yocto': ['i586', 'i686', 'x86_64', 'arm', 'aarch64', 'powerpc', 'powerpc64', 'mips', 'mipsel'],
30 }
31
32 host = platform.system().lower()
33
34 # the host string contains version of windows. 6.3, 6.4, 10.0 which is 8.0, 8.1, and 10 respectively.
35 # Let's canonicalize the msys_nt-XX.X system name by stripping version off.
36 if 'msys_nt' in host:
37     host = 'msys_nt'
38
39 if host not in host_target_map:
40     print "\nError: Current system (%s) isn't supported\n" % host
41     Exit(1)
42
43 ######################################################################
44 # Get build options (the optins from command line)
45 ######################################################################
46 target_os = ARGUMENTS.get('TARGET_OS', host).lower() # target os
47
48 if target_os not in host_target_map[host]:
49     print "\nError: Unknown target os: %s (Allow values: %s)\n" % (target_os, host_target_map[host])
50     Exit(1)
51
52 if target_os == 'android':
53     default_arch = 'x86'
54 else:
55     default_arch = platform.machine()
56     if target_os == 'windows':
57         default_arch = default_arch.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', False)
64
65 # Get the device name. This name can be used as network display name wherever possible
66 device_name = ARGUMENTS.get('DEVICE_NAME', "OIC-DEVICE")
67
68 default_with_upstream_libcoap = 0
69
70 if ARGUMENTS.get('TEST'):
71     logging_default = False
72 else:
73     release_mode = False
74     if ARGUMENTS.get('RELEASE', True) in ['y', 'yes', 'true', 't', '1', 'on', 'all', True]:
75         release_mode = True
76     logging_default = (release_mode == False)
77
78 # targets that do not support the DTLS build (SECURED=1 build option)
79 targets_without_dtls_support = ['arduino'];
80 if ARGUMENTS.get('SECURED') == '1' and target_os in targets_without_dtls_support:
81         print "\nError: DTLS not supported on target os: %s MUST build with SECURED=0\n" % (target_os)
82         Exit(1)
83
84 ######################################################################
85 # Common build options (release, target os, target arch)
86 ######################################################################
87 targets_disallow_multitransport = ['arduino']
88
89 help_vars = Variables()
90
91 help_vars.Add('PROJECT_VERSION', 'The version of IoTivity', project_version)
92
93 help_vars.Add(BoolVariable('VERBOSE', 'Show compilation', False))
94 help_vars.Add(BoolVariable('RELEASE', 'Build for release?', True)) # set to 'no', 'false' or 0 for debug
95 help_vars.Add(EnumVariable('TARGET_OS', 'Target platform', host, host_target_map[host]))
96
97
98 help_vars.Add(BoolVariable('WITH_RA', 'Build with Remote Access module', False))
99 help_vars.Add(BoolVariable('WITH_TCP', 'Build with TCP adapter', False))
100 help_vars.Add(BoolVariable('WITH_PROXY', 'Build with CoAP-HTTP Proxy', False))
101 help_vars.Add(ListVariable('WITH_MQ', 'Build with MQ publisher/broker', 'OFF', ['OFF', 'SUB', 'PUB', 'BROKER']))
102 help_vars.Add(BoolVariable('WITH_CLOUD', 'Build including AccountManager class and Cloud Client sample', False))
103 help_vars.Add(ListVariable('RD_MODE', 'Resource Directory build mode', 'CLIENT', ['CLIENT', 'SERVER']))
104
105 help_vars.Add(BoolVariable('SIMULATOR', 'Build with simulator module', False))
106
107 help_vars.Add(BoolVariable('WITH_RA_IBB', 'Build with Remote Access module(workssys)', False))
108
109
110 if target_os in targets_disallow_multitransport:
111     help_vars.Add(ListVariable('TARGET_TRANSPORT', 'Target transport', 'IP', ['BT', 'BLE', 'IP', 'NFC']))
112 else:
113     help_vars.Add(ListVariable('TARGET_TRANSPORT', 'Target transport', 'ALL', ['ALL', 'BT', 'BLE', 'IP', 'NFC']))
114
115 help_vars.Add(EnumVariable('TARGET_ARCH', 'Target architecture', default_arch, os_arch_map[target_os]))
116 if target_os in targets_without_dtls_support:
117     help_vars.Add(EnumVariable('SECURED', 'Build with DTLS', '0', allowed_values=('0', '1')))
118 else:
119     help_vars.Add(EnumVariable('SECURED', 'Build with DTLS', '1', allowed_values=('0', '1')))
120 help_vars.Add(EnumVariable('MULTIPLE_OWNER', 'Enable multiple owner', '0', allowed_values=('0', '1')))
121 help_vars.Add(EnumVariable('EXC_PROV_SUPPORT', 'Except OCPMAPI library(libocpmapi.so)', '0', allowed_values=('0', '1')))
122 help_vars.Add(EnumVariable('TEST', 'Run unit tests', '0', allowed_values=('0', '1')))
123 help_vars.Add(BoolVariable('LOGGING', 'Enable stack logging', logging_default))
124 help_vars.Add(EnumVariable('LOG_LEVEL', 'Enable stack logging level', 'DEBUG', allowed_values=('DEBUG', 'INFO', 'ERROR', 'WARNING', 'FATAL')))
125 help_vars.Add(BoolVariable('UPLOAD', 'Upload binary ? (For Arduino)', require_upload))
126 help_vars.Add(EnumVariable('ROUTING', 'Enable routing', 'EP', allowed_values=('GW', 'EP')))
127 help_vars.Add(EnumVariable('BUILD_SAMPLE', 'Build with sample', 'ON', allowed_values=('ON', 'OFF')))
128 help_vars.AddVariables(('DEVICE_NAME', 'Network display name for device (For Arduino)', device_name, None, None),)
129 help_vars.Add(PathVariable('ANDROID_NDK', 'Android NDK path', None, PathVariable.PathAccept))
130 help_vars.Add(PathVariable('ANDROID_HOME', 'Android SDK path', None, PathVariable.PathAccept))
131 help_vars.Add(PathVariable('ANDROID_GRADLE', 'Gradle binary file', None, PathVariable.PathIsFile))
132 help_vars.Add(EnumVariable('WITH_UPSTREAM_LIBCOAP', 'Use latest stable version of LibCoAP downloaded from github', default_with_upstream_libcoap, allowed_values=('0','1')))
133 help_vars.Add(BoolVariable('WITH_ENV', 'Use compiler options from environment', False))
134 help_vars.Add(BoolVariable('AUTOMATIC_UPDATE', 'Makes libcoap update automatically to the required versions if needed.', False))
135
136 if target_os == 'windows':
137         # For VS2013, MSVC_VERSION is '12.0'. For VS2015, MSVC_VERSION is '14.0'.
138         # Default value is None, meaning that SCons has to choose automatically a VS version.
139         help_vars.Add(EnumVariable('MSVC_VERSION', 'MSVC compiler version - Windows', None, allowed_values=('12.0', '14.0')))
140         help_vars.Add(EnumVariable('UWP_APP', 'Build for a Universal Windows Platform (UWP) Application', '0', allowed_values=('0', '1')))
141
142 help_vars.Add(BoolVariable('BUILD_JAVA', 'Build Java bindings', False))
143 help_vars.Add(PathVariable('JAVA_HOME', 'JDK directory', os.environ.get('JAVA_HOME'), PathVariable.PathAccept))
144 help_vars.Add(EnumVariable('OIC_SUPPORT_TIZEN_TRACE', 'Tizen Trace(T-trace) api availability', 'False', allowed_values=('True', 'False')))
145
146 AddOption('--prefix',
147                   dest='prefix',
148                   type='string',
149                   nargs=1,
150                   action='store',
151                   metavar='DIR',
152                   help='installation prefix')
153
154 ######################################################################
155 # Platform(build target) specific options: SDK/NDK & toolchain
156 ######################################################################
157 targets_support_cc = ['linux', 'arduino', 'tizen']
158
159 if target_os in targets_support_cc:
160     # Set cross compile toolchain
161     help_vars.Add('TC_PREFIX', "Toolchain prefix (Generally only be required for cross-compiling)", os.environ.get('TC_PREFIX'))
162     help_vars.Add(PathVariable('TC_PATH',
163             'Toolchain path (Generally only be required for cross-compiling)',
164             os.environ.get('TC_PATH')))
165
166 if target_os in ['android', 'arduino']: # Android/Arduino always uses GNU compiler regardless of the host
167     env = Environment(variables = help_vars,
168             tools = ['gnulink', 'gcc', 'g++', 'ar', 'as', 'textfile']
169             )
170 else:
171     env = Environment(variables = help_vars, tools = ['default', 'textfile'],
172             TARGET_ARCH = target_arch, TARGET_OS = target_os,
173             PREFIX = GetOption('prefix'),
174             LIB_INSTALL_DIR = ARGUMENTS.get('LIB_INSTALL_DIR') #for 64bit build
175             )
176
177 Help(help_vars.GenerateHelpText(env))
178
179 if env.get('WITH_ENV'):
180     env['ENV'] = os.environ
181     if 'CC' in os.environ:
182         env['CC'] = Split(os.environ['CC'])
183         print "using CC from environment: %s" % env['CC']
184     if 'CXX' in os.environ:
185         env['CXX'] = Split(os.environ['CXX'])
186         print "using CXX from environment: %s" % env['CXX']
187     if 'CFLAGS' in os.environ:
188         env['CFLAGS'] = Split(os.environ['CFLAGS'])
189         print "using CFLAGS from environment: %s" % env['CFLAGS']
190     if 'CXXFLAGS' in os.environ:
191         env['CXXFLAGS'] = Split(os.environ['CXXFLAGS'])
192         print "using CXXFLAGS from environment: %s" % env['CXXFLAGS']
193     if 'CCFLAGS' in os.environ:
194         env['CCFLAGS'] = Split(os.environ['CCFLAGS'])
195         print "using CCFLAGS from environment: %s" % env['CCFLAGS']
196     if 'CPPFLAGS' in os.environ:
197         env['CPPFLAGS'] = Split(os.environ['CPPFLAGS'])
198         print "using CPPFLAGS from environment: %s" % env['CPPFLAGS']
199     if 'LDFLAGS' in os.environ:
200         env['LINKFLAGS'] = Split(os.environ['LDFLAGS'])
201         print "using LDFLAGS/LINKFLAGS from environment: %s" % env['LINKFLAGS']
202
203 tc_set_msg = '''
204 ************************************ Warning **********************************
205 *   Environment variable TC_PREFIX/TC_PATH is set. It will change the default *
206 * toolchain, if it isn't what you expect you should unset it, otherwise it may*
207 * cause inexplicable errors.                                                  *
208 *******************************************************************************
209 '''
210 if env.get('VERBOSE') == False:
211     env['CCCOMSTR'] = "Compiling $TARGET"
212     env['SHCCCOMSTR'] = "Compiling $TARGET"
213     env['CXXCOMSTR'] = "Compiling $TARGET"
214     env['SHCXXCOMSTR'] = "Compiling $TARGET"
215     env['LINKCOMSTR'] = "Linking $TARGET"
216     env['SHLINKCOMSTR'] = "Linking $TARGET"
217     env['ARCOMSTR'] = "Archiving $TARGET"
218     env['RANLIBCOMSTR'] = "Indexing Archive $TARGET"
219
220 if target_os in targets_support_cc:
221     prefix = env.get('TC_PREFIX')
222     tc_path = env.get('TC_PATH')
223     if prefix:
224         env.Replace(CC = prefix + env.get('CC', 'gcc'))
225         env.Replace(CXX = prefix + env.get('CXX', 'g++'))
226         env.Replace(AR = prefix + env.get('AR', 'ar'))
227         env.Replace(AS = prefix + env.get('AS', 'as'))
228         env.Replace(RANLIB = prefix + env.get('RANLIB', 'ranlib'))
229
230     if tc_path:
231         env.PrependENVPath('PATH', tc_path)
232         sys_root = os.path.abspath(tc_path + '/../')
233         env.AppendUnique(CCFLAGS = ['--sysroot=' + sys_root])
234         env.AppendUnique(LINKFLAGS = ['--sysroot=' + sys_root])
235
236     if prefix or tc_path:
237         print tc_set_msg
238
239 # Import env variables only if reproductibility is ensured
240 if target_os in ['yocto']:
241     env['CONFIG_ENVIRONMENT_IMPORT'] = True
242 else:
243     env['CONFIG_ENVIRONMENT_IMPORT'] = False
244
245 if env['CONFIG_ENVIRONMENT_IMPORT'] == True:
246     print "warning: importing some environment variables for OS: %s" % target_os
247     for ev in ['PATH', 'PKG_CONFIG', 'PKG_CONFIG_PATH', 'PKG_CONFIG_SYSROOT_DIR']:
248         if os.environ.get(ev) != None:
249             env['ENV'][ev] = os.environ.get(ev)
250     if os.environ['LDFLAGS'] != None:
251         env.AppendUnique(LINKFLAGS = Split(os.environ['LDFLAGS']))
252
253 # Ensure scons be able to change its working directory
254 env.SConscriptChdir(1)
255
256 # Set the source directory and build directory
257 #   Source directory: 'dir'
258 #   Build directory: 'dir'/out/<target_os>/<target_arch>/<release or debug>/
259 #   On windows, the build directory will be:
260 #     'dir'/out/windows/<win32 or uwp>/<target_arch>/<release or debug>/
261 #
262 # You can get the directory as following:
263 #   env.get('SRC_DIR')
264 #   env.get('BUILD_DIR')
265
266 def __set_dir(env, dir):
267     if not os.path.exists(dir + '/SConstruct'):
268         print '''
269 *************************************** Error *********************************
270 * The directory(%s) seems isn't a source code directory, no SConstruct file is
271 * found. *
272 *******************************************************************************
273 ''' % dir
274         Exit(1)
275
276     build_dir = dir + '/out/' + target_os + '/'
277
278     if target_os == 'windows':
279         if env.get('UWP_APP') == '1':
280             build_dir = build_dir + 'uwp/'
281         else:
282             build_dir = build_dir + 'win32/'
283
284     build_dir = build_dir + target_arch
285
286     if env.get('RELEASE'):
287         build_dir = build_dir + '/release/'
288     else:
289         build_dir = build_dir + '/debug/'
290
291     env.VariantDir(build_dir, dir, duplicate=0)
292
293     env.Replace(BUILD_DIR = build_dir)
294     env.Replace(SRC_DIR = dir)
295
296 def __src_to_obj(env, src, home = ''):
297     obj = env.get('BUILD_DIR') + src.replace(home, '')
298     if env.get('OBJSUFFIX'):
299         obj += env.get('OBJSUFFIX')
300     return env.Object(obj, src)
301
302 def __install(ienv, targets, name):
303     i_n = ienv.Install(env.get('BUILD_DIR'), targets)
304     Alias(name, i_n)
305     env.AppendUnique(TS = [name])
306
307 def __installlib(ienv, targets, name):
308     user_prefix = env.get('PREFIX')
309     if user_prefix:
310         user_lib = env.get('LIB_INSTALL_DIR')
311         if user_lib:
312             i_n = ienv.Install(user_lib, targets)
313         else:
314             i_n = ienv.Install(user_prefix + '/lib', targets)
315         ienv.Alias("install", i_n)
316     else:
317         i_n = ienv.Install(env.get('BUILD_DIR'), targets)
318     ienv.Alias("install", i_n)
319
320 def __installbin(ienv, targets, name):
321     user_prefix = env.get('PREFIX')
322     if user_prefix:
323         i_n = ienv.Install(user_prefix + '/bin', targets)
324         ienv.Alias("install", i_n)
325
326 def __installheader(ienv, targets, dir, name):
327     user_prefix = env.get('PREFIX')
328     if user_prefix:
329         i_n = ienv.Install(user_prefix + '/include/' + dir ,targets)
330     else:
331         i_n = ienv.Install(os.path.join(env.get('BUILD_DIR'), 'include', dir), targets)
332     ienv.Alias("install", i_n)
333
334 def __installpcfile(ienv, targets, name):
335     user_prefix = env.get('PREFIX')
336     if user_prefix:
337         user_lib = env.get('LIB_INSTALL_DIR')
338         if user_lib:
339             i_n = ienv.Install(user_lib + '/pkgconfig', targets)
340         else:
341             i_n = ienv.Install(user_prefix + '/lib/pkgconfig', targets)
342     else:
343         i_n = ienv.Install(env.get('BUILD_DIR') + 'lib/pkgconfig', targets)
344     ienv.Alias("install", i_n)
345
346 def __append_target(ienv, name, targets = None):
347     if targets:
348         env.Alias(name, targets)
349     env.AppendUnique(TS = [name])
350
351 def __print_targets(env):
352     Help('''
353 ===============================================================================
354 Targets:\n    ''')
355     for t in env.get('TS'):
356         Help(t + ' ')
357     Help('''
358 \nDefault all targets will be built. You can specify the target to build:
359
360     $ scons [options] [target]
361 ===============================================================================
362 ''')
363
364 env.AddMethod(__set_dir, 'SetDir')
365 env.AddMethod(__print_targets, 'PrintTargets')
366 env.AddMethod(__src_to_obj, 'SrcToObj')
367 env.AddMethod(__append_target, 'AppendTarget')
368 env.AddMethod(__install, 'InstallTarget')
369 env.AddMethod(__installlib, 'UserInstallTargetLib')
370 env.AddMethod(__installbin, 'UserInstallTargetBin')
371 env.AddMethod(__installheader, 'UserInstallTargetHeader')
372 env.AddMethod(__installpcfile, 'UserInstallTargetPCFile')
373 env.SetDir(env.GetLaunchDir())
374 env['ROOT_DIR']=env.GetLaunchDir()+'/..'
375
376 Export('env')
377
378 ######################################################################
379 # Scons to generate the iotivity.pc file from iotivity.pc.in file
380 ######################################################################
381 pc_file = env.get('SRC_DIR') + '/iotivity.pc.in'
382
383 user_prefix = env.get('PREFIX')
384 user_lib = env.get('LIB_INSTALL_DIR')
385
386 if not user_prefix:
387     user_prefix = env.get('BUILD_DIR').encode('string_escape')
388
389 if not user_lib:
390     user_lib = '$${prefix}/lib'
391
392 defines = []
393 if env.get('LOGGING'):
394     defines.append('-DTB_LOG=1')
395
396 if env.get('ROUTING') == 'GW':
397     defines.append('-DROUTING_GATEWAY=1')
398 elif env.get('ROUTING') == 'EP':
399     defines.append('-DROUTING_EP=1')
400
401 libs = []
402 if env.get('WITH_TCP'):
403     defines.append('-DTCP_ADAPTER=1')
404     if env.get('SECURED') == '1':
405         defines.append('-D__WITH_TLS__=1')
406
407 if env.get('SECURED') == '1':
408     defines.append('-D__WITH_DTLS__=1')
409     if env.get('EXC_PROV_SUPPORT') == '0':
410         libs.append('-locpmapi')
411
412 pc_vars = {
413     '\@VERSION\@': project_version,
414     '\@PREFIX\@': user_prefix,
415     '\@EXEC_PREFIX\@': user_prefix,
416     '\@LIB_INSTALL_DIR\@': user_lib,
417     '\@DEFINES\@': " ".join(defines),
418     '\@LIBS\@': " ".join(libs)
419 }
420
421 env.Substfile(pc_file, SUBST_DICT = pc_vars)
422
423 ######################################################################
424 # Setting global compiler flags
425 ######################################################################
426 target_transport = env.get('TARGET_TRANSPORT')
427 with_mq = env.get('WITH_MQ')
428 with_ra = env.get('WITH_RA')
429 with_tcp = env.get('WITH_TCP')
430 rd_mode = env.get('RD_MODE')
431 with_ra_ibb = env.get('WITH_RA_IBB')
432
433 env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
434
435 if (env.get('WITH_UPSTREAM_LIBCOAP') == '1'):
436     env.AppendUnique(CPPDEFINES = ['WITH_UPSTREAM_LIBCOAP'])
437
438 if (target_os not in ['arduino', 'windows']):
439     env.AppendUnique(CPPDEFINES = ['WITH_POSIX'])
440
441 if (target_os in ['darwin','ios']):
442     env.AppendUnique(CPPDEFINES = ['_DARWIN_C_SOURCE'])
443
444 if (env.get('SECURED') == '1'):
445     env.AppendUnique(CPPDEFINES = ['__WITH_DTLS__'])
446
447 if ((env.get('SECURED') == '1') and with_tcp):
448     env.AppendUnique(CPPDEFINES = ['__WITH_TLS__'])
449
450 if (env.get('MULTIPLE_OWNER') == '1'):
451     env.AppendUnique(CPPDEFINES=['MULTIPLE_OWNER'])
452
453 if (env.get('ROUTING') == 'GW'):
454     env.AppendUnique(CPPDEFINES = ['ROUTING_GATEWAY'])
455 elif (env.get('ROUTING') == 'EP'):
456     env.AppendUnique(CPPDEFINES = ['ROUTING_EP'])
457
458 if (target_os == 'arduino'):
459     env.AppendUnique(CPPDEFINES = ['SINGLE_THREAD'])
460     env.AppendUnique(CPPDEFINES = ['WITH_ARDUINO'])
461 elif (('IP' in target_transport) or ('ALL' in target_transport)):
462         env.AppendUnique(CPPDEFINES = ['WITH_BWT'])
463
464 if (target_os in ['linux', 'tizen', 'android'] and with_tcp):
465     env.AppendUnique(CPPDEFINES = ['WITH_TCP'])
466
467 if (target_os in ['linux', 'tizen', 'android', 'arduino', 'ios']):
468     if (('BLE' in target_transport) or ('BT' in target_transport) or ('ALL' in target_transport)):
469         env.AppendUnique(CPPDEFINES = ['WITH_TCP'])
470
471 if 'ALL' in target_transport:
472     if with_ra:
473         env.AppendUnique(CPPDEFINES = ['RA_ADAPTER'])
474     if with_tcp:
475         env.AppendUnique(CPPDEFINES = ['TCP_ADAPTER'])
476     if (target_os in ['linux', 'yocto']):
477         env.AppendUnique(CPPDEFINES = ['IP_ADAPTER', 'NO_EDR_ADAPTER', 'LE_ADAPTER'])
478     elif (target_os == 'android'):
479         env.AppendUnique(CPPDEFINES = ['IP_ADAPTER', 'EDR_ADAPTER', 'LE_ADAPTER', 'NFC_ADAPTER'])
480     elif (target_os in['darwin', 'ios', 'msys_nt', 'windows']):
481         env.AppendUnique(CPPDEFINES = ['IP_ADAPTER', 'NO_EDR_ADAPTER', 'NO_LE_ADAPTER'])
482     else:
483         env.AppendUnique(CPPDEFINES = ['IP_ADAPTER', 'EDR_ADAPTER', 'LE_ADAPTER'])
484 else:
485     if ('BT' in target_transport):
486         if (target_os == 'linux'):
487             print "CA Transport BT is not supported "
488             Exit(1)
489         else:
490             env.AppendUnique(CPPDEFINES = ['EDR_ADAPTER'])
491     else:
492         env.AppendUnique(CPPDEFINES = ['NO_EDR_ADAPTER'])
493
494     if ('BLE' in target_transport):
495         env.AppendUnique(CPPDEFINES = ['LE_ADAPTER'])
496     else:
497         env.AppendUnique(CPPDEFINES = ['NO_LE_ADAPTER'])
498
499     if ('IP' in target_transport):
500         env.AppendUnique(CPPDEFINES = ['IP_ADAPTER'])
501     else:
502         env.AppendUnique(CPPDEFINES = ['NO_IP_ADAPTER'])
503
504     if with_tcp:
505         if (target_os in ['linux', 'tizen', 'android', 'arduino', 'ios', 'windows']):
506             env.AppendUnique(CPPDEFINES = ['TCP_ADAPTER', 'WITH_TCP'])
507         else:
508             print "CA Transport TCP is not supported "
509             Exit(1)
510     else:
511         env.AppendUnique(CPPDEFINES = ['NO_TCP_ADAPTER'])
512
513     if ('NFC' in target_transport):
514         if (target_os == 'android'):
515             env.AppendUnique(CPPDEFINES = ['NFC_ADAPTER'])
516         else:
517             print "CA Transport NFC is not supported "
518             Exit(1)
519     else:
520         env.AppendUnique(CPPDEFINES = ['NO_NFC_ADAPTER'])
521
522 if ('SUB' in with_mq):
523     env.AppendUnique(CPPDEFINES = ['MQ_SUBSCRIBER', 'WITH_MQ'])
524
525 if ('PUB' in with_mq):
526     env.AppendUnique(CPPDEFINES = ['MQ_PUBLISHER', 'WITH_MQ'])
527
528 if ('BROKER' in with_mq):
529     env.AppendUnique(CPPDEFINES = ['MQ_BROKER', 'WITH_MQ'])
530
531 if env.get('LOGGING'):
532     env.AppendUnique(CPPDEFINES = ['TB_LOG'])
533
534 if env.get('WITH_CLOUD') and with_tcp:
535     env.AppendUnique(CPPDEFINES = ['WITH_CLOUD'])
536
537 if 'CLIENT' in rd_mode:
538     env.AppendUnique(CPPDEFINES = ['RD_CLIENT'])
539
540 if 'SERVER' in rd_mode:
541     env.AppendUnique(CPPDEFINES = ['RD_SERVER'])
542
543 if with_ra_ibb:
544     env.AppendUnique(CPPDEFINES = ['RA_ADAPTER_IBB'])
545
546 env.SConscript('external_builders.scons')
547
548 ######################################################################
549 # Link scons to Yocto cross-toolchain ONLY when target_os is yocto
550 ######################################################################
551 if target_os == "yocto":
552     '''
553     This code injects Yocto cross-compilation tools+flags into scons'
554     build environment in order to invoke the relevant tools while
555     performing a build.
556     '''
557     import os.path
558     try:
559         CC = os.environ['CC']
560         target_prefix = CC.split()[0]
561         target_prefix = target_prefix[:len(target_prefix)-3]
562         tools = {"CC" : target_prefix+"gcc",
563                 "CXX" : target_prefix+"g++",
564                 "AS" : target_prefix+"as",
565                 "LD" : target_prefix+"ld",
566                 "GDB" : target_prefix+"gdb",
567                 "STRIP" : target_prefix+"strip",
568                 "RANLIB" : target_prefix+"ranlib",
569                 "OBJCOPY" : target_prefix+"objcopy",
570                 "OBJDUMP" : target_prefix+"objdump",
571                 "AR" : target_prefix+"ar",
572                 "NM" : target_prefix+"nm",
573                 "M4" : "m4",
574                 "STRINGS": target_prefix+"strings"}
575         PATH = os.environ['PATH'].split(os.pathsep)
576         for tool in tools:
577             if tool in os.environ:
578                 for path in PATH:
579                     if os.path.isfile(os.path.join(path, tools[tool])):
580                         env[tool] = os.path.join(path, os.environ[tool])
581                         break
582         env['CROSS_COMPILE'] = target_prefix[:len(target_prefix) - 1]
583     except:
584         print "ERROR in Yocto cross-toolchain environment"
585         Exit(1)
586     '''
587     Now reset TARGET_OS to linux so that all linux specific build configurations
588     hereupon apply for the entirety of the build process.
589     '''
590     env['TARGET_OS'] = 'linux'
591     '''
592     We want to preserve debug symbols to allow BitBake to generate both DEBUG and
593     RELEASE packages for OIC.
594     '''
595     env.AppendUnique(CCFLAGS = ['-g'])
596     '''
597     Additional flags to pass to the Yocto toolchain.
598     '''
599     if env.get('RELEASE'):
600         env.AppendUnique(CPPDEFINES = ['NDEBUG'])
601     env.AppendUnique(CPPDEFINES = ['__linux__', '_GNU_SOURCE'])
602     env.AppendUnique(CFLAGS = ['-std=gnu99'])
603     env.AppendUnique(CCFLAGS = ['-Wall', '-Wextra', '-fPIC'])
604     env.AppendUnique(LIBS = ['dl', 'pthread', 'uuid'])
605     Export('env')
606 else:
607     '''
608     If target_os is not Yocto, continue with the regular build process
609     '''
610     # Load config of target os
611     env.SConscript(target_os + '/SConscript')
612
613 # Delete the temp files of configuration
614 if env.GetOption('clean'):
615     dir = env.get('SRC_DIR')
616
617     if os.path.exists(dir + '/config.log'):
618         Execute(Delete(dir + '/config.log'))
619     if os.path.exists(dir + '/.sconsign.dblite'):
620         Execute(Delete(dir + '/.sconsign.dblite'))
621     if os.path.exists(dir + '/.sconf_temp'):
622         Execute(Delete(dir + '/.sconf_temp'))
623
624 ######################################################################
625 # Check for PThreads support
626 ######################################################################
627 import iotivityconfig
628 from iotivityconfig import *
629
630 conf = Configure(env,
631         custom_tests =
632         {
633             'CheckPThreadsSupport' : iotivityconfig.check_pthreads
634         } )
635
636 # Identify whether we have pthreads support, which is necessary for
637 # threading and mutexes.  This will set the environment variable
638 # POSIX_SUPPORTED, 1 if it is supported, 0 otherwise
639 conf.CheckPThreadsSupport()
640
641 env = conf.Finish()
642 ######################################################################
643
644 # must call external_builders.scons second time to properly setup
645 # UnpackAll builder this is due to the system path not being pulled
646 # in for windows till after the windows target has run.
647 env.SConscript('external_builders.scons')
648 env.SConscript('external_libs.scons')
649 Return('env')