blob: 402bbb41e1dbf9aff43b305b0585503f05a52b8c [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")),
45 EnumVariable("build", "Build type", "cross_compile", allowed_values=("native", "cross_compile")),
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 Barbierdbdab852017-06-23 15:42:00 +010062
63SConsignFile('build/.%s' % env['build_dir'])
Anthony Barbier46d59272017-05-04 09:15:15 +010064
65Help(vars.GenerateHelpText(env))
66
Anthony Barbierdbdab852017-06-23 15:42:00 +010067if env['neon'] and 'x86' in env['arch']:
68 print "Cannot compile NEON for x86"
69 Exit(1)
70
71if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
72 print "Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above"
73 print "Update your version of SCons or use set_soname=0"
74 Exit(1)
75
76if env['os'] == 'bare_metal':
77 if env['cppthreads'] or env['openmp']:
78 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
79 Exit(1)
80
81env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
82 '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
83 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
84 '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
85 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +000086
Anthony Barbierdbdab852017-06-23 15:42:00 +010087env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
88
Anthony Barbierf45d5a92018-01-24 16:23:15 +000089default_cpp_compiler = 'g++' if env['os'] != 'android' else 'clang++'
90default_c_compiler = 'gcc' if env['os'] != 'android' else 'clang'
91cpp_compiler = os.environ.get('CXX', default_cpp_compiler)
92c_compiler = os.environ.get('CC', default_c_compiler)
93
94if env['os'] == 'android' and ( cpp_compiler != 'clang++' or c_compiler != 'clang'):
95 print "WARNING: Only clang is officially supported to build the Compute Library for Android"
96
97if cpp_compiler == 'clang++':
Anthony Barbierdbdab852017-06-23 15:42:00 +010098 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
99else:
100 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
101
102if env['cppthreads']:
103 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
104
105if env['openmp']:
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000106 if cpp_compiler == 'clang++':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100107 print "Clang does not support OpenMP. Use scheduler=cpp."
108 Exit(1)
109
110 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
111 env.Append(CXXFLAGS = ['-fopenmp'])
112 env.Append(LINKFLAGS = ['-fopenmp'])
113
114prefix = ""
115if env['arch'] == 'armv7a':
116 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
117
Kaizen8938bd32017-09-28 14:38:23 +0100118 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100119 prefix = "arm-linux-gnueabihf-"
120 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Kaizen8938bd32017-09-28 14:38:23 +0100121 elif env['os'] == 'bare_metal':
Kaizenbf8b01d2017-10-12 14:26:51 +0100122 prefix = "arm-eabi-"
Kaizen8938bd32017-09-28 14:38:23 +0100123 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100124 elif env['os'] == 'android':
125 prefix = "arm-linux-androideabi-"
126 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
127elif env['arch'] == 'arm64-v8a':
128 env.Append(CXXFLAGS = ['-march=armv8-a'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000129 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8A'])
Kaizen8938bd32017-09-28 14:38:23 +0100130 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100131 prefix = "aarch64-linux-gnu-"
Kaizen8938bd32017-09-28 14:38:23 +0100132 elif env['os'] == 'bare_metal':
Kaizenbf8b01d2017-10-12 14:26:51 +0100133 prefix = "aarch64-elf-"
Anthony Barbierdbdab852017-06-23 15:42:00 +0100134 elif env['os'] == 'android':
135 prefix = "aarch64-linux-android-"
136elif env['arch'] == 'arm64-v8.2-a':
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000137 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8_2'])
138
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000139 if cpp_compiler == 'clang++':
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000140 env.Append(CXXFLAGS = ['-fno-integrated-as'])
141
Kaizen8938bd32017-09-28 14:38:23 +0100142 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100143 prefix = "aarch64-linux-gnu-"
Kaizen8938bd32017-09-28 14:38:23 +0100144 elif env['os'] == 'bare_metal':
145 prefix = "aarch64-elf-"
Anthony Barbierdbdab852017-06-23 15:42:00 +0100146 elif env['os'] == 'android':
147 prefix = "aarch64-linux-android-"
148elif env['arch'] == 'x86_32':
149 env.Append(CCFLAGS = ['-m32'])
150 env.Append(LINKFLAGS = ['-m32'])
151elif env['arch'] == 'x86_64':
152 env.Append(CCFLAGS = ['-m64'])
153 env.Append(LINKFLAGS = ['-m64'])
154
155if env['build'] == 'native':
156 prefix = ""
157
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000158env['CC'] = prefix + c_compiler
159env['CXX'] = prefix + cpp_compiler
Anthony Barbierdbdab852017-06-23 15:42:00 +0100160env['LD'] = prefix + "ld"
161env['AS'] = prefix + "as"
162env['AR'] = prefix + "ar"
163env['RANLIB'] = prefix + "ranlib"
Anthony Barbier46d59272017-05-04 09:15:15 +0100164
165if not GetOption("help"):
Anthony Barbierdbdab852017-06-23 15:42:00 +0100166 try:
bradford barr8d418ab2017-08-07 14:17:09 -0400167 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
Anthony Barbierdbdab852017-06-23 15:42:00 +0100168 except OSError:
169 print("ERROR: Compiler '%s' not found" % env['CXX'])
170 Exit(1)
171
Anthony Barbierf45d5a92018-01-24 16:23:15 +0000172 if cpp_compiler == 'g++':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100173 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
174 print "GCC 6.2.1 or newer is required to compile armv8.2-a code"
175 Exit(1)
176 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
177 print "GCC 4.9 or newer is required to compile NEON code for AArch64"
178 Exit(1)
179
180 if version_at_least(compiler_ver, '6.1'):
181 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
182
183 if compiler_ver == '4.8.3':
184 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
185
Kaizen8938bd32017-09-28 14:38:23 +0100186if env['standalone']:
187 env.Append(CXXFLAGS = ['-fPIC'])
188 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000189 if env['cppthreads']:
190 env.Append(LINKFLAGS = ['-lpthread'])
Kaizen8938bd32017-09-28 14:38:23 +0100191
Anthony Barbierdbdab852017-06-23 15:42:00 +0100192if env['Werror']:
193 env.Append(CXXFLAGS = ['-Werror'])
194
195if env['os'] == 'android':
196 env.Append(CPPDEFINES = ['ANDROID'])
197 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
198elif env['os'] == 'bare_metal':
199 env.Append(LINKFLAGS = ['-static'])
Kaizen8938bd32017-09-28 14:38:23 +0100200 env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100201 env.Append(CXXFLAGS = ['-fPIC'])
202 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
Kaizen8938bd32017-09-28 14:38:23 +0100203 env.Append(CPPDEFINES = ['BARE_METAL'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100204
205if env['opencl']:
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000206 if env['os'] in ['bare_metal'] or env['standalone']:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100207 print("Cannot link OpenCL statically, which is required on bare metal")
208 Exit(1)
209
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000210if env['opencl'] or env['gles_compute']:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100211 if env['embed_kernels']:
212 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
213
214if env['debug']:
215 env['asserts'] = True
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000216 env['logging'] = True
Anthony Barbierdbdab852017-06-23 15:42:00 +0100217 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
218 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
219else:
220 env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
221
222if env['asserts']:
223 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
Kaizen8938bd32017-09-28 14:38:23 +0100224 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100225
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000226if env['logging']:
227 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
228
Anthony Barbierdbdab852017-06-23 15:42:00 +0100229env.Append(CPPPATH = ['#/include', "#"])
230env.Append(CXXFLAGS = env['extra_cxx_flags'])
231
232Export('vars')
233Export('env')
234Export('version_at_least')
235
Anthony Barbierdbdab852017-06-23 15:42:00 +0100236if env['opencl']:
237 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
238
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000239if env['gles_compute'] and env['os'] != 'android':
240 env.Append(CPPPATH = ['#/include/linux'])
241 env.Append(LIBPATH = ["#build/%s/opengles-3.1-stubs" % env['build_dir']])
242 SConscript("./opengles-3.1-stubs/SConscript", variant_dir="build/%s/opengles-3.1-stubs" % env['build_dir'], duplicate=0)
243
244SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
245
Kaizenbf8b01d2017-10-12 14:26:51 +0100246if env['examples'] and env['os'] != 'bare_metal':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100247 SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
248
Kaizen8938bd32017-09-28 14:38:23 +0100249if env['os'] != 'bare_metal':
250 SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)