blob: e7bfa4e05b45973e3ff2e81d1f84bde7b052df38 [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001# Copyright (c) 2016, 2017 ARM Limited.
2#
3# SPDX-License-Identifier: MIT
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to
7# deal in the Software without restriction, including without limitation the
8# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9# sell copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in all
13# copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
22
Anthony Barbierdbdab852017-06-23 15:42:00 +010023import SCons
Anthony Barbier46d59272017-05-04 09:15:15 +010024import os
Anthony Barbierdbdab852017-06-23 15:42:00 +010025import subprocess
26
27def version_at_least(version, required):
28 end = min(len(version), len(required))
29
30 for i in range(0, end, 2):
31 if int(version[i]) < int(required[i]):
32 return False
33 elif int(version[i]) > int(required[i]):
34 return True
35
36 return True
Anthony Barbier46d59272017-05-04 09:15:15 +010037
38vars = Variables("scons")
39vars.AddVariables(
40 BoolVariable("debug", "Debug", False),
41 BoolVariable("asserts", "Enable asserts (this flag is forced to 1 for debug=1)", False),
Anthony Barbier8140e1e2017-12-14 23:48:46 +000042 BoolVariable("logging", "Logging (this flag is forced to 1 for debug=1)", False),
Jenkins36ccc902020-02-21 11:10:48 +000043 EnumVariable("arch", "Target Architecture", "armv7a",
44 allowed_values=("armv7a", "arm64-v8a", "arm64-v8.2-a", "arm64-v8.2-a-sve", "x86_32", "x86_64",
45 "armv8a", "armv8.2-a", "armv8.2-a-sve", "armv8.6-a", "x86")),
46 EnumVariable("estate", "Execution State", "auto", allowed_values=("auto", "32", "64")),
Anthony Barbier46d59272017-05-04 09:15:15 +010047 EnumVariable("os", "Target OS", "linux", allowed_values=("linux", "android", "bare_metal")),
Anthony Barbier06ea0482018-02-22 15:45:35 +000048 EnumVariable("build", "Build type", "cross_compile", allowed_values=("native", "cross_compile", "embed_only")),
Anthony Barbierdbdab852017-06-23 15:42:00 +010049 BoolVariable("examples", "Build example programs", True),
Jenkins0e205f72019-11-28 16:53:35 +000050 BoolVariable("gemm_tuner", "Build gemm_tuner programs", True),
Anthony Barbier46d59272017-05-04 09:15:15 +010051 BoolVariable("Werror", "Enable/disable the -Werror compilation flag", True),
Kaizen8938bd32017-09-28 14:38:23 +010052 BoolVariable("standalone", "Builds the tests as standalone executables, links statically with libgcc, libstdc++ and libarm_compute", False),
Anthony Barbier46d59272017-05-04 09:15:15 +010053 BoolVariable("opencl", "Enable OpenCL support", True),
54 BoolVariable("neon", "Enable Neon support", False),
Anthony Barbier8140e1e2017-12-14 23:48:46 +000055 BoolVariable("gles_compute", "Enable OpenGL ES Compute Shader support", False),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000056 BoolVariable("embed_kernels", "Embed OpenCL kernels and OpenGL ES compute shaders in library binary", True),
Anthony Barbier46d59272017-05-04 09:15:15 +010057 BoolVariable("set_soname", "Set the library's soname and shlibversion (requires SCons 2.4 or above)", False),
58 BoolVariable("openmp", "Enable OpenMP backend", False),
59 BoolVariable("cppthreads", "Enable C++11 threads backend", True),
Anthony Barbierdbdab852017-06-23 15:42:00 +010060 PathVariable("build_dir", "Specify sub-folder for the build", ".", PathVariable.PathAccept),
Jenkinsb9abeae2018-11-22 11:58:08 +000061 PathVariable("install_dir", "Specify sub-folder for the install", "", PathVariable.PathAccept),
Jenkins514be652019-02-28 12:25:18 +000062 BoolVariable("exceptions", "Enable/disable C++ exception support", True),
Jenkins36ccc902020-02-21 11:10:48 +000063 PathVariable("linker_script", "Use an external linker script", "", PathVariable.PathAccept),
Jenkins514be652019-02-28 12:25:18 +000064 ("toolchain_prefix", "Override the toolchain prefix", ""),
Jenkins36ccc902020-02-21 11:10:48 +000065 ("compiler_prefix", "Override the compiler prefix", ""),
Jenkinsb3a371b2018-05-23 11:36:53 +010066 ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", ""),
Jenkinsb9abeae2018-11-22 11:58:08 +000067 ("extra_link_flags", "Extra LD flags to be appended to the build command", ""),
Jenkinsb3a371b2018-05-23 11:36:53 +010068 ("compiler_cache", "Command to prefix to the C and C++ compiler (e.g ccache)", "")
Anthony Barbier46d59272017-05-04 09:15:15 +010069)
70
Anthony Barbierdbdab852017-06-23 15:42:00 +010071env = Environment(platform="posix", variables=vars, ENV = os.environ)
Jenkinsb9abeae2018-11-22 11:58:08 +000072build_path = env['build_dir']
73# If build_dir is a relative path then add a #build/ prefix:
74if not env['build_dir'].startswith('/'):
75 SConsignFile('build/%s/.scons' % build_path)
76 build_path = "#build/%s" % build_path
77else:
78 SConsignFile('%s/.scons' % build_path)
79
80install_path = env['install_dir']
81#If the install_dir is a relative path then assume it's from inside build_dir
82if not env['install_dir'].startswith('/') and install_path != "":
83 install_path = "%s/%s" % (build_path, install_path)
84
85env.Append(LIBPATH = [build_path])
Anthony Barbier06ea0482018-02-22 15:45:35 +000086Export('env')
87Export('vars')
Anthony Barbierdbdab852017-06-23 15:42:00 +010088
Jenkinsb9abeae2018-11-22 11:58:08 +000089def install_lib( lib ):
90 # If there is no install folder, then there is nothing to do:
91 if install_path == "":
92 return lib
93 return env.Install( "%s/lib/" % install_path, lib)
94def install_bin( bin ):
95 # If there is no install folder, then there is nothing to do:
96 if install_path == "":
97 return bin
98 return env.Install( "%s/bin/" % install_path, bin)
99def install_include( inc ):
100 if install_path == "":
101 return inc
102 return env.Install( "%s/include/" % install_path, inc)
103
104Export('install_lib')
105Export('install_bin')
Anthony Barbier46d59272017-05-04 09:15:15 +0100106
107Help(vars.GenerateHelpText(env))
108
Jenkins36ccc902020-02-21 11:10:48 +0000109if env['linker_script'] and env['os'] != 'bare_metal':
110 print("Linker script is only supported for bare_metal builds")
111 Exit(1)
112
Anthony Barbier06ea0482018-02-22 15:45:35 +0000113if env['build'] == "embed_only":
Jenkinsb9abeae2018-11-22 11:58:08 +0000114 SConscript('./SConscript', variant_dir=build_path, duplicate=0)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000115 Return()
116
Anthony Barbierdbdab852017-06-23 15:42:00 +0100117if env['neon'] and 'x86' in env['arch']:
ggardete2542c92018-06-29 17:01:01 +0200118 print("Cannot compile NEON for x86")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100119 Exit(1)
120
121if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
ggardete2542c92018-06-29 17:01:01 +0200122 print("Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above")
123 print("Update your version of SCons or use set_soname=0")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100124 Exit(1)
125
126if env['os'] == 'bare_metal':
127 if env['cppthreads'] or env['openmp']:
128 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
129 Exit(1)
130
Jenkins514be652019-02-28 12:25:18 +0000131if not env['exceptions']:
132 if env['opencl'] or env['gles_compute']:
133 print("ERROR: OpenCL and GLES are not supported when building without exceptions. Use opencl=0 gles_compute=0")
134 Exit(1)
135
136 env.Append(CPPDEFINES = ['ARM_COMPUTE_EXCEPTIONS_DISABLED'])
137 env.Append(CXXFLAGS = ['-fno-exceptions'])
138
Jenkins0e205f72019-11-28 16:53:35 +0000139env.Append(CXXFLAGS = ['-Wall','-DARCH_ARM',
140 '-Wextra','-pedantic','-Wdisabled-optimization','-Wformat=2',
Anthony Barbierdbdab852017-06-23 15:42:00 +0100141 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
Jenkins36ccc902020-02-21 11:10:48 +0000142 '-std=gnu++11','-Woverloaded-virtual', '-Wformat-security',
Jenkins0e205f72019-11-28 16:53:35 +0000143 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-overlength-strings'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000144
Anthony Barbierdbdab852017-06-23 15:42:00 +0100145env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
146
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000147default_cpp_compiler = 'g++' if env['os'] != 'android' else 'clang++'
148default_c_compiler = 'gcc' if env['os'] != 'android' else 'clang'
149cpp_compiler = os.environ.get('CXX', default_cpp_compiler)
150c_compiler = os.environ.get('CC', default_c_compiler)
151
Jenkinsb3a371b2018-05-23 11:36:53 +0100152if env['os'] == 'android' and ( 'clang++' not in cpp_compiler or 'clang' not in c_compiler ):
ggardete2542c92018-06-29 17:01:01 +0200153 print( "WARNING: Only clang is officially supported to build the Compute Library for Android")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000154
Jenkinsb3a371b2018-05-23 11:36:53 +0100155if 'clang++' in cpp_compiler:
Jenkins0e205f72019-11-28 16:53:35 +0000156 env.Append(CXXFLAGS = ['-Wno-vla-extension'])
157elif 'armclang' in cpp_compiler:
158 pass
Anthony Barbierdbdab852017-06-23 15:42:00 +0100159else:
Jenkins0e205f72019-11-28 16:53:35 +0000160 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100161
162if env['cppthreads']:
163 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
164
165if env['openmp']:
Jenkinsb3a371b2018-05-23 11:36:53 +0100166 if 'clang++' in cpp_compiler:
ggardete2542c92018-06-29 17:01:01 +0200167 print( "Clang does not support OpenMP. Use scheduler=cpp.")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100168 Exit(1)
169
170 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
171 env.Append(CXXFLAGS = ['-fopenmp'])
172 env.Append(LINKFLAGS = ['-fopenmp'])
173
Jenkins36ccc902020-02-21 11:10:48 +0000174# Validate and define state
175if env['estate'] == 'auto':
176 if 'v7a' in env['arch']:
177 env['estate'] = '32'
178 else:
179 env['estate'] = '64'
180
181# Map legacy arch
182if 'arm64' in env['arch']:
183 env['estate'] = '64'
184
185if 'v7a' in env['estate'] and env['estate'] == '64':
186 print("ERROR: armv7a architecture has only 32-bit execution state")
187 Exit(1)
188
Jenkins0e205f72019-11-28 16:53:35 +0000189# Add architecture specific flags
Anthony Barbierdbdab852017-06-23 15:42:00 +0100190prefix = ""
Jenkins36ccc902020-02-21 11:10:48 +0000191if 'v7a' in env['arch']:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100192 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
Jenkins36ccc902020-02-21 11:10:48 +0000193 if env['os'] == 'android':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100194 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
Jenkinsb9abeae2018-11-22 11:58:08 +0000195 else:
Jenkins36ccc902020-02-21 11:10:48 +0000196 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
197elif 'v8' in env['arch']:
198 if 'sve' in env['arch']:
199 env.Append(CXXFLAGS = ['-march=armv8.2-a+sve+fp16+dotprod'])
200 elif 'v8.2-a' in env['arch']:
Jenkinsb9abeae2018-11-22 11:58:08 +0000201 env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16']) # explicitly enable fp16 extension otherwise __ARM_FEATURE_FP16_VECTOR_ARITHMETIC is undefined
Jenkins36ccc902020-02-21 11:10:48 +0000202 else:
203 env.Append(CXXFLAGS = ['-march=armv8-a'])
204
205 if 'v8.6-a' in env['arch']:
206 env.Append(CXXFLAGS = ['-DV8P6'])
207
208elif 'x86' in env['arch']:
209 if env['estate'] == '32':
210 env.Append(CCFLAGS = ['-m32'])
211 env.Append(LINKFLAGS = ['-m32'])
212 else:
213 env.Append(CXXFLAGS = ['-fPIC'])
214 env.Append(CCFLAGS = ['-m64'])
215 env.Append(LINKFLAGS = ['-m64'])
216
217# Define toolchain
218prefix = ""
219if 'x86' not in env['arch']:
220 if env['estate'] == '32':
221 if env['os'] == 'linux':
222 prefix = "arm-linux-gnueabihf-" if 'v7' in env['arch'] else "armv8l-linux-gnueabihf-"
223 elif env['os'] == 'bare_metal':
224 prefix = "arm-eabi-"
225 elif env['os'] == 'android':
226 prefix = "arm-linux-androideabi-"
227 elif env['estate'] == '64' and 'v8' in env['arch']:
228 if env['os'] == 'linux':
229 prefix = "aarch64-linux-gnu-"
230 elif env['os'] == 'bare_metal':
231 prefix = "aarch64-elf-"
232 elif env['os'] == 'android':
233 prefix = "aarch64-linux-android-"
Anthony Barbierdbdab852017-06-23 15:42:00 +0100234
235if env['build'] == 'native':
236 prefix = ""
237
Jenkins514be652019-02-28 12:25:18 +0000238if env["toolchain_prefix"] != "":
239 prefix = env["toolchain_prefix"]
240
Jenkins36ccc902020-02-21 11:10:48 +0000241compiler_prefix = prefix
242if env["compiler_prefix"] != "":
243 compiler_prefix = env["compiler_prefix"]
244
245env['CC'] = env['compiler_cache']+ " " + compiler_prefix + c_compiler
246env['CXX'] = env['compiler_cache']+ " " + compiler_prefix + cpp_compiler
Anthony Barbierdbdab852017-06-23 15:42:00 +0100247env['LD'] = prefix + "ld"
248env['AS'] = prefix + "as"
249env['AR'] = prefix + "ar"
250env['RANLIB'] = prefix + "ranlib"
Anthony Barbier46d59272017-05-04 09:15:15 +0100251
252if not GetOption("help"):
Anthony Barbierdbdab852017-06-23 15:42:00 +0100253 try:
bradford barr8d418ab2017-08-07 14:17:09 -0400254 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
Anthony Barbierdbdab852017-06-23 15:42:00 +0100255 except OSError:
256 print("ERROR: Compiler '%s' not found" % env['CXX'])
257 Exit(1)
258
Jenkins0e205f72019-11-28 16:53:35 +0000259 if 'armclang' in cpp_compiler:
260 pass
261 elif 'clang++' not in cpp_compiler:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100262 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
ggardete2542c92018-06-29 17:01:01 +0200263 print("GCC 6.2.1 or newer is required to compile armv8.2-a code")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100264 Exit(1)
265 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
ggardete2542c92018-06-29 17:01:01 +0200266 print("GCC 4.9 or newer is required to compile NEON code for AArch64")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100267 Exit(1)
268
269 if version_at_least(compiler_ver, '6.1'):
270 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
271
272 if compiler_ver == '4.8.3':
273 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
274
Kaizen8938bd32017-09-28 14:38:23 +0100275if env['standalone']:
276 env.Append(CXXFLAGS = ['-fPIC'])
277 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
278
Anthony Barbierdbdab852017-06-23 15:42:00 +0100279if env['Werror']:
280 env.Append(CXXFLAGS = ['-Werror'])
281
282if env['os'] == 'android':
283 env.Append(CPPDEFINES = ['ANDROID'])
Jenkins36ccc902020-02-21 11:10:48 +0000284 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++', '-ldl'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100285elif env['os'] == 'bare_metal':
286 env.Append(LINKFLAGS = ['-static'])
Kaizen8938bd32017-09-28 14:38:23 +0100287 env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100288 env.Append(CXXFLAGS = ['-fPIC'])
289 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
Kaizen8938bd32017-09-28 14:38:23 +0100290 env.Append(CPPDEFINES = ['BARE_METAL'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100291
292if env['opencl']:
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000293 if env['os'] in ['bare_metal'] or env['standalone']:
Jenkinsb3a371b2018-05-23 11:36:53 +0100294 print("Cannot link OpenCL statically, which is required for bare metal / standalone builds")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100295 Exit(1)
296
Jenkinsb3a371b2018-05-23 11:36:53 +0100297if env['gles_compute']:
298 if env['os'] in ['bare_metal'] or env['standalone']:
299 print("Cannot link OpenGLES statically, which is required for bare metal / standalone builds")
300 Exit(1)
301
302if env["os"] not in ["android", "bare_metal"] and (env['opencl'] or env['cppthreads']):
303 env.Append(LIBS = ['pthread'])
304
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000305if env['opencl'] or env['gles_compute']:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100306 if env['embed_kernels']:
307 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
308
309if env['debug']:
310 env['asserts'] = True
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000311 env['logging'] = True
Anthony Barbierdbdab852017-06-23 15:42:00 +0100312 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
313 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
314else:
Jenkins975dfe12019-09-02 11:47:54 +0100315 env.Append(CXXFLAGS = ['-O3'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100316
317if env['asserts']:
318 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
Kaizen8938bd32017-09-28 14:38:23 +0100319 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100320
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000321if env['logging']:
322 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
323
Anthony Barbierdbdab852017-06-23 15:42:00 +0100324env.Append(CPPPATH = ['#/include', "#"])
325env.Append(CXXFLAGS = env['extra_cxx_flags'])
Jenkinsb9abeae2018-11-22 11:58:08 +0000326env.Append(LINKFLAGS = env['extra_link_flags'])
327
328Default( install_include("arm_compute"))
329Default( install_include("support"))
Jenkins4ba87db2019-05-23 17:11:51 +0100330Default( install_include("utils"))
331for dirname in os.listdir("./include"):
332 Default( install_include("include/%s" % dirname))
Anthony Barbierdbdab852017-06-23 15:42:00 +0100333
Anthony Barbierdbdab852017-06-23 15:42:00 +0100334Export('version_at_least')
335
Anthony Barbierdbdab852017-06-23 15:42:00 +0100336
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000337if env['gles_compute'] and env['os'] != 'android':
338 env.Append(CPPPATH = ['#/include/linux'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000339
Jenkinsb9abeae2018-11-22 11:58:08 +0000340SConscript('./SConscript', variant_dir=build_path, duplicate=0)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000341
Jenkins36ccc902020-02-21 11:10:48 +0000342if env['examples'] and env['exceptions']:
343 if env['os'] == 'bare_metal' and env['arch'] == 'armv7a':
344 print("WARNING: Building examples for bare metal and armv7a is not supported. Use examples=0")
345 Return()
Jenkinsb9abeae2018-11-22 11:58:08 +0000346 SConscript('./examples/SConscript', variant_dir='%s/examples' % build_path, duplicate=0)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100347
Jenkins36ccc902020-02-21 11:10:48 +0000348if env['exceptions']:
349 if env['os'] == 'bare_metal' and env['arch'] == 'armv7a':
350 print("WARNING: Building tests for bare metal and armv7a is not supported")
351 Return()
Jenkinsb9abeae2018-11-22 11:58:08 +0000352 SConscript('./tests/SConscript', variant_dir='%s/tests' % build_path, duplicate=0)