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