blob: 7c224f14cfc4e6fc882955dc9507e3cf7ffadaf0 [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 TestStaticPrebuiltLibrary(t *testing.T) {
24 runBp2BuildTestCaseSimple(t,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000025 Bp2buildTestCase{
26 Description: "prebuilt library static simple",
27 ModuleTypeUnderTest: "cc_prebuilt_library_static",
28 ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory,
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_static {
34 name: "libtest",
35 srcs: ["libf.so"],
36 bazel_module: { bp2build_available: true },
37}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000038 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000039 MakeBazelTarget("prebuilt_library_static", "libtest", AttrNameToString{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000040 "static_library": `"libf.so"`,
41 }),
42 },
43 })
44}
45
46func TestStaticPrebuiltLibraryWithArchVariance(t *testing.T) {
47 runBp2BuildTestCaseSimple(t,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000048 Bp2buildTestCase{
49 Description: "prebuilt library static with arch variance",
50 ModuleTypeUnderTest: "cc_prebuilt_library_static",
51 ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory,
52 Filesystem: map[string]string{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000053 "libf.so": "",
54 "libg.so": "",
55 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000056 Blueprint: `
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000057cc_prebuilt_library_static {
58 name: "libtest",
59 arch: {
60 arm64: { srcs: ["libf.so"], },
61 arm: { srcs: ["libg.so"], },
62 },
63 bazel_module: { bp2build_available: true },
64}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000065 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +000066 MakeBazelTarget("prebuilt_library_static", "libtest", AttrNameToString{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000067 "static_library": `select({
68 "//build/bazel/platforms/arch:arm": "libg.so",
69 "//build/bazel/platforms/arch:arm64": "libf.so",
70 "//conditions:default": None,
71 })`,
72 }),
73 },
74 })
75}
76
77func TestStaticPrebuiltLibraryStaticStanzaFails(t *testing.T) {
78 runBp2BuildTestCaseSimple(t,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000079 Bp2buildTestCase{
80 Description: "prebuilt library with static stanza fails because multiple sources",
81 ModuleTypeUnderTest: "cc_prebuilt_library_static",
82 ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory,
83 Filesystem: map[string]string{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000084 "libf.so": "",
85 "libg.so": "",
86 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +000087 Blueprint: `
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000088cc_prebuilt_library_static {
89 name: "libtest",
90 srcs: ["libf.so"],
91 static: {
92 srcs: ["libg.so"],
93 },
94 bazel_module: { bp2build_available: true },
95}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +000096 ExpectedErr: fmt.Errorf("Expected at most one source file"),
Trevor Radcliffe58ea4512022-04-07 20:36:39 +000097 })
98}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +000099
100func TestCcLibraryStaticConvertLex(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000101 runCcLibrarySharedTestCase(t, Bp2buildTestCase{
102 Description: "cc_library_static with lex files",
103 ModuleTypeUnderTest: "cc_library_static",
104 ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
105 Filesystem: map[string]string{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000106 "foo.c": "",
107 "bar.cc": "",
108 "foo1.l": "",
109 "bar1.ll": "",
110 "foo2.l": "",
111 "bar2.ll": "",
112 },
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000113 Blueprint: `cc_library_static {
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000114 name: "foo_lib",
115 srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
116 lex: { flags: ["--foo_flags"] },
117 include_build_directory: false,
118 bazel_module: { bp2build_available: true },
119}`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000120 ExpectedBazelTargets: []string{
Alixe06d75b2022-08-31 18:28:19 +0000121 MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000122 "srcs": `[
123 "foo1.l",
124 "foo2.l",
125 ]`,
126 "lexopts": `["--foo_flags"]`,
127 }),
Alixe06d75b2022-08-31 18:28:19 +0000128 MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000129 "srcs": `[
130 "bar1.ll",
131 "bar2.ll",
132 ]`,
133 "lexopts": `["--foo_flags"]`,
134 }),
Alixe06d75b2022-08-31 18:28:19 +0000135 MakeBazelTarget("cc_library_static", "foo_lib", AttrNameToString{
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000136 "srcs": `[
137 "bar.cc",
138 ":foo_lib_genlex_ll",
139 ]`,
140 "srcs_c": `[
141 "foo.c",
142 ":foo_lib_genlex_l",
143 ]`,
144 }),
145 },
146 })
147}