blob: 5bf5c802f67b12b4adaf50bfcbce05193542bc74 [file] [log] [blame]
Rupert Shuttleworth54e78412021-02-15 11:04:32 +00001// Copyright 2021 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.
14
15package bp2build
16
17import (
18 "android/soong/android"
19 "android/soong/cc"
20 "strings"
21 "testing"
22)
23
24const (
25 // See cc/testing.go for more context
26 soongCcLibraryPreamble = `
27cc_defaults {
28 name: "linux_bionic_supported",
29}
30
31toolchain_library {
32 name: "libclang_rt.builtins-x86_64-android",
33 defaults: ["linux_bionic_supported"],
34 vendor_available: true,
35 vendor_ramdisk_available: true,
36 product_available: true,
37 recovery_available: true,
38 native_bridge_supported: true,
39 src: "",
40}
41
42toolchain_library {
43 name: "libatomic",
44 defaults: ["linux_bionic_supported"],
45 vendor_available: true,
46 vendor_ramdisk_available: true,
47 product_available: true,
48 recovery_available: true,
49 native_bridge_supported: true,
50 src: "",
51}`
52)
53
54func TestCcLibraryHeadersLoadStatement(t *testing.T) {
55 testCases := []struct {
56 bazelTargets BazelTargets
57 expectedLoadStatements string
58 }{
59 {
60 bazelTargets: BazelTargets{
61 BazelTarget{
62 name: "cc_library_headers_target",
63 ruleClass: "cc_library_headers",
64 // Note: no bzlLoadLocation for native rules
65 },
66 },
67 expectedLoadStatements: ``,
68 },
69 }
70
71 for _, testCase := range testCases {
72 actual := testCase.bazelTargets.LoadStatements()
73 expected := testCase.expectedLoadStatements
74 if actual != expected {
75 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
76 }
77 }
78
79}
80
81func TestCcLibraryHeadersBp2Build(t *testing.T) {
82 testCases := []struct {
83 description string
84 moduleTypeUnderTest string
85 moduleTypeUnderTestFactory android.ModuleFactory
86 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
87 preArchMutators []android.RegisterMutatorFunc
88 depsMutators []android.RegisterMutatorFunc
89 bp string
90 expectedBazelTargets []string
91 filesystem map[string]string
92 dir string
93 }{
94 {
95 description: "cc_library_headers test",
96 moduleTypeUnderTest: "cc_library_headers",
97 moduleTypeUnderTestFactory: cc.LibraryHeaderFactory,
98 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryHeadersBp2Build,
99 filesystem: map[string]string{
100 "lib-1/lib1a.h": "",
101 "lib-1/lib1b.h": "",
102 "lib-2/lib2a.h": "",
103 "lib-2/lib2b.h": "",
104 "dir-1/dir1a.h": "",
105 "dir-1/dir1b.h": "",
106 "dir-2/dir2a.h": "",
107 "dir-2/dir2b.h": "",
108 },
109 bp: soongCcLibraryPreamble + `
110cc_library_headers {
111 name: "lib-1",
112 export_include_dirs: ["lib-1"],
113 bazel_module: { bp2build_available: true },
114}
115
116cc_library_headers {
117 name: "lib-2",
118 export_include_dirs: ["lib-2"],
119 bazel_module: { bp2build_available: true },
120}
121
122cc_library_headers {
123 name: "foo_headers",
124 export_include_dirs: ["dir-1", "dir-2"],
125 header_libs: ["lib-1", "lib-2"],
126 export_header_lib_headers: ["lib-1", "lib-2"],
127 bazel_module: { bp2build_available: true },
128}`,
129 expectedBazelTargets: []string{`cc_library_headers(
130 name = "foo_headers",
131 deps = [
132 ":lib-1",
133 ":lib-2",
134 ],
135 hdrs = [
136 "dir-1/dir1a.h",
137 "dir-1/dir1b.h",
138 "dir-2/dir2a.h",
139 "dir-2/dir2b.h",
140 ],
141 includes = [
142 "dir-1",
143 "dir-2",
144 ],
145)`, `cc_library_headers(
146 name = "lib-1",
147 hdrs = [
148 "lib-1/lib1a.h",
149 "lib-1/lib1b.h",
150 ],
151 includes = [
152 "lib-1",
153 ],
154)`, `cc_library_headers(
155 name = "lib-2",
156 hdrs = [
157 "lib-2/lib2a.h",
158 "lib-2/lib2b.h",
159 ],
160 includes = [
161 "lib-2",
162 ],
163)`},
164 },
165 }
166
167 dir := "."
168 for _, testCase := range testCases {
169 filesystem := make(map[string][]byte)
170 toParse := []string{
171 "Android.bp",
172 }
173 for f, content := range testCase.filesystem {
174 if strings.HasSuffix(f, "Android.bp") {
175 toParse = append(toParse, f)
176 }
177 filesystem[f] = []byte(content)
178 }
179 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
180 ctx := android.NewTestContext(config)
181
182 cc.RegisterCCBuildComponents(ctx)
183 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
184
185 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
186 for _, m := range testCase.depsMutators {
187 ctx.DepsBp2BuildMutators(m)
188 }
189 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
190 ctx.RegisterForBazelConversion()
191
192 _, errs := ctx.ParseFileList(dir, toParse)
193 if Errored(t, testCase.description, errs) {
194 continue
195 }
196 _, errs = ctx.ResolveDependencies(config)
197 if Errored(t, testCase.description, errs) {
198 continue
199 }
200
201 checkDir := dir
202 if testCase.dir != "" {
203 checkDir = testCase.dir
204 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500205 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500206 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000207 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
208 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
209 } else {
210 for i, target := range bazelTargets {
211 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
212 t.Errorf(
213 "%s: Expected generated Bazel target to be '%s', got '%s'",
214 testCase.description,
215 w,
216 g,
217 )
218 }
219 }
220 }
221 }
222}