2 # This script includes generic build options:
3 # release/debug, target os, target arch, cross toolchain, build environment etc
8 # Map of host os and allowed target os (host: allowed target os)
10 'linux': ['linux', 'android', 'arduino', 'yocto', 'tizen'],
11 'windows': ['windows', 'android', 'arduino'],
12 'darwin': ['darwin', 'ios', 'android', 'arduino'],
13 'msys_nt' :['msys_nt'],
16 # Map of os and allowed archs (os: allowed archs)
18 'linux': ['x86', 'x86_64', 'arm', 'arm-v7a', 'arm64'],
19 'tizen': ['x86', 'x86_64', 'arm', 'arm-v7a', 'armeabi-v7a', 'arm64'],
20 'android': ['x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'armeabi-v7a-hard', 'arm64-v8a'],
21 'windows': ['x86', 'amd64', 'arm'],
22 'msys_nt':['x86', 'x86_64'],
23 'darwin': ['i386', 'x86_64'],
24 'ios': ['i386', 'x86_64', 'armv7', 'armv7s', 'arm64'],
25 'arduino': ['avr', 'arm'],
26 'yocto': ['i586', 'x86_64', 'arm', 'powerpc', 'powerpc64', 'mips', 'mipsel'],
29 host = platform.system().lower()
31 # the host string contains version of windows. 6.3, 6.4, 10.0 which is 8.0, 8.1, and 10 respectively.
32 # Let's canonicalize the msys_nt-XX.X system name by stripping version off.
36 if not host_target_map.has_key(host):
37 print "\nError: Current system (%s) isn't supported\n" % host
40 ######################################################################
41 # Get build options (the optins from command line)
42 ######################################################################
43 target_os = ARGUMENTS.get('TARGET_OS', host).lower() # target os
45 if target_os not in host_target_map[host]:
46 print "\nError: Unknown target os: %s (Allow values: %s)\n" % (target_os, host_target_map[host])
49 if target_os == 'android':
52 default_arch = platform.machine()
54 if default_arch not in os_arch_map[target_os]:
55 default_arch = os_arch_map[target_os][0].lower()
57 target_arch = ARGUMENTS.get('TARGET_ARCH', default_arch) # target arch
59 # True if binary needs to be installed on board. (Might need root permissions)
60 # set to 'no', 'false' or 0 for only compilation
61 require_upload = ARGUMENTS.get('UPLOAD', False)
63 # Get the device name. This name can be used as network display name wherever possible
64 device_name = ARGUMENTS.get('DEVICE_NAME', "OIC-DEVICE")
66 if ARGUMENTS.get('TEST'):
67 logging_default = False
70 if ARGUMENTS.get('RELEASE', True) in ['y', 'yes', 'true', 't', '1', 'on', 'all', True]:
72 logging_default = (release_mode == False)
76 ######################################################################
77 # Common build options (release, target os, target arch)
78 ######################################################################
79 targets_disallow_multitransport = ['arduino']
81 help_vars = Variables()
82 help_vars.Add(BoolVariable('VERBOSE', 'Show compilation', False))
83 help_vars.Add(BoolVariable('RELEASE', 'Build for release?', True)) # set to 'no', 'false' or 0 for debug
84 help_vars.Add(EnumVariable('TARGET_OS', 'Target platform', host, host_target_map[host]))
87 help_vars.Add(BoolVariable('WITH_RA', 'Build with Remote Access module', False))
88 help_vars.Add(BoolVariable('WITH_TCP', 'Build with TCP adapter', False))
89 help_vars.Add(ListVariable('WITH_MQ', 'Build with MQ publisher/broker', 'OFF', ['OFF', 'SUB', 'PUB', 'BROKER']))
90 help_vars.Add(EnumVariable('WITH_RD', 'Build including Resource Directory', '0', allowed_values=('0', '1')))
91 help_vars.Add(BoolVariable('WITH_CLOUD', 'Build including Cloud Connector and Cloud Client sample', False))
93 help_vars.Add(BoolVariable('SIMULATOR', 'Build with simulator module', False))
95 help_vars.Add(BoolVariable('WITH_RA_IBB', 'Build with Remote Access module(workssys)', False))
98 if target_os in targets_disallow_multitransport:
99 help_vars.Add(ListVariable('TARGET_TRANSPORT', 'Target transport', 'IP', ['BT', 'BLE', 'IP', 'NFC']))
101 help_vars.Add(ListVariable('TARGET_TRANSPORT', 'Target transport', 'ALL', ['ALL', 'BT', 'BLE', 'IP', 'NFC']))
103 help_vars.Add(EnumVariable('TARGET_ARCH', 'Target architecture', default_arch, os_arch_map[target_os]))
104 help_vars.Add(EnumVariable('SECURED', 'Build with DTLS', '0', allowed_values=('0', '1')))
105 help_vars.Add(EnumVariable('DTLS_WITH_X509', 'DTLS with X.509 support', '0', allowed_values=('0', '1')))
106 help_vars.Add(EnumVariable('TEST', 'Run unit tests', '0', allowed_values=('0', '1')))
107 help_vars.Add(BoolVariable('LOGGING', 'Enable stack logging', logging_default))
108 help_vars.Add(BoolVariable('UPLOAD', 'Upload binary ? (For Arduino)', require_upload))
109 help_vars.Add(EnumVariable('ROUTING', 'Enable routing', 'EP', allowed_values=('GW', 'EP')))
110 help_vars.Add(EnumVariable('BUILD_SAMPLE', 'Build with sample', 'ON', allowed_values=('ON', 'OFF')))
111 help_vars.AddVariables(('DEVICE_NAME', 'Network display name for device (For Arduino)', device_name, None, None),)
112 help_vars.Add(PathVariable('ANDROID_NDK', 'Android NDK path', None, PathVariable.PathAccept))
113 help_vars.Add(PathVariable('ANDROID_HOME', 'Android SDK path', None, PathVariable.PathAccept))
114 help_vars.Add(PathVariable('ANDROID_GRADLE', 'Gradle binary file', None, PathVariable.PathIsFile))
115 #ES_TARGET_ENROLLEE is for specifying what is our target enrollee (Arduino or rest of platforms which support Multicast)
116 help_vars.Add(EnumVariable('ES_TARGET_ENROLLEE', 'Target Enrollee', 'arduino', allowed_values=('arduino', 'tizen', 'linux')))
117 #ES_ROLE is for specifying the role (Enrollee or Mediator) for which scons is being executed
118 help_vars.Add(EnumVariable('ES_ROLE', 'Target build mode', 'mediator', allowed_values=('mediator', 'enrollee')))
119 #ES_SOFT_MODE is for specifying MODE (Mode 1 : Enrollee with Soft AP or Mode 2 : Mediator with Soft AP)
120 help_vars.Add(EnumVariable('ES_SOFTAP_MODE', 'Target build mode', 'ENROLLEE_SOFTAP', allowed_values=('ENROLLEE_SOFTAP', 'MEDIATOR_SOFTAP')))
122 AddOption('--prefix',
128 help='installation prefix')
130 ######################################################################
131 # Platform(build target) specific options: SDK/NDK & toolchain
132 ######################################################################
133 targets_support_cc = ['linux', 'arduino', 'tizen']
135 if target_os in targets_support_cc:
136 # Set cross compile toolchain
137 help_vars.Add('TC_PREFIX', "Toolchain prefix (Generally only be required for cross-compiling)", os.environ.get('TC_PREFIX'))
138 help_vars.Add(PathVariable('TC_PATH',
139 'Toolchain path (Generally only be required for cross-compiling)',
140 os.environ.get('TC_PATH')))
142 if target_os in ['android', 'arduino']: # Android/Arduino always uses GNU compiler regardless of the host
143 env = Environment(variables = help_vars,
144 tools = ['gnulink', 'gcc', 'g++', 'ar', 'as', 'textfile']
147 env = Environment(variables = help_vars, tools = ['default', 'textfile'],
148 TARGET_ARCH = target_arch, TARGET_OS = target_os,
149 PREFIX = GetOption('prefix'),
150 LIB_INSTALL_DIR = ARGUMENTS.get('LIB_INSTALL_DIR') #for 64bit build
152 Help(help_vars.GenerateHelpText(env))
155 ************************************ Warning **********************************
156 * Enviornment variable TC_PREFIX/TC_PATH is set. It will change the default *
157 * toolchain, if it isn't what you expect you should unset it, otherwise it may*
158 * cause inexplicable errors. *
159 *******************************************************************************
161 if env.get('VERBOSE') == False:
162 env['CCCOMSTR'] = "Compiling $TARGET"
163 env['SHCCCOMSTR'] = "Compiling $TARGET"
164 env['CXXCOMSTR'] = "Compiling $TARGET"
165 env['SHCXXCOMSTR'] = "Compiling $TARGET"
166 env['LINKCOMSTR'] = "Linking $TARGET"
167 env['SHLINKCOMSTR'] = "Linking $TARGET"
168 env['ARCOMSTR'] = "Archiving $TARGET"
169 env['RANLIBCOMSTR'] = "Indexing Archive $TARGET"
171 if target_os in targets_support_cc:
172 prefix = env.get('TC_PREFIX')
173 tc_path = env.get('TC_PATH')
175 env.Replace(CC = prefix + env.get('CC', 'gcc'))
176 env.Replace(CXX = prefix + env.get('CXX', 'g++'))
177 env.Replace(AR = prefix + env.get('AR', 'ar'))
178 env.Replace(AS = prefix + env.get('AS', 'as'))
179 env.Replace(RANLIB = prefix + env.get('RANLIB', 'ranlib'))
182 env.PrependENVPath('PATH', tc_path)
183 sys_root = os.path.abspath(tc_path + '/../')
184 env.AppendUnique(CCFLAGS = ['--sysroot=' + sys_root])
185 env.AppendUnique(LINKFLAGS = ['--sysroot=' + sys_root])
187 if prefix or tc_path:
190 # If cross-compiling, honor environment settings for toolchain to avoid picking up native tools
191 if os.environ.get('PKG_CONFIG') != None:
192 env["ENV"]["PKG_CONFIG"] = os.environ.get("PKG_CONFIG")
193 if os.environ.get('PKG_CONFIG_PATH') != None:
194 env["ENV"]["PKG_CONFIG_PATH"] = os.environ.get("PKG_CONFIG_PATH")
195 if os.environ.get('PKG_CONFIG_SYSROOT_DIR') != None:
196 env["ENV"]["PKG_CONFIG_SYSROOT_DIR"] = os.environ.get("PKG_CONFIG_SYSROOT_DIR")
198 # Ensure scons be able to change its working directory
199 env.SConscriptChdir(1)
201 # Set the source directory and build directory
202 # Source directory: 'dir'
203 # Build directory: 'dir'/out/<target_os>/<target_arch>/<release or debug>/
205 # You can get the directory as following:
207 # env.get('BUILD_DIR')
209 def __set_dir(env, dir):
210 if not os.path.exists(dir + '/SConstruct'):
212 *************************************** Error *********************************
213 * The directory(%s) seems isn't a source code directory, no SConstruct file is
215 *******************************************************************************
219 if env.get('RELEASE'):
220 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/release/'
222 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/debug/'
223 env.VariantDir(build_dir, dir, duplicate=0)
225 env.Replace(BUILD_DIR = build_dir)
226 env.Replace(SRC_DIR = dir)
228 def __src_to_obj(env, src, home = ''):
229 obj = env.get('BUILD_DIR') + src.replace(home, '')
230 if env.get('OBJSUFFIX'):
231 obj += env.get('OBJSUFFIX')
232 return env.Object(obj, src)
234 def __install(ienv, targets, name):
235 i_n = ienv.Install(env.get('BUILD_DIR'), targets)
237 env.AppendUnique(TS = [name])
239 def __installlib(ienv, targets, name):
240 user_prefix = env.get('PREFIX')
242 user_lib = env.get('LIB_INSTALL_DIR')
244 i_n = ienv.Install(user_lib, targets)
246 i_n = ienv.Install(user_prefix + '/lib', targets)
247 ienv.Alias("install", i_n)
249 i_n = ienv.Install(env.get('BUILD_DIR'), targets)
250 ienv.Alias("install", i_n)
252 def __installbin(ienv, targets, name):
253 user_prefix = env.get('PREFIX')
255 i_n = ienv.Install(user_prefix + '/bin', targets)
256 ienv.Alias("install", i_n)
258 def __installheader(ienv, targets, dir, name):
259 user_prefix = env.get('PREFIX')
261 i_n = ienv.Install(user_prefix + '/include/' + dir ,targets)
263 i_n = ienv.Install(os.path.join(env.get('BUILD_DIR'), 'include', dir), targets)
264 ienv.Alias("install", i_n)
266 def __installpcfile(ienv, targets, name):
267 user_prefix = env.get('PREFIX')
269 user_lib = env.get('LIB_INSTALL_DIR')
271 i_n = ienv.Install(user_lib + '/pkgconfig', targets)
273 i_n = ienv.Install(user_prefix + '/lib/pkgconfig', targets)
275 i_n = ienv.Install(env.get('BUILD_DIR') + 'lib/pkgconfig', targets)
276 ienv.Alias("install", i_n)
278 def __append_target(ienv, name, targets = None):
280 env.Alias(name, targets)
281 env.AppendUnique(TS = [name])
283 def __print_targets(env):
285 ===============================================================================
287 for t in env.get('TS'):
290 \nDefault all targets will be built. You can specify the target to build:
292 $ scons [options] [target]
293 ===============================================================================
296 env.AddMethod(__set_dir, 'SetDir')
297 env.AddMethod(__print_targets, 'PrintTargets')
298 env.AddMethod(__src_to_obj, 'SrcToObj')
299 env.AddMethod(__append_target, 'AppendTarget')
300 env.AddMethod(__install, 'InstallTarget')
301 env.AddMethod(__installlib, 'UserInstallTargetLib')
302 env.AddMethod(__installbin, 'UserInstallTargetBin')
303 env.AddMethod(__installheader, 'UserInstallTargetHeader')
304 env.AddMethod(__installpcfile, 'UserInstallTargetPCFile')
305 env.SetDir(env.GetLaunchDir())
306 env['ROOT_DIR']=env.GetLaunchDir()+'/..'
310 ######################################################################
311 # Scons to generate the iotivity.pc file from iotivity.pc.in file
312 ######################################################################
313 pc_file = env.get('SRC_DIR') + '/iotivity.pc.in'
315 if env.get('ROUTING') == 'GW':
316 routing_define = 'ROUTING_GATEWAY'
317 elif env.get('ROUTING') == 'EP':
318 routing_define = 'ROUTING_EP'
320 user_prefix = env.get('PREFIX')
321 user_lib = env.get('LIB_INSTALL_DIR')
323 user_lib = '$${prefix}/lib'
326 pc_vars = {'\@PREFIX\@': user_prefix,
327 '\@EXEC_PREFIX\@':user_prefix,
328 '\@VERSION\@': '1.1.1',
329 '\@LIB_INSTALL_DIR\@': user_lib,
330 '\@ROUTING_DEFINE\@': routing_define
333 pc_vars = {'\@PREFIX\@': env.get('BUILD_DIR').encode('string_escape'),
334 '\@EXEC_PREFIX\@': env.get('BUILD_DIR').encode('string_escape'),
335 '\@VERSION\@': '1.1.1',
336 '\@LIB_INSTALL_DIR\@': user_lib,
337 '\@ROUTING_DEFINE\@': routing_define
340 env.Substfile(pc_file, SUBST_DICT = pc_vars)
342 ######################################################################
343 # Link scons to Yocto cross-toolchain ONLY when target_os is yocto
344 ######################################################################
345 if target_os == "yocto":
347 This code injects Yocto cross-compilation tools+flags into scons'
348 build environment in order to invoke the relevant tools while
353 CC = os.environ['CC']
354 target_prefix = CC.split()[0]
355 target_prefix = target_prefix[:len(target_prefix)-3]
356 tools = {"CC" : target_prefix+"gcc",
357 "CXX" : target_prefix+"g++",
358 "AS" : target_prefix+"as",
359 "LD" : target_prefix+"ld",
360 "GDB" : target_prefix+"gdb",
361 "STRIP" : target_prefix+"strip",
362 "RANLIB" : target_prefix+"ranlib",
363 "OBJCOPY" : target_prefix+"objcopy",
364 "OBJDUMP" : target_prefix+"objdump",
365 "AR" : target_prefix+"ar",
366 "NM" : target_prefix+"nm",
368 "STRINGS": target_prefix+"strings"}
369 PATH = os.environ['PATH'].split(os.pathsep)
371 if tool in os.environ:
373 if os.path.isfile(os.path.join(path, tools[tool])):
374 env[tool] = os.path.join(path, os.environ[tool])
376 env['CROSS_COMPILE'] = target_prefix[:len(target_prefix) - 1]
378 print "ERROR in Yocto cross-toolchain environment"
381 Now reset TARGET_OS to linux so that all linux specific build configurations
382 hereupon apply for the entirety of the build process.
384 env['TARGET_OS'] = 'linux'
386 We want to preserve debug symbols to allow BitBake to generate both DEBUG and
387 RELEASE packages for OIC.
389 env.AppendUnique(CCFLAGS = ['-g'])
391 Additional flags to pass to the Yocto toolchain.
393 if env.get('RELEASE'):
394 env.AppendUnique(CPPDEFINES = ['NDEBUG'])
395 if env.get('LOGGING'):
396 env.AppendUnique(CPPDEFINES = ['TB_LOG'])
397 env.AppendUnique(CPPDEFINES = ['WITH_POSIX', '__linux__', '_GNU_SOURCE'])
398 env.AppendUnique(CFLAGS = ['-std=gnu99'])
399 env.AppendUnique(CCFLAGS = ['-Wall', '-Wextra', '-fPIC'])
400 env.AppendUnique(LIBS = ['dl', 'pthread', 'uuid'])
404 If target_os is not Yocto, continue with the regular build process
406 # Load config of target os
407 env.SConscript(target_os + '/SConscript')
409 # Delete the temp files of configuration
410 if env.GetOption('clean'):
411 dir = env.get('SRC_DIR')
413 if os.path.exists(dir + '/config.log'):
414 Execute(Delete(dir + '/config.log'))
415 if os.path.exists(dir + '/.sconsign.dblite'):
416 Execute(Delete(dir + '/.sconsign.dblite'))
417 if os.path.exists(dir + '/.sconf_temp'):
418 Execute(Delete(dir + '/.sconf_temp'))
420 ######################################################################
421 # Check for PThreads support
422 ######################################################################
423 import iotivityconfig
424 from iotivityconfig import *
426 conf = Configure(env,
429 'CheckPThreadsSupport' : iotivityconfig.check_pthreads
432 # Identify whether we have pthreads support, which is necessary for
433 # threading and mutexes. This will set the environment variable
434 # POSIX_SUPPORTED, 1 if it is supported, 0 otherwise
435 conf.CheckPThreadsSupport()
437 ######################################################################
438 # Generate macros for presence of headers
439 ######################################################################
440 cxx_headers = ['arpa/inet.h',
467 if target_os == 'arduino':
468 # Detection of headers on the Arduino platform is currently broken.
471 if target_os == 'msys_nt':
472 # WinPThread provides a pthread.h, but we want to use native threads.
473 cxx_headers.remove('pthread.h')
475 def get_define_from_header_file(header_file):
476 header_file_converted = header_file.replace("/","_").replace(".","_").upper()
477 return "HAVE_" + header_file_converted
479 for header_file_name in cxx_headers:
480 if conf.CheckCXXHeader(header_file_name):
481 conf.env.AppendUnique(CPPDEFINES = [get_define_from_header_file(header_file_name)])
484 ######################################################################
486 env.SConscript('external_libs.scons')