| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 1 | // Copyright 2019 Google Inc. All rights reserved. | 
 | 2 | // | 
 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 4 | // you may not use this file except in compliance with the License. | 
 | 5 | // You may obtain a copy of the License at | 
 | 6 | // | 
 | 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
 | 8 | // | 
 | 9 | // Unless required by applicable law or agreed to in writing, software | 
 | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
 | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 12 | // See the License for the specific language governing permissions and | 
 | 13 | // limitations under the License. | 
 | 14 |  | 
 | 15 | package java | 
 | 16 |  | 
 | 17 | import ( | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 18 | 	"fmt" | 
 | 19 | 	"io" | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 20 |  | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 21 | 	"android/soong/android" | 
| Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 22 | 	"android/soong/bazel" | 
| Ulya Trafimovich | 31e444e | 2020-08-14 17:32:16 +0100 | [diff] [blame] | 23 | 	"android/soong/dexpreopt" | 
| Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 24 |  | 
 | 25 | 	"github.com/google/blueprint/proptools" | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 26 | ) | 
 | 27 |  | 
 | 28 | type DeviceHostConverter struct { | 
 | 29 | 	android.ModuleBase | 
 | 30 | 	android.DefaultableModuleBase | 
| Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 31 | 	android.BazelModuleBase | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 32 |  | 
 | 33 | 	properties DeviceHostConverterProperties | 
 | 34 |  | 
 | 35 | 	headerJars                    android.Paths | 
 | 36 | 	implementationJars            android.Paths | 
 | 37 | 	implementationAndResourceJars android.Paths | 
 | 38 | 	resourceJars                  android.Paths | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 39 |  | 
| Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 40 | 	srcJarArgs []string | 
 | 41 | 	srcJarDeps android.Paths | 
 | 42 |  | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 43 | 	combinedHeaderJar         android.Path | 
 | 44 | 	combinedImplementationJar android.Path | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 45 | } | 
 | 46 |  | 
 | 47 | type DeviceHostConverterProperties struct { | 
 | 48 | 	// List of modules whose contents will be visible to modules that depend on this module. | 
 | 49 | 	Libs []string | 
 | 50 | } | 
 | 51 |  | 
 | 52 | type DeviceForHost struct { | 
 | 53 | 	DeviceHostConverter | 
 | 54 | } | 
 | 55 |  | 
 | 56 | // java_device_for_host makes the classes.jar output of a device java_library module available to host | 
 | 57 | // java_library modules. | 
 | 58 | // | 
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 59 | // It is rarely necessary, and its usage is restricted to a few allowed projects. | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 60 | func DeviceForHostFactory() android.Module { | 
 | 61 | 	module := &DeviceForHost{} | 
 | 62 |  | 
 | 63 | 	module.AddProperties(&module.properties) | 
 | 64 |  | 
 | 65 | 	InitJavaModule(module, android.HostSupported) | 
 | 66 | 	return module | 
 | 67 | } | 
 | 68 |  | 
 | 69 | type HostForDevice struct { | 
 | 70 | 	DeviceHostConverter | 
 | 71 | } | 
 | 72 |  | 
 | 73 | // java_host_for_device makes the classes.jar output of a host java_library module available to device | 
 | 74 | // java_library modules. | 
 | 75 | // | 
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 76 | // It is rarely necessary, and its usage is restricted to a few allowed projects. | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 77 | func HostForDeviceFactory() android.Module { | 
 | 78 | 	module := &HostForDevice{} | 
 | 79 |  | 
 | 80 | 	module.AddProperties(&module.properties) | 
 | 81 |  | 
 | 82 | 	InitJavaModule(module, android.DeviceSupported) | 
| Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 83 | 	android.InitBazelModule(module) | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 84 | 	return module | 
 | 85 | } | 
 | 86 |  | 
 | 87 | var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"} | 
 | 88 |  | 
 | 89 | func (d *DeviceForHost) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 90 | 	ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), | 
 | 91 | 		deviceHostConverterDepTag, d.properties.Libs...) | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 92 | } | 
 | 93 |  | 
 | 94 | func (d *HostForDevice) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 95 | 	ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), | 
 | 96 | 		deviceHostConverterDepTag, d.properties.Libs...) | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 97 | } | 
 | 98 |  | 
 | 99 | func (d *DeviceHostConverter) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
 | 100 | 	if len(d.properties.Libs) < 1 { | 
 | 101 | 		ctx.PropertyErrorf("libs", "at least one dependency is required") | 
 | 102 | 	} | 
 | 103 |  | 
 | 104 | 	ctx.VisitDirectDepsWithTag(deviceHostConverterDepTag, func(m android.Module) { | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 105 | 		if ctx.OtherModuleHasProvider(m, JavaInfoProvider) { | 
 | 106 | 			dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo) | 
 | 107 | 			d.headerJars = append(d.headerJars, dep.HeaderJars...) | 
 | 108 | 			d.implementationJars = append(d.implementationJars, dep.ImplementationJars...) | 
 | 109 | 			d.implementationAndResourceJars = append(d.implementationAndResourceJars, dep.ImplementationAndResourcesJars...) | 
 | 110 | 			d.resourceJars = append(d.resourceJars, dep.ResourceJars...) | 
| Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 111 |  | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 112 | 			d.srcJarArgs = append(d.srcJarArgs, dep.SrcJarArgs...) | 
 | 113 | 			d.srcJarDeps = append(d.srcJarDeps, dep.SrcJarDeps...) | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 114 | 		} else { | 
 | 115 | 			ctx.PropertyErrorf("libs", "module %q cannot be used as a dependency", ctx.OtherModuleName(m)) | 
 | 116 | 		} | 
 | 117 | 	}) | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 118 |  | 
 | 119 | 	jarName := ctx.ModuleName() + ".jar" | 
 | 120 |  | 
 | 121 | 	if len(d.implementationAndResourceJars) > 1 { | 
 | 122 | 		outputFile := android.PathForModuleOut(ctx, "combined", jarName) | 
 | 123 | 		TransformJarsToJar(ctx, outputFile, "combine", d.implementationAndResourceJars, | 
 | 124 | 			android.OptionalPath{}, false, nil, nil) | 
 | 125 | 		d.combinedImplementationJar = outputFile | 
| Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 126 | 	} else if len(d.implementationAndResourceJars) == 1 { | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 127 | 		d.combinedImplementationJar = d.implementationAndResourceJars[0] | 
 | 128 | 	} | 
 | 129 |  | 
 | 130 | 	if len(d.headerJars) > 1 { | 
 | 131 | 		outputFile := android.PathForModuleOut(ctx, "turbine-combined", jarName) | 
 | 132 | 		TransformJarsToJar(ctx, outputFile, "turbine combine", d.headerJars, | 
| Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 133 | 			android.OptionalPath{}, false, nil, []string{"META-INF/TRANSITIVE"}) | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 134 | 		d.combinedHeaderJar = outputFile | 
| Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 135 | 	} else if len(d.headerJars) == 1 { | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 136 | 		d.combinedHeaderJar = d.headerJars[0] | 
 | 137 | 	} | 
 | 138 |  | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 139 | 	ctx.SetProvider(JavaInfoProvider, JavaInfo{ | 
 | 140 | 		HeaderJars:                     d.headerJars, | 
 | 141 | 		ImplementationAndResourcesJars: d.implementationAndResourceJars, | 
 | 142 | 		ImplementationJars:             d.implementationJars, | 
 | 143 | 		ResourceJars:                   d.resourceJars, | 
 | 144 | 		SrcJarArgs:                     d.srcJarArgs, | 
 | 145 | 		SrcJarDeps:                     d.srcJarDeps, | 
| Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 146 | 		// TODO: Not sure if aconfig flags that have been moved between device and host variants | 
 | 147 | 		// make sense. | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 148 | 	}) | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 149 |  | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 150 | } | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 151 |  | 
 | 152 | func (d *DeviceHostConverter) HeaderJars() android.Paths { | 
 | 153 | 	return d.headerJars | 
 | 154 | } | 
 | 155 |  | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 156 | func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths { | 
 | 157 | 	return d.implementationAndResourceJars | 
 | 158 | } | 
 | 159 |  | 
| Ulyana Trafimovich | 5539e7b | 2020-06-04 14:08:17 +0000 | [diff] [blame] | 160 | func (d *DeviceHostConverter) DexJarBuildPath() android.Path { | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 161 | 	return nil | 
 | 162 | } | 
 | 163 |  | 
| Ulya Trafimovich | 9f3052c | 2020-06-09 14:31:19 +0100 | [diff] [blame] | 164 | func (d *DeviceHostConverter) DexJarInstallPath() android.Path { | 
 | 165 | 	return nil | 
 | 166 | } | 
 | 167 |  | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 168 | func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths { | 
 | 169 | 	return nil | 
 | 170 | } | 
 | 171 |  | 
| Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 172 | func (d *DeviceHostConverter) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap { | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 173 | 	return nil | 
 | 174 | } | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 175 |  | 
| Jiyong Park | 618922e | 2020-01-08 13:35:43 +0900 | [diff] [blame] | 176 | func (d *DeviceHostConverter) JacocoReportClassesFile() android.Path { | 
 | 177 | 	return nil | 
 | 178 | } | 
 | 179 |  | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 180 | func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData { | 
 | 181 | 	return android.AndroidMkData{ | 
 | 182 | 		Class:      "JAVA_LIBRARIES", | 
 | 183 | 		OutputFile: android.OptionalPathForPath(d.combinedImplementationJar), | 
| Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 184 | 		// Make does not support Windows Java modules | 
 | 185 | 		Disabled: d.Os() == android.Windows, | 
 | 186 | 		Include:  "$(BUILD_SYSTEM)/soong_java_prebuilt.mk", | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 187 | 		Extra: []android.AndroidMkExtraFunc{ | 
 | 188 | 			func(w io.Writer, outputFile android.Path) { | 
 | 189 | 				fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true") | 
 | 190 | 				fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String()) | 
 | 191 | 				fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String()) | 
 | 192 | 			}, | 
 | 193 | 		}, | 
 | 194 | 	} | 
 | 195 | } | 
| Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 196 |  | 
 | 197 | type bazelDeviceHostConverterAttributes struct { | 
| Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 198 | 	Exports bazel.LabelListAttribute | 
| Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 199 | } | 
 | 200 |  | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 201 | func (d *DeviceHostConverter) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) { | 
| Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 202 | 	ctx.CreateBazelTargetModule( | 
 | 203 | 		bazel.BazelTargetModuleProperties{ | 
 | 204 | 			Rule_class:        "java_host_for_device", | 
 | 205 | 			Bzl_load_location: "//build/bazel/rules/java:host_for_device.bzl", | 
 | 206 | 		}, | 
 | 207 | 		android.CommonAttributes{Name: d.Name()}, | 
 | 208 | 		&bazelDeviceHostConverterAttributes{ | 
| Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 209 | 			Exports: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, d.properties.Libs)), | 
| Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 210 | 		}, | 
 | 211 | 	) | 
| Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 212 | 	neverLinkAttrs := &javaLibraryAttributes{ | 
 | 213 | 		Exports:   bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + d.Name()}), | 
| Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame] | 214 | 		Neverlink: bazel.BoolAttribute{Value: proptools.BoolPtr(true)}, | 
 | 215 | 		javaCommonAttributes: &javaCommonAttributes{ | 
 | 216 | 			Sdk_version: bazel.StringAttribute{Value: proptools.StringPtr("none")}, | 
 | 217 | 		}, | 
| Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 218 | 	} | 
 | 219 | 	ctx.CreateBazelTargetModule( | 
 | 220 | 		javaLibraryBazelTargetModuleProperties(), | 
 | 221 | 		android.CommonAttributes{Name: d.Name() + "-neverlink"}, | 
 | 222 | 		neverLinkAttrs) | 
 | 223 |  | 
 | 224 | } |