blob: 239fb020fd1efc8022aaade74ddecb60adc2422d [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
17# First we start off with a template for Android.bp,
18# with holes for source lists and include directories.
19bp = string.Template('''// This file is autogenerated by gn_to_bp.py.
20
Derek Sollenberger5a932162017-09-21 14:25:14 -040021cc_library_static {
Mike Kleinee43f6f2016-12-12 14:09:38 -050022 name: "libskia",
23 cflags: [
Leon Scroggins III981a31e2017-10-06 11:53:53 -040024 $cflags
25 ],
26
27 cppflags:[
28 $cflags_cc
Mike Kleinee43f6f2016-12-12 14:09:38 -050029 ],
Mike Klein308b5ac2016-12-06 16:03:52 -050030
Mike Kleinee43f6f2016-12-12 14:09:38 -050031 export_include_dirs: [
32 $export_includes
33 ],
Mike Klein27eb22b2016-12-07 12:27:56 -050034
Mike Kleinee43f6f2016-12-12 14:09:38 -050035 local_include_dirs: [
36 $local_includes
37 ],
Mike Klein27eb22b2016-12-07 12:27:56 -050038
Mike Kleinee43f6f2016-12-12 14:09:38 -050039 srcs: [
40 $srcs
41 ],
Mike Klein308b5ac2016-12-06 16:03:52 -050042
Mike Kleinee43f6f2016-12-12 14:09:38 -050043 arch: {
44 arm: {
45 srcs: [
46 $arm_srcs
47 ],
Mike Klein27eb22b2016-12-07 12:27:56 -050048
Leon Scroggins IIIf7332d32017-08-10 09:09:54 -040049 neon: {
Mike Kleinee43f6f2016-12-12 14:09:38 -050050 srcs: [
51 $arm_neon_srcs
52 ],
53 },
54 },
55
56 arm64: {
57 srcs: [
58 $arm64_srcs
59 ],
60 },
61
62 mips: {
63 srcs: [
Mike Klein40a82bd2016-12-20 17:34:29 -050064 $none_srcs
Mike Kleinee43f6f2016-12-12 14:09:38 -050065 ],
66 },
67
68 mips64: {
69 srcs: [
Mike Klein40a82bd2016-12-20 17:34:29 -050070 $none_srcs
Mike Kleinee43f6f2016-12-12 14:09:38 -050071 ],
72 },
73
74 x86: {
75 srcs: [
76 $x86_srcs
77 ],
78 },
79
80 x86_64: {
81 srcs: [
82 $x86_srcs
83 ],
84 },
Mike Klein308b5ac2016-12-06 16:03:52 -050085 },
Mike Klein27eb22b2016-12-07 12:27:56 -050086
Derek Sollenberger5a932162017-09-21 14:25:14 -040087 defaults: ["skia_deps"],
88}
89
90cc_defaults {
91 name: "skia_deps",
Mike Kleinee43f6f2016-12-12 14:09:38 -050092 shared_libs: [
93 "libEGL",
94 "libGLESv2",
95 "libdng_sdk",
96 "libexpat",
97 "libft2",
Leon Scroggins III04be2b52017-08-17 15:13:20 -040098 "libheif",
Mike Kleinee43f6f2016-12-12 14:09:38 -050099 "libicui18n",
100 "libicuuc",
101 "libjpeg",
102 "liblog",
103 "libpiex",
104 "libpng",
105 "libvulkan",
106 "libz",
Derek Sollenberger488f0d62017-03-03 15:48:33 -0500107 "libcutils",
Stan Iliev7e910df2017-06-02 10:29:21 -0400108 "libnativewindow",
Mike Kleinee43f6f2016-12-12 14:09:38 -0500109 ],
110 static_libs: [
Matt Saretta3091092017-02-20 12:50:52 -0500111 "libarect",
Mike Kleinee43f6f2016-12-12 14:09:38 -0500112 "libsfntly",
113 "libwebp-decode",
114 "libwebp-encode",
115 ],
Derek Sollenberger5a932162017-09-21 14:25:14 -0400116 group_static_libs: true,
117}
118
119cc_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 III6ccd2ca2017-01-26 17:21:27 -0500131}
132
133cc_test {
134 name: "skia_dm",
135
Derek Sollenberger5a932162017-09-21 14:25:14 -0400136 defaults: [
137 "skia_tool_deps"
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500138 ],
139
140 local_include_dirs: [
141 $dm_includes
142 ],
143
144 srcs: [
145 $dm_srcs
146 ],
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500147}
148
149cc_test {
150 name: "skia_nanobench",
151
Derek Sollenberger5a932162017-09-21 14:25:14 -0400152 defaults: [
153 "skia_tool_deps"
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500154 ],
155
156 local_include_dirs: [
157 $nanobench_includes
158 ],
159
160 srcs: [
161 $nanobench_srcs
162 ],
Mike Kleinee43f6f2016-12-12 14:09:38 -0500163}''')
Mike Klein308b5ac2016-12-06 16:03:52 -0500164
165# We'll run GN to get the main source lists and include directories for Skia.
166gn_args = {
Mike Kleine459afd2017-03-03 09:21:30 -0500167 'is_official_build': 'true',
Mike Kleine459afd2017-03-03 09:21:30 -0500168 'skia_enable_tools': 'true',
Leon Scroggins III04be2b52017-08-17 15:13:20 -0400169 'skia_use_libheif': 'true',
Mike Kleine459afd2017-03-03 09:21:30 -0500170 'skia_use_vulkan': 'true',
171 'target_cpu': '"none"',
172 'target_os': '"android"',
Mike Klein308b5ac2016-12-06 16:03:52 -0500173}
174gn_args = ' '.join(sorted('%s=%s' % (k,v) for (k,v) in gn_args.iteritems()))
175
176tmp = tempfile.mkdtemp()
177subprocess.check_call(['gn', 'gen', tmp, '--args=%s' % gn_args, '--ide=json'])
178
179js = json.load(open(os.path.join(tmp, 'project.json')))
180
181def strip_slashes(lst):
Ben Wagnere1275232017-02-23 18:00:06 -0500182 return {str(p.lstrip('/')) for p in lst}
Mike Klein27eb22b2016-12-07 12:27:56 -0500183
Mike Klein308b5ac2016-12-06 16:03:52 -0500184srcs = strip_slashes(js['targets']['//:skia']['sources'])
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400185cflags = strip_slashes(js['targets']['//:skia']['cflags'])
186cflags_cc = strip_slashes(js['targets']['//:skia']['cflags_cc'])
Mike Klein308b5ac2016-12-06 16:03:52 -0500187local_includes = strip_slashes(js['targets']['//:skia']['include_dirs'])
188export_includes = strip_slashes(js['targets']['//:public']['include_dirs'])
189
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500190dm_srcs = strip_slashes(js['targets']['//:dm']['sources'])
191dm_includes = strip_slashes(js['targets']['//:dm']['include_dirs'])
192
193nanobench_target = js['targets']['//:nanobench']
194nanobench_srcs = strip_slashes(nanobench_target['sources'])
195nanobench_includes = strip_slashes(nanobench_target['include_dirs'])
196
197def 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 Wagnere1275232017-02-23 18:00:06 -0500207 srcs_to_extend.update(strip_slashes(js['targets'][dep].get('sources', [])))
208 GrabDependentSrcs(dep, srcs_to_extend, exclude)
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500209
210GrabDependentSrcs('//:skia', srcs, None)
211GrabDependentSrcs('//:dm', dm_srcs, 'skia')
212GrabDependentSrcs('//:nanobench', nanobench_srcs, 'skia')
Mike Klein27eb22b2016-12-07 12:27:56 -0500213
214# No need to list headers.
Ben Wagnere1275232017-02-23 18:00:06 -0500215srcs = {s for s in srcs if not s.endswith('.h')}
216dm_srcs = {s for s in dm_srcs if not s.endswith('.h')}
217nanobench_srcs = {s for s in nanobench_srcs if not s.endswith('.h')}
Mike Klein27eb22b2016-12-07 12:27:56 -0500218
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400219# Only use the generated flags related to warnings.
220cflags = {s for s in cflags if s.startswith('-W')}
221cflags_cc = {s for s in cflags_cc if s.startswith('-W')}
222# Add the rest of the flags we want.
223cflags = cflags.union([
224 "-fvisibility=hidden",
225 "-D_FORTIFY_SOURCE=1",
226 "-DSKIA_DLL",
227 "-DSKIA_IMPLEMENTATION=1",
228 "-DATRACE_TAG=ATRACE_TAG_VIEW",
229])
230cflags_cc.add("-fexceptions")
231
232# We need to undefine FORTIFY_SOURCE before we define it. Insert it at the
233# beginning after sorting.
234cflags = sorted(cflags)
235cflags.insert(0, "-U_FORTIFY_SOURCE")
236
Mike Klein308b5ac2016-12-06 16:03:52 -0500237# Most defines go into SkUserConfig.h, where they're seen by Skia and its users.
Mike Klein308b5ac2016-12-06 16:03:52 -0500238defines = [str(d) for d in js['targets']['//:skia']['defines']]
Mike Kleine459afd2017-03-03 09:21:30 -0500239defines.remove('NDEBUG') # Let the Android build control this.
Mike Klein308b5ac2016-12-06 16:03:52 -0500240defines.remove('SKIA_IMPLEMENTATION=1') # Only libskia should have this define.
Mike Klein308b5ac2016-12-06 16:03:52 -0500241
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 Klein308b5ac2016-12-06 16:03:52 -0500247
248def 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
253builtins = { 'get_path_info': get_path_info }
254defs = {}
Leon Scroggins III4f8a4672016-12-19 09:32:21 -0500255here = os.path.dirname(__file__)
Mike Klein308b5ac2016-12-06 16:03:52 -0500256execfile(os.path.join(here, 'opts.gni'), builtins, defs)
Mike Klein308b5ac2016-12-06 16:03:52 -0500257
Mike Klein27eb22b2016-12-07 12:27:56 -0500258# Turn paths from opts.gni into paths relative to external/skia.
Mike Klein308b5ac2016-12-06 16:03:52 -0500259def 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 Kleinee43f6f2016-12-12 14:09:38 -0500267# Turn a list of strings into the style bpfmt outputs.
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500268def bpfmt(indent, lst, sort=True):
269 if sort:
270 lst = sorted(lst)
271 return ('\n' + ' '*indent).join('"%s",' % v for v in lst)
Mike Klein308b5ac2016-12-06 16:03:52 -0500272
273# OK! We have everything to fill in Android.bp...
274with open('Android.bp', 'w') as f:
275 print >>f, bp.substitute({
Mike Kleinee43f6f2016-12-12 14:09:38 -0500276 'export_includes': bpfmt(8, export_includes),
277 'local_includes': bpfmt(8, local_includes),
278 'srcs': bpfmt(8, srcs),
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400279 'cflags': bpfmt(8, cflags, False),
280 'cflags_cc': bpfmt(8, cflags_cc),
Mike Klein308b5ac2016-12-06 16:03:52 -0500281
Mike Kleinee43f6f2016-12-12 14:09:38 -0500282 '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 Klein40a82bd2016-12-20 17:34:29 -0500286 'none_srcs': bpfmt(16, scrub(defs['none'])),
Mike Kleinee43f6f2016-12-12 14:09:38 -0500287 'x86_srcs': bpfmt(16, scrub(defs['sse2'] +
288 defs['ssse3'] +
289 defs['sse41'] +
290 defs['sse42'] +
Mike Reede32500f2017-07-19 17:20:37 -0400291 defs['avx' ])),
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500292
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500293 '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 Klein308b5ac2016-12-06 16:03:52 -0500298 })
299
300#... and all the #defines we want to put in SkUserConfig.h.
Mike Kleinc3083332016-12-12 09:03:56 -0500301with open('include/config/SkUserConfig.h', 'w') as f:
Leon Scroggins III51b2f1b2017-07-11 15:53:41 -0400302 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 Klein308b5ac2016-12-06 16:03:52 -0500304 print >>f, '#ifndef SkUserConfig_DEFINED'
305 print >>f, '#define SkUserConfig_DEFINED'
Leon Scroggins III51b2f1b2017-07-11 15:53:41 -0400306 print >>f, '#include "SkUserConfigManual.h"'
Mike Klein27eb22b2016-12-07 12:27:56 -0500307 for define in sorted(defines):
Mike Kleinc3083332016-12-12 09:03:56 -0500308 print >>f, ' #define', define.replace('=', ' ')
Mike Klein308b5ac2016-12-06 16:03:52 -0500309 print >>f, '#endif//SkUserConfig_DEFINED'