blob: 049f84a02d3a560091c61e66538f45458ec3ccf6 [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"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000126
127 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000128 bazel_module: { bp2build_available: true },
129}`,
130 expectedBazelTargets: []string{`cc_library_headers(
131 name = "foo_headers",
132 deps = [
133 ":lib-1",
134 ":lib-2",
135 ],
136 hdrs = [
137 "dir-1/dir1a.h",
138 "dir-1/dir1b.h",
139 "dir-2/dir2a.h",
140 "dir-2/dir2b.h",
141 ],
142 includes = [
143 "dir-1",
144 "dir-2",
145 ],
146)`, `cc_library_headers(
147 name = "lib-1",
148 hdrs = [
149 "lib-1/lib1a.h",
150 "lib-1/lib1b.h",
151 ],
152 includes = [
153 "lib-1",
154 ],
155)`, `cc_library_headers(
156 name = "lib-2",
157 hdrs = [
158 "lib-2/lib2a.h",
159 "lib-2/lib2b.h",
160 ],
161 includes = [
162 "lib-2",
163 ],
164)`},
165 },
166 }
167
168 dir := "."
169 for _, testCase := range testCases {
170 filesystem := make(map[string][]byte)
171 toParse := []string{
172 "Android.bp",
173 }
174 for f, content := range testCase.filesystem {
175 if strings.HasSuffix(f, "Android.bp") {
176 toParse = append(toParse, f)
177 }
178 filesystem[f] = []byte(content)
179 }
180 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
181 ctx := android.NewTestContext(config)
182
183 cc.RegisterCCBuildComponents(ctx)
184 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
185
186 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
187 for _, m := range testCase.depsMutators {
188 ctx.DepsBp2BuildMutators(m)
189 }
190 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
191 ctx.RegisterForBazelConversion()
192
193 _, errs := ctx.ParseFileList(dir, toParse)
194 if Errored(t, testCase.description, errs) {
195 continue
196 }
197 _, errs = ctx.ResolveDependencies(config)
198 if Errored(t, testCase.description, errs) {
199 continue
200 }
201
202 checkDir := dir
203 if testCase.dir != "" {
204 checkDir = testCase.dir
205 }
Jingwen Chen164e0862021-02-19 00:48:40 -0500206 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
Jingwen Chenba369ad2021-02-22 10:19:34 -0500207 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
Rupert Shuttleworth54e78412021-02-15 11:04:32 +0000208 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
209 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
210 } else {
211 for i, target := range bazelTargets {
212 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
213 t.Errorf(
214 "%s: Expected generated Bazel target to be '%s', got '%s'",
215 testCase.description,
216 w,
217 g,
218 )
219 }
220 }
221 }
222 }
223}