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