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.0'
11 # Map of host os and allowed target os (host: allowed target os)
13 'linux': ['linux', 'android', 'arduino', 'yocto', 'tizen'],
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'],
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', 'powerpc', 'powerpc64', 'mips', 'mipsel'],
32 host = platform.system().lower()
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.
39 if not host_target_map.has_key(host):
40 print "\nError: Current system (%s) isn't supported\n" % host
43 ######################################################################
44 # Get build options (the optins from command line)
45 ######################################################################
46 target_os = ARGUMENTS.get('TARGET_OS', host).lower() # target os
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])
52 if target_os == 'android':
55 default_arch = platform.machine()
57 if default_arch not in os_arch_map[target_os]:
58 default_arch = os_arch_map[target_os][0].lower()
60 target_arch = ARGUMENTS.get('TARGET_ARCH', default_arch) # target arch
62 # True if binary needs to be installed on board. (Might need root permissions)
63 # set to 'no', 'false' or 0 for only compilation
64 require_upload = ARGUMENTS.get('UPLOAD', False)
66 # Get the device name. This name can be used as network display name wherever possible
67 device_name = ARGUMENTS.get('DEVICE_NAME', "OIC-DEVICE")
69 default_with_upstream_libcoap = 0
71 if ARGUMENTS.get('TEST'):
72 logging_default = False
75 if ARGUMENTS.get('RELEASE', True) in ['y', 'yes', 'true', 't', '1', 'on', 'all', True]:
77 logging_default = (release_mode == False)
79 # targets that do not support the DTLS build (SECURED=1 build option)
80 targets_without_dtls_support = ['arduino'];
81 if ARGUMENTS.get('SECURED') == '1' and target_os in targets_without_dtls_support:
82 print "\nError: DTLS not supported on target os: %s MUST build with SECURED=0\n" % (target_os)
85 ######################################################################
86 # Common build options (release, target os, target arch)
87 ######################################################################
88 targets_disallow_multitransport = ['arduino']
90 help_vars = Variables()
91 help_vars.Add(BoolVariable('VERBOSE', 'Show compilation', False))
92 help_vars.Add(BoolVariable('RELEASE', 'Build for release?', True)) # set to 'no', 'false' or 0 for debug
93 help_vars.Add(EnumVariable('TARGET_OS', 'Target platform', host, host_target_map[host]))
96 help_vars.Add(BoolVariable('WITH_RA', 'Build with Remote Access module', False))
97 help_vars.Add(BoolVariable('WITH_TCP', 'Build with TCP adapter', False))
98 help_vars.Add(BoolVariable('WITH_PROXY', 'Build with CoAP-HTTP Proxy', False))
99 help_vars.Add(ListVariable('WITH_MQ', 'Build with MQ publisher/broker', 'OFF', ['OFF', 'SUB', 'PUB', 'BROKER']))
100 help_vars.Add(BoolVariable('WITH_CLOUD', 'Build including AccountManager class and Cloud Client sample', False))
101 help_vars.Add(ListVariable('RD_MODE', 'Resource Directory build mode', 'CLIENT', ['CLIENT', 'SERVER']))
103 help_vars.Add(BoolVariable('SIMULATOR', 'Build with simulator module', False))
105 help_vars.Add(BoolVariable('WITH_RA_IBB', 'Build with Remote Access module(workssys)', False))
108 if target_os in targets_disallow_multitransport:
109 help_vars.Add(ListVariable('TARGET_TRANSPORT', 'Target transport', 'IP', ['BT', 'BLE', 'IP', 'NFC']))
111 help_vars.Add(ListVariable('TARGET_TRANSPORT', 'Target transport', 'ALL', ['ALL', 'BT', 'BLE', 'IP', 'NFC']))
113 help_vars.Add(EnumVariable('TARGET_ARCH', 'Target architecture', default_arch, os_arch_map[target_os]))
114 help_vars.Add(EnumVariable('SECURED', 'Build with DTLS', '0', allowed_values=('0', '1')))
115 help_vars.Add(EnumVariable('DTLS_WITH_X509', 'DTLS with X.509 support', '0', allowed_values=('0', '1')))
116 help_vars.Add(EnumVariable('TEST', 'Run unit tests', '0', allowed_values=('0', '1')))
117 help_vars.Add(BoolVariable('LOGGING', 'Enable stack logging', logging_default))
118 help_vars.Add(BoolVariable('UPLOAD', 'Upload binary ? (For Arduino)', require_upload))
119 help_vars.Add(EnumVariable('ROUTING', 'Enable routing', 'EP', allowed_values=('GW', 'EP')))
120 help_vars.Add(EnumVariable('BUILD_SAMPLE', 'Build with sample', 'ON', allowed_values=('ON', 'OFF')))
121 help_vars.AddVariables(('DEVICE_NAME', 'Network display name for device (For Arduino)', device_name, None, None),)
122 help_vars.Add(PathVariable('ANDROID_NDK', 'Android NDK path', None, PathVariable.PathAccept))
123 help_vars.Add(PathVariable('ANDROID_HOME', 'Android SDK path', None, PathVariable.PathAccept))
124 help_vars.Add(PathVariable('ANDROID_GRADLE', 'Gradle binary file', None, PathVariable.PathIsFile))
125 help_vars.Add(EnumVariable('WITH_UPSTREAM_LIBCOAP', 'Use latest stable version of LibCoAP downloaded from github', default_with_upstream_libcoap, allowed_values=('0','1')))
127 AddOption('--prefix',
133 help='installation prefix')
135 ######################################################################
136 # Platform(build target) specific options: SDK/NDK & toolchain
137 ######################################################################
138 targets_support_cc = ['linux', 'arduino', 'tizen']
140 if target_os in targets_support_cc:
141 # Set cross compile toolchain
142 help_vars.Add('TC_PREFIX', "Toolchain prefix (Generally only be required for cross-compiling)", os.environ.get('TC_PREFIX'))
143 help_vars.Add(PathVariable('TC_PATH',
144 'Toolchain path (Generally only be required for cross-compiling)',
145 os.environ.get('TC_PATH')))
147 if target_os in ['android', 'arduino']: # Android/Arduino always uses GNU compiler regardless of the host
148 env = Environment(variables = help_vars,
149 tools = ['gnulink', 'gcc', 'g++', 'ar', 'as', 'textfile']
152 env = Environment(variables = help_vars, tools = ['default', 'textfile'],
153 TARGET_ARCH = target_arch, TARGET_OS = target_os,
154 PREFIX = GetOption('prefix'),
155 LIB_INSTALL_DIR = ARGUMENTS.get('LIB_INSTALL_DIR') #for 64bit build
157 Help(help_vars.GenerateHelpText(env))
160 ************************************ Warning **********************************
161 * Enviornment variable TC_PREFIX/TC_PATH is set. It will change the default *
162 * toolchain, if it isn't what you expect you should unset it, otherwise it may*
163 * cause inexplicable errors. *
164 *******************************************************************************
166 if env.get('VERBOSE') == False:
167 env['CCCOMSTR'] = "Compiling $TARGET"
168 env['SHCCCOMSTR'] = "Compiling $TARGET"
169 env['CXXCOMSTR'] = "Compiling $TARGET"
170 env['SHCXXCOMSTR'] = "Compiling $TARGET"
171 env['LINKCOMSTR'] = "Linking $TARGET"
172 env['SHLINKCOMSTR'] = "Linking $TARGET"
173 env['ARCOMSTR'] = "Archiving $TARGET"
174 env['RANLIBCOMSTR'] = "Indexing Archive $TARGET"
176 if target_os in targets_support_cc:
177 prefix = env.get('TC_PREFIX')
178 tc_path = env.get('TC_PATH')
180 env.Replace(CC = prefix + env.get('CC', 'gcc'))
181 env.Replace(CXX = prefix + env.get('CXX', 'g++'))
182 env.Replace(AR = prefix + env.get('AR', 'ar'))
183 env.Replace(AS = prefix + env.get('AS', 'as'))
184 env.Replace(RANLIB = prefix + env.get('RANLIB', 'ranlib'))
187 env.PrependENVPath('PATH', tc_path)
188 sys_root = os.path.abspath(tc_path + '/../')
189 env.AppendUnique(CCFLAGS = ['--sysroot=' + sys_root])
190 env.AppendUnique(LINKFLAGS = ['--sysroot=' + sys_root])
192 if prefix or tc_path:
195 # Import env variables only if reproductibility is ensured
196 if target_os in ['yocto']:
197 env['CONFIG_ENVIRONMENT_IMPORT'] = True
199 env['CONFIG_ENVIRONMENT_IMPORT'] = False
201 if env['CONFIG_ENVIRONMENT_IMPORT'] == True:
202 print "warning: importing some environment variables for OS: %s" % target_os
203 for ev in ['PATH', 'PKG_CONFIG', 'PKG_CONFIG_PATH', 'PKG_CONFIG_SYSROOT_DIR']:
204 if os.environ.get(ev) != None:
205 env['ENV'][ev] = os.environ.get(ev)
206 if os.environ['LDFLAGS'] != None:
207 env.AppendUnique(LINKFLAGS = Split(os.environ['LDFLAGS']))
209 # Ensure scons be able to change its working directory
210 env.SConscriptChdir(1)
212 # Set the source directory and build directory
213 # Source directory: 'dir'
214 # Build directory: 'dir'/out/<target_os>/<target_arch>/<release or debug>/
216 # You can get the directory as following:
218 # env.get('BUILD_DIR')
220 def __set_dir(env, dir):
221 if not os.path.exists(dir + '/SConstruct'):
223 *************************************** Error *********************************
224 * The directory(%s) seems isn't a source code directory, no SConstruct file is
226 *******************************************************************************
230 if env.get('RELEASE'):
231 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/release/'
233 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/debug/'
234 env.VariantDir(build_dir, dir, duplicate=0)
236 env.Replace(BUILD_DIR = build_dir)
237 env.Replace(SRC_DIR = dir)
239 def __src_to_obj(env, src, home = ''):
240 obj = env.get('BUILD_DIR') + src.replace(home, '')
241 if env.get('OBJSUFFIX'):
242 obj += env.get('OBJSUFFIX')
243 return env.Object(obj, src)
245 def __install(ienv, targets, name):
246 i_n = ienv.Install(env.get('BUILD_DIR'), targets)
248 env.AppendUnique(TS = [name])
250 def __installlib(ienv, targets, name):
251 user_prefix = env.get('PREFIX')
253 user_lib = env.get('LIB_INSTALL_DIR')
255 i_n = ienv.Install(user_lib, targets)
257 i_n = ienv.Install(user_prefix + '/lib', targets)
258 ienv.Alias("install", i_n)
260 i_n = ienv.Install(env.get('BUILD_DIR'), targets)
261 ienv.Alias("install", i_n)
263 def __installbin(ienv, targets, name):
264 user_prefix = env.get('PREFIX')
266 i_n = ienv.Install(user_prefix + '/bin', targets)
267 ienv.Alias("install", i_n)
269 def __installheader(ienv, targets, dir, name):
270 user_prefix = env.get('PREFIX')
272 i_n = ienv.Install(user_prefix + '/include/' + dir ,targets)
274 i_n = ienv.Install(os.path.join(env.get('BUILD_DIR'), 'include', dir), targets)
275 ienv.Alias("install", i_n)
277 def __installpcfile(ienv, targets, name):
278 user_prefix = env.get('PREFIX')
280 user_lib = env.get('LIB_INSTALL_DIR')
282 i_n = ienv.Install(user_lib + '/pkgconfig', targets)
284 i_n = ienv.Install(user_prefix + '/lib/pkgconfig', targets)
286 i_n = ienv.Install(env.get('BUILD_DIR') + 'lib/pkgconfig', targets)
287 ienv.Alias("install", i_n)
289 def __append_target(ienv, name, targets = None):
291 env.Alias(name, targets)
292 env.AppendUnique(TS = [name])
294 def __print_targets(env):
296 ===============================================================================
298 for t in env.get('TS'):
301 \nDefault all targets will be built. You can specify the target to build:
303 $ scons [options] [target]
304 ===============================================================================
307 env.AddMethod(__set_dir, 'SetDir')
308 env.AddMethod(__print_targets, 'PrintTargets')
309 env.AddMethod(__src_to_obj, 'SrcToObj')
310 env.AddMethod(__append_target, 'AppendTarget')
311 env.AddMethod(__install, 'InstallTarget')
312 env.AddMethod(__installlib, 'UserInstallTargetLib')
313 env.AddMethod(__installbin, 'UserInstallTargetBin')
314 env.AddMethod(__installheader, 'UserInstallTargetHeader')
315 env.AddMethod(__installpcfile, 'UserInstallTargetPCFile')
316 env.SetDir(env.GetLaunchDir())
317 env['ROOT_DIR']=env.GetLaunchDir()+'/..'
321 ######################################################################
322 # Scons to generate the iotivity.pc file from iotivity.pc.in file
323 ######################################################################
324 pc_file = env.get('SRC_DIR') + '/iotivity.pc.in'
326 user_prefix = env.get('PREFIX')
327 user_lib = env.get('LIB_INSTALL_DIR')
330 user_prefix = env.get('BUILD_DIR').encode('string_escape')
333 user_lib = '$${prefix}/lib'
336 if env.get('LOGGING'):
337 defines.append('-DTB_LOG=1')
339 if env.get('ROUTING') == 'GW':
340 defines.append('-DROUTING_GATEWAY=1')
341 elif env.get('ROUTING') == 'EP':
342 defines.append('-DROUTING_EP=1')
345 '\@VERSION\@': project_version,
346 '\@PREFIX\@': user_prefix,
347 '\@EXEC_PREFIX\@': user_prefix,
348 '\@LIB_INSTALL_DIR\@': user_lib,
349 '\@DEFINES\@': " ".join(defines)
352 env.Substfile(pc_file, SUBST_DICT = pc_vars)
354 ######################################################################
355 # Link scons to Yocto cross-toolchain ONLY when target_os is yocto
356 ######################################################################
357 if target_os == "yocto":
359 This code injects Yocto cross-compilation tools+flags into scons'
360 build environment in order to invoke the relevant tools while
365 CC = os.environ['CC']
366 target_prefix = CC.split()[0]
367 target_prefix = target_prefix[:len(target_prefix)-3]
368 tools = {"CC" : target_prefix+"gcc",
369 "CXX" : target_prefix+"g++",
370 "AS" : target_prefix+"as",
371 "LD" : target_prefix+"ld",
372 "GDB" : target_prefix+"gdb",
373 "STRIP" : target_prefix+"strip",
374 "RANLIB" : target_prefix+"ranlib",
375 "OBJCOPY" : target_prefix+"objcopy",
376 "OBJDUMP" : target_prefix+"objdump",
377 "AR" : target_prefix+"ar",
378 "NM" : target_prefix+"nm",
380 "STRINGS": target_prefix+"strings"}
381 PATH = os.environ['PATH'].split(os.pathsep)
383 if tool in os.environ:
385 if os.path.isfile(os.path.join(path, tools[tool])):
386 env[tool] = os.path.join(path, os.environ[tool])
388 env['CROSS_COMPILE'] = target_prefix[:len(target_prefix) - 1]
390 print "ERROR in Yocto cross-toolchain environment"
393 Now reset TARGET_OS to linux so that all linux specific build configurations
394 hereupon apply for the entirety of the build process.
396 env['TARGET_OS'] = 'linux'
398 We want to preserve debug symbols to allow BitBake to generate both DEBUG and
399 RELEASE packages for OIC.
401 env.AppendUnique(CCFLAGS = ['-g'])
403 Additional flags to pass to the Yocto toolchain.
405 if env.get('RELEASE'):
406 env.AppendUnique(CPPDEFINES = ['NDEBUG'])
407 if env.get('LOGGING'):
408 env.AppendUnique(CPPDEFINES = ['TB_LOG'])
409 env.AppendUnique(CPPDEFINES = ['WITH_POSIX', '__linux__', '_GNU_SOURCE'])
410 env.AppendUnique(CFLAGS = ['-std=gnu99'])
411 env.AppendUnique(CCFLAGS = ['-Wall', '-Wextra', '-fPIC'])
412 env.AppendUnique(LIBS = ['dl', 'pthread', 'uuid'])
416 If target_os is not Yocto, continue with the regular build process
418 # Load config of target os
419 env.SConscript(target_os + '/SConscript')
421 # Delete the temp files of configuration
422 if env.GetOption('clean'):
423 dir = env.get('SRC_DIR')
425 if os.path.exists(dir + '/config.log'):
426 Execute(Delete(dir + '/config.log'))
427 if os.path.exists(dir + '/.sconsign.dblite'):
428 Execute(Delete(dir + '/.sconsign.dblite'))
429 if os.path.exists(dir + '/.sconf_temp'):
430 Execute(Delete(dir + '/.sconf_temp'))
432 ######################################################################
433 # Check for PThreads support
434 ######################################################################
435 import iotivityconfig
436 from iotivityconfig import *
438 conf = Configure(env,
441 'CheckPThreadsSupport' : iotivityconfig.check_pthreads
444 # Identify whether we have pthreads support, which is necessary for
445 # threading and mutexes. This will set the environment variable
446 # POSIX_SUPPORTED, 1 if it is supported, 0 otherwise
447 conf.CheckPThreadsSupport()
450 ######################################################################
452 env.SConscript('external_libs.scons')