blob: b74a6b9a2a77e7cd96632af6a6d4156af43908e1 [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 Barbier6ff3b192017-09-04 18:44:23 +010059 ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", "")
60)
61
62env = Environment(platform="posix", variables=vars, ENV = os.environ)
Anthony Barbierb2881fc2017-09-29 17:12:12 +010063env.Append(LIBPATH = ["#build/%s" % env['build_dir']])
Anthony Barbier6a3daf12018-02-19 17:24:27 +000064Export('env')
65Export('vars')
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066
67SConsignFile('build/.%s' % env['build_dir'])
68
69Help(vars.GenerateHelpText(env))
70
Anthony Barbier6a3daf12018-02-19 17:24:27 +000071if env['build'] == "embed_only":
72 SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
73 Return()
74
Anthony Barbier6ff3b192017-09-04 18:44:23 +010075if env['neon'] and 'x86' in env['arch']:
76 print "Cannot compile NEON for x86"
77 Exit(1)
78
79if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
80 print "Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above"
81 print "Update your version of SCons or use set_soname=0"
82 Exit(1)
83
84if env['os'] == 'bare_metal':
85 if env['cppthreads'] or env['openmp']:
86 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
87 Exit(1)
88
89env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
90 '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
91 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
92 '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
Anthony Barbier6fa009e2018-02-16 16:49:39 +000093 '-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 +000094
Anthony Barbier6ff3b192017-09-04 18:44:23 +010095env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
96
Anthony Barbiera026e982018-01-18 10:57:52 +000097default_cpp_compiler = 'g++' if env['os'] != 'android' else 'clang++'
98default_c_compiler = 'gcc' if env['os'] != 'android' else 'clang'
99cpp_compiler = os.environ.get('CXX', default_cpp_compiler)
100c_compiler = os.environ.get('CC', default_c_compiler)
101
102if env['os'] == 'android' and ( cpp_compiler != 'clang++' or c_compiler != 'clang'):
103 print "WARNING: Only clang is officially supported to build the Compute Library for Android"
104
105if cpp_compiler == 'clang++':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
107else:
108 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
109
110if env['cppthreads']:
111 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
112
113if env['openmp']:
Anthony Barbiera026e982018-01-18 10:57:52 +0000114 if cpp_compiler == 'clang++':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 print "Clang does not support OpenMP. Use scheduler=cpp."
116 Exit(1)
117
118 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
119 env.Append(CXXFLAGS = ['-fopenmp'])
120 env.Append(LINKFLAGS = ['-fopenmp'])
121
122prefix = ""
123if env['arch'] == 'armv7a':
124 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
125
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100126 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127 prefix = "arm-linux-gnueabihf-"
128 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100129 elif env['os'] == 'bare_metal':
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100130 prefix = "arm-eabi-"
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100131 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132 elif env['os'] == 'android':
133 prefix = "arm-linux-androideabi-"
134 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
135elif env['arch'] == 'arm64-v8a':
136 env.Append(CXXFLAGS = ['-march=armv8-a'])
Pablo Tello181e6512017-11-15 13:28:27 +0000137 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8A'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100138 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139 prefix = "aarch64-linux-gnu-"
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100140 elif env['os'] == 'bare_metal':
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100141 prefix = "aarch64-elf-"
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100142 elif env['os'] == 'android':
143 prefix = "aarch64-linux-android-"
144elif env['arch'] == 'arm64-v8.2-a':
Pablo Tellofa7dc842018-01-25 10:48:46 +0000145 env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16']) # explicitly enable fp16 extension otherwise __ARM_FEATURE_FP16_VECTOR_ARITHMETIC is undefined
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100146 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8_2'])
Anthony Barbiera026e982018-01-18 10:57:52 +0000147 if cpp_compiler == 'clang++':
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000148 env.Append(CXXFLAGS = ['-fno-integrated-as'])
149
Pablo Tello9e40cf72017-09-15 16:14:55 +0100150 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 prefix = "aarch64-linux-gnu-"
Pablo Tello9e40cf72017-09-15 16:14:55 +0100152 elif env['os'] == 'bare_metal':
153 prefix = "aarch64-elf-"
Anthony Barbier6ff3b192017-09-04 18:44:23 +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
Anthony Barbiera026e982018-01-18 10:57:52 +0000166env['CC'] = prefix + c_compiler
167env['CXX'] = prefix + cpp_compiler
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100168env['LD'] = prefix + "ld"
169env['AS'] = prefix + "as"
170env['AR'] = prefix + "ar"
171env['RANLIB'] = prefix + "ranlib"
172
173if not GetOption("help"):
174 try:
Anthony Barbier907dba82017-08-25 10:11:39 +0100175 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 except OSError:
177 print("ERROR: Compiler '%s' not found" % env['CXX'])
178 Exit(1)
179
Anthony Barbiera026e982018-01-18 10:57:52 +0000180 if cpp_compiler == 'g++':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
182 print "GCC 6.2.1 or newer is required to compile armv8.2-a code"
183 Exit(1)
184 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
185 print "GCC 4.9 or newer is required to compile NEON code for AArch64"
186 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
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100194if env['standalone']:
195 env.Append(CXXFLAGS = ['-fPIC'])
196 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
Pablo Tello89519332017-11-17 11:52:36 +0000197 if env['cppthreads']:
198 env.Append(LINKFLAGS = ['-lpthread'])
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100199
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200if env['Werror']:
201 env.Append(CXXFLAGS = ['-Werror'])
202
203if env['os'] == 'android':
204 env.Append(CPPDEFINES = ['ANDROID'])
Georgios Pinitas9873ea32017-12-05 15:28:55 +0000205 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206elif env['os'] == 'bare_metal':
207 env.Append(LINKFLAGS = ['-static'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100208 env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100209 env.Append(CXXFLAGS = ['-fPIC'])
210 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100211 env.Append(CPPDEFINES = ['BARE_METAL'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212
213if env['opencl']:
Georgios Pinitase6032da2017-10-26 14:13:35 +0100214 if env['os'] in ['bare_metal'] or env['standalone']:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215 print("Cannot link OpenCL statically, which is required on bare metal")
216 Exit(1)
217
Anthony Barbier7068f992017-10-26 15:23:08 +0100218if env['opencl'] or env['gles_compute']:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100219 if env['embed_kernels']:
220 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
221
222if env['debug']:
223 env['asserts'] = True
Georgios Pinitas3faea252017-10-30 14:13:50 +0000224 env['logging'] = True
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
226 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
227else:
228 env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
229
230if env['asserts']:
231 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
Moritz Pflanzer8b74c782017-09-14 14:33:20 +0100232 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100233
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100234if env['logging']:
235 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
236
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237env.Append(CPPPATH = ['#/include', "#"])
238env.Append(CXXFLAGS = env['extra_cxx_flags'])
239
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100240Export('version_at_least')
241
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100242if env['opencl']:
243 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
244
Anthony Barbier7068f992017-10-26 15:23:08 +0100245if env['gles_compute'] and env['os'] != 'android':
Anthony Barbier14c86a92017-12-14 16:27:41 +0000246 env.Append(CPPPATH = ['#/include/linux'])
247 env.Append(LIBPATH = ["#build/%s/opengles-3.1-stubs" % env['build_dir']])
248 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 +0100249
Anthony Barbier6c0348f2017-11-10 18:32:38 +0000250SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
251
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100252if env['examples'] and env['os'] != 'bare_metal':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100253 SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
254
Pablo Tello9e40cf72017-09-15 16:14:55 +0100255if env['os'] != 'bare_metal':
256 SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)