blob: 9e1c9ca83af221b0ec262b1a5164e39229dc8972 [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),
Anthony Barbier46d59272017-05-04 09:15:15 +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 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),
Anthony Barbier46d59272017-05-04 09:15:15 +010057 ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", "")
58)
59
Anthony Barbierdbdab852017-06-23 15:42:00 +010060env = Environment(platform="posix", variables=vars, ENV = os.environ)
Kaizenbf8b01d2017-10-12 14:26:51 +010061env.Append(LIBPATH = ["#build/%s" % env['build_dir']])
Anthony Barbier06ea0482018-02-22 15:45:35 +000062Export('env')
63Export('vars')
Anthony Barbierdbdab852017-06-23 15:42:00 +010064
65SConsignFile('build/.%s' % env['build_dir'])
Anthony Barbier46d59272017-05-04 09:15:15 +010066
67Help(vars.GenerateHelpText(env))
68
Anthony Barbier06ea0482018-02-22 15:45:35 +000069if env['build'] == "embed_only":
70 SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
71 Return()
72
Anthony Barbierdbdab852017-06-23 15:42:00 +010073if env['neon'] and 'x86' in env['arch']:
74 print "Cannot compile NEON for x86"
75 Exit(1)
76
77if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
78 print "Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above"
79 print "Update your version of SCons or use set_soname=0"
80 Exit(1)
81
82if env['os'] == 'bare_metal':
83 if env['cppthreads'] or env['openmp']:
84 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
85 Exit(1)
86
87env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
88 '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
89 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
90 '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
Anthony Barbier06ea0482018-02-22 15:45:35 +000091 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow','-Wno-implicit-fallthrough'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +000092
Anthony Barbierdbdab852017-06-23 15:42:00 +010093env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
94
Anthony Barbierf45d5a92018-01-24 16:23:15 +000095default_cpp_compiler = 'g++' if env['os'] != 'android' else 'clang++'
96default_c_compiler = 'gcc' if env['os'] != 'android' else 'clang'
97cpp_compiler = os.environ.get('CXX', default_cpp_compiler)
98c_compiler = os.environ.get('CC', default_c_compiler)
99
100if env['os'] == 'android' and ( cpp_compiler != 'clang++' or c_compiler != 'clang'):
101 print "WARNING: Only clang is officially supported to build the Compute Library for Android"
102
103if cpp_compiler == 'clang++':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100104 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
105else:
106 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
107
108if env['cppthreads']:
109 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
110
111if env['openmp']:
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000112 if cpp_compiler == 'clang++':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100113 print "Clang does not support OpenMP. Use scheduler=cpp."
114 Exit(1)
115
116 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
117 env.Append(CXXFLAGS = ['-fopenmp'])
118 env.Append(LINKFLAGS = ['-fopenmp'])
119
120prefix = ""
121if env['arch'] == 'armv7a':
122 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
123
Kaizen8938bd32017-09-28 14:38:23 +0100124 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100125 prefix = "arm-linux-gnueabihf-"
126 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Kaizen8938bd32017-09-28 14:38:23 +0100127 elif env['os'] == 'bare_metal':
Kaizenbf8b01d2017-10-12 14:26:51 +0100128 prefix = "arm-eabi-"
Kaizen8938bd32017-09-28 14:38:23 +0100129 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100130 elif env['os'] == 'android':
131 prefix = "arm-linux-androideabi-"
132 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
133elif env['arch'] == 'arm64-v8a':
134 env.Append(CXXFLAGS = ['-march=armv8-a'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000135 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8A'])
Kaizen8938bd32017-09-28 14:38:23 +0100136 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100137 prefix = "aarch64-linux-gnu-"
Kaizen8938bd32017-09-28 14:38:23 +0100138 elif env['os'] == 'bare_metal':
Kaizenbf8b01d2017-10-12 14:26:51 +0100139 prefix = "aarch64-elf-"
Anthony Barbierdbdab852017-06-23 15:42:00 +0100140 elif env['os'] == 'android':
141 prefix = "aarch64-linux-android-"
142elif env['arch'] == 'arm64-v8.2-a':
Anthony Barbier06ea0482018-02-22 15:45:35 +0000143 env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16']) # explicitly enable fp16 extension otherwise __ARM_FEATURE_FP16_VECTOR_ARITHMETIC is undefined
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000144 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8_2'])
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000145 if cpp_compiler == 'clang++':
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000146 env.Append(CXXFLAGS = ['-fno-integrated-as'])
147
Kaizen8938bd32017-09-28 14:38:23 +0100148 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100149 prefix = "aarch64-linux-gnu-"
Kaizen8938bd32017-09-28 14:38:23 +0100150 elif env['os'] == 'bare_metal':
151 prefix = "aarch64-elf-"
Anthony Barbierdbdab852017-06-23 15:42:00 +0100152 elif env['os'] == 'android':
153 prefix = "aarch64-linux-android-"
154elif env['arch'] == 'x86_32':
155 env.Append(CCFLAGS = ['-m32'])
156 env.Append(LINKFLAGS = ['-m32'])
157elif env['arch'] == 'x86_64':
158 env.Append(CCFLAGS = ['-m64'])
159 env.Append(LINKFLAGS = ['-m64'])
160
161if env['build'] == 'native':
162 prefix = ""
163
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000164env['CC'] = prefix + c_compiler
165env['CXX'] = prefix + cpp_compiler
Anthony Barbierdbdab852017-06-23 15:42:00 +0100166env['LD'] = prefix + "ld"
167env['AS'] = prefix + "as"
168env['AR'] = prefix + "ar"
169env['RANLIB'] = prefix + "ranlib"
Anthony Barbier46d59272017-05-04 09:15:15 +0100170
171if not GetOption("help"):
Anthony Barbierdbdab852017-06-23 15:42:00 +0100172 try:
bradford barr8d418ab2017-08-07 14:17:09 -0400173 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
Anthony Barbierdbdab852017-06-23 15:42:00 +0100174 except OSError:
175 print("ERROR: Compiler '%s' not found" % env['CXX'])
176 Exit(1)
177
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000178 if cpp_compiler == 'g++':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100179 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
180 print "GCC 6.2.1 or newer is required to compile armv8.2-a code"
181 Exit(1)
182 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
183 print "GCC 4.9 or newer is required to compile NEON code for AArch64"
184 Exit(1)
185
186 if version_at_least(compiler_ver, '6.1'):
187 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
188
189 if compiler_ver == '4.8.3':
190 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
191
Kaizen8938bd32017-09-28 14:38:23 +0100192if env['standalone']:
193 env.Append(CXXFLAGS = ['-fPIC'])
194 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000195 if env['cppthreads']:
196 env.Append(LINKFLAGS = ['-lpthread'])
Kaizen8938bd32017-09-28 14:38:23 +0100197
Anthony Barbierdbdab852017-06-23 15:42:00 +0100198if env['Werror']:
199 env.Append(CXXFLAGS = ['-Werror'])
200
201if env['os'] == 'android':
202 env.Append(CPPDEFINES = ['ANDROID'])
203 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
204elif env['os'] == 'bare_metal':
205 env.Append(LINKFLAGS = ['-static'])
Kaizen8938bd32017-09-28 14:38:23 +0100206 env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100207 env.Append(CXXFLAGS = ['-fPIC'])
208 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
Kaizen8938bd32017-09-28 14:38:23 +0100209 env.Append(CPPDEFINES = ['BARE_METAL'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100210
211if env['opencl']:
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000212 if env['os'] in ['bare_metal'] or env['standalone']:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100213 print("Cannot link OpenCL statically, which is required on bare metal")
214 Exit(1)
215
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000216if env['opencl'] or env['gles_compute']:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100217 if env['embed_kernels']:
218 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
219
220if env['debug']:
221 env['asserts'] = True
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000222 env['logging'] = True
Anthony Barbierdbdab852017-06-23 15:42:00 +0100223 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
224 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
225else:
226 env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
227
228if env['asserts']:
229 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
Kaizen8938bd32017-09-28 14:38:23 +0100230 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100231
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000232if env['logging']:
233 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
234
Anthony Barbierdbdab852017-06-23 15:42:00 +0100235env.Append(CPPPATH = ['#/include', "#"])
236env.Append(CXXFLAGS = env['extra_cxx_flags'])
237
Anthony Barbierdbdab852017-06-23 15:42:00 +0100238Export('version_at_least')
239
Anthony Barbierdbdab852017-06-23 15:42:00 +0100240if env['opencl']:
241 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
242
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000243if env['gles_compute'] and env['os'] != 'android':
244 env.Append(CPPPATH = ['#/include/linux'])
245 env.Append(LIBPATH = ["#build/%s/opengles-3.1-stubs" % env['build_dir']])
246 SConscript("./opengles-3.1-stubs/SConscript", variant_dir="build/%s/opengles-3.1-stubs" % env['build_dir'], duplicate=0)
247
248SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
249
Kaizenbf8b01d2017-10-12 14:26:51 +0100250if env['examples'] and env['os'] != 'bare_metal':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100251 SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
252
Kaizen8938bd32017-09-28 14:38:23 +0100253if env['os'] != 'bare_metal':
254 SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)