Tizen build system using GBS.
[platform/upstream/iotivity.git] / build_common / SConscript
index b3dd335..6a31598 100644 (file)
 ##
 # This script includes generic build options:
-#    release/debug, build target, CPU ARCH, toolchain, build environment etc
+#    release/debug, target os, target arch, cross toolchain, build environment etc
 ##
 import os
 import platform
 
-host = platform.system()
-default_arch = platform.machine()
-
-if host not in ['Linux', 'Windows', 'Darwin']:
-       host = 'Linux'
-
-# Map of host and allowed targets
-allow_target_map = {
-               'Linux': ['Linux', 'Android', 'Arduino'],
-               'Windows': ['Windows', 'WinRT', 'Android', 'Arduino'],
-               'Darwin': ['Darwin', 'IOS', 'Android', 'Arduino'],
+# Map of host os and allowed target os (host: allowed target os)
+host_target_map = {
+               'linux': ['linux', 'android', 'arduino', 'yocto', 'tizen'],
+               'windows': ['windows', 'winrt', 'android', 'arduino'],
+               'darwin': ['darwin', 'ios', 'android', 'arduino'],
                }
 
-# Map of platform(target) and allowed archs
-allow_arch_map = {
+# Map of os and allowed archs (os: allowed archs)
+os_arch_map = {
                'linux': ['x86', 'x86_64', 'arm', 'arm64'],
+               'tizen': ['x86', 'x86_64', 'arm', 'arm64', 'armeabi-v7a'],
                'android': ['x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'armeabi-v7a-hard', 'arm64-v8a'],
                'windows': ['x86', 'amd64', 'arm'],
                'winrt': ['arm'],
                'darwin': ['i386', 'x86_64'],
                'ios': ['i386', 'x86_64', 'armv7', 'armv7s', 'arm64'],
                'arduino': ['avr', 'arm'],
+               'yocto': ['x86', 'x86_64'],
                }
 
+host = platform.system().lower()
+
+if not host_target_map.has_key(host):
+       print "\nError: Current system (%s) isn't supported\n" % host
+       Exit(1)
+
 ######################################################################
 # Get build options (the optins from command line)
 ######################################################################
-BUILD_TARGET = ARGUMENTS.get('BUILD_TARGET', host).lower() # target platform
+target_os = ARGUMENTS.get('TARGET_OS', host).lower() # target os
 
-if not allow_arch_map.has_key(BUILD_TARGET):
-       print "\nError: Unknown target platform: %s (Allow values: %s)\n" % (BUILD_TARGET, allow_target_map[host])
+if target_os not in host_target_map[host]:
+       print "\nError: Unknown target os: %s (Allow values: %s)\n" % (target_os, host_target_map[host])
        Exit(1)
 
-if default_arch not in allow_arch_map[BUILD_TARGET]:
-       default_arch = allow_arch_map[BUILD_TARGET][0]
-       default_arch = default_arch.lower()
+default_arch = platform.machine()
+if default_arch not in os_arch_map[target_os]:
+       default_arch = os_arch_map[target_os][0].lower()
+
+target_arch = ARGUMENTS.get('TARGET_ARCH', default_arch) # target arch
+
+# True if binary needs to be installed on board. (Might need root permissions)
+# set to 'no', 'false' or 0 for only compilation
+require_upload = ARGUMENTS.get('UPLOAD', False) 
+
+if ARGUMENTS.get('TEST'):
+       logging_default = False
+else:
+       logging_default = (ARGUMENTS.get('RELEASE', True) == 'false')
 
-TARGET_CPU_ARCH = ARGUMENTS.get('CPU_ARCH', default_arch) # target CPU ARCH
-ANDROID_NDK = ARGUMENTS.get('ANDROID_NDK', os.environ.get('ANDROID_NDK')) # ANDROID NDK base directory
-SYS_VERSION = ARGUMENTS.get('SYS_VERSION', os.environ.get('SYS_VERSION'))  # OSX/IOS version
-ARDUINO_HOME = ARGUMENTS.get('ARDUINO_HOME', os.environ.get('ARDUINO_HOME')) # ARDUINO root directory
 
-######################################################################
-# Common build options (release, build target, CPU)
-######################################################################
-vars = Variables()
-vars.Add(BoolVariable('RELEASE', 'Build for release?', True)) # set to 'no', 'false' or 0 for debug
-vars.Add(EnumVariable('BUILD_TARGET', 'Target platform', host, allow_target_map[host]))
-vars.Add(EnumVariable('CPU_ARCH', 'Target CPU ARCH', default_arch, allow_arch_map[BUILD_TARGET]))
 
 ######################################################################
-# Platform(build target) specific options: SDK/NDK & toolchain
+# Common build options (release, target os, target arch)
 ######################################################################
-targets_support_cc = ['linux', 'arduino']
+targets_disallow_multitransport = ['arduino']
 
-if BUILD_TARGET == 'android':
-       vars.Add(PathVariable('ANDROID_NDK', 'Android NDK root directory', os.environ.get('ANDROID_NDK')))
+help_vars = Variables()
+help_vars.Add(BoolVariable('VERBOSE', 'Show compilation', False))
+help_vars.Add(BoolVariable('RELEASE', 'Build for release?', True)) # set to 'no', 'false' or 0 for debug
+help_vars.Add(EnumVariable('TARGET_OS', 'Target platform', host, host_target_map[host]))
 
-elif BUILD_TARGET in ['darwin', 'ios']:
-       vars.Add('SYS_VERSION', 'MAC OS X version / IOS version', os.environ.get('SYS_VERSION'))
+if target_os in targets_disallow_multitransport:
+    help_vars.Add(ListVariable('TARGET_TRANSPORT', 'Target transport', 'ETHERNET', ['WIFI', 'BT', 'BLE', 'ETHERNET']))
+else:
+    help_vars.Add(ListVariable('TARGET_TRANSPORT', 'Target transport', 'ALL', ['ALL', 'WIFI', 'BT', 'BLE', 'ETHERNET']))
 
-elif BUILD_TARGET == 'arduino':
-       vars.Add(PathVariable('ARDUINO_HOME', 'ARDUINO root directory', os.environ.get('ARDUINO_HOME')))
+help_vars.Add(EnumVariable('TARGET_ARCH', 'Target architecture', default_arch, os_arch_map[target_os]))
+help_vars.Add(EnumVariable('SECURED', 'Build with DTLS', '0', allowed_values=('0', '1')))
+help_vars.Add(EnumVariable('TEST', 'Run unit tests', '0', allowed_values=('0', '1')))
+help_vars.Add(BoolVariable('LOGGING', 'Enable stack logging', logging_default))
+help_vars.Add(BoolVariable('UPLOAD', 'Upload binary ? (For Arduino)', require_upload))
+help_vars.Add(EnumVariable('BUILD_SAMPLE', 'Build with sample', 'ON', allowed_values=('ON', 'OFF')))
+######################################################################
+# Platform(build target) specific options: SDK/NDK & toolchain
+######################################################################
+targets_support_cc = ['linux', 'arduino', 'tizen']
 
-if BUILD_TARGET in targets_support_cc:
+if target_os in targets_support_cc:
        # Set cross compile toolchain
-       vars.Add('TC_PREFIX', "Toolchain prefix (Generally only be required for cross-compiling)", os.environ.get('TC_PREFIX'))
-       vars.Add(PathVariable('TC_PATH',
+       help_vars.Add('TC_PREFIX', "Toolchain prefix (Generally only be required for cross-compiling)", os.environ.get('TC_PREFIX'))
+       help_vars.Add(PathVariable('TC_PATH',
                        'Toolchain path (Generally only be required for cross-compiling)',
                        os.environ.get('TC_PATH')))
 
-if BUILD_TARGET == 'android': # Android always uses GNU compiler regardless of the host
-       env = Environment(variables = vars,
+if target_os in ['android', 'arduino']: # Android/Arduino always uses GNU compiler regardless of the host
+       env = Environment(variables = help_vars,
                        tools = ['gnulink', 'gcc', 'g++', 'ar', 'as']
                        )
 else:
-       env = Environment(variables = vars, TARGET_ARCH = TARGET_CPU_ARCH, TARGET_OS = BUILD_TARGET)
+       env = Environment(variables = help_vars, TARGET_ARCH = target_arch, TARGET_OS = target_os)
 
-Help(vars.GenerateHelpText(env))
-
-RELEASE_BUILD = env.get('RELEASE') # Whethere is release build, True: release, False: debug
+Help(help_vars.GenerateHelpText(env))
 
 tc_set_msg = '''
 ************************************ Warning **********************************
@@ -94,10 +106,16 @@ tc_set_msg = '''
 * cause inexplicable errors.                                                  *
 *******************************************************************************
 '''
+if env.get('VERBOSE') == False:
+       env['CCCOMSTR'] = "Compiling $TARGET"
+       env['CXXCOMSTR'] = "Compiling $TARGET"
+       env['LINKCOMSTR'] = "Linking $TARGET"
+       env['ARCOMSTR'] = "Archiving $TARGET"
+       env['RANLIBCOMSTR'] = "Indexing Archive $TARGET"
 
-if BUILD_TARGET in targets_support_cc:
-       prefix = ARGUMENTS.get('TC_PREFIX', os.environ.get('TC_PREFIX'))
-       tc_path = ARGUMENTS.get('TC_PATH', os.environ.get('TC_PATH'))
+if target_os in targets_support_cc:
+       prefix = env.get('TC_PREFIX')
+       tc_path = env.get('TC_PATH')
        if prefix:
                env.Replace(CC = prefix + 'gcc')
                env.Replace(CXX = prefix + 'g++')
@@ -109,8 +127,7 @@ if BUILD_TARGET in targets_support_cc:
        if tc_path:
                env.PrependENVPath('PATH', tc_path)
                sys_root = os.path.abspath(tc_path + '/../')
-               env.AppendUnique(CFLAGS = ['--sysroot=' + sys_root])
-               env.AppendUnique(CXXFLAGS = ['--sysroot=' + sys_root])
+               env.AppendUnique(CCFLAGS = ['--sysroot=' + sys_root])
                env.AppendUnique(LINKFLAGS = ['--sysroot=' + sys_root])
 
        if prefix or tc_path:
@@ -119,10 +136,137 @@ if BUILD_TARGET in targets_support_cc:
 # Ensure scons be able to change its working directory
 env.SConscriptChdir(1)
 
-Export('env', 'RELEASE_BUILD', 'BUILD_TARGET', 'TARGET_CPU_ARCH',
-               'ANDROID_NDK', 'SYS_VERSION', 'ARDUINO_HOME')
+# Set the source directory and build directory
+#   Source directory: 'dir'
+#   Build directory: 'dir'/out/<target_os>/<target_arch>/<release or debug>/
+#
+# You can get the directory as following:
+#   env.get('SRC_DIR')
+#   env.get('BUILD_DIR')
+
+def __set_dir(env, dir):
+       if not os.path.exists(dir + '/SConstruct'):
+               print '''
+*************************************** Error *********************************
+* The directory(%s) seems isn't a source code directory, no SConstruct file is
+* found. *
+*******************************************************************************
+''' % dir
+               Exit(1)
+
+       if env.get('RELEASE'):
+               build_dir = dir + '/out/' + target_os + '/' + target_arch + '/release/'
+       else:
+               build_dir = dir + '/out/' + target_os + '/' + target_arch + '/debug/'
+       env.VariantDir(build_dir, dir, duplicate=0)
+
+       env.Replace(BUILD_DIR = build_dir)
+       env.Replace(SRC_DIR = dir)
+
+def __src_to_obj(env, src, home = ''):
+       obj = env.get('BUILD_DIR') + src.replace(home, '')
+       if env.get('OBJSUFFIX'):
+               obj += env.get('OBJSUFFIX')
+       return env.Object(obj, src)
+
+def __install(ienv, targets, name):
+       i_n = ienv.Install(env.get('BUILD_DIR'), targets)
+       Alias(name, i_n)
+       env.AppendUnique(TS = [name])
+
+def __append_target(ienv, target):
+       env.AppendUnique(TS = [target])
+
+def __print_targets(env):
+       Help('''
+===============================================================================
+Targets:\n    ''')
+       for t in env.get('TS'):
+               Help(t + ' ')
+       Help('''
+\nDefault all targets will be built. You can specify the target to build:
+
+    $ scons [options] [target]
+===============================================================================
+''')
+
+env.AddMethod(__set_dir, 'SetDir')
+env.AddMethod(__print_targets, 'PrintTargets')
+env.AddMethod(__src_to_obj, 'SrcToObj')
+env.AddMethod(__append_target, 'AppendTarget')
+env.AddMethod(__install, 'InstallTarget')
+env.SetDir(env.GetLaunchDir())
+env['ROOT_DIR']=env.GetLaunchDir()+'/..'
+
+Export('env')
+
+######################################################################
+# Link scons to Yocto cross-toolchain ONLY when target_os is yocto
+######################################################################
+if target_os == "yocto":
+    '''
+    This code injects Yocto cross-compilation tools+flags into scons' 
+    build environment in order to invoke the relevant tools while 
+    performing a build.
+    '''
+    import os.path
+    try:
+        CC = os.environ['CC']
+        target_prefix = CC.split()[0]
+        target_prefix = target_prefix[:len(target_prefix)-3]
+        tools = {"CC" : target_prefix+"gcc",
+                "CXX" : target_prefix+"g++",
+                "AS" : target_prefix+"as",
+                "LD" : target_prefix+"ld",
+                "GDB" : target_prefix+"gdb",
+                "STRIP" : target_prefix+"strip",
+                "RANLIB" : target_prefix+"ranlib",
+                "OBJCOPY" : target_prefix+"objcopy",
+                "OBJDUMP" : target_prefix+"objdump",
+                "AR" : target_prefix+"ar",
+                "NM" : target_prefix+"nm",
+                "M4" : "m4",
+                "STRINGS": target_prefix+"strings"}
+        PATH = os.environ['PATH'].split(os.pathsep)
+        for tool in tools:
+            if tool in os.environ:
+                for path in PATH:
+                    if os.path.isfile(os.path.join(path, tools[tool])):
+                        env[tool] = os.path.join(path, os.environ[tool])
+                        break
+    except:
+        print "ERROR in Yocto cross-toolchain environment"
+        Exit(1)
+    '''
+    Now reset TARGET_OS to linux so that all linux specific build configurations
+    hereupon apply for the entirety of the build process.
+    '''
+    env['TARGET_OS'] = 'linux'
+    '''
+    We want to preserve debug symbols to allow BitBake to generate both DEBUG and
+    RELEASE packages for OIC. 
+    '''
+    env['CCFLAGS'].append('-g')
+    Export('env')
+else:
+    '''
+    If target_os is not Yocto, continue with the regular build process
+    '''
+    # Load config of target os
+    if target_os in ['linux', 'tizen']:
+               env.SConscript('linux/SConscript')
+    else:
+               env.SConscript(target_os + '/SConscript')
+
+env.SConscript('external_libs.scons')
+
+# Delete the temp files of configuration
+if env.GetOption('clean'):
+       dir = env.get('SRC_DIR')
 
-# Load config of specific platform(build target)
-env.SConscript(BUILD_TARGET + '/SConscript')
+       if os.path.exists(dir + '/config.log'):
+               Execute(Delete(dir + '/config.log'))
+               Execute(Delete(dir + '/.sconsign.dblite'))
+               Execute(Delete(dir + '/.sconf_temp'))
 
-Return('env')
\ No newline at end of file
+Return('env')