Debian Codegraph @

gn/gn_to_bp_utils.py

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