2 # This script includes generic build options:
3 # release/debug, build target, CPU ARCH, toolchain, build environment etc
8 host = platform.system()
9 default_arch = platform.machine()
11 if host not in ['Linux', 'Windows', 'Darwin']:
14 # Map of host and allowed targets
16 'Linux': ['Linux', 'Android', 'Arduino'],
17 'Windows': ['Windows', 'WinRT', 'Android', 'Arduino'],
18 'Darwin': ['Darwin', 'IOS', 'Android', 'Arduino'],
21 # Map of platform(target) and allowed archs
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'],
27 'darwin': ['i386', 'x86_64'],
28 'ios': ['i386', 'x86_64', 'armv7', 'armv7s', 'arm64'],
29 'arduino': ['avr', 'arm'],
32 ######################################################################
33 # Get build options (the optins from command line)
34 ######################################################################
35 BUILD_TARGET = ARGUMENTS.get('BUILD_TARGET', host).lower() # target platform
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])
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()
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
50 ######################################################################
51 # Common build options (release, build target, CPU)
52 ######################################################################
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]))
58 ######################################################################
59 # Platform(build target) specific options: SDK/NDK & toolchain
60 ######################################################################
61 targets_support_cc = ['linux', 'arduino']
63 if BUILD_TARGET == 'android':
64 vars.Add(PathVariable('ANDROID_NDK', 'Android NDK root directory', os.environ.get('ANDROID_NDK')))
66 elif BUILD_TARGET in ['darwin', 'ios']:
67 vars.Add('SYS_VERSION', 'MAC OS X version / IOS version', os.environ.get('SYS_VERSION'))
69 elif BUILD_TARGET == 'arduino':
70 vars.Add(PathVariable('ARDUINO_HOME', 'ARDUINO root directory', os.environ.get('ARDUINO_HOME')))
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')))
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']
84 env = Environment(variables = vars, TARGET_ARCH = TARGET_CPU_ARCH, TARGET_OS = BUILD_TARGET)
86 Help(vars.GenerateHelpText(env))
88 RELEASE_BUILD = env.get('RELEASE') # Whethere is release build, True: release, False: debug
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 *******************************************************************************
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'))
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')
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])
116 if prefix or tc_path:
119 # Ensure scons be able to change its working directory
120 env.SConscriptChdir(1)
122 Export('env', 'RELEASE_BUILD', 'BUILD_TARGET', 'TARGET_CPU_ARCH',
123 'ANDROID_NDK', 'SYS_VERSION', 'ARDUINO_HOME')
125 # Load config of specific platform(build target)
126 env.SConscript(BUILD_TARGET + '/SConscript')