Merge changes I3ae20595,I24aaf62e,I763a5d30,I07270c9a,Ib91dfdd6, ...

* changes:
  gn2bp: Generate a copy of 'cronet_jni_registration' for java
  gn2bp: Fix args formatting for java_cpp_enum.py script
  gn2bp: Move 'sanitize_version_filepath' to base class
  gn2bp: Create `java_genrule` from java actions
  gn2bp: Convert java_actions to `java_group`
  gn2bp: Remove redundant checks
This commit is contained in:
Patrick Rohr
2022-12-02 03:38:38 +00:00
committed by Gerrit Code Review
3 changed files with 443 additions and 659 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -397,15 +397,12 @@ class Module(object):
return write_blueprint_key_value(output, name, value, sort) return write_blueprint_key_value(output, name, value, sort)
def is_compiled(self): def is_compiled(self):
return self.type not in ('cc_genrule', 'filegroup', 'cc_defaults') return self.type not in ('cc_genrule', 'filegroup', 'java_genrule')
def is_genrule(self): def is_genrule(self):
return self.type == "cc_genrule" return self.type == "cc_genrule"
def has_input_files(self): def has_input_files(self):
for target in self.target.values():
if len(target.srcs) > 0:
return True
return len(self.srcs) > 0 or any([len(target.srcs) > 0 for target in self.target.values()]) return len(self.srcs) > 0 or any([len(target.srcs) > 0 for target in self.target.values()])
def merge_attribute(self, key, source_module, allowed_archs, source_key = None): def merge_attribute(self, key, source_module, allowed_archs, source_key = None):
@@ -699,6 +696,12 @@ class BaseActionSanitizer():
self.target.args.append(arg) self.target.args.append(arg)
self.target.args.append(value) self.target.args.append(value)
def _sanitize_filepath_with_location_tag(self, arg):
if arg.startswith('../../'):
arg = self._sanitize_filepath(arg)
arg = self._add_location_tag(arg)
return arg
# wrap filename in location tag. # wrap filename in location tag.
def _add_location_tag(self, filename): def _add_location_tag(self, filename):
return '$(location %s)' % filename return '$(location %s)' % filename
@@ -763,12 +766,6 @@ class JniRegistrationGeneratorSanitizer(BaseActionSanitizer):
return super().get_args() return super().get_args()
class VersionSanitizer(BaseActionSanitizer): class VersionSanitizer(BaseActionSanitizer):
def _sanitize_version_filepath(self, arg):
if arg.startswith('../../'):
arg = self._sanitize_filepath(arg)
arg = self._add_location_tag(arg)
return arg
def _sanitize_eval(self): def _sanitize_eval(self):
assert (self._is_value_arg('-e')) assert (self._is_value_arg('-e'))
# arg for -e EVAL option should be passed in -e PATCH_HI=int(PATCH)//256 format. # arg for -e EVAL option should be passed in -e PATCH_HI=int(PATCH)//256 format.
@@ -783,10 +780,16 @@ class VersionSanitizer(BaseActionSanitizer):
self._set_value_arg('-o', '$(out)') self._set_value_arg('-o', '$(out)')
# args for the version.py contain file path without leading --arg key. So apply sanitize # args for the version.py contain file path without leading --arg key. So apply sanitize
# function for all the args. # function for all the args.
self._update_all_args(self._sanitize_version_filepath) self._update_all_args(self._sanitize_filepath_with_location_tag)
self._sanitize_eval() self._sanitize_eval()
return super().get_args() return super().get_args()
class JavaCppEnumSanitizer(BaseActionSanitizer):
def get_args(self):
self._update_all_args(self._sanitize_filepath_with_location_tag)
self._set_value_arg('--srcjar', '$(out)')
return super().get_args()
def get_action_sanitizer(target): def get_action_sanitizer(target):
if target.script == "//build/write_buildflag_header.py": if target.script == "//build/write_buildflag_header.py":
return WriteBuildFlagHeaderSanitizer(target) return WriteBuildFlagHeaderSanitizer(target)
@@ -798,6 +801,8 @@ def get_action_sanitizer(target):
return JniRegistrationGeneratorSanitizer(target) return JniRegistrationGeneratorSanitizer(target)
elif target.script == "//build/util/version.py": elif target.script == "//build/util/version.py":
return VersionSanitizer(target) return VersionSanitizer(target)
elif target.script == "//build/android/gyp/java_cpp_enum.py":
return JavaCppEnumSanitizer(target)
else: else:
# TODO: throw exception here once all script hacks have been converted. # TODO: throw exception here once all script hacks have been converted.
return BaseActionSanitizer(target) return BaseActionSanitizer(target)
@@ -833,11 +838,11 @@ def create_action_foreach_modules(blueprint, target):
new_args.append(arg) new_args.append(arg)
target.args = new_args target.args = new_args
return create_action_module(blueprint, target) return create_action_module(blueprint, target, 'cc_genrule')
def create_action_module(blueprint, target): def create_action_module(blueprint, target, type):
bp_module_name = label_to_module_name(target.name) bp_module_name = label_to_module_name(target.name)
module = Module('cc_genrule', bp_module_name, target.name) module = Module(type, bp_module_name, target.name)
sanitizer = get_action_sanitizer(target) sanitizer = get_action_sanitizer(target)
target.args = sanitizer.get_args() target.args = sanitizer.get_args()
@@ -967,7 +972,6 @@ def set_module_include_dirs(module, cflags, include_dirs):
for d in include_dirs for d in include_dirs
if not re.match('^//out/.*', d)]) if not re.match('^//out/.*', d)])
def create_modules_from_target(blueprint, gn, gn_target_name): def create_modules_from_target(blueprint, gn, gn_target_name):
"""Generate module(s) for a given GN target. """Generate module(s) for a given GN target.
@@ -1007,7 +1011,15 @@ def create_modules_from_target(blueprint, gn, gn_target_name):
if module is None: if module is None:
return None return None
elif target.type == 'action': elif target.type == 'action':
module = create_action_module(blueprint, target) if gn_utils.is_java_action(target.script, target.outputs) and \
target.name != "//components/cronet/android:cronet_jni_registration":
# This is a cc_genrule that generates both a c++ header file and
# srcjar for java. Treat it as a cc_genrule for the time being while
# making anothe deep copy of the module and append _java to it for
# the java_library.
module = create_action_module(blueprint, target, 'java_genrule')
else:
module = create_action_module(blueprint, target, 'cc_genrule')
elif target.type == 'action_foreach': elif target.type == 'action_foreach':
module = create_action_foreach_modules(blueprint, target) module = create_action_foreach_modules(blueprint, target)
elif target.type == 'copy': elif target.type == 'copy':
@@ -1186,6 +1198,14 @@ def create_java_module(blueprint, gn):
bp_module_name = module_prefix + 'java' bp_module_name = module_prefix + 'java'
module = Module('java_library', bp_module_name, '//gn:java') module = Module('java_library', bp_module_name, '//gn:java')
module.srcs.update([gn_utils.label_to_path(source) for source in gn.java_sources]) module.srcs.update([gn_utils.label_to_path(source) for source in gn.java_sources])
for dep in gn.java_actions:
dep_module = create_modules_from_target(blueprint, gn, dep)
if "cronet_jni_registration" in dep_module.name:
dep_module = copy.deepcopy(dep_module)
dep_module.name += "_java"
dep_module.type = 'java_genrule'
dep_module.out = [out for out in dep_module.out if out.endswith(".srcjar")]
blueprint.add_module(dep_module)
blueprint.add_module(module) blueprint.add_module(module)
def update_jni_registration_module(module, gn): def update_jni_registration_module(module, gn):

View File

@@ -30,7 +30,20 @@ import sys
BUILDFLAGS_TARGET = '//gn:gen_buildflags' BUILDFLAGS_TARGET = '//gn:gen_buildflags'
GEN_VERSION_TARGET = '//src/base:version_gen_h' GEN_VERSION_TARGET = '//src/base:version_gen_h'
LINKER_UNIT_TYPES = ('executable', 'shared_library', 'static_library', 'source_set') LINKER_UNIT_TYPES = ('executable', 'shared_library', 'static_library', 'source_set')
JAVA_BANNED_SCRIPTS = [
"//build/android/gyp/turbine.py",
"//build/android/gyp/compile_java.py",
"//build/android/gyp/filter_zip.py",
"//build/android/gyp/dex.py",
"//build/android/gyp/write_build_config.py",
"//build/android/gyp/create_r_java.py",
"//build/android/gyp/ijar.py",
"//build/android/gyp/create_r_java.py",
"//build/android/gyp/bytecode_processor.py",
"//build/android/gyp/prepare_resources.py",
"//build/android/gyp/aar.py",
"//build/android/gyp/zip.py",
]
# TODO(primiano): investigate these, they require further componentization. # TODO(primiano): investigate these, they require further componentization.
ODR_VIOLATION_IGNORE_TARGETS = { ODR_VIOLATION_IGNORE_TARGETS = {
'//test/cts:perfetto_cts_deps', '//test/cts:perfetto_cts_deps',
@@ -72,6 +85,11 @@ def label_to_target_name_with_path(label):
def _is_java_source(src): def _is_java_source(src):
return os.path.splitext(src)[1] == '.java' and not src.startswith("//out/test/gen/") return os.path.splitext(src)[1] == '.java' and not src.startswith("//out/test/gen/")
def is_java_action(script, outputs):
return (script != "" and script not in JAVA_BANNED_SCRIPTS) and any(
[file.endswith(".srcjar") or file.endswith(".java")
for file in outputs])
class GnParser(object): class GnParser(object):
"""A parser with some cleverness for GN json desc files """A parser with some cleverness for GN json desc files
@@ -223,6 +241,7 @@ class GnParser(object):
self.actions = {} self.actions = {}
self.proto_libs = {} self.proto_libs = {}
self.java_sources = set() self.java_sources = set()
self.java_actions = set()
def _get_response_file_contents(self, action_desc): def _get_response_file_contents(self, action_desc):
# response_file_contents are formatted as: # response_file_contents are formatted as:
@@ -285,7 +304,9 @@ class GnParser(object):
# genrule's do not allow to overload cmd per target OS / arch. Create a # genrule's do not allow to overload cmd per target OS / arch. Create a
# separate action for every architecture. # separate action for every architecture.
# Cover both action and action_foreach # Cover both action and action_foreach
if type_.startswith('action'): if type_.startswith('action') and \
not is_java_action(desc.get("script", ""), desc.get("outputs", [])):
# Don't meddle with the java actions name
target_name += '__' + arch target_name += '__' + arch
target = self.all_targets.get(target_name) target = self.all_targets.get(target_name)
@@ -319,6 +340,14 @@ class GnParser(object):
elif target.type in LINKER_UNIT_TYPES: elif target.type in LINKER_UNIT_TYPES:
self.linker_units[gn_target_name] = target self.linker_units[gn_target_name] = target
target.arch[arch].sources.update(desc.get('sources', [])) target.arch[arch].sources.update(desc.get('sources', []))
elif desc.get("script", "") in JAVA_BANNED_SCRIPTS or self._is_java_target(target):
# java_group identifies the group target generated by the android_library
# or java_library template. A java_group must not be added as a dependency, but sources are collected
log.debug('Found java target %s', target.name)
if target.type == "action":
# Convert java actions into java_group and keep the inputs for collection.
target.inputs.update(desc.get('inputs', []))
target.type = 'java_group'
elif target.type in ['action', 'action_foreach']: elif target.type in ['action', 'action_foreach']:
self.actions[gn_target_name] = target self.actions[gn_target_name] = target
target.inputs.update(desc.get('inputs', [])) target.inputs.update(desc.get('inputs', []))
@@ -331,11 +360,6 @@ class GnParser(object):
elif target.type == 'copy': elif target.type == 'copy':
# TODO: copy rules are not currently implemented. # TODO: copy rules are not currently implemented.
self.actions[gn_target_name] = target self.actions[gn_target_name] = target
elif self._is_java_target(target):
# java_group identifies the group target generated by the android_library
# or java_library template. A java_group must not be added as a dependency, but sources are collected
log.debug('Found java target %s', target.name)
target.type = 'java_group'
# Default for 'public' is //* - all headers in 'sources' are public. # Default for 'public' is //* - all headers in 'sources' are public.
# TODO(primiano): if a 'public' section is specified (even if empty), then # TODO(primiano): if a 'public' section is specified (even if empty), then
@@ -402,6 +426,8 @@ class GnParser(object):
log.debug('Adding java sources for %s', dep.name) log.debug('Adding java sources for %s', dep.name)
java_srcs = [src for src in dep.inputs if _is_java_source(src)] java_srcs = [src for src in dep.inputs if _is_java_source(src)]
self.java_sources.update(java_srcs) self.java_sources.update(java_srcs)
if dep.type in ["action"] and target.type == "java_group":
self.java_actions.add(dep.name)
return target return target
def get_proto_exports(self, proto_desc): def get_proto_exports(self, proto_desc):