Derek Sollenberger | 5d3f770 | 2018-02-01 09:22:53 -0500 | [diff] [blame] | 1 | #!/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 | |
| 10 | import argparse |
| 11 | import json |
| 12 | import os |
| 13 | import pprint |
| 14 | import string |
| 15 | import subprocess |
| 16 | import tempfile |
| 17 | |
| 18 | parser = argparse.ArgumentParser(description='Process some cmdline flags.') |
| 19 | parser.add_argument('--gn', dest='gn_cmd', default='gn') |
| 20 | args = parser.parse_args() |
| 21 | |
| 22 | def 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 | |
| 29 | def _strip_slash(lst): |
| 30 | return {str(p.lstrip('/')) for p in lst} |
| 31 | |
| 32 | def 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']: |
Florin Malita | 46fbf8c | 2018-06-10 01:18:28 -0400 | [diff] [blame] | 36 | if 'modules' in dep: |
| 37 | continue # Modules require special handling -- skip for now. |
Derek Sollenberger | 5d3f770 | 2018-02-01 09:22:53 -0500 | [diff] [blame] | 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 | |
| 47 | def CleanupCFlags(cflags): |
| 48 | # Only use the generated flags related to warnings. |
| 49 | cflags = {s for s in cflags if s.startswith('-W')} |
Greg Daniel | 18dbfd0 | 2018-05-29 10:46:51 -0400 | [diff] [blame] | 50 | # Add additional warning suppressions so we can build |
| 51 | # third_party/vulkanmemoryallocator |
| 52 | cflags = cflags.union([ |
Derek Sollenberger | 1821a5b | 2018-09-11 14:48:36 -0400 | [diff] [blame] | 53 | "-Wno-implicit-fallthrough", |
Greg Daniel | 18dbfd0 | 2018-05-29 10:46:51 -0400 | [diff] [blame] | 54 | "-Wno-missing-field-initializers", |
Derek Sollenberger | 1821a5b | 2018-09-11 14:48:36 -0400 | [diff] [blame] | 55 | "-Wno-thread-safety-analysis", |
Greg Daniel | 18dbfd0 | 2018-05-29 10:46:51 -0400 | [diff] [blame] | 56 | "-Wno-unused-variable", |
| 57 | ]) |
Derek Sollenberger | 5d3f770 | 2018-02-01 09:22:53 -0500 | [diff] [blame] | 58 | # 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 Sollenberger | 5d3f770 | 2018-02-01 09:22:53 -0500 | [diff] [blame] | 65 | "-DSK_PRINT_CODEC_MESSAGES", |
| 66 | ]) |
| 67 | |
| 68 | # We need to undefine FORTIFY_SOURCE before we define it. Insert it at the |
| 69 | # beginning after sorting. |
| 70 | cflags = sorted(cflags) |
| 71 | cflags.insert(0, "-U_FORTIFY_SOURCE") |
| 72 | return cflags |
| 73 | |
| 74 | def CleanupCCFlags(cflags_cc): |
| 75 | # Only use the generated flags related to warnings. |
| 76 | cflags_cc = {s for s in cflags_cc if s.startswith('-W')} |
| 77 | # Add the rest of the flags we want. |
| 78 | cflags_cc.add("-fexceptions") |
| 79 | return cflags_cc |
| 80 | |
| 81 | def _get_path_info(path, kind): |
| 82 | assert path == "../src" |
| 83 | assert kind == "abspath" |
| 84 | # While we want absolute paths in GN, relative paths work best here. |
| 85 | return "src" |
| 86 | |
| 87 | def GetArchSources(opts_file): |
| 88 | # For architecture specific files, it's easier to just read the same source |
| 89 | # that GN does (opts.gni) rather than re-run GN once for each architecture. |
| 90 | |
| 91 | # This .gni file we want to read is close enough to Python syntax |
| 92 | # that we can use execfile() if we supply definitions for GN builtins. |
| 93 | builtins = { 'get_path_info': _get_path_info } |
| 94 | defs = {} |
| 95 | execfile(opts_file, builtins, defs) |
| 96 | |
| 97 | # Perform any string substitutions. |
| 98 | for arch in defs: |
| 99 | defs[arch] = [ p.replace('$_src', 'src') for p in defs[arch]] |
| 100 | |
| 101 | return defs |
| 102 | |
| 103 | def WriteUserConfig(userConfigPath, defines): |
| 104 | # Most defines go into SkUserConfig.h |
| 105 | defines.remove('NDEBUG') # Controlled by the Android build |
| 106 | defines.remove('SKIA_IMPLEMENTATION=1') # don't export this define. |
Leon Scroggins III | ee0e5d0 | 2019-02-27 10:59:11 -0500 | [diff] [blame] | 107 | if 'WIN32_LEAN_AND_MEAN' in defines: # Controlled by the Android build |
| 108 | defines.remove('WIN32_LEAN_AND_MEAN') |
Leon Scroggins III | 631dbc8 | 2019-03-04 13:54:02 -0500 | [diff] [blame] | 109 | if '_HAS_EXCEPTIONS=0' in defines: # Controlled by the Android build |
| 110 | defines.remove('_HAS_EXCEPTIONS=0') |
Derek Sollenberger | 5d3f770 | 2018-02-01 09:22:53 -0500 | [diff] [blame] | 111 | |
| 112 | #... and all the #defines we want to put in SkUserConfig.h. |
| 113 | with open(userConfigPath, 'w') as f: |
| 114 | print >>f, '// DO NOT MODIFY! This file is autogenerated by gn_to_bp.py.' |
| 115 | print >>f, '// If need to change a define, modify SkUserConfigManual.h' |
Leon Scroggins III | c41a5f5 | 2018-11-15 15:54:59 -0500 | [diff] [blame] | 116 | print >>f, '#pragma once' |
Derek Sollenberger | 5d3f770 | 2018-02-01 09:22:53 -0500 | [diff] [blame] | 117 | print >>f, '#include "SkUserConfigManual.h"' |
| 118 | for define in sorted(defines): |
Leon Scroggins III | c41a5f5 | 2018-11-15 15:54:59 -0500 | [diff] [blame] | 119 | print >>f, '#define', define.replace('=', ' ') |