blob: 903c81634d229ae09380d0f1a4cba0900d4fe474 [file] [log] [blame]
Colin Crossc5075e92022-12-05 16:46:39 -08001// 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 runCcPrebuiltObjectTestCase(t *testing.T, testCase Bp2buildTestCase) {
24 t.Helper()
25 description := fmt.Sprintf("cc_prebuilt_object: %s", testCase.Description)
26 testCase.ModuleTypeUnderTest = "cc_prebuilt_object"
27 testCase.ModuleTypeUnderTestFactory = cc.PrebuiltObjectFactory
28 testCase.Description = description
29 t.Run(description, func(t *testing.T) {
30 t.Helper()
31 RunBp2BuildTestCaseSimple(t, testCase)
32 })
33}
34
35func TestPrebuiltObject(t *testing.T) {
36 runCcPrebuiltObjectTestCase(t,
37 Bp2buildTestCase{
38 Description: "simple",
39 Filesystem: map[string]string{
40 "obj.o": "",
41 },
42 Blueprint: `
43cc_prebuilt_object {
44 name: "objtest",
45 srcs: ["obj.o"],
46 bazel_module: { bp2build_available: true },
47}`,
48 ExpectedBazelTargets: []string{
49 MakeBazelTarget("cc_prebuilt_object", "objtest", AttrNameToString{
50 "src": `"obj.o"`,
51 })},
52 })
53}
54
55func TestPrebuiltObjectWithArchVariance(t *testing.T) {
56 runCcPrebuiltObjectTestCase(t,
57 Bp2buildTestCase{
58 Description: "with arch variance",
59 Filesystem: map[string]string{
60 "obja.o": "",
61 "objb.o": "",
62 },
63 Blueprint: `
64cc_prebuilt_object {
65 name: "objtest",
66 arch: {
67 arm64: { srcs: ["obja.o"], },
68 arm: { srcs: ["objb.o"], },
69 },
70 bazel_module: { bp2build_available: true },
71}`, ExpectedBazelTargets: []string{
72 MakeBazelTarget("cc_prebuilt_object", "objtest", AttrNameToString{
73 "src": `select({
74 "//build/bazel/platforms/arch:arm": "objb.o",
75 "//build/bazel/platforms/arch:arm64": "obja.o",
76 "//conditions:default": None,
77 })`,
78 }),
79 },
80 })
81}
82
83func TestPrebuiltObjectMultipleSrcsFails(t *testing.T) {
84 runCcPrebuiltObjectTestCase(t,
85 Bp2buildTestCase{
86 Description: "fails because multiple sources",
87 Filesystem: map[string]string{
88 "obja": "",
89 "objb": "",
90 },
91 Blueprint: `
92cc_prebuilt_object {
93 name: "objtest",
94 srcs: ["obja.o", "objb.o"],
95 bazel_module: { bp2build_available: true },
96}`,
97 ExpectedErr: fmt.Errorf("Expected at most one source file"),
98 })
99}
100
101// TODO: nosrcs test