blob: e8551e550edd75db875de7b066e55df70f685171 [file] [log] [blame]
Wei Libafb6d62021-12-10 03:14:59 -08001// Copyright 2021 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 bp2build
16
17import (
18 "testing"
19
20 "android/soong/android"
21 "android/soong/cc"
22 "android/soong/java"
23)
24
Sam Delmerico3177a6e2022-06-21 19:28:33 +000025func runJavaBinaryHostTestCase(t *testing.T, tc Bp2buildTestCase) {
Wei Libafb6d62021-12-10 03:14:59 -080026 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000027 (&tc).ModuleTypeUnderTest = "java_binary_host"
28 (&tc).ModuleTypeUnderTestFactory = java.BinaryHostFactory
29 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
Wei Libafb6d62021-12-10 03:14:59 -080030 ctx.RegisterModuleType("cc_library_host_shared", cc.LibraryHostSharedFactory)
Sam Delmericoc0161432022-02-25 21:34:51 +000031 ctx.RegisterModuleType("java_library", java.LibraryFactory)
Alixb4e09a02022-09-27 15:36:01 +000032 ctx.RegisterModuleType("java_import_host", java.ImportFactory)
Wei Libafb6d62021-12-10 03:14:59 -080033 }, tc)
34}
35
Lukacs T. Berkibc5f7312022-10-31 09:01:34 +000036var testFs = map[string]string{
Wei Libafb6d62021-12-10 03:14:59 -080037 "test.mf": "Main-Class: com.android.test.MainClass",
38 "other/Android.bp": `cc_library_host_shared {
39 name: "jni-lib-1",
40 stl: "none",
41}`,
42}
43
44func TestJavaBinaryHost(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000045 runJavaBinaryHostTestCase(t, Bp2buildTestCase{
46 Description: "java_binary_host with srcs, exclude_srcs, jni_libs, javacflags, and manifest.",
Lukacs T. Berkibc5f7312022-10-31 09:01:34 +000047 Filesystem: testFs,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000048 Blueprint: `java_binary_host {
Wei Libafb6d62021-12-10 03:14:59 -080049 name: "java-binary-host-1",
50 srcs: ["a.java", "b.java"],
51 exclude_srcs: ["b.java"],
52 manifest: "test.mf",
53 jni_libs: ["jni-lib-1"],
Sam Delmerico4e272292022-01-06 20:03:51 +000054 javacflags: ["-Xdoclint:all/protected"],
Wei Libafb6d62021-12-10 03:14:59 -080055 bazel_module: { bp2build_available: true },
Vinh Tran3ac6daf2022-04-22 19:09:58 -040056 java_version: "8",
Wei Libafb6d62021-12-10 03:14:59 -080057}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000058 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000059 MakeBazelTarget("java_binary", "java-binary-host-1", AttrNameToString{
Wei Libafb6d62021-12-10 03:14:59 -080060 "srcs": `["a.java"]`,
61 "main_class": `"com.android.test.MainClass"`,
62 "deps": `["//other:jni-lib-1"]`,
63 "jvm_flags": `["-Djava.library.path=$${RUNPATH}other"]`,
Vinh Tran3ac6daf2022-04-22 19:09:58 -040064 "javacopts": `[
65 "-Xdoclint:all/protected",
66 "-source 1.8 -target 1.8",
67 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +000068 "target_compatible_with": `select({
69 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
70 "//conditions:default": [],
71 })`,
Wei Libafb6d62021-12-10 03:14:59 -080072 }),
73 },
74 })
75}
Sam Delmericoc0161432022-02-25 21:34:51 +000076
77func TestJavaBinaryHostRuntimeDeps(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +000078 runJavaBinaryHostTestCase(t, Bp2buildTestCase{
79 Description: "java_binary_host with srcs, exclude_srcs, jni_libs, javacflags, and manifest.",
Lukacs T. Berkibc5f7312022-10-31 09:01:34 +000080 Filesystem: testFs,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000081 Blueprint: `java_binary_host {
Sam Delmericoc0161432022-02-25 21:34:51 +000082 name: "java-binary-host-1",
83 static_libs: ["java-dep-1"],
84 manifest: "test.mf",
85 bazel_module: { bp2build_available: true },
86}
87
88java_library {
89 name: "java-dep-1",
90 srcs: ["a.java"],
91 bazel_module: { bp2build_available: false },
92}
93`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000094 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000095 MakeBazelTarget("java_binary", "java-binary-host-1", AttrNameToString{
Sam Delmericoc0161432022-02-25 21:34:51 +000096 "main_class": `"com.android.test.MainClass"`,
97 "runtime_deps": `[":java-dep-1"]`,
98 "target_compatible_with": `select({
99 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
100 "//conditions:default": [],
101 })`,
102 }),
103 },
104 })
105}
Alixb4e09a02022-09-27 15:36:01 +0000106
107func TestJavaBinaryHostLibs(t *testing.T) {
108 runJavaBinaryHostTestCase(t, Bp2buildTestCase{
109 Description: "java_binary_host with srcs, libs.",
Lukacs T. Berkibc5f7312022-10-31 09:01:34 +0000110 Filesystem: testFs,
Alixb4e09a02022-09-27 15:36:01 +0000111 Blueprint: `java_binary_host {
112 name: "java-binary-host-libs",
113 libs: ["java-lib-dep-1"],
114 manifest: "test.mf",
115 srcs: ["a.java"],
116}
117
118java_import_host{
119 name: "java-lib-dep-1",
120 jars: ["foo.jar"],
121 bazel_module: { bp2build_available: false },
122}
123`,
124 ExpectedBazelTargets: []string{
125 MakeBazelTarget("java_binary", "java-binary-host-libs", AttrNameToString{
126 "main_class": `"com.android.test.MainClass"`,
127 "srcs": `["a.java"]`,
128 "deps": `[":java-lib-dep-1-neverlink"]`,
129 "target_compatible_with": `select({
130 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
131 "//conditions:default": [],
132 })`,
133 }),
134 },
135 })
136}