• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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("cc_prebuilt_library_static", "libtest", AttrNameToString{
40					"static_library": `"libf.so"`,
41				}),
42				MakeBazelTarget("cc_prebuilt_library_static", "libtest_alwayslink", AttrNameToString{
43					"static_library": `"libf.so"`,
44					"alwayslink":     "True",
45				}),
46			},
47		})
48}
49
50func TestStaticPrebuiltLibraryWithArchVariance(t *testing.T) {
51	RunBp2BuildTestCaseSimple(t,
52		Bp2buildTestCase{
53			Description:                "prebuilt library static with arch variance",
54			ModuleTypeUnderTest:        "cc_prebuilt_library_static",
55			ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory,
56			Filesystem: map[string]string{
57				"libf.so": "",
58				"libg.so": "",
59			},
60			Blueprint: `
61cc_prebuilt_library_static {
62	name: "libtest",
63	arch: {
64		arm64: { srcs: ["libf.so"], },
65		arm: { srcs: ["libg.so"], },
66	},
67	bazel_module: { bp2build_available: true },
68}`,
69			ExpectedBazelTargets: []string{
70				MakeBazelTarget("cc_prebuilt_library_static", "libtest", AttrNameToString{
71					"static_library": `select({
72        "//build/bazel/platforms/arch:arm": "libg.so",
73        "//build/bazel/platforms/arch:arm64": "libf.so",
74        "//conditions:default": None,
75    })`}),
76				MakeBazelTarget("cc_prebuilt_library_static", "libtest_alwayslink", AttrNameToString{
77					"alwayslink": "True",
78					"static_library": `select({
79        "//build/bazel/platforms/arch:arm": "libg.so",
80        "//build/bazel/platforms/arch:arm64": "libf.so",
81        "//conditions:default": None,
82    })`}),
83			},
84		})
85}
86
87func TestStaticPrebuiltLibraryStaticStanzaFails(t *testing.T) {
88	RunBp2BuildTestCaseSimple(t,
89		Bp2buildTestCase{
90			Description:                "prebuilt library with static stanza fails because multiple sources",
91			ModuleTypeUnderTest:        "cc_prebuilt_library_static",
92			ModuleTypeUnderTestFactory: cc.PrebuiltStaticLibraryFactory,
93			Filesystem: map[string]string{
94				"libf.so": "",
95				"libg.so": "",
96			},
97			Blueprint: `
98cc_prebuilt_library_static {
99	name: "libtest",
100	srcs: ["libf.so"],
101	static: {
102		srcs: ["libg.so"],
103	},
104	bazel_module: { bp2build_available: true },
105}`,
106			ExpectedErr: fmt.Errorf("Expected at most one source file"),
107		})
108}
109
110func TestCcLibraryStaticConvertLex(t *testing.T) {
111	runCcLibrarySharedTestCase(t, Bp2buildTestCase{
112		Description:                "cc_library_static with lex files",
113		ModuleTypeUnderTest:        "cc_library_static",
114		ModuleTypeUnderTestFactory: cc.LibraryStaticFactory,
115		Filesystem: map[string]string{
116			"foo.c":   "",
117			"bar.cc":  "",
118			"foo1.l":  "",
119			"bar1.ll": "",
120			"foo2.l":  "",
121			"bar2.ll": "",
122		},
123		Blueprint: `cc_library_static {
124	name: "foo_lib",
125	srcs: ["foo.c", "bar.cc", "foo1.l", "foo2.l", "bar1.ll", "bar2.ll"],
126	lex: { flags: ["--foo_flags"] },
127	include_build_directory: false,
128	bazel_module: { bp2build_available: true },
129}`,
130		ExpectedBazelTargets: []string{
131			MakeBazelTarget("genlex", "foo_lib_genlex_l", AttrNameToString{
132				"srcs": `[
133        "foo1.l",
134        "foo2.l",
135    ]`,
136				"lexopts": `["--foo_flags"]`,
137			}),
138			MakeBazelTarget("genlex", "foo_lib_genlex_ll", AttrNameToString{
139				"srcs": `[
140        "bar1.ll",
141        "bar2.ll",
142    ]`,
143				"lexopts": `["--foo_flags"]`,
144			}),
145			MakeBazelTarget("cc_library_static", "foo_lib", AttrNameToString{
146				"srcs": `[
147        "bar.cc",
148        ":foo_lib_genlex_ll",
149    ]`,
150				"srcs_c": `[
151        "foo.c",
152        ":foo_lib_genlex_l",
153    ]`,
154			}),
155		},
156	})
157}
158