1# 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 29 version_list = version.split('.') 30 required_list = required.split('.') 31 end = min(len(version_list), len(required_list)) 32 for i in range(0, end): 33 if int(version_list[i]) < int(required_list[i]): 34 return False 35 elif int(version_list[i]) > int(required_list[i]): 36 return True 37 38 return True 39 40vars = Variables("scons") 41vars.AddVariables( 42 BoolVariable("debug", "Debug", False), 43 BoolVariable("asserts", "Enable asserts (this flag is forced to 1 for debug=1)", False), 44 BoolVariable("logging", "Logging (this flag is forced to 1 for debug=1)", False), 45 EnumVariable("arch", "Target Architecture", "armv7a", 46 allowed_values=("armv7a", "arm64-v8a", "arm64-v8.2-a", "arm64-v8.2-a-sve", "x86_32", "x86_64", 47 "armv8a", "armv8.2-a", "armv8.2-a-sve", "armv8.6-a", "armv8.6-a-sve", "x86")), 48 EnumVariable("estate", "Execution State", "auto", allowed_values=("auto", "32", "64")), 49 EnumVariable("os", "Target OS", "linux", allowed_values=("linux", "android", "tizen", "bare_metal")), 50 EnumVariable("build", "Build type", "cross_compile", allowed_values=("native", "cross_compile", "embed_only")), 51 BoolVariable("examples", "Build example programs", True), 52 BoolVariable("gemm_tuner", "Build gemm_tuner programs", True), 53 BoolVariable("Werror", "Enable/disable the -Werror compilation flag", True), 54 BoolVariable("standalone", "Builds the tests as standalone executables, links statically with libgcc, libstdc++ and libarm_compute", False), 55 BoolVariable("opencl", "Enable OpenCL support", True), 56 BoolVariable("neon", "Enable Neon support", False), 57 BoolVariable("gles_compute", "Enable OpenGL ES Compute Shader support", False), 58 BoolVariable("embed_kernels", "Embed OpenCL kernels and OpenGL ES compute shaders in library binary", True), 59 BoolVariable("set_soname", "Set the library's soname and shlibversion (requires SCons 2.4 or above)", False), 60 BoolVariable("tracing", "Enable runtime tracing", False), 61 BoolVariable("openmp", "Enable OpenMP backend", False), 62 BoolVariable("cppthreads", "Enable C++11 threads backend", True), 63 PathVariable("build_dir", "Specify sub-folder for the build", ".", PathVariable.PathAccept), 64 PathVariable("install_dir", "Specify sub-folder for the install", "", PathVariable.PathAccept), 65 BoolVariable("exceptions", "Enable/disable C++ exception support", True), 66 PathVariable("linker_script", "Use an external linker script", "", PathVariable.PathAccept), 67 ListVariable("custom_options", "Custom options that can be used to turn on/off features", "none", ["disable_mmla_fp"]), 68 ListVariable("data_type_support", "Enable a list of data types to support", "all", ["qasymm8", "qasymm8_signed", "qsymm16", "fp16", "fp32"]), 69 ("toolchain_prefix", "Override the toolchain prefix", ""), 70 ("compiler_prefix", "Override the compiler prefix", ""), 71 ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", ""), 72 ("extra_link_flags", "Extra LD flags to be appended to the build command", ""), 73 ("compiler_cache", "Command to prefix to the C and C++ compiler (e.g ccache)", "") 74) 75 76env = Environment(platform="posix", variables=vars, ENV = os.environ) 77build_path = env['build_dir'] 78# If build_dir is a relative path then add a #build/ prefix: 79if not env['build_dir'].startswith('/'): 80 SConsignFile('build/%s/.scons' % build_path) 81 build_path = "#build/%s" % build_path 82else: 83 SConsignFile('%s/.scons' % build_path) 84 85install_path = env['install_dir'] 86#If the install_dir is a relative path then assume it's from inside build_dir 87if not env['install_dir'].startswith('/') and install_path != "": 88 install_path = "%s/%s" % (build_path, install_path) 89 90env.Append(LIBPATH = [build_path]) 91Export('env') 92Export('vars') 93 94def install_lib( lib ): 95 # If there is no install folder, then there is nothing to do: 96 if install_path == "": 97 return lib 98 return env.Install( "%s/lib/" % install_path, lib) 99def install_bin( bin ): 100 # If there is no install folder, then there is nothing to do: 101 if install_path == "": 102 return bin 103 return env.Install( "%s/bin/" % install_path, bin) 104def install_include( inc ): 105 if install_path == "": 106 return inc 107 return env.Install( "%s/include/" % install_path, inc) 108 109Export('install_lib') 110Export('install_bin') 111 112Help(vars.GenerateHelpText(env)) 113 114if env['linker_script'] and env['os'] != 'bare_metal': 115 print("Linker script is only supported for bare_metal builds") 116 Exit(1) 117 118if env['build'] == "embed_only": 119 SConscript('./SConscript', variant_dir=build_path, duplicate=0) 120 Return() 121 122if env['neon'] and 'x86' in env['arch']: 123 print("Cannot compile NEON for x86") 124 Exit(1) 125 126if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"): 127 print("Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above") 128 print("Update your version of SCons or use set_soname=0") 129 Exit(1) 130 131if env['os'] == 'bare_metal': 132 if env['cppthreads'] or env['openmp']: 133 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0") 134 Exit(1) 135 136if not env['exceptions']: 137 if env['opencl'] or env['gles_compute']: 138 print("ERROR: OpenCL and GLES are not supported when building without exceptions. Use opencl=0 gles_compute=0") 139 Exit(1) 140 141 env.Append(CPPDEFINES = ['ARM_COMPUTE_EXCEPTIONS_DISABLED']) 142 env.Append(CXXFLAGS = ['-fno-exceptions']) 143 144env.Append(CXXFLAGS = ['-Wall','-DARCH_ARM', 145 '-Wextra','-pedantic','-Wdisabled-optimization','-Wformat=2', 146 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default', 147 '-std=gnu++11','-Woverloaded-virtual', '-Wformat-security', 148 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-overlength-strings']) 149 150env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP']) 151 152default_cpp_compiler = 'g++' if env['os'] != 'android' else 'clang++' 153default_c_compiler = 'gcc' if env['os'] != 'android' else 'clang' 154cpp_compiler = os.environ.get('CXX', default_cpp_compiler) 155c_compiler = os.environ.get('CC', default_c_compiler) 156 157if env['os'] == 'android' and ( 'clang++' not in cpp_compiler or 'clang' not in c_compiler ): 158 print( "WARNING: Only clang is officially supported to build the Compute Library for Android") 159 160if 'clang++' in cpp_compiler: 161 env.Append(CXXFLAGS = ['-Wno-vla-extension']) 162elif 'armclang' in cpp_compiler: 163 pass 164else: 165 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel']) 166 167if env['cppthreads']: 168 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)]) 169 170if env['openmp']: 171 if 'clang++' in cpp_compiler: 172 print( "Clang does not support OpenMP. Use scheduler=cpp.") 173 Exit(1) 174 175 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)]) 176 env.Append(CXXFLAGS = ['-fopenmp']) 177 env.Append(LINKFLAGS = ['-fopenmp']) 178 179# Validate and define state 180if env['estate'] == 'auto': 181 if 'v7a' in env['arch']: 182 env['estate'] = '32' 183 else: 184 env['estate'] = '64' 185 186# Map legacy arch 187if 'arm64' in env['arch']: 188 env['estate'] = '64' 189 190if 'v7a' in env['estate'] and env['estate'] == '64': 191 print("ERROR: armv7a architecture has only 32-bit execution state") 192 Exit(1) 193 194# Add architecture specific flags 195prefix = "" 196if 'v7a' in env['arch']: 197 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon']) 198 if env['os'] == 'android' or env['os'] == 'tizen': 199 env.Append(CXXFLAGS = ['-mfloat-abi=softfp']) 200 else: 201 env.Append(CXXFLAGS = ['-mfloat-abi=hard']) 202elif 'v8' in env['arch']: 203 if 'sve' in env['arch']: 204 env.Append(CXXFLAGS = ['-march=armv8.2-a+sve+fp16+dotprod']) 205 elif 'v8.2-a' in env['arch']: 206 env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16']) # explicitly enable fp16 extension otherwise __ARM_FEATURE_FP16_VECTOR_ARITHMETIC is undefined 207 else: 208 env.Append(CXXFLAGS = ['-march=armv8-a']) 209 210 if 'v8.6-a' in env['arch']: 211 env.Append(CPPDEFINES = ['MMLA_INT8', 'V8P6', 'V8P6_BF', 'ARM_COMPUTE_FORCE_BF16']) 212 if "disable_mmla_fp" not in env['custom_options']: 213 env.Append(CPPDEFINES = ['MMLA_FP32']) 214 215elif 'x86' in env['arch']: 216 if env['estate'] == '32': 217 env.Append(CCFLAGS = ['-m32']) 218 env.Append(LINKFLAGS = ['-m32']) 219 else: 220 env.Append(CXXFLAGS = ['-fPIC']) 221 env.Append(CCFLAGS = ['-m64']) 222 env.Append(LINKFLAGS = ['-m64']) 223 224# Define toolchain 225prefix = "" 226if 'x86' not in env['arch']: 227 if env['estate'] == '32': 228 if env['os'] == 'linux': 229 prefix = "arm-linux-gnueabihf-" if 'v7' in env['arch'] else "armv8l-linux-gnueabihf-" 230 elif env['os'] == 'bare_metal': 231 prefix = "arm-eabi-" 232 elif env['os'] == 'android': 233 prefix = "arm-linux-androideabi-" 234 elif env['os'] == 'tizen': 235 prefix = "armv7l-tizen-linux-gnueabi-" 236 elif env['estate'] == '64' and 'v8' in env['arch']: 237 if env['os'] == 'linux': 238 prefix = "aarch64-linux-gnu-" 239 elif env['os'] == 'bare_metal': 240 prefix = "aarch64-elf-" 241 elif env['os'] == 'android': 242 prefix = "aarch64-linux-android-" 243 elif env['os'] == 'tizen': 244 prefix = "aarch64-tizen-linux-gnu-" 245 246if env['build'] == 'native': 247 prefix = "" 248 249if env["toolchain_prefix"] != "": 250 prefix = env["toolchain_prefix"] 251 252compiler_prefix = prefix 253if env["compiler_prefix"] != "": 254 compiler_prefix = env["compiler_prefix"] 255 256env['CC'] = env['compiler_cache']+ " " + compiler_prefix + c_compiler 257env['CXX'] = env['compiler_cache']+ " " + compiler_prefix + cpp_compiler 258env['LD'] = prefix + "ld" 259env['AS'] = prefix + "as" 260env['AR'] = prefix + "ar" 261env['RANLIB'] = prefix + "ranlib" 262 263if not GetOption("help"): 264 try: 265 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).decode().strip() 266 except OSError: 267 print("ERROR: Compiler '%s' not found" % env['CXX']) 268 Exit(1) 269 270 if 'armclang' in cpp_compiler: 271 pass 272 elif 'clang++' not in cpp_compiler: 273 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'): 274 print("GCC 6.2.1 or newer is required to compile armv8.2-a code") 275 Exit(1) 276 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'): 277 print("GCC 4.9 or newer is required to compile NEON code for AArch64") 278 Exit(1) 279 280 if version_at_least(compiler_ver, '6.1'): 281 env.Append(CXXFLAGS = ['-Wno-ignored-attributes']) 282 283 if compiler_ver == '4.8.3': 284 env.Append(CXXFLAGS = ['-Wno-array-bounds']) 285 286 if not version_at_least(compiler_ver, '7.0.0') and env['os'] == 'bare_metal': 287 env.Append(LINKFLAGS = ['-fstack-protector-strong']) 288 289if env['data_type_support']: 290 if any(i in env['data_type_support'] for i in ['all', 'fp16']): 291 env.Append(CXXFLAGS = ['-DENABLE_FP16_KERNELS']) 292 if any(i in env['data_type_support'] for i in ['all', 'fp32']): 293 env.Append(CXXFLAGS = ['-DENABLE_FP32_KERNELS']) 294 if any(i in env['data_type_support'] for i in ['all', 'qasymm8']): 295 env.Append(CXXFLAGS = ['-DENABLE_QASYMM8_KERNELS']) 296 if any(i in env['data_type_support'] for i in ['all', 'qasymm8_signed']): 297 env.Append(CXXFLAGS = ['-DENABLE_QASYMM8_SIGNED_KERNELS']) 298 if any(i in env['data_type_support'] for i in ['all', 'qsymm16']): 299 env.Append(CXXFLAGS = ['-DENABLE_QSYMM16_KERNELS']) 300 301if env['standalone']: 302 env.Append(CXXFLAGS = ['-fPIC']) 303 env.Append(LINKFLAGS = ['-static-libgcc','-static-libstdc++']) 304 305if env['Werror']: 306 env.Append(CXXFLAGS = ['-Werror']) 307 308if env['os'] == 'android': 309 env.Append(CPPDEFINES = ['ANDROID']) 310 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++', '-ldl']) 311elif env['os'] == 'bare_metal': 312 env.Append(LINKFLAGS = ['-static']) 313 env.Append(LINKFLAGS = ['-specs=rdimon.specs']) 314 env.Append(CXXFLAGS = ['-fPIC']) 315 env.Append(CPPDEFINES = ['NO_MULTI_THREADING']) 316 env.Append(CPPDEFINES = ['BARE_METAL']) 317if env['os'] == 'linux' and env['arch'] == 'armv7a': 318 env.Append(CXXFLAGS = [ '-Wno-psabi' ]) 319 320if env['opencl']: 321 if env['os'] in ['bare_metal'] or env['standalone']: 322 print("Cannot link OpenCL statically, which is required for bare metal / standalone builds") 323 Exit(1) 324 325if env['gles_compute']: 326 if env['os'] in ['bare_metal'] or env['standalone']: 327 print("Cannot link OpenGLES statically, which is required for bare metal / standalone builds") 328 Exit(1) 329 330if env["os"] not in ["android", "bare_metal"] and (env['opencl'] or env['cppthreads']): 331 env.Append(LIBS = ['pthread']) 332 333if env['opencl'] or env['gles_compute']: 334 if env['embed_kernels']: 335 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS']) 336 337if env['debug']: 338 env['asserts'] = True 339 env['logging'] = True 340 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2']) 341 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED']) 342else: 343 env.Append(CXXFLAGS = ['-O3']) 344 345if env['asserts']: 346 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED']) 347 env.Append(CXXFLAGS = ['-fstack-protector-strong']) 348 349if env['logging']: 350 env.Append(CPPDEFINES = ['ARM_COMPUTE_LOGGING_ENABLED']) 351 352env.Append(CPPPATH = ['#/include', "#"]) 353env.Append(CXXFLAGS = env['extra_cxx_flags']) 354env.Append(LINKFLAGS = env['extra_link_flags']) 355 356Default( install_include("arm_compute")) 357Default( install_include("support")) 358Default( install_include("utils")) 359for dirname in os.listdir("./include"): 360 Default( install_include("include/%s" % dirname)) 361 362Export('version_at_least') 363 364if env['gles_compute'] and env['os'] != 'android': 365 env.Append(CPPPATH = ['#/include/linux']) 366 367SConscript('./SConscript', variant_dir=build_path, duplicate=0) 368 369if env['examples'] and env['exceptions']: 370 if env['os'] == 'bare_metal' and env['arch'] == 'armv7a': 371 print("WARNING: Building examples for bare metal and armv7a is not supported. Use examples=0") 372 Return() 373 SConscript('./examples/SConscript', variant_dir='%s/examples' % build_path, duplicate=0) 374 375if env['exceptions']: 376 if env['os'] == 'bare_metal' and env['arch'] == 'armv7a': 377 print("WARNING: Building tests for bare metal and armv7a is not supported") 378 Return() 379 SConscript('./tests/SConscript', variant_dir='%s/tests' % build_path, duplicate=0) 380