1 # Copyright (c) 2016, 2017 ARM Limited.
3 # SPDX-License-Identifier: MIT
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:
12 # The above copyright notice and this permission notice shall be included in all
13 # copies or substantial portions of the Software.
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
27 def version_at_least(version, required):
28 end = min(len(version), len(required))
30 for i in range(0, end, 2):
31 if int(version[i]) < int(required[i]):
33 elif int(version[i]) > int(required[i]):
38 vars = Variables("scons")
40 BoolVariable("debug", "Debug", False),
41 BoolVariable("asserts", "Enable asserts (this flag is forced to 1 for debug=1)", False),
42 BoolVariable("logging", "Logging (this flag is forced to 1 for debug=1)", False),
43 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),
48 BoolVariable("standalone", "Builds the tests as standalone executables, links statically with libgcc, libstdc++ and libarm_compute", False),
49 BoolVariable("opencl", "Enable OpenCL support", True),
50 BoolVariable("neon", "Enable Neon support", False),
51 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),
53 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),
57 ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", "")
60 env = Environment(platform="posix", variables=vars, ENV = os.environ)
61 env.Append(LIBPATH = ["#build/%s" % env['build_dir']])
63 SConsignFile('build/.%s' % env['build_dir'])
65 Help(vars.GenerateHelpText(env))
67 if env['neon'] and 'x86' in env['arch']:
68 print "Cannot compile NEON for x86"
71 if 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"
76 if 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")
81 env.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'])
87 env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
89 if os.environ.get('CXX', 'g++') == 'clang++':
90 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
92 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
95 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
98 if os.environ.get('CXX', 'g++') == 'clang++':
99 print "Clang does not support OpenMP. Use scheduler=cpp."
102 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
103 env.Append(CXXFLAGS = ['-fopenmp'])
104 env.Append(LINKFLAGS = ['-fopenmp'])
107 if env['arch'] == 'armv7a':
108 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
110 if env['os'] == 'linux':
111 prefix = "arm-linux-gnueabihf-"
112 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
113 elif env['os'] == 'bare_metal':
115 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
116 elif env['os'] == 'android':
117 prefix = "arm-linux-androideabi-"
118 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
119 elif env['arch'] == 'arm64-v8a':
120 env.Append(CXXFLAGS = ['-march=armv8-a'])
121 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8A'])
122 if env['os'] == 'linux':
123 prefix = "aarch64-linux-gnu-"
124 elif env['os'] == 'bare_metal':
125 prefix = "aarch64-elf-"
126 elif env['os'] == 'android':
127 prefix = "aarch64-linux-android-"
128 elif env['arch'] == 'arm64-v8.2-a':
129 env.Append(CPPDEFINES = ['ARM_COMPUTE_AARCH64_V8_2'])
131 if os.environ.get('CXX', 'g++') == 'clang++':
132 env.Append(CXXFLAGS = ['-fno-integrated-as'])
134 if env['os'] == 'linux':
135 prefix = "aarch64-linux-gnu-"
136 elif env['os'] == 'bare_metal':
137 prefix = "aarch64-elf-"
138 elif env['os'] == 'android':
139 prefix = "aarch64-linux-android-"
140 elif env['arch'] == 'x86_32':
141 env.Append(CCFLAGS = ['-m32'])
142 env.Append(LINKFLAGS = ['-m32'])
143 elif env['arch'] == 'x86_64':
144 env.Append(CCFLAGS = ['-m64'])
145 env.Append(LINKFLAGS = ['-m64'])
147 if env['build'] == 'native':
150 env['CC'] = prefix + os.environ.get('CC', 'gcc')
151 env['CXX'] = prefix + os.environ.get('CXX', 'g++')
152 env['LD'] = prefix + "ld"
153 env['AS'] = prefix + "as"
154 env['AR'] = prefix + "ar"
155 env['RANLIB'] = prefix + "ranlib"
157 if not GetOption("help"):
159 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
161 print("ERROR: Compiler '%s' not found" % env['CXX'])
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"
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"
172 if version_at_least(compiler_ver, '6.1'):
173 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
175 if compiler_ver == '4.8.3':
176 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
178 if env['standalone']:
179 env.Append(CXXFLAGS = ['-fPIC'])
180 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++'])
181 if env['cppthreads']:
182 env.Append(LINKFLAGS = ['-lpthread'])
185 env.Append(CXXFLAGS = ['-Werror'])
187 if env['os'] == 'android':
188 env.Append(CPPDEFINES = ['ANDROID'])
189 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
190 elif env['os'] == 'bare_metal':
191 env.Append(LINKFLAGS = ['-static'])
192 env.Append(LINKFLAGS = ['-specs=rdimon.specs'])
193 env.Append(CXXFLAGS = ['-fPIC'])
194 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
195 env.Append(CPPDEFINES = ['BARE_METAL'])
198 if env['os'] in ['bare_metal'] or env['standalone']:
199 print("Cannot link OpenCL statically, which is required on bare metal")
202 if env['opencl'] or env['gles_compute']:
203 if env['embed_kernels']:
204 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
207 env['asserts'] = True
208 env['logging'] = True
209 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
210 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
212 env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
215 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
216 env.Append(CXXFLAGS = ['-fstack-protector-strong'])
219 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED'])
221 env.Append(CPPPATH = ['#/include', "#"])
222 env.Append(CXXFLAGS = env['extra_cxx_flags'])
226 Export('version_at_least')
229 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
231 if 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)
236 SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
238 if env['examples'] and env['os'] != 'bare_metal':
239 SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
241 if env['os'] != 'bare_metal':
242 SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)