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