blob: 32c3f4d9bef847174933930af26b51e02a70e0fe [file] [log] [blame]
Trevor Radcliffe58ea4512022-04-07 20:36:39 +00001// Copyright 2022 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//
Colin Crossd079e0b2022-08-16 10:27:33 -07007// http://www.apache.org/licenses/LICENSE-2.0
Trevor Radcliffe58ea4512022-04-07 20:36:39 +00008//
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.
14package bp2build
15
16import (
17 "fmt"
18 "testing"
19
20 "android/soong/cc"
21)
22
23func TestPrebuiltLibraryStaticAndSharedSimple(t *testing.T) {
24 runBp2BuildTestCaseSimple(t,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000025 Bp2buildTestCase{
26 Description: "prebuilt library static and shared simple",
27 ModuleTypeUnderTest: "cc_prebuilt_library",
28 ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
29 Filesystem: map[string]string{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000030 "libf.so": "",
31 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000032 Blueprint: `
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000033cc_prebuilt_library {
34 name: "libtest",
35 srcs: ["libf.so"],
36 bazel_module: { bp2build_available: true },
37}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000038 ExpectedBazelTargets: []string{
39 makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000040 "static_library": `"libf.so"`,
41 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +000042 makeBazelTarget("prebuilt_library_shared", "libtest", AttrNameToString{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000043 "shared_library": `"libf.so"`,
44 }),
45 },
46 })
47}
48
49func TestPrebuiltLibraryWithArchVariance(t *testing.T) {
50 runBp2BuildTestCaseSimple(t,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000051 Bp2buildTestCase{
52 Description: "prebuilt library with arch variance",
53 ModuleTypeUnderTest: "cc_prebuilt_library",
54 ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
55 Filesystem: map[string]string{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000056 "libf.so": "",
57 "libg.so": "",
58 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000059 Blueprint: `
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000060cc_prebuilt_library {
61 name: "libtest",
62 arch: {
63 arm64: { srcs: ["libf.so"], },
64 arm: { srcs: ["libg.so"], },
65 },
66 bazel_module: { bp2build_available: true },
67}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000068 ExpectedBazelTargets: []string{
69 makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000070 "static_library": `select({
71 "//build/bazel/platforms/arch:arm": "libg.so",
72 "//build/bazel/platforms/arch:arm64": "libf.so",
73 "//conditions:default": None,
74 })`,
75 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +000076 makeBazelTarget("prebuilt_library_shared", "libtest", AttrNameToString{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000077 "shared_library": `select({
78 "//build/bazel/platforms/arch:arm": "libg.so",
79 "//build/bazel/platforms/arch:arm64": "libf.so",
80 "//conditions:default": None,
81 })`,
82 }),
83 },
84 })
85}
86
87func TestPrebuiltLibraryAdditionalAttrs(t *testing.T) {
88 runBp2BuildTestCaseSimple(t,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000089 Bp2buildTestCase{
90 Description: "prebuilt library additional attributes",
91 ModuleTypeUnderTest: "cc_prebuilt_library",
92 ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
93 Filesystem: map[string]string{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000094 "libf.so": "",
95 "testdir/1/": "",
96 "testdir/2/": "",
97 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000098 Blueprint: `
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000099cc_prebuilt_library {
100 name: "libtest",
101 srcs: ["libf.so"],
102 export_include_dirs: ["testdir/1/"],
103 export_system_include_dirs: ["testdir/2/"],
104 bazel_module: { bp2build_available: true },
105}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000106 ExpectedBazelTargets: []string{
107 makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000108 "static_library": `"libf.so"`,
109 "export_includes": `["testdir/1/"]`,
110 "export_system_includes": `["testdir/2/"]`,
111 }),
112 // TODO(b/229374533): When fixed, update this test
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000113 makeBazelTarget("prebuilt_library_shared", "libtest", AttrNameToString{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000114 "shared_library": `"libf.so"`,
115 }),
116 },
117 })
118}
119
120func TestPrebuiltLibrarySharedStanzaFails(t *testing.T) {
121 runBp2BuildTestCaseSimple(t,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000122 Bp2buildTestCase{
123 Description: "prebuilt library with shared stanza fails because multiple sources",
124 ModuleTypeUnderTest: "cc_prebuilt_library",
125 ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
126 Filesystem: map[string]string{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000127 "libf.so": "",
128 "libg.so": "",
129 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000130 Blueprint: `
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000131cc_prebuilt_library {
132 name: "libtest",
133 srcs: ["libf.so"],
134 shared: {
135 srcs: ["libg.so"],
136 },
137 bazel_module: { bp2build_available: true },
138}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000139 ExpectedErr: fmt.Errorf("Expected at most one source file"),
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000140 })
141}
142
143func TestPrebuiltLibraryStaticStanzaFails(t *testing.T) {
144 runBp2BuildTestCaseSimple(t,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000145 Bp2buildTestCase{
146 Description: "prebuilt library with static stanza fails because multiple sources",
147 ModuleTypeUnderTest: "cc_prebuilt_library",
148 ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
149 Filesystem: map[string]string{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000150 "libf.so": "",
151 "libg.so": "",
152 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000153 Blueprint: `
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000154cc_prebuilt_library {
155 name: "libtest",
156 srcs: ["libf.so"],
157 static: {
158 srcs: ["libg.so"],
159 },
160 bazel_module: { bp2build_available: true },
161}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000162 ExpectedErr: fmt.Errorf("Expected at most one source file"),
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000163 })
164}
165
166func TestPrebuiltLibrarySharedAndStaticStanzas(t *testing.T) {
167 runBp2BuildTestCaseSimple(t,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000168 Bp2buildTestCase{
169 Description: "prebuilt library with both shared and static stanzas",
170 ModuleTypeUnderTest: "cc_prebuilt_library",
171 ModuleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
172 Filesystem: map[string]string{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000173 "libf.so": "",
174 "libg.so": "",
175 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000176 Blueprint: `
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000177cc_prebuilt_library {
178 name: "libtest",
179 static: {
180 srcs: ["libf.so"],
181 },
182 shared: {
183 srcs: ["libg.so"],
184 },
185 bazel_module: { bp2build_available: true },
186}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000187 ExpectedBazelTargets: []string{
188 makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", AttrNameToString{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000189 "static_library": `"libf.so"`,
190 }),
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000191 makeBazelTarget("prebuilt_library_shared", "libtest", AttrNameToString{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000192 "shared_library": `"libg.so"`,
193 }),
194 },
195 })
196}
197
198// TODO(b/228623543): When this bug is fixed, enable this test
199//func TestPrebuiltLibraryOnlyShared(t *testing.T) {
200// runBp2BuildTestCaseSimple(t,
201// bp2buildTestCase{
202// description: "prebuilt library shared only",
203// moduleTypeUnderTest: "cc_prebuilt_library",
204// moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
205// filesystem: map[string]string{
206// "libf.so": "",
207// },
208// blueprint: `
209//cc_prebuilt_library {
210// name: "libtest",
211// srcs: ["libf.so"],
212// static: {
213// enabled: false,
214// },
215// bazel_module: { bp2build_available: true },
216//}`,
217// expectedBazelTargets: []string{
218// makeBazelTarget("prebuilt_library_shared", "libtest", attrNameToString{
219// "shared_library": `"libf.so"`,
220// }),
221// },
222// })
223//}
224
225// TODO(b/228623543): When this bug is fixed, enable this test
226//func TestPrebuiltLibraryOnlyStatic(t *testing.T) {
227// runBp2BuildTestCaseSimple(t,
228// bp2buildTestCase{
229// description: "prebuilt library static only",
230// moduleTypeUnderTest: "cc_prebuilt_library",
231// moduleTypeUnderTestFactory: cc.PrebuiltLibraryFactory,
232// filesystem: map[string]string{
233// "libf.so": "",
234// },
235// blueprint: `
236//cc_prebuilt_library {
237// name: "libtest",
238// srcs: ["libf.so"],
239// shared: {
240// enabled: false,
241// },
242// bazel_module: { bp2build_available: true },
243//}`,
244// expectedBazelTargets: []string{
245// makeBazelTarget("prebuilt_library_static", "libtest_bp2build_cc_library_static", attrNameToString{
246// "static_library": `"libf.so"`,
247// }),
248// },
249// })
250//}