Merge "[IOT-1785] Merge branch 'iot-1785'"
[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 not host_target_map.has_key(host):
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
135 if target_os == 'windows':
136         # For VS2013, MSVC_VERSION is '12.0'. For VS2015, MSVC_VERSION is '14.0'.
137         # Default value is None, meaning that SCons has to choose automatically a VS version.
138         help_vars.Add(EnumVariable('MSVC_VERSION', 'MSVC compiler version - Windows', None, allowed_values=('12.0', '14.0')))
139
140 help_vars.Add(BoolVariable('BUILD_JAVA', 'Build Java bindings', False))
141 help_vars.Add(PathVariable('JAVA_HOME', 'JDK directory', os.environ.get('JAVA_HOME'), PathVariable.PathAccept))
142 help_vars.Add(EnumVariable('OIC_SUPPORT_TIZEN_TRACE', 'Tizen Trace(T-trace) api availability', 'False', allowed_values=('True', 'False')))
143
144 AddOption('--prefix',
145                   dest='prefix',
146                   type='string',
147                   nargs=1,
148                   action='store',
149                   metavar='DIR',
150                   help='installation prefix')
151
152 ######################################################################
153 # Platform(build target) specific options: SDK/NDK & toolchain
154 ######################################################################
155 targets_support_cc = ['linux', 'arduino', 'tizen']
156
157 if target_os in targets_support_cc:
158     # Set cross compile toolchain
159     help_vars.Add('TC_PREFIX', "Toolchain prefix (Generally only be required for cross-compiling)", os.environ.get('TC_PREFIX'))
160     help_vars.Add(PathVariable('TC_PATH',
161             'Toolchain path (Generally only be required for cross-compiling)',
162             os.environ.get('TC_PATH')))
163
164 if target_os in ['android', 'arduino']: # Android/Arduino always uses GNU compiler regardless of the host
165     env = Environment(variables = help_vars,
166             tools = ['gnulink', 'gcc', 'g++', 'ar', 'as', 'textfile']
167             )
168 else:
169     env = Environment(variables = help_vars, tools = ['default', 'textfile'],
170             TARGET_ARCH = target_arch, TARGET_OS = target_os,
171             PREFIX = GetOption('prefix'),
172             LIB_INSTALL_DIR = ARGUMENTS.get('LIB_INSTALL_DIR') #for 64bit build
173             )
174 Help(help_vars.GenerateHelpText(env))
175
176 if env.get('WITH_ENV'):
177     env['ENV'] = os.environ
178     if 'CC' in os.environ:
179         env['CC'] = Split(os.environ['CC'])
180         print "using CC from environment: %s" % env['CC']
181     if 'CXX' in os.environ:
182         env['CXX'] = Split(os.environ['CXX'])
183         print "using CXX from environment: %s" % env['CXX']
184     if 'CFLAGS' in os.environ:
185         env['CFLAGS'] = Split(os.environ['CFLAGS'])
186         print "using CFLAGS from environment: %s" % env['CFLAGS']
187     if 'CXXFLAGS' in os.environ:
188         env['CXXFLAGS'] = Split(os.environ['CXXFLAGS'])
189         print "using CXXFLAGS from environment: %s" % env['CXXFLAGS']
190     if 'CCFLAGS' in os.environ:
191         env['CCFLAGS'] = Split(os.environ['CCFLAGS'])
192         print "using CCFLAGS from environment: %s" % env['CCFLAGS']
193     if 'CPPFLAGS' in os.environ:
194         env['CPPFLAGS'] = Split(os.environ['CPPFLAGS'])
195         print "using CPPFLAGS from environment: %s" % env['CPPFLAGS']
196     if 'LDFLAGS' in os.environ:
197         env['LINKFLAGS'] = Split(os.environ['LDFLAGS'])
198         print "using LDFLAGS/LINKFLAGS from environment: %s" % env['LINKFLAGS']
199
200 tc_set_msg = '''
201 ************************************ Warning **********************************
202 *   Environment variable TC_PREFIX/TC_PATH is set. It will change the default *
203 * toolchain, if it isn't what you expect you should unset it, otherwise it may*
204 * cause inexplicable errors.                                                  *
205 *******************************************************************************
206 '''
207 if env.get('VERBOSE') == False:
208     env['CCCOMSTR'] = "Compiling $TARGET"
209     env['SHCCCOMSTR'] = "Compiling $TARGET"
210     env['CXXCOMSTR'] = "Compiling $TARGET"
211     env['SHCXXCOMSTR'] = "Compiling $TARGET"
212     env['LINKCOMSTR'] = "Linking $TARGET"
213     env['SHLINKCOMSTR'] = "Linking $TARGET"
214     env['ARCOMSTR'] = "Archiving $TARGET"
215     env['RANLIBCOMSTR'] = "Indexing Archive $TARGET"
216
217 if target_os in targets_support_cc:
218     prefix = env.get('TC_PREFIX')
219     tc_path = env.get('TC_PATH')
220     if prefix:
221         env.Replace(CC = prefix + env.get('CC', 'gcc'))
222         env.Replace(CXX = prefix + env.get('CXX', 'g++'))
223         env.Replace(AR = prefix + env.get('AR', 'ar'))
224         env.Replace(AS = prefix + env.get('AS', 'as'))
225         env.Replace(RANLIB = prefix + env.get('RANLIB', 'ranlib'))
226
227     if tc_path:
228         env.PrependENVPath('PATH', tc_path)
229         sys_root = os.path.abspath(tc_path + '/../')
230         env.AppendUnique(CCFLAGS = ['--sysroot=' + sys_root])
231         env.AppendUnique(LINKFLAGS = ['--sysroot=' + sys_root])
232
233     if prefix or tc_path:
234         print tc_set_msg
235
236 # Import env variables only if reproductibility is ensured
237 if target_os in ['yocto']:
238     env['CONFIG_ENVIRONMENT_IMPORT'] = True
239 else:
240     env['CONFIG_ENVIRONMENT_IMPORT'] = False
241
242 if env['CONFIG_ENVIRONMENT_IMPORT'] == True:
243     print "warning: importing some environment variables for OS: %s" % target_os
244     for ev in ['PATH', 'PKG_CONFIG', 'PKG_CONFIG_PATH', 'PKG_CONFIG_SYSROOT_DIR']:
245         if os.environ.get(ev) != None:
246             env['ENV'][ev] = os.environ.get(ev)
247     if os.environ['LDFLAGS'] != None:
248         env.AppendUnique(LINKFLAGS = Split(os.environ['LDFLAGS']))
249
250 # Ensure scons be able to change its working directory
251 env.SConscriptChdir(1)
252
253 # Set the source directory and build directory
254 #   Source directory: 'dir'
255 #   Build directory: 'dir'/out/<target_os>/<target_arch>/<release or debug>/
256 #
257 # You can get the directory as following:
258 #   env.get('SRC_DIR')
259 #   env.get('BUILD_DIR')
260
261 def __set_dir(env, dir):
262     if not os.path.exists(dir + '/SConstruct'):
263         print '''
264 *************************************** Error *********************************
265 * The directory(%s) seems isn't a source code directory, no SConstruct file is
266 * found. *
267 *******************************************************************************
268 ''' % dir
269         Exit(1)
270
271     if env.get('RELEASE'):
272         build_dir = dir + '/out/' + target_os + '/' + target_arch + '/release/'
273     else:
274         build_dir = dir + '/out/' + target_os + '/' + target_arch + '/debug/'
275     env.VariantDir(build_dir, dir, duplicate=0)
276
277     env.Replace(BUILD_DIR = build_dir)
278     env.Replace(SRC_DIR = dir)
279
280 def __src_to_obj(env, src, home = ''):
281     obj = env.get('BUILD_DIR') + src.replace(home, '')
282     if env.get('OBJSUFFIX'):
283         obj += env.get('OBJSUFFIX')
284     return env.Object(obj, src)
285
286 def __install(ienv, targets, name):
287     i_n = ienv.Install(env.get('BUILD_DIR'), targets)
288     Alias(name, i_n)
289     env.AppendUnique(TS = [name])
290
291 def __installlib(ienv, targets, name):
292     user_prefix = env.get('PREFIX')
293     if user_prefix:
294         user_lib = env.get('LIB_INSTALL_DIR')
295         if user_lib:
296             i_n = ienv.Install(user_lib, targets)
297         else:
298             i_n = ienv.Install(user_prefix + '/lib', targets)
299         ienv.Alias("install", i_n)
300     else:
301         i_n = ienv.Install(env.get('BUILD_DIR'), targets)
302     ienv.Alias("install", i_n)
303
304 def __installbin(ienv, targets, name):
305     user_prefix = env.get('PREFIX')
306     if user_prefix:
307         i_n = ienv.Install(user_prefix + '/bin', targets)
308         ienv.Alias("install", i_n)
309
310 def __installheader(ienv, targets, dir, name):
311     user_prefix = env.get('PREFIX')
312     if user_prefix:
313         i_n = ienv.Install(user_prefix + '/include/' + dir ,targets)
314     else:
315         i_n = ienv.Install(os.path.join(env.get('BUILD_DIR'), 'include', dir), targets)
316     ienv.Alias("install", i_n)
317
318 def __installpcfile(ienv, targets, name):
319     user_prefix = env.get('PREFIX')
320     if user_prefix:
321         user_lib = env.get('LIB_INSTALL_DIR')
322         if user_lib:
323             i_n = ienv.Install(user_lib + '/pkgconfig', targets)
324         else:
325             i_n = ienv.Install(user_prefix + '/lib/pkgconfig', targets)
326     else:
327         i_n = ienv.Install(env.get('BUILD_DIR') + 'lib/pkgconfig', targets)
328     ienv.Alias("install", i_n)
329
330 def __append_target(ienv, name, targets = None):
331     if targets:
332         env.Alias(name, targets)
333     env.AppendUnique(TS = [name])
334
335 def __print_targets(env):
336     Help('''
337 ===============================================================================
338 Targets:\n    ''')
339     for t in env.get('TS'):
340         Help(t + ' ')
341     Help('''
342 \nDefault all targets will be built. You can specify the target to build:
343
344     $ scons [options] [target]
345 ===============================================================================
346 ''')
347
348 env.AddMethod(__set_dir, 'SetDir')
349 env.AddMethod(__print_targets, 'PrintTargets')
350 env.AddMethod(__src_to_obj, 'SrcToObj')
351 env.AddMethod(__append_target, 'AppendTarget')
352 env.AddMethod(__install, 'InstallTarget')
353 env.AddMethod(__installlib, 'UserInstallTargetLib')
354 env.AddMethod(__installbin, 'UserInstallTargetBin')
355 env.AddMethod(__installheader, 'UserInstallTargetHeader')
356 env.AddMethod(__installpcfile, 'UserInstallTargetPCFile')
357 env.SetDir(env.GetLaunchDir())
358 env['ROOT_DIR']=env.GetLaunchDir()+'/..'
359
360 Export('env')
361
362 ######################################################################
363 # Scons to generate the iotivity.pc file from iotivity.pc.in file
364 ######################################################################
365 pc_file = env.get('SRC_DIR') + '/iotivity.pc.in'
366
367 user_prefix = env.get('PREFIX')
368 user_lib = env.get('LIB_INSTALL_DIR')
369
370 if not user_prefix:
371     user_prefix = env.get('BUILD_DIR').encode('string_escape')
372
373 if not user_lib:
374     user_lib = '$${prefix}/lib'
375
376 defines = []
377 if env.get('LOGGING'):
378     defines.append('-DTB_LOG=1')
379
380 if env.get('ROUTING') == 'GW':
381     defines.append('-DROUTING_GATEWAY=1')
382 elif env.get('ROUTING') == 'EP':
383     defines.append('-DROUTING_EP=1')
384
385 libs = []
386 if env.get('WITH_TCP'):
387     defines.append('-DTCP_ADAPTER=1')
388     if env.get('SECURED') == '1':
389         defines.append('-D__WITH_TLS__=1')
390
391 if env.get('SECURED') == '1':
392     defines.append('-D__WITH_DTLS__=1')
393     if env.get('EXC_PROV_SUPPORT') == '0':
394         libs.append('-locpmapi')
395
396 pc_vars = {
397     '\@VERSION\@': project_version,
398     '\@PREFIX\@': user_prefix,
399     '\@EXEC_PREFIX\@': user_prefix,
400     '\@LIB_INSTALL_DIR\@': user_lib,
401     '\@DEFINES\@': " ".join(defines),
402     '\@LIBS\@': " ".join(libs)
403 }
404
405 env.Substfile(pc_file, SUBST_DICT = pc_vars)
406
407 ######################################################################
408 # Setting global compiler flags
409 ######################################################################
410 target_transport = env.get('TARGET_TRANSPORT')
411 with_mq = env.get('WITH_MQ')
412 with_ra = env.get('WITH_RA')
413 with_tcp = env.get('WITH_TCP')
414 rd_mode = env.get('RD_MODE')
415 with_ra_ibb = env.get('WITH_RA_IBB')
416
417 env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
418
419 if (env.get('WITH_UPSTREAM_LIBCOAP') == '1'):
420     env.AppendUnique(CPPDEFINES = ['WITH_UPSTREAM_LIBCOAP'])
421
422 if (target_os not in ['arduino', 'windows']):
423     env.AppendUnique(CPPDEFINES = ['WITH_POSIX'])
424
425 if (target_os in ['darwin','ios']):
426     env.AppendUnique(CPPDEFINES = ['_DARWIN_C_SOURCE'])
427
428 if (env.get('SECURED') == '1'):
429     env.AppendUnique(CPPDEFINES = ['__WITH_DTLS__'])
430
431 if ((env.get('SECURED') == '1') and with_tcp):
432     env.AppendUnique(CPPDEFINES = ['__WITH_TLS__'])
433
434 if (env.get('MULTIPLE_OWNER') == '1'):
435     env.AppendUnique(CPPDEFINES=['MULTIPLE_OWNER'])
436
437 if (env.get('ROUTING') == 'GW'):
438     env.AppendUnique(CPPDEFINES = ['ROUTING_GATEWAY'])
439 elif (env.get('ROUTING') == 'EP'):
440     env.AppendUnique(CPPDEFINES = ['ROUTING_EP'])
441
442 if (target_os == 'arduino'):
443     env.AppendUnique(CPPDEFINES = ['SINGLE_THREAD'])
444     env.AppendUnique(CPPDEFINES = ['WITH_ARDUINO'])
445 elif (('IP' in target_transport) or ('ALL' in target_transport)):
446         env.AppendUnique(CPPDEFINES = ['WITH_BWT'])
447
448 if (target_os in ['linux', 'tizen', 'android'] and with_tcp):
449     env.AppendUnique(CPPDEFINES = ['WITH_TCP'])
450
451 if (target_os in ['linux', 'tizen', 'android', 'arduino', 'ios']):
452     if (('BLE' in target_transport) or ('BT' in target_transport) or ('ALL' in target_transport)):
453         env.AppendUnique(CPPDEFINES = ['WITH_TCP'])
454
455 if 'ALL' in target_transport:
456     if with_ra:
457         env.AppendUnique(CPPDEFINES = ['RA_ADAPTER'])
458     if with_tcp:
459         env.AppendUnique(CPPDEFINES = ['TCP_ADAPTER'])
460     if (target_os in ['linux', 'yocto']):
461         env.AppendUnique(CPPDEFINES = ['IP_ADAPTER', 'NO_EDR_ADAPTER', 'LE_ADAPTER'])
462     elif (target_os == 'android'):
463         env.AppendUnique(CPPDEFINES = ['IP_ADAPTER', 'EDR_ADAPTER', 'LE_ADAPTER', 'NFC_ADAPTER'])
464     elif (target_os in['darwin', 'ios', 'msys_nt', 'windows']):
465         env.AppendUnique(CPPDEFINES = ['IP_ADAPTER', 'NO_EDR_ADAPTER', 'NO_LE_ADAPTER'])
466     else:
467         env.AppendUnique(CPPDEFINES = ['IP_ADAPTER', 'EDR_ADAPTER', 'LE_ADAPTER'])
468 else:
469     if ('BT' in target_transport):
470         if (target_os == 'linux'):
471             print "CA Transport BT is not supported "
472             Exit(1)
473         else:
474             env.AppendUnique(CPPDEFINES = ['EDR_ADAPTER'])
475     else:
476         env.AppendUnique(CPPDEFINES = ['NO_EDR_ADAPTER'])
477
478     if ('BLE' in target_transport):
479         env.AppendUnique(CPPDEFINES = ['LE_ADAPTER'])
480     else:
481         env.AppendUnique(CPPDEFINES = ['NO_LE_ADAPTER'])
482
483     if ('IP' in target_transport):
484         env.AppendUnique(CPPDEFINES = ['IP_ADAPTER'])
485     else:
486         env.AppendUnique(CPPDEFINES = ['NO_IP_ADAPTER'])
487
488     if with_tcp:
489         if (target_os in ['linux', 'tizen', 'android', 'arduino', 'ios', 'windows']):
490             env.AppendUnique(CPPDEFINES = ['TCP_ADAPTER', 'WITH_TCP'])
491         else:
492             print "CA Transport TCP is not supported "
493             Exit(1)
494     else:
495         env.AppendUnique(CPPDEFINES = ['NO_TCP_ADAPTER'])
496
497     if ('NFC' in target_transport):
498         if (target_os == 'android'):
499             env.AppendUnique(CPPDEFINES = ['NFC_ADAPTER'])
500         else:
501             print "CA Transport NFC is not supported "
502             Exit(1)
503     else:
504         env.AppendUnique(CPPDEFINES = ['NO_NFC_ADAPTER'])
505
506 if ('SUB' in with_mq):
507     env.AppendUnique(CPPDEFINES = ['MQ_SUBSCRIBER', 'WITH_MQ'])
508
509 if ('PUB' in with_mq):
510     env.AppendUnique(CPPDEFINES = ['MQ_PUBLISHER', 'WITH_MQ'])
511
512 if ('BROKER' in with_mq):
513     env.AppendUnique(CPPDEFINES = ['MQ_BROKER', 'WITH_MQ'])
514
515 if env.get('LOGGING'):
516     env.AppendUnique(CPPDEFINES = ['TB_LOG'])
517
518 if env.get('WITH_CLOUD') and with_tcp:
519     env.AppendUnique(CPPDEFINES = ['WITH_CLOUD'])
520
521 if 'CLIENT' in rd_mode:
522     env.AppendUnique(CPPDEFINES = ['RD_CLIENT'])
523
524 if 'SERVER' in rd_mode:
525     env.AppendUnique(CPPDEFINES = ['RD_SERVER'])
526
527 if with_ra_ibb:
528     env.AppendUnique(CPPDEFINES = ['RA_ADAPTER_IBB'])
529 ######################################################################
530 # Link scons to Yocto cross-toolchain ONLY when target_os is yocto
531 ######################################################################
532 if target_os == "yocto":
533     '''
534     This code injects Yocto cross-compilation tools+flags into scons'
535     build environment in order to invoke the relevant tools while
536     performing a build.
537     '''
538     import os.path
539     try:
540         CC = os.environ['CC']
541         target_prefix = CC.split()[0]
542         target_prefix = target_prefix[:len(target_prefix)-3]
543         tools = {"CC" : target_prefix+"gcc",
544                 "CXX" : target_prefix+"g++",
545                 "AS" : target_prefix+"as",
546                 "LD" : target_prefix+"ld",
547                 "GDB" : target_prefix+"gdb",
548                 "STRIP" : target_prefix+"strip",
549                 "RANLIB" : target_prefix+"ranlib",
550                 "OBJCOPY" : target_prefix+"objcopy",
551                 "OBJDUMP" : target_prefix+"objdump",
552                 "AR" : target_prefix+"ar",
553                 "NM" : target_prefix+"nm",
554                 "M4" : "m4",
555                 "STRINGS": target_prefix+"strings"}
556         PATH = os.environ['PATH'].split(os.pathsep)
557         for tool in tools:
558             if tool in os.environ:
559                 for path in PATH:
560                     if os.path.isfile(os.path.join(path, tools[tool])):
561                         env[tool] = os.path.join(path, os.environ[tool])
562                         break
563         env['CROSS_COMPILE'] = target_prefix[:len(target_prefix) - 1]
564     except:
565         print "ERROR in Yocto cross-toolchain environment"
566         Exit(1)
567     '''
568     Now reset TARGET_OS to linux so that all linux specific build configurations
569     hereupon apply for the entirety of the build process.
570     '''
571     env['TARGET_OS'] = 'linux'
572     '''
573     We want to preserve debug symbols to allow BitBake to generate both DEBUG and
574     RELEASE packages for OIC.
575     '''
576     env.AppendUnique(CCFLAGS = ['-g'])
577     '''
578     Additional flags to pass to the Yocto toolchain.
579     '''
580     if env.get('RELEASE'):
581         env.AppendUnique(CPPDEFINES = ['NDEBUG'])
582     env.AppendUnique(CPPDEFINES = ['__linux__', '_GNU_SOURCE'])
583     env.AppendUnique(CFLAGS = ['-std=gnu99'])
584     env.AppendUnique(CCFLAGS = ['-Wall', '-Wextra', '-fPIC'])
585     env.AppendUnique(LIBS = ['dl', 'pthread', 'uuid'])
586     Export('env')
587 else:
588     '''
589     If target_os is not Yocto, continue with the regular build process
590     '''
591     # Load config of target os
592     env.SConscript(target_os + '/SConscript')
593
594 # Delete the temp files of configuration
595 if env.GetOption('clean'):
596     dir = env.get('SRC_DIR')
597
598     if os.path.exists(dir + '/config.log'):
599         Execute(Delete(dir + '/config.log'))
600     if os.path.exists(dir + '/.sconsign.dblite'):
601         Execute(Delete(dir + '/.sconsign.dblite'))
602     if os.path.exists(dir + '/.sconf_temp'):
603         Execute(Delete(dir + '/.sconf_temp'))
604
605 ######################################################################
606 # Check for PThreads support
607 ######################################################################
608 import iotivityconfig
609 from iotivityconfig import *
610
611 conf = Configure(env,
612         custom_tests =
613         {
614             'CheckPThreadsSupport' : iotivityconfig.check_pthreads
615         } )
616
617 # Identify whether we have pthreads support, which is necessary for
618 # threading and mutexes.  This will set the environment variable
619 # POSIX_SUPPORTED, 1 if it is supported, 0 otherwise
620 conf.CheckPThreadsSupport()
621
622 env = conf.Finish()
623 ######################################################################
624
625 env.SConscript('external_libs.scons')
626 Return('env')