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
29 SONAME_VERSION="1.0.0"
31 vars = Variables('scons')
32 vars.Add(EnumVariable('debug','Debug (default=0)', '0', allowed_values=('0','1')))
33 vars.Add(EnumVariable('asserts','Enable asserts (This flag is forced to 1 for debug=1) (default=0)', '0', allowed_values=('0','1')))
34 vars.Add(EnumVariable('arch','Target Architecture (default=armv7a)', 'armv7a', allowed_values=('armv7a','arm64-v8a','arm64-v8.2-a','x86_32','x86_64')))
35 vars.Add(EnumVariable('os','Target OS (default=linux)', 'linux', allowed_values=('linux','android','bare_metal')))
36 vars.Add(EnumVariable('build','Build type: (default=cross_compile)', 'cross_compile', allowed_values=('native','cross_compile')))
37 vars.Add(EnumVariable('Werror','Enable/disable the -Werror compilation flag (Default=1)', '1', allowed_values=('0','1')))
38 vars.Add(EnumVariable('opencl','Enable OpenCL support(Default=1)', '1', allowed_values=('0','1')))
39 vars.Add(EnumVariable('neon','Enable Neon support(Default=0)', '0', allowed_values=('0','1')))
40 vars.Add(EnumVariable('embed_kernels', 'Embed OpenCL kernels in library binary(Default=0)', '0', allowed_values=('0','1')))
41 vars.Add(BoolVariable('set_soname','Set the library\'s soname and shlibversion (Requires SCons 2.4 or above)', 0))
42 vars.Add(('extra_cxx_flags','Extra CXX flags to be appended to the build command', ''))
44 env = Environment(platform='posix', variables = vars, ENV = os.environ)
46 Help(vars.GenerateHelpText(env))
48 def version_at_least(version, required):
49 end = min(len(version), len(required))
51 for i in range(0, end, 2):
52 if int(version[i]) < int(required[i]):
54 elif int(version[i]) > int(required[i]):
59 if not GetOption("help"):
60 flags = ['-D_GLIBCXX_USE_NANOSLEEP','-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
61 '-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
62 '-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
63 '-fpermissive','-std=c++11','-Wno-vla','-Woverloaded-virtual',
64 '-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow']
67 if env['neon'] == '1' 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 os.environ.get('CXX','g++') == 'clang++':
77 flags += ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags']
79 flags += ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel']
83 #Generate string with build options library version to embed in the library:
86 git_hash = subprocess.check_output(["git", "rev-parse","HEAD"])
87 except OSError: # In case git is not present
89 except subprocess.CalledProcessError:
92 version_filename = "%s/arm_compute_version.embed" % os.path.dirname(Glob("src/core/*")[0].rstr())
93 build_info = "\"arm_compute_version=%s Build options: %s Git hash=%s\"" % (VERSION, vars.args, git_hash.strip())
94 open(version_filename,"w").write(build_info)
95 files_to_delete.append( version_filename )
97 def build_library(name, sources, libs, static=False):
99 obj = env.StaticLibrary(name, source = sources, LIBS=libs )
101 if env['set_soname']:
102 obj = env.SharedLibrary(name, source = sources, LIBS=libs, SHLIBVERSION=SONAME_VERSION)
104 # Manually delete symlinks or SCons will get confused:
105 directory = os.path.dirname( obj[0].path )
106 library_prefix = obj[0].path[:-(1+len(SONAME_VERSION))]
107 real_lib="%s.%s" % (library_prefix, SONAME_VERSION)
108 for f in Glob( "#%s*" % library_prefix):
109 if str(f) != real_lib:
110 symlinks.append("%s/%s" % (directory,str(f)))
111 clean = env.Command('clean-%s' % str(obj[0]), [], Delete(symlinks))
115 obj = env.SharedLibrary(name, source = sources, LIBS=libs)
120 def resolve_includes(target, source, env):
122 FileEntry = collections.namedtuple('FileEntry', 'target_name file_contents')
125 pattern = re.compile("#include \"(.*)\"")
130 name = s.rstr().split("/")[-1]
131 contents = s.get_contents().splitlines()
132 embed_target_name = s.abspath + "embed"
133 entry = FileEntry(target_name=embed_target_name, file_contents=contents)
134 files.append((name,entry))
136 # Create dictionary of tupled list
137 files_dict = dict(files)
139 # Check for includes (can only be files in the same folder)
143 tmp_file = file[1].file_contents
147 for line in tmp_file:
148 found = pattern.search(line)
150 include_file = found.group(1)
151 data = files_dict[include_file].file_contents
152 updated_file.extend(data)
154 updated_file.append(line)
157 # Check if all include are replaced.
158 if file_count == len(tmp_file):
162 tmp_file = updated_file
164 # Append and prepend string literal identifiers and add expanded file to final list
165 tmp_file.insert(0, "R\"(\n")
166 tmp_file.append("\n)\"")
167 entry = FileEntry(target_name=file[1].target_name, file_contents=tmp_file)
168 final_files.append((file[0], entry))
171 for file in final_files:
172 out_file = open(file[1].target_name, 'w+')
173 contents = file[1].file_contents
174 for line in contents:
175 out_file.write("%s\n" % line)
182 if env['arch'] == 'armv7a':
183 flags += ['-march=armv7-a','-mthumb','-mfpu=neon']
185 if env['os'] in ['linux','bare_metal']:
186 prefix = "arm-linux-gnueabihf-"
187 flags += ['-mfloat-abi=hard']
188 elif env['os'] == 'android':
189 prefix = "arm-linux-androideabi-"
190 flags += ['-mfloat-abi=softfp']
191 elif env['arch'] == 'arm64-v8a':
192 flags += ['-march=armv8-a']
193 if env['os'] in ['linux','bare_metal']:
194 prefix = "aarch64-linux-gnu-"
195 elif env['os'] == 'android':
196 prefix = "aarch64-linux-android-"
197 elif env['arch'] == 'arm64-v8.2-a':
198 flags += ['-march=armv8.2-a+fp16+simd']
199 flags += ['-DARM_COMPUTE_ENABLE_FP16']
200 if env['os'] in ['linux','bare_metal']:
201 prefix = "aarch64-linux-gnu-"
202 elif env['os'] == 'android':
203 prefix = "aarch64-linux-android-"
204 elif env['arch'] == 'x86_32':
206 elif env['arch'] == 'x86_64':
209 if env['build'] == 'native':
212 env['CC'] = prefix + os.environ.get('CC','gcc')
213 env['CXX'] = prefix + os.environ.get('CXX','g++')
214 env['LD'] = prefix + "ld"
215 env['AS'] = prefix + "as"
216 env['AR'] = prefix + "ar"
217 env['RANLIB'] = prefix + "ranlib"
220 compiler_ver = subprocess.check_output( [env['CXX'] , "-dumpversion"] ).strip()
222 print "ERROR: Compiler '%s' not found" % env['CXX']
225 if os.environ.get('CXX','g++') == 'g++':
226 if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
227 print "GCC 6.2.1 or newer is required to compile armv8.2-a code"
230 if env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
231 print "GCC 4.9 or newer is required to compile NEON code for AArch64"
234 if version_at_least(compiler_ver, '6.1'):
235 flags += ['-Wno-ignored-attributes']
237 if compiler_ver == '4.8.3':
238 flags += ['-Wno-array-bounds']
240 if env['Werror'] == '1':
244 if env['os'] == 'android':
245 flags += ['-DANDROID']
246 env.Append(LINKFLAGS=['-pie','-static-libstdc++'])
247 example_libs = ['arm_compute-static']
248 elif env['os'] == 'bare_metal':
249 env.Append(LINKFLAGS=['-static'])
250 flags += ['-fPIC','-DNO_MULTI_THREADING']
251 example_libs = ['arm_compute-static']
254 example_libs = ['arm_compute']
256 if env['opencl'] == '1':
257 if env['os'] == 'bare_metal':
258 raise Exception("Cannot link OpenCL statically, which is required on bare metal")
259 core_libs += ['OpenCL']
260 if env['embed_kernels'] == '1':
261 flags += ['-DEMBEDDED_KERNELS']
263 if env['debug'] == '1':
265 flags += ['-O0','-g','-gdwarf-2']
267 flags += ['-O3','-ftree-vectorize']
269 if env['asserts'] == '1':
270 flags += ['-DARM_COMPUTE_ASSERTS_ENABLED']
272 env.Append(CPPPATH=['.','#include'])
273 env.Append(LIBPATH=['#build','.'])
274 env.Append(CXXFLAGS=flags)
275 env.Append(CXXFLAGS=env['extra_cxx_flags'])
277 core_files = Glob('src/core/*.cpp')
278 core_files += Glob('src/core/CPP/*.cpp')
280 files = Glob('src/runtime/*.cpp')
283 core_files += Glob('src/core/CPP/kernels/*.cpp')
285 files += Glob('src/runtime/CPP/*.cpp')
287 if env['opencl'] == '1':
288 example_libs += ['OpenCL']
289 core_files += Glob('src/core/CL/*.cpp')
290 core_files += Glob('src/core/CL/kernels/*.cpp')
291 files += Glob('src/runtime/CL/*.cpp')
292 files += Glob('src/runtime/CL/functions/*.cpp')
293 # Generate embed files
294 if env['embed_kernels'] == '1':
295 cl_files = Glob('src/core/CL/cl_kernels/*.cl') + Glob('src/core/CL/cl_kernels/*.h')
297 for file in cl_files:
298 source_name = file.rstr()
299 source_list.append(source_name)
300 embed_files.append(source_name + "embed")
301 generate_embed = env.Command(embed_files, source_list, action=resolve_includes)
302 Default(generate_embed)
303 files_to_delete += embed_files
305 if env['neon'] == '1':
306 core_files += Glob('src/core/NEON/*.cpp')
307 core_files += Glob('src/core/NEON/kernels/*.cpp')
308 files += Glob('src/runtime/NEON/*.cpp')
309 files += Glob('src/runtime/NEON/functions/*.cpp')
312 static_core_objects = [ env.StaticObject( f ) for f in core_files ]
313 shared_core_objects = [ env.SharedObject( f ) for f in core_files ]
315 arm_compute_core_a = build_library('arm_compute_core-static', static_core_objects, core_libs, static=True)
316 objects.append(arm_compute_core_a)
317 Export('arm_compute_core_a')
319 if env['os'] != 'bare_metal':
320 arm_compute_core_so = build_library('arm_compute_core', shared_core_objects, core_libs, static=False)
321 objects.append(arm_compute_core_so)
322 Export('arm_compute_core_so')
323 shared_objects = [ env.SharedObject( f ) for f in files ]
324 static_objects = [ env.StaticObject( f ) for f in files ]
326 arm_compute_a = build_library('arm_compute-static', static_core_objects + static_objects, libs, static=True)
327 objects.append(arm_compute_a)
328 Export('arm_compute_a')
329 if env['os'] != 'bare_metal':
330 arm_compute_so = build_library('arm_compute', shared_core_objects + shared_objects, libs, static=False)
331 objects.append(arm_compute_so)
332 Export('arm_compute_so')
334 # Delete produced embed files
335 clean_embed = env.Command('clean-embed', [], Delete(files_to_delete))
337 env.Depends(clean_embed, objects)
338 alias = env.Alias("arm_compute",objects)
342 test_helpers = env.Object("test_helpers/Utils.cpp")
344 if env['opencl'] == '1' and env['neon'] == '1':
345 for file in Glob("examples/neoncl_*.cpp"):
346 example = os.path.basename( os.path.splitext(str(file))[0])
347 prog = env.Program(example, ['examples/%s.cpp' % example, test_helpers], LIBS=example_libs)
348 alias = env.Alias(example, prog)
349 Depends(prog, objects)
352 if env['opencl'] == '1':
353 for file in Glob("examples/cl_*.cpp"):
354 example = os.path.basename( os.path.splitext(str(file))[0])
355 prog = env.Program(example, ['examples/%s.cpp' % example, test_helpers], LIBS=example_libs)
356 alias = env.Alias(example, prog)
357 Depends(prog, objects)
360 if env['neon'] == '1':
361 for file in Glob("examples/neon_*.cpp"):
362 example = os.path.basename( os.path.splitext(str(file))[0])
363 prog = env.Program(example, ['examples/%s.cpp' % example, test_helpers], LIBS=example_libs)
364 alias = env.Alias(example, prog)
365 Depends(prog, objects)