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, |
| 146 | }) |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 147 | |
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 | |
| 150 | func (d *DeviceHostConverter) HeaderJars() android.Paths { |
| 151 | return d.headerJars |
| 152 | } |
| 153 | |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 154 | func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths { |
| 155 | return d.implementationAndResourceJars |
| 156 | } |
| 157 | |
Ulyana Trafimovich | 5539e7b | 2020-06-04 14:08:17 +0000 | [diff] [blame] | 158 | func (d *DeviceHostConverter) DexJarBuildPath() android.Path { |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 159 | return nil |
| 160 | } |
| 161 | |
Ulya Trafimovich | 9f3052c | 2020-06-09 14:31:19 +0100 | [diff] [blame] | 162 | func (d *DeviceHostConverter) DexJarInstallPath() android.Path { |
| 163 | return nil |
| 164 | } |
| 165 | |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 166 | func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths { |
| 167 | return nil |
| 168 | } |
| 169 | |
Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 170 | func (d *DeviceHostConverter) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap { |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 171 | return nil |
| 172 | } |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 173 | |
Jiyong Park | 618922e | 2020-01-08 13:35:43 +0900 | [diff] [blame] | 174 | func (d *DeviceHostConverter) JacocoReportClassesFile() android.Path { |
| 175 | return nil |
| 176 | } |
| 177 | |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 178 | func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData { |
| 179 | return android.AndroidMkData{ |
| 180 | Class: "JAVA_LIBRARIES", |
| 181 | OutputFile: android.OptionalPathForPath(d.combinedImplementationJar), |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 182 | // Make does not support Windows Java modules |
| 183 | Disabled: d.Os() == android.Windows, |
| 184 | Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk", |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 185 | Extra: []android.AndroidMkExtraFunc{ |
| 186 | func(w io.Writer, outputFile android.Path) { |
| 187 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true") |
| 188 | fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String()) |
| 189 | fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String()) |
| 190 | }, |
| 191 | }, |
| 192 | } |
| 193 | } |
Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 194 | |
| 195 | type bazelDeviceHostConverterAttributes struct { |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame^] | 196 | Exports bazel.LabelListAttribute |
Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | func (d *DeviceHostConverter) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
| 200 | ctx.CreateBazelTargetModule( |
| 201 | bazel.BazelTargetModuleProperties{ |
| 202 | Rule_class: "java_host_for_device", |
| 203 | Bzl_load_location: "//build/bazel/rules/java:host_for_device.bzl", |
| 204 | }, |
| 205 | android.CommonAttributes{Name: d.Name()}, |
| 206 | &bazelDeviceHostConverterAttributes{ |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame^] | 207 | Exports: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, d.properties.Libs)), |
Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 208 | }, |
| 209 | ) |
Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 210 | neverLinkAttrs := &javaLibraryAttributes{ |
| 211 | Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + d.Name()}), |
Romain Jobredeaux | 2eef2e1 | 2023-02-24 12:07:08 -0500 | [diff] [blame^] | 212 | Neverlink: bazel.BoolAttribute{Value: proptools.BoolPtr(true)}, |
| 213 | javaCommonAttributes: &javaCommonAttributes{ |
| 214 | Sdk_version: bazel.StringAttribute{Value: proptools.StringPtr("none")}, |
| 215 | }, |
Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame] | 216 | } |
| 217 | ctx.CreateBazelTargetModule( |
| 218 | javaLibraryBazelTargetModuleProperties(), |
| 219 | android.CommonAttributes{Name: d.Name() + "-neverlink"}, |
| 220 | neverLinkAttrs) |
| 221 | |
| 222 | } |