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 | |
| 17 | # First we start off with a template for Android.bp, |
| 18 | # with holes for source lists and include directories. |
| 19 | bp = string.Template('''// This file is autogenerated by gn_to_bp.py. |
| 20 | |
| 21 | cc_library { |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 22 | name: "libskia", |
| 23 | cflags: [ |
| 24 | "-fexceptions", |
| 25 | "-Wno-unused-parameter", |
| 26 | "-U_FORTIFY_SOURCE", |
| 27 | "-D_FORTIFY_SOURCE=1", |
| 28 | "-DSKIA_IMPLEMENTATION=1", |
| 29 | ], |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 30 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 31 | export_include_dirs: [ |
| 32 | $export_includes |
| 33 | ], |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 34 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 35 | local_include_dirs: [ |
| 36 | $local_includes |
| 37 | ], |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 38 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 39 | srcs: [ |
| 40 | $srcs |
| 41 | ], |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 42 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 43 | arch: { |
| 44 | arm: { |
| 45 | srcs: [ |
| 46 | $arm_srcs |
| 47 | ], |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 48 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 49 | armv7_a_neon: { |
| 50 | srcs: [ |
| 51 | $arm_neon_srcs |
| 52 | ], |
| 53 | }, |
| 54 | }, |
| 55 | |
| 56 | arm64: { |
| 57 | srcs: [ |
| 58 | $arm64_srcs |
| 59 | ], |
| 60 | }, |
| 61 | |
| 62 | mips: { |
| 63 | srcs: [ |
Mike Klein | 40a82bd | 2016-12-20 17:34:29 -0500 | [diff] [blame] | 64 | $none_srcs |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 65 | ], |
| 66 | }, |
| 67 | |
| 68 | mips64: { |
| 69 | srcs: [ |
Mike Klein | 40a82bd | 2016-12-20 17:34:29 -0500 | [diff] [blame] | 70 | $none_srcs |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 71 | ], |
| 72 | }, |
| 73 | |
| 74 | x86: { |
| 75 | srcs: [ |
| 76 | $x86_srcs |
| 77 | ], |
| 78 | }, |
| 79 | |
| 80 | x86_64: { |
| 81 | srcs: [ |
| 82 | $x86_srcs |
| 83 | ], |
| 84 | }, |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 85 | }, |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 86 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 87 | shared_libs: [ |
| 88 | "libEGL", |
| 89 | "libGLESv2", |
| 90 | "libdng_sdk", |
| 91 | "libexpat", |
| 92 | "libft2", |
| 93 | "libicui18n", |
| 94 | "libicuuc", |
| 95 | "libjpeg", |
| 96 | "liblog", |
| 97 | "libpiex", |
| 98 | "libpng", |
| 99 | "libvulkan", |
| 100 | "libz", |
| 101 | ], |
| 102 | static_libs: [ |
| 103 | "libsfntly", |
| 104 | "libwebp-decode", |
| 105 | "libwebp-encode", |
| 106 | ], |
| 107 | }''') |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 108 | |
| 109 | # We'll run GN to get the main source lists and include directories for Skia. |
| 110 | gn_args = { |
| 111 | 'skia_enable_vulkan_debug_layers': 'false', |
Mike Klein | c308333 | 2016-12-12 09:03:56 -0500 | [diff] [blame] | 112 | 'skia_use_system_expat': 'true', |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 113 | 'skia_use_vulkan': 'true', |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 114 | 'target_cpu': '"none"', |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 115 | 'target_os': '"android"', |
| 116 | } |
| 117 | gn_args = ' '.join(sorted('%s=%s' % (k,v) for (k,v) in gn_args.iteritems())) |
| 118 | |
| 119 | tmp = tempfile.mkdtemp() |
| 120 | subprocess.check_call(['gn', 'gen', tmp, '--args=%s' % gn_args, '--ide=json']) |
| 121 | |
| 122 | js = json.load(open(os.path.join(tmp, 'project.json'))) |
| 123 | |
| 124 | def strip_slashes(lst): |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 125 | return [str(p.lstrip('/')) for p in lst] |
| 126 | |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 127 | srcs = strip_slashes(js['targets']['//:skia']['sources']) |
| 128 | local_includes = strip_slashes(js['targets']['//:skia']['include_dirs']) |
| 129 | export_includes = strip_slashes(js['targets']['//:public']['include_dirs']) |
| 130 | |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 131 | # Grab the sources from targets :skia depends on (optional Skia components). |
| 132 | for dep in js['targets']['//:skia']['deps']: |
| 133 | if 'third_party' in dep: |
| 134 | continue # We've handled all third-party DEPS as static or shared_libs. |
Mike Klein | c308333 | 2016-12-12 09:03:56 -0500 | [diff] [blame] | 135 | if 'none' in dep: |
| 136 | continue # We'll handle all cpu-specific sources manually later. |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 137 | srcs.extend(strip_slashes(js['targets'][dep].get('sources', []))) |
| 138 | |
| 139 | # No need to list headers. |
| 140 | srcs = [s for s in srcs if not s.endswith('.h')] |
| 141 | |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 142 | # Most defines go into SkUserConfig.h, where they're seen by Skia and its users. |
| 143 | # Start with the defines :skia uses, minus a couple. We'll add more in a bit. |
| 144 | defines = [str(d) for d in js['targets']['//:skia']['defines']] |
| 145 | defines.remove('SKIA_IMPLEMENTATION=1') # Only libskia should have this define. |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 146 | |
| 147 | # For architecture specific files, it's easier to just read the same source |
| 148 | # that GN does (opts.gni) rather than re-run GN once for each architecture. |
| 149 | |
| 150 | # This .gni file we want to read is close enough to Python syntax |
| 151 | # that we can use execfile() if we supply definitions for GN builtins. |
| 152 | # While we're at it, grab defines specific to Android Framework the same way. |
| 153 | |
| 154 | def get_path_info(path, kind): |
| 155 | assert kind == "abspath" |
| 156 | # While we want absolute paths in GN, relative paths work best here. |
| 157 | return path |
| 158 | |
| 159 | builtins = { 'get_path_info': get_path_info } |
| 160 | defs = {} |
Leon Scroggins III | 4f8a467 | 2016-12-19 09:32:21 -0500 | [diff] [blame] | 161 | here = os.path.dirname(__file__) |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 162 | execfile(os.path.join(here, 'opts.gni'), builtins, defs) |
| 163 | execfile(os.path.join(here, 'android_framework_defines.gni'), builtins, defs) |
| 164 | |
| 165 | # This should finish off the defines. |
| 166 | defines += defs['android_framework_defines'] |
| 167 | defines.extend([ |
| 168 | 'GR_GL_CUSTOM_SETUP_HEADER "gl/GrGLConfig_chrome.h"', |
| 169 | 'SKIA_DLL', |
| 170 | 'SK_BUILD_FOR_ANDROID_FRAMEWORK', |
| 171 | 'SK_DEFAULT_FONT_CACHE_LIMIT (768 * 1024)', |
| 172 | 'SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (512 * 1024)', |
| 173 | 'SK_IGNORE_ETC1_SUPPORT', |
| 174 | 'SK_USE_FREETYPE_EMBOLDEN', |
| 175 | ]) |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 176 | # TODO: move these all to android_framework_defines.gni? |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 177 | |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 178 | # Turn paths from opts.gni into paths relative to external/skia. |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 179 | def scrub(lst): |
| 180 | # Perform any string substitutions. |
| 181 | for var in defs: |
| 182 | if type(defs[var]) is str: |
| 183 | lst = [ p.replace('$'+var, defs[var]) for p in lst ] |
| 184 | # Relativize paths to top-level skia/ directory. |
| 185 | return [os.path.relpath(p, '..') for p in lst] |
| 186 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 187 | # Turn a list of strings into the style bpfmt outputs. |
| 188 | def bpfmt(indent, lst): |
| 189 | return ('\n' + ' '*indent).join('"%s",' % v for v in sorted(lst)) |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 190 | |
| 191 | # OK! We have everything to fill in Android.bp... |
| 192 | with open('Android.bp', 'w') as f: |
| 193 | print >>f, bp.substitute({ |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 194 | 'export_includes': bpfmt(8, export_includes), |
| 195 | 'local_includes': bpfmt(8, local_includes), |
| 196 | 'srcs': bpfmt(8, srcs), |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 197 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 198 | 'arm_srcs': bpfmt(16, scrub(defs['armv7'])), |
| 199 | 'arm_neon_srcs': bpfmt(20, scrub(defs['neon'])), |
| 200 | 'arm64_srcs': bpfmt(16, scrub(defs['arm64'] + |
| 201 | defs['crc32'])), |
Mike Klein | 40a82bd | 2016-12-20 17:34:29 -0500 | [diff] [blame] | 202 | 'none_srcs': bpfmt(16, scrub(defs['none'])), |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 203 | 'x86_srcs': bpfmt(16, scrub(defs['sse2'] + |
| 204 | defs['ssse3'] + |
| 205 | defs['sse41'] + |
| 206 | defs['sse42'] + |
| 207 | defs['avx' ] + |
| 208 | defs['hsw' ])) |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 209 | }) |
| 210 | |
| 211 | #... and all the #defines we want to put in SkUserConfig.h. |
Mike Klein | c308333 | 2016-12-12 09:03:56 -0500 | [diff] [blame] | 212 | with open('include/config/SkUserConfig.h', 'w') as f: |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 213 | print >>f, '// This file is autogenerated by gn_to_bp.py.' |
| 214 | print >>f, '#ifndef SkUserConfig_DEFINED' |
| 215 | print >>f, '#define SkUserConfig_DEFINED' |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 216 | for define in sorted(defines): |
Mike Klein | c308333 | 2016-12-12 09:03:56 -0500 | [diff] [blame] | 217 | print >>f, ' #define', define.replace('=', ' ') |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 218 | print >>f, '#endif//SkUserConfig_DEFINED' |