blob: 60eb2867022e317760bea950abc9b4265d12aaf8 [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),
Jenkinsb9abeae2018-11-22 11:58:08 +000043 EnumVariable("arch", "Target Architecture", "armv7a", allowed_values=("armv7a", "arm64-v8a", "arm64-v8.2-a", "arm64-v8.2-a-sve", "x86_32", "x86_64")),
Anthony Barbier46d59272017-05-04 09:15:15 +010044 EnumVariable("os", "Target OS", "linux", allowed_values=("linux", "android", "bare_metal")),
Anthony Barbier06ea0482018-02-22 15:45:35 +000045 EnumVariable("build", "Build type", "cross_compile", allowed_values=("native", "cross_compile", "embed_only")),
Anthony Barbierdbdab852017-06-23 15:42:00 +010046 BoolVariable("examples", "Build example programs", True),
Anthony Barbier46d59272017-05-04 09:15:15 +010047 BoolVariable("Werror", "Enable/disable the -Werror compilation flag", True),
Kaizen8938bd32017-09-28 14:38:23 +010048 BoolVariable("standalone", "Builds the tests as standalone executables, links statically with libgcc, libstdc++ and libarm_compute", False),
Anthony Barbier46d59272017-05-04 09:15:15 +010049 BoolVariable("opencl", "Enable OpenCL support", True),
50 BoolVariable("neon", "Enable Neon support", False),
Anthony Barbier8140e1e2017-12-14 23:48:46 +000051 BoolVariable("gles_compute", "Enable OpenGL ES Compute Shader support", False),
Anthony Barbierf45d5a92018-01-24 16:23:15 +000052 BoolVariable("embed_kernels", "Embed OpenCL kernels and OpenGL ES compute shaders in library binary", True),
Anthony Barbier46d59272017-05-04 09:15:15 +010053 BoolVariable("set_soname", "Set the library's soname and shlibversion (requires SCons 2.4 or above)", False),
54 BoolVariable("openmp", "Enable OpenMP backend", False),
55 BoolVariable("cppthreads", "Enable C++11 threads backend", True),
Anthony Barbierdbdab852017-06-23 15:42:00 +010056 PathVariable("build_dir", "Specify sub-folder for the build", ".", PathVariable.PathAccept),
Jenkinsb9abeae2018-11-22 11:58:08 +000057 PathVariable("install_dir", "Specify sub-folder for the install", "", PathVariable.PathAccept),
Jenkins514be652019-02-28 12:25:18 +000058 BoolVariable("exceptions", "Enable/disable C++ exception support", True),
59 ("toolchain_prefix", "Override the toolchain prefix", ""),
Jenkinsb3a371b2018-05-23 11:36:53 +010060 ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", ""),
Jenkinsb9abeae2018-11-22 11:58:08 +000061 ("extra_link_flags", "Extra LD flags to be appended to the build command", ""),
Jenkinsb3a371b2018-05-23 11:36:53 +010062 ("compiler_cache", "Command to prefix to the C and C++ compiler (e.g ccache)", "")
Anthony Barbier46d59272017-05-04 09:15:15 +010063)
64
Anthony Barbierdbdab852017-06-23 15:42:00 +010065env = Environment(platform="posix", variables=vars, ENV = os.environ)
Jenkinsb9abeae2018-11-22 11:58:08 +000066build_path = env['build_dir']
67# If build_dir is a relative path then add a #build/ prefix:
68if not env['build_dir'].startswith('/'):
69 SConsignFile('build/%s/.scons' % build_path)
70 build_path = "#build/%s" % build_path
71else:
72 SConsignFile('%s/.scons' % build_path)
73
74install_path = env['install_dir']
75#If the install_dir is a relative path then assume it's from inside build_dir
76if not env['install_dir'].startswith('/') and install_path != "":
77 install_path = "%s/%s" % (build_path, install_path)
78
79env.Append(LIBPATH = [build_path])
Anthony Barbier06ea0482018-02-22 15:45:35 +000080Export('env')
81Export('vars')
Anthony Barbierdbdab852017-06-23 15:42:00 +010082
Jenkinsb9abeae2018-11-22 11:58:08 +000083def install_lib( lib ):
84 # If there is no install folder, then there is nothing to do:
85 if install_path == "":
86 return lib
87 return env.Install( "%s/lib/" % install_path, lib)
88def install_bin( bin ):
89 # If there is no install folder, then there is nothing to do:
90 if install_path == "":
91 return bin
92 return env.Install( "%s/bin/" % install_path, bin)
93def install_include( inc ):
94 if install_path == "":
95 return inc
96 return env.Install( "%s/include/" % install_path, inc)
97
98Export('install_lib')
99Export('install_bin')
Anthony Barbier46d59272017-05-04 09:15:15 +0100100
101Help(vars.GenerateHelpText(env))
102
Anthony Barbier06ea0482018-02-22 15:45:35 +0000103if env['build'] == "embed_only":
Jenkinsb9abeae2018-11-22 11:58:08 +0000104 SConscript('./SConscript', variant_dir=build_path, duplicate=0)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000105 Return()
106
Anthony Barbierdbdab852017-06-23 15:42:00 +0100107if env['neon'] and 'x86' in env['arch']:
ggardete2542c92018-06-29 17:01:01 +0200108 print("Cannot compile NEON for x86")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100109 Exit(1)
110
111if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
ggardete2542c92018-06-29 17:01:01 +0200112 print("Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above")
113 print("Update your version of SCons or use set_soname=0")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100114 Exit(1)
115
116if env['os'] == 'bare_metal':
117 if env['cppthreads'] or env['openmp']:
118 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
119 Exit(1)
120
Jenkins514be652019-02-28 12:25:18 +0000121if not env['exceptions']:
122 if env['opencl'] or env['gles_compute']:
123 print("ERROR: OpenCL and GLES are not supported when building without exceptions. Use opencl=0 gles_compute=0")
124 Exit(1)
125
126 env.Append(CPPDEFINES = ['ARM_COMPUTE_EXCEPTIONS_DISABLED'])
127 env.Append(CXXFLAGS = ['-fno-exceptions'])
128
Anthony Barbierdbdab852017-06-23 15:42:00 +0100129env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
130 '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
131 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
132 '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
Jenkins52ba29e2018-08-29 15:32:11 +0000133 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000134
Anthony Barbierdbdab852017-06-23 15:42:00 +0100135env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
136
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000137default_cpp_compiler = 'g++' if env['os'] != 'android' else 'clang++'
138default_c_compiler = 'gcc' if env['os'] != 'android' else 'clang'
139cpp_compiler = os.environ.get('CXX', default_cpp_compiler)
140c_compiler = os.environ.get('CC', default_c_compiler)
141
Jenkinsb3a371b2018-05-23 11:36:53 +0100142if env['os'] == 'android' and ( 'clang++' not in cpp_compiler or 'clang' not in c_compiler ):
ggardete2542c92018-06-29 17:01:01 +0200143 print( "WARNING: Only clang is officially supported to build the Compute Library for Android")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000144
Jenkinsb3a371b2018-05-23 11:36:53 +0100145if 'clang++' in cpp_compiler:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100146 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
147else:
Jenkins975dfe12019-09-02 11:47:54 +0100148 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel', '-Wno-redundant-move'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100149
150if env['cppthreads']:
151 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
152
153if env['openmp']:
Jenkinsb3a371b2018-05-23 11:36:53 +0100154 if 'clang++' in cpp_compiler:
ggardete2542c92018-06-29 17:01:01 +0200155 print( "Clang does not support OpenMP. Use scheduler=cpp.")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100156 Exit(1)
157
158 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
159 env.Append(CXXFLAGS = ['-fopenmp'])
160 env.Append(LINKFLAGS = ['-fopenmp'])
161
162prefix = ""
163if env['arch'] == 'armv7a':
164 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
165
Kaizen8938bd32017-09-28 14:38:23 +0100166 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100167 prefix = "arm-linux-gnueabihf-"
168 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Kaizen8938bd32017-09-28 14:38:23 +0100169 elif env['os'] == 'bare_metal':
Kaizenbf8b01d2017-10-12 14:26:51 +0100170 prefix = "arm-eabi-"
Kaizen8938bd32017-09-28 14:38:23 +0100171 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100172 elif env['os'] == 'android':
173 prefix = "arm-linux-androideabi-"
174 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
175elif env['arch'] == 'arm64-v8a':
176 env.Append(CXXFLAGS = ['-march=armv8-a'])
Jenkinsb3a371b2018-05-23 11:36:53 +0100177 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8A','NO_DOT_IN_TOOLCHAIN'])
Kaizen8938bd32017-09-28 14:38:23 +0100178 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100179 prefix = "aarch64-linux-gnu-"
Kaizen8938bd32017-09-28 14:38:23 +0100180 elif env['os'] == 'bare_metal':
Kaizenbf8b01d2017-10-12 14:26:51 +0100181 prefix = "aarch64-elf-"
Anthony Barbierdbdab852017-06-23 15:42:00 +0100182 elif env['os'] == 'android':
183 prefix = "aarch64-linux-android-"
Jenkinsb3a371b2018-05-23 11:36:53 +0100184 if 'clang++' in cpp_compiler:
185 env.Append(CXXFLAGS = ['-no-integrated-as'])
Jenkinsb9abeae2018-11-22 11:58:08 +0000186elif 'arm64-v8.2-a' in env['arch']:
187 if env['arch'] == 'arm64-v8.2-a-sve':
Jenkinsb9abeae2018-11-22 11:58:08 +0000188 env.Append(CXXFLAGS = ['-march=armv8.2-a+sve+fp16+dotprod'])
189 else:
190 env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16']) # explicitly enable fp16 extension otherwise __ARM_FEATURE_FP16_VECTOR_ARITHMETIC is undefined
Jenkins975dfe12019-09-02 11:47:54 +0100191 if env['os'] == 'linux':
192 prefix = "aarch64-linux-gnu-"
193 elif env['os'] == 'bare_metal':
194 prefix = "aarch64-elf-"
195 elif env['os'] == 'android':
196 prefix = "aarch64-linux-android-"
Jenkinsb3a371b2018-05-23 11:36:53 +0100197 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8_2','NO_DOT_IN_TOOLCHAIN'])
198 if 'clang++' in cpp_compiler:
199 env.Append(CXXFLAGS = ['-no-integrated-as'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100200elif env['arch'] == 'x86_32':
201 env.Append(CCFLAGS = ['-m32'])
202 env.Append(LINKFLAGS = ['-m32'])
203elif env['arch'] == 'x86_64':
Jenkins4ba87db2019-05-23 17:11:51 +0100204 env.Append(CXXFLAGS = ['-fPIC'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100205 env.Append(CCFLAGS = ['-m64'])
206 env.Append(LINKFLAGS = ['-m64'])
207
208if env['build'] == 'native':
209 prefix = ""
210
Jenkins514be652019-02-28 12:25:18 +0000211if env["toolchain_prefix"] != "":
212 prefix = env["toolchain_prefix"]
213
Jenkinsb3a371b2018-05-23 11:36:53 +0100214env['CC'] = env['compiler_cache']+" "+prefix + c_compiler
215env['CXX'] = env['compiler_cache']+" "+prefix + cpp_compiler
Anthony Barbierdbdab852017-06-23 15:42:00 +0100216env['LD'] = prefix + "ld"
217env['AS'] = prefix + "as"
218env['AR'] = prefix + "ar"
219env['RANLIB'] = prefix + "ranlib"
Anthony Barbier46d59272017-05-04 09:15:15 +0100220
221if not GetOption("help"):
Anthony Barbierdbdab852017-06-23 15:42:00 +0100222 try:
bradford barr8d418ab2017-08-07 14:17:09 -0400223 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
Anthony Barbierdbdab852017-06-23 15:42:00 +0100224 except OSError:
225 print("ERROR: Compiler '%s' not found" % env['CXX'])
226 Exit(1)
227
Jenkinsb3a371b2018-05-23 11:36:53 +0100228 if 'clang++' not in cpp_compiler:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100229 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
ggardete2542c92018-06-29 17:01:01 +0200230 print("GCC 6.2.1 or newer is required to compile armv8.2-a code")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100231 Exit(1)
232 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
ggardete2542c92018-06-29 17:01:01 +0200233 print("GCC 4.9 or newer is required to compile NEON code for AArch64")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100234 Exit(1)
235
236 if version_at_least(compiler_ver, '6.1'):
237 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
238
239 if compiler_ver == '4.8.3':
240 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
241
Kaizen8938bd32017-09-28 14:38:23 +0100242if env['standalone']:
243 env.Append(CXXFLAGS = ['-fPIC'])
244 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
245
Anthony Barbierdbdab852017-06-23 15:42:00 +0100246if env['Werror']:
247 env.Append(CXXFLAGS = ['-Werror'])
248
249if env['os'] == 'android':
250 env.Append(CPPDEFINES = ['ANDROID'])
251 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
252elif env['os'] == 'bare_metal':
253 env.Append(LINKFLAGS = ['-static'])
Kaizen8938bd32017-09-28 14:38:23 +0100254 env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100255 env.Append(CXXFLAGS = ['-fPIC'])
256 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
Kaizen8938bd32017-09-28 14:38:23 +0100257 env.Append(CPPDEFINES = ['BARE_METAL'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100258
259if env['opencl']:
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000260 if env['os'] in ['bare_metal'] or env['standalone']:
Jenkinsb3a371b2018-05-23 11:36:53 +0100261 print("Cannot link OpenCL statically, which is required for bare metal / standalone builds")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100262 Exit(1)
263
Jenkinsb3a371b2018-05-23 11:36:53 +0100264if env['gles_compute']:
265 if env['os'] in ['bare_metal'] or env['standalone']:
266 print("Cannot link OpenGLES statically, which is required for bare metal / standalone builds")
267 Exit(1)
268
269if env["os"] not in ["android", "bare_metal"] and (env['opencl'] or env['cppthreads']):
270 env.Append(LIBS = ['pthread'])
271
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000272if env['opencl'] or env['gles_compute']:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100273 if env['embed_kernels']:
274 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
275
276if env['debug']:
277 env['asserts'] = True
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000278 env['logging'] = True
Anthony Barbierdbdab852017-06-23 15:42:00 +0100279 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
280 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
281else:
Jenkins975dfe12019-09-02 11:47:54 +0100282 env.Append(CXXFLAGS = ['-O3'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100283
284if env['asserts']:
285 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
Kaizen8938bd32017-09-28 14:38:23 +0100286 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100287
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000288if env['logging']:
289 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
290
Anthony Barbierdbdab852017-06-23 15:42:00 +0100291env.Append(CPPPATH = ['#/include', "#"])
292env.Append(CXXFLAGS = env['extra_cxx_flags'])
Jenkinsb9abeae2018-11-22 11:58:08 +0000293env.Append(LINKFLAGS = env['extra_link_flags'])
294
295Default( install_include("arm_compute"))
296Default( install_include("support"))
Jenkins4ba87db2019-05-23 17:11:51 +0100297Default( install_include("utils"))
298for dirname in os.listdir("./include"):
299 Default( install_include("include/%s" % dirname))
Anthony Barbierdbdab852017-06-23 15:42:00 +0100300
Anthony Barbierdbdab852017-06-23 15:42:00 +0100301Export('version_at_least')
302
Anthony Barbierdbdab852017-06-23 15:42:00 +0100303if env['opencl']:
Jenkinsb9abeae2018-11-22 11:58:08 +0000304 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="%s/opencl-1.2-stubs" % build_path, duplicate=0)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100305
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000306if env['gles_compute'] and env['os'] != 'android':
307 env.Append(CPPPATH = ['#/include/linux'])
Jenkinsb9abeae2018-11-22 11:58:08 +0000308 SConscript("./opengles-3.1-stubs/SConscript", variant_dir="%s/opengles-3.1-stubs" % build_path, duplicate=0)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000309
Jenkinsb9abeae2018-11-22 11:58:08 +0000310SConscript('./SConscript', variant_dir=build_path, duplicate=0)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000311
Jenkins514be652019-02-28 12:25:18 +0000312if env['examples'] and env['os'] != 'bare_metal' and env['exceptions']:
Jenkinsb9abeae2018-11-22 11:58:08 +0000313 SConscript('./examples/SConscript', variant_dir='%s/examples' % build_path, duplicate=0)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100314
Jenkins514be652019-02-28 12:25:18 +0000315if env['os'] != 'bare_metal' and env['exceptions']:
Jenkinsb9abeae2018-11-22 11:58:08 +0000316 SConscript('./tests/SConscript', variant_dir='%s/tests' % build_path, duplicate=0)