blob: b40b66caabc34cb584e0213946dbb2eee419bd6e [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001# 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.
22import collections
23import os.path
24import re
25import subprocess
26
27VERSION = "v0.0-unreleased"
28SONAME_VERSION="1.0.0"
29
30Import('env')
31Import('vars')
32
Anthony Barbierb2881fc2017-09-29 17:12:12 +010033def build_library(name, sources, static=False, libs=[]):
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034 if static:
Anthony Barbierb2881fc2017-09-29 17:12:12 +010035 obj = arm_compute_env.StaticLibrary(name, source=sources, LIBS = arm_compute_env["LIBS"] + libs)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036 else:
37 if env['set_soname']:
Anthony Barbierb2881fc2017-09-29 17:12:12 +010038 obj = arm_compute_env.SharedLibrary(name, source=sources, SHLIBVERSION = SONAME_VERSION, LIBS = arm_compute_env["LIBS"] + libs)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
40 symlinks = []
41 # Manually delete symlinks or SCons will get confused:
42 directory = os.path.dirname(obj[0].path)
43 library_prefix = obj[0].path[:-(1 + len(SONAME_VERSION))]
44 real_lib = "%s.%s" % (library_prefix, SONAME_VERSION)
45
46 for f in Glob("#%s*" % library_prefix):
47 if str(f) != real_lib:
48 symlinks.append("%s/%s" % (directory,str(f)))
49
50 clean = arm_compute_env.Command('clean-%s' % str(obj[0]), [], Delete(symlinks))
51 Default(clean)
52 Depends(obj, clean)
53 else:
Anthony Barbierb2881fc2017-09-29 17:12:12 +010054 obj = arm_compute_env.SharedLibrary(name, source=sources, LIBS = arm_compute_env["LIBS"] + libs)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010055
56 Default(obj)
57 return obj
58
59def resolve_includes(target, source, env):
60 # File collection
61 FileEntry = collections.namedtuple('FileEntry', 'target_name file_contents')
62
63 # Include pattern
64 pattern = re.compile("#include \"(.*)\"")
65
66 # Get file contents
67 files = []
68 for i in range(len(source)):
69 src = source[i]
70 dst = target[i]
71 contents = src.get_contents().splitlines()
72 entry = FileEntry(target_name=dst, file_contents=contents)
73 files.append((os.path.basename(src.get_path()),entry))
74
75 # Create dictionary of tupled list
76 files_dict = dict(files)
77
78 # Check for includes (can only be files in the same folder)
79 final_files = []
80 for file in files:
81 done = False
82 tmp_file = file[1].file_contents
83 while not done:
84 file_count = 0
85 updated_file = []
86 for line in tmp_file:
87 found = pattern.search(line)
88 if found:
89 include_file = found.group(1)
90 data = files_dict[include_file].file_contents
91 updated_file.extend(data)
92 else:
93 updated_file.append(line)
94 file_count += 1
95
96 # Check if all include are replaced.
97 if file_count == len(tmp_file):
98 done = True
99
100 # Update temp file
101 tmp_file = updated_file
102
103 # Append and prepend string literal identifiers and add expanded file to final list
104 tmp_file.insert(0, "R\"(\n")
105 tmp_file.append("\n)\"")
106 entry = FileEntry(target_name=file[1].target_name, file_contents=tmp_file)
107 final_files.append((file[0], entry))
108
109 # Write output files
110 for file in final_files:
111 with open(file[1].target_name.get_path(), 'w+') as out_file:
112 out_file.write( "\n".join( file[1].file_contents ))
113
114def create_version_file(target, source, env):
115# Generate string with build options library version to embed in the library:
116 try:
117 git_hash = subprocess.check_output(["git", "rev-parse", "HEAD"])
118 except (OSError, subprocess.CalledProcessError):
119 git_hash="unknown"
120
121 version_filename = "%s/arm_compute_version.embed" % Dir("src/core").path
122 build_info = "\"arm_compute_version=%s Build options: %s Git hash=%s\"" % (VERSION, vars.args, git_hash.strip())
123 with open(target[0].get_path(), "w") as fd:
124 fd.write(build_info)
125
126
127arm_compute_env = env.Clone()
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000128# Don't allow undefined references in the libraries:
129arm_compute_env.Append(LINKFLAGS=['-Wl,--no-undefined','-Wl,--no-allow-shlib-undefined'])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
131generate_embed = [ arm_compute_env.Command("src/core/arm_compute_version.embed", "", action=create_version_file) ]
132arm_compute_env.Append(CPPPATH =[Dir("./src/core/").path] )
133
134if env["os"] not in ["android", "bare_metal"]:
135 arm_compute_env.Append(LIBS = ['pthread'])
136
137arm_compute_env.Append(LIBS = ['dl'])
138
139core_files = Glob('src/core/*.cpp')
140core_files += Glob('src/core/CPP/*.cpp')
141core_files += Glob('src/core/CPP/kernels/*.cpp')
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100142core_files += Glob('src/core/utils/*/*.cpp')
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100143
Anthony Barbier8e6faf12017-08-01 17:03:19 +0100144runtime_files = Glob('src/runtime/*.cpp')
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145# CLHarrisCorners uses the Scheduler to run CPP kernels
Anthony Barbier8e6faf12017-08-01 17:03:19 +0100146runtime_files += Glob('src/runtime/CPP/SingleThreadScheduler.cpp')
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147
148if env['cppthreads']:
Anthony Barbier8e6faf12017-08-01 17:03:19 +0100149 runtime_files += Glob('src/runtime/CPP/CPPScheduler.cpp')
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150
151if env['openmp']:
Anthony Barbier8e6faf12017-08-01 17:03:19 +0100152 runtime_files += Glob('src/runtime/OMP/OMPScheduler.cpp')
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153
154if env['opencl']:
155 core_files += Glob('src/core/CL/*.cpp')
156 core_files += Glob('src/core/CL/kernels/*.cpp')
157
Anthony Barbier8e6faf12017-08-01 17:03:19 +0100158 runtime_files += Glob('src/runtime/CL/*.cpp')
159 runtime_files += Glob('src/runtime/CL/functions/*.cpp')
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160
161 # Generate embed files
162 if env['embed_kernels']:
163 cl_files = Glob('src/core/CL/cl_kernels/*.cl')
164 cl_files += Glob('src/core/CL/cl_kernels/*.h')
165
166 embed_files = [ f.get_path()+"embed" for f in cl_files ]
167 arm_compute_env.Append(CPPPATH =[Dir("./src/core/CL/").path] )
168
169 generate_embed.append(arm_compute_env.Command(embed_files, cl_files, action=resolve_includes))
170
171if env['neon']:
172 core_files += Glob('src/core/NEON/*.cpp')
173 core_files += Glob('src/core/NEON/kernels/*.cpp')
174
Moritz Pflanzer80373f62017-09-15 10:42:58 +0100175 if env['arch'] == "armv7a":
176 core_files += Glob('src/core/NEON/kernels/arm32/*.cpp')
177
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100178 if "arm64-v8" in env['arch']:
179 core_files += Glob('src/core/NEON/kernels/arm64/*.cpp')
180
Anthony Barbier8e6faf12017-08-01 17:03:19 +0100181 runtime_files += Glob('src/runtime/NEON/*.cpp')
182 runtime_files += Glob('src/runtime/NEON/functions/*.cpp')
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183
Anthony Barbier7068f992017-10-26 15:23:08 +0100184if env['gles_compute']:
185 if env['os'] != 'android':
186 arm_compute_env.Append(CPPPATH = ["#opengles-3.1/include", "#opengles-3.1/mali_include"])
187
188 core_files += Glob('src/core/GLES_COMPUTE/*.cpp')
189 core_files += Glob('src/core/GLES_COMPUTE/kernels/*.cpp')
190
191 runtime_files += Glob('src/runtime/GLES_COMPUTE/*.cpp')
192 runtime_files += Glob('src/runtime/GLES_COMPUTE/functions/*.cpp')
193
194 # Generate embed files
195 if env['embed_kernels']:
196 cs_files = Glob('src/core/GLES_COMPUTE/cs_shaders/*.cs')
197 cs_files += Glob('src/core/GLES_COMPUTE/cs_shaders/*.h')
198
199 embed_files = [ f.get_path()+"embed" for f in cs_files ]
200 arm_compute_env.Append(CPPPATH =[Dir("./src/core/GLES_COMPUTE/").path] )
201
202 generate_embed.append(arm_compute_env.Command(embed_files, cs_files, action=resolve_includes))
203
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204static_core_objects = [arm_compute_env.StaticObject(f) for f in core_files]
205shared_core_objects = [arm_compute_env.SharedObject(f) for f in core_files]
206
207arm_compute_core_a = build_library('arm_compute_core-static', static_core_objects, static=True)
208Export('arm_compute_core_a')
209
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100210if env['os'] != 'bare_metal' and not env['standalone']:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 arm_compute_core_so = build_library('arm_compute_core', shared_core_objects, static=False)
212 Export('arm_compute_core_so')
213
Anthony Barbier8e6faf12017-08-01 17:03:19 +0100214shared_runtime_objects = [arm_compute_env.SharedObject(f) for f in runtime_files]
215static_runtime_objects = [arm_compute_env.StaticObject(f) for f in runtime_files]
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216
Anthony Barbierb2881fc2017-09-29 17:12:12 +0100217arm_compute_a = build_library('arm_compute-static', static_runtime_objects, static=True, libs = [ arm_compute_core_a ])
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218Export('arm_compute_a')
219
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100220if env['os'] != 'bare_metal' and not env['standalone']:
Anthony Barbierb2881fc2017-09-29 17:12:12 +0100221 arm_compute_so = build_library('arm_compute', shared_runtime_objects, static=False, libs = [ "arm_compute_core" ])
222 Depends(arm_compute_so, arm_compute_core_so)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223 Export('arm_compute_so')
224
Anthony Barbier2a07e182017-08-04 18:20:27 +0100225if env['neon'] and env['opencl']:
Anthony Barbier6c0348f2017-11-10 18:32:38 +0000226 Import('opencl')
Anthony Barbier2a07e182017-08-04 18:20:27 +0100227 graph_files = Glob('src/graph/*.cpp')
228 graph_files += Glob('src/graph/nodes/*.cpp')
Georgios Pinitas407c3e62017-10-25 18:26:46 +0100229 graph_files += Glob('src/graph/operations/*.cpp')
Georgios Pinitas0c29cd32017-10-18 17:29:27 +0100230
Anthony Barbier2a07e182017-08-04 18:20:27 +0100231 graph_files += Glob('src/graph/CL/*.cpp')
232 graph_files += Glob('src/graph/NEON/*.cpp')
233
234 shared_graph_objects = [arm_compute_env.SharedObject(f) for f in graph_files]
235 static_graph_objects = [arm_compute_env.StaticObject(f) for f in graph_files]
236
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000237 #FIXME OpenCL can't be linked statically: revisit when we can have a NEON only build of the graph library
238 #arm_compute_graph_a = build_library('arm_compute_graph-static', static_graph_objects, static=True, libs = [ arm_compute_a ])
239 #Export('arm_compute_graph_a')
Anthony Barbier2a07e182017-08-04 18:20:27 +0100240
Anthony Barbier6c0348f2017-11-10 18:32:38 +0000241 arm_compute_env.Append(LIBPATH = ["#build/%s/opencl-1.2-stubs" % env['build_dir']])
242 arm_compute_graph_so = build_library('arm_compute_graph', shared_graph_objects, static=False, libs = [ "arm_compute", "arm_compute_core", "OpenCL" ])
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000243 Depends(arm_compute_graph_so, [ arm_compute_so, opencl])
Anthony Barbier2a07e182017-08-04 18:20:27 +0100244 Export('arm_compute_graph_so')
245
Isabella Gottardib28f29d2017-11-09 17:05:07 +0000246 #graph_alias = arm_compute_env.Alias("arm_compute_graph", [arm_compute_graph_a, arm_compute_graph_so])
247 graph_alias = arm_compute_env.Alias("arm_compute_graph", [arm_compute_graph_so])
Anthony Barbier2a07e182017-08-04 18:20:27 +0100248 Default(graph_alias)
249
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100250if env['standalone']:
251 alias = arm_compute_env.Alias("arm_compute", [arm_compute_a])
252else:
253 alias = arm_compute_env.Alias("arm_compute", [arm_compute_a, arm_compute_so])
254
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100255Default(alias)
256
257Default(generate_embed)
Pablo Telloc6cb35a2017-06-21 15:39:47 +0100258
259if env['standalone']:
260 Depends([alias,arm_compute_core_a], generate_embed)
261else:
262 Depends([alias,arm_compute_core_so, arm_compute_core_a], generate_embed)