blob: 556448cdac74890f4339386c5fd7bfd79a8f2ae6 [file] [log] [blame]
Derek Sollenberger5d3f7702018-02-01 09:22:53 -05001#!/usr/bin/env python
2#
3# Copyright 2018 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
Martin Vejdarskibeaaf472020-03-03 13:53:20 +070010from __future__ import print_function
11
Derek Sollenberger5d3f7702018-02-01 09:22:53 -050012import argparse
13import json
14import os
15import pprint
16import string
17import subprocess
18import tempfile
19
20parser = argparse.ArgumentParser(description='Process some cmdline flags.')
21parser.add_argument('--gn', dest='gn_cmd', default='gn')
22args = parser.parse_args()
23
24def GenerateJSONFromGN(gn_args):
25 gn_args = ' '.join(sorted('%s=%s' % (k,v) for (k,v) in gn_args.iteritems()))
26 tmp = tempfile.mkdtemp()
27 subprocess.check_call([args.gn_cmd, 'gen', tmp, '--args=%s' % gn_args,
28 '--ide=json'])
29 return json.load(open(os.path.join(tmp, 'project.json')))
30
31def _strip_slash(lst):
32 return {str(p.lstrip('/')) for p in lst}
33
34def GrabDependentValues(js, name, value_type, list_to_extend, exclude):
35 # Grab the values from other targets that $name depends on (e.g. optional
36 # Skia components, gms, tests, etc).
37 for dep in js['targets'][name]['deps']:
38 if 'third_party' in dep:
39 continue # We've handled all third-party DEPS as static or shared_libs.
40 if 'none' in dep:
41 continue # We'll handle all cpu-specific sources manually later.
42 if exclude and exclude in dep:
43 continue
44 list_to_extend.update(_strip_slash(js['targets'][dep].get(value_type, [])))
45 GrabDependentValues(js, dep, value_type, list_to_extend, exclude)
46
47def CleanupCFlags(cflags):
48 # Only use the generated flags related to warnings.
49 cflags = {s for s in cflags if s.startswith('-W')}
Greg Daniel18dbfd02018-05-29 10:46:51 -040050 # Add additional warning suppressions so we can build
51 # third_party/vulkanmemoryallocator
52 cflags = cflags.union([
Derek Sollenberger1821a5b2018-09-11 14:48:36 -040053 "-Wno-implicit-fallthrough",
Greg Daniel18dbfd02018-05-29 10:46:51 -040054 "-Wno-missing-field-initializers",
Derek Sollenberger1821a5b2018-09-11 14:48:36 -040055 "-Wno-thread-safety-analysis",
Greg Daniel18dbfd02018-05-29 10:46:51 -040056 "-Wno-unused-variable",
57 ])
Derek Sollenberger5d3f7702018-02-01 09:22:53 -050058 # Add the rest of the flags we want.
59 cflags = cflags.union([
60 "-fvisibility=hidden",
61 "-D_FORTIFY_SOURCE=1",
62 "-DSKIA_DLL",
63 "-DSKIA_IMPLEMENTATION=1",
64 "-DATRACE_TAG=ATRACE_TAG_VIEW",
Derek Sollenberger5d3f7702018-02-01 09:22:53 -050065 ])
66
67 # We need to undefine FORTIFY_SOURCE before we define it. Insert it at the
68 # beginning after sorting.
69 cflags = sorted(cflags)
70 cflags.insert(0, "-U_FORTIFY_SOURCE")
71 return cflags
72
73def CleanupCCFlags(cflags_cc):
74 # Only use the generated flags related to warnings.
75 cflags_cc = {s for s in cflags_cc if s.startswith('-W')}
76 # Add the rest of the flags we want.
77 cflags_cc.add("-fexceptions")
78 return cflags_cc
79
80def _get_path_info(path, kind):
81 assert path == "../src"
82 assert kind == "abspath"
83 # While we want absolute paths in GN, relative paths work best here.
84 return "src"
85
86def GetArchSources(opts_file):
87 # For architecture specific files, it's easier to just read the same source
88 # that GN does (opts.gni) rather than re-run GN once for each architecture.
89
90 # This .gni file we want to read is close enough to Python syntax
91 # that we can use execfile() if we supply definitions for GN builtins.
92 builtins = { 'get_path_info': _get_path_info }
93 defs = {}
94 execfile(opts_file, builtins, defs)
95
96 # Perform any string substitutions.
97 for arch in defs:
98 defs[arch] = [ p.replace('$_src', 'src') for p in defs[arch]]
99
100 return defs
101
102def WriteUserConfig(userConfigPath, defines):
103 # Most defines go into SkUserConfig.h
104 defines.remove('NDEBUG') # Controlled by the Android build
Mike Kleinf5f4e812019-04-29 15:45:14 +0000105 defines.remove('SKIA_IMPLEMENTATION=1') # don't export this define.
Leon Scroggins IIIee0e5d02019-02-27 10:59:11 -0500106 if 'WIN32_LEAN_AND_MEAN' in defines: # Controlled by the Android build
107 defines.remove('WIN32_LEAN_AND_MEAN')
Leon Scroggins III631dbc82019-03-04 13:54:02 -0500108 if '_HAS_EXCEPTIONS=0' in defines: # Controlled by the Android build
109 defines.remove('_HAS_EXCEPTIONS=0')
Derek Sollenberger5d3f7702018-02-01 09:22:53 -0500110
111 #... and all the #defines we want to put in SkUserConfig.h.
112 with open(userConfigPath, 'w') as f:
Martin Vejdarskibeaaf472020-03-03 13:53:20 +0700113 print('// DO NOT MODIFY! This file is autogenerated by gn_to_bp.py.', file=f)
114 print('// If need to change a define, modify SkUserConfigManual.h', file=f)
115 print('#pragma once', file=f)
116 print('#include "SkUserConfigManual.h"', file=f)
Derek Sollenberger5d3f7702018-02-01 09:22:53 -0500117 for define in sorted(defines):
Martin Vejdarskibeaaf472020-03-03 13:53:20 +0700118 print('', file=f)
119 print('#ifndef', define.split('=')[0], file=f)
120 print('#define', define.replace('=', ' ', 1), file=f)
121 print('#endif', file=f)