| 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" | 
| Ulya Trafimovich | 31e444e | 2020-08-14 17:32:16 +0100 | [diff] [blame] | 22 | 	"android/soong/dexpreopt" | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 23 |  | 
 | 24 | 	"github.com/google/blueprint/depset" | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 25 | ) | 
 | 26 |  | 
 | 27 | type DeviceHostConverter struct { | 
 | 28 | 	android.ModuleBase | 
 | 29 | 	android.DefaultableModuleBase | 
 | 30 |  | 
 | 31 | 	properties DeviceHostConverterProperties | 
 | 32 |  | 
 | 33 | 	headerJars                    android.Paths | 
 | 34 | 	implementationJars            android.Paths | 
 | 35 | 	implementationAndResourceJars android.Paths | 
 | 36 | 	resourceJars                  android.Paths | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 37 |  | 
| Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 38 | 	srcJarArgs []string | 
 | 39 | 	srcJarDeps android.Paths | 
 | 40 |  | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 41 | 	combinedHeaderJar         android.Path | 
 | 42 | 	combinedImplementationJar android.Path | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 43 | } | 
 | 44 |  | 
 | 45 | type DeviceHostConverterProperties struct { | 
 | 46 | 	// List of modules whose contents will be visible to modules that depend on this module. | 
 | 47 | 	Libs []string | 
 | 48 | } | 
 | 49 |  | 
 | 50 | type DeviceForHost struct { | 
 | 51 | 	DeviceHostConverter | 
 | 52 | } | 
 | 53 |  | 
 | 54 | // java_device_for_host makes the classes.jar output of a device java_library module available to host | 
 | 55 | // java_library modules. | 
 | 56 | // | 
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 57 | // 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] | 58 | func DeviceForHostFactory() android.Module { | 
 | 59 | 	module := &DeviceForHost{} | 
 | 60 |  | 
 | 61 | 	module.AddProperties(&module.properties) | 
 | 62 |  | 
 | 63 | 	InitJavaModule(module, android.HostSupported) | 
 | 64 | 	return module | 
 | 65 | } | 
 | 66 |  | 
 | 67 | type HostForDevice struct { | 
 | 68 | 	DeviceHostConverter | 
 | 69 | } | 
 | 70 |  | 
 | 71 | // java_host_for_device makes the classes.jar output of a host java_library module available to device | 
 | 72 | // java_library modules. | 
 | 73 | // | 
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 74 | // 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] | 75 | func HostForDeviceFactory() android.Module { | 
 | 76 | 	module := &HostForDevice{} | 
 | 77 |  | 
 | 78 | 	module.AddProperties(&module.properties) | 
 | 79 |  | 
 | 80 | 	InitJavaModule(module, android.DeviceSupported) | 
 | 81 | 	return module | 
 | 82 | } | 
 | 83 |  | 
 | 84 | var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"} | 
 | 85 |  | 
 | 86 | func (d *DeviceForHost) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 87 | 	ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), | 
 | 88 | 		deviceHostConverterDepTag, d.properties.Libs...) | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 89 | } | 
 | 90 |  | 
 | 91 | func (d *HostForDevice) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 92 | 	ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), | 
 | 93 | 		deviceHostConverterDepTag, d.properties.Libs...) | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 94 | } | 
 | 95 |  | 
 | 96 | func (d *DeviceHostConverter) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
 | 97 | 	if len(d.properties.Libs) < 1 { | 
 | 98 | 		ctx.PropertyErrorf("libs", "at least one dependency is required") | 
 | 99 | 	} | 
 | 100 |  | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 101 | 	var transitiveHeaderJars []depset.DepSet[android.Path] | 
 | 102 | 	var transitiveImplementationJars []depset.DepSet[android.Path] | 
 | 103 | 	var transitiveResourceJars []depset.DepSet[android.Path] | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 104 |  | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 105 | 	ctx.VisitDirectDepsWithTag(deviceHostConverterDepTag, func(m android.Module) { | 
| Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 106 | 		if dep, ok := android.OtherModuleProvider(ctx, m, JavaInfoProvider); ok { | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 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 | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 114 |  | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 115 | 			transitiveHeaderJars = append(transitiveHeaderJars, dep.TransitiveStaticLibsHeaderJars) | 
 | 116 | 			transitiveImplementationJars = append(transitiveImplementationJars, dep.TransitiveStaticLibsImplementationJars) | 
 | 117 | 			transitiveResourceJars = append(transitiveResourceJars, dep.TransitiveStaticLibsResourceJars) | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 118 | 		} else { | 
 | 119 | 			ctx.PropertyErrorf("libs", "module %q cannot be used as a dependency", ctx.OtherModuleName(m)) | 
 | 120 | 		} | 
 | 121 | 	}) | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 122 |  | 
 | 123 | 	jarName := ctx.ModuleName() + ".jar" | 
 | 124 |  | 
 | 125 | 	if len(d.implementationAndResourceJars) > 1 { | 
 | 126 | 		outputFile := android.PathForModuleOut(ctx, "combined", jarName) | 
 | 127 | 		TransformJarsToJar(ctx, outputFile, "combine", d.implementationAndResourceJars, | 
 | 128 | 			android.OptionalPath{}, false, nil, nil) | 
 | 129 | 		d.combinedImplementationJar = outputFile | 
| Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 130 | 	} else if len(d.implementationAndResourceJars) == 1 { | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 131 | 		d.combinedImplementationJar = d.implementationAndResourceJars[0] | 
 | 132 | 	} | 
 | 133 |  | 
 | 134 | 	if len(d.headerJars) > 1 { | 
 | 135 | 		outputFile := android.PathForModuleOut(ctx, "turbine-combined", jarName) | 
 | 136 | 		TransformJarsToJar(ctx, outputFile, "turbine combine", d.headerJars, | 
| Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 137 | 			android.OptionalPath{}, false, nil, []string{"META-INF/TRANSITIVE"}) | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 138 | 		d.combinedHeaderJar = outputFile | 
| Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 139 | 	} else if len(d.headerJars) == 1 { | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 140 | 		d.combinedHeaderJar = d.headerJars[0] | 
 | 141 | 	} | 
 | 142 |  | 
