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 | |
| 33 | def build_library(name, sources, static=False): |
| 34 | if static: |
| 35 | obj = arm_compute_env.StaticLibrary(name, source=sources) |
| 36 | else: |
| 37 | if env['set_soname']: |
| 38 | obj = arm_compute_env.SharedLibrary(name, source=sources, SHLIBVERSION = SONAME_VERSION) |
| 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: |
| 54 | obj = arm_compute_env.SharedLibrary(name, source=sources) |
| 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 | |
| 126 | |
| 127 | arm_compute_env = env.Clone() |
| 128 | |
| 129 | generate_embed = [ arm_compute_env.Command("src/core/arm_compute_version.embed", "", action=create_version_file) ] |
| 130 | arm_compute_env.Append(CPPPATH =[Dir("./src/core/").path] ) |
| 131 | |
| 132 | if env["os"] not in ["android", "bare_metal"]: |
| 133 | arm_compute_env.Append(LIBS = ['pthread']) |
| 134 | |
| 135 | arm_compute_env.Append(LIBS = ['dl']) |
| 136 | |
| 137 | core_files = Glob('src/core/*.cpp') |
| 138 | core_files += Glob('src/core/CPP/*.cpp') |
| 139 | core_files += Glob('src/core/CPP/kernels/*.cpp') |
| 140 | |
| 141 | files = Glob('src/runtime/*.cpp') |
| 142 | # CLHarrisCorners uses the Scheduler to run CPP kernels |
| 143 | files += Glob('src/runtime/CPP/SingleThreadScheduler.cpp') |
| 144 | |
| 145 | if env['cppthreads']: |
| 146 | files += Glob('src/runtime/CPP/CPPScheduler.cpp') |
| 147 | |
| 148 | if env['openmp']: |
| 149 | files += Glob('src/runtime/OMP/OMPScheduler.cpp') |
| 150 | |
| 151 | if env['opencl']: |
| 152 | core_files += Glob('src/core/CL/*.cpp') |
| 153 | core_files += Glob('src/core/CL/kernels/*.cpp') |
| 154 | |
| 155 | files += Glob('src/runtime/CL/*.cpp') |
| 156 | files += Glob('src/runtime/CL/functions/*.cpp') |
| 157 | |
| 158 | # Generate embed files |
| 159 | if env['embed_kernels']: |
| 160 | cl_files = Glob('src/core/CL/cl_kernels/*.cl') |
| 161 | cl_files += Glob('src/core/CL/cl_kernels/*.h') |
| 162 | |
| 163 | embed_files = [ f.get_path()+"embed" for f in cl_files ] |
| 164 | arm_compute_env.Append(CPPPATH =[Dir("./src/core/CL/").path] ) |
| 165 | |
| 166 | generate_embed.append(arm_compute_env.Command(embed_files, cl_files, action=resolve_includes)) |
| 167 | |
| 168 | if env['neon']: |
| 169 | core_files += Glob('src/core/NEON/*.cpp') |
| 170 | core_files += Glob('src/core/NEON/kernels/*.cpp') |
| 171 | |
| 172 | files += Glob('src/runtime/NEON/*.cpp') |
| 173 | files += Glob('src/runtime/NEON/functions/*.cpp') |
| 174 | |
| 175 | static_core_objects = [arm_compute_env.StaticObject(f) for f in core_files] |
| 176 | shared_core_objects = [arm_compute_env.SharedObject(f) for f in core_files] |
| 177 | |
| 178 | arm_compute_core_a = build_library('arm_compute_core-static', static_core_objects, static=True) |
| 179 | Export('arm_compute_core_a') |
| 180 | |
Pablo Tello | c6cb35a | 2017-06-21 15:39:47 +0100 | [diff] [blame] | 181 | if env['os'] != 'bare_metal' and not env['standalone']: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 182 | arm_compute_core_so = build_library('arm_compute_core', shared_core_objects, static=False) |
| 183 | Export('arm_compute_core_so') |
| 184 | |
| 185 | shared_objects = [arm_compute_env.SharedObject(f) for f in files] |
| 186 | static_objects = [arm_compute_env.StaticObject(f) for f in files] |
| 187 | |
| 188 | arm_compute_a = build_library('arm_compute-static', static_core_objects + static_objects, static=True) |
| 189 | Export('arm_compute_a') |
| 190 | |
Pablo Tello | c6cb35a | 2017-06-21 15:39:47 +0100 | [diff] [blame] | 191 | if env['os'] != 'bare_metal' and not env['standalone']: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 192 | arm_compute_so = build_library('arm_compute', shared_core_objects + shared_objects, static=False) |
| 193 | Export('arm_compute_so') |
| 194 | |
Pablo Tello | c6cb35a | 2017-06-21 15:39:47 +0100 | [diff] [blame] | 195 | if env['standalone']: |
| 196 | alias = arm_compute_env.Alias("arm_compute", [arm_compute_a]) |
| 197 | else: |
| 198 | alias = arm_compute_env.Alias("arm_compute", [arm_compute_a, arm_compute_so]) |
| 199 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 200 | Default(alias) |
| 201 | |
| 202 | Default(generate_embed) |
Pablo Tello | c6cb35a | 2017-06-21 15:39:47 +0100 | [diff] [blame] | 203 | |
| 204 | if env['standalone']: |
| 205 | Depends([alias,arm_compute_core_a], generate_embed) |
| 206 | else: |
| 207 | Depends([alias,arm_compute_core_so, arm_compute_core_a], generate_embed) |