blob: c8c7079a89b76ea2c747cb32129e78a710db626d [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',
Derek Sollenberger488f0d62017-03-03 15:48:33 -050036 'libcutils',
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -050037]
38
39# The ordering here is important: libsfntly needs to come after libskia.
40tool_static_libs = [
Matt Sarettaf7bbc82017-02-20 10:41:39 -050041 'libarect',
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -050042 'libjsoncpp',
43 'libskia',
44 'libsfntly',
45 'libwebp-decode',
46 'libwebp-encode',
47]
48
Mike Klein308b5ac2016-12-06 16:03:52 -050049# First we start off with a template for Android.bp,
50# with holes for source lists and include directories.
51bp = string.Template('''// This file is autogenerated by gn_to_bp.py.
52
53cc_library {
Mike Kleinee43f6f2016-12-12 14:09:38 -050054 name: "libskia",
55 cflags: [
56 "-fexceptions",
57 "-Wno-unused-parameter",
58 "-U_FORTIFY_SOURCE",
59 "-D_FORTIFY_SOURCE=1",
60 "-DSKIA_IMPLEMENTATION=1",
Derek Sollenberger488f0d62017-03-03 15:48:33 -050061 "-DATRACE_TAG=ATRACE_TAG_VIEW",
Mike Kleinee43f6f2016-12-12 14:09:38 -050062 ],
Mike Klein308b5ac2016-12-06 16:03:52 -050063
Mike Kleinee43f6f2016-12-12 14:09:38 -050064 export_include_dirs: [
65 $export_includes
66 ],
Mike Klein27eb22b2016-12-07 12:27:56 -050067
Mike Kleinee43f6f2016-12-12 14:09:38 -050068 local_include_dirs: [
69 $local_includes
70 ],
Mike Klein27eb22b2016-12-07 12:27:56 -050071
Mike Kleinee43f6f2016-12-12 14:09:38 -050072 srcs: [
73 $srcs
74 ],
Mike Klein308b5ac2016-12-06 16:03:52 -050075
Mike Kleinee43f6f2016-12-12 14:09:38 -050076 arch: {
77 arm: {
78 srcs: [
79 $arm_srcs
80 ],
Mike Klein27eb22b2016-12-07 12:27:56 -050081
Mike Kleinee43f6f2016-12-12 14:09:38 -050082 armv7_a_neon: {
83 srcs: [
84 $arm_neon_srcs
85 ],
86 },
87 },
88
89 arm64: {
90 srcs: [
91 $arm64_srcs
92 ],
93 },
94
95 mips: {
96 srcs: [
Mike Klein40a82bd2016-12-20 17:34:29 -050097 $none_srcs
Mike Kleinee43f6f2016-12-12 14:09:38 -050098 ],
99 },
100
101 mips64: {
102 srcs: [
Mike Klein40a82bd2016-12-20 17:34:29 -0500103 $none_srcs
Mike Kleinee43f6f2016-12-12 14:09:38 -0500104 ],
105 },
106
107 x86: {
108 srcs: [
109 $x86_srcs
110 ],
111 },
112
113 x86_64: {
114 srcs: [
115 $x86_srcs
116 ],
117 },
Mike Klein308b5ac2016-12-06 16:03:52 -0500118 },
Mike Klein27eb22b2016-12-07 12:27:56 -0500119
Mike Kleinee43f6f2016-12-12 14:09:38 -0500120 shared_libs: [
121 "libEGL",
122 "libGLESv2",
123 "libdng_sdk",
124 "libexpat",
125 "libft2",
126 "libicui18n",
127 "libicuuc",
128 "libjpeg",
129 "liblog",
130 "libpiex",
131 "libpng",
132 "libvulkan",
133 "libz",
Derek Sollenberger488f0d62017-03-03 15:48:33 -0500134 "libcutils",
Mike Kleinee43f6f2016-12-12 14:09:38 -0500135 ],
136 static_libs: [
Matt Saretta3091092017-02-20 12:50:52 -0500137 "libarect",
Mike Kleinee43f6f2016-12-12 14:09:38 -0500138 "libsfntly",
139 "libwebp-decode",
140 "libwebp-encode",
141 ],
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500142}
143
144cc_test {
145 name: "skia_dm",
146
147 cflags: [
148 $tool_cflags
149 ],
150
151 local_include_dirs: [
152 $dm_includes
153 ],
154
155 srcs: [
156 $dm_srcs
157 ],
158
159 shared_libs: [
160 $tool_shared_libs
161 ],
162
163 static_libs: [
164 $tool_static_libs
165 ],
166}
167
168cc_test {
169 name: "skia_nanobench",
170
171 cflags: [
172 $tool_cflags
173 ],
174
175 local_include_dirs: [
176 $nanobench_includes
177 ],
178
179 srcs: [
180 $nanobench_srcs
181 ],
182
183 shared_libs: [
184 $tool_shared_libs
185 ],
186
187 static_libs: [
188 $tool_static_libs
189 ],
Mike Kleinee43f6f2016-12-12 14:09:38 -0500190}''')
Mike Klein308b5ac2016-12-06 16:03:52 -0500191
192# We'll run GN to get the main source lists and include directories for Skia.
193gn_args = {
Mike Kleine459afd2017-03-03 09:21:30 -0500194 'is_official_build': 'true',
Mike Kleine459afd2017-03-03 09:21:30 -0500195 'skia_enable_tools': 'true',
196 'skia_use_vulkan': 'true',
197 'target_cpu': '"none"',
198 'target_os': '"android"',
Mike Klein308b5ac2016-12-06 16:03:52 -0500199}
200gn_args = ' '.join(sorted('%s=%s' % (k,v) for (k,v) in gn_args.iteritems()))
201
202tmp = tempfile.mkdtemp()
203subprocess.check_call(['gn', 'gen', tmp, '--args=%s' % gn_args, '--ide=json'])
204
205js = json.load(open(os.path.join(tmp, 'project.json')))
206
207def strip_slashes(lst):
Ben Wagnere1275232017-02-23 18:00:06 -0500208 return {str(p.lstrip('/')) for p in lst}
Mike Klein27eb22b2016-12-07 12:27:56 -0500209
Mike Klein308b5ac2016-12-06 16:03:52 -0500210srcs = strip_slashes(js['targets']['//:skia']['sources'])
211local_includes = strip_slashes(js['targets']['//:skia']['include_dirs'])
212export_includes = strip_slashes(js['targets']['//:public']['include_dirs'])
213
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500214dm_srcs = strip_slashes(js['targets']['//:dm']['sources'])
215dm_includes = strip_slashes(js['targets']['//:dm']['include_dirs'])
216
217nanobench_target = js['targets']['//:nanobench']
218nanobench_srcs = strip_slashes(nanobench_target['sources'])
219nanobench_includes = strip_slashes(nanobench_target['include_dirs'])
220
221def GrabDependentSrcs(name, srcs_to_extend, exclude):
222 # Grab the sources from other targets that $name depends on (e.g. optional
223 # Skia components, gms, tests, etc).
224 for dep in js['targets'][name]['deps']:
225 if 'third_party' in dep:
226 continue # We've handled all third-party DEPS as static or shared_libs.
227 if 'none' in dep:
228 continue # We'll handle all cpu-specific sources manually later.
229 if exclude and exclude in dep:
230 continue
Ben Wagnere1275232017-02-23 18:00:06 -0500231 srcs_to_extend.update(strip_slashes(js['targets'][dep].get('sources', [])))
232 GrabDependentSrcs(dep, srcs_to_extend, exclude)
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500233
234GrabDependentSrcs('//:skia', srcs, None)
235GrabDependentSrcs('//:dm', dm_srcs, 'skia')
236GrabDependentSrcs('//:nanobench', nanobench_srcs, 'skia')
Mike Klein27eb22b2016-12-07 12:27:56 -0500237
238# No need to list headers.
Ben Wagnere1275232017-02-23 18:00:06 -0500239srcs = {s for s in srcs if not s.endswith('.h')}
240dm_srcs = {s for s in dm_srcs if not s.endswith('.h')}
241nanobench_srcs = {s for s in nanobench_srcs if not s.endswith('.h')}
Mike Klein27eb22b2016-12-07 12:27:56 -0500242
Mike Klein308b5ac2016-12-06 16:03:52 -0500243# Most defines go into SkUserConfig.h, where they're seen by Skia and its users.
244# Start with the defines :skia uses, minus a couple. We'll add more in a bit.
245defines = [str(d) for d in js['targets']['//:skia']['defines']]
Mike Kleine459afd2017-03-03 09:21:30 -0500246defines.remove('NDEBUG') # Let the Android build control this.
Mike Klein308b5ac2016-12-06 16:03:52 -0500247defines.remove('SKIA_IMPLEMENTATION=1') # Only libskia should have this define.
Mike Klein308b5ac2016-12-06 16:03:52 -0500248
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.
254# While we're at it, grab defines specific to Android Framework the same way.
255
256def 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
261builtins = { 'get_path_info': get_path_info }
262defs = {}
Leon Scroggins III4f8a4672016-12-19 09:32:21 -0500263here = os.path.dirname(__file__)
Mike Klein308b5ac2016-12-06 16:03:52 -0500264execfile(os.path.join(here, 'opts.gni'), builtins, defs)
265execfile(os.path.join(here, 'android_framework_defines.gni'), builtins, defs)
266
267# This should finish off the defines.
268defines += defs['android_framework_defines']
269defines.extend([
270 'GR_GL_CUSTOM_SETUP_HEADER "gl/GrGLConfig_chrome.h"',
271 'SKIA_DLL',
272 'SK_BUILD_FOR_ANDROID_FRAMEWORK',
273 'SK_DEFAULT_FONT_CACHE_LIMIT (768 * 1024)',
274 'SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE (512 * 1024)',
275 'SK_IGNORE_ETC1_SUPPORT',
276 'SK_USE_FREETYPE_EMBOLDEN',
277])
Mike Klein27eb22b2016-12-07 12:27:56 -0500278# TODO: move these all to android_framework_defines.gni?
Mike Klein308b5ac2016-12-06 16:03:52 -0500279
Mike Klein27eb22b2016-12-07 12:27:56 -0500280# Turn paths from opts.gni into paths relative to external/skia.
Mike Klein308b5ac2016-12-06 16:03:52 -0500281def scrub(lst):
282 # Perform any string substitutions.
283 for var in defs:
284 if type(defs[var]) is str:
285 lst = [ p.replace('$'+var, defs[var]) for p in lst ]
286 # Relativize paths to top-level skia/ directory.
287 return [os.path.relpath(p, '..') for p in lst]
288
Mike Kleinee43f6f2016-12-12 14:09:38 -0500289# Turn a list of strings into the style bpfmt outputs.
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500290def bpfmt(indent, lst, sort=True):
291 if sort:
292 lst = sorted(lst)
293 return ('\n' + ' '*indent).join('"%s",' % v for v in lst)
Mike Klein308b5ac2016-12-06 16:03:52 -0500294
295# OK! We have everything to fill in Android.bp...
296with open('Android.bp', 'w') as f:
297 print >>f, bp.substitute({
Mike Kleinee43f6f2016-12-12 14:09:38 -0500298 'export_includes': bpfmt(8, export_includes),
299 'local_includes': bpfmt(8, local_includes),
300 'srcs': bpfmt(8, srcs),
Mike Klein308b5ac2016-12-06 16:03:52 -0500301
Mike Kleinee43f6f2016-12-12 14:09:38 -0500302 'arm_srcs': bpfmt(16, scrub(defs['armv7'])),
303 'arm_neon_srcs': bpfmt(20, scrub(defs['neon'])),
304 'arm64_srcs': bpfmt(16, scrub(defs['arm64'] +
305 defs['crc32'])),
Mike Klein40a82bd2016-12-20 17:34:29 -0500306 'none_srcs': bpfmt(16, scrub(defs['none'])),
Mike Kleinee43f6f2016-12-12 14:09:38 -0500307 'x86_srcs': bpfmt(16, scrub(defs['sse2'] +
308 defs['ssse3'] +
309 defs['sse41'] +
310 defs['sse42'] +
311 defs['avx' ] +
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500312 defs['hsw' ])),
313
314 'tool_cflags' : bpfmt(8, tool_cflags),
315 'tool_shared_libs' : bpfmt(8, tool_shared_libs),
316 'tool_static_libs' : bpfmt(8, tool_static_libs, False),
317
318 'dm_includes' : bpfmt(8, dm_includes),
319 'dm_srcs' : bpfmt(8, dm_srcs),
320
321 'nanobench_includes' : bpfmt(8, nanobench_includes),
322 'nanobench_srcs' : bpfmt(8, nanobench_srcs),
Mike Klein308b5ac2016-12-06 16:03:52 -0500323 })
324
325#... and all the #defines we want to put in SkUserConfig.h.
Mike Kleinc3083332016-12-12 09:03:56 -0500326with open('include/config/SkUserConfig.h', 'w') as f:
Mike Klein308b5ac2016-12-06 16:03:52 -0500327 print >>f, '// This file is autogenerated by gn_to_bp.py.'
328 print >>f, '#ifndef SkUserConfig_DEFINED'
329 print >>f, '#define SkUserConfig_DEFINED'
Mike Klein27eb22b2016-12-07 12:27:56 -0500330 for define in sorted(defines):
Mike Kleinc3083332016-12-12 09:03:56 -0500331 print >>f, ' #define', define.replace('=', ' ')
Mike Klein308b5ac2016-12-06 16:03:52 -0500332 print >>f, '#endif//SkUserConfig_DEFINED'