blob: ede4f7d1d5e64b81119cfe4fa887be4c18246448 [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),
Jenkinsb3a371b2018-05-23 11:36:53 +010057 ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", ""),
58 ("compiler_cache", "Command to prefix to the C and C++ compiler (e.g ccache)", "")
Anthony Barbier46d59272017-05-04 09:15:15 +010059)
60
Anthony Barbierdbdab852017-06-23 15:42:00 +010061env = Environment(platform="posix", variables=vars, ENV = os.environ)
Kaizenbf8b01d2017-10-12 14:26:51 +010062env.Append(LIBPATH = ["#build/%s" % env['build_dir']])
Anthony Barbier06ea0482018-02-22 15:45:35 +000063Export('env')
64Export('vars')
Anthony Barbierdbdab852017-06-23 15:42:00 +010065
66SConsignFile('build/.%s' % env['build_dir'])
Anthony Barbier46d59272017-05-04 09:15:15 +010067
68Help(vars.GenerateHelpText(env))
69
Anthony Barbier06ea0482018-02-22 15:45:35 +000070if env['build'] == "embed_only":
71 SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
72 Return()
73
Anthony Barbierdbdab852017-06-23 15:42:00 +010074if env['neon'] and 'x86' in env['arch']:
ggardete2542c92018-06-29 17:01:01 +020075 print("Cannot compile NEON for x86")
Anthony Barbierdbdab852017-06-23 15:42:00 +010076 Exit(1)
77
78if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
ggardete2542c92018-06-29 17:01:01 +020079 print("Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above")
80 print("Update your version of SCons or use set_soname=0")
Anthony Barbierdbdab852017-06-23 15:42:00 +010081 Exit(1)
82
83if env['os'] == 'bare_metal':
84 if env['cppthreads'] or env['openmp']:
85 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
86 Exit(1)
87
88env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
89 '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
90 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
91 '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
Jenkins52ba29e2018-08-29 15:32:11 +000092 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +000093
Anthony Barbierdbdab852017-06-23 15:42:00 +010094env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
95
Anthony Barbierf45d5a92018-01-24 16:23:15 +000096default_cpp_compiler = 'g++' if env['os'] != 'android' else 'clang++'
97default_c_compiler = 'gcc' if env['os'] != 'android' else 'clang'
98cpp_compiler = os.environ.get('CXX', default_cpp_compiler)
99c_compiler = os.environ.get('CC', default_c_compiler)
100
Jenkinsb3a371b2018-05-23 11:36:53 +0100101if env['os'] == 'android' and ( 'clang++' not in cpp_compiler or 'clang' not in c_compiler ):
ggardete2542c92018-06-29 17:01:01 +0200102 print( "WARNING: Only clang is officially supported to build the Compute Library for Android")
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000103
Jenkinsb3a371b2018-05-23 11:36:53 +0100104if 'clang++' in cpp_compiler:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100105 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
106else:
Jenkins52ba29e2018-08-29 15:32:11 +0000107 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel','-Wno-implicit-fallthrough'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100108
109if env['cppthreads']:
110 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
111
112if env['openmp']:
Jenkinsb3a371b2018-05-23 11:36:53 +0100113 if 'clang++' in cpp_compiler:
ggardete2542c92018-06-29 17:01:01 +0200114 print( "Clang does not support OpenMP. Use scheduler=cpp.")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100115 Exit(1)
116
117 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
118 env.Append(CXXFLAGS = ['-fopenmp'])
119 env.Append(LINKFLAGS = ['-fopenmp'])
120
121prefix = ""
122if env['arch'] == 'armv7a':
123 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
124
Kaizen8938bd32017-09-28 14:38:23 +0100125 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100126 prefix = "arm-linux-gnueabihf-"
127 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Kaizen8938bd32017-09-28 14:38:23 +0100128 elif env['os'] == 'bare_metal':
Kaizenbf8b01d2017-10-12 14:26:51 +0100129 prefix = "arm-eabi-"
Kaizen8938bd32017-09-28 14:38:23 +0100130 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100131 elif env['os'] == 'android':
132 prefix = "arm-linux-androideabi-"
133 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
134elif env['arch'] == 'arm64-v8a':
135 env.Append(CXXFLAGS = ['-march=armv8-a'])
Jenkinsb3a371b2018-05-23 11:36:53 +0100136 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8A','NO_DOT_IN_TOOLCHAIN'])
Kaizen8938bd32017-09-28 14:38:23 +0100137 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100138 prefix = "aarch64-linux-gnu-"
Kaizen8938bd32017-09-28 14:38:23 +0100139 elif env['os'] == 'bare_metal':
Kaizenbf8b01d2017-10-12 14:26:51 +0100140 prefix = "aarch64-elf-"
Anthony Barbierdbdab852017-06-23 15:42:00 +0100141 elif env['os'] == 'android':
142 prefix = "aarch64-linux-android-"
Jenkinsb3a371b2018-05-23 11:36:53 +0100143 if 'clang++' in cpp_compiler:
144 env.Append(CXXFLAGS = ['-no-integrated-as'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100145elif env['arch'] == 'arm64-v8.2-a':
Anthony Barbier06ea0482018-02-22 15:45:35 +0000146 env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16']) # explicitly enable fp16 extension otherwise __ARM_FEATURE_FP16_VECTOR_ARITHMETIC is undefined
Jenkinsb3a371b2018-05-23 11:36:53 +0100147 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8_2','NO_DOT_IN_TOOLCHAIN'])
148 if 'clang++' in cpp_compiler:
149 env.Append(CXXFLAGS = ['-no-integrated-as'])
Kaizen8938bd32017-09-28 14:38:23 +0100150 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100151 prefix = "aarch64-linux-gnu-"
Kaizen8938bd32017-09-28 14:38:23 +0100152 elif env['os'] == 'bare_metal':
153 prefix = "aarch64-elf-"
Anthony Barbierdbdab852017-06-23 15:42:00 +0100154 elif env['os'] == 'android':
155 prefix = "aarch64-linux-android-"
156elif env['arch'] == 'x86_32':
157 env.Append(CCFLAGS = ['-m32'])
158 env.Append(LINKFLAGS = ['-m32'])
159elif env['arch'] == 'x86_64':
160 env.Append(CCFLAGS = ['-m64'])
161 env.Append(LINKFLAGS = ['-m64'])
162
163if env['build'] == 'native':
164 prefix = ""
165
Jenkinsb3a371b2018-05-23 11:36:53 +0100166env['CC'] = env['compiler_cache']+" "+prefix + c_compiler
167env['CXX'] = env['compiler_cache']+" "+prefix + cpp_compiler
Anthony Barbierdbdab852017-06-23 15:42:00 +0100168env['LD'] = prefix + "ld"
169env['AS'] = prefix + "as"
170env['AR'] = prefix + "ar"
171env['RANLIB'] = prefix + "ranlib"
Anthony Barbier46d59272017-05-04 09:15:15 +0100172
173if not GetOption("help"):
Anthony Barbierdbdab852017-06-23 15:42:00 +0100174 try:
bradford barr8d418ab2017-08-07 14:17:09 -0400175 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
Anthony Barbierdbdab852017-06-23 15:42:00 +0100176 except OSError:
177 print("ERROR: Compiler '%s' not found" % env['CXX'])
178 Exit(1)
179
Jenkinsb3a371b2018-05-23 11:36:53 +0100180 if 'clang++' not in cpp_compiler:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100181 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
ggardete2542c92018-06-29 17:01:01 +0200182 print("GCC 6.2.1 or newer is required to compile armv8.2-a code")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100183 Exit(1)
184 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
ggardete2542c92018-06-29 17:01:01 +0200185 print("GCC 4.9 or newer is required to compile NEON code for AArch64")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100186 Exit(1)
187
188 if version_at_least(compiler_ver, '6.1'):
189 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
190
191 if compiler_ver == '4.8.3':
192 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
193
Kaizen8938bd32017-09-28 14:38:23 +0100194if env['standalone']:
195 env.Append(CXXFLAGS = ['-fPIC'])
196 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
197
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']:
Jenkinsb3a371b2018-05-23 11:36:53 +0100213 print("Cannot link OpenCL statically, which is required for bare metal / standalone builds")
Anthony Barbierdbdab852017-06-23 15:42:00 +0100214 Exit(1)
215
Jenkinsb3a371b2018-05-23 11:36:53 +0100216if env['gles_compute']:
217 if env['os'] in ['bare_metal'] or env['standalone']:
218 print("Cannot link OpenGLES statically, which is required for bare metal / standalone builds")
219 Exit(1)
220
221if env["os"] not in ["android", "bare_metal"] and (env['opencl'] or env['cppthreads']):
222 env.Append(LIBS = ['pthread'])
223
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000224if env['opencl'] or env['gles_compute']:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100225 if env['embed_kernels']:
226 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
227
228if env['debug']:
229 env['asserts'] = True
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000230 env['logging'] = True
Anthony Barbierdbdab852017-06-23 15:42:00 +0100231 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
232 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
233else:
234 env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
235
236if env['asserts']:
237 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
Kaizen8938bd32017-09-28 14:38:23 +0100238 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100239
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000240if env['logging']:
241 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
242
Anthony Barbierdbdab852017-06-23 15:42:00 +0100243env.Append(CPPPATH = ['#/include', "#"])
244env.Append(CXXFLAGS = env['extra_cxx_flags'])
245
Anthony Barbierdbdab852017-06-23 15:42:00 +0100246Export('version_at_least')
247
Anthony Barbierdbdab852017-06-23 15:42:00 +0100248if env['opencl']:
249 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
250
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000251if env['gles_compute'] and env['os'] != 'android':
252 env.Append(CPPPATH = ['#/include/linux'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000253 SConscript("./opengles-3.1-stubs/SConscript", variant_dir="build/%s/opengles-3.1-stubs" % env['build_dir'], duplicate=0)
254
255SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
256
Kaizenbf8b01d2017-10-12 14:26:51 +0100257if env['examples'] and env['os'] != 'bare_metal':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100258 SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
259
Kaizen8938bd32017-09-28 14:38:23 +0100260if env['os'] != 'bare_metal':
261 SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)