Merge "Update build scripts"
[platform/upstream/iotivity.git] / build_common / SConscript
1 ##
2 # This script includes generic build options:
3 #    release/debug, target os, target arch, cross toolchain, build environment etc
4 ##
5 import os
6 import platform
7
8 # Map of host os and allowed target os (host: allowed target os)
9 host_target_map = {
10                 'linux': ['linux', 'android', 'arduino'],
11                 'windows': ['windows', 'winrt', 'android', 'arduino'],
12                 'darwin': ['darwin', 'ios', 'android', 'arduino'],
13                 }
14
15 # Map of os and allowed archs (os: allowed archs)
16 os_arch_map = {
17                 'linux': ['x86', 'x86_64', 'arm', 'arm64'],
18                 'android': ['x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'armeabi-v7a-hard', 'arm64-v8a'],
19                 'windows': ['x86', 'amd64', 'arm'],
20                 'winrt': ['arm'],
21                 'darwin': ['i386', 'x86_64'],
22                 'ios': ['i386', 'x86_64', 'armv7', 'armv7s', 'arm64'],
23                 'arduino': ['avr', 'arm'],
24                 }
25
26 host = platform.system().lower()
27
28 if not host_target_map.has_key(host):
29         print "\nError: Current system (%s) isn't supported\n" % host
30         Exit(1)
31
32 ######################################################################
33 # Get build options (the optins from command line)
34 ######################################################################
35 target_os = ARGUMENTS.get('TARGET_OS', host).lower() # target os
36
37 if target_os not in host_target_map[host]:
38         print "\nError: Unknown target os: %s (Allow values: %s)\n" % (target_os, host_target_map[host])
39         Exit(1)
40
41 default_arch = platform.machine()
42 if default_arch not in os_arch_map[target_os]:
43         default_arch = os_arch_map[target_os][0].lower()
44
45 target_arch = ARGUMENTS.get('TARGET_ARCH', default_arch) # target arch
46
47 ######################################################################
48 # Common build options (release, target os, target arch)
49 ######################################################################
50 help_vars = Variables()
51 help_vars.Add(BoolVariable('RELEASE', 'Build for release?', True)) # set to 'no', 'false' or 0 for debug
52 help_vars.Add(EnumVariable('TARGET_OS', 'Target platform', host, host_target_map[host]))
53 help_vars.Add(EnumVariable('TARGET_ARCH', 'Target architecture', default_arch, os_arch_map[target_os]))
54
55 ######################################################################
56 # Platform(build target) specific options: SDK/NDK & toolchain
57 ######################################################################
58 targets_support_cc = ['linux', 'arduino']
59
60 if target_os in targets_support_cc:
61         # Set cross compile toolchain
62         help_vars.Add('TC_PREFIX', "Toolchain prefix (Generally only be required for cross-compiling)", os.environ.get('TC_PREFIX'))
63         help_vars.Add(PathVariable('TC_PATH',
64                         'Toolchain path (Generally only be required for cross-compiling)',
65                         os.environ.get('TC_PATH')))
66
67 if target_os in ['android', 'arduino']: # Android/Arduino always uses GNU compiler regardless of the host
68         env = Environment(variables = help_vars,
69                         tools = ['gnulink', 'gcc', 'g++', 'ar', 'as']
70                         )
71 else:
72         env = Environment(variables = help_vars, TARGET_ARCH = target_arch, TARGET_OS = target_os)
73
74 Help(help_vars.GenerateHelpText(env))
75
76 tc_set_msg = '''
77 ************************************ Warning **********************************
78 *   Enviornment variable TC_PREFIX/TC_PATH is set. It will change the default *
79 * toolchain, if it isn't what you expect you should unset it, otherwise it may*
80 * cause inexplicable errors.                                                  *
81 *******************************************************************************
82 '''
83
84 if target_os in targets_support_cc:
85         prefix = env.get('TC_PREFIX')
86         tc_path = env.get('TC_PATH')
87         if prefix:
88                 env.Replace(CC = prefix + 'gcc')
89                 env.Replace(CXX = prefix + 'g++')
90                 env.Replace(AR = prefix + 'ar')
91                 env.Replace(AS = prefix + 'as')
92                 env.Replace(LINK = prefix + 'ld')
93                 env.Replace(RANLIB = prefix + 'ranlib')
94
95         if tc_path:
96                 env.PrependENVPath('PATH', tc_path)
97                 sys_root = os.path.abspath(tc_path + '/../')
98                 env.AppendUnique(CCFLAGS = ['--sysroot=' + sys_root])
99                 env.AppendUnique(LINKFLAGS = ['--sysroot=' + sys_root])
100
101         if prefix or tc_path:
102                 print tc_set_msg
103
104 # Ensure scons be able to change its working directory
105 env.SConscriptChdir(1)
106
107 # Set the source directory and build directory
108 #   Source directory: 'dir'
109 #   Build directory: 'dir'/out/<target_os>/<target_arch>/<release or debug>/
110 #
111 # You can get the directory as following:
112 #   env.get('SRC_DIR')
113 #   env.get('BUILD_DIR')
114
115 def __set_dir(env, dir):
116         if not os.path.exists(dir + '/SConstruct'):
117                 print '''
118 *************************************** Error *********************************
119 * The directory(%s) seems isn't a source code directory, no SConstruct file is
120 * found. *
121 *******************************************************************************
122 ''' % dir
123                 Exit(1)
124
125         if env.get('RELEASE'):
126                 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/release/'
127         else:
128                 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/debug/'
129         env.VariantDir(build_dir, dir, duplicate=0)
130
131         env.Replace(BUILD_DIR = build_dir)
132         env.Replace(SRC_DIR = dir)
133
134 def __src_to_obj(env, src, home = ''):
135         obj = env.get('BUILD_DIR') + src.replace(home, '')
136         if env.get('OBJSUFFIX'):
137                 obj += env.get('OBJSUFFIX')
138         return env.Object(obj, src)
139
140 def __install(ienv, targets, name):
141         i_n = ienv.Install(env.get('BUILD_DIR'), targets)
142         Alias(name, i_n)
143         env.AppendUnique(TS = [name])
144
145 def __append_target(ienv, target):
146         env.AppendUnique(TS = [target])
147
148 def __print_targets(env):
149         Help('''
150 ===============================================================================
151 Targets:\n    ''')
152         for t in env.get('TS'):
153                 Help(t + ' ')
154         Help('''
155 \nDefault all targets will be built. You can specify the target to build:
156
157     $ scons [options] [target]
158 ===============================================================================
159 ''')
160
161 env.AddMethod(__set_dir, 'SetDir')
162 env.AddMethod(__print_targets, 'PrintTargets')
163 env.AddMethod(__src_to_obj, 'SrcToObj')
164 env.AddMethod(__append_target, 'AppendTarget')
165 env.AddMethod(__install, 'InstallTarget')
166 env.SetDir(env.GetLaunchDir())
167
168 Export('env')
169
170 # Load config of target os
171 env.SConscript(target_os + '/SConscript')
172
173 # Delete the temp files of configuration
174 if env.GetOption('clean'):
175         dir = env.get('SRC_DIR')
176
177         if os.path.exists(dir + '/config.log'):
178                 Execute(Delete(dir + '/config.log'))
179                 Execute(Delete(dir + '/.sconsign.dblite'))
180                 Execute(Delete(dir + '/.sconf_temp'))
181
182 Return('env')