| Colin Cross | 7727c7f | 2024-07-18 15:36:32 -0700 | [diff] [blame] | 143 | 	android.SetProvider(ctx, JavaInfoProvider, &JavaInfo{ | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 144 | 		HeaderJars:                             d.headerJars, | 
 | 145 | 		LocalHeaderJars:                        d.headerJars, | 
| Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 146 | 		TransitiveStaticLibsHeaderJars:         depset.New(depset.PREORDER, nil, transitiveHeaderJars), | 
 | 147 | 		TransitiveStaticLibsImplementationJars: depset.New(depset.PREORDER, nil, transitiveImplementationJars), | 
 | 148 | 		TransitiveStaticLibsResourceJars:       depset.New(depset.PREORDER, nil, transitiveResourceJars), | 
| Colin Cross | c9b4f6b | 2024-07-26 15:25:46 -0700 | [diff] [blame] | 149 | 		ImplementationAndResourcesJars:         d.implementationAndResourceJars, | 
 | 150 | 		ImplementationJars:                     d.implementationJars, | 
 | 151 | 		ResourceJars:                           d.resourceJars, | 
 | 152 | 		SrcJarArgs:                             d.srcJarArgs, | 
 | 153 | 		SrcJarDeps:                             d.srcJarDeps, | 
 | 154 | 		StubsLinkType:                          Implementation, | 
| Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 155 | 		// TODO: Not sure if aconfig flags that have been moved between device and host variants | 
 | 156 | 		// make sense. | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 157 | 	}) | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 158 |  | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 159 | } | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 160 |  | 
 | 161 | func (d *DeviceHostConverter) HeaderJars() android.Paths { | 
 | 162 | 	return d.headerJars | 
 | 163 | } | 
 | 164 |  | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 165 | func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths { | 
 | 166 | 	return d.implementationAndResourceJars | 
 | 167 | } | 
 | 168 |  | 
| Spandan Das | 59a4a2b | 2024-01-09 21:35:56 +0000 | [diff] [blame] | 169 | func (d *DeviceHostConverter) DexJarBuildPath(ctx android.ModuleErrorfContext) android.Path { | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 170 | 	return nil | 
 | 171 | } | 
 | 172 |  | 
| Ulya Trafimovich | 9f3052c | 2020-06-09 14:31:19 +0100 | [diff] [blame] | 173 | func (d *DeviceHostConverter) DexJarInstallPath() android.Path { | 
 | 174 | 	return nil | 
 | 175 | } | 
 | 176 |  | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 177 | func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths { | 
 | 178 | 	return nil | 
 | 179 | } | 
 | 180 |  | 
| Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 181 | func (d *DeviceHostConverter) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap { | 
| Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 182 | 	return nil | 
 | 183 | } | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 184 |  | 
| Jiyong Park | 618922e | 2020-01-08 13:35:43 +0900 | [diff] [blame] | 185 | func (d *DeviceHostConverter) JacocoReportClassesFile() android.Path { | 
 | 186 | 	return nil | 
 | 187 | } | 
 | 188 |  | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 189 | func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData { | 
 | 190 | 	return android.AndroidMkData{ | 
 | 191 | 		Class:      "JAVA_LIBRARIES", | 
 | 192 | 		OutputFile: android.OptionalPathForPath(d.combinedImplementationJar), | 
| Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 193 | 		// Make does not support Windows Java modules | 
 | 194 | 		Disabled: d.Os() == android.Windows, | 
 | 195 | 		Include:  "$(BUILD_SYSTEM)/soong_java_prebuilt.mk", | 
| Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 196 | 		Extra: []android.AndroidMkExtraFunc{ | 
 | 197 | 			func(w io.Writer, outputFile android.Path) { | 
 | 198 | 				fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true") | 
 | 199 | 				fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String()) | 
 | 200 | 				fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String()) | 
 | 201 | 			}, | 
 | 202 | 		}, | 
 | 203 | 	} | 
 | 204 | } | 
| Spandan Das | f5ee86c | 2024-08-08 21:11:42 +0000 | [diff] [blame] | 205 |  | 
 | 206 | // implement the following interface for IDE completion. | 
 | 207 | var _ android.IDEInfo = (*DeviceHostConverter)(nil) | 
 | 208 |  | 
| Cole Faust | b36d31d | 2024-08-27 16:04:28 -0700 | [diff] [blame] | 209 | func (d *DeviceHostConverter) IDEInfo(ctx android.BaseModuleContext, ideInfo *android.IdeInfo) { | 
| Spandan Das | f5ee86c | 2024-08-08 21:11:42 +0000 | [diff] [blame] | 210 | 	ideInfo.Deps = append(ideInfo.Deps, d.properties.Libs...) | 
 | 211 | 	ideInfo.Libs = append(ideInfo.Libs, d.properties.Libs...) | 
 | 212 | } |