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