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