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" |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | type DeviceHostConverter struct { |
| 25 | android.ModuleBase |
| 26 | android.DefaultableModuleBase |
| 27 | |
| 28 | properties DeviceHostConverterProperties |
| 29 | |
| 30 | headerJars android.Paths |
| 31 | implementationJars android.Paths |
| 32 | implementationAndResourceJars android.Paths |
| 33 | resourceJars android.Paths |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 34 | |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 35 | srcJarArgs []string |
| 36 | srcJarDeps android.Paths |
| 37 | |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 38 | combinedHeaderJar android.Path |
| 39 | combinedImplementationJar android.Path |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | type DeviceHostConverterProperties struct { |
| 43 | // List of modules whose contents will be visible to modules that depend on this module. |
| 44 | Libs []string |
| 45 | } |
| 46 | |
| 47 | type DeviceForHost struct { |
| 48 | DeviceHostConverter |
| 49 | } |
| 50 | |
| 51 | // java_device_for_host makes the classes.jar output of a device java_library module available to host |
| 52 | // java_library modules. |
| 53 | // |
Jaewoong Jung | 707d788 | 2019-05-13 15:11:23 -0700 | [diff] [blame] | 54 | // It is rarely necessary, and its usage is restricted to a few whitelisted projects. |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 55 | func DeviceForHostFactory() android.Module { |
| 56 | module := &DeviceForHost{} |
| 57 | |
| 58 | module.AddProperties(&module.properties) |
| 59 | |
| 60 | InitJavaModule(module, android.HostSupported) |
| 61 | return module |
| 62 | } |
| 63 | |
| 64 | type HostForDevice struct { |
| 65 | DeviceHostConverter |
| 66 | } |
| 67 | |
| 68 | // java_host_for_device makes the classes.jar output of a host java_library module available to device |
| 69 | // java_library modules. |
| 70 | // |
Jaewoong Jung | 707d788 | 2019-05-13 15:11:23 -0700 | [diff] [blame] | 71 | // It is rarely necessary, and its usage is restricted to a few whitelisted projects. |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 72 | func HostForDeviceFactory() android.Module { |
| 73 | module := &HostForDevice{} |
| 74 | |
| 75 | module.AddProperties(&module.properties) |
| 76 | |
| 77 | InitJavaModule(module, android.DeviceSupported) |
| 78 | return module |
| 79 | } |
| 80 | |
| 81 | var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"} |
| 82 | |
| 83 | func (d *DeviceForHost) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame^] | 84 | ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), |
| 85 | deviceHostConverterDepTag, d.properties.Libs...) |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | func (d *HostForDevice) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame^] | 89 | ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), |
| 90 | deviceHostConverterDepTag, d.properties.Libs...) |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | func (d *DeviceHostConverter) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 94 | if len(d.properties.Libs) < 1 { |
| 95 | ctx.PropertyErrorf("libs", "at least one dependency is required") |
| 96 | } |
| 97 | |
| 98 | ctx.VisitDirectDepsWithTag(deviceHostConverterDepTag, func(m android.Module) { |
| 99 | if dep, ok := m.(Dependency); ok { |
| 100 | d.headerJars = append(d.headerJars, dep.HeaderJars()...) |
| 101 | d.implementationJars = append(d.implementationJars, dep.ImplementationJars()...) |
| 102 | d.implementationAndResourceJars = append(d.implementationAndResourceJars, dep.ImplementationAndResourcesJars()...) |
| 103 | d.resourceJars = append(d.resourceJars, dep.ResourceJars()...) |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 104 | |
| 105 | srcJarArgs, srcJarDeps := dep.SrcJarArgs() |
| 106 | d.srcJarArgs = append(d.srcJarArgs, srcJarArgs...) |
| 107 | d.srcJarDeps = append(d.srcJarDeps, 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 |
| 120 | } else { |
| 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 |
| 129 | } else { |
| 130 | d.combinedHeaderJar = d.headerJars[0] |
| 131 | } |
| 132 | |
Colin Cross | 3d7c982 | 2019-03-01 13:46:24 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | var _ Dependency = (*DeviceHostConverter)(nil) |
| 136 | |
| 137 | func (d *DeviceHostConverter) HeaderJars() android.Paths { |
| 138 | return d.headerJars |
| 139 | } |
| 140 | |
| 141 | func (d *DeviceHostConverter) ImplementationJars() android.Paths { |
| 142 | return d.implementationJars |
| 143 | } |
| 144 | |
| 145 | func (d *DeviceHostConverter) ResourceJars() android.Paths { |
| 146 | return d.resourceJars |
| 147 | } |
| 148 | |
| 149 | func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths { |
| 150 | return d.implementationAndResourceJars |
| 151 | } |
| 152 | |
| 153 | func (d *DeviceHostConverter) DexJar() android.Path { |
| 154 | return nil |
| 155 | } |
| 156 | |
| 157 | func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths { |
| 158 | return nil |
| 159 | } |
| 160 | |
| 161 | func (d *DeviceHostConverter) ExportedSdkLibs() []string { |
| 162 | return nil |
| 163 | } |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 164 | |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 165 | func (d *DeviceHostConverter) SrcJarArgs() ([]string, android.Paths) { |
| 166 | return d.srcJarArgs, d.srcJarDeps |
| 167 | } |
| 168 | |
Colin Cross | 28c3eb6 | 2019-04-23 22:04:46 -0700 | [diff] [blame] | 169 | func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData { |
| 170 | return android.AndroidMkData{ |
| 171 | Class: "JAVA_LIBRARIES", |
| 172 | OutputFile: android.OptionalPathForPath(d.combinedImplementationJar), |
| 173 | Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk", |
| 174 | Extra: []android.AndroidMkExtraFunc{ |
| 175 | func(w io.Writer, outputFile android.Path) { |
| 176 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true") |
| 177 | fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String()) |
| 178 | fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String()) |
| 179 | }, |
| 180 | }, |
| 181 | } |
| 182 | } |