blob: 59839c884694d6f0da9a9ae189b5519ebe645004 [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//
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.
14package bp2build
15
16import (
17 "fmt"
18 "testing"
19
20 "android/soong/cc"
21)
22
23func TestStaticPrebuiltLibrary(t *testing.T) {
24 runBp2BuildTestCaseSimple(t,
25 bp2buildTestCase{
26 description: "prebuilt library static simple",
27 moduleTypeUnderTest: "cc_prebuilt_library_static",
28 moduleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory,
29 filesystem: map[string]string{
30 "libf.so": "",
31 },
32 blueprint: `
33cc_prebuilt_library_static {
34 name: "libtest",
35 srcs: ["libf.so"],
36 bazel_module: { bp2build_available: true },
37}`,
38 expectedBazelTargets: []string{
39 makeBazelTarget("prebuilt_library_static", "libtest", attrNameToString{
40 "static_library": `"libf.so"`,
41 }),
42 },
43 })
44}
45
46func TestStaticPrebuiltLibraryWithArchVariance(t *testing.T) {
47 runBp2BuildTestCaseSimple(t,
48 bp2buildTestCase{
49 description: "prebuilt library static with arch variance",
50 moduleTypeUnderTest: "cc_prebuilt_library_static",
51 moduleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory,
52 filesystem: map[string]string{
53 "libf.so": "",
54 "libg.so": "",
55 },
56 blueprint: `
57cc_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}`,
65 expectedBazelTargets: []string{
66 makeBazelTarget("prebuilt_library_static", "libtest", attrNameToString{
67 "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,
79 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{
84 "libf.so": "",
85 "libg.so": "",
86 },
87 blueprint: `
88cc_prebuilt_library_static {
89 name: "libtest",
90 srcs: ["libf.so"],
91 static: {
92 srcs: ["libg.so"],
93 },
94 bazel_module: { bp2build_available: true },
95}`,
96 expectedErr: fmt.Errorf("Expected at most one source file"),
97 })
98}
Trevor Radcliffeef9c9002022-05-13 20:55:35 +000099
100func TestCcLibraryStaticConvertLex(t *testing.T) {
101 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{
106 "foo.c": "",
107 "bar.cc": "",
108 "foo1.l": "",
109 "bar1.ll": "",
110 "foo2.l": "",
111 "bar2.ll": "",
112 },
113 blueprint: `cc_library_static {
114 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}`,
120 expectedBazelTargets: []string{
121 makeBazelTarget("genlex", "foo_lib_genlex_l", attrNameToString{
122 "srcs": `[
123 "foo1.l",
124 "foo2.l",
125 ]`,
126 "lexopts": `["--foo_flags"]`,
127 }),
128 makeBazelTarget("genlex", "foo_lib_genlex_ll", attrNameToString{
129 "srcs": `[
130 "bar1.ll",
131 "bar2.ll",
132 ]`,
133 "lexopts": `["--foo_flags"]`,
134 }),
135 makeBazelTarget("cc_library_static", "foo_lib", attrNameToString{
136 "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}