blob: ce7ef6a0833d792c8d0585d62740d941afd59c46 [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),
Jenkinsb3a371b2018-05-23 11:36:53 +010058 ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", ""),
Jenkinsb9abeae2018-11-22 11:58:08 +000059 ("extra_link_flags", "Extra LD flags to be appended to the build command", ""),
Jenkinsb3a371b2018-05-23 11:36:53 +010060 ("compiler_cache", "Command to prefix to the C and C++ compiler (e.g ccache)", "")
Anthony Barbier46d59272017-05-04 09:15:15 +010061)
62
Anthony Barbierdbdab852017-06-23 15:42:00 +010063env = Environment(platform="posix", variables=vars, ENV = os.environ)
Jenkinsb9abeae2018-11-22 11:58:08 +000064build_path = env['build_dir']
65# If build_dir is a relative path then add a #build/ prefix:
66if not env['build_dir'].startswith('/'):
67 SConsignFile('build/%s/.scons' % build_path)
68 build_path = "#build/%s" % build_path
69else:
70 SConsignFile('%s/.scons' % build_path)
71
72install_path = env['install_dir']
73#If the install_dir is a relative path then assume it's from inside build_dir
74if not env['install_dir'].startswith('/') and install_path != "":
75 install_path = "%s/%s" % (build_path, install_path)
76
77env.Append(LIBPATH = [build_path])
Anthony Barbier06ea0482018-02-22 15:45:35 +000078Export('env')
79Export('vars')
Anthony Barbierdbdab852017-06-23 15:42:00 +010080
Jenkinsb9abeae2018-11-22 11:58:08 +000081def install_lib( lib ):
82 # If there is no install folder, then there is nothing to do:
83 if install_path == "":
84 return lib
85 return env.Install( "%s/lib/" % install_path, lib)
86def install_bin( bin ):
87 # If there is no install folder, then there is nothing to do:
88 if install_path == "":
89 return bin
90 return env.Install( "%s/bin/" % install_path, bin)
91def install_include( inc ):
92 if install_path == "":
93 return inc
94 return env.Install( "%s/include/" % install_path, inc)
95
96Export('install_lib')
97Export('install_bin')
Anthony Barbier46d59272017-05-04 09:15:15 +010098
99Help(vars.GenerateHelpText(env))
100
Anthony Barbier06ea0482018-02-22 15:45:35 +0000101if env['build'] == "embed_only":
Jenkinsb9abeae2018-11-22 11:58:08 +0000102 SConscript('./SConscript', variant_dir=build_path, duplicate=0)
Anthony Barbier06ea0482018-02-22 15:45:35 +0000103 Return()
104
Anthony Barbierdbdab852017-06-23 15:42:00 +0100105if env['neon'] and 'x86' in env['arch']:
ggardete2542c92018-06-29 17:01:01 +0200106 print("Cannot compile NEON for x86")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100107 Exit(1)
108
109if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
ggardete2542c92018-06-29 17:01:01 +0200110 print("Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above")
111 print("Update your version of SCons or use set_soname=0")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100112 Exit(1)
113
114if env['os'] == 'bare_metal':
115 if env['cppthreads'] or env['openmp']:
116 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
117 Exit(1)
118
119env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
120 '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
121 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
122 '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
Jenkins52ba29e2018-08-29 15:32:11 +0000123 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000124
Anthony Barbierdbdab852017-06-23 15:42:00 +0100125env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
126
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000127default_cpp_compiler = 'g++' if env['os'] != 'android' else 'clang++'
128default_c_compiler = 'gcc' if env['os'] != 'android' else 'clang'
129cpp_compiler = os.environ.get('CXX', default_cpp_compiler)
130c_compiler = os.environ.get('CC', default_c_compiler)
131
Jenkinsb3a371b2018-05-23 11:36:53 +0100132if env['os'] == 'android' and ( 'clang++' not in cpp_compiler or 'clang' not in c_compiler ):
ggardete2542c92018-06-29 17:01:01 +0200133 print( "WARNING: Only clang is officially supported to build the Compute Library for Android")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000134
Jenkinsb3a371b2018-05-23 11:36:53 +0100135if 'clang++' in cpp_compiler:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100136 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
137else:
Jenkins52ba29e2018-08-29 15:32:11 +0000138 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel','-Wno-implicit-fallthrough'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100139
140if env['cppthreads']:
141 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
142
143if env['openmp']:
Jenkinsb3a371b2018-05-23 11:36:53 +0100144 if 'clang++' in cpp_compiler:
ggardete2542c92018-06-29 17:01:01 +0200145 print( "Clang does not support OpenMP. Use scheduler=cpp.")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100146 Exit(1)
147
148 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
149 env.Append(CXXFLAGS = ['-fopenmp'])
150 env.Append(LINKFLAGS = ['-fopenmp'])
151
152prefix = ""
153if env['arch'] == 'armv7a':
154 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
155
Kaizen8938bd32017-09-28 14:38:23 +0100156 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100157 prefix = "arm-linux-gnueabihf-"
158 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Kaizen8938bd32017-09-28 14:38:23 +0100159 elif env['os'] == 'bare_metal':
Kaizenbf8b01d2017-10-12 14:26:51 +0100160 prefix = "arm-eabi-"
Kaizen8938bd32017-09-28 14:38:23 +0100161 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100162 elif env['os'] == 'android':
163 prefix = "arm-linux-androideabi-"
164 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
165elif env['arch'] == 'arm64-v8a':
166 env.Append(CXXFLAGS = ['-march=armv8-a'])
Jenkinsb3a371b2018-05-23 11:36:53 +0100167 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8A','NO_DOT_IN_TOOLCHAIN'])
Kaizen8938bd32017-09-28 14:38:23 +0100168 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100169 prefix = "aarch64-linux-gnu-"
Kaizen8938bd32017-09-28 14:38:23 +0100170 elif env['os'] == 'bare_metal':
Kaizenbf8b01d2017-10-12 14:26:51 +0100171 prefix = "aarch64-elf-"
Anthony Barbierdbdab852017-06-23 15:42:00 +0100172 elif env['os'] == 'android':
173 prefix = "aarch64-linux-android-"
Jenkinsb3a371b2018-05-23 11:36:53 +0100174 if 'clang++' in cpp_compiler:
175 env.Append(CXXFLAGS = ['-no-integrated-as'])
Jenkinsb9abeae2018-11-22 11:58:08 +0000176elif 'arm64-v8.2-a' in env['arch']:
177 if env['arch'] == 'arm64-v8.2-a-sve':
178 if env['os'] != 'bare_metal':
179 print("Only bare metal SVE is supported at the moment")
180 Exit(1)
181 env.Append(CXXFLAGS = ['-march=armv8.2-a+sve+fp16+dotprod'])
182 else:
183 env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16']) # explicitly enable fp16 extension otherwise __ARM_FEATURE_FP16_VECTOR_ARITHMETIC is undefined
184 if env['os'] == 'linux':
185 prefix = "aarch64-linux-gnu-"
186 elif env['os'] == 'bare_metal':
187 prefix = "aarch64-elf-"
188 elif env['os'] == 'android':
189 prefix = "aarch64-linux-android-"
Jenkinsb3a371b2018-05-23 11:36:53 +0100190 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8_2','NO_DOT_IN_TOOLCHAIN'])
191 if 'clang++' in cpp_compiler:
192 env.Append(CXXFLAGS = ['-no-integrated-as'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100193elif env['arch'] == 'x86_32':
194 env.Append(CCFLAGS = ['-m32'])
195 env.Append(LINKFLAGS = ['-m32'])
196elif env['arch'] == 'x86_64':
197 env.Append(CCFLAGS = ['-m64'])
198 env.Append(LINKFLAGS = ['-m64'])
199
200if env['build'] == 'native':
201 prefix = ""
202
Jenkinsb3a371b2018-05-23 11:36:53 +0100203env['CC'] = env['compiler_cache']+" "+prefix + c_compiler
204env['CXX'] = env['compiler_cache']+" "+prefix + cpp_compiler
Anthony Barbierdbdab852017-06-23 15:42:00 +0100205env['LD'] = prefix + "ld"
206env['AS'] = prefix + "as"
207env['AR'] = prefix + "ar"
208env['RANLIB'] = prefix + "ranlib"
Anthony Barbier46d59272017-05-04 09:15:15 +0100209
210if not GetOption("help"):
Anthony Barbierdbdab852017-06-23 15:42:00 +0100211 try:
bradford barr8d418ab2017-08-07 14:17:09 -0400212 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
Anthony Barbierdbdab852017-06-23 15:42:00 +0100213 except OSError:
214 print("ERROR: Compiler '%s' not found" % env['CXX'])
215 Exit(1)
216
Jenkinsb3a371b2018-05-23 11:36:53 +0100217 if 'clang++' not in cpp_compiler:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100218 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
ggardete2542c92018-06-29 17:01:01 +0200219 print("GCC 6.2.1 or newer is required to compile armv8.2-a code")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100220 Exit(1)
221 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
ggardete2542c92018-06-29 17:01:01 +0200222 print("GCC 4.9 or newer is required to compile NEON code for AArch64")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100223 Exit(1)
224
225 if version_at_least(compiler_ver, '6.1'):
226 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
227
228 if compiler_ver == '4.8.3':
229 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
230
Kaizen8938bd32017-09-28 14:38:23 +0100231if env['standalone']:
232 env.Append(CXXFLAGS = ['-fPIC'])
233 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
234
Anthony Barbierdbdab852017-06-23 15:42:00 +0100235if env['Werror']:
236 env.Append(CXXFLAGS = ['-Werror'])
237
238if env['os'] == 'android':
239 env.Append(CPPDEFINES = ['ANDROID'])
240 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
241elif env['os'] == 'bare_metal':
242 env.Append(LINKFLAGS = ['-static'])
Kaizen8938bd32017-09-28 14:38:23 +0100243 env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100244 env.Append(CXXFLAGS = ['-fPIC'])
245 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
Kaizen8938bd32017-09-28 14:38:23 +0100246 env.Append(CPPDEFINES = ['BARE_METAL'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100247
248if env['opencl']:
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000249 if env['os'] in ['bare_metal'] or env['standalone']:
Jenkinsb3a371b2018-05-23 11:36:53 +0100250 print("Cannot link OpenCL statically, which is required for bare metal / standalone builds")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100251 Exit(1)
252
Jenkinsb3a371b2018-05-23 11:36:53 +0100253if env['gles_compute']:
254 if env['os'] in ['bare_metal'] or env['standalone']:
255 print("Cannot link OpenGLES statically, which is required for bare metal / standalone builds")
256 Exit(1)
257
258if env["os"] not in ["android", "bare_metal"] and (env['opencl'] or env['cppthreads']):
259 env.Append(LIBS = ['pthread'])
260
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000261if env['opencl'] or env['gles_compute']:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100262 if env['embed_kernels']:
263 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
264
265if env['debug']:
266 env['asserts'] = True
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000267 env['logging'] = True
Anthony Barbierdbdab852017-06-23 15:42:00 +0100268 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
269 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
270else:
271 env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
272
273if env['asserts']:
274 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
Kaizen8938bd32017-09-28 14:38:23 +0100275 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100276
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000277if env['logging']:
278 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
279
Anthony Barbierdbdab852017-06-23 15:42:00 +0100280env.Append(CPPPATH = ['#/include', "#"])
281env.Append(CXXFLAGS = env['extra_cxx_flags'])
Jenkinsb9abeae2018-11-22 11:58:08 +0000282env.Append(LINKFLAGS = env['extra_link_flags'])
283
284Default( install_include("arm_compute"))
285Default( install_include("support"))
Anthony Barbierdbdab852017-06-23 15:42:00 +0100286
Anthony Barbierdbdab852017-06-23 15:42:00 +0100287Export('version_at_least')
288
Anthony Barbierdbdab852017-06-23 15:42:00 +0100289if env['opencl']:
Jenkinsb9abeae2018-11-22 11:58:08 +0000290 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="%s/opencl-1.2-stubs" % build_path, duplicate=0)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100291
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000292if env['gles_compute'] and env['os'] != 'android':
293 env.Append(CPPPATH = ['#/include/linux'])
Jenkinsb9abeae2018-11-22 11:58:08 +0000294 SConscript("./opengles-3.1-stubs/SConscript", variant_dir="%s/opengles-3.1-stubs" % build_path, duplicate=0)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000295
Jenkinsb9abeae2018-11-22 11:58:08 +0000296SConscript('./SConscript', variant_dir=build_path, duplicate=0)
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000297
Kaizenbf8b01d2017-10-12 14:26:51 +0100298if env['examples'] and env['os'] != 'bare_metal':
Jenkinsb9abeae2018-11-22 11:58:08 +0000299 SConscript('./examples/SConscript', variant_dir='%s/examples' % build_path, duplicate=0)
Anthony Barbierdbdab852017-06-23 15:42:00 +0100300
Kaizen8938bd32017-09-28 14:38:23 +0100301if env['os'] != 'bare_metal':
Jenkinsb9abeae2018-11-22 11:58:08 +0000302 SConscript('./tests/SConscript', variant_dir='%s/tests' % build_path, duplicate=0)