blob: b92f4d73e3aaaea94c29c3c5a6b26504ae107167 [file] [log] [blame]
Colin Cross3d7c9822019-03-01 13:46:24 -08001// 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
15package java
16
17import (
Colin Cross28c3eb62019-04-23 22:04:46 -070018 "fmt"
19 "io"
Colin Cross3d7c9822019-03-01 13:46:24 -080020
21 "github.com/google/blueprint"
Colin Cross28c3eb62019-04-23 22:04:46 -070022
23 "android/soong/android"
Colin Cross3d7c9822019-03-01 13:46:24 -080024)
25
26type 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 Cross28c3eb62019-04-23 22:04:46 -070036
Colin Cross0c4ce212019-05-03 15:28:19 -070037 srcJarArgs []string
38 srcJarDeps android.Paths
39
Colin Cross28c3eb62019-04-23 22:04:46 -070040 combinedHeaderJar android.Path
41 combinedImplementationJar android.Path
Colin Cross3d7c9822019-03-01 13:46:24 -080042}
43
44type DeviceHostConverterProperties struct {
45 // List of modules whose contents will be visible to modules that depend on this module.
46 Libs []string
47}
48
49type 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//
56// It is rarely necessary, and its used is restricted to a few whitelisted projects.
57func DeviceForHostFactory() android.Module {
58 module := &DeviceForHost{}
59
60 module.AddProperties(&module.properties)
61
62 InitJavaModule(module, android.HostSupported)
63 return module
64}
65
66type 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//
73// It is rarely necessary, and its used is restricted to a few whitelisted projects.
74func HostForDeviceFactory() android.Module {
75 module := &HostForDevice{}
76
77 module.AddProperties(&module.properties)
78
79 InitJavaModule(module, android.DeviceSupported)
80 return module
81}
82
83var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"}
84
85func (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
90func (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
95func (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 Cross0c4ce212019-05-03 15:28:19 -0700106
107 srcJarArgs, srcJarDeps := dep.SrcJarArgs()
108 d.srcJarArgs = append(d.srcJarArgs, srcJarArgs...)
109 d.srcJarDeps = append(d.srcJarDeps, srcJarDeps...)
Colin Cross3d7c9822019-03-01 13:46:24 -0800110 } else {
111 ctx.PropertyErrorf("libs", "module %q cannot be used as a dependency", ctx.OtherModuleName(m))
112 }
113 })
Colin Cross28c3eb62019-04-23 22:04:46 -0700114
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,
129 android.OptionalPath{}, false, nil, nil)
130 d.combinedHeaderJar = outputFile
131 } else {
132 d.combinedHeaderJar = d.headerJars[0]
133 }
134
Colin Cross3d7c9822019-03-01 13:46:24 -0800135}
136
137var _ Dependency = (*DeviceHostConverter)(nil)
138
139func (d *DeviceHostConverter) HeaderJars() android.Paths {
140 return d.headerJars
141}
142
143func (d *DeviceHostConverter) ImplementationJars() android.Paths {
144 return d.implementationJars
145}
146
147func (d *DeviceHostConverter) ResourceJars() android.Paths {
148 return d.resourceJars
149}
150
151func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths {
152 return d.implementationAndResourceJars
153}
154
155func (d *DeviceHostConverter) DexJar() android.Path {
156 return nil
157}
158
159func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths {
160 return nil
161}
162
163func (d *DeviceHostConverter) ExportedSdkLibs() []string {
164 return nil
165}
Colin Cross28c3eb62019-04-23 22:04:46 -0700166
Colin Cross0c4ce212019-05-03 15:28:19 -0700167func (d *DeviceHostConverter) SrcJarArgs() ([]string, android.Paths) {
168 return d.srcJarArgs, d.srcJarDeps
169}
170
Colin Cross28c3eb62019-04-23 22:04:46 -0700171func (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}