Create SdkType to encapsulate behavioral differences of sdk and exports

Previously, the differences in behavior between the sdk and
module_exports modules was implemented by checking the sdk name
everywhere that it was needed. This change encapsulates the differences
in a new SdkType dataclass which is itself determined from the sdk name
when needed.

Bug: 238203992
Test: atest --host mainline_modules_sdks_test
      packages/modules/common/build/mainline_modules_sdks.sh
      # Compare before and after to make sure that they are
      # consistent.
Change-Id: Ia00069a70fda421dff4aeaa1f833b93e95574b98
This commit is contained in:
Paul Duffin
2022-07-25 16:37:11 +00:00
parent db3a3c7df6
commit d7b7cddf54

View File

@@ -533,7 +533,8 @@ java_sdk_library_import {{
target_dict = {} target_dict = {}
for module in modules: for module in modules:
for sdk in module.sdks: for sdk in module.sdks:
if "host-exports" in sdk or "test-exports" in sdk: sdk_type = sdk_type_from_name(sdk)
if not sdk_type.providesApis:
continue continue
sdk_info_file = sdk_snapshot_info_file(self.mainline_sdks_dir, sdk_info_file = sdk_snapshot_info_file(self.mainline_sdks_dir,
@@ -595,7 +596,8 @@ java_sdk_library_import {{
"""For each module sdk, create the api diff file.""" """For each module sdk, create the api diff file."""
for module in modules: for module in modules:
for sdk in module.sdks: for sdk in module.sdks:
if "host-exports" in sdk or "test-exports" in sdk: sdk_type = sdk_type_from_name(sdk)
if not sdk_type.providesApis:
continue continue
self.create_snapshot_api_diff(sdk, sdk_version, target_dict, self.create_snapshot_api_diff(sdk, sdk_version, target_dict,
snapshots_dir) snapshots_dir)
@@ -1189,7 +1191,8 @@ class SdkDistProducer:
def dist_sdk_snapshot_api_diff(self, sdk_dist_dir, sdk, sdk_version, module, def dist_sdk_snapshot_api_diff(self, sdk_dist_dir, sdk, sdk_version, module,
snapshots_dir): snapshots_dir):
"""Copy the sdk snapshot api diff file to a dist directory.""" """Copy the sdk snapshot api diff file to a dist directory."""
if "host-exports" in sdk or "test-exports" in sdk: sdk_type = sdk_type_from_name(sdk)
if not sdk_type.providesApis:
return return
sdk_dist_subdir = os.path.join(sdk_dist_dir, module.apex, "sdk") sdk_dist_subdir = os.path.join(sdk_dist_dir, module.apex, "sdk")
@@ -1225,10 +1228,8 @@ class SdkDistProducer:
def populate_dist_snapshot(self, build_release, module, sdk, sdk_dist_dir, def populate_dist_snapshot(self, build_release, module, sdk, sdk_dist_dir,
sdk_version, snapshots_dir): sdk_version, snapshots_dir):
subdir = re.sub("^.+-(sdk|(host|test)-exports)$", r"\1", sdk) sdk_type = sdk_type_from_name(sdk)
if subdir not in ("sdk", "host-exports", "test-exports"): subdir = sdk_type.name
raise Exception(f"{sdk} is not a valid name, expected it to end"
f" with -(sdk|host-exports|test-exports)")
sdk_dist_subdir = os.path.join(sdk_dist_dir, module.apex, subdir) sdk_dist_subdir = os.path.join(sdk_dist_dir, module.apex, subdir)
sdk_path = sdk_snapshot_zip_file(snapshots_dir, sdk, sdk_version) sdk_path = sdk_snapshot_zip_file(snapshots_dir, sdk, sdk_version)
@@ -1375,6 +1376,33 @@ def google_to_aosp_name(name):
return name.replace("com.google.android.", "com.android.") return name.replace("com.google.android.", "com.android.")
@dataclasses.dataclass(frozen=True)
class SdkType:
name: str
providesApis: bool = False
Sdk = SdkType(
name="sdk",
providesApis=True,
)
HostExports = SdkType(name="host-exports")
TestExports = SdkType(name="test-exports")
def sdk_type_from_name(name):
if name.endswith("-sdk"):
return Sdk
if name.endswith("-host-exports"):
return HostExports
if name.endswith("-test-exports"):
return TestExports
raise Exception(f"{name} is not a valid sdk name, expected it to end"
f" with -(sdk|host-exports|test-exports)")
def filter_modules(modules, target_build_apps): def filter_modules(modules, target_build_apps):
if target_build_apps: if target_build_apps:
target_build_apps = target_build_apps.split() target_build_apps = target_build_apps.split()