blob: e0605c9d333ebb0d54a57c9a810be6cce17d793b [file] [log] [blame]
Mike Klein308b5ac2016-12-06 16:03:52 -05001#!/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
10import json
11import os
12import pprint
13import string
14import subprocess
Mike Klein308b5ac2016-12-06 16:03:52 -050015import tempfile
16
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -050017tool_cflags = [
18 '-Wno-unused-parameter',
19]
20
21# It's easier to maintain one list instead of separate lists.
22tool_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',
36]
37
38# The ordering here is important: libsfntly needs to come after libskia.
39tool_static_libs = [
40 'libjsoncpp',
41 'libskia',
42 'libsfntly',
43 'libwebp-decode',
44 'libwebp-encode',
45]
46
Mike Klein308b5ac2016-12-06 16:03:52 -050047# First we start off with a template for Android.bp,
48# with holes for source lists and include directories.
49bp = string.Template('''// This file is autogenerated by gn_to_bp.py.
50
51cc_library {
Mike Kleinee43f6f2016-12-12 14:09:38 -050052 name: "libskia",
53 cflags: [
54 "-fexceptions",
55 "-Wno-unused-parameter",
56 "-U_FORTIFY_SOURCE",
57 "-D_FORTIFY_SOURCE=1",
58 "-DSKIA_IMPLEMENTATION=1",
59 ],
Mike Klein308b5ac2016-12-06 16:03:52 -050060
Mike Kleinee43f6f2016-12-12 14:09:38 -050061 export_include_dirs: [
62 $export_includes
63 ],
Mike Klein27eb22b2016-12-07 12:27:56 -050064
Mike Kleinee43f6f2016-12-12 14:09:38 -050065 local_include_dirs: [
66 $local_includes
67 ],
Mike Klein27eb22b2016-12-07 12:27:56 -050068
Mike Kleinee43f6f2016-12-12 14:09:38 -050069 srcs: [
70 $srcs
71 ],
Mike Klein308b5ac2016-12-06 16:03:52 -050072
Mike Kleinee43f6f2016-12-12 14:09:38 -050073 arch: {
74 arm: {
75 srcs: [
76 $arm_srcs
77 ],
Mike Klein27eb22b2016-12-07 12:27:56 -050078
Mike Kleinee43f6f2016-12-12 14:09:38 -050079 armv7_a_neon: {
80 srcs: [
81 $arm_neon_srcs
82 ],
83 },
84 },
85
86 arm64: {
87 srcs: [
88 $arm64_srcs
89 ],
90 },
91
92 mips: {
93 srcs: [
Mike Klein40a82bd2016-12-20 17:34:29 -050094 $none_srcs
Mike Kleinee43f6f2016-12-12 14:09:38 -050095 ],
96 },
97
98 mips64: {
99 srcs: [
Mike Klein40a82bd2016-12-20 17:34:29 -0500100 $none_srcs
Mike Kleinee43f6f2016-12-12 14:09:38 -0500101 ],
102 },
103
104 x86: {
105 srcs: [
106 $x86_srcs
107 ],
108 },
109
110 x86_64: {
111 srcs: [
112 $x86_srcs
113 ],
114 },
Mike Klein308b5ac2016-12-06 16:03:52 -0500115 },
Mike Klein27eb22b2016-12-07 12:27:56 -0500116
Mike Kleinee43f6f2016-12-12 14:09:38 -0500117 shared_libs: [
118 "libEGL",
119 "libGLESv2",
120 "libdng_sdk",
121 "libexpat",
122 "libft2",
123 "libicui18n",
124 "libicuuc",
125 "libjpeg",
126 "liblog",
127 "libpiex",
128 "libpng",
129 "libvulkan",
130 "libz",
131 ],
132 static_libs: [
133 "libsfntly",
134 "libwebp-decode",
135 "libwebp-encode",
136 ],
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500137}
138
139cc_test {
140 name: "skia_dm",
141
142 cflags: [
143 $tool_cflags
144 ],
145
146 local_include_dirs: [
147 $dm_includes
148 ],
149
150 srcs: [
151 $dm_srcs
152 ],
153
154 shared_libs: [
155 $tool_shared_libs
156 ],
157
158 static_libs: [
159 $tool_static_libs
160 ],
161}
162
163cc_test {
164 name: "skia_nanobench",
165
166 cflags: [
167 $tool_cflags
168 ],
169
170 local_include_dirs: [
171 $nanobench_includes
172 ],
173
174 srcs: [
175 $nanobench_srcs
176 ],
177
178 shared_libs: [
179 $tool_shared_libs
180 ],
181
182 static_libs: [
183 $tool_static_libs
184 ],
Mike Kleinee43f6f2016-12-12 14:09:38 -0500185}''')
Mike Klein308b5ac2016-12-06 16:03:52 -0500186
187# We'll run GN to get the main source lists and include directories for Skia.
188gn_args = {
Mike Klein1c471872017-01-13 15:27:45 -0500189 'skia_enable_splicer': 'false',
Mike Klein308b5ac2016-12-06 16:03:52 -0500190 'skia_enable_vulkan_debug_layers': 'false',
Mike Kleinc3083332016-12-12 09:03:56 -0500191 'skia_use_system_expat': 'true',
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500192 'skia_use_system_jsoncpp': 'true',
193 'skia_use_system_libpng': 'true',
194 'skia_use_system_zlib': 'true',
Mike Klein308b5ac2016-12-06 16:03:52 -0500195 'skia_use_vulkan': 'true',
Mike Klein27eb22b2016-12-07 12:27:56 -0500196 'target_cpu': '"none"',
Mike Klein308b5ac2016-12-06 16:03:52 -0500197 'target_os': '"android"',
198}
199gn_args = ' '.join(sorted('%s=%s' % (k,v) for (k,v) in gn_args.iteritems()))
200
201tmp = tempfile.mkdtemp()
202subprocess.check_call(['gn', 'gen', tmp, '--args=%s' % gn_args, '--ide=json'])
203
204js = json.load(open(os.path.join(tmp, 'project.json')))
205
206def strip_slashes(lst):
Mike Klein27eb22b2016-12-07 12:27:56 -0500207 return [str(p.lstrip('/')) for p in lst]
208
Mike Klein308b5ac2016-12-06 16:03:52 -0500209srcs = strip_slashes(js['targets']['//:skia']['sources'])
210local_includes = strip_slashes(js['targets']['//:skia']['include_dirs'])
211export_includes = strip_slashes(js['targets']['//:public']['include_dirs'])
212
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500213dm_srcs = strip_slashes(js['targets']['//:dm']['sources'])
214dm_includes = strip_slashes(js['targets']['//:dm']['include_dirs'])
215
216nanobench_target = js['targets']['//:nanobench']
217nanobench_srcs = strip_slashes(nanobench_target['sources'])
218nanobench_includes = strip_slashes(nanobench_target['include_dirs'])
219
220def GrabDependentSrcs(name, srcs_to_extend, exclude):
221 # Grab the sources from other targets that $name depends on (e.g. optional
222 # Skia components, gms, tests, etc).
223 for dep in js['targets'][name]['deps']:
224 if 'third_party' in dep:
225 continue # We've handled all third-party DEPS as static or shared_libs.
226 if 'none' in dep:
227 continue # We'll handle all cpu-specific sources manually later.
228 if exclude and exclude in dep:
229 continue
230 srcs_to_extend.extend(strip_slashes(js['targets'][dep].get('sources', [])))
231
232GrabDependentSrcs('//:skia', srcs, None)
233GrabDependentSrcs('//:dm', dm_srcs, 'skia')
234GrabDependentSrcs('//:nanobench', nanobench_srcs, 'skia')
Mike Klein27eb22b2016-12-07 12:27:56 -0500235
236# No need to list headers.
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500237srcs = [s for s in srcs if not s.endswith('.h')]
238dm_srcs = [s for s in dm_srcs if not s.endswith('.h')]
239nanobench_srcs = [s for s in nanobench_srcs if not s.endswith('.h')]
Mike Klein27eb22b2016-12-07 12:27:56 -0500240
Mike Klein308b5ac2016-12-06 16:03:52 -0500241# Most defines go into SkUserConfig.h, where they're seen by Skia and its users.
242# Start with the defines :skia uses, minus a couple. We'll add more in a bit.
243defines = [str(d) for d in js['targets']['//:skia']['defines']]
244defines.remove('SKIA_IMPLEMENTATION=1') # Only libskia should have this define.
Mike Klein308b5ac2016-12-06 16:03:52 -0500245
246# For architecture specific files, it's easier to just read the same source
247# that GN does (opts.gni) rather than re-run GN once for each architecture.
248
249# This .gni file we want to read is close enough to Python syntax
250# that we can use execfile() if we supply definitions for GN builtins.
251# While we're at it, grab defines specific to Android Framework the same way.
252
253def get_path_info(path, kind):
254 assert kind == "abspath"
255 # While we want absolute paths in GN, relative paths work best here.
256 return path
257
258builtins = { 'get_path_info': get_path_info }
259defs = {}
Leon Scroggins III4f8a4672016-12-19 09:32:21 -0500260here = os.path.dirname(__file__)
Mike Klein308b5ac2016-12-06 16:03:52 -0500261execfile(os.path.join(here, 'opts.gni'), builtins, defs)
262execfile(os.path.join(here, 'android_framework_defines.gni'), builtins, defs)
263
264# This should finish off the defines.
265defines += defs['android_framework_defines']
266defines.extend([
267 'GR_GL_CUSTOM_SETUP_HEADER "gl/GrGLConfig_chrome.h"',
268 'SKIA_DLL',
269 'SK_BUILD_FOR_ANDROID_FRAMEWORK',
270 'SK_DEFAULT_FONT_CACHE_LIMIT (768 * 1024)',
271 'SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (512 * 1024)',
272 'SK_IGNORE_ETC1_SUPPORT',
273 'SK_USE_FREETYPE_EMBOLDEN',
274])
Mike Klein27eb22b2016-12-07 12:27:56 -0500275# TODO: move these all to android_framework_defines.gni?
Mike Klein308b5ac2016-12-06 16:03:52 -0500276
Mike Klein27eb22b2016-12-07 12:27:56 -0500277# Turn paths from opts.gni into paths relative to external/skia.
Mike Klein308b5ac2016-12-06 16:03:52 -0500278def scrub(lst):
279 # Perform any string substitutions.
280 for var in defs:
281 if type(defs[var]) is str:
282 lst = [ p.replace('$'+var, defs[var]) for p in lst ]
283 # Relativize paths to top-level skia/ directory.
284 return [os.path.relpath(p, '..') for p in lst]
285
Mike Kleinee43f6f2016-12-12 14:09:38 -0500286# Turn a list of strings into the style bpfmt outputs.
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500287def bpfmt(indent, lst, sort=True):
288 if sort:
289 lst = sorted(lst)
290 return ('\n' + ' '*indent).join('"%s",' % v for v in lst)
Mike Klein308b5ac2016-12-06 16:03:52 -0500291
292# OK! We have everything to fill in Android.bp...
293with open('Android.bp', 'w') as f:
294 print >>f, bp.substitute({
Mike Kleinee43f6f2016-12-12 14:09:38 -0500295 'export_includes': bpfmt(8, export_includes),
296 'local_includes': bpfmt(8, local_includes),
297 'srcs': bpfmt(8, srcs),
Mike Klein308b5ac2016-12-06 16:03:52 -0500298
Mike Kleinee43f6f2016-12-12 14:09:38 -0500299 'arm_srcs': bpfmt(16, scrub(defs['armv7'])),
300 'arm_neon_srcs': bpfmt(20, scrub(defs['neon'])),
301 'arm64_srcs': bpfmt(16, scrub(defs['arm64'] +
302 defs['crc32'])),
Mike Klein40a82bd2016-12-20 17:34:29 -0500303 'none_srcs': bpfmt(16, scrub(defs['none'])),
Mike Kleinee43f6f2016-12-12 14:09:38 -0500304 'x86_srcs': bpfmt(16, scrub(defs['sse2'] +
305 defs['ssse3'] +
306 defs['sse41'] +
307 defs['sse42'] +
308 defs['avx' ] +
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500309 defs['hsw' ])),
310
311 'tool_cflags' : bpfmt(8, tool_cflags),
312 'tool_shared_libs' : bpfmt(8, tool_shared_libs),
313 'tool_static_libs' : bpfmt(8, tool_static_libs, False),
314
315 'dm_includes' : bpfmt(8, dm_includes),
316 'dm_srcs' : bpfmt(8, dm_srcs),
317
318 'nanobench_includes' : bpfmt(8, nanobench_includes),
319 'nanobench_srcs' : bpfmt(8, nanobench_srcs),
Mike Klein308b5ac2016-12-06 16:03:52 -0500320 })
321
322#... and all the #defines we want to put in SkUserConfig.h.
Mike Kleinc3083332016-12-12 09:03:56 -0500323with open('include/config/SkUserConfig.h', 'w') as f:
Mike Klein308b5ac2016-12-06 16:03:52 -0500324 print >>f, '// This file is autogenerated by gn_to_bp.py.'
325 print >>f, '#ifndef SkUserConfig_DEFINED'
326 print >>f, '#define SkUserConfig_DEFINED'
Mike Klein27eb22b2016-12-07 12:27:56 -0500327 for define in sorted(defines):
Mike Kleinc3083332016-12-12 09:03:56 -0500328 print >>f, ' #define', define.replace('=', ' ')
Mike Klein308b5ac2016-12-06 16:03:52 -0500329 print >>f, '#endif//SkUserConfig_DEFINED'