blob: 4bf90c107d87e55e1f892ea955fbce71beaf3d14 [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)
58 BoolVariable("internal_only", "Enable ARM internal only tests", True),
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 Barbierb2881fc2017-09-29 17:12:12 +010064env.Append(LIBPATH = ["#build/%s" % env['build_dir']])
Anthony Barbier6a3daf12018-02-19 17:24:27 +000065Export('env')
66Export('vars')
Anthony Barbier6ff3b192017-09-04 18:44:23 +010067
68SConsignFile('build/.%s' % env['build_dir'])
69
70Help(vars.GenerateHelpText(env))
71
Anthony Barbier6a3daf12018-02-19 17:24:27 +000072if env['build'] == "embed_only":
73 SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
74 Return()
75
Anthony Barbier6ff3b192017-09-04 18:44:23 +010076if env['neon'] and 'x86' in env['arch']:
77 print "Cannot compile NEON for x86"
78 Exit(1)
79
80if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
81 print "Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above"
82 print "Update your version of SCons or use set_soname=0"
83 Exit(1)
84
85if env['os'] == 'bare_metal':
86 if env['cppthreads'] or env['openmp']:
87 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
88 Exit(1)
89
90env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
91 '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
92 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
93 '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
Anthony Barbier6fa009e2018-02-16 16:49:39 +000094 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow','-Wno-implicit-fallthrough'])
Isabella Gottardib28f29d2017-11-09 17:05:07 +000095
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
97
Anthony Barbiera026e982018-01-18 10:57:52 +000098default_cpp_compiler = 'g++' if env['os'] != 'android' else 'clang++'
99default_c_compiler = 'gcc' if env['os'] != 'android' else 'clang'
100cpp_compiler = os.environ.get('CXX', default_cpp_compiler)
101c_compiler = os.environ.get('CC', default_c_compiler)
102
Anthony Barbierdd669372018-03-01 17:08:54 +0000103if env['os'] == 'android' and ( 'clang++' not in cpp_compiler or 'clang' not in c_compiler ):
Anthony Barbiera026e982018-01-18 10:57:52 +0000104 print "WARNING: Only clang is officially supported to build the Compute Library for Android"
105
Anthony Barbierdd669372018-03-01 17:08:54 +0000106if 'clang++' in cpp_compiler:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
108else:
109 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
110
111if env['cppthreads']:
112 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
113
114if env['openmp']:
Anthony Barbierdd669372018-03-01 17:08:54 +0000115 if 'clang++' in cpp_compiler:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116 print "Clang does not support OpenMP. Use scheduler=cpp."
117 Exit(1)
118
119 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
120 env.Append(CXXFLAGS = ['-fopenmp'])
121 env.Append(LINKFLAGS = ['-fopenmp'])
122
123prefix = ""
124if env['arch'] == 'armv7a':
125 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
126
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100127 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 prefix = "arm-linux-gnueabihf-"
129 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100130 elif env['os'] == 'bare_metal':
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100131 prefix = "arm-eabi-"
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100132 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133 elif env['os'] == 'android':
134 prefix = "arm-linux-androideabi-"
135 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
136elif env['arch'] == 'arm64-v8a':
137 env.Append(CXXFLAGS = ['-march=armv8-a'])
Pablo Telloeb82fd22018-02-23 13:43:50 +0000138 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8A','NO_DOT_IN_TOOLCHAIN'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100139 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 prefix = "aarch64-linux-gnu-"
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100141 elif env['os'] == 'bare_metal':
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100142 prefix = "aarch64-elf-"
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143 elif env['os'] == 'android':
144 prefix = "aarch64-linux-android-"
Pablo Telloeb82fd22018-02-23 13:43:50 +0000145 env.Append(CXXFLAGS = ['-no-integrated-as'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146elif env['arch'] == 'arm64-v8.2-a':
Pablo Tellofa7dc842018-01-25 10:48:46 +0000147 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 +0000148 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8_2','NO_DOT_IN_TOOLCHAIN'])
Anthony Barbierdd669372018-03-01 17:08:54 +0000149 if 'clang++' in cpp_compiler:
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000150 env.Append(CXXFLAGS = ['-fno-integrated-as'])
Pablo Tello9e40cf72017-09-15 16:14:55 +0100151 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 prefix = "aarch64-linux-gnu-"
Pablo Tello9e40cf72017-09-15 16:14:55 +0100153 elif env['os'] == 'bare_metal':
154 prefix = "aarch64-elf-"
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 elif env['os'] == 'android':
156 prefix = "aarch64-linux-android-"
157elif env['arch'] == 'x86_32':
158 env.Append(CCFLAGS = ['-m32'])
159 env.Append(LINKFLAGS = ['-m32'])
160elif env['arch'] == 'x86_64':
161 env.Append(CCFLAGS = ['-m64'])
162 env.Append(LINKFLAGS = ['-m64'])
163
164if env['build'] == 'native':
165 prefix = ""
166
Anthony Barbier7390e052018-03-13 09:29:41 +0000167env['CC'] = env['compiler_cache']+" "+prefix + c_compiler
168env['CXX'] = env['compiler_cache']+" "+prefix + cpp_compiler
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169env['LD'] = prefix + "ld"
170env['AS'] = prefix + "as"
171env['AR'] = prefix + "ar"
172env['RANLIB'] = prefix + "ranlib"
173
Georgios Pinitasa18aee72018-03-13 19:52:02 +0000174if 'ccache' in env['compiler_cache'] and 'clang++' in cpp_compiler:
175 env['ENV']['CCACHE_CPP2'] = 'yes'
176
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177if not GetOption("help"):
178 try:
Anthony Barbier907dba82017-08-25 10:11:39 +0100179 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100180 except OSError:
181 print("ERROR: Compiler '%s' not found" % env['CXX'])
182 Exit(1)
183
Anthony Barbierdd669372018-03-01 17:08:54 +0000184 if 'clang++' not in cpp_compiler:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
186 print "GCC 6.2.1 or newer is required to compile armv8.2-a code"
187 Exit(1)
188 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
189 print "GCC 4.9 or newer is required to compile NEON code for AArch64"
190 Exit(1)
191
192 if version_at_least(compiler_ver, '6.1'):
193 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
194
195 if compiler_ver == '4.8.3':
196 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
197
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100198if env['standalone']:
199 env.Append(CXXFLAGS = ['-fPIC'])
200 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
Pablo Tello89519332017-11-17 11:52:36 +0000201 if env['cppthreads']:
202 env.Append(LINKFLAGS = ['-lpthread'])
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100203
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204if env['Werror']:
205 env.Append(CXXFLAGS = ['-Werror'])
206
207if env['os'] == 'android':
208 env.Append(CPPDEFINES = ['ANDROID'])
Georgios Pinitas9873ea32017-12-05 15:28:55 +0000209 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100210elif env['os'] == 'bare_metal':
211 env.Append(LINKFLAGS = ['-static'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100212 env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213 env.Append(CXXFLAGS = ['-fPIC'])
214 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100215 env.Append(CPPDEFINES = ['BARE_METAL'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216
217if env['opencl']:
Georgios Pinitase6032da2017-10-26 14:13:35 +0100218 if env['os'] in ['bare_metal'] or env['standalone']:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100219 print("Cannot link OpenCL statically, which is required on bare metal")
220 Exit(1)
221
Anthony Barbier7068f992017-10-26 15:23:08 +0100222if env['opencl'] or env['gles_compute']:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223 if env['embed_kernels']:
224 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
225
226if env['debug']:
227 env['asserts'] = True
Georgios Pinitas3faea252017-10-30 14:13:50 +0000228 env['logging'] = True
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
230 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
231else:
232 env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
233
234if env['asserts']:
235 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
Moritz Pflanzer8b74c782017-09-14 14:33:20 +0100236 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100238if env['logging']:
239 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
240
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241env.Append(CPPPATH = ['#/include', "#"])
242env.Append(CXXFLAGS = env['extra_cxx_flags'])
243
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100244Export('version_at_least')
245
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246if env['opencl']:
247 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
248
Anthony Barbier7068f992017-10-26 15:23:08 +0100249if env['gles_compute'] and env['os'] != 'android':
Anthony Barbier14c86a92017-12-14 16:27:41 +0000250 env.Append(CPPPATH = ['#/include/linux'])
251 env.Append(LIBPATH = ["#build/%s/opengles-3.1-stubs" % env['build_dir']])
252 SConscript("./opengles-3.1-stubs/SConscript", variant_dir="build/%s/opengles-3.1-stubs" % env['build_dir'], duplicate=0)
Anthony Barbier7068f992017-10-26 15:23:08 +0100253
Anthony Barbier6c0348f2017-11-10 18:32:38 +0000254SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
255
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100256if env['examples'] and env['os'] != 'bare_metal':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100257 SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
258
Pablo Tello9e40cf72017-09-15 16:14:55 +0100259if env['os'] != 'bare_metal':
260 SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)