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 | |
Derek Sollenberger | 5a93216 | 2017-09-21 14:25:14 -0400 | [diff] [blame] | 21 | cc_library_static { |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 22 | name: "libskia", |
| 23 | cflags: [ |
Leon Scroggins III | 981a31e | 2017-10-06 11:53:53 -0400 | [diff] [blame] | 24 | $cflags |
| 25 | ], |
| 26 | |
| 27 | cppflags:[ |
| 28 | $cflags_cc |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 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 | |
Leon Scroggins III | f7332d3 | 2017-08-10 09:09:54 -0400 | [diff] [blame] | 49 | neon: { |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 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 | |
Derek Sollenberger | 5a93216 | 2017-09-21 14:25:14 -0400 | [diff] [blame] | 87 | defaults: ["skia_deps"], |
| 88 | } |
| 89 | |
| 90 | cc_defaults { |
| 91 | name: "skia_deps", |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 92 | shared_libs: [ |
| 93 | "libEGL", |
| 94 | "libGLESv2", |
| 95 | "libdng_sdk", |
| 96 | "libexpat", |
| 97 | "libft2", |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 98 | "libheif", |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 99 | "libicui18n", |
| 100 | "libicuuc", |
| 101 | "libjpeg", |
| 102 | "liblog", |
| 103 | "libpiex", |
| 104 | "libpng", |
| 105 | "libvulkan", |
| 106 | "libz", |
Derek Sollenberger | 488f0d6 | 2017-03-03 15:48:33 -0500 | [diff] [blame] | 107 | "libcutils", |
Stan Iliev | 7e910df | 2017-06-02 10:29:21 -0400 | [diff] [blame] | 108 | "libnativewindow", |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 109 | ], |
| 110 | static_libs: [ |
Matt Sarett | a309109 | 2017-02-20 12:50:52 -0500 | [diff] [blame] | 111 | "libarect", |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 112 | "libsfntly", |
| 113 | "libwebp-decode", |
| 114 | "libwebp-encode", |
| 115 | ], |
Derek Sollenberger | 5a93216 | 2017-09-21 14:25:14 -0400 | [diff] [blame] | 116 | group_static_libs: true, |
| 117 | } |
| 118 | |
| 119 | cc_defaults { |
| 120 | name: "skia_tool_deps", |
| 121 | defaults: [ |
| 122 | "skia_deps" |
| 123 | ], |
| 124 | static_libs: [ |
| 125 | "libjsoncpp", |
| 126 | "libskia", |
| 127 | ], |
| 128 | cflags: [ |
| 129 | "-Wno-unused-parameter" |
| 130 | ], |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | cc_test { |
| 134 | name: "skia_dm", |
| 135 | |
Derek Sollenberger | 5a93216 | 2017-09-21 14:25:14 -0400 | [diff] [blame] | 136 | defaults: [ |
| 137 | "skia_tool_deps" |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 138 | ], |
| 139 | |
| 140 | local_include_dirs: [ |
| 141 | $dm_includes |
| 142 | ], |
| 143 | |
| 144 | srcs: [ |
| 145 | $dm_srcs |
| 146 | ], |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | cc_test { |
| 150 | name: "skia_nanobench", |
| 151 | |
Derek Sollenberger | 5a93216 | 2017-09-21 14:25:14 -0400 | [diff] [blame] | 152 | defaults: [ |
| 153 | "skia_tool_deps" |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 154 | ], |
| 155 | |
| 156 | local_include_dirs: [ |
| 157 | $nanobench_includes |
| 158 | ], |
| 159 | |
| 160 | srcs: [ |
| 161 | $nanobench_srcs |
| 162 | ], |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 163 | }''') |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 164 | |
| 165 | # We'll run GN to get the main source lists and include directories for Skia. |
| 166 | gn_args = { |
Mike Klein | e459afd | 2017-03-03 09:21:30 -0500 | [diff] [blame] | 167 | 'is_official_build': 'true', |
Mike Klein | e459afd | 2017-03-03 09:21:30 -0500 | [diff] [blame] | 168 | 'skia_enable_tools': 'true', |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 169 | 'skia_use_libheif': 'true', |
Mike Klein | e459afd | 2017-03-03 09:21:30 -0500 | [diff] [blame] | 170 | 'skia_use_vulkan': 'true', |
| 171 | 'target_cpu': '"none"', |
| 172 | 'target_os': '"android"', |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 173 | } |
| 174 | gn_args = ' '.join(sorted('%s=%s' % (k,v) for (k,v) in gn_args.iteritems())) |
| 175 | |
| 176 | tmp = tempfile.mkdtemp() |
| 177 | subprocess.check_call(['gn', 'gen', tmp, '--args=%s' % gn_args, '--ide=json']) |
| 178 | |
| 179 | js = json.load(open(os.path.join(tmp, 'project.json'))) |
| 180 | |
| 181 | def strip_slashes(lst): |
Ben Wagner | e127523 | 2017-02-23 18:00:06 -0500 | [diff] [blame] | 182 | return {str(p.lstrip('/')) for p in lst} |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 183 | |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 184 | srcs = strip_slashes(js['targets']['//:skia']['sources']) |
Leon Scroggins III | 981a31e | 2017-10-06 11:53:53 -0400 | [diff] [blame] | 185 | cflags = strip_slashes(js['targets']['//:skia']['cflags']) |
| 186 | cflags_cc = strip_slashes(js['targets']['//:skia']['cflags_cc']) |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 187 | local_includes = strip_slashes(js['targets']['//:skia']['include_dirs']) |
| 188 | export_includes = strip_slashes(js['targets']['//:public']['include_dirs']) |
| 189 | |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 190 | dm_srcs = strip_slashes(js['targets']['//:dm']['sources']) |
| 191 | dm_includes = strip_slashes(js['targets']['//:dm']['include_dirs']) |
| 192 | |
| 193 | nanobench_target = js['targets']['//:nanobench'] |
| 194 | nanobench_srcs = strip_slashes(nanobench_target['sources']) |
| 195 | nanobench_includes = strip_slashes(nanobench_target['include_dirs']) |
| 196 | |
| 197 | def GrabDependentSrcs(name, srcs_to_extend, exclude): |
| 198 | # Grab the sources from other targets that $name depends on (e.g. optional |
| 199 | # Skia components, gms, tests, etc). |
| 200 | for dep in js['targets'][name]['deps']: |
| 201 | if 'third_party' in dep: |
| 202 | continue # We've handled all third-party DEPS as static or shared_libs. |
| 203 | if 'none' in dep: |
| 204 | continue # We'll handle all cpu-specific sources manually later. |
| 205 | if exclude and exclude in dep: |
| 206 | continue |
Ben Wagner | e127523 | 2017-02-23 18:00:06 -0500 | [diff] [blame] | 207 | srcs_to_extend.update(strip_slashes(js['targets'][dep].get('sources', []))) |
| 208 | GrabDependentSrcs(dep, srcs_to_extend, exclude) |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 209 | |
| 210 | GrabDependentSrcs('//:skia', srcs, None) |
| 211 | GrabDependentSrcs('//:dm', dm_srcs, 'skia') |
| 212 | GrabDependentSrcs('//:nanobench', nanobench_srcs, 'skia') |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 213 | |
| 214 | # No need to list headers. |
Ben Wagner | e127523 | 2017-02-23 18:00:06 -0500 | [diff] [blame] | 215 | srcs = {s for s in srcs if not s.endswith('.h')} |
| 216 | dm_srcs = {s for s in dm_srcs if not s.endswith('.h')} |
| 217 | 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] | 218 | |
Leon Scroggins III | 981a31e | 2017-10-06 11:53:53 -0400 | [diff] [blame] | 219 | # Only use the generated flags related to warnings. |
| 220 | cflags = {s for s in cflags if s.startswith('-W')} |
| 221 | cflags_cc = {s for s in cflags_cc if s.startswith('-W')} |
| 222 | # Add the rest of the flags we want. |
| 223 | cflags = cflags.union([ |
| 224 | "-fvisibility=hidden", |
| 225 | "-D_FORTIFY_SOURCE=1", |
| 226 | "-DSKIA_DLL", |
| 227 | "-DSKIA_IMPLEMENTATION=1", |
| 228 | "-DATRACE_TAG=ATRACE_TAG_VIEW", |
| 229 | ]) |
| 230 | cflags_cc.add("-fexceptions") |
| 231 | |
| 232 | # We need to undefine FORTIFY_SOURCE before we define it. Insert it at the |
| 233 | # beginning after sorting. |
| 234 | cflags = sorted(cflags) |
| 235 | cflags.insert(0, "-U_FORTIFY_SOURCE") |
| 236 | |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 237 | # 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] | 238 | defines = [str(d) for d in js['targets']['//:skia']['defines']] |
Mike Klein | e459afd | 2017-03-03 09:21:30 -0500 | [diff] [blame] | 239 | defines.remove('NDEBUG') # Let the Android build control this. |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 240 | defines.remove('SKIA_IMPLEMENTATION=1') # Only libskia should have this define. |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 241 | |
| 242 | # For architecture specific files, it's easier to just read the same source |
| 243 | # that GN does (opts.gni) rather than re-run GN once for each architecture. |
| 244 | |
| 245 | # This .gni file we want to read is close enough to Python syntax |
| 246 | # that we can use execfile() if we supply definitions for GN builtins. |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 247 | |
| 248 | def get_path_info(path, kind): |
| 249 | assert kind == "abspath" |
| 250 | # While we want absolute paths in GN, relative paths work best here. |
| 251 | return path |
| 252 | |
| 253 | builtins = { 'get_path_info': get_path_info } |
| 254 | defs = {} |
Leon Scroggins III | 4f8a467 | 2016-12-19 09:32:21 -0500 | [diff] [blame] | 255 | here = os.path.dirname(__file__) |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 256 | execfile(os.path.join(here, 'opts.gni'), builtins, defs) |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 257 | |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 258 | # Turn paths from opts.gni into paths relative to external/skia. |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 259 | def scrub(lst): |
| 260 | # Perform any string substitutions. |
| 261 | for var in defs: |
| 262 | if type(defs[var]) is str: |
| 263 | lst = [ p.replace('$'+var, defs[var]) for p in lst ] |
| 264 | # Relativize paths to top-level skia/ directory. |
| 265 | return [os.path.relpath(p, '..') for p in lst] |
| 266 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 267 | # Turn a list of strings into the style bpfmt outputs. |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 268 | def bpfmt(indent, lst, sort=True): |
| 269 | if sort: |
| 270 | lst = sorted(lst) |
| 271 | return ('\n' + ' '*indent).join('"%s",' % v for v in lst) |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 272 | |
| 273 | # OK! We have everything to fill in Android.bp... |
| 274 | with open('Android.bp', 'w') as f: |
| 275 | print >>f, bp.substitute({ |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 276 | 'export_includes': bpfmt(8, export_includes), |
| 277 | 'local_includes': bpfmt(8, local_includes), |
| 278 | 'srcs': bpfmt(8, srcs), |
Leon Scroggins III | 981a31e | 2017-10-06 11:53:53 -0400 | [diff] [blame] | 279 | 'cflags': bpfmt(8, cflags, False), |
| 280 | 'cflags_cc': bpfmt(8, cflags_cc), |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 281 | |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 282 | 'arm_srcs': bpfmt(16, scrub(defs['armv7'])), |
| 283 | 'arm_neon_srcs': bpfmt(20, scrub(defs['neon'])), |
| 284 | 'arm64_srcs': bpfmt(16, scrub(defs['arm64'] + |
| 285 | defs['crc32'])), |
Mike Klein | 40a82bd | 2016-12-20 17:34:29 -0500 | [diff] [blame] | 286 | 'none_srcs': bpfmt(16, scrub(defs['none'])), |
Mike Klein | ee43f6f | 2016-12-12 14:09:38 -0500 | [diff] [blame] | 287 | 'x86_srcs': bpfmt(16, scrub(defs['sse2'] + |
| 288 | defs['ssse3'] + |
| 289 | defs['sse41'] + |
| 290 | defs['sse42'] + |
Mike Reed | e32500f | 2017-07-19 17:20:37 -0400 | [diff] [blame] | 291 | defs['avx' ])), |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 292 | |
Leon Scroggins III | 6ccd2ca | 2017-01-26 17:21:27 -0500 | [diff] [blame] | 293 | 'dm_includes' : bpfmt(8, dm_includes), |
| 294 | 'dm_srcs' : bpfmt(8, dm_srcs), |
| 295 | |
| 296 | 'nanobench_includes' : bpfmt(8, nanobench_includes), |
| 297 | 'nanobench_srcs' : bpfmt(8, nanobench_srcs), |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 298 | }) |
| 299 | |
| 300 | #... and all the #defines we want to put in SkUserConfig.h. |
Mike Klein | c308333 | 2016-12-12 09:03:56 -0500 | [diff] [blame] | 301 | with open('include/config/SkUserConfig.h', 'w') as f: |
Leon Scroggins III | 51b2f1b | 2017-07-11 15:53:41 -0400 | [diff] [blame] | 302 | print >>f, '// DO NOT MODIFY! This file is autogenerated by gn_to_bp.py.' |
| 303 | print >>f, '// If need to change a define, modify SkUserConfigManual.h' |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 304 | print >>f, '#ifndef SkUserConfig_DEFINED' |
| 305 | print >>f, '#define SkUserConfig_DEFINED' |
Leon Scroggins III | 51b2f1b | 2017-07-11 15:53:41 -0400 | [diff] [blame] | 306 | print >>f, '#include "SkUserConfigManual.h"' |
Mike Klein | 27eb22b | 2016-12-07 12:27:56 -0500 | [diff] [blame] | 307 | for define in sorted(defines): |
Mike Klein | c308333 | 2016-12-12 09:03:56 -0500 | [diff] [blame] | 308 | print >>f, ' #define', define.replace('=', ' ') |
Mike Klein | 308b5ac | 2016-12-06 16:03:52 -0500 | [diff] [blame] | 309 | print >>f, '#endif//SkUserConfig_DEFINED' |