• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/bazel"
23	"android/soong/dexpreopt"
24
25	"github.com/google/blueprint/proptools"
26)
27
28type DeviceHostConverter struct {
29	android.ModuleBase
30	android.DefaultableModuleBase
31	android.BazelModuleBase
32
33	properties DeviceHostConverterProperties
34
35	headerJars                    android.Paths
36	implementationJars            android.Paths
37	implementationAndResourceJars android.Paths
38	resourceJars                  android.Paths
39
40	srcJarArgs []string
41	srcJarDeps android.Paths
42
43	combinedHeaderJar         android.Path
44	combinedImplementationJar android.Path
45}
46
47type DeviceHostConverterProperties struct {
48	// List of modules whose contents will be visible to modules that depend on this module.
49	Libs []string
50}
51
52type DeviceForHost struct {
53	DeviceHostConverter
54}
55
56// java_device_for_host makes the classes.jar output of a device java_library module available to host
57// java_library modules.
58//
59// It is rarely necessary, and its usage is restricted to a few allowed projects.
60func DeviceForHostFactory() android.Module {
61	module := &DeviceForHost{}
62
63	module.AddProperties(&module.properties)
64
65	InitJavaModule(module, android.HostSupported)
66	return module
67}
68
69type HostForDevice struct {
70	DeviceHostConverter
71}
72
73// java_host_for_device makes the classes.jar output of a host java_library module available to device
74// java_library modules.
75//
76// It is rarely necessary, and its usage is restricted to a few allowed projects.
77func HostForDeviceFactory() android.Module {
78	module := &HostForDevice{}
79
80	module.AddProperties(&module.properties)
81
82	InitJavaModule(module, android.DeviceSupported)
83	android.InitBazelModule(module)
84	return module
85}
86
87var deviceHostConverterDepTag = dependencyTag{name: "device_host_converter"}
88
89func (d *DeviceForHost) DepsMutator(ctx android.BottomUpMutatorContext) {
90	ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(),
91		deviceHostConverterDepTag, d.properties.Libs...)
92}
93
94func (d *HostForDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
95	ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
96		deviceHostConverterDepTag, d.properties.Libs...)
97}
98
99func (d *DeviceHostConverter) GenerateAndroidBuildActions(ctx android.ModuleContext) {
100	if len(d.properties.Libs) < 1 {
101		ctx.PropertyErrorf("libs", "at least one dependency is required")
102	}
103
104	ctx.VisitDirectDepsWithTag(deviceHostConverterDepTag, func(m android.Module) {
105		if ctx.OtherModuleHasProvider(m, JavaInfoProvider) {
106			dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
107			d.headerJars = append(d.headerJars, dep.HeaderJars...)
108			d.implementationJars = append(d.implementationJars, dep.ImplementationJars...)
109			d.implementationAndResourceJars = append(d.implementationAndResourceJars, dep.ImplementationAndResourcesJars...)
110			d.resourceJars = append(d.resourceJars, dep.ResourceJars...)
111
112			d.srcJarArgs = append(d.srcJarArgs, dep.SrcJarArgs...)
113			d.srcJarDeps = append(d.srcJarDeps, dep.SrcJarDeps...)
114		} else {
115			ctx.PropertyErrorf("libs", "module %q cannot be used as a dependency", ctx.OtherModuleName(m))
116		}
117	})
118
119	jarName := ctx.ModuleName() + ".jar"
120
121	if len(d.implementationAndResourceJars) > 1 {
122		outputFile := android.PathForModuleOut(ctx, "combined", jarName)
123		TransformJarsToJar(ctx, outputFile, "combine", d.implementationAndResourceJars,
124			android.OptionalPath{}, false, nil, nil)
125		d.combinedImplementationJar = outputFile
126	} else if len(d.implementationAndResourceJars) == 1 {
127		d.combinedImplementationJar = d.implementationAndResourceJars[0]
128	}
129
130	if len(d.headerJars) > 1 {
131		outputFile := android.PathForModuleOut(ctx, "turbine-combined", jarName)
132		TransformJarsToJar(ctx, outputFile, "turbine combine", d.headerJars,
133			android.OptionalPath{}, false, nil, []string{"META-INF/TRANSITIVE"})
134		d.combinedHeaderJar = outputFile
135	} else if len(d.headerJars) == 1 {
136		d.combinedHeaderJar = d.headerJars[0]
137	}
138
139	ctx.SetProvider(JavaInfoProvider, JavaInfo{
140		HeaderJars:                     d.headerJars,
141		ImplementationAndResourcesJars: d.implementationAndResourceJars,
142		ImplementationJars:             d.implementationJars,
143		ResourceJars:                   d.resourceJars,
144		SrcJarArgs:                     d.srcJarArgs,
145		SrcJarDeps:                     d.srcJarDeps,
146	})
147
148}
149
150func (d *DeviceHostConverter) HeaderJars() android.Paths {
151	return d.headerJars
152}
153
154func (d *DeviceHostConverter) ImplementationAndResourcesJars() android.Paths {
155	return d.implementationAndResourceJars
156}
157
158func (d *DeviceHostConverter) DexJarBuildPath() android.Path {
159	return nil
160}
161
162func (d *DeviceHostConverter) DexJarInstallPath() android.Path {
163	return nil
164}
165
166func (d *DeviceHostConverter) AidlIncludeDirs() android.Paths {
167	return nil
168}
169
170func (d *DeviceHostConverter) ClassLoaderContexts() dexpreopt.ClassLoaderContextMap {
171	return nil
172}
173
174func (d *DeviceHostConverter) JacocoReportClassesFile() android.Path {
175	return nil
176}
177
178func (d *DeviceHostConverter) AndroidMk() android.AndroidMkData {
179	return android.AndroidMkData{
180		Class:      "JAVA_LIBRARIES",
181		OutputFile: android.OptionalPathForPath(d.combinedImplementationJar),
182		// Make does not support Windows Java modules
183		Disabled: d.Os() == android.Windows,
184		Include:  "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
185		Extra: []android.AndroidMkExtraFunc{
186			func(w io.Writer, outputFile android.Path) {
187				fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
188				fmt.Fprintln(w, "LOCAL_SOONG_HEADER_JAR :=", d.combinedHeaderJar.String())
189				fmt.Fprintln(w, "LOCAL_SOONG_CLASSES_JAR :=", d.combinedImplementationJar.String())
190			},
191		},
192	}
193}
194
195type bazelDeviceHostConverterAttributes struct {
196	Exports bazel.LabelListAttribute
197}
198
199func (d *DeviceHostConverter) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
200	ctx.CreateBazelTargetModule(
201		bazel.BazelTargetModuleProperties{
202			Rule_class:        "java_host_for_device",
203			Bzl_load_location: "//build/bazel/rules/java:host_for_device.bzl",
204		},
205		android.CommonAttributes{Name: d.Name()},
206		&bazelDeviceHostConverterAttributes{
207			Exports: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, d.properties.Libs)),
208		},
209	)
210	neverLinkAttrs := &javaLibraryAttributes{
211		Exports:   bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + d.Name()}),
212		Neverlink: bazel.BoolAttribute{Value: proptools.BoolPtr(true)},
213		javaCommonAttributes: &javaCommonAttributes{
214			Sdk_version: bazel.StringAttribute{Value: proptools.StringPtr("none")},
215		},
216	}
217	ctx.CreateBazelTargetModule(
218		javaLibraryBazelTargetModuleProperties(),
219		android.CommonAttributes{Name: d.Name() + "-neverlink"},
220		neverLinkAttrs)
221
222}
223