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
15package java
16
17import (
18	"fmt"
19	"io"
20
21	"android/soong/android"
22	"android/soong/dexpreopt"
23)
24
25type 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
35
36	srcJarArgs []string
37	srcJarDeps android.Paths
38
39	combinedHeaderJar         android.Path
40	combinedImplementationJar android.Path
41}
42
43type DeviceHostConverterProperties struct {
44	// List of modules whose contents will be visible to modules that depend on this module.
45	Libs []string
46}
47
48type 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//
55// It is rarely necessary, and its usage is restricted to a few allowed projects.
56func DeviceForHostFactory() android.Module {
57	module := &DeviceForHost{}
58
59	module.AddProperties(&module.properties)
60
61	InitJavaModule(module, android.HostSupported)
62	return module
63}
64
65type 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//
72// It is rarely necessary, and its usage is restricted to a few allowed projects.
73func HostForDeviceFactory() android.Module {
74	module := &HostForDevice{}
75
76	module.AddProperties(&module.properties)
77
78	InitJavaModule(module, android.DeviceSupported)
79	return module
80}
81
82var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"}
83
84func (d *DeviceForHost) DepsMutator(ctx android.BottomUpMutatorContext) {
85	ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
86		deviceHostConverterDepTag, d.properties.Libs...)
87}
88
89func (d *HostForDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
90	ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
91		deviceHostConverterDepTag, d.properties.Libs...)
92}
93
94func (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) {
100		if ctx.OtherModuleHasProvider(m, JavaInfoProvider) {
101			dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
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...)
106
107			d.srcJarArgs = append(d.srcJarArgs, dep.SrcJarArgs...)
108			d.srcJarDeps = append(d.srcJarDeps, dep.SrcJarDeps...)
109		} else {
110			ctx.PropertyErrorf("libs", "module %q cannot be used as a dependency", ctx.OtherModuleName(m))
111		}
112	})
113
114	jarName := ctx.ModuleName() + ".jar"
115
116	if len(d.implementationAndResourceJars) > 1 {
117		outputFile := android.PathForModuleOut(ctx, "combined", jarName)
118		TransformJarsToJar(ctx, outputFile, "combine", d.implementationAndResourceJars,
119			android.OptionalPath{}, false, nil, nil)
120		d.combinedImplementationJar = outputFile
121	} else {
122		d.combinedImplementationJar = d.implementationAndResourceJars[0]
123	}
124
125	if len(d.headerJars) > 1 {
126		outputFile := android.PathForModuleOut(ctx, "turbine-combined", jarName)
127		TransformJarsToJar(ctx, outputFile, "turbine combine", d.headerJars,
128			android.OptionalPath{}, false, nil, []string{"META-INF/TRANSITIVE"})
129		d.combinedHeaderJar = outputFile
130	} else {
131		d.combinedHeaderJar = d.headerJars[0]
132	}
133
134	ctx.SetProvider(JavaInfoProvider, JavaInfo{
135		HeaderJars:                     d.headerJars,
136		ImplementationAndResourcesJars: d.implementationAndResourceJars,
137		ImplementationJars:             d.implementationJars,
138		ResourceJars:                   d.resourceJars,
139		SrcJarArgs:                     d.srcJarArgs,
140		SrcJarDeps:                     d.srcJarDeps,
141	})
142
143}
144
145func (d *DeviceHostConverter) HeaderJars() android.Paths {
146	return d.headerJars
147}
148
149func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths {
150	return d.implementationAndResourceJars
151}
152
153func (d *DeviceHostConverter) DexJarBuildPath() android.Path {
154	return nil
155}
156
157func (d *DeviceHostConverter) DexJarInstallPath() android.Path {
158	return nil
159}
160
161func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths {
162	return nil
163}
164
165func (d *DeviceHostConverter) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
166	return nil
167}
168
169func (d *DeviceHostConverter) JacocoReportClassesFile() android.Path {
170	return nil
171}
172
173func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData {
174	return android.AndroidMkData{
175		Class:      "JAVA_LIBRARIES",
176		OutputFile: android.OptionalPathForPath(d.combinedImplementationJar),
177		Include:    "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
178		Extra: []android.AndroidMkExtraFunc{
179			func(w io.Writer, outputFile android.Path) {
180				fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
181				fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String())
182				fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String())
183			},
184		},
185	}
186}
187