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" |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | type DeviceHostConverter struct { |
| 27 | android.ModuleBase |
| 28 | android.DefaultableModuleBase |
Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame^] | 29 | android.BazelModuleBase |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 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) |
Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame^] | 81 | android.InitBazelModule(module) |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 82 | return module |
| 83 | } |
| 84 | |
| 85 | var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"} |
| 86 | |
| 87 | func (d *DeviceForHost) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 88 | ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), |
| 89 | deviceHostConverterDepTag, d.properties.Libs...) |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | func (d *HostForDevice) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 93 | ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), |
| 94 | deviceHostConverterDepTag, d.properties.Libs...) |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | func (d *DeviceHostConverter) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 98 | if len(d.properties.Libs) < 1 { |
| 99 | ctx.PropertyErrorf("libs", "at least one dependency is required") |
| 100 | } |
| 101 | |
| 102 | ctx.VisitDirectDepsWithTag(deviceHostConverterDepTag, func(m android.Module) { |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 103 | if ctx.OtherModuleHasProvider(m, JavaInfoProvider) { |
| 104 | dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo) |
| 105 | d.headerJars = append(d.headerJars, dep.HeaderJars...) |
| 106 | d.implementationJars = append(d.implementationJars, dep.ImplementationJars...) |
| 107 | d.implementationAndResourceJars = append(d.implementationAndResourceJars, dep.ImplementationAndResourcesJars...) |
| 108 | d.resourceJars = append(d.resourceJars, dep.ResourceJars...) |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 109 | |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 110 | d.srcJarArgs = append(d.srcJarArgs, dep.SrcJarArgs...) |
| 111 | d.srcJarDeps = append(d.srcJarDeps, dep.SrcJarDeps...) |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 112 | } else { |
| 113 | ctx.PropertyErrorf("libs", "module %q cannot be used as a dependency", ctx.OtherModuleName(m)) |
| 114 | } |
| 115 | }) |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 116 | |
| 117 | jarName := ctx.ModuleName() + ".jar" |
| 118 | |
| 119 | if len(d.implementationAndResourceJars) > 1 { |
| 120 | outputFile := android.PathForModuleOut(ctx, "combined", jarName) |
| 121 | TransformJarsToJar(ctx, outputFile, "combine", d.implementationAndResourceJars, |
| 122 | android.OptionalPath{}, false, nil, nil) |
| 123 | d.combinedImplementationJar = outputFile |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 124 | } else if len(d.implementationAndResourceJars) == 1 { |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 125 | d.combinedImplementationJar = d.implementationAndResourceJars[0] |
| 126 | } |
| 127 | |
| 128 | if len(d.headerJars) > 1 { |
| 129 | outputFile := android.PathForModuleOut(ctx, "turbine-combined", jarName) |
| 130 | TransformJarsToJar(ctx, outputFile, "turbine combine", d.headerJars, |
Colin Cross | 6c6e6cd | 2019-05-08 14:30:12 -0700 | [diff] [blame] | 131 | android.OptionalPath{}, false, nil, []string{"META-INF/TRANSITIVE"}) |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 132 | d.combinedHeaderJar = outputFile |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 133 | } else if len(d.headerJars) == 1 { |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 134 | d.combinedHeaderJar = d.headerJars[0] |
| 135 | } |
| 136 | |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 137 | ctx.SetProvider(JavaInfoProvider, JavaInfo{ |
| 138 | HeaderJars: d.headerJars, |
| 139 | ImplementationAndResourcesJars: d.implementationAndResourceJars, |
| 140 | ImplementationJars: d.implementationJars, |
| 141 | ResourceJars: d.resourceJars, |
| 142 | SrcJarArgs: d.srcJarArgs, |
| 143 | SrcJarDeps: d.srcJarDeps, |
| 144 | }) |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 145 | |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 146 | } |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 147 | |
| 148 | func (d *DeviceHostConverter) HeaderJars() android.Paths { |
| 149 | return d.headerJars |
| 150 | } |
| 151 | |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 152 | func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths { |
| 153 | return d.implementationAndResourceJars |
| 154 | } |
| 155 | |
Ulyana Trafimovich | 5539e7b | 2020-06-04 14:08:17 +0000 | [diff] [blame] | 156 | func (d *DeviceHostConverter) DexJarBuildPath() android.Path { |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 157 | return nil |
| 158 | } |
| 159 | |
Ulya Trafimovich | 9f3052c | 2020-06-09 14:31:19 +0100 | [diff] [blame] | 160 | func (d *DeviceHostConverter) DexJarInstallPath() android.Path { |
| 161 | return nil |
| 162 | } |
| 163 | |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 164 | func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths { |
| 165 | return nil |
| 166 | } |
| 167 | |
Ulya Trafimovich | b23d28c | 2020-10-08 12:53:58 +0100 | [diff] [blame] | 168 | func (d *DeviceHostConverter) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap { |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 169 | return nil |
| 170 | } |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 171 | |
Jiyong Park | 618922e | 2020-01-08 13:35:43 +0900 | [diff] [blame] | 172 | func (d *DeviceHostConverter) JacocoReportClassesFile() android.Path { |
| 173 | return nil |
| 174 | } |
| 175 | |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 176 | func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData { |
| 177 | return android.AndroidMkData{ |
| 178 | Class: "JAVA_LIBRARIES", |
| 179 | OutputFile: android.OptionalPathForPath(d.combinedImplementationJar), |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 180 | // Make does not support Windows Java modules |
| 181 | Disabled: d.Os() == android.Windows, |
| 182 | Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk", |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 183 | Extra: []android.AndroidMkExtraFunc{ |
| 184 | func(w io.Writer, outputFile android.Path) { |
| 185 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true") |
| 186 | fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String()) |
| 187 | fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String()) |
| 188 | }, |
| 189 | }, |
| 190 | } |
| 191 | } |
Romain Jobredeaux | e7370ea | 2023-04-04 10:30:14 -0400 | [diff] [blame^] | 192 | |
| 193 | type bazelDeviceHostConverterAttributes struct { |
| 194 | Deps bazel.LabelListAttribute |
| 195 | } |
| 196 | |
| 197 | func (d *DeviceHostConverter) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
| 198 | ctx.CreateBazelTargetModule( |
| 199 | bazel.BazelTargetModuleProperties{ |
| 200 | Rule_class: "java_host_for_device", |
| 201 | Bzl_load_location: "//build/bazel/rules/java:host_for_device.bzl", |
| 202 | }, |
| 203 | android.CommonAttributes{Name: d.Name()}, |
| 204 | &bazelDeviceHostConverterAttributes{ |
| 205 | Deps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, d.properties.Libs)), |
| 206 | }, |
| 207 | ) |
| 208 | neverlinkProp := true |
| 209 | neverLinkAttrs := &javaLibraryAttributes{ |
| 210 | Exports: bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + d.Name()}), |
| 211 | Neverlink: bazel.BoolAttribute{Value: &neverlinkProp}, |
| 212 | } |
| 213 | ctx.CreateBazelTargetModule( |
| 214 | javaLibraryBazelTargetModuleProperties(), |
| 215 | android.CommonAttributes{Name: d.Name() + "-neverlink"}, |
| 216 | neverLinkAttrs) |
| 217 | |
| 218 | } |