Andrew Top | 200ce4b | 2018-01-29 13:43:50 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2016 Google Inc. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
| 7 | |
| 8 | # Generate Android.bp for Skia from GN configuration. |
| 9 | |
| 10 | import json |
| 11 | import os |
| 12 | import pprint |
| 13 | import string |
| 14 | import subprocess |
| 15 | import tempfile |
| 16 | |
| 17 | tool_cflags = [ |
| 18 | '-Wno-unused-parameter', |
| 19 | ] |
| 20 | |
| 21 | # It's easier to maintain one list instead of separate lists. |
| 22 | tool_shared_libs = [ |
| 23 | 'liblog', |
| 24 | 'libGLESv2', |
| 25 | 'libEGL', |
| 26 | 'libvulkan', |
| 27 | 'libz', |
| 28 | 'libjpeg', |
| 29 | 'libpng', |
| 30 | 'libicuuc', |
| 31 | 'libicui18n', |
| 32 | 'libexpat', |
| 33 | 'libft2', |
| 34 | 'libdng_sdk', |
| 35 | 'libpiex', |
| 36 | 'libcutils', |
| 37 | 'libnativewindow', |
| 38 | ] |
| 39 | |
| 40 | # The ordering here is important: libsfntly needs to come after libskia. |
| 41 | tool_static_libs = [ |
| 42 | 'libarect', |
| 43 | 'libjsoncpp', |
| 44 | 'libskia', |
| 45 | 'libsfntly', |
| 46 | 'libwebp-decode', |
| 47 | 'libwebp-encode', |
| 48 | ] |
| 49 | |
| 50 | # First we start off with a template for Android.bp, |
| 51 | # with holes for source lists and include directories. |
| 52 | bp = string.Template('''// This file is autogenerated by gn_to_bp.py. |
| 53 | |
| 54 | cc_library { |
| 55 | name: "libskia", |
| 56 | cflags: [ |
| 57 | "-fexceptions", |
| 58 | "-Wno-unused-parameter", |
| 59 | "-U_FORTIFY_SOURCE", |
| 60 | "-D_FORTIFY_SOURCE=1", |
| 61 | "-DSKIA_IMPLEMENTATION=1", |
| 62 | "-DATRACE_TAG=ATRACE_TAG_VIEW", |
| 63 | ], |
| 64 | |
| 65 | export_include_dirs: [ |
| 66 | $export_includes |
| 67 | ], |
| 68 | |
| 69 | local_include_dirs: [ |
| 70 | $local_includes |
| 71 | ], |
| 72 | |
| 73 | srcs: [ |
| 74 | $srcs |
| 75 | ], |
| 76 | |
| 77 | arch: { |
| 78 | arm: { |
| 79 | srcs: [ |
| 80 | $arm_srcs |
| 81 | ], |
| 82 | |
| 83 | armv7_a_neon: { |
| 84 | srcs: [ |
| 85 | $arm_neon_srcs |
| 86 | ], |
| 87 | }, |
| 88 | }, |
| 89 | |
| 90 | arm64: { |
| 91 | srcs: [ |
| 92 | $arm64_srcs |
| 93 | ], |
| 94 | }, |
| 95 | |
| 96 | mips: { |
| 97 | srcs: [ |
| 98 | $none_srcs |
| 99 | ], |
| 100 | }, |
| 101 | |
| 102 | mips64: { |
| 103 | srcs: [ |
| 104 | $none_srcs |
| 105 | ], |
| 106 | }, |
| 107 | |
| 108 | x86: { |
| 109 | srcs: [ |
| 110 | $x86_srcs |
| 111 | ], |
| 112 | }, |
| 113 | |
| 114 | x86_64: { |
| 115 | srcs: [ |
| 116 | $x86_srcs |
| 117 | ], |
| 118 | }, |
| 119 | }, |
| 120 | |
| 121 | shared_libs: [ |
| 122 | "libEGL", |
| 123 | "libGLESv2", |
| 124 | "libdng_sdk", |
| 125 | "libexpat", |
| 126 | "libft2", |
| 127 | "libicui18n", |
| 128 | "libicuuc", |
| 129 | "libjpeg", |
| 130 | "liblog", |
| 131 | "libpiex", |
| 132 | "libpng", |
| 133 | "libvulkan", |
| 134 | "libz", |
| 135 | "libcutils", |
| 136 | "libnativewindow", |
| 137 | ], |
| 138 | static_libs: [ |
| 139 | "libarect", |
| 140 | "libsfntly", |
| 141 | "libwebp-decode", |
| 142 | "libwebp-encode", |
| 143 | ], |
| 144 | } |
| 145 | |
| 146 | cc_test { |
| 147 | name: "skia_dm", |
| 148 | |
| 149 | cflags: [ |
| 150 | $tool_cflags |
| 151 | ], |
| 152 | |
| 153 | local_include_dirs: [ |
| 154 | $dm_includes |
| 155 | ], |
| 156 | |
| 157 | srcs: [ |
| 158 | $dm_srcs |
| 159 | ], |
| 160 | |
| 161 | shared_libs: [ |
| 162 | $tool_shared_libs |
| 163 | ], |
| 164 | |
| 165 | static_libs: [ |
| 166 | $tool_static_libs |
| 167 | ], |
| 168 | } |
| 169 | |
| 170 | cc_test { |
| 171 | name: "skia_nanobench", |
| 172 | |
| 173 | cflags: [ |
| 174 | $tool_cflags |
| 175 | ], |
| 176 | |
| 177 | local_include_dirs: [ |
| 178 | $nanobench_includes |
| 179 | ], |
| 180 | |
| 181 | srcs: [ |
| 182 | $nanobench_srcs |
| 183 | ], |
| 184 | |
| 185 | shared_libs: [ |
| 186 | $tool_shared_libs |
| 187 | ], |
| 188 | |
| 189 | static_libs: [ |
| 190 | $tool_static_libs |
| 191 | ], |
| 192 | }''') |
| 193 | |
| 194 | # We'll run GN to get the main source lists and include directories for Skia. |
| 195 | gn_args = { |
| 196 | 'is_official_build': 'true', |
| 197 | 'skia_enable_tools': 'true', |
| 198 | 'skia_use_vulkan': 'true', |
| 199 | 'target_cpu': '"none"', |
| 200 | 'target_os': '"android"', |
| 201 | } |
| 202 | gn_args = ' '.join(sorted('%s=%s' % (k,v) for (k,v) in gn_args.iteritems())) |
| 203 | |
| 204 | tmp = tempfile.mkdtemp() |
| 205 | subprocess.check_call(['gn', 'gen', tmp, '--args=%s' % gn_args, '--ide=json']) |
| 206 | |
| 207 | js = json.load(open(os.path.join(tmp, 'project.json'))) |
| 208 | |
| 209 | def strip_slashes(lst): |
| 210 | return {str(p.lstrip('/')) for p in lst} |
| 211 | |
| 212 | srcs = strip_slashes(js['targets']['//:skia']['sources']) |
| 213 | local_includes = strip_slashes(js['targets']['//:skia']['include_dirs']) |
| 214 | export_includes = strip_slashes(js['targets']['//:public']['include_dirs']) |
| 215 | |
| 216 | dm_srcs = strip_slashes(js['targets']['//:dm']['sources']) |
| 217 | dm_includes = strip_slashes(js['targets']['//:dm']['include_dirs']) |
| 218 | |
| 219 | nanobench_target = js['targets']['//:nanobench'] |
| 220 | nanobench_srcs = strip_slashes(nanobench_target['sources']) |
| 221 | nanobench_includes = strip_slashes(nanobench_target['include_dirs']) |
| 222 | |
| 223 | def GrabDependentSrcs(name, srcs_to_extend, exclude): |
| 224 | # Grab the sources from other targets that $name depends on (e.g. optional |
| 225 | # Skia components, gms, tests, etc). |
| 226 | for dep in js['targets'][name]['deps']: |
| 227 | if 'third_party' in dep: |
| 228 | continue # We've handled all third-party DEPS as static or shared_libs. |
| 229 | if 'none' in dep: |
| 230 | continue # We'll handle all cpu-specific sources manually later. |
| 231 | if exclude and exclude in dep: |
| 232 | continue |
| 233 | srcs_to_extend.update(strip_slashes(js['targets'][dep].get('sources', []))) |
| 234 | GrabDependentSrcs(dep, srcs_to_extend, exclude) |
| 235 | |
| 236 | GrabDependentSrcs('//:skia', srcs, None) |
| 237 | GrabDependentSrcs('//:dm', dm_srcs, 'skia') |
| 238 | GrabDependentSrcs('//:nanobench', nanobench_srcs, 'skia') |
| 239 | |
| 240 | # No need to list headers. |
| 241 | srcs = {s for s in srcs if not s.endswith('.h')} |
| 242 | dm_srcs = {s for s in dm_srcs if not s.endswith('.h')} |
| 243 | nanobench_srcs = {s for s in nanobench_srcs if not s.endswith('.h')} |
| 244 | |
| 245 | # Most defines go into SkUserConfig.h, where they're seen by Skia and its users. |
| 246 | defines = [str(d) for d in js['targets']['//:skia']['defines']] |
| 247 | defines.remove('NDEBUG') # Let the Android build control this. |
| 248 | defines.remove('SKIA_IMPLEMENTATION=1') # Only libskia should have this define. |
| 249 | |
| 250 | # For architecture specific files, it's easier to just read the same source |
| 251 | # that GN does (opts.gni) rather than re-run GN once for each architecture. |
| 252 | |
| 253 | # This .gni file we want to read is close enough to Python syntax |
| 254 | # that we can use execfile() if we supply definitions for GN builtins. |
| 255 | |
| 256 | def get_path_info(path, kind): |
| 257 | assert kind == "abspath" |
| 258 | # While we want absolute paths in GN, relative paths work best here. |
| 259 | return path |
| 260 | |
| 261 | builtins = { 'get_path_info': get_path_info } |
| 262 | defs = {} |
| 263 | here = os.path.dirname(__file__) |
| 264 | execfile(os.path.join(here, 'opts.gni'), builtins, defs) |
| 265 | |
| 266 | # Turn paths from opts.gni into paths relative to external/skia. |
| 267 | def scrub(lst): |
| 268 | # Perform any string substitutions. |
| 269 | for var in defs: |
| 270 | if type(defs[var]) is str: |
| 271 | lst = [ p.replace('$'+var, defs[var]) for p in lst ] |
| 272 | # Relativize paths to top-level skia/ directory. |
| 273 | return [os.path.relpath(p, '..') for p in lst] |
| 274 | |
| 275 | # Turn a list of strings into the style bpfmt outputs. |
| 276 | def bpfmt(indent, lst, sort=True): |
| 277 | if sort: |
| 278 | lst = sorted(lst) |
| 279 | return ('\n' + ' '*indent).join('"%s",' % v for v in lst) |
| 280 | |
| 281 | # OK! We have everything to fill in Android.bp... |
| 282 | with open('Android.bp', 'w') as f: |
| 283 | print >>f, bp.substitute({ |
| 284 | 'export_includes': bpfmt(8, export_includes), |
| 285 | 'local_includes': bpfmt(8, local_includes), |
| 286 | 'srcs': bpfmt(8, srcs), |
| 287 | |
| 288 | 'arm_srcs': bpfmt(16, scrub(defs['armv7'])), |
| 289 | 'arm_neon_srcs': bpfmt(20, scrub(defs['neon'])), |
| 290 | 'arm64_srcs': bpfmt(16, scrub(defs['arm64'] + |
| 291 | defs['crc32'])), |
| 292 | 'none_srcs': bpfmt(16, scrub(defs['none'])), |
| 293 | 'x86_srcs': bpfmt(16, scrub(defs['sse2'] + |
| 294 | defs['ssse3'] + |
| 295 | defs['sse41'] + |
| 296 | defs['sse42'] + |
| 297 | defs['avx' ])), |
| 298 | |
| 299 | 'tool_cflags' : bpfmt(8, tool_cflags), |
| 300 | 'tool_shared_libs' : bpfmt(8, tool_shared_libs), |
| 301 | 'tool_static_libs' : bpfmt(8, tool_static_libs, False), |
| 302 | |
| 303 | 'dm_includes' : bpfmt(8, dm_includes), |
| 304 | 'dm_srcs' : bpfmt(8, dm_srcs), |
| 305 | |
| 306 | 'nanobench_includes' : bpfmt(8, nanobench_includes), |
| 307 | 'nanobench_srcs' : bpfmt(8, nanobench_srcs), |
| 308 | }) |
| 309 | |
| 310 | #... and all the #defines we want to put in SkUserConfig.h. |
| 311 | with open('include/config/SkUserConfig.h', 'w') as f: |
| 312 | print >>f, '// DO NOT MODIFY! This file is autogenerated by gn_to_bp.py.' |
| 313 | print >>f, '// If need to change a define, modify SkUserConfigManual.h' |
| 314 | print >>f, '#ifndef SkUserConfig_DEFINED' |
| 315 | print >>f, '#define SkUserConfig_DEFINED' |
| 316 | print >>f, '#include "SkUserConfigManual.h"' |
| 317 | for define in sorted(defines): |
| 318 | print >>f, ' #define', define.replace('=', ' ') |
| 319 | print >>f, '#endif//SkUserConfig_DEFINED' |