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'],
11 'windows': ['windows', 'winrt', 'android', 'arduino'],
12 'darwin': ['darwin', 'ios', 'android', 'arduino'],
15 # Map of os and allowed archs (os: allowed archs)
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'],
21 'darwin': ['i386', 'x86_64'],
22 'ios': ['i386', 'x86_64', 'armv7', 'armv7s', 'arm64'],
23 'arduino': ['avr', 'arm'],
26 host = platform.system().lower()
28 if not host_target_map.has_key(host):
29 print "\nError: Current system (%s) isn't supported\n" % host
32 ######################################################################
33 # Get build options (the optins from command line)
34 ######################################################################
35 target_os = ARGUMENTS.get('TARGET_OS', host).lower() # target os
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])
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()
45 target_arch = ARGUMENTS.get('TARGET_ARCH', default_arch) # target arch
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]))
55 ######################################################################
56 # Platform(build target) specific options: SDK/NDK & toolchain
57 ######################################################################
58 targets_support_cc = ['linux', 'arduino']
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')))
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']
72 env = Environment(variables = help_vars, TARGET_ARCH = target_arch, TARGET_OS = target_os)
74 Help(help_vars.GenerateHelpText(env))
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 *******************************************************************************
84 if target_os in targets_support_cc:
85 prefix = env.get('TC_PREFIX')
86 tc_path = env.get('TC_PATH')
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')
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])
101 if prefix or tc_path:
104 # Ensure scons be able to change its working directory
105 env.SConscriptChdir(1)
107 # Set the source directory and build directory
108 # Source directory: 'dir'
109 # Build directory: 'dir'/out/<target_os>/<target_arch>/<release or debug>/
111 # You can get the directory as following:
113 # env.get('BUILD_DIR')
115 def __set_dir(env, dir):
116 if not os.path.exists(dir + '/SConstruct'):
118 *************************************** Error *********************************
119 * The directory(%s) seems isn't a source code directory, no SConstruct file is
121 *******************************************************************************
125 if env.get('RELEASE'):
126 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/release/'
128 build_dir = dir + '/out/' + target_os + '/' + target_arch + '/debug/'
129 env.VariantDir(build_dir, dir, duplicate=0)
131 env.Replace(BUILD_DIR = build_dir)
132 env.Replace(SRC_DIR = dir)
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)
140 def __install(ienv, targets, name):
141 i_n = ienv.Install(env.get('BUILD_DIR'), targets)
143 env.AppendUnique(TS = [name])
145 def __append_target(ienv, target):
146 env.AppendUnique(TS = [target])
148 def __print_targets(env):
150 ===============================================================================
152 for t in env.get('TS'):
155 \nDefault all targets will be built. You can specify the target to build:
157 $ scons [options] [target]
158 ===============================================================================
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())
170 # Load config of target os
171 env.SConscript(target_os + '/SConscript')
173 # Delete the temp files of configuration
174 if env.GetOption('clean'):
175 dir = env.get('SRC_DIR')
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'))