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