-
Notifications
You must be signed in to change notification settings - Fork 14
/
sconscript
382 lines (313 loc) · 13.1 KB
/
sconscript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# Copyright (c) 2016, 2017 ARM Limited.
#
# SPDX-License-Identifier: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import collections
import os.path
import re
import subprocess
import SCons
VERSION = "v17.05"
SONAME_VERSION="2.0.0"
Import('env')
Import('vars')
def version_at_least(version, required):
end = min(len(version), len(required))
for i in range(0, end, 2):
if int(version[i]) < int(required[i]):
return False
elif int(version[i]) > int(required[i]):
return True
return True
def build_library(name, sources, libs, static=False):
if static:
obj = env.StaticLibrary(name, source = sources, LIBS=libs )
else:
if env['set_soname']:
obj = env.SharedLibrary(name, source = sources, LIBS=libs, SHLIBVERSION=SONAME_VERSION)
symlinks = []
# Manually delete symlinks or SCons will get confused:
directory = os.path.dirname( obj[0].path )
library_prefix = obj[0].path[:-(1+len(SONAME_VERSION))]
real_lib="%s.%s" % (library_prefix, SONAME_VERSION)
for f in Glob( "#%s*" % library_prefix):
if str(f) != real_lib:
symlinks.append("%s/%s" % (directory,str(f)))
clean = env.Command('clean-%s' % str(obj[0]), [], Delete(symlinks))
Default(clean)
Depends(obj, clean)
else:
obj = env.SharedLibrary(name, source = sources, LIBS=libs)
Default(obj)
return obj
def resolve_includes(target, source, env):
# File collection
FileEntry = collections.namedtuple('FileEntry', 'target_name file_contents')
# Include pattern
pattern = re.compile("#include \"(.*)\"")
# Get file contents
files = []
for s in source:
name = s.rstr().split("/")[-1]
contents = s.get_contents().splitlines()
embed_target_name = s.abspath + "embed"
entry = FileEntry(target_name=embed_target_name, file_contents=contents)
files.append((name,entry))
# Create dictionary of tupled list
files_dict = dict(files)
# Check for includes (can only be files in the same folder)
final_files = []
for file in files:
done = False
tmp_file = file[1].file_contents
while not done:
file_count = 0
updated_file = []
for line in tmp_file:
found = pattern.search(line)
if found:
include_file = found.group(1)
data = files_dict[include_file].file_contents
updated_file.extend(data)
else:
updated_file.append(line)
file_count += 1
# Check if all include are replaced.
if file_count == len(tmp_file):
done = True
# Update temp file
tmp_file = updated_file
# Append and prepend string literal identifiers and add expanded file to final list
tmp_file.insert(0, "R\"(\n")
tmp_file.append("\n)\"")
entry = FileEntry(target_name=file[1].target_name, file_contents=tmp_file)
final_files.append((file[0], entry))
# Write output files
for file in final_files:
with open(file[1].target_name, 'w+') as out_file:
contents = file[1].file_contents
for line in contents:
out_file.write("%s\n" % line)
if GetOption("help"):
Exit(0)
flags = ['-D_GLIBCXX_USE_NANOSLEEP','-Wno-deprecated-declarations','-Wall','-DARCH_ARM',
'-Wextra','-Wno-unused-parameter','-pedantic','-Wdisabled-optimization','-Wformat=2',
'-Winit-self','-Wstrict-overflow=2','-Wswitch-default',
'-fpermissive','-std=gnu++11','-Wno-vla','-Woverloaded-virtual',
'-Wctor-dtor-privacy','-Wsign-promo','-Weffc++','-Wno-format-nonliteral','-Wno-overlength-strings','-Wno-strict-overflow']
if env['neon'] and 'x86' in env['arch']:
print "Cannot compile NEON for x86"
Exit(1)
if env['set_soname'] and not version_at_least(SCons.__version__, "2.4"):
print "Setting the library's SONAME / SHLIBVERSION requires SCons 2.4 or above"
print "Update your version of SCons or use set_soname=0"
Exit(1)
if os.environ.get('CXX','g++') == 'clang++':
flags += ['-Wno-format-nonliteral','-Wno-deprecated-increment-bool','-Wno-vla-extension','-Wno-mismatched-tags']
else:
flags += ['-Wlogical-op','-Wnoexcept','-Wstrict-null-sentinel']
if env['cppthreads']:
flags += ['-DARM_COMPUTE_CPP_SCHEDULER=1']
if env['openmp']:
if os.environ.get('CXX','g++') == 'clang++':
print "Clang does not support OpenMP. Use scheduler=cpp."
Exit(1)
flags += ['-DARM_COMPUTE_OPENMP_SCHEDULER=1','-fopenmp']
env.Append(LINKFLAGS=['-fopenmp'])
files_to_delete = []
# Generate string with build options library version to embed in the library:
try:
git_hash = subprocess.check_output(["git", "rev-parse","HEAD"])
except (OSError, subprocess.CalledProcessError):
git_hash="unknown"
version_filename = "%s/arm_compute_version.embed" % os.path.dirname(Glob("src/core/*")[0].rstr())
build_info = "\"arm_compute_version=%s Build options: %s Git hash=%s\"" % (VERSION, vars.args, git_hash.strip())
with open(version_filename, "w") as fd:
fd.write(build_info)
files_to_delete.append(version_filename)
core_libs = ['dl']
libs = ['dl']
prefix=""
if env['arch'] == 'armv7a':
flags += ['-march=armv7-a','-mthumb','-mfpu=neon']
if env['os'] in ['linux','bare_metal']:
prefix = "arm-linux-gnueabihf-"
flags += ['-mfloat-abi=hard']
elif env['os'] == 'android':
prefix = "arm-linux-androideabi-"
flags += ['-mfloat-abi=softfp']
elif env['arch'] == 'arm64-v8a':
flags += ['-march=armv8-a']
if env['os'] in ['linux','bare_metal']:
prefix = "aarch64-linux-gnu-"
elif env['os'] == 'android':
prefix = "aarch64-linux-android-"
elif env['arch'] == 'arm64-v8.2-a':
flags += ['-march=armv8.2-a+fp16+simd']
flags += ['-DARM_COMPUTE_ENABLE_FP16']
if env['os'] in ['linux','bare_metal']:
prefix = "aarch64-linux-gnu-"
elif env['os'] == 'android':
prefix = "aarch64-linux-android-"
elif env['arch'] == 'x86_32':
flags += ['-m32']
elif env['arch'] == 'x86_64':
flags += ['-m64']
if env['build'] == 'native':
prefix = ""
env['CC'] = prefix + os.environ.get('CC','gcc')
env['CXX'] = prefix + os.environ.get('CXX','g++')
env['LD'] = prefix + "ld"
env['AS'] = prefix + "as"
env['AR'] = prefix + "ar"
env['RANLIB'] = prefix + "ranlib"
try:
compiler_ver = subprocess.check_output( [env['CXX'] , "-dumpversion"] ).strip()
except OSError:
print "ERROR: Compiler '%s' not found" % env['CXX']
Exit(1)
if os.environ.get('CXX','g++') == 'g++':
if env['arch'] == 'arm64-v8.2-a' and not version_at_least(compiler_ver, '6.2.1'):
print "GCC 6.2.1 or newer is required to compile armv8.2-a code"
Exit(1)
if env['arch'] == 'arm64-v8a' and not version_at_least(compiler_ver, '4.9'):
print "GCC 4.9 or newer is required to compile NEON code for AArch64"
Exit(1)
if version_at_least(compiler_ver, '6.1'):
flags += ['-Wno-ignored-attributes']
if compiler_ver == '4.8.3':
flags += ['-Wno-array-bounds']
if env['Werror']:
flags += ['-Werror']
example_libs = []
if env['os'] == 'android':
flags += ['-DANDROID']
env.Append(LINKFLAGS=['-pie','-static-libstdc++'])
example_libs = ['arm_compute-static']
elif env['os'] == 'bare_metal':
env.Append(LINKFLAGS=['-static'])
flags += ['-fPIC','-DNO_MULTI_THREADING']
example_libs = ['arm_compute-static']
else:
libs += ['pthread']
example_libs = ['arm_compute']
if env['opencl']:
if env['os'] == 'bare_metal':
raise Exception("Cannot link OpenCL statically, which is required on bare metal")
if env['embed_kernels']:
flags += ['-DEMBEDDED_KERNELS']
if env['debug']:
env['asserts'] = True
flags += ['-O0','-g','-gdwarf-2']
else:
flags += ['-O3','-ftree-vectorize']
if env['asserts']:
flags += ['-DARM_COMPUTE_ASSERTS_ENABLED']
env.Append(CPPPATH=['.','#include'])
env.Append(LIBPATH=['#build/%s' % env['build_dir'],'.'])
env.Append(CXXFLAGS=flags)
env.Append(CXXFLAGS=env['extra_cxx_flags'])
core_files = Glob('src/core/*.cpp')
core_files += Glob('src/core/CPP/*.cpp')
files = Glob('src/runtime/*.cpp')
embed_files = []
core_files += Glob('src/core/CPP/kernels/*.cpp')
# CLHarrisCorners uses the Scheduler to run CPP kernels
files += Glob('src/runtime/CPP/SingleThreadScheduler.cpp')
if env['os'] == 'bare_metal':
if env['cppthreads'] or env['openmp']:
print "ERROR: OpenMP and C++11 threads not supported in bare_metal. Use cppthreads=0 openmp=0"
Exit(1)
else:
if env['cppthreads']:
files += Glob('src/runtime/CPP/CPPScheduler.cpp')
if env['openmp']:
files += Glob('src/runtime/OMP/OMPScheduler.cpp')
if env['opencl']:
core_files += Glob('src/core/CL/*.cpp')
core_files += Glob('src/core/CL/kernels/*.cpp')
files += Glob('src/runtime/CL/*.cpp')
files += Glob('src/runtime/CL/functions/*.cpp')
# Generate embed files
if env['embed_kernels']:
cl_files = Glob('src/core/CL/cl_kernels/*.cl') + Glob('src/core/CL/cl_kernels/*.h')
source_list = []
for file in cl_files:
source_name = file.rstr()
source_list.append(source_name)
embed_files.append(source_name + "embed")
generate_embed = env.Command(embed_files, source_list, action=resolve_includes)
Default(generate_embed)
files_to_delete += embed_files
if env['neon']:
core_files += Glob('src/core/NEON/*.cpp')
core_files += Glob('src/core/NEON/kernels/*.cpp')
files += Glob('src/runtime/NEON/*.cpp')
files += Glob('src/runtime/NEON/functions/*.cpp')
objects=[]
static_core_objects = [ env.StaticObject( f ) for f in core_files ]
shared_core_objects = [ env.SharedObject( f ) for f in core_files ]
arm_compute_core_a = build_library('arm_compute_core-static', static_core_objects, core_libs, static=True)
objects.append(arm_compute_core_a)
Export('arm_compute_core_a')
if env['os'] != 'bare_metal':
arm_compute_core_so = build_library('arm_compute_core', shared_core_objects, core_libs, static=False)
objects.append(arm_compute_core_so)
Export('arm_compute_core_so')
shared_objects = [ env.SharedObject( f ) for f in files ]
static_objects = [ env.StaticObject( f ) for f in files ]
arm_compute_a = build_library('arm_compute-static', static_core_objects + static_objects, libs, static=True)
objects.append(arm_compute_a)
Export('arm_compute_a')
if env['os'] != 'bare_metal':
arm_compute_so = build_library('arm_compute', shared_core_objects + shared_objects, libs, static=False)
objects.append(arm_compute_so)
Export('arm_compute_so')
# Delete produced embed files
clean_embed = env.Command('clean-embed', [], Delete(files_to_delete))
Default(clean_embed)
env.Depends(clean_embed, objects)
alias = env.Alias("arm_compute",objects)
Default(alias)
# Build examples
if env['examples']:
test_helpers = env.Object("test_helpers/Utils.cpp")
if env['opencl'] and env['neon']:
for file in Glob("examples/neoncl_*.cpp"):
example = os.path.basename( os.path.splitext(str(file))[0])
prog = env.Program(example, ['examples/%s.cpp' % example, test_helpers], LIBS=example_libs+['OpenCL'])
alias = env.Alias(example, prog)
Depends(prog, objects)
Default( alias )
if env['opencl']:
for file in Glob("examples/cl_*.cpp"):
example = os.path.basename( os.path.splitext(str(file))[0])
prog = env.Program(example, ['examples/%s.cpp' % example, test_helpers], LIBS=example_libs+['OpenCL'])
alias = env.Alias(example, prog)
Depends(prog, objects)
Default( alias )
if env['neon']:
for file in Glob("examples/neon_*.cpp"):
example = os.path.basename( os.path.splitext(str(file))[0])
prog = env.Program(example, ['examples/%s.cpp' % example, test_helpers], LIBS=example_libs)
alias = env.Alias(example, prog)
Depends(prog, objects)
Default( alias )
Export('env')