xref: /aosp_15_r20/build/soong/java/device_host_converter_test.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2019 Google Inc. All rights reserved.
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage java
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"android/soong/android"
19*333d2b36SAndroid Build Coastguard Worker	"slices"
20*333d2b36SAndroid Build Coastguard Worker	"strings"
21*333d2b36SAndroid Build Coastguard Worker	"testing"
22*333d2b36SAndroid Build Coastguard Worker)
23*333d2b36SAndroid Build Coastguard Worker
24*333d2b36SAndroid Build Coastguard Workerfunc TestDeviceForHost(t *testing.T) {
25*333d2b36SAndroid Build Coastguard Worker	bp := `
26*333d2b36SAndroid Build Coastguard Worker		java_library {
27*333d2b36SAndroid Build Coastguard Worker			name: "device_module",
28*333d2b36SAndroid Build Coastguard Worker			srcs: ["a.java"],
29*333d2b36SAndroid Build Coastguard Worker			java_resources: ["java-res/a/a"],
30*333d2b36SAndroid Build Coastguard Worker		}
31*333d2b36SAndroid Build Coastguard Worker
32*333d2b36SAndroid Build Coastguard Worker		java_import {
33*333d2b36SAndroid Build Coastguard Worker			name: "device_import_module",
34*333d2b36SAndroid Build Coastguard Worker			jars: ["a.jar"],
35*333d2b36SAndroid Build Coastguard Worker		}
36*333d2b36SAndroid Build Coastguard Worker
37*333d2b36SAndroid Build Coastguard Worker		java_device_for_host {
38*333d2b36SAndroid Build Coastguard Worker			name: "device_for_host_module",
39*333d2b36SAndroid Build Coastguard Worker			libs: [
40*333d2b36SAndroid Build Coastguard Worker				"device_module",
41*333d2b36SAndroid Build Coastguard Worker				"device_import_module",
42*333d2b36SAndroid Build Coastguard Worker			],
43*333d2b36SAndroid Build Coastguard Worker		}
44*333d2b36SAndroid Build Coastguard Worker
45*333d2b36SAndroid Build Coastguard Worker		java_library_host {
46*333d2b36SAndroid Build Coastguard Worker			name: "host_module",
47*333d2b36SAndroid Build Coastguard Worker			srcs: ["b.java"],
48*333d2b36SAndroid Build Coastguard Worker			java_resources: ["java-res/b/b"],
49*333d2b36SAndroid Build Coastguard Worker			static_libs: ["device_for_host_module"],
50*333d2b36SAndroid Build Coastguard Worker		}
51*333d2b36SAndroid Build Coastguard Worker	`
52*333d2b36SAndroid Build Coastguard Worker
53*333d2b36SAndroid Build Coastguard Worker	ctx, config := testJava(t, bp)
54*333d2b36SAndroid Build Coastguard Worker
55*333d2b36SAndroid Build Coastguard Worker	deviceModule := ctx.ModuleForTests("device_module", "android_common")
56*333d2b36SAndroid Build Coastguard Worker	deviceTurbineCombined := deviceModule.Output("turbine-combined/device_module.jar")
57*333d2b36SAndroid Build Coastguard Worker	deviceJavac := deviceModule.Output("javac/device_module.jar")
58*333d2b36SAndroid Build Coastguard Worker	deviceRes := deviceModule.Output("res/device_module.jar")
59*333d2b36SAndroid Build Coastguard Worker
60*333d2b36SAndroid Build Coastguard Worker	deviceImportModule := ctx.ModuleForTests("device_import_module", "android_common")
61*333d2b36SAndroid Build Coastguard Worker	deviceImportCombined := deviceImportModule.Output("combined/device_import_module.jar")
62*333d2b36SAndroid Build Coastguard Worker
63*333d2b36SAndroid Build Coastguard Worker	hostModule := ctx.ModuleForTests("host_module", config.BuildOSCommonTarget.String())
64*333d2b36SAndroid Build Coastguard Worker	hostJavac := hostModule.Output("javac/host_module.jar")
65*333d2b36SAndroid Build Coastguard Worker	hostRes := hostModule.Output("res/host_module.jar")
66*333d2b36SAndroid Build Coastguard Worker	combined := hostModule.Output("combined/host_module.jar")
67*333d2b36SAndroid Build Coastguard Worker	resCombined := hostModule.Output("res-combined/host_module.jar")
68*333d2b36SAndroid Build Coastguard Worker
69*333d2b36SAndroid Build Coastguard Worker	// check classpath of host module with dependency on device_for_host_module
70*333d2b36SAndroid Build Coastguard Worker	expectedClasspath := "-classpath " + strings.Join(android.Paths{
71*333d2b36SAndroid Build Coastguard Worker		deviceTurbineCombined.Output,
72*333d2b36SAndroid Build Coastguard Worker		deviceImportCombined.Output,
73*333d2b36SAndroid Build Coastguard Worker	}.Strings(), ":")
74*333d2b36SAndroid Build Coastguard Worker
75*333d2b36SAndroid Build Coastguard Worker	if hostJavac.Args["classpath"] != expectedClasspath {
76*333d2b36SAndroid Build Coastguard Worker		t.Errorf("expected host_module javac classpath:\n%s\ngot:\n%s",
77*333d2b36SAndroid Build Coastguard Worker			expectedClasspath, hostJavac.Args["classpath"])
78*333d2b36SAndroid Build Coastguard Worker	}
79*333d2b36SAndroid Build Coastguard Worker
80*333d2b36SAndroid Build Coastguard Worker	// check host module merged with static dependency implementation jars from device_for_host module
81*333d2b36SAndroid Build Coastguard Worker	expectedInputs := android.Paths{
82*333d2b36SAndroid Build Coastguard Worker		hostJavac.Output,
83*333d2b36SAndroid Build Coastguard Worker		deviceJavac.Output,
84*333d2b36SAndroid Build Coastguard Worker		deviceImportCombined.Output,
85*333d2b36SAndroid Build Coastguard Worker	}
86*333d2b36SAndroid Build Coastguard Worker
87*333d2b36SAndroid Build Coastguard Worker	if !slices.Equal(combined.Inputs.Strings(), expectedInputs.Strings()) {
88*333d2b36SAndroid Build Coastguard Worker		t.Errorf("expected host_module combined inputs:\n%q\ngot:\n%q",
89*333d2b36SAndroid Build Coastguard Worker			expectedInputs, combined.Inputs)
90*333d2b36SAndroid Build Coastguard Worker	}
91*333d2b36SAndroid Build Coastguard Worker
92*333d2b36SAndroid Build Coastguard Worker	// check host module merged with static dependency resource jars from device_for_host module
93*333d2b36SAndroid Build Coastguard Worker	expectedInputs = android.Paths{
94*333d2b36SAndroid Build Coastguard Worker		hostRes.Output,
95*333d2b36SAndroid Build Coastguard Worker		deviceRes.Output,
96*333d2b36SAndroid Build Coastguard Worker	}
97*333d2b36SAndroid Build Coastguard Worker
98*333d2b36SAndroid Build Coastguard Worker	if !slices.Equal(resCombined.Inputs.Strings(), expectedInputs.Strings()) {
99*333d2b36SAndroid Build Coastguard Worker		t.Errorf("expected host_module res combined inputs:\n%q\ngot:\n%q",
100*333d2b36SAndroid Build Coastguard Worker			expectedInputs, resCombined.Inputs)
101*333d2b36SAndroid Build Coastguard Worker	}
102*333d2b36SAndroid Build Coastguard Worker}
103*333d2b36SAndroid Build Coastguard Worker
104*333d2b36SAndroid Build Coastguard Workerfunc TestHostForDevice(t *testing.T) {
105*333d2b36SAndroid Build Coastguard Worker	bp := `
106*333d2b36SAndroid Build Coastguard Worker		java_library_host {
107*333d2b36SAndroid Build Coastguard Worker			name: "host_module",
108*333d2b36SAndroid Build Coastguard Worker			srcs: ["a.java"],
109*333d2b36SAndroid Build Coastguard Worker			java_resources: ["java-res/a/a"],
110*333d2b36SAndroid Build Coastguard Worker		}
111*333d2b36SAndroid Build Coastguard Worker
112*333d2b36SAndroid Build Coastguard Worker		java_import_host {
113*333d2b36SAndroid Build Coastguard Worker			name: "host_import_module",
114*333d2b36SAndroid Build Coastguard Worker			jars: ["a.jar"],
115*333d2b36SAndroid Build Coastguard Worker		}
116*333d2b36SAndroid Build Coastguard Worker
117*333d2b36SAndroid Build Coastguard Worker		java_host_for_device {
118*333d2b36SAndroid Build Coastguard Worker			name: "host_for_device_module",
119*333d2b36SAndroid Build Coastguard Worker			libs: [
120*333d2b36SAndroid Build Coastguard Worker				"host_module",
121*333d2b36SAndroid Build Coastguard Worker				"host_import_module",
122*333d2b36SAndroid Build Coastguard Worker			],
123*333d2b36SAndroid Build Coastguard Worker		}
124*333d2b36SAndroid Build Coastguard Worker
125*333d2b36SAndroid Build Coastguard Worker		java_library {
126*333d2b36SAndroid Build Coastguard Worker			name: "device_module",
127*333d2b36SAndroid Build Coastguard Worker			sdk_version: "core_platform",
128*333d2b36SAndroid Build Coastguard Worker			srcs: ["b.java"],
129*333d2b36SAndroid Build Coastguard Worker			java_resources: ["java-res/b/b"],
130*333d2b36SAndroid Build Coastguard Worker			static_libs: ["host_for_device_module"],
131*333d2b36SAndroid Build Coastguard Worker		}
132*333d2b36SAndroid Build Coastguard Worker	`
133*333d2b36SAndroid Build Coastguard Worker
134*333d2b36SAndroid Build Coastguard Worker	ctx, config := testJava(t, bp)
135*333d2b36SAndroid Build Coastguard Worker
136*333d2b36SAndroid Build Coastguard Worker	hostModule := ctx.ModuleForTests("host_module", config.BuildOSCommonTarget.String())
137*333d2b36SAndroid Build Coastguard Worker	hostJavac := hostModule.Output("javac/host_module.jar")
138*333d2b36SAndroid Build Coastguard Worker	hostJavacHeader := hostModule.Output("javac-header/host_module.jar")
139*333d2b36SAndroid Build Coastguard Worker	hostRes := hostModule.Output("res/host_module.jar")
140*333d2b36SAndroid Build Coastguard Worker
141*333d2b36SAndroid Build Coastguard Worker	hostImportModule := ctx.ModuleForTests("host_import_module", config.BuildOSCommonTarget.String())
142*333d2b36SAndroid Build Coastguard Worker	hostImportCombined := hostImportModule.Output("combined/host_import_module.jar")
143*333d2b36SAndroid Build Coastguard Worker
144*333d2b36SAndroid Build Coastguard Worker	deviceModule := ctx.ModuleForTests("device_module", "android_common")
145*333d2b36SAndroid Build Coastguard Worker	deviceJavac := deviceModule.Output("javac/device_module.jar")
146*333d2b36SAndroid Build Coastguard Worker	deviceRes := deviceModule.Output("res/device_module.jar")
147*333d2b36SAndroid Build Coastguard Worker	combined := deviceModule.Output("combined/device_module.jar")
148*333d2b36SAndroid Build Coastguard Worker	resCombined := deviceModule.Output("res-combined/device_module.jar")
149*333d2b36SAndroid Build Coastguard Worker
150*333d2b36SAndroid Build Coastguard Worker	// check classpath of device module with dependency on host_for_device_module
151*333d2b36SAndroid Build Coastguard Worker	expectedClasspath := "-classpath " + strings.Join(android.Paths{
152*333d2b36SAndroid Build Coastguard Worker		hostJavacHeader.Output,
153*333d2b36SAndroid Build Coastguard Worker		hostImportCombined.Output,
154*333d2b36SAndroid Build Coastguard Worker	}.Strings(), ":")
155*333d2b36SAndroid Build Coastguard Worker
156*333d2b36SAndroid Build Coastguard Worker	if deviceJavac.Args["classpath"] != expectedClasspath {
157*333d2b36SAndroid Build Coastguard Worker		t.Errorf("expected device_module javac classpath:\n%s\ngot:\n%s",
158*333d2b36SAndroid Build Coastguard Worker			expectedClasspath, deviceJavac.Args["classpath"])
159*333d2b36SAndroid Build Coastguard Worker	}
160*333d2b36SAndroid Build Coastguard Worker
161*333d2b36SAndroid Build Coastguard Worker	// check device module merged with static dependency implementation jars from host_for_device module
162*333d2b36SAndroid Build Coastguard Worker	expectedInputs := android.Paths{
163*333d2b36SAndroid Build Coastguard Worker		deviceJavac.Output,
164*333d2b36SAndroid Build Coastguard Worker		hostJavac.Output,
165*333d2b36SAndroid Build Coastguard Worker		hostImportCombined.Output,
166*333d2b36SAndroid Build Coastguard Worker	}
167*333d2b36SAndroid Build Coastguard Worker
168*333d2b36SAndroid Build Coastguard Worker	if !slices.Equal(combined.Inputs.Strings(), expectedInputs.Strings()) {
169*333d2b36SAndroid Build Coastguard Worker		t.Errorf("expected device_module combined inputs:\n%q\ngot:\n%q",
170*333d2b36SAndroid Build Coastguard Worker			expectedInputs, combined.Inputs)
171*333d2b36SAndroid Build Coastguard Worker	}
172*333d2b36SAndroid Build Coastguard Worker
173*333d2b36SAndroid Build Coastguard Worker	// check device module merged with static dependency resource jars from host_for_device module
174*333d2b36SAndroid Build Coastguard Worker	expectedInputs = android.Paths{
175*333d2b36SAndroid Build Coastguard Worker		deviceRes.Output,
176*333d2b36SAndroid Build Coastguard Worker		hostRes.Output,
177*333d2b36SAndroid Build Coastguard Worker	}
178*333d2b36SAndroid Build Coastguard Worker
179*333d2b36SAndroid Build Coastguard Worker	if !slices.Equal(resCombined.Inputs.Strings(), expectedInputs.Strings()) {
180*333d2b36SAndroid Build Coastguard Worker		t.Errorf("expected device_module res combined inputs:\n%q\ngot:\n%q",
181*333d2b36SAndroid Build Coastguard Worker			expectedInputs, resCombined.Inputs)
182*333d2b36SAndroid Build Coastguard Worker	}
183*333d2b36SAndroid Build Coastguard Worker}
184