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 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),
47 BoolVariable("opencl", "Enable OpenCL support", True),
48 BoolVariable("neon", "Enable Neon support", False),
49 BoolVariable("embed_kernels", "Embed OpenCL kernels in library binary", False),
50 BoolVariable("set_soname", "Set the library's soname and shlibversion (requires SCons 2.4 or above)", False),
51 BoolVariable("openmp", "Enable OpenMP backend", False),
52 BoolVariable("cppthreads", "Enable C++11 threads backend", True),
53 PathVariable("build_dir", "Specify sub-folder for the build", ".", PathVariable.PathAccept),
54 ("extra_cxx_flags", "Extra CXX flags to be appended to the build command", "")
57 env = Environment(platform="posix", variables=vars, ENV = os.environ)
59 SConsignFile('build/.%s' % env['build_dir'])
61 Help(vars.GenerateHelpText(env))
63 if env['neon'] and 'x86' in env['arch']:
64 print "Cannot compile NEON for x86"
67 if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
68 print "Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above"
69 print "Update your version of SCons or use set_soname=0"
72 if env['os'] == 'bare_metal':
73 if env['cppthreads'] or env['openmp']:
74 print("ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0")
77 env.Append(CXXFLAGS = ['-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
78 '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
79 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
80 '-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
81 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow'])
82 env.Append(CPPDEFINES = ['_GLIBCXX_USE_NANOSLEEP'])
84 if os.environ.get('CXX', 'g++') == 'clang++':
85 env.Append(CXXFLAGS = ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags'])
87 env.Append(CXXFLAGS = ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel'])
90 env.Append(CPPDEFINES = [('ARM_COMPUTE_CPP_SCHEDULER', 1)])
93 if os.environ.get('CXX', 'g++') == 'clang++':
94 print "Clang does not support OpenMP. Use scheduler=cpp."
97 env.Append(CPPDEFINES = [('ARM_COMPUTE_OPENMP_SCHEDULER', 1)])
98 env.Append(CXXFLAGS = ['-fopenmp'])
99 env.Append(LINKFLAGS = ['-fopenmp'])
102 if env['arch'] == 'armv7a':
103 env.Append(CXXFLAGS = ['-march=armv7-a', '-mthumb', '-mfpu=neon'])
105 if env['os'] in ['linux', 'bare_metal']:
106 prefix = "arm-linux-gnueabihf-"
107 env.Append(CXXFLAGS = ['-mfloat-abi=hard'])
108 elif env['os'] == 'android':
109 prefix = "arm-linux-androideabi-"
110 env.Append(CXXFLAGS = ['-mfloat-abi=softfp'])
111 elif env['arch'] == 'arm64-v8a':
112 env.Append(CXXFLAGS = ['-march=armv8-a'])
114 if env['os'] in ['linux', 'bare_metal']:
115 prefix = "aarch64-linux-gnu-"
116 elif env['os'] == 'android':
117 prefix = "aarch64-linux-android-"
118 elif env['arch'] == 'arm64-v8.2-a':
119 env.Append(CXXFLAGS = ['-march=armv8.2-a+fp16+simd'])
120 env.Append(CPPDEFINES = ['ARM_COMPUTE_ENABLE_FP16'])
122 if env['os'] in ['linux', 'bare_metal']:
123 prefix = "aarch64-linux-gnu-"
124 elif env['os'] == 'android':
125 prefix = "aarch64-linux-android-"
126 elif env['arch'] == 'x86_32':
127 env.Append(CCFLAGS = ['-m32'])
128 env.Append(LINKFLAGS = ['-m32'])
129 elif env['arch'] == 'x86_64':
130 env.Append(CCFLAGS = ['-m64'])
131 env.Append(LINKFLAGS = ['-m64'])
133 if env['build'] == 'native':
136 env['CC'] = prefix + os.environ.get('CC', 'gcc')
137 env['CXX'] = prefix + os.environ.get('CXX', 'g++')
138 env['LD'] = prefix + "ld"
139 env['AS'] = prefix + "as"
140 env['AR'] = prefix + "ar"
141 env['RANLIB'] = prefix + "ranlib"
143 if not GetOption("help"):
145 compiler_ver = subprocess.check_output(env['CXX'].split() + ["-dumpversion"]).strip()
147 print("ERROR: Compiler '%s' not found" % env['CXX'])
150 if os.environ.get('CXX','g++') == 'g++':
151 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
152 print "GCC 6.2.1 or newer is required to compile armv8.2-a code"
154 elif env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
155 print "GCC 4.9 or newer is required to compile NEON code for AArch64"
158 if version_at_least(compiler_ver, '6.1'):
159 env.Append(CXXFLAGS = ['-Wno-ignored-attributes'])
161 if compiler_ver == '4.8.3':
162 env.Append(CXXFLAGS = ['-Wno-array-bounds'])
165 env.Append(CXXFLAGS = ['-Werror'])
167 if env['os'] == 'android':
168 env.Append(CPPDEFINES = ['ANDROID'])
169 env.Append(LINKFLAGS = ['-pie', '-static-libstdc++'])
170 elif env['os'] == 'bare_metal':
171 env.Append(LINKFLAGS = ['-static'])
172 env.Append(CXXFLAGS = ['-fPIC'])
173 env.Append(CPPDEFINES = ['NO_MULTI_THREADING'])
176 if env['os'] == 'bare_metal':
177 print("Cannot link OpenCL statically, which is required on bare metal")
180 if env['embed_kernels']:
181 env.Append(CPPDEFINES = ['EMBEDDED_KERNELS'])
184 env['asserts'] = True
185 env.Append(CXXFLAGS = ['-O0','-g','-gdwarf-2'])
186 env.Append(CPPDEFINES = ['ARM_COMPUTE_DEBUG_ENABLED'])
188 env.Append(CXXFLAGS = ['-O3','-ftree-vectorize'])
191 env.Append(CPPDEFINES = ['ARM_COMPUTE_ASSERTS_ENABLED'])
193 env.Append(CPPPATH = ['#/include', "#"])
194 env.Append(CXXFLAGS = env['extra_cxx_flags'])
198 Export('version_at_least')
200 SConscript('./SConscript', variant_dir='#build/%s' % env['build_dir'], duplicate=0)
203 SConscript("./opencl-1.2-stubs/SConscript", variant_dir="build/%s/opencl-1.2-stubs" % env['build_dir'], duplicate=0)
206 SConscript('./examples/SConscript', variant_dir='#build/%s/examples' % env['build_dir'], duplicate=0)
208 SConscript('./tests/SConscript', variant_dir='#build/%s/tests' % env['build_dir'], duplicate=0)