blob: 216920f05907866802e7d3b732c2af11a8ec8b2b [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001# 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
23import SCons
24import os
25import 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
37
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),
Georgios Pinitas3faea252017-10-30 14:13:50 +000042 BoolVariable("logging", "Logging (this flag is forced to 1 for debug=1)", False),
Georgios Pinitasf2cdce32019-12-09 18:35:57 +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", "armv8a", "armv8.2-a", "armv8.2-a-sve", "x86")),
45 EnumVariable("estate", "Execution State", "auto", allowed_values=("auto", "32", "64")),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046 EnumVariable("os", "Target OS", "linux", allowed_values=("linux", "android", "bare_metal")),
Anthony Barbier6a3daf12018-02-19 17:24:27 +000047 EnumVariable("build", "Build type", "cross_compile", allowed_values=("native", "cross_compile", "embed_only")),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048 BoolVariable("examples", "Build example programs", True),
SiCong Li8b4c7302019-09-19 12:18:15 +010049 BoolVariable("gemm_tuner", "Build gemm_tuner programs", True),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050 BoolVariable("Werror", "Enable/disable the -Werror compilation flag", True),
Pablo Telloc6cb35a2017-06-21 15:39:47 +010051 BoolVariable("standalone", "Builds the tests as standalone executables, links statically with libgcc, libstdc++ and libarm_compute", False),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010052 BoolVariable("opencl", "Enable OpenCL support", True),
53 BoolVariable("neon", "Enable Neon support", False),
Anthony Barbier7068f992017-10-26 15:23:08 +010054 BoolVariable("gles_compute", "Enable OpenGL ES Compute Shader support", False),
Anthony Barbiercc0a80b2017-12-15 11:37:29 +000055 BoolVariable("embed_kernels", "Embed OpenCL kernels and OpenGL ES compute shaders in library binary", True),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010056 BoolVariable("set_soname", "Set the library's soname and shlibversion (requires SCons 2.4 or above)", False),
57 BoolVariable("openmp", "Enable OpenMP backend", False),
58 BoolVariable("cppthreads", "Enable C++11 threads backend", True),
59 PathVariable("build_dir", "Specify sub-folder for the build", ".", PathVariable.PathAccept),
Anthony Barbier01bbd5f2018-11-01 15:10:51 +000060 PathVariable("install_dir", "Specify sub-folder for the install", "", PathVariable.PathAccept),
Michalis Spyrou323ce0f2018-11-30 16:30:43 +000061 BoolVariable("exceptions", "Enable/disable C++ exception support", True),
Anthony Barbiera4e5e1e2017-10-05 14:55:34 +010062 #FIXME Remove before release (And remove all references to INTERNAL_ONLY)
Georgios Pinitas37831162018-08-20 15:41:10 +010063 BoolVariable("internal_only", "Enable ARM internal only tests", False),
Anthony Barbier149de5b2018-12-06 10:27:37 +000064 ("toolchain_prefix", "Override the toolchain prefix", ""),
Anthony Barbier7390e052018-03-13 09:29:41 +000065 ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", ""),
Georgios Pinitas421405b2018-10-26 19:05:32 +010066 ("extra_link_flags", "Extra LD flags to be appended to the build command", ""),
Anthony Barbier7390e052018-03-13 09:29:41 +000067 ("compiler_cache", "Command to prefix to the C and C++ compiler (e.g ccache)", "")
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068)
69
70env = Environment(platform="posix", variables=vars, ENV = os.environ)
Anthony Barbiere8a55df2018-10-26 09:05:01 +010071build_path = env['build_dir']
72# If build_dir is a relative path then add a #build/ prefix:
73if not env['build_dir'].startswith('/'):
74 SConsignFile('build/%s/.scons' % build_path)
75 build_path = "#build/%s" % build_path
76else:
77 SConsignFile('%s/.scons' % build_path)
78
Anthony Barbier01bbd5f2018-11-01 15:10:51 +000079install_path = env['install_dir']
80#If the install_dir is a relative path then assume it's from inside build_dir
81if not env['install_dir'].startswith('/') and install_path != "":
82 install_path = "%s/%s" % (build_path, install_path)
83
Anthony Barbiere8a55df2018-10-26 09:05:01 +010084env.Append(LIBPATH = [build_path])
Anthony Barbier6a3daf12018-02-19 17:24:27 +000085Export('env')
86Export('vars')
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087
Anthony Barbier01bbd5f2018-11-01 15:10:51 +000088def install_lib( lib ):
89 # If there is no install folder, then there is nothing to do:
90 if install_path == "":
91 return lib
92 return env.Install( "%s/lib/" % install_path, lib)
93def install_bin( bin ):
94 # If there is no install folder, then there is nothing to do:
95 if install_path == "":
96 return bin
97 return env.Install( "%s/bin/" % install_path, bin)
98def install_include( inc ):
99 if install_path == "":
100 return inc
101 return env.Install( "%s/include/" % install_path, inc)
102
103Export('install_lib')
104Export('install_bin')
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
106Help(vars.GenerateHelpText(env))
107
Anthony Barbier6a3daf12018-02-19 17:24:27 +0000108if env['build'] == "embed_only":
Anthony Barbiere8a55df2018-10-26 09:05:01 +0100109 SConscript('./SConscript', variant_dir=build_path, duplicate=0)
Anthony Barbier6a3daf12018-02-19 17:24:27 +0000110 Return()
111
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112if env['neon'] and 'x86' in env['arch']:
ggardet767c9f72018-06-29 17:01:01 +0200113 print("Cannot compile NEON for x86")
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 Exit(1)
115
116if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
ggardet767c9f72018-06-29 17:01:01 +0200117 print("Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above")
118 print("Update your version of SCons or use set_soname=0")
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100119 Exit(1)
120
121if env['os'] == 'bare_metal':
122 if env['cppthreads'] or env['openmp']:
123 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
124 Exit(1)
125
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000126if not env['exceptions']:
127 if env['opencl'] or env['gles_compute']:
128 print("ERROR: OpenCL and GLES are not supported when building without exceptions. Use opencl=0 gles_compute=0")
129 Exit(1)
130
131 env.Append(CPPDEFINES = ['ARM_COMPUTE_EXCEPTIONS_DISABLED'])
132 env.Append(CXXFLAGS = ['-fno-exceptions'])
133
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100134env.Append(CXXFLAGS = ['-Wall','-DARCH_ARM',
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100135 '-Wextra','-pedantic','-Wdisabled-optimization','-Wformat=2',
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
Michalis Spyroufae513c2019-10-16 17:41:33 +0100137 '-fpermissive','-std=gnu++11','-Woverloaded-virtual', '-Wformat-security',
138 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-overlength-strings'])
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000139
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
141
Anthony Barbiera026e982018-01-18 10:57:52 +0000142default_cpp_compiler = 'g++' if env['os'] != 'android' else 'clang++'
143default_c_compiler = 'gcc' if env['os'] != 'android' else 'clang'
144cpp_compiler = os.environ.get('CXX', default_cpp_compiler)
145c_compiler = os.environ.get('CC', default_c_compiler)
146
Anthony Barbierdd669372018-03-01 17:08:54 +0000147if env['os'] == 'android' and ( 'clang++' not in cpp_compiler or 'clang' not in c_compiler ):
ggardet767c9f72018-06-29 17:01:01 +0200148 print( "WARNING: Only clang is officially supported to build the Compute Library for Android")
Anthony Barbiera026e982018-01-18 10:57:52 +0000149
Anthony Barbierdd669372018-03-01 17:08:54 +0000150if 'clang++' in cpp_compiler:
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100151 env.Append(CXXFLAGS = ['-Wno-vla-extension'])
Georgios Pinitase874ef92019-09-09 17:40:33 +0100152elif 'armclang' in cpp_compiler:
153 pass
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154else:
Michalis Spyrou7c60c992019-10-10 14:33:47 +0100155 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156
157if env['cppthreads']:
158 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
159
160if env['openmp']:
Anthony Barbierdd669372018-03-01 17:08:54 +0000161 if 'clang++' in cpp_compiler:
ggardet767c9f72018-06-29 17:01:01 +0200162 print( "Clang does not support OpenMP. Use scheduler=cpp.")
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 Exit(1)
164
165 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
166 env.Append(CXXFLAGS = ['-fopenmp'])
167 env.Append(LINKFLAGS = ['-fopenmp'])
168
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000169# Validate and define state
170if env['estate'] == 'auto':
171 if 'v7a' in env['arch']:
172 env['estate'] = '32'
173 else:
174 env['estate'] = '64'
175
176# Map legacy arch
177if 'arm64' in env['arch']:
178 env['estate'] = '64'
179
180if 'v7a' in env['estate'] and env['estate'] == '64':
181 print("ERROR: armv7a architecture has only 32-bit execution state")
182 Exit(1)
183
Georgios Pinitase874ef92019-09-09 17:40:33 +0100184# Add architecture specific flags
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185prefix = ""
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000186if 'v7a' in env['arch']:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000188 if env['os'] == 'android':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000190 else:
191 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
192elif 'v8a' in env['arch']:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193 env.Append(CXXFLAGS = ['-march=armv8-a'])
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000194 if env['estate'] == '32':
195 env.Append(CXXFLAGS = ['-mfpu=neon-fp-armv8'])
196elif 'v8.2-a' in env['arch']:
197 if env['estate'] == '32':
198 env.Append(CXXFLAGS = ['-mfpu=neon-fp-armv8'])
199 if 'sve' in env['arch']:
Georgios Pinitas421405b2018-10-26 19:05:32 +0100200 env.Append(CXXFLAGS = ['-march=armv8.2-a+sve+fp16+dotprod'])
201 else:
202 env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16']) # explicitly enable fp16 extension otherwise __ARM_FEATURE_FP16_VECTOR_ARITHMETIC is undefined
Georgios Pinitasf2cdce32019-12-09 18:35:57 +0000203elif 'x86' in env['arch']:
204 if env['estate'] == '32':
205 env.Append(CCFLAGS = ['-m32'])
206 env.Append(LINKFLAGS = ['-m32'])
207 else:
208 env.Append(CXXFLAGS = ['-fPIC'])
209 env.Append(CCFLAGS = ['-m64'])
210 env.Append(LINKFLAGS = ['-m64'])
211
212# Define toolchain
213prefix = ""
214if 'x86' not in env['arch']:
215 if env['estate'] == '32':
216 if env['os'] == 'linux':
217 prefix = "arm-linux-gnueabihf-" if 'v7' in env['arch'] else "armv8l-linux-gnueabihf-"
218 elif env['os'] == 'bare_metal':
219 prefix = "arm-eabi-"
220 elif env['os'] == 'android':
221 prefix = "arm-linux-androideabi-"
222 elif env['estate'] == '64' and 'v8' in env['arch']:
223 if env['os'] == 'linux':
224 prefix = "aarch64-linux-gnu-"
225 elif env['os'] == 'bare_metal':
226 prefix = "aarch64-elf-"
227 elif env['os'] == 'android':
228 prefix = "aarch64-linux-android-"
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229
230if env['build'] == 'native':
231 prefix = ""
232
Anthony Barbier149de5b2018-12-06 10:27:37 +0000233if env["toolchain_prefix"] != "":
234 prefix = env["toolchain_prefix"]
235
Anthony Barbier7390e052018-03-13 09:29:41 +0000236env['CC'] = env['compiler_cache']+" "+prefix + c_compiler
237env['CXX'] = env['compiler_cache']+" "+prefix + cpp_compiler
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100238env['LD'] = prefix + "ld"
239env['AS'] = prefix + "as"
240env['AR'] = prefix + "ar"
241env['RANLIB'] = prefix + "ranlib"
242
243if not GetOption("help"):
244 try:
Anthony Barbier907dba82017-08-25 10:11:39 +0100245 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246 except OSError:
247 print("ERROR: Compiler '%s' not found" % env['CXX'])
248 Exit(1)
249
Georgios Pinitase874ef92019-09-09 17:40:33 +0100250 if 'armclang' in cpp_compiler:
251 pass
252 elif 'clang++' not in cpp_compiler:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100253 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
ggardet767c9f72018-06-29 17:01:01 +0200254 print("GCC 6.2.1 or newer is required to compile armv8.2-a code")
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100255 Exit(1)
256 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
ggardet767c9f72018-06-29 17:01:01 +0200257 print("GCC 4.9 or newer is required to compile NEON code for AArch64")
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100258 Exit(1)
259
260 if version_at_least(compiler_ver, '6.1'):
261 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
262
263 if compiler_ver == '4.8.3':
264 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
265
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100266if env['standalone']:
267 env.Append(CXXFLAGS = ['-fPIC'])
Anthony Barbier665c89b2018-07-16 11:40:09 +0100268 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100269
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100270if env['Werror']:
271 env.Append(CXXFLAGS = ['-Werror'])
272
273if env['os'] == 'android':
274 env.Append(CPPDEFINES = ['ANDROID'])
Anthony Barbier665c89b2018-07-16 11:40:09 +0100275 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100276elif env['os'] == 'bare_metal':
277 env.Append(LINKFLAGS = ['-static'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100278 env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100279 env.Append(CXXFLAGS = ['-fPIC'])
280 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100281 env.Append(CPPDEFINES = ['BARE_METAL'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100282
283if env['opencl']:
Georgios Pinitase6032da2017-10-26 14:13:35 +0100284 if env['os'] in ['bare_metal'] or env['standalone']:
Anthony Barbier5f0124d2018-04-13 14:05:34 +0100285 print("Cannot link OpenCL statically, which is required for bare metal / standalone builds")
286 Exit(1)
287
288if env['gles_compute']:
289 if env['os'] in ['bare_metal'] or env['standalone']:
290 print("Cannot link OpenGLES statically, which is required for bare metal / standalone builds")
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100291 Exit(1)
292
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100293if env["os"] not in ["android", "bare_metal"] and (env['opencl'] or env['cppthreads']):
294 env.Append(LIBS = ['pthread'])
295
Anthony Barbier7068f992017-10-26 15:23:08 +0100296if env['opencl'] or env['gles_compute']:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100297 if env['embed_kernels']:
298 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
299
300if env['debug']:
301 env['asserts'] = True
Georgios Pinitas3faea252017-10-30 14:13:50 +0000302 env['logging'] = True
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100303 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
304 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
305else:
Ramana Radhakrishnand176d542019-07-04 11:38:45 +0100306 env.Append(CXXFLAGS = ['-O3'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100307
308if env['asserts']:
309 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
Moritz Pflanzer8b74c782017-09-14 14:33:20 +0100310 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100311
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100312if env['logging']:
313 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
314
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100315env.Append(CPPPATH = ['#/include', "#"])
316env.Append(CXXFLAGS = env['extra_cxx_flags'])
Georgios Pinitas421405b2018-10-26 19:05:32 +0100317env.Append(LINKFLAGS = env['extra_link_flags'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100318
Anthony Barbier01bbd5f2018-11-01 15:10:51 +0000319Default( install_include("arm_compute"))
Anthony Barbier10565952018-11-15 09:50:06 +0000320Default( install_include("support"))
Michele Di Giorgio6afea902019-04-03 11:40:20 +0100321Default( install_include("utils"))
322for dirname in os.listdir("./include"):
323 Default( install_include("include/%s" % dirname))
Anthony Barbier01bbd5f2018-11-01 15:10:51 +0000324
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100325Export('version_at_least')
326
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100327
Anthony Barbier7068f992017-10-26 15:23:08 +0100328if env['gles_compute'] and env['os'] != 'android':
Anthony Barbier14c86a92017-12-14 16:27:41 +0000329 env.Append(CPPPATH = ['#/include/linux'])
Anthony Barbier7068f992017-10-26 15:23:08 +0100330
Anthony Barbiere8a55df2018-10-26 09:05:01 +0100331SConscript('./SConscript', variant_dir=build_path, duplicate=0)
Anthony Barbier6c0348f2017-11-10 18:32:38 +0000332
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000333if env['examples'] and env['os'] != 'bare_metal' and env['exceptions']:
Anthony Barbiere8a55df2018-10-26 09:05:01 +0100334 SConscript('./examples/SConscript', variant_dir='%s/examples' % build_path, duplicate=0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100335
Michalis Spyrou323ce0f2018-11-30 16:30:43 +0000336if env['os'] != 'bare_metal' and env['exceptions']:
Anthony Barbiere8a55df2018-10-26 09:05:01 +0100337 SConscript('./tests/SConscript', variant_dir='%s/tests' % build_path, duplicate=0)