blob: 4f27b87d4d7b6a25093a5176bd4b067322693ed5 [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
Zhizhou Yange18b7792017-12-11 10:37:44 -080094 defaults: ["skia_deps",
95 "skia_pgo",
96 ],
97}
98
99// Build libskia with PGO by default.
100// Location of PGO profile data is defined in build/soong/cc/pgo.go
101// and is separate from skia.
102// To turn it off, set ANDROID_PGO_NO_PROFILE_USE environment variable
103// or set enable_profile_use property to false.
104cc_defaults {
105 name: "skia_pgo",
106 pgo: {
107 instrumentation: true,
108 profile_file: "skia/skia.profdata",
109 benchmarks: ["hwui", "skia"],
110 enable_profile_use: true,
111 },
Derek Sollenberger5a932162017-09-21 14:25:14 -0400112}
113
Pirama Arumuga Nainarde2b95e2017-12-13 11:07:39 -0800114// "defaults" property to disable profile use for Skia tools and benchmarks.
115cc_defaults {
116 name: "skia_pgo_no_profile_use",
117 defaults: [
118 "skia_pgo",
119 ],
120 pgo: {
121 enable_profile_use: false,
122 },
123}
124
Derek Sollenberger5a932162017-09-21 14:25:14 -0400125cc_defaults {
126 name: "skia_deps",
Mike Kleinee43f6f2016-12-12 14:09:38 -0500127 shared_libs: [
128 "libEGL",
129 "libGLESv2",
130 "libdng_sdk",
131 "libexpat",
132 "libft2",
Leon Scroggins III04be2b52017-08-17 15:13:20 -0400133 "libheif",
Mike Kleinee43f6f2016-12-12 14:09:38 -0500134 "libicui18n",
135 "libicuuc",
136 "libjpeg",
137 "liblog",
138 "libpiex",
139 "libpng",
140 "libvulkan",
141 "libz",
Derek Sollenberger488f0d62017-03-03 15:48:33 -0500142 "libcutils",
Stan Iliev7e910df2017-06-02 10:29:21 -0400143 "libnativewindow",
Mike Kleinee43f6f2016-12-12 14:09:38 -0500144 ],
145 static_libs: [
Matt Saretta3091092017-02-20 12:50:52 -0500146 "libarect",
Mike Kleinee43f6f2016-12-12 14:09:38 -0500147 "libsfntly",
148 "libwebp-decode",
149 "libwebp-encode",
150 ],
Derek Sollenberger5a932162017-09-21 14:25:14 -0400151 group_static_libs: true,
152}
153
154cc_defaults {
155 name: "skia_tool_deps",
156 defaults: [
Pirama Arumuga Nainarde2b95e2017-12-13 11:07:39 -0800157 "skia_deps",
158 "skia_pgo_no_profile_use"
Derek Sollenberger5a932162017-09-21 14:25:14 -0400159 ],
160 static_libs: [
161 "libjsoncpp",
162 "libskia",
163 ],
164 cflags: [
Derek Sollenberger9a72ae12017-12-14 13:01:30 -0500165 "-Wno-unused-parameter",
166 "-Wno-unused-variable",
Derek Sollenberger5a932162017-09-21 14:25:14 -0400167 ],
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500168}
169
170cc_test {
171 name: "skia_dm",
172
Derek Sollenberger5a932162017-09-21 14:25:14 -0400173 defaults: [
174 "skia_tool_deps"
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500175 ],
176
177 local_include_dirs: [
178 $dm_includes
179 ],
180
181 srcs: [
182 $dm_srcs
183 ],
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500184}
185
186cc_test {
187 name: "skia_nanobench",
188
Derek Sollenberger5a932162017-09-21 14:25:14 -0400189 defaults: [
190 "skia_tool_deps"
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500191 ],
192
193 local_include_dirs: [
194 $nanobench_includes
195 ],
196
197 srcs: [
198 $nanobench_srcs
199 ],
Mike Kleinee43f6f2016-12-12 14:09:38 -0500200}''')
Mike Klein308b5ac2016-12-06 16:03:52 -0500201
202# We'll run GN to get the main source lists and include directories for Skia.
203gn_args = {
Mike Kleine459afd2017-03-03 09:21:30 -0500204 'is_official_build': 'true',
Mike Kleine459afd2017-03-03 09:21:30 -0500205 'skia_enable_tools': 'true',
Leon Scroggins III04be2b52017-08-17 15:13:20 -0400206 'skia_use_libheif': 'true',
Mike Kleine459afd2017-03-03 09:21:30 -0500207 'skia_use_vulkan': 'true',
208 'target_cpu': '"none"',
209 'target_os': '"android"',
Greg Danielafb7ec72017-12-07 12:48:30 -0500210 'skia_vulkan_header': '"Skia_Vulkan_Android.h"',
Mike Klein308b5ac2016-12-06 16:03:52 -0500211}
212gn_args = ' '.join(sorted('%s=%s' % (k,v) for (k,v) in gn_args.iteritems()))
213
214tmp = tempfile.mkdtemp()
215subprocess.check_call(['gn', 'gen', tmp, '--args=%s' % gn_args, '--ide=json'])
216
217js = json.load(open(os.path.join(tmp, 'project.json')))
218
219def strip_slashes(lst):
Ben Wagnere1275232017-02-23 18:00:06 -0500220 return {str(p.lstrip('/')) for p in lst}
Mike Klein27eb22b2016-12-07 12:27:56 -0500221
Mike Klein308b5ac2016-12-06 16:03:52 -0500222srcs = strip_slashes(js['targets']['//:skia']['sources'])
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400223cflags = strip_slashes(js['targets']['//:skia']['cflags'])
224cflags_cc = strip_slashes(js['targets']['//:skia']['cflags_cc'])
Mike Klein308b5ac2016-12-06 16:03:52 -0500225local_includes = strip_slashes(js['targets']['//:skia']['include_dirs'])
226export_includes = strip_slashes(js['targets']['//:public']['include_dirs'])
227
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500228dm_srcs = strip_slashes(js['targets']['//:dm']['sources'])
229dm_includes = strip_slashes(js['targets']['//:dm']['include_dirs'])
230
231nanobench_target = js['targets']['//:nanobench']
232nanobench_srcs = strip_slashes(nanobench_target['sources'])
233nanobench_includes = strip_slashes(nanobench_target['include_dirs'])
234
235def GrabDependentSrcs(name, srcs_to_extend, exclude):
236 # Grab the sources from other targets that $name depends on (e.g. optional
237 # Skia components, gms, tests, etc).
238 for dep in js['targets'][name]['deps']:
239 if 'third_party' in dep:
240 continue # We've handled all third-party DEPS as static or shared_libs.
241 if 'none' in dep:
242 continue # We'll handle all cpu-specific sources manually later.
243 if exclude and exclude in dep:
244 continue
Ben Wagnere1275232017-02-23 18:00:06 -0500245 srcs_to_extend.update(strip_slashes(js['targets'][dep].get('sources', [])))
246 GrabDependentSrcs(dep, srcs_to_extend, exclude)
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500247
248GrabDependentSrcs('//:skia', srcs, None)
249GrabDependentSrcs('//:dm', dm_srcs, 'skia')
250GrabDependentSrcs('//:nanobench', nanobench_srcs, 'skia')
Mike Klein27eb22b2016-12-07 12:27:56 -0500251
252# No need to list headers.
Ben Wagnere1275232017-02-23 18:00:06 -0500253srcs = {s for s in srcs if not s.endswith('.h')}
254dm_srcs = {s for s in dm_srcs if not s.endswith('.h')}
255nanobench_srcs = {s for s in nanobench_srcs if not s.endswith('.h')}
Mike Klein27eb22b2016-12-07 12:27:56 -0500256
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400257# Only use the generated flags related to warnings.
258cflags = {s for s in cflags if s.startswith('-W')}
259cflags_cc = {s for s in cflags_cc if s.startswith('-W')}
260# Add the rest of the flags we want.
261cflags = cflags.union([
262 "-fvisibility=hidden",
263 "-D_FORTIFY_SOURCE=1",
264 "-DSKIA_DLL",
265 "-DSKIA_IMPLEMENTATION=1",
266 "-DATRACE_TAG=ATRACE_TAG_VIEW",
267])
268cflags_cc.add("-fexceptions")
269
270# We need to undefine FORTIFY_SOURCE before we define it. Insert it at the
271# beginning after sorting.
272cflags = sorted(cflags)
273cflags.insert(0, "-U_FORTIFY_SOURCE")
274
Greg Danielafb7ec72017-12-07 12:48:30 -0500275# We need to add the include path to the vulkan defines and header file set in
276# then skia_vulkan_header gn arg that is used for framework builds.
277local_includes.add("platform_tools/android/vulkan")
278export_includes.add("platform_tools/android/vulkan")
279
Mike Klein308b5ac2016-12-06 16:03:52 -0500280# Most defines go into SkUserConfig.h, where they're seen by Skia and its users.
Mike Klein308b5ac2016-12-06 16:03:52 -0500281defines = [str(d) for d in js['targets']['//:skia']['defines']]
Mike Kleine459afd2017-03-03 09:21:30 -0500282defines.remove('NDEBUG') # Let the Android build control this.
Mike Klein308b5ac2016-12-06 16:03:52 -0500283defines.remove('SKIA_IMPLEMENTATION=1') # Only libskia should have this define.
Mike Klein308b5ac2016-12-06 16:03:52 -0500284
285# For architecture specific files, it's easier to just read the same source
286# that GN does (opts.gni) rather than re-run GN once for each architecture.
287
288# This .gni file we want to read is close enough to Python syntax
289# that we can use execfile() if we supply definitions for GN builtins.
Mike Klein308b5ac2016-12-06 16:03:52 -0500290
291def get_path_info(path, kind):
292 assert kind == "abspath"
293 # While we want absolute paths in GN, relative paths work best here.
294 return path
295
296builtins = { 'get_path_info': get_path_info }
297defs = {}
Leon Scroggins III4f8a4672016-12-19 09:32:21 -0500298here = os.path.dirname(__file__)
Mike Klein308b5ac2016-12-06 16:03:52 -0500299execfile(os.path.join(here, 'opts.gni'), builtins, defs)
Mike Klein308b5ac2016-12-06 16:03:52 -0500300
Mike Klein27eb22b2016-12-07 12:27:56 -0500301# Turn paths from opts.gni into paths relative to external/skia.
Mike Klein308b5ac2016-12-06 16:03:52 -0500302def scrub(lst):
303 # Perform any string substitutions.
304 for var in defs:
305 if type(defs[var]) is str:
306 lst = [ p.replace('$'+var, defs[var]) for p in lst ]
307 # Relativize paths to top-level skia/ directory.
308 return [os.path.relpath(p, '..') for p in lst]
309
Mike Kleinee43f6f2016-12-12 14:09:38 -0500310# Turn a list of strings into the style bpfmt outputs.
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500311def bpfmt(indent, lst, sort=True):
312 if sort:
313 lst = sorted(lst)
314 return ('\n' + ' '*indent).join('"%s",' % v for v in lst)
Mike Klein308b5ac2016-12-06 16:03:52 -0500315
316# OK! We have everything to fill in Android.bp...
317with open('Android.bp', 'w') as f:
318 print >>f, bp.substitute({
Mike Kleinee43f6f2016-12-12 14:09:38 -0500319 'export_includes': bpfmt(8, export_includes),
320 'local_includes': bpfmt(8, local_includes),
321 'srcs': bpfmt(8, srcs),
Leon Scroggins III981a31e2017-10-06 11:53:53 -0400322 'cflags': bpfmt(8, cflags, False),
323 'cflags_cc': bpfmt(8, cflags_cc),
Mike Klein308b5ac2016-12-06 16:03:52 -0500324
Mike Kleinee43f6f2016-12-12 14:09:38 -0500325 'arm_srcs': bpfmt(16, scrub(defs['armv7'])),
326 'arm_neon_srcs': bpfmt(20, scrub(defs['neon'])),
327 'arm64_srcs': bpfmt(16, scrub(defs['arm64'] +
328 defs['crc32'])),
Mike Klein40a82bd2016-12-20 17:34:29 -0500329 'none_srcs': bpfmt(16, scrub(defs['none'])),
Mike Kleinee43f6f2016-12-12 14:09:38 -0500330 'x86_srcs': bpfmt(16, scrub(defs['sse2'] +
331 defs['ssse3'] +
332 defs['sse41'] +
333 defs['sse42'] +
Mike Reede32500f2017-07-19 17:20:37 -0400334 defs['avx' ])),
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500335
Leon Scroggins III6ccd2ca2017-01-26 17:21:27 -0500336 'dm_includes' : bpfmt(8, dm_includes),
337 'dm_srcs' : bpfmt(8, dm_srcs),
338
339 'nanobench_includes' : bpfmt(8, nanobench_includes),
340 'nanobench_srcs' : bpfmt(8, nanobench_srcs),
Mike Klein308b5ac2016-12-06 16:03:52 -0500341 })
342
343#... and all the #defines we want to put in SkUserConfig.h.
Mike Kleinc3083332016-12-12 09:03:56 -0500344with open('include/config/SkUserConfig.h', 'w') as f:
Leon Scroggins III51b2f1b2017-07-11 15:53:41 -0400345 print >>f, '// DO NOT MODIFY! This file is autogenerated by gn_to_bp.py.'
346 print >>f, '// If need to change a define, modify SkUserConfigManual.h'
Mike Klein308b5ac2016-12-06 16:03:52 -0500347 print >>f, '#ifndef SkUserConfig_DEFINED'
348 print >>f, '#define SkUserConfig_DEFINED'
Leon Scroggins III51b2f1b2017-07-11 15:53:41 -0400349 print >>f, '#include "SkUserConfigManual.h"'
Mike Klein27eb22b2016-12-07 12:27:56 -0500350 for define in sorted(defines):
Mike Kleinc3083332016-12-12 09:03:56 -0500351 print >>f, ' #define', define.replace('=', ' ')
Mike Klein308b5ac2016-12-06 16:03:52 -0500352 print >>f, '#endif//SkUserConfig_DEFINED'