gn2bp: Convert cc_objects to cc_static_library
* cc_objects are more like source-sets. However, they are not commonly used in AOSP which makes it risky to rely on them.
* Clang native code coverage is not working for cc_objects. Which is needed for the dashboard.
Size Delta to .SO
x64: -0.1MB
x86: 0MB
arm: 0MB
arm64: 0MB
Test: m
Bug: 270027426
Change-Id: I1d74e0293dac91334bded73b0e89465b7af7480c
diff --git a/tools/gn2bp/gen_android_bp b/tools/gn2bp/gen_android_bp
index 2c970a5..d714a90 100755
--- a/tools/gn2bp/gen_android_bp
+++ b/tools/gn2bp/gen_android_bp
@@ -549,7 +549,13 @@
return self.type == "cc_genrule"
def has_input_files(self):
- return len(self.srcs) > 0 or any([len(target.srcs) > 0 for target in self.target.values()])
+ if len(self.srcs) > 0:
+ return True
+ if any([len(target.srcs) > 0 for target in self.target.values()]):
+ return True
+ # Allow cc_static_library with export_generated_headers as those are crucial for
+ # the depending modules
+ return len(self.export_generated_headers) > 0
def merge_attribute(self, key, source_module, allowed_archs, source_key = None):
"""
@@ -591,8 +597,8 @@
def to_string(self, output):
for m in sorted(self.modules.values(), key=lambda m: m.name):
- if m.type != "cc_object" or m.has_input_files():
- # Don't print cc_object with empty srcs. These attributes are already
+ if m.type != "cc_library_static" or m.has_input_files():
+ # Don't print cc_library_static with empty srcs. These attributes are already
# propagated up the tree. Printing them messes the presubmits because
# every module is compiled while those targets are not reachable in
# a normal compilation path.
@@ -1345,10 +1351,9 @@
def set_module_flags(module, module_type, cflags, defines, ldflags, libs):
module.cflags.update(_get_cflags(cflags, defines))
- if module_type != 'cc_object':
- module.ldflags.update({flag for flag in ldflags
- if flag in ldflag_allowlist or flag.startswith("-Wl,-wrap,")})
- _set_linker_script(module, libs)
+ module.ldflags.update({flag for flag in ldflags
+ if flag in ldflag_allowlist or flag.startswith("-Wl,-wrap,")})
+ _set_linker_script(module, libs)
# TODO: implement proper cflag parsing.
for flag in cflags:
if '-std=' in flag:
@@ -1397,12 +1402,10 @@
# Can be used for both host and device targets.
module_type = 'cc_binary'
module = Module(module_type, bp_module_name, gn_target_name)
- elif target.type == 'static_library':
+ elif target.type in ['static_library', 'source_set']:
module = Module('cc_library_static', bp_module_name, gn_target_name)
elif target.type == 'shared_library':
module = Module('cc_library_shared', bp_module_name, gn_target_name)
- elif target.type == 'source_set':
- module = Module('cc_object', bp_module_name, gn_target_name)
elif target.type == 'group':
# "group" targets are resolved recursively by gn_utils.get_target().
# There's nothing we need to do at this level for them.
@@ -1525,27 +1528,21 @@
if not module.is_compiled() or module.is_genrule():
continue
+ # Drop compiled modules that doesn't provide any benefit. This is mostly
+ # applicable to source_sets when converted to cc_static_library, sometimes
+ # the source set only has header files which are dropped so the module becomes empty.
+ if dep_module.is_compiled() and not dep_module.has_input_files():
+ continue
+
if dep_module.type == 'cc_library_shared':
module.shared_libs.add(dep_module.name)
elif dep_module.type == 'cc_library_static':
module.static_libs.add(dep_module.name)
- elif dep_module.type == 'cc_object':
- module.merge_attribute('generated_headers', dep_module, target.arch.keys())
- if module.type != 'cc_object':
- if dep_module.has_input_files():
- # Only add it as part of srcs if the dep_module has input files otherwise
- # this would throw an error.
- module.srcs.add(":" + dep_module.name)
- module.merge_attribute('export_generated_headers', dep_module,
- target.arch.keys(), 'generated_headers')
elif dep_module.type == 'cc_genrule':
module.merge_attribute('generated_headers', dep_module, [], 'genrule_headers')
module.merge_attribute('srcs', dep_module, [], 'genrule_srcs')
module.merge_attribute('shared_libs', dep_module, [], 'genrule_shared_libs')
module.merge_attribute('header_libs', dep_module, [], 'genrule_header_libs')
- if module.type not in ["cc_object"]:
- module.merge_attribute('export_generated_headers', dep_module, [],
- 'genrule_headers')
elif dep_module.type == 'cc_binary':
continue # Ignore executables deps (used by cmdline integration tests).
else:
@@ -1563,20 +1560,13 @@
# Arch-specific dependencies currently only include cc_library_static.
# Revisit this approach once we need to support more target types.
if dep_module.type == 'cc_library_static':
- module.target[arch_name].static_libs.add(dep_module.name)
+ if dep_module.has_input_files():
+ module.target[arch_name].static_libs.add(dep_module.name)
elif dep_module.type == 'cc_genrule':
module.target[arch_name].generated_headers.update(dep_module.genrule_headers)
module.target[arch_name].srcs.update(dep_module.genrule_srcs)
module.target[arch_name].shared_libs.update(dep_module.genrule_shared_libs)
module.target[arch_name].header_libs.update(dep_module.genrule_header_libs)
- if module.type not in ["cc_object"]:
- module.target[arch_name].export_generated_headers.update(
- dep_module.genrule_headers)
- elif dep_module.type == 'cc_object':
- if dep_module.has_input_files():
- # Only add it as part of srcs if the dep_module has input files otherwise
- # this would throw an error.
- module.target[arch_name].srcs.add(":" + dep_module.name)
else:
raise Error('Unsupported arch-specific dependency %s of target %s with type %s' %
(dep_module.name, target.name, dep_module.type))