blob: 4278e0b679f622f1c9298300451bc66893993d66 [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),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010043 EnumVariable("arch", "Target Architecture", "armv7a", allowed_values=("armv7a", "arm64-v8a", "arm64-v8.2-a", "x86_32", "x86_64")),
44 EnumVariable("os", "Target OS", "linux", allowed_values=("linux", "android", "bare_metal")),
Anthony Barbier6a3daf12018-02-19 17:24:27 +000045 EnumVariable("build", "Build type", "cross_compile", allowed_values=("native", "cross_compile", "embed_only")),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010046 BoolVariable("examples", "Build example programs", True),
47 BoolVariable("Werror", "Enable/disable the -Werror compilation flag", True),
Pablo Telloc6cb35a2017-06-21 15:39:47 +010048 BoolVariable("standalone", "Builds the tests as standalone executables, links statically with libgcc, libstdc++ and libarm_compute", False),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 BoolVariable("opencl", "Enable OpenCL support", True),
50 BoolVariable("neon", "Enable Neon support", False),
Anthony Barbier7068f992017-10-26 15:23:08 +010051 BoolVariable("gles_compute", "Enable OpenGL ES Compute Shader support", False),
Anthony Barbiercc0a80b2017-12-15 11:37:29 +000052 BoolVariable("embed_kernels", "Embed OpenCL kernels and OpenGL ES compute shaders in library binary", True),
Anthony Barbier6ff3b192017-09-04 18:44:23 +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),
56 PathVariable("build_dir", "Specify sub-folder for the build", ".", PathVariable.PathAccept),
Anthony Barbiera4e5e1e2017-10-05 14:55:34 +010057 #FIXME Remove before release (And remove all references to INTERNAL_ONLY)
Georgios Pinitas37831162018-08-20 15:41:10 +010058 BoolVariable("internal_only", "Enable ARM internal only tests", False),
Anthony Barbier7390e052018-03-13 09:29:41 +000059 ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", ""),
60 ("compiler_cache", "Command to prefix to the C and C++ compiler (e.g ccache)", "")
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061)
62
63env = Environment(platform="posix", variables=vars, ENV = os.environ)
Anthony Barbiere8a55df2018-10-26 09:05:01 +010064build_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
72env.Append(LIBPATH = [build_path])
Anthony Barbier6a3daf12018-02-19 17:24:27 +000073Export('env')
74Export('vars')
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076
77Help(vars.GenerateHelpText(env))
78
Anthony Barbier6a3daf12018-02-19 17:24:27 +000079if env['build'] == "embed_only":
Anthony Barbiere8a55df2018-10-26 09:05:01 +010080 SConscript('./SConscript', variant_dir=build_path, duplicate=0)
Anthony Barbier6a3daf12018-02-19 17:24:27 +000081 Return()
82
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083if env['neon'] and 'x86' in env['arch']:
ggardet767c9f72018-06-29 17:01:01 +020084 print("Cannot compile NEON for x86")
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085 Exit(1)
86
87if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
ggardet767c9f72018-06-29 17:01:01 +020088 print("Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above")
89 print("Update your version of SCons or use set_soname=0")
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 Exit(1)
91
92if env['os'] == 'bare_metal':
93 if env['cppthreads'] or env['openmp']:
94 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
95 Exit(1)
96
97env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
98 '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
99 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
100 '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
Anthony Barbier95008d82018-07-02 09:27:20 +0100101 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow'])
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000102
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
104
Anthony Barbiera026e982018-01-18 10:57:52 +0000105default_cpp_compiler = 'g++' if env['os'] != 'android' else 'clang++'
106default_c_compiler = 'gcc' if env['os'] != 'android' else 'clang'
107cpp_compiler = os.environ.get('CXX', default_cpp_compiler)
108c_compiler = os.environ.get('CC', default_c_compiler)
109
Anthony Barbierdd669372018-03-01 17:08:54 +0000110if env['os'] == 'android' and ( 'clang++' not in cpp_compiler or 'clang' not in c_compiler ):
ggardet767c9f72018-06-29 17:01:01 +0200111 print( "WARNING: Only clang is officially supported to build the Compute Library for Android")
Anthony Barbiera026e982018-01-18 10:57:52 +0000112
Anthony Barbierdd669372018-03-01 17:08:54 +0000113if 'clang++' in cpp_compiler:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
115else:
Georgios Pinitas72593c72018-07-03 11:33:41 +0100116 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel','-Wno-implicit-fallthrough'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117
118if env['cppthreads']:
119 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
120
121if env['openmp']:
Anthony Barbierdd669372018-03-01 17:08:54 +0000122 if 'clang++' in cpp_compiler:
ggardet767c9f72018-06-29 17:01:01 +0200123 print( "Clang does not support OpenMP. Use scheduler=cpp.")
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 Exit(1)
125
126 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
127 env.Append(CXXFLAGS = ['-fopenmp'])
128 env.Append(LINKFLAGS = ['-fopenmp'])
129
130prefix = ""
131if env['arch'] == 'armv7a':
132 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
133
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100134 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135 prefix = "arm-linux-gnueabihf-"
136 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100137 elif env['os'] == 'bare_metal':
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100138 prefix = "arm-eabi-"
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100139 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 elif env['os'] == 'android':
141 prefix = "arm-linux-androideabi-"
142 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
143elif env['arch'] == 'arm64-v8a':
144 env.Append(CXXFLAGS = ['-march=armv8-a'])
Pablo Telloeb82fd22018-02-23 13:43:50 +0000145 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8A','NO_DOT_IN_TOOLCHAIN'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100146 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147 prefix = "aarch64-linux-gnu-"
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100148 elif env['os'] == 'bare_metal':
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100149 prefix = "aarch64-elf-"
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 elif env['os'] == 'android':
151 prefix = "aarch64-linux-android-"
Anthony Barbier2539dd22018-03-15 14:16:52 +0000152 if 'clang++' in cpp_compiler:
Pablo Telloeb82fd22018-02-23 13:43:50 +0000153 env.Append(CXXFLAGS = ['-no-integrated-as'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154elif env['arch'] == 'arm64-v8.2-a':
Pablo Tellofa7dc842018-01-25 10:48:46 +0000155 env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16']) # explicitly enable fp16 extension otherwise __ARM_FEATURE_FP16_VECTOR_ARITHMETIC is undefined
Pablo Telloeb82fd22018-02-23 13:43:50 +0000156 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8_2','NO_DOT_IN_TOOLCHAIN'])
Anthony Barbierdd669372018-03-01 17:08:54 +0000157 if 'clang++' in cpp_compiler:
Anthony Barbier2539dd22018-03-15 14:16:52 +0000158 env.Append(CXXFLAGS = ['-no-integrated-as'])
Pablo Tello9e40cf72017-09-15 16:14:55 +0100159 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 prefix = "aarch64-linux-gnu-"
Pablo Tello9e40cf72017-09-15 16:14:55 +0100161 elif env['os'] == 'bare_metal':
162 prefix = "aarch64-elf-"
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100163 elif env['os'] == 'android':
164 prefix = "aarch64-linux-android-"
165elif env['arch'] == 'x86_32':
166 env.Append(CCFLAGS = ['-m32'])
167 env.Append(LINKFLAGS = ['-m32'])
168elif env['arch'] == 'x86_64':
169 env.Append(CCFLAGS = ['-m64'])
170 env.Append(LINKFLAGS = ['-m64'])
171
172if env['build'] == 'native':
173 prefix = ""
174
Anthony Barbier7390e052018-03-13 09:29:41 +0000175env['CC'] = env['compiler_cache']+" "+prefix + c_compiler
176env['CXX'] = env['compiler_cache']+" "+prefix + cpp_compiler
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177env['LD'] = prefix + "ld"
178env['AS'] = prefix + "as"
179env['AR'] = prefix + "ar"
180env['RANLIB'] = prefix + "ranlib"
181
182if not GetOption("help"):
183 try:
Anthony Barbier907dba82017-08-25 10:11:39 +0100184 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 except OSError:
186 print("ERROR: Compiler '%s' not found" % env['CXX'])
187 Exit(1)
188
Anthony Barbierdd669372018-03-01 17:08:54 +0000189 if 'clang++' not in cpp_compiler:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
ggardet767c9f72018-06-29 17:01:01 +0200191 print("GCC 6.2.1 or newer is required to compile armv8.2-a code")
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192 Exit(1)
193 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
ggardet767c9f72018-06-29 17:01:01 +0200194 print("GCC 4.9 or newer is required to compile NEON code for AArch64")
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195 Exit(1)
196
197 if version_at_least(compiler_ver, '6.1'):
198 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
199
200 if compiler_ver == '4.8.3':
201 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
202
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100203if env['standalone']:
204 env.Append(CXXFLAGS = ['-fPIC'])
Anthony Barbier665c89b2018-07-16 11:40:09 +0100205 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100206
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207if env['Werror']:
208 env.Append(CXXFLAGS = ['-Werror'])
209
210if env['os'] == 'android':
211 env.Append(CPPDEFINES = ['ANDROID'])
Anthony Barbier665c89b2018-07-16 11:40:09 +0100212 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213elif env['os'] == 'bare_metal':
214 env.Append(LINKFLAGS = ['-static'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100215 env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216 env.Append(CXXFLAGS = ['-fPIC'])
217 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100218 env.Append(CPPDEFINES = ['BARE_METAL'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100219
220if env['opencl']:
Georgios Pinitase6032da2017-10-26 14:13:35 +0100221 if env['os'] in ['bare_metal'] or env['standalone']:
Anthony Barbier5f0124d2018-04-13 14:05:34 +0100222 print("Cannot link OpenCL statically, which is required for bare metal / standalone builds")
223 Exit(1)
224
225if env['gles_compute']:
226 if env['os'] in ['bare_metal'] or env['standalone']:
227 print("Cannot link OpenGLES statically, which is required for bare metal / standalone builds")
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100228 Exit(1)
229
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100230if env["os"] not in ["android", "bare_metal"] and (env['opencl'] or env['cppthreads']):
231 env.Append(LIBS = ['pthread'])
232
Anthony Barbier7068f992017-10-26 15:23:08 +0100233if env['opencl'] or env['gles_compute']:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100234 if env['embed_kernels']:
235 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
236
237if env['debug']:
238 env['asserts'] = True
Georgios Pinitas3faea252017-10-30 14:13:50 +0000239 env['logging'] = True
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
241 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
242else:
243 env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
244
245if env['asserts']:
246 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
Moritz Pflanzer8b74c782017-09-14 14:33:20 +0100247 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100248
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100249if env['logging']:
250 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
251
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100252env.Append(CPPPATH = ['#/include', "#"])
253env.Append(CXXFLAGS = env['extra_cxx_flags'])
254
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100255Export('version_at_least')
256
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100257if env['opencl']:
Anthony Barbiere8a55df2018-10-26 09:05:01 +0100258 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="%s/opencl-1.2-stubs" % build_path, duplicate=0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100259
Anthony Barbier7068f992017-10-26 15:23:08 +0100260if env['gles_compute'] and env['os'] != 'android':
Anthony Barbier14c86a92017-12-14 16:27:41 +0000261 env.Append(CPPPATH = ['#/include/linux'])
Anthony Barbiere8a55df2018-10-26 09:05:01 +0100262 SConscript("./opengles-3.1-stubs/SConscript", variant_dir="%s/opengles-3.1-stubs" % build_path, duplicate=0)
Anthony Barbier7068f992017-10-26 15:23:08 +0100263
Anthony Barbiere8a55df2018-10-26 09:05:01 +0100264SConscript('./SConscript', variant_dir=build_path, duplicate=0)
Anthony Barbier6c0348f2017-11-10 18:32:38 +0000265
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100266if env['examples'] and env['os'] != 'bare_metal':
Anthony Barbiere8a55df2018-10-26 09:05:01 +0100267 SConscript('./examples/SConscript', variant_dir='%s/examples' % build_path, duplicate=0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100268
Pablo Tello9e40cf72017-09-15 16:14:55 +0100269if env['os'] != 'bare_metal':
Anthony Barbiere8a55df2018-10-26 09:05:01 +0100270 SConscript('./tests/SConscript', variant_dir='%s/tests' % build_path, duplicate=0)