blob: 9c883e50a88b3fbe868fd9446e4d9ec49e7fa046 [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
37 combinedHeaderJar android.Path
38 combinedImplementationJar android.Path
Colin Cross3d7c9822019-03-01 13:46:24 -080039}
40
41type DeviceHostConverterProperties struct {
42 // List of modules whose contents will be visible to modules that depend on this module.
43 Libs []string
44}
45
46type DeviceForHost struct {
47 DeviceHostConverter
48}
49
50// java_device_for_host makes the classes.jar output of a device java_library module available to host
51// java_library modules.
52//
53// It is rarely necessary, and its used is restricted to a few whitelisted projects.
54func DeviceForHostFactory() android.Module {
55 module := &DeviceForHost{}
56
57 module.AddProperties(&module.properties)
58
59 InitJavaModule(module, android.HostSupported)
60 return module
61}
62
63type HostForDevice struct {
64 DeviceHostConverter
65}
66
67// java_host_for_device makes the classes.jar output of a host java_library module available to device
68// java_library modules.
69//
70// It is rarely necessary, and its used is restricted to a few whitelisted projects.
71func HostForDeviceFactory() android.Module {
72 module := &HostForDevice{}
73
74 module.AddProperties(&module.properties)
75
76 InitJavaModule(module, android.DeviceSupported)
77 return module
78}
79
80var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"}
81
82func (d *DeviceForHost) DepsMutator(ctx android.BottomUpMutatorContext) {
83 variation := []blueprint.Variation{{Mutator: "arch", Variation: "android_common"}}
84 ctx.AddFarVariationDependencies(variation, deviceHostConverterDepTag, d.properties.Libs...)
85}
86
87func (d *HostForDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
88 variation := []blueprint.Variation{{Mutator: "arch", Variation: ctx.Config().BuildOsCommonVariant}}
89 ctx.AddFarVariationDependencies(variation, deviceHostConverterDepTag, d.properties.Libs...)
90}
91
92func (d *DeviceHostConverter) GenerateAndroidBuildActions(ctx android.ModuleContext) {
93 if len(d.properties.Libs) < 1 {
94 ctx.PropertyErrorf("libs", "at least one dependency is required")
95 }
96
97 ctx.VisitDirectDepsWithTag(deviceHostConverterDepTag, func(m android.Module) {
98 if dep, ok := m.(Dependency); ok {
99 d.headerJars = append(d.headerJars, dep.HeaderJars()...)
100 d.implementationJars = append(d.implementationJars, dep.ImplementationJars()...)
101 d.implementationAndResourceJars = append(d.implementationAndResourceJars, dep.ImplementationAndResourcesJars()...)
102 d.resourceJars = append(d.resourceJars, dep.ResourceJars()...)
103 } else {
104 ctx.PropertyErrorf("libs", "module %q cannot be used as a dependency", ctx.OtherModuleName(m))
105 }
106 })
Colin Cross28c3eb62019-04-23 22:04:46 -0700107
108 jarName := ctx.ModuleName() + ".jar"
109
110 if len(d.implementationAndResourceJars) > 1 {
111 outputFile := android.PathForModuleOut(ctx, "combined", jarName)
112 TransformJarsToJar(ctx, outputFile, "combine", d.implementationAndResourceJars,
113 android.OptionalPath{}, false, nil, nil)
114 d.combinedImplementationJar = outputFile
115 } else {
116 d.combinedImplementationJar = d.implementationAndResourceJars[0]
117 }
118
119 if len(d.headerJars) > 1 {
120 outputFile := android.PathForModuleOut(ctx, "turbine-combined", jarName)
121 TransformJarsToJar(ctx, outputFile, "turbine combine", d.headerJars,
122 android.OptionalPath{}, false, nil, nil)
123 d.combinedHeaderJar = outputFile
124 } else {
125 d.combinedHeaderJar = d.headerJars[0]
126 }
127
Colin Cross3d7c9822019-03-01 13:46:24 -0800128}
129
130var _ Dependency = (*DeviceHostConverter)(nil)
131
132func (d *DeviceHostConverter) HeaderJars() android.Paths {
133 return d.headerJars
134}
135
136func (d *DeviceHostConverter) ImplementationJars() android.Paths {
137 return d.implementationJars
138}
139
140func (d *DeviceHostConverter) ResourceJars() android.Paths {
141 return d.resourceJars
142}
143
144func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths {
145 return d.implementationAndResourceJars
146}
147
148func (d *DeviceHostConverter) DexJar() android.Path {
149 return nil
150}
151
152func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths {
153 return nil
154}
155
156func (d *DeviceHostConverter) ExportedSdkLibs() []string {
157 return nil
158}
Colin Cross28c3eb62019-04-23 22:04:46 -0700159
160func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData {
161 return android.AndroidMkData{
162 Class: "JAVA_LIBRARIES",
163 OutputFile: android.OptionalPathForPath(d.combinedImplementationJar),
164 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
165 Extra: []android.AndroidMkExtraFunc{
166 func(w io.Writer, outputFile android.Path) {
167 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
168 fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String())
169 fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String())
170 },
171 },
172 }
173}