Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6a2bd115 authored by Paul Duffin's avatar Paul Duffin
Browse files

Generate struct with field for each api scope

Makes it easier to add new api scopes by generating a struct with a
field for each scope.

Bug: 153443117
Bug: 153306490
Test: m nothing
Merged-In: Iec77a858d2c851d99e56568b89cb30aa3ff2a244
Change-Id: Iec77a858d2c851d99e56568b89cb30aa3ff2a244
parent 455b0bf9
Loading
Loading
Loading
Loading
+53 −23
Original line number Diff line number Diff line
@@ -15,17 +15,18 @@
package java

import (
	"android/soong/android"

	"fmt"
	"path"
	"path/filepath"
	"reflect"
	"sort"
	"strings"
	"sync"

	"github.com/google/blueprint"
	"github.com/google/blueprint/proptools"

	"android/soong/android"
)

const (
@@ -66,6 +67,9 @@ type apiScope struct {
	// The name of the api scope, e.g. public, system, test
	name string

	// The name of the field in the dynamically created structure.
	fieldName string

	// The tag to use to depend on the stubs library module.
	stubsTag scopeDependencyTag

@@ -93,7 +97,7 @@ type apiScope struct {

// Initialize a scope, creating and adding appropriate dependency tags
func initApiScope(scope *apiScope) *apiScope {
	//apiScope := &scope
	scope.fieldName = proptools.FieldNameForProperty(scope.name)
	scope.stubsTag = scopeDependencyTag{
		name:     scope.name + "-stubs",
		apiScope: scope,
@@ -806,15 +810,6 @@ type sdkLibraryImportProperties struct {
	// List of shared java libs, common to all scopes, that this module has
	// dependencies to
	Libs []string

	// Properties associated with the public api scope.
	Public sdkLibraryScopeProperties

	// Properties associated with the system api scope.
	System sdkLibraryScopeProperties

	// Properties associated with the test api scope.
	Test sdkLibraryScopeProperties
}

type sdkLibraryImport struct {
@@ -824,16 +819,59 @@ type sdkLibraryImport struct {

	properties sdkLibraryImportProperties

	// Map from api scope to the scope specific property structure.
	scopeProperties map[*apiScope]*sdkLibraryScopeProperties

	commonToSdkLibraryAndImport
}

var _ SdkLibraryDependency = (*sdkLibraryImport)(nil)

// The type of a structure that contains a field of type sdkLibraryScopeProperties
// for each apiscope in allApiScopes, e.g. something like:
// struct {
//   Public sdkLibraryScopeProperties
//   System sdkLibraryScopeProperties
//   ...
// }
var allScopeStructType = createAllScopePropertiesStructType()

// Dynamically create a structure type for each apiscope in allApiScopes.
func createAllScopePropertiesStructType() reflect.Type {
	var fields []reflect.StructField
	for _, apiScope := range allApiScopes {
		field := reflect.StructField{
			Name: apiScope.fieldName,
			Type: reflect.TypeOf(sdkLibraryScopeProperties{}),
		}
		fields = append(fields, field)
	}

	return reflect.StructOf(fields)
}

// Create an instance of the scope specific structure type and return a map
// from apiscope to a pointer to each scope specific field.
func createPropertiesInstance() (interface{}, map[*apiScope]*sdkLibraryScopeProperties) {
	allScopePropertiesPtr := reflect.New(allScopeStructType)
	allScopePropertiesStruct := allScopePropertiesPtr.Elem()
	scopeProperties := make(map[*apiScope]*sdkLibraryScopeProperties)

	for _, apiScope := range allApiScopes {
		field := allScopePropertiesStruct.FieldByName(apiScope.fieldName)
		scopeProperties[apiScope] = field.Addr().Interface().(*sdkLibraryScopeProperties)
	}

	return allScopePropertiesPtr.Interface(), scopeProperties
}

// java_sdk_library_import imports a prebuilt java_sdk_library.
func sdkLibraryImportFactory() android.Module {
	module := &sdkLibraryImport{}

	module.AddProperties(&module.properties)
	allScopeProperties, scopeToProperties := createPropertiesInstance()
	module.scopeProperties = scopeToProperties
	module.AddProperties(&module.properties, allScopeProperties)

	android.InitPrebuiltModule(module, &[]string{""})
	InitJavaModule(module, android.HostAndDeviceSupported)
@@ -857,7 +895,7 @@ func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookConte
		module.prebuilt.ForcePrefer()
	}

	for apiScope, scopeProperties := range module.scopeProperties() {
	for apiScope, scopeProperties := range module.scopeProperties {
		if len(scopeProperties.Jars) == 0 {
			continue
		}
@@ -907,16 +945,8 @@ func (module *sdkLibraryImport) createInternalModules(mctx android.LoadHookConte
	*javaSdkLibraries = append(*javaSdkLibraries, module.BaseModuleName())
}

func (module *sdkLibraryImport) scopeProperties() map[*apiScope]*sdkLibraryScopeProperties {
	p := make(map[*apiScope]*sdkLibraryScopeProperties)
	p[apiScopePublic] = &module.properties.Public
	p[apiScopeSystem] = &module.properties.System
	p[apiScopeTest] = &module.properties.Test
	return p
}

func (module *sdkLibraryImport) DepsMutator(ctx android.BottomUpMutatorContext) {
	for apiScope, scopeProperties := range module.scopeProperties() {
	for apiScope, scopeProperties := range module.scopeProperties {
		if len(scopeProperties.Jars) == 0 {
			continue
		}