blob: 20260926a966c84e2226e880ae007c4bff3bc285 [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 ],
Leon Scroggins III74663e72017-11-30 14:42:58 -050078 cflags: [
Leon Scroggins III6d892f52017-12-01 09:48:10 -050079 // 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 III74663e72017-11-30 14:42:58 -050083 "-Wno-over-aligned"
84 ],
Mike Kleinee43f6f2016-12-12 14:09:38 -050085 },
86
87 x86_64: {
88 srcs: [
89 $x86_srcs
90 ],
91 },
Mike Klein308b5ac2016-12-06 16:03:52 -050092 },
Mike Klein27eb22b2016-12-07 12:27:56 -050093
Derek Sollenberger5a932162017-09-21 14:25:14 -040094 defaults: ["skia_deps"],
95}
96
97cc_defaults {
98 name: "skia_deps",
Mike Kleinee43f6f2016-12-12 14:09:38 -050099 shared_libs: [
100 "libEGL",
101 "libGLESv2",
102 "libdng_sdk",
103 "libexpat",
104 "libft2",
Leon Scroggins III04be2b52017-08-17 15:13:20 -0400105 "libheif",
Mike Kleinee43f6f2016-12-12 14:09:38 -0500106 "libicui18n",
107 "libicuuc",
108 "libjpeg",
109 "liblog",
110 "libpiex",
111 "libpng",
112 "libvulkan",
113 "libz",
Derek Sollenberger488f0d62017-03-03 15:48:33 -0500114 "libcutils",
Stan Iliev7e910df2017-06-02 10:29:21 -0400115 "libnativewindow",
Mike Kleinee43f6f2016-12-12 14:09:38 -0500116 ],
117 static_libs: [
Matt Saretta3091092017-02-20 12:50:52 -0500118 "libarect",
Mike Kleinee43f6f2016-12-12 14:09:38 -0500119 "libsfntly",
120 "libwebp-decode",
121 "libwebp-encode",
122 ],
Derek Sollenberger5a932162017-09-21 14:25:14 -0400123 group_static_libs: true,
124}
125
126cc_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 III6ccd2ca2017-01-26 17:21:27 -0500138}
139
140cc_test {
141 name: "skia_dm",
142
Derek Sollenberger5a932162017-09-21 14:25:14 -0400143 defaults: [
144 "skia_tool_deps"
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500145 ],
146
147 local_include_dirs: [
148 $dm_includes
149 ],
150
151 srcs: [
152 $dm_srcs
153 ],
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500154}
155
156cc_test {
157 name: "skia_nanobench",
158
Derek Sollenberger5a932162017-09-21 14:25:14 -0400159 defaults: [
160 "skia_tool_deps"
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500161 ],
162
163 local_include_dirs: [
164 $nanobench_includes
165 ],
166
167 srcs: [
168 $nanobench_srcs
169 ],
Mike Kleinee43f6f2016-12-12 14:09:38 -0500170}''')
Mike Klein308b5ac2016-12-06 16:03:52 -0500171
172# We'll run GN to get the main source lists and include directories for Skia.
173gn_args = {
Mike Kleine459afd2017-03-03 09:21:30 -0500174 'is_official_build': 'true',
Mike Kleine459afd2017-03-03 09:21:30 -0500175 'skia_enable_tools': 'true',
Leon Scroggins III04be2b52017-08-17 15:13:20 -0400176 'skia_use_libheif': 'true',
Mike Kleine459afd2017-03-03 09:21:30 -0500177 'skia_use_vulkan': 'true',
178 'target_cpu': '"none"',
179 'target_os': '"android"',
Mike Klein308b5ac2016-12-06 16:03:52 -0500180}
181gn_args = ' '.join(sorted('%s=%s' % (k,v) for (k,v) in gn_args.iteritems()))
182
183tmp = tempfile.mkdtemp()
184subprocess.check_call(['gn', 'gen', tmp, '--args=%s' % gn_args, '--ide=json'])
185
186js = json.load(open(os.path.join(tmp, 'project.json')))
187
188def strip_slashes(lst):
Ben Wagnere1275232017-02-23 18:00:06 -0500189 return {str(p.lstrip('/')) for p in lst}
Mike Klein27eb22b2016-12-07 12:27:56 -0500190
Mike Klein308b5ac2016-12-06 16:03:52 -0500191srcs = strip_slashes(js['targets']['//:skia']['sources'])
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400192cflags = strip_slashes(js['targets']['//:skia']['cflags'])
193cflags_cc = strip_slashes(js['targets']['//:skia']['cflags_cc'])
Mike Klein308b5ac2016-12-06 16:03:52 -0500194local_includes = strip_slashes(js['targets']['//:skia']['include_dirs'])
195export_includes = strip_slashes(js['targets']['//:public']['include_dirs'])
196
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500197dm_srcs = strip_slashes(js['targets']['//:dm']['sources'])
198dm_includes = strip_slashes(js['targets']['//:dm']['include_dirs'])
199
200nanobench_target = js['targets']['//:nanobench']
201nanobench_srcs = strip_slashes(nanobench_target['sources'])
202nanobench_includes = strip_slashes(nanobench_target['include_dirs'])
203
204def 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 Wagnere1275232017-02-23 18:00:06 -0500214 srcs_to_extend.update(strip_slashes(js['targets'][dep].get('sources', [])))
215 GrabDependentSrcs(dep, srcs_to_extend, exclude)
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500216
217GrabDependentSrcs('//:skia', srcs, None)
218GrabDependentSrcs('//:dm', dm_srcs, 'skia')
219GrabDependentSrcs('//:nanobench', nanobench_srcs, 'skia')
Mike Klein27eb22b2016-12-07 12:27:56 -0500220
221# No need to list headers.
Ben Wagnere1275232017-02-23 18:00:06 -0500222srcs = {s for s in srcs if not s.endswith('.h')}
223dm_srcs = {s for s in dm_srcs if not s.endswith('.h')}
224nanobench_srcs = {s for s in nanobench_srcs if not s.endswith('.h')}
Mike Klein27eb22b2016-12-07 12:27:56 -0500225
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400226# Only use the generated flags related to warnings.
227cflags = {s for s in cflags if s.startswith('-W')}
228cflags_cc = {s for s in cflags_cc if s.startswith('-W')}
229# Add the rest of the flags we want.
230cflags = cflags.union([
231 "-fvisibility=hidden",
232 "-D_FORTIFY_SOURCE=1",
233 "-DSKIA_DLL",
234 "-DSKIA_IMPLEMENTATION=1",
235 "-DATRACE_TAG=ATRACE_TAG_VIEW",
236])
237cflags_cc.add("-fexceptions")
238
239# We need to undefine FORTIFY_SOURCE before we define it. Insert it at the
240# beginning after sorting.
241cflags = sorted(cflags)
242cflags.insert(0, "-U_FORTIFY_SOURCE")
243
Mike Klein308b5ac2016-12-06 16:03:52 -0500244# Most defines go into SkUserConfig.h, where they're seen by Skia and its users.
Mike Klein308b5ac2016-12-06 16:03:52 -0500245defines = [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.
Mike Klein308b5ac2016-12-06 16:03:52 -0500254
255def 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
260builtins = { 'get_path_info': get_path_info }
261defs = {}
Leon Scroggins III4f8a4672016-12-19 09:32:21 -0500262here = os.path.dirname(__file__)
Mike Klein308b5ac2016-12-06 16:03:52 -0500263execfile(os.path.join(here, 'opts.gni'), builtins, defs)
Mike Klein308b5ac2016-12-06 16:03:52 -0500264
Mike Klein27eb22b2016-12-07 12:27:56 -0500265# Turn paths from opts.gni into paths relative to external/skia.
Mike Klein308b5ac2016-12-06 16:03:52 -0500266def 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 Kleinee43f6f2016-12-12 14:09:38 -0500274# Turn a list of strings into the style bpfmt outputs.
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500275def bpfmt(indent, lst, sort=True):
276 if sort:
277 lst = sorted(lst)
278 return ('\n' + ' '*indent).join('"%s",' % v for v in lst)
Mike Klein308b5ac2016-12-06 16:03:52 -0500279
280# OK! We have everything to fill in Android.bp...
281with open('Android.bp', 'w') as f:
282 print >>f, bp.substitute({
Mike Kleinee43f6f2016-12-12 14:09:38 -0500283 'export_includes': bpfmt(8, export_includes),
284 'local_includes': bpfmt(8, local_includes),
285 'srcs': bpfmt(8, srcs),
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400286 'cflags': bpfmt(8, cflags, False),
287 'cflags_cc': bpfmt(8, cflags_cc),
Mike Klein308b5ac2016-12-06 16:03:52 -0500288
Mike Kleinee43f6f2016-12-12 14:09:38 -0500289 '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 Klein40a82bd2016-12-20 17:34:29 -0500293 'none_srcs': bpfmt(16, scrub(defs['none'])),
Mike Kleinee43f6f2016-12-12 14:09:38 -0500294 'x86_srcs': bpfmt(16, scrub(defs['sse2'] +
295 defs['ssse3'] +
296 defs['sse41'] +
297 defs['sse42'] +
Mike Reede32500f2017-07-19 17:20:37 -0400298 defs['avx' ])),
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500299
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500300 '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 Klein308b5ac2016-12-06 16:03:52 -0500305 })
306
307#... and all the #defines we want to put in SkUserConfig.h.
Mike Kleinc3083332016-12-12 09:03:56 -0500308with open('include/config/SkUserConfig.h', 'w') as f:
Leon Scroggins III51b2f1b2017-07-11 15:53:41 -0400309 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 Klein308b5ac2016-12-06 16:03:52 -0500311 print >>f, '#ifndef SkUserConfig_DEFINED'
312 print >>f, '#define SkUserConfig_DEFINED'
Leon Scroggins III51b2f1b2017-07-11 15:53:41 -0400313 print >>f, '#include "SkUserConfigManual.h"'
Mike Klein27eb22b2016-12-07 12:27:56 -0500314 for define in sorted(defines):
Mike Kleinc3083332016-12-12 09:03:56 -0500315 print >>f, ' #define', define.replace('=', ' ')
Mike Klein308b5ac2016-12-06 16:03:52 -0500316 print >>f, '#endif//SkUserConfig_DEFINED'