blob: 34d18ff0b98c6618ab35e9417d31311e6630e60d [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")),
45 EnumVariable("build", "Build type", "cross_compile", allowed_values=("native", "cross_compile")),
46 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 Barbier6ff3b192017-09-04 18:44:23 +010064
65SConsignFile('build/.%s' % env['build_dir'])
66
67Help(vars.GenerateHelpText(env))
68
69if env['neon'] and 'x86' in env['arch']:
70 print "Cannot compile NEON for x86"
71 Exit(1)
72
73if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
74 print "Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above"
75 print "Update your version of SCons or use set_soname=0"
76 Exit(1)
77
78if env['os'] == 'bare_metal':
79 if env['cppthreads'] or env['openmp']:
80 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
81 Exit(1)
82
83env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
84 '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
85 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
86 '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
87 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow'])
Isabella Gottardib28f29d2017-11-09 17:05:07 +000088
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
90
91if os.environ.get('CXX', 'g++') == 'clang++':
92 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
93else:
94 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
95
96if env['cppthreads']:
97 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
98
99if env['openmp']:
100 if os.environ.get('CXX', 'g++') == 'clang++':
101 print "Clang does not support OpenMP. Use scheduler=cpp."
102 Exit(1)
103
104 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
105 env.Append(CXXFLAGS = ['-fopenmp'])
106 env.Append(LINKFLAGS = ['-fopenmp'])
107
108prefix = ""
109if env['arch'] == 'armv7a':
110 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
111
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100112 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 prefix = "arm-linux-gnueabihf-"
114 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100115 elif env['os'] == 'bare_metal':
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100116 prefix = "arm-eabi-"
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100117 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118 elif env['os'] == 'android':
119 prefix = "arm-linux-androideabi-"
120 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
121elif env['arch'] == 'arm64-v8a':
122 env.Append(CXXFLAGS = ['-march=armv8-a'])
Pablo Tello181e6512017-11-15 13:28:27 +0000123 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8A'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100124 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125 prefix = "aarch64-linux-gnu-"
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100126 elif env['os'] == 'bare_metal':
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100127 prefix = "aarch64-elf-"
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128 elif env['os'] == 'android':
129 prefix = "aarch64-linux-android-"
130elif env['arch'] == 'arm64-v8.2-a':
Ioan-Cristian Szabo33fd07b2017-10-26 15:42:24 +0100131 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8_2'])
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000132
133 if os.environ.get('CXX', 'g++') == 'clang++':
134 env.Append(CXXFLAGS = ['-fno-integrated-as'])
135
Pablo Tello9e40cf72017-09-15 16:14:55 +0100136 if env['os'] == 'linux':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 prefix = "aarch64-linux-gnu-"
Pablo Tello9e40cf72017-09-15 16:14:55 +0100138 elif env['os'] == 'bare_metal':
139 prefix = "aarch64-elf-"
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140 elif env['os'] == 'android':
141 prefix = "aarch64-linux-android-"
142elif env['arch'] == 'x86_32':
143 env.Append(CCFLAGS = ['-m32'])
144 env.Append(LINKFLAGS = ['-m32'])
145elif env['arch'] == 'x86_64':
146 env.Append(CCFLAGS = ['-m64'])
147 env.Append(LINKFLAGS = ['-m64'])
148
149if env['build'] == 'native':
150 prefix = ""
151
152env['CC'] = prefix + os.environ.get('CC', 'gcc')
153env['CXX'] = prefix + os.environ.get('CXX', 'g++')
154env['LD'] = prefix + "ld"
155env['AS'] = prefix + "as"
156env['AR'] = prefix + "ar"
157env['RANLIB'] = prefix + "ranlib"
158
159if not GetOption("help"):
160 try:
Anthony Barbier907dba82017-08-25 10:11:39 +0100161 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162 except OSError:
163 print("ERROR: Compiler '%s' not found" % env['CXX'])
164 Exit(1)
165
166 if os.environ.get('CXX','g++') == 'g++':
167 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
168 print "GCC 6.2.1 or newer is required to compile armv8.2-a code"
169 Exit(1)
170 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
171 print "GCC 4.9 or newer is required to compile NEON code for AArch64"
172 Exit(1)
173
174 if version_at_least(compiler_ver, '6.1'):
175 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
176
177 if compiler_ver == '4.8.3':
178 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
179
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100180if env['standalone']:
181 env.Append(CXXFLAGS = ['-fPIC'])
182 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
Pablo Tello89519332017-11-17 11:52:36 +0000183 if env['cppthreads']:
184 env.Append(LINKFLAGS = ['-lpthread'])
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100185
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186if env['Werror']:
187 env.Append(CXXFLAGS = ['-Werror'])
188
189if env['os'] == 'android':
190 env.Append(CPPDEFINES = ['ANDROID'])
Georgios Pinitas9873ea32017-12-05 15:28:55 +0000191 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192elif env['os'] == 'bare_metal':
193 env.Append(LINKFLAGS = ['-static'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100194 env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195 env.Append(CXXFLAGS = ['-fPIC'])
196 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
Michalis Spyrou07781ac2017-08-31 15:11:41 +0100197 env.Append(CPPDEFINES = ['BARE_METAL'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198
199if env['opencl']:
Georgios Pinitase6032da2017-10-26 14:13:35 +0100200 if env['os'] in ['bare_metal'] or env['standalone']:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201 print("Cannot link OpenCL statically, which is required on bare metal")
202 Exit(1)
203
Anthony Barbier7068f992017-10-26 15:23:08 +0100204if env['opencl'] or env['gles_compute']:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205 if env['embed_kernels']:
206 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
207
208if env['debug']:
209 env['asserts'] = True
Georgios Pinitas3faea252017-10-30 14:13:50 +0000210 env['logging'] = True
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
212 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
213else:
214 env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
215
216if env['asserts']:
217 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
Moritz Pflanzer8b74c782017-09-14 14:33:20 +0100218 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100219
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100220if env['logging']:
221 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
222
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223env.Append(CPPPATH = ['#/include', "#"])
224env.Append(CXXFLAGS = env['extra_cxx_flags'])
225
226Export('vars')
227Export('env')
228Export('version_at_least')
229
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100230if env['opencl']:
231 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
232
Anthony Barbier7068f992017-10-26 15:23:08 +0100233if env['gles_compute'] and env['os'] != 'android':
Anthony Barbier14c86a92017-12-14 16:27:41 +0000234 env.Append(CPPPATH = ['#/include/linux'])
235 env.Append(LIBPATH = ["#build/%s/opengles-3.1-stubs" % env['build_dir']])
236 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 +0100237
Anthony Barbier6c0348f2017-11-10 18:32:38 +0000238SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
239
Michalis Spyrou6e52ba32017-10-04 15:40:38 +0100240if env['examples'] and env['os'] != 'bare_metal':
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241 SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
242
Pablo Tello9e40cf72017-09-15 16:14:55 +0100243if env['os'] != 'bare_metal':
244 SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)