Build system base on scons(oic-resource)
[platform/upstream/iotivity.git] / build_common / SConscript
1 ##
2 # This script includes generic build options:
3 #    release/debug, build target, CPU ARCH, toolchain, build environment etc
4 ##
5 import os
6 import platform
7
8 host = platform.system()
9 default_arch = platform.machine()
10
11 if host not in ['Linux', 'Windows', 'Darwin']:
12         host = 'Linux'
13
14 # Map of host and allowed targets
15 allow_target_map = {
16                 'Linux': ['Linux', 'Android', 'Arduino'],
17                 'Windows': ['Windows', 'WinRT', 'Android', 'Arduino'],
18                 'Darwin': ['Darwin', 'IOS', 'Android', 'Arduino'],
19                 }
20
21 # Map of platform(target) and allowed archs
22 allow_arch_map = {
23                 'linux': ['x86', 'x86_64', 'arm', 'arm64'],
24                 'android': ['x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'armeabi-v7a-hard', 'arm64-v8a'],
25                 'windows': ['x86', 'amd64', 'arm'],
26                 'winrt': ['arm'],
27                 'darwin': ['i386', 'x86_64'],
28                 'ios': ['i386', 'x86_64', 'armv7', 'armv7s', 'arm64'],
29                 'arduino': ['avr', 'arm'],
30                 }
31
32 ######################################################################
33 # Get build options (the optins from command line)
34 ######################################################################
35 BUILD_TARGET = ARGUMENTS.get('BUILD_TARGET', host).lower() # target platform
36
37 if not allow_arch_map.has_key(BUILD_TARGET):
38         print "\nError: Unknown target platform: %s (Allow values: %s)\n" % (BUILD_TARGET, allow_target_map[host])
39         Exit(1)
40
41 if default_arch not in allow_arch_map[BUILD_TARGET]:
42         default_arch = allow_arch_map[BUILD_TARGET][0]
43         default_arch = default_arch.lower()
44
45 TARGET_CPU_ARCH = ARGUMENTS.get('CPU_ARCH', default_arch) # target CPU ARCH
46 ANDROID_NDK = ARGUMENTS.get('ANDROID_NDK', os.environ.get('ANDROID_NDK')) # ANDROID NDK base directory
47 SYS_VERSION = ARGUMENTS.get('SYS_VERSION', os.environ.get('SYS_VERSION'))  # OSX/IOS version
48 ARDUINO_HOME = ARGUMENTS.get('ARDUINO_HOME', os.environ.get('ARDUINO_HOME')) # ARDUINO root directory
49
50 ######################################################################
51 # Common build options (release, build target, CPU)
52 ######################################################################
53 vars = Variables()
54 vars.Add(BoolVariable('RELEASE', 'Build for release?', True)) # set to 'no', 'false' or 0 for debug
55 vars.Add(EnumVariable('BUILD_TARGET', 'Target platform', host, allow_target_map[host]))
56 vars.Add(EnumVariable('CPU_ARCH', 'Target CPU ARCH', default_arch, allow_arch_map[BUILD_TARGET]))
57
58 ######################################################################
59 # Platform(build target) specific options: SDK/NDK & toolchain
60 ######################################################################
61 targets_support_cc = ['linux', 'arduino']
62
63 if BUILD_TARGET == 'android':
64         vars.Add(PathVariable('ANDROID_NDK', 'Android NDK root directory', os.environ.get('ANDROID_NDK')))
65
66 elif BUILD_TARGET in ['darwin', 'ios']:
67         vars.Add('SYS_VERSION', 'MAC OS X version / IOS version', os.environ.get('SYS_VERSION'))
68
69 elif BUILD_TARGET == 'arduino':
70         vars.Add(PathVariable('ARDUINO_HOME', 'ARDUINO root directory', os.environ.get('ARDUINO_HOME')))
71
72 if BUILD_TARGET in targets_support_cc:
73         # Set cross compile toolchain
74         vars.Add('TC_PREFIX', "Toolchain prefix (Generally only be required for cross-compiling)", os.environ.get('TC_PREFIX'))
75         vars.Add(PathVariable('TC_PATH',
76                         'Toolchain path (Generally only be required for cross-compiling)',
77                         os.environ.get('TC_PATH')))
78
79 if BUILD_TARGET == 'android': # Android always uses GNU compiler regardless of the host
80         env = Environment(variables = vars,
81                         tools = ['gnulink', 'gcc', 'g++', 'ar', 'as']
82                         )
83 else:
84         env = Environment(variables = vars, TARGET_ARCH = TARGET_CPU_ARCH, TARGET_OS = BUILD_TARGET)
85
86 Help(vars.GenerateHelpText(env))
87
88 RELEASE_BUILD = env.get('RELEASE') # Whethere is release build, True: release, False: debug
89
90 tc_set_msg = '''
91 ************************************ Warning **********************************
92 *   Enviornment variable TC_PREFIX/TC_PATH is set. It will change the default *
93 * toolchain, if it isn't what you expect you should unset it, otherwise it may*
94 * cause inexplicable errors.                                                  *
95 *******************************************************************************
96 '''
97
98 if BUILD_TARGET in targets_support_cc:
99         prefix = ARGUMENTS.get('TC_PREFIX', os.environ.get('TC_PREFIX'))
100         tc_path = ARGUMENTS.get('TC_PATH', os.environ.get('TC_PATH'))
101         if prefix:
102                 env.Replace(CC = prefix + 'gcc')
103                 env.Replace(CXX = prefix + 'g++')
104                 env.Replace(AR = prefix + 'ar')
105                 env.Replace(AS = prefix + 'as')
106                 env.Replace(LINK = prefix + 'ld')
107                 env.Replace(RANLIB = prefix + 'ranlib')
108
109         if tc_path:
110                 env.PrependENVPath('PATH', tc_path)
111                 sys_root = os.path.abspath(tc_path + '/../')
112                 env.AppendUnique(CFLAGS = ['--sysroot=' + sys_root])
113                 env.AppendUnique(CXXFLAGS = ['--sysroot=' + sys_root])
114                 env.AppendUnique(LINKFLAGS = ['--sysroot=' + sys_root])
115
116         if prefix or tc_path:
117                 print tc_set_msg
118
119 # Ensure scons be able to change its working directory
120 env.SConscriptChdir(1)
121
122 Export('env', 'RELEASE_BUILD', 'BUILD_TARGET', 'TARGET_CPU_ARCH',
123                 'ANDROID_NDK', 'SYS_VERSION', 'ARDUINO_HOME')
124
125 # Load config of specific platform(build target)
126 env.SConscript(BUILD_TARGET + '/SConscript')
127
128 Return('env')