1 # -*- mode: python; python-indent-offset: 4; indent-tabs-mode: nil -*-
3 # This script includes generic build options:
4 # release/debug, target os, target arch, cross toolchain, build environment etc
9 project_version = '1.2.1'
11 # Map of host os and allowed target os (host: allowed target os)
13 'linux': ['linux', 'android', 'arduino', 'yocto', 'tizen', 'tizenrt'],
14 'windows': ['windows', 'android', 'arduino'],
15 'darwin': ['darwin', 'ios', 'android', 'arduino'],
16 'msys_nt' :['msys_nt'],
19 # Map of os and allowed archs (os: allowed archs)
21 'linux': ['x86', 'x86_64', 'arm', 'arm-v7a', 'armeabi-v7a', 'arm64'],
22 'tizen': ['x86', 'x86_64', 'arm', 'arm-v7a', 'armeabi-v7a', 'arm64', 'riscv64'],
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 'tizenrt': ['armv7-r', 'armv7-m'],
33 host = platform.system().lower()
35 # the host string contains version of windows. 6.3, 6.4, 10.0 which is 8.0, 8.1, and 10 respectively.
36 # Let's canonicalize the msys_nt-XX.X system name by stripping version off.
40 if not host_target_map.has_key(host):
41 print "\nError: Current system (%s) isn't supported\n" % host
44 ######################################################################
45 # Get build options (the optins from command line)
46 ######################################################################
47 target_os = ARGUMENTS.get('TARGET_OS', host).lower() # target os
49 if target_os not in host_target_map[host]:
50 print "\nError: Unknown target os: %s (Allow values: %s)\n" % (target_os, host_target_map[host])
53 if target_os == 'android':
56 default_arch = platform.machine()
58 if default_arch not in os_arch_map[target_os]:
59 default_arch = os_arch_map[target_os][0].lower()
61 target_arch = ARGUMENTS.get('TARGET_ARCH', default_arch) # target arch
63 # True if binary needs to be installed on board. (Might need root permissions)
64 # set to 'no', 'false' or 0 for only compilation
65 require_upload = ARGUMENTS.get('UPLOAD', False)
67 # Get the device name. This name can be used as network display name wherever possible
68 device_name = ARGUMENTS.get('DEVICE_NAME', "OIC-DEVICE")
70 default_with_upstream_libcoap = 0
72 if ARGUMENTS.get('TEST'):
73 logging_default = False
76 if ARGUMENTS.get('RELEASE', True) in ['y', 'yes', 'true', 't', '1', 'on', 'all', True]:
78 logging_default = (release_mode == False)
80 # targets that do not support the DTLS build (SECURED=1 build option)
81 targets_without_dtls_support = ['arduino'];
82 if ARGUMENTS.get('SECURED') == '1' and target_os in targets_without_dtls_support:
83 print "\nError: DTLS not supported on target os: %s MUST build with SECURED=0\n" % (target_os)
86 ######################################################################
87 # Common build options (release, target os, target arch)
88 ######################################################################
89 targets_disallow_multitransport = ['arduino']
91 help_vars = Variables()
92 help_vars.Add(BoolVariable('VERBOSE', 'Show compilation', False))
93 help_vars.Add(BoolVariable('RELEASE', 'Build for release?', True)) # set to 'no', 'false' or 0 for debug
94 help_vars.Add(EnumVariable('TARGET_OS', 'Target platform', host, host_target_map[host]))
97 help_vars.Add(BoolVariable('WITH_RA', 'Build with Remote Access module', False))
98 help_vars.Add(BoolVariable('WITH_TCP', 'Build with TCP adapter', False))
99 help_vars.Add(BoolVariable('DISABLE_TCP_SERVER', 'Disable TCP server', 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 help_vars.Add(BoolVariable('DISABLE_PRESENCE', 'Disable Presence Feature', False))
105 help_vars.Add(BoolVariable('DISABLE_BLE_SERVER', 'Disable BLE server', False))
107 help_vars.Add(BoolVariable('SIMULATOR', 'Build with simulator module', False))
109 help_vars.Add(BoolVariable('WITH_RA_IBB', 'Build with Remote Access module(workssys)', False))
110 help_vars.Add(BoolVariable('WITH_ASAN', 'Build with Address Sanitizer support', False))
112 if target_os in targets_disallow_multitransport:
113 help_vars.Add(ListVariable('TARGET_TRANSPORT', 'Target transport', 'IP', ['BT', 'BLE', 'IP', 'NFC']))
115 help_vars.Add(ListVariable('TARGET_TRANSPORT', 'Target transport', 'ALL', ['ALL', 'BT', 'BLE', 'IP', 'NFC']))
117 help_vars.Add(EnumVariable('TARGET_ARCH', 'Target architecture', default_arch, os_arch_map[target_os]))
118 help_vars.Add(EnumVariable('SECURED', 'Build with DTLS', '0', allowed_values=('0', '1')))
119 help_vars.Add(EnumVariable('WITH_MBEDTLS_STATIC_LIB', 'Build mbedTLS library as static library', '0', 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(EnumVariable('BLE_CUSTOM_ADV', 'Build with CUSTOM Advertisement', 'False', allowed_values=('True', 'False')))
134 help_vars.Add(EnumVariable('BLE_DIVISION', 'BLE Build with division code', 'OFF', allowed_values=('OFF', 'MCD', 'VD')))
135 help_vars.Add(EnumVariable('BLE_TIZEN_30', 'Build with tizen 3.0 BLE api', 'False', allowed_values=('True', 'False')))
136 help_vars.Add(EnumVariable('BLE_TIZEN_40', 'Build with tizen 4.0 BLE api', 'False', allowed_values=('True', 'False')))
137 help_vars.Add(EnumVariable('BLE_TIZEN_55', 'Build with tizen 5.5 BLE api', 'False', allowed_values=('True', 'False')))
138 help_vars.Add(PathVariable('TIZENRT_OS_DIR', 'Absolute Path to TizenRT OS directory', None, PathVariable.PathAccept))
139 help_vars.Add(EnumVariable('PLATFORM_TLS', 'Use platform tls instead of local mbedtls', '0', allowed_values=('0', '1')))
140 help_vars.Add(EnumVariable('OIC_SUPPORT_TIZEN_TRACE', 'Tizen Trace(T-trace) api availability', 'False', allowed_values=('True', 'False')))
141 help_vars.Add(BoolVariable('WITH_PROCESS_EVENT','Build including process event logics in ocstack', 'False'))
142 help_vars.Add(BoolVariable('DISABLE_AUTO_TCP_NSPROVIDER_DISCOVERY', 'Disable auto-discovery of NSProvider over TCP for TCP connected events', False))
143 help_vars.Add(BoolVariable('ENABLE_DUP','Enable duplicate IPv4/IPv6 packets', 'False'))
144 AddOption('--prefix',
150 help='installation prefix')
152 ######################################################################
153 # Platform(build target) specific options: SDK/NDK & toolchain
154 ######################################################################
155 targets_support_cc = ['linux', 'arduino', 'tizen', 'tizenrt']
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')))
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']
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
175 targets_support_gcov = ['linux', 'darwin']
176 if target_os in targets_support_gcov:
177 # Build option to enable coverage checking using gcov.
178 # Requires gcc or clang family compilers.
179 # Actual compiler flags need to be set in target-specific script.
181 BoolVariable('COVERAGE_CHECKS',
182 'Build support for running code coverage checks',
185 Help(help_vars.GenerateHelpText(env))
188 ************************************ Warning **********************************
189 * Enviornment variable TC_PREFIX/TC_PATH is set. It will change the default *
190 * toolchain, if it isn't what you expect you should unset it, otherwise it may*
191 * cause inexplicable errors. *
192 *******************************************************************************
194 if env.get('VERBOSE') == False:
195 env['CCCOMSTR'] = "Compiling $TARGET"
196 env['SHCCCOMSTR'] = "Compiling $TARGET"
197 env['CXXCOMSTR'] = "Compiling $TARGET"
198 env['SHCXXCOMSTR'] = "Compiling $TARGET"
199 env['LINKCOMSTR'] = "Linking $TARGET"
200 env['SHLINKCOMSTR'] = "Linking $TARGET"
201 env['ARCOMSTR'] = "Archiving $TARGET"
202 env['RANLIBCOMSTR'] = "Indexing Archive $TARGET"
204 if target_os in targets_support_cc:
205 prefix = env.get('TC_PREFIX')
206 tc_path = env.get('TC_PATH')
208 env.Replace(CC = prefix + env.get('CC', 'gcc'))
209 env.Replace(CXX = prefix + env.get('CXX', 'g++'))
210 env.Replace(AR = prefix + env.get('AR', 'ar'))
211 env.Replace(AS = prefix + env.get('AS', 'as'))
212 env.Replace(RANLIB = prefix + env.get('RANLIB', 'ranlib'))
215 env.PrependENVPath('PATH', tc_path)
216 sys_root = os.path.abspath(tc_path + '/../')
217 env.AppendUnique(CCFLAGS = ['--sysroot=' + sys_root])
218 env.AppendUnique(LINKFLAGS = ['--sysroot=' + sys_root])
220 if target_os == 'tizenrt':
221 env.PrependENVPath('PATH', os.environ['PATH'])
223 if prefix or tc_path:
226 # If cross-compiling, honor environment settings for toolchain to avoid picking up native tools
227 if os.environ.get('PKG_CONFIG') != None:
228 env["ENV"]["PKG_CONFIG"] = os.environ.get("PKG_CONFIG")
229 if os.environ.get('PKG_CONFIG_PATH') != None:
230 env["ENV"]["PKG_CONFIG_PATH"] = os.environ.get("PKG_CONFIG_PATH")
231 if os.environ.get('PKG_CONFIG_SYSROOT_DIR') != None:
232 env["ENV"]["PKG_CONFIG_SYSROOT_DIR"] = os.environ.get("PKG_CONFIG_SYSROOT_DIR")
234 # Ensure scons be able to change its working directory
235 env.SConscriptChdir(1)
237 # Set the source directory and build directory
238 # Source directory: 'dir'
239 # Build directory: 'dir'/out/<target_os>/<target_arch>/<release or debug>/
241 # You can get the directory as following:
243 # env.get('BUILD_DIR')
245 def __set_dir(env, dir):
246 if not os.path.exists(dir + '/SConstruct'):
248 *************************************** Error *********************************
249 * The directory(%s) seems isn't a source code directory, no SConstruct file is
251 *******************************************************************************
255 if env.get('RELEASE'):
256 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/release/'
258 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/debug/'
259 env.VariantDir(build_dir, dir, duplicate=0)
261 env.Replace(BUILD_DIR = build_dir)
262 env.Replace(SRC_DIR = dir)
264 def __src_to_obj(env, src, home = ''):
265 obj = env.get('BUILD_DIR') + src.replace(home, '')
266 if env.get('OBJSUFFIX'):
267 obj += env.get('OBJSUFFIX')
268 return env.Object(obj, src)
270 def __install(ienv, targets, name):
271 i_n = ienv.Install(env.get('BUILD_DIR'), targets)
273 env.AppendUnique(TS = [name])
275 def __installlib(ienv, targets, name):
276 user_prefix = env.get('PREFIX')
278 user_lib = env.get('LIB_INSTALL_DIR')
280 i_n = ienv.Install(user_lib, targets)
282 i_n = ienv.Install(user_prefix + '/lib', targets)
283 ienv.Alias("install", i_n)
285 i_n = ienv.Install(env.get('BUILD_DIR'), targets)
286 ienv.Alias("install", i_n)
288 def __installbin(ienv, targets, name):
289 user_prefix = env.get('PREFIX')
291 i_n = ienv.Install(user_prefix + '/bin', targets)
292 ienv.Alias("install", i_n)
294 def __installheader(ienv, targets, dir, name):
295 user_prefix = env.get('PREFIX')
297 i_n = ienv.Install(user_prefix + '/include/' + dir ,targets)
299 i_n = ienv.Install(os.path.join(env.get('BUILD_DIR'), 'include', dir), targets)
300 ienv.Alias("install", i_n)
302 def __installpcfile(ienv, targets, name):
303 user_prefix = env.get('PREFIX')
305 user_lib = env.get('LIB_INSTALL_DIR')
307 i_n = ienv.Install(user_lib + '/pkgconfig', targets)
309 i_n = ienv.Install(user_prefix + '/lib/pkgconfig', targets)
311 i_n = ienv.Install(env.get('BUILD_DIR') + 'lib/pkgconfig', targets)
312 ienv.Alias("install", i_n)
314 def __append_target(ienv, name, targets = None):
316 env.Alias(name, targets)
317 env.AppendUnique(TS = [name])
319 def __print_targets(env):
321 ===============================================================================
323 for t in env.get('TS'):
326 \nDefault all targets will be built. You can specify the target to build:
328 $ scons [options] [target]
329 ===============================================================================
332 env.AddMethod(__set_dir, 'SetDir')
333 env.AddMethod(__print_targets, 'PrintTargets')
334 env.AddMethod(__src_to_obj, 'SrcToObj')
335 env.AddMethod(__append_target, 'AppendTarget')
336 env.AddMethod(__install, 'InstallTarget')
337 env.AddMethod(__installlib, 'UserInstallTargetLib')
338 env.AddMethod(__installbin, 'UserInstallTargetBin')
339 env.AddMethod(__installheader, 'UserInstallTargetHeader')
340 env.AddMethod(__installpcfile, 'UserInstallTargetPCFile')
341 env.SetDir(env.GetLaunchDir())
342 env['ROOT_DIR']=env.GetLaunchDir()+'/..'
346 ######################################################################
347 # Scons to generate the iotivity.pc file from iotivity.pc.in file
348 ######################################################################
349 pc_file = env.get('SRC_DIR') + '/iotivity.pc.in'
351 user_prefix = env.get('PREFIX')
352 user_lib = env.get('LIB_INSTALL_DIR')
355 user_prefix = env.get('BUILD_DIR').encode('string_escape')
358 user_lib = '$${prefix}/lib'
361 if env.get('LOGGING'):
362 defines.append('-DTB_LOG=1')
364 if env.get('ROUTING') == 'GW':
365 defines.append('-DROUTING_GATEWAY=1')
366 elif env.get('ROUTING') == 'EP':
367 defines.append('-DROUTING_EP=1')
369 if env.get('WITH_TCP'):
370 defines.append('-DTCP_ADAPTER=1')
371 if env.get('SECURED') == '1':
372 defines.append('-D__WITH_TLS__=1')
374 if env.get('DISABLE_TCP_SERVER'):
375 defines.append('-DDISABLE_TCP_SERVER=1')
377 if env.get('DISABLE_PRESENCE') == False:
378 env.AppendUnique(CPPDEFINES = ['WITH_PRESENCE'])
380 if env.get('DISABLE_BLE_SERVER'):
381 defines.append('-DDISABLE_BLE_SERVER=1')
383 if env.get('WITH_PROCESS_EVENT'):
384 env.AppendUnique(CPPDEFINES=['WITH_PROCESS_EVENT'])
386 if env.get('DISABLE_AUTO_TCP_NSPROVIDER_DISCOVERY'):
387 env.AppendUnique(CPPDEFINES = ['DISABLE_AUTO_TCP_NSPROVIDER_DISCOVERY'])
389 if env.get('ENABLE_DUP'):
390 env.AppendUnique(CPPDEFINES = ['ENABLE_DUP'])
393 if env.get('SECURED') == '1':
394 defines.append('-D__WITH_DTLS__=1')
395 if env.get('EXC_PROV_SUPPORT') == '0':
396 libs.append('-locpmapi') # CAPI
397 libs.append('-locprovision') # C++API
400 '\@VERSION\@': project_version,
401 '\@PREFIX\@': user_prefix,
402 '\@EXEC_PREFIX\@': user_prefix,
403 '\@LIB_INSTALL_DIR\@': user_lib,
404 '\@DEFINES\@': " ".join(defines),
405 '\@LIBS\@': " ".join(libs)
408 env.Substfile(pc_file, SUBST_DICT = pc_vars)
410 ######################################################################
411 # Link scons to Yocto cross-toolchain ONLY when target_os is yocto
412 ######################################################################
413 if target_os == "yocto":
415 This code injects Yocto cross-compilation tools+flags into scons'
416 build environment in order to invoke the relevant tools while
421 CC = os.environ['CC']
422 target_prefix = CC.split()[0]
423 target_prefix = target_prefix[:len(target_prefix)-3]
424 tools = {"CC" : target_prefix+"gcc",
425 "CXX" : target_prefix+"g++",
426 "AS" : target_prefix+"as",
427 "LD" : target_prefix+"ld",
428 "GDB" : target_prefix+"gdb",
429 "STRIP" : target_prefix+"strip",
430 "RANLIB" : target_prefix+"ranlib",
431 "OBJCOPY" : target_prefix+"objcopy",
432 "OBJDUMP" : target_prefix+"objdump",
433 "AR" : target_prefix+"ar",
434 "NM" : target_prefix+"nm",
436 "STRINGS": target_prefix+"strings"}
437 PATH = os.environ['PATH'].split(os.pathsep)
439 if tool in os.environ:
441 if os.path.isfile(os.path.join(path, tools[tool])):
442 env[tool] = os.path.join(path, os.environ[tool])
444 env['CROSS_COMPILE'] = target_prefix[:len(target_prefix) - 1]
445 if os.environ['LDFLAGS'] != None:
446 env.AppendUnique(LINKFLAGS = Split(os.environ['LDFLAGS']))
448 print "ERROR in Yocto cross-toolchain environment"
451 Now reset TARGET_OS to linux so that all linux specific build configurations
452 hereupon apply for the entirety of the build process.
454 env['TARGET_OS'] = 'linux'
456 We want to preserve debug symbols to allow BitBake to generate both DEBUG and
457 RELEASE packages for OIC.
459 env.AppendUnique(CCFLAGS = ['-g'])
461 Additional flags to pass to the Yocto toolchain.
463 if env.get('RELEASE'):
464 env.AppendUnique(CPPDEFINES = ['NDEBUG'])
465 if env.get('LOGGING'):
466 env.AppendUnique(CPPDEFINES = ['TB_LOG'])
467 env.AppendUnique(CPPDEFINES = ['WITH_POSIX', '__linux__', '_GNU_SOURCE'])
468 env.AppendUnique(CFLAGS = ['-std=gnu99'])
469 env.AppendUnique(CCFLAGS = ['-Wall', '-Wextra', '-fPIC'])
470 env.AppendUnique(LIBS = ['dl', 'pthread', 'uuid'])
474 If target_os is not Yocto, continue with the regular build process
476 # Load config of target os
477 env.SConscript(target_os + '/SConscript')
479 # Delete the temp files of configuration
480 if env.GetOption('clean'):
481 dir = env.get('SRC_DIR')
483 if os.path.exists(dir + '/config.log'):
484 Execute(Delete(dir + '/config.log'))
485 if os.path.exists(dir + '/.sconsign.dblite'):
486 Execute(Delete(dir + '/.sconsign.dblite'))
487 if os.path.exists(dir + '/.sconf_temp'):
488 Execute(Delete(dir + '/.sconf_temp'))
490 ######################################################################
491 # Check for PThreads support
492 ######################################################################
493 import iotivityconfig
494 from iotivityconfig import *
496 conf = Configure(env,
499 'CheckPThreadsSupport' : iotivityconfig.check_pthreads
502 # Identify whether we have pthreads support, which is necessary for
503 # threading and mutexes. This will set the environment variable
504 # POSIX_SUPPORTED, 1 if it is supported, 0 otherwise
505 conf.CheckPThreadsSupport()
508 ######################################################################
510 env.SConscript('external_libs.scons')