blob: 090b94200f257cc51ac637807830bd19dffe3a7c [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),
52 BoolVariable("embed_kernels", "Embed OpenCL kernels and OpenGL ES compute shaders in library binary", False),
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
89if os.environ.get('CXX', 'g++') == 'clang++':
90 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
91else:
92 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
93
94if env['cppthreads']:
95 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
96
97if env['openmp']:
98 if os.environ.get('CXX', 'g++') == 'clang++':
99 print "Clang does not support OpenMP. Use scheduler=cpp."
100 Exit(1)
101
102 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
103 env.Append(CXXFLAGS = ['-fopenmp'])
104 env.Append(LINKFLAGS = ['-fopenmp'])
105
106prefix = ""
107if env['arch'] == 'armv7a':
108 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
109
Kaizen8938bd32017-09-28 14:38:23 +0100110 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100111 prefix = "arm-linux-gnueabihf-"
112 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Kaizen8938bd32017-09-28 14:38:23 +0100113 elif env['os'] == 'bare_metal':
Kaizenbf8b01d2017-10-12 14:26:51 +0100114 prefix = "arm-eabi-"
Kaizen8938bd32017-09-28 14:38:23 +0100115 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100116 elif env['os'] == 'android':
117 prefix = "arm-linux-androideabi-"
118 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
119elif env['arch'] == 'arm64-v8a':
120 env.Append(CXXFLAGS = ['-march=armv8-a'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000121 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8A'])
Kaizen8938bd32017-09-28 14:38:23 +0100122 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100123 prefix = "aarch64-linux-gnu-"
Kaizen8938bd32017-09-28 14:38:23 +0100124 elif env['os'] == 'bare_metal':
Kaizenbf8b01d2017-10-12 14:26:51 +0100125 prefix = "aarch64-elf-"
Anthony Barbierdbdab852017-06-23 15:42:00 +0100126 elif env['os'] == 'android':
127 prefix = "aarch64-linux-android-"
128elif env['arch'] == 'arm64-v8.2-a':
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000129 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8_2'])
130
131 if os.environ.get('CXX', 'g++') == 'clang++':
132 env.Append(CXXFLAGS = ['-fno-integrated-as'])
133
Kaizen8938bd32017-09-28 14:38:23 +0100134 if env['os'] == 'linux':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100135 prefix = "aarch64-linux-gnu-"
Kaizen8938bd32017-09-28 14:38:23 +0100136 elif env['os'] == 'bare_metal':
137 prefix = "aarch64-elf-"
Anthony Barbierdbdab852017-06-23 15:42:00 +0100138 elif env['os'] == 'android':
139 prefix = "aarch64-linux-android-"
140elif env['arch'] == 'x86_32':
141 env.Append(CCFLAGS = ['-m32'])
142 env.Append(LINKFLAGS = ['-m32'])
143elif env['arch'] == 'x86_64':
144 env.Append(CCFLAGS = ['-m64'])
145 env.Append(LINKFLAGS = ['-m64'])
146
147if env['build'] == 'native':
148 prefix = ""
149
150env['CC'] = prefix + os.environ.get('CC', 'gcc')
151env['CXX'] = prefix + os.environ.get('CXX', 'g++')
152env['LD'] = prefix + "ld"
153env['AS'] = prefix + "as"
154env['AR'] = prefix + "ar"
155env['RANLIB'] = prefix + "ranlib"
Anthony Barbier46d59272017-05-04 09:15:15 +0100156
157if not GetOption("help"):
Anthony Barbierdbdab852017-06-23 15:42:00 +0100158 try:
bradford barr8d418ab2017-08-07 14:17:09 -0400159 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
Anthony Barbierdbdab852017-06-23 15:42:00 +0100160 except OSError:
161 print("ERROR: Compiler '%s' not found" % env['CXX'])
162 Exit(1)
163
164 if os.environ.get('CXX','g++') == 'g++':
165 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
166 print "GCC 6.2.1 or newer is required to compile armv8.2-a code"
167 Exit(1)
168 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
169 print "GCC 4.9 or newer is required to compile NEON code for AArch64"
170 Exit(1)
171
172 if version_at_least(compiler_ver, '6.1'):
173 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
174
175 if compiler_ver == '4.8.3':
176 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
177
Kaizen8938bd32017-09-28 14:38:23 +0100178if env['standalone']:
179 env.Append(CXXFLAGS = ['-fPIC'])
180 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000181 if env['cppthreads']:
182 env.Append(LINKFLAGS = ['-lpthread'])
Kaizen8938bd32017-09-28 14:38:23 +0100183
Anthony Barbierdbdab852017-06-23 15:42:00 +0100184if env['Werror']:
185 env.Append(CXXFLAGS = ['-Werror'])
186
187if env['os'] == 'android':
188 env.Append(CPPDEFINES = ['ANDROID'])
189 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
190elif env['os'] == 'bare_metal':
191 env.Append(LINKFLAGS = ['-static'])
Kaizen8938bd32017-09-28 14:38:23 +0100192 env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100193 env.Append(CXXFLAGS = ['-fPIC'])
194 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
Kaizen8938bd32017-09-28 14:38:23 +0100195 env.Append(CPPDEFINES = ['BARE_METAL'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100196
197if env['opencl']:
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000198 if env['os'] in ['bare_metal'] or env['standalone']:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100199 print("Cannot link OpenCL statically, which is required on bare metal")
200 Exit(1)
201
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000202if env['opencl'] or env['gles_compute']:
Anthony Barbierdbdab852017-06-23 15:42:00 +0100203 if env['embed_kernels']:
204 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
205
206if env['debug']:
207 env['asserts'] = True
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000208 env['logging'] = True
Anthony Barbierdbdab852017-06-23 15:42:00 +0100209 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
210 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
211else:
212 env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
213
214if env['asserts']:
215 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
Kaizen8938bd32017-09-28 14:38:23 +0100216 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
Anthony Barbierdbdab852017-06-23 15:42:00 +0100217
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000218if env['logging']:
219 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
220
Anthony Barbierdbdab852017-06-23 15:42:00 +0100221env.Append(CPPPATH = ['#/include', "#"])
222env.Append(CXXFLAGS = env['extra_cxx_flags'])
223
224Export('vars')
225Export('env')
226Export('version_at_least')
227
Anthony Barbierdbdab852017-06-23 15:42:00 +0100228if env['opencl']:
229 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
230
Anthony Barbier8140e1e2017-12-14 23:48:46 +0000231if env['gles_compute'] and env['os'] != 'android':
232 env.Append(CPPPATH = ['#/include/linux'])
233 env.Append(LIBPATH = ["#build/%s/opengles-3.1-stubs" % env['build_dir']])
234 SConscript("./opengles-3.1-stubs/SConscript", variant_dir="build/%s/opengles-3.1-stubs" % env['build_dir'], duplicate=0)
235
236SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
237
Kaizenbf8b01d2017-10-12 14:26:51 +0100238if env['examples'] and env['os'] != 'bare_metal':
Anthony Barbierdbdab852017-06-23 15:42:00 +0100239 SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
240
Kaizen8938bd32017-09-28 14:38:23 +0100241if env['os'] != 'bare_metal':
242 SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)