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', |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 37 | 'libnativewindow', |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 38 | ] |
| 39 | |
| 40 | # The ordering here is important: libsfntly needs to come after libskia. |
| 41 | tool_static_libs = [ |
Matt Sarett | af7bbc8 | 2017-02-20 10:41:39 -0500 | [diff] [blame] | 42 | 'libarect', |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 43 | 'libjsoncpp', |
| 44 | 'libskia', |
| 45 | 'libsfntly', |
| 46 | 'libwebp-decode', |
| 47 | 'libwebp-encode', |
| 48 | ] |
| 49 | |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 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 { |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 55 | name: "libskia", |
| 56 | cflags: [ |
| 57 | "-fexceptions", |
| 58 | "-Wno-unused-parameter", |
| 59 | "-U_FORTIFY_SOURCE", |
| 60 | "-D_FORTIFY_SOURCE=1", |
| 61 | "-DSKIA_IMPLEMENTATION=1", |
Derek Sollenberger | 488f0d6 | 2017-03-03 15:48:33 -0500 | [diff] [blame] | 62 | "-DATRACE_TAG=ATRACE_TAG_VIEW", |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 63 | ], |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 64 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 65 | export_include_dirs: [ |
| 66 | $export_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 | local_include_dirs: [ |
| 70 | $local_includes |
| 71 | ], |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 72 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 73 | srcs: [ |
| 74 | $srcs |
| 75 | ], |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 76 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 77 | arch: { |
| 78 | arm: { |
| 79 | srcs: [ |
| 80 | $arm_srcs |
| 81 | ], |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 82 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 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: [ |
Mike Klein | 40a82bd | 2016-12-20 17:34:29 -0500 | [diff] [blame] | 98 | $none_srcs |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 99 | ], |
| 100 | }, |
| 101 | |
| 102 | mips64: { |
| 103 | srcs: [ |
Mike Klein | 40a82bd | 2016-12-20 17:34:29 -0500 | [diff] [blame] | 104 | $none_srcs |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 105 | ], |
| 106 | }, |
| 107 | |
| 108 | x86: { |
| 109 | srcs: [ |
| 110 | $x86_srcs |
| 111 | ], |
| 112 | }, |
| 113 | |
| 114 | x86_64: { |
| 115 | srcs: [ |
| 116 | $x86_srcs |
| 117 | ], |
| 118 | }, |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 119 | }, |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 120 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 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", |
Derek Sollenberger | 488f0d6 | 2017-03-03 15:48:33 -0500 | [diff] [blame] | 135 | "libcutils", |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 136 | "libnativewindow", |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 137 | ], |
| 138 | static_libs: [ |
Matt Sarett | a309109 | 2017-02-20 12:50:52 -0500 | [diff] [blame] | 139 | "libarect", |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 140 | "libsfntly", |
| 141 | "libwebp-decode", |
| 142 | "libwebp-encode", |
| 143 | ], |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 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 | ], |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 192 | }''') |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 193 | |
| 194 | # We'll run GN to get the main source lists and include directories for Skia. |
| 195 | gn_args = { |
Mike Klein | e459afd | 2017-03-03 09:21:30 -0500 | [diff] [blame] | 196 | 'is_official_build': 'true', |
Mike Klein | e459afd | 2017-03-03 09:21:30 -0500 | [diff] [blame] | 197 | 'skia_enable_tools': 'true', |
| 198 | 'skia_use_vulkan': 'true', |
| 199 | 'target_cpu': '"none"', |
| 200 | 'target_os': '"android"', |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 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): |
Ben Wagner | e127523 | 2017-02-23 18:00:06 -0500 | [diff] [blame] | 210 | return {str(p.lstrip('/')) for p in lst} |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 211 | |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 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 | |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 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 |
Ben Wagner | e127523 | 2017-02-23 18:00:06 -0500 | [diff] [blame] | 233 | srcs_to_extend.update(strip_slashes(js['targets'][dep].get('sources', []))) |
| 234 | GrabDependentSrcs(dep, srcs_to_extend, exclude) |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 235 | |
| 236 | GrabDependentSrcs('//:skia', srcs, None) |
| 237 | GrabDependentSrcs('//:dm', dm_srcs, 'skia') |
| 238 | GrabDependentSrcs('//:nanobench', nanobench_srcs, 'skia') |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 239 | |
| 240 | # No need to list headers. |
Ben Wagner | e127523 | 2017-02-23 18:00:06 -0500 | [diff] [blame] | 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')} |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 244 | |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 245 | # Most defines go into SkUserConfig.h, where they're seen by Skia and its users. |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 246 | defines = [str(d) for d in js['targets']['//:skia']['defines']] |
Mike Klein | e459afd | 2017-03-03 09:21:30 -0500 | [diff] [blame] | 247 | defines.remove('NDEBUG') # Let the Android build control this. |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 248 | defines.remove('SKIA_IMPLEMENTATION=1') # Only libskia should have this define. |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 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. |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 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) |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 265 | |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 266 | # Turn paths from opts.gni into paths relative to external/skia. |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 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 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 275 | # Turn a list of strings into the style bpfmt outputs. |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 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) |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 280 | |
| 281 | # OK! We have everything to fill in Android.bp... |
| 282 | with open('Android.bp', 'w') as f: |
| 283 | print >>f, bp.substitute({ |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 284 | 'export_includes': bpfmt(8, export_includes), |
| 285 | 'local_includes': bpfmt(8, local_includes), |
| 286 | 'srcs': bpfmt(8, srcs), |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 287 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 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'])), |
Mike Klein | 40a82bd | 2016-12-20 17:34:29 -0500 | [diff] [blame] | 292 | 'none_srcs': bpfmt(16, scrub(defs['none'])), |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 293 | 'x86_srcs': bpfmt(16, scrub(defs['sse2'] + |
| 294 | defs['ssse3'] + |
| 295 | defs['sse41'] + |
| 296 | defs['sse42'] + |
Mike Reed | e32500f | 2017-07-19 17:20:37 -0400 | [diff] [blame] | 297 | defs['avx' ])), |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 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), |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 308 | }) |
| 309 | |
| 310 | #... and all the #defines we want to put in SkUserConfig.h. |
Mike Klein | c308333 | 2016-12-12 09:03:56 -0500 | [diff] [blame] | 311 | with open('include/config/SkUserConfig.h', 'w') as f: |
Leon Scroggins III | 51b2f1b | 2017-07-11 15:53:41 -0400 | [diff] [blame] | 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' |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 314 | print >>f, '#ifndef SkUserConfig_DEFINED' |
| 315 | print >>f, '#define SkUserConfig_DEFINED' |
Leon Scroggins III | 51b2f1b | 2017-07-11 15:53:41 -0400 | [diff] [blame] | 316 | print >>f, '#include "SkUserConfigManual.h"' |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 317 | for define in sorted(defines): |
Mike Klein | c308333 | 2016-12-12 09:03:56 -0500 | [diff] [blame] | 318 | print >>f, ' #define', define.replace('=', ' ') |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 319 | print >>f, '#endif//SkUserConfig_DEFINED' |