diff options
Diffstat (limited to 'java/java.go')
| -rw-r--r-- | java/java.go | 137 | 
1 files changed, 113 insertions, 24 deletions
diff --git a/java/java.go b/java/java.go index aa9f936d0..a026610c2 100644 --- a/java/java.go +++ b/java/java.go @@ -273,6 +273,8 @@ type JavaInfo struct {  	// JacocoReportClassesFile is the path to a jar containing uninstrumented classes that will be  	// instrumented by jacoco.  	JacocoReportClassesFile android.Path + +	// TODO: Add device config declarations here?  }  var JavaInfoProvider = blueprint.NewProvider(JavaInfo{}) @@ -936,6 +938,10 @@ type TestOptions struct {  	// Extra <option> tags to add to the auto generated test xml file. The "key"  	// is optional in each of these.  	Tradefed_options []tradefed.Option + +	// Extra <option> tags to add to the auto generated test xml file under the test runner, e.g., AndroidJunitTest. +	// The "key" is optional in each of these. +	Test_runner_options []tradefed.Option  }  type testProperties struct { @@ -1218,6 +1224,7 @@ func (j *Test) generateAndroidBuildActionsWithConfig(ctx android.ModuleContext,  		TestSuites:              j.testProperties.Test_suites,  		Config:                  configs,  		OptionsForAutogenerated: j.testProperties.Test_options.Tradefed_options, +		TestRunnerOptions:       j.testProperties.Test_options.Test_runner_options,  		AutoGenConfig:           j.testProperties.Auto_gen_config,  		UnitTest:                j.testProperties.Test_options.Unit_test,  		DeviceTemplate:          "${JavaTestConfigTemplate}", @@ -1416,6 +1423,8 @@ func TestHostFactory() android.Module {  		nil,  		nil) +	android.InitBazelModule(module) +  	InitJavaModuleMultiTargets(module, android.HostSupported)  	return module @@ -1728,6 +1737,14 @@ func metalavaStubCmd(ctx android.ModuleContext, rule *android.RuleBuilder,  		FlagWithArg("--hide ", "InvalidNullabilityOverride").  		FlagWithArg("--hide ", "ChangedDefault") +	// Force metalava to ignore classes on the classpath when an API file contains missing classes. +	// See b/285140653 for more information. +	cmd.FlagWithArg("--api-class-resolution ", "api") + +	// Force metalava to sort overloaded methods by their order in the source code. +	// See b/285312164 for more information. +	cmd.FlagWithArg("--api-overloaded-method-order ", "source") +  	return cmd  } @@ -2798,6 +2815,14 @@ type bp2BuildJavaInfo struct {  	hasKotlin bool  } +// Replaces //a/b/my_xsd_config with //a/b/my_xsd_config-java +func xsdConfigJavaTarget(ctx android.BazelConversionPathContext, mod blueprint.Module) string { +	callback := func(xsd android.XsdConfigBp2buildTargets) string { +		return xsd.JavaBp2buildTargetName() +	} +	return android.XsdConfigBp2buildTarget(ctx, mod, callback) +} +  // convertLibraryAttrsBp2Build returns a javaCommonAttributes struct with  // converted attributes shared across java_* modules and a bp2BuildJavaInfo struct  // which has other non-attribute information needed for bp2build conversion @@ -2812,8 +2837,15 @@ func (m *Library) convertLibraryAttrsBp2Build(ctx android.TopDownMutatorContext)  	for axis, configToProps := range archVariantProps {  		for config, _props := range configToProps {  			if archProps, ok := _props.(*CommonProperties); ok { -				archSrcs := android.BazelLabelForModuleSrcExcludes(ctx, archProps.Srcs, archProps.Exclude_srcs) +				srcsNonXsd, srcsXsd := android.PartitionXsdSrcs(ctx, archProps.Srcs) +				excludeSrcsNonXsd, _ := android.PartitionXsdSrcs(ctx, archProps.Exclude_srcs) +				archSrcs := android.BazelLabelForModuleSrcExcludes(ctx, srcsNonXsd, excludeSrcsNonXsd)  				srcs.SetSelectValue(axis, config, archSrcs) + +				// Add to static deps +				xsdJavaConfigLibraryLabels := android.BazelLabelForModuleDepsWithFn(ctx, srcsXsd, xsdConfigJavaTarget) +				staticDeps.Append(xsdJavaConfigLibraryLabels) +  			}  		}  	} @@ -3074,24 +3106,15 @@ func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) {  	// Attribute jvm_flags  	var jvmFlags bazel.StringListAttribute  	if m.binaryProperties.Jni_libs != nil { -		jniLibPackages := map[string]bool{} -		for _, jniLibLabel := range android.BazelLabelForModuleDeps(ctx, m.binaryProperties.Jni_libs).Includes { -			jniLibPackage := jniLibLabel.Label -			indexOfColon := strings.Index(jniLibLabel.Label, ":") -			if indexOfColon > 0 { -				// JNI lib from other package -				jniLibPackage = jniLibLabel.Label[2:indexOfColon] -			} else if indexOfColon == 0 { -				// JNI lib in the same package of java_binary -				packageOfCurrentModule := m.GetBazelLabel(ctx, m) -				jniLibPackage = packageOfCurrentModule[2:strings.Index(packageOfCurrentModule, ":")] -			} -			if _, inMap := jniLibPackages[jniLibPackage]; !inMap { -				jniLibPackages[jniLibPackage] = true +		jniLibPackages := []string{} +		for _, jniLib := range m.binaryProperties.Jni_libs { +			if jniLibModule, exists := ctx.ModuleFromName(jniLib); exists { +				otherDir := ctx.OtherModuleDir(jniLibModule) +				jniLibPackages = append(jniLibPackages, filepath.Join(otherDir, jniLib))  			}  		}  		jniLibPaths := []string{} -		for jniLibPackage, _ := range jniLibPackages { +		for _, jniLibPackage := range jniLibPackages {  			// See cs/f:.*/third_party/bazel/.*java_stub_template.txt for the use of RUNPATH  			jniLibPaths = append(jniLibPaths, "$${RUNPATH}"+jniLibPackage)  		} @@ -3114,23 +3137,89 @@ func javaBinaryHostBp2Build(ctx android.TopDownMutatorContext, m *Binary) {  		return  	} -	libName := m.Name() + "_lib" +	libInfo := libraryCreationInfo{ +		deps:      deps, +		attrs:     commonAttrs, +		baseName:  m.Name(), +		hasKotlin: bp2BuildInfo.hasKotlin, +	} +	libName := createLibraryTarget(ctx, libInfo) +	binAttrs.Runtime_deps.Add(&bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + libName}}) + +	// Create the BazelTargetModule. +	ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, binAttrs) +} + +type javaTestHostAttributes struct { +	*javaCommonAttributes +	Deps         bazel.LabelListAttribute +	Runtime_deps bazel.LabelListAttribute +} + +// javaTestHostBp2Build is for java_test_host bp2build. +func javaTestHostBp2Build(ctx android.TopDownMutatorContext, m *TestHost) { +	commonAttrs, bp2BuildInfo := m.convertLibraryAttrsBp2Build(ctx) +	depLabels := bp2BuildInfo.DepLabels + +	deps := depLabels.Deps +	deps.Append(depLabels.StaticDeps) + +	var runtimeDeps bazel.LabelListAttribute +	attrs := &javaTestHostAttributes{ +		Runtime_deps: runtimeDeps, +	} +	props := bazel.BazelTargetModuleProperties{ +		Rule_class:        "java_test", +		Bzl_load_location: "//build/bazel/rules/java:test.bzl", +	} + +	if commonAttrs.Srcs.IsEmpty() { +		// if there are no sources, then the dependencies can only be used at runtime +		attrs.Runtime_deps = deps +		attrs.javaCommonAttributes = commonAttrs +		ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) +		return +	} + +	libInfo := libraryCreationInfo{ +		deps:      deps, +		attrs:     commonAttrs, +		baseName:  m.Name(), +		hasKotlin: bp2BuildInfo.hasKotlin, +	} +	libName := createLibraryTarget(ctx, libInfo) +	attrs.Runtime_deps.Add(&bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + libName}}) + +	// Create the BazelTargetModule. +	ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) +} + +// libraryCreationInfo encapsulates the info needed to create java_library target from +// java_binary_host or java_test_host. +type libraryCreationInfo struct { +	deps      bazel.LabelListAttribute +	attrs     *javaCommonAttributes +	baseName  string +	hasKotlin bool +} + +// helper function that creates java_library target from java_binary_host or java_test_host, +// and returns the library target name, +func createLibraryTarget(ctx android.TopDownMutatorContext, libInfo libraryCreationInfo) string { +	libName := libInfo.baseName + "_lib"  	var libProps bazel.BazelTargetModuleProperties -	if bp2BuildInfo.hasKotlin { +	if libInfo.hasKotlin {  		libProps = ktJvmLibraryBazelTargetModuleProperties()  	} else {  		libProps = javaLibraryBazelTargetModuleProperties()  	}  	libAttrs := &javaLibraryAttributes{ -		Deps:                 deps, -		javaCommonAttributes: commonAttrs, +		Deps:                 libInfo.deps, +		javaCommonAttributes: libInfo.attrs,  	}  	ctx.CreateBazelTargetModule(libProps, android.CommonAttributes{Name: libName}, libAttrs) -	binAttrs.Runtime_deps.Add(&bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + libName}}) - -	// Create the BazelTargetModule. -	ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, binAttrs) +	return libName  }  type bazelJavaImportAttributes struct {  |