Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | import collections |
| 23 | import os.path |
| 24 | import re |
| 25 | import subprocess |
| 26 | |
| 27 | VERSION = "v0.0-unreleased" |
| 28 | SONAME_VERSION="1.0.0" |
| 29 | |
| 30 | Import('env') |
| 31 | Import('vars') |
| 32 | |
Anthony Barbier | b2881fc | 2017-09-29 17:12:12 +0100 | [diff] [blame] | 33 | def build_library(name, sources, static=False, libs=[]): |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | if static: |
Anthony Barbier | b2881fc | 2017-09-29 17:12:12 +0100 | [diff] [blame] | 35 | obj = arm_compute_env.StaticLibrary(name, source=sources, LIBS = arm_compute_env["LIBS"] + libs) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | else: |
| 37 | if env['set_soname']: |
Anthony Barbier | b2881fc | 2017-09-29 17:12:12 +0100 | [diff] [blame] | 38 | obj = arm_compute_env.SharedLibrary(name, source=sources, SHLIBVERSION = SONAME_VERSION, LIBS = arm_compute_env["LIBS"] + libs) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
| 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 Barbier | b2881fc | 2017-09-29 17:12:12 +0100 | [diff] [blame] | 54 | obj = arm_compute_env.SharedLibrary(name, source=sources, LIBS = arm_compute_env["LIBS"] + libs) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 55 | |
| 56 | Default(obj) |
| 57 | return obj |
| 58 | |
| 59 | def 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 | |
| 114 | def 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 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 126 | arm_compute_env = env.Clone() |
Anthony Barbier | 6a3daf1 | 2018-02-19 17:24:27 +0000 | [diff] [blame] | 127 | |
| 128 | # Generate embed files |
| 129 | generate_embed = [ arm_compute_env.Command("src/core/arm_compute_version.embed", "", action=create_version_file) ] |
| 130 | if env['opencl'] and env['embed_kernels']: |
| 131 | cl_files = Glob('src/core/CL/cl_kernels/*.cl') |
| 132 | cl_files += Glob('src/core/CL/cl_kernels/*.h') |
| 133 | |
| 134 | embed_files = [ f.get_path()+"embed" for f in cl_files ] |
| 135 | arm_compute_env.Append(CPPPATH =[Dir("./src/core/CL/").path] ) |
| 136 | |
| 137 | generate_embed.append(arm_compute_env.Command(embed_files, cl_files, action=resolve_includes)) |
| 138 | |
| 139 | if env['gles_compute'] and env['embed_kernels']: |
| 140 | cs_files = Glob('src/core/GLES_COMPUTE/cs_shaders/*.cs') |
| 141 | cs_files += Glob('src/core/GLES_COMPUTE/cs_shaders/*.h') |
| 142 | |
| 143 | embed_files = [ f.get_path()+"embed" for f in cs_files ] |
| 144 | arm_compute_env.Append(CPPPATH =[Dir("./src/core/GLES_COMPUTE/").path] ) |
| 145 | |
| 146 | generate_embed.append(arm_compute_env.Command(embed_files, cs_files, action=resolve_includes)) |
| 147 | |
| 148 | Default(generate_embed) |
| 149 | if env["build"] == "embed_only": |
| 150 | Return() |
| 151 | |
Isabella Gottardi | b28f29d | 2017-11-09 17:05:07 +0000 | [diff] [blame] | 152 | # Don't allow undefined references in the libraries: |
Michalis Spyrou | 989f1b5 | 2018-01-23 16:47:45 +0000 | [diff] [blame] | 153 | arm_compute_env.Append(LINKFLAGS=['-Wl,--no-undefined']) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 154 | arm_compute_env.Append(CPPPATH =[Dir("./src/core/").path] ) |
| 155 | |
| 156 | if env["os"] not in ["android", "bare_metal"]: |
| 157 | arm_compute_env.Append(LIBS = ['pthread']) |
| 158 | |
| 159 | arm_compute_env.Append(LIBS = ['dl']) |
| 160 | |
| 161 | core_files = Glob('src/core/*.cpp') |
| 162 | core_files += Glob('src/core/CPP/*.cpp') |
| 163 | core_files += Glob('src/core/CPP/kernels/*.cpp') |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 164 | core_files += Glob('src/core/utils/*/*.cpp') |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | |
Anthony Barbier | 8e6faf1 | 2017-08-01 17:03:19 +0100 | [diff] [blame] | 166 | runtime_files = Glob('src/runtime/*.cpp') |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 167 | runtime_files += Glob('src/runtime/CPP/ICPPSimpleFunction.cpp') |
| 168 | runtime_files += Glob('src/runtime/CPP/functions/*.cpp') |
| 169 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 170 | # CLHarrisCorners uses the Scheduler to run CPP kernels |
Anthony Barbier | 8e6faf1 | 2017-08-01 17:03:19 +0100 | [diff] [blame] | 171 | runtime_files += Glob('src/runtime/CPP/SingleThreadScheduler.cpp') |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 173 | # FIXME : Rename graph2 -> graph |
| 174 | graph2_files = Glob('src/graph2/*.cpp') |
| 175 | graph2_files += Glob('src/graph2/*/*.cpp') |
| 176 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | if env['cppthreads']: |
Anthony Barbier | 8e6faf1 | 2017-08-01 17:03:19 +0100 | [diff] [blame] | 178 | runtime_files += Glob('src/runtime/CPP/CPPScheduler.cpp') |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 179 | |
| 180 | if env['openmp']: |
Anthony Barbier | 8e6faf1 | 2017-08-01 17:03:19 +0100 | [diff] [blame] | 181 | runtime_files += Glob('src/runtime/OMP/OMPScheduler.cpp') |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 182 | |
| 183 | if env['opencl']: |
| 184 | core_files += Glob('src/core/CL/*.cpp') |
| 185 | core_files += Glob('src/core/CL/kernels/*.cpp') |
| 186 | |
Anthony Barbier | 8e6faf1 | 2017-08-01 17:03:19 +0100 | [diff] [blame] | 187 | runtime_files += Glob('src/runtime/CL/*.cpp') |
| 188 | runtime_files += Glob('src/runtime/CL/functions/*.cpp') |
Georgios Pinitas | c0d1c86 | 2018-03-23 15:13:15 +0000 | [diff] [blame] | 189 | runtime_files += Glob('src/runtime/CL/tuners/*.cpp') |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 190 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 191 | graph2_files += Glob('src/graph2/backends/CL/*.cpp') |
| 192 | |
| 193 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 194 | if env['neon']: |
| 195 | core_files += Glob('src/core/NEON/*.cpp') |
| 196 | core_files += Glob('src/core/NEON/kernels/*.cpp') |
| 197 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 198 | core_files += Glob('src/core/NEON/kernels/arm_gemm/*.cpp') |
| 199 | |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 200 | # build winograd sources for either v7a / v8a |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 201 | core_files += Glob('src/core/NEON/kernels/convolution/*/*.cpp') |
| 202 | core_files += Glob('src/core/NEON/kernels/convolution/winograd/*/*.cpp') |
Pablo Tello | 11c3b33 | 2018-01-25 15:05:13 +0000 | [diff] [blame] | 203 | arm_compute_env.Append(CPPPATH = ["arm_compute/core/NEON/kernels/winograd/", "arm_compute/core/NEON/kernels/assembly/"]) |
Pablo Tello | 9ceebbe | 2018-01-10 16:44:13 +0000 | [diff] [blame] | 204 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 205 | graph2_files += Glob('src/graph2/backends/NEON/*.cpp') |
| 206 | |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 207 | if env['arch'] == "armv7a": |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 208 | core_files += Glob('src/core/NEON/kernels/arm_gemm/kernels/a32_*/*.cpp') |
| 209 | |
Moritz Pflanzer | 80373f6 | 2017-09-15 10:42:58 +0100 | [diff] [blame] | 210 | |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 211 | if "arm64-v8" in env['arch']: |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 212 | core_files += Glob('src/core/NEON/kernels/arm_gemm/kernels/a64_*/*.cpp') |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 213 | |
Anthony Barbier | 8e6faf1 | 2017-08-01 17:03:19 +0100 | [diff] [blame] | 214 | runtime_files += Glob('src/runtime/NEON/*.cpp') |
| 215 | runtime_files += Glob('src/runtime/NEON/functions/*.cpp') |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 216 | |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 217 | if env['gles_compute']: |
| 218 | if env['os'] != 'android': |
| 219 | arm_compute_env.Append(CPPPATH = ["#opengles-3.1/include", "#opengles-3.1/mali_include"]) |
| 220 | |
| 221 | core_files += Glob('src/core/GLES_COMPUTE/*.cpp') |
| 222 | core_files += Glob('src/core/GLES_COMPUTE/kernels/*.cpp') |
| 223 | |
| 224 | runtime_files += Glob('src/runtime/GLES_COMPUTE/*.cpp') |
| 225 | runtime_files += Glob('src/runtime/GLES_COMPUTE/functions/*.cpp') |
| 226 | |
Georgios Pinitas | fbb8054 | 2018-03-27 17:15:49 +0100 | [diff] [blame] | 227 | graph2_files += Glob('src/graph2/backends/GLES/*.cpp') |
| 228 | |
Anthony Barbier | ab49073 | 2017-12-05 13:32:37 +0000 | [diff] [blame] | 229 | arm_compute_core_a = build_library('arm_compute_core-static', core_files, static=True) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 230 | Export('arm_compute_core_a') |
| 231 | |
Pablo Tello | c6cb35a | 2017-06-21 15:39:47 +0100 | [diff] [blame] | 232 | if env['os'] != 'bare_metal' and not env['standalone']: |
Anthony Barbier | ab49073 | 2017-12-05 13:32:37 +0000 | [diff] [blame] | 233 | arm_compute_core_so = build_library('arm_compute_core', core_files, static=False) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 234 | Export('arm_compute_core_so') |
| 235 | |
Anthony Barbier | ab49073 | 2017-12-05 13:32:37 +0000 | [diff] [blame] | 236 | arm_compute_a = build_library('arm_compute-static', runtime_files, static=True, libs = [ arm_compute_core_a ]) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 237 | Export('arm_compute_a') |
| 238 | |
Pablo Tello | c6cb35a | 2017-06-21 15:39:47 +0100 | [diff] [blame] | 239 | if env['os'] != 'bare_metal' and not env['standalone']: |
Anthony Barbier | ab49073 | 2017-12-05 13:32:37 +0000 | [diff] [blame] | 240 | arm_compute_so = build_library('arm_compute', runtime_files, static=False, libs = [ "arm_compute_core" ]) |
Anthony Barbier | b2881fc | 2017-09-29 17:12:12 +0100 | [diff] [blame] | 241 | Depends(arm_compute_so, arm_compute_core_so) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 242 | Export('arm_compute_so') |
| 243 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 244 | arm_compute_graph2_a = build_library('arm_compute_graph2-static', graph2_files, static=True, libs = [ arm_compute_a]) |
| 245 | Export('arm_compute_graph2_a') |
| 246 | |
| 247 | if env['os'] != 'bare_metal' and not env['standalone']: |
| 248 | arm_compute_graph2_so = build_library('arm_compute_graph2', graph2_files, static=False, libs = [ "arm_compute" , "arm_compute_core"]) |
| 249 | Depends(arm_compute_graph2_so, arm_compute_so) |
| 250 | Export('arm_compute_graph2_so') |
| 251 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 252 | if env['neon'] and env['opencl']: |
Anthony Barbier | 6c0348f | 2017-11-10 18:32:38 +0000 | [diff] [blame] | 253 | Import('opencl') |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 254 | graph_files = Glob('src/graph/*.cpp') |
| 255 | graph_files += Glob('src/graph/nodes/*.cpp') |
Georgios Pinitas | 407c3e6 | 2017-10-25 18:26:46 +0100 | [diff] [blame] | 256 | graph_files += Glob('src/graph/operations/*.cpp') |
Georgios Pinitas | 0c29cd3 | 2017-10-18 17:29:27 +0100 | [diff] [blame] | 257 | |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 258 | graph_files += Glob('src/graph/CL/*.cpp') |
| 259 | graph_files += Glob('src/graph/NEON/*.cpp') |
| 260 | |
| 261 | shared_graph_objects = [arm_compute_env.SharedObject(f) for f in graph_files] |
| 262 | static_graph_objects = [arm_compute_env.StaticObject(f) for f in graph_files] |
| 263 | |
Georgios Pinitas | 9873ea3 | 2017-12-05 15:28:55 +0000 | [diff] [blame] | 264 | arm_compute_graph_a = build_library('arm_compute_graph-static', static_graph_objects, static=True, libs = [ arm_compute_a ]) |
| 265 | Export('arm_compute_graph_a') |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 266 | |
Anthony Barbier | 6c0348f | 2017-11-10 18:32:38 +0000 | [diff] [blame] | 267 | arm_compute_env.Append(LIBPATH = ["#build/%s/opencl-1.2-stubs" % env['build_dir']]) |
Anthony Barbier | a9e1533 | 2017-12-22 16:37:30 +0000 | [diff] [blame] | 268 | arm_compute_graph_so = build_library('arm_compute_graph', shared_graph_objects, static=False, libs = [ "arm_compute", "arm_compute_core"]) |
Georgios Pinitas | 9873ea3 | 2017-12-05 15:28:55 +0000 | [diff] [blame] | 269 | Depends(arm_compute_graph_so, arm_compute_so) |
| 270 | Depends(arm_compute_graph_so, opencl) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 271 | Export('arm_compute_graph_so') |
| 272 | |
Georgios Pinitas | 9873ea3 | 2017-12-05 15:28:55 +0000 | [diff] [blame] | 273 | graph_alias = arm_compute_env.Alias("arm_compute_graph", [arm_compute_graph_a, arm_compute_graph_so]) |
Anthony Barbier | 2a07e18 | 2017-08-04 18:20:27 +0100 | [diff] [blame] | 274 | Default(graph_alias) |
| 275 | |
Pablo Tello | c6cb35a | 2017-06-21 15:39:47 +0100 | [diff] [blame] | 276 | if env['standalone']: |
| 277 | alias = arm_compute_env.Alias("arm_compute", [arm_compute_a]) |
| 278 | else: |
| 279 | alias = arm_compute_env.Alias("arm_compute", [arm_compute_a, arm_compute_so]) |
| 280 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 281 | Default(alias) |
| 282 | |
Pablo Tello | c6cb35a | 2017-06-21 15:39:47 +0100 | [diff] [blame] | 283 | if env['standalone']: |
| 284 | Depends([alias,arm_compute_core_a], generate_embed) |
| 285 | else: |
| 286 | Depends([alias,arm_compute_core_so, arm_compute_core_a], generate_embed) |