blob: 4428a09cd037c7c98fb32d2bac9d9bb219848776 [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),
42 EnumVariable("arch", "Target Architecture", "armv7a", allowed_values=("armv7a", "arm64-v8a", "arm64-v8.2-a", "x86_32", "x86_64")),
43 EnumVariable("os", "Target OS", "linux", allowed_values=("linux", "android", "bare_metal")),
44 EnumVariable("build", "Build type", "cross_compile", allowed_values=("native", "cross_compile")),
45 BoolVariable("examples", "Build example programs", True),
46 BoolVariable("Werror", "Enable/disable the -Werror compilation flag", True),
Pablo Telloc6cb35a2017-06-21 15:39:47 +010047 BoolVariable("standalone", "Builds the tests as standalone executables, links statically with libgcc, libstdc++ and libarm_compute", False),
Anthony Barbier6ff3b192017-09-04 18:44:23 +010048 BoolVariable("opencl", "Enable OpenCL support", True),
49 BoolVariable("neon", "Enable Neon support", False),
50 BoolVariable("embed_kernels", "Embed OpenCL kernels in library binary", False),
51 BoolVariable("set_soname", "Set the library's soname and shlibversion (requires SCons 2.4 or above)", False),
52 BoolVariable("openmp", "Enable OpenMP backend", False),
53 BoolVariable("cppthreads", "Enable C++11 threads backend", True),
54 PathVariable("build_dir", "Specify sub-folder for the build", ".", PathVariable.PathAccept),
55 ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", "")
56)
57
58env = Environment(platform="posix", variables=vars, ENV = os.environ)
Anthony Barbierb2881fc2017-09-29 17:12:12 +010059env.Append(LIBPATH = ["#build/%s" % env['build_dir']])
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060
61SConsignFile('build/.%s' % env['build_dir'])
62
63Help(vars.GenerateHelpText(env))
64
65if env['neon'] and 'x86' in env['arch']:
66 print "Cannot compile NEON for x86"
67 Exit(1)
68
69if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
70 print "Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above"
71 print "Update your version of SCons or use set_soname=0"
72 Exit(1)
73
74if env['os'] == 'bare_metal':
75 if env['cppthreads'] or env['openmp']:
76 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
77 Exit(1)
78
79env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
80 '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
81 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
82 '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
83 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow'])
84env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
85
86if os.environ.get('CXX', 'g++') == 'clang++':
87 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
88else:
89 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
90
91if env['cppthreads']:
92 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
93
94if env['openmp']:
95 if os.environ.get('CXX', 'g++') == 'clang++':
96 print "Clang does not support OpenMP. Use scheduler=cpp."
97 Exit(1)
98
99 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
100 env.Append(CXXFLAGS = ['-fopenmp'])
101 env.Append(LINKFLAGS = ['-fopenmp'])
102
103prefix = ""
104if env['arch'] == 'armv7a':
105 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
106
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100107 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108 prefix = "arm-linux-gnueabihf-"
109 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100110 elif env['os'] == 'bare_metal':
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100111 prefix = "arm-eabi-"
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100112 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 elif env['os'] == 'android':
114 prefix = "arm-linux-androideabi-"
115 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
116elif env['arch'] == 'arm64-v8a':
117 env.Append(CXXFLAGS = ['-march=armv8-a'])
118
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100119 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120 prefix = "aarch64-linux-gnu-"
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100121 elif env['os'] == 'bare_metal':
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100122 prefix = "aarch64-elf-"
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 elif env['os'] == 'android':
124 prefix = "aarch64-linux-android-"
125elif env['arch'] == 'arm64-v8.2-a':
126 env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16+simd'])
127 env.Append(CPPDEFINES = ['ARM_COMPUTE_ENABLE_FP16'])
Pablo Tello9e40cf72017-09-15 16:14:55 +0100128 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 prefix = "aarch64-linux-gnu-"
Pablo Tello9e40cf72017-09-15 16:14:55 +0100130 elif env['os'] == 'bare_metal':
131 prefix = "aarch64-elf-"
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132 elif env['os'] == 'android':
133 prefix = "aarch64-linux-android-"
134elif env['arch'] == 'x86_32':
135 env.Append(CCFLAGS = ['-m32'])
136 env.Append(LINKFLAGS = ['-m32'])
137elif env['arch'] == 'x86_64':
138 env.Append(CCFLAGS = ['-m64'])
139 env.Append(LINKFLAGS = ['-m64'])
140
141if env['build'] == 'native':
142 prefix = ""
143
144env['CC'] = prefix + os.environ.get('CC', 'gcc')
145env['CXX'] = prefix + os.environ.get('CXX', 'g++')
146env['LD'] = prefix + "ld"
147env['AS'] = prefix + "as"
148env['AR'] = prefix + "ar"
149env['RANLIB'] = prefix + "ranlib"
150
151if not GetOption("help"):
152 try:
Anthony Barbier907dba82017-08-25 10:11:39 +0100153 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 except OSError:
155 print("ERROR: Compiler '%s' not found" % env['CXX'])
156 Exit(1)
157
158 if os.environ.get('CXX','g++') == 'g++':
159 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
160 print "GCC 6.2.1 or newer is required to compile armv8.2-a code"
161 Exit(1)
162 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
163 print "GCC 4.9 or newer is required to compile NEON code for AArch64"
164 Exit(1)
165
166 if version_at_least(compiler_ver, '6.1'):
167 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
168
169 if compiler_ver == '4.8.3':
170 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
171
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100172if env['standalone']:
173 env.Append(CXXFLAGS = ['-fPIC'])
174 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
175
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176if env['Werror']:
177 env.Append(CXXFLAGS = ['-Werror'])
178
179if env['os'] == 'android':
180 env.Append(CPPDEFINES = ['ANDROID'])
181 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
182elif env['os'] == 'bare_metal':
183 env.Append(LINKFLAGS = ['-static'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100184 env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 env.Append(CXXFLAGS = ['-fPIC'])
186 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100187 env.Append(CPPDEFINES = ['BARE_METAL'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188
189if env['opencl']:
190 if env['os'] == 'bare_metal':
191 print("Cannot link OpenCL statically, which is required on bare metal")
192 Exit(1)
193
194 if env['embed_kernels']:
195 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
196
197if env['debug']:
198 env['asserts'] = True
199 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
200 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
201else:
202 env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
203
204if env['asserts']:
205 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
Moritz Pflanzer8b74c782017-09-14 14:33:20 +0100206 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207
208env.Append(CPPPATH = ['#/include', "#"])
209env.Append(CXXFLAGS = env['extra_cxx_flags'])
210
211Export('vars')
212Export('env')
213Export('version_at_least')
214
215SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
216
217if env['opencl']:
218 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
219
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100220if env['examples'] and env['os'] != 'bare_metal':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221 SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
222
Pablo Tello9e40cf72017-09-15 16:14:55 +0100223if env['os'] != 'bare_metal':
224 SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)