blob: 783af2e7c452fee7a66a9922477cc3465ff35813 [file] [log] [blame]
Jingwen Chen63930982021-03-24 10:04:33 -04001// 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)
42
43func TestCcLibraryBp2Build(t *testing.T) {
44 testCases := []struct {
45 description string
46 moduleTypeUnderTest string
47 moduleTypeUnderTestFactory android.ModuleFactory
48 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
49 bp string
50 expectedBazelTargets []string
51 filesystem map[string]string
52 dir string
53 }{
54 {
55 description: "cc_library - simple example",
56 moduleTypeUnderTest: "cc_library",
57 moduleTypeUnderTestFactory: cc.LibraryFactory,
58 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
59 filesystem: map[string]string{
60 "android.cpp": "",
61 "darwin.cpp": "",
62 // Refer to cc.headerExts for the supported header extensions in Soong.
63 "header.h": "",
64 "header.hh": "",
65 "header.hpp": "",
66 "header.hxx": "",
67 "header.h++": "",
68 "header.inl": "",
69 "header.inc": "",
70 "header.ipp": "",
71 "header.h.generic": "",
72 "impl.cpp": "",
73 "linux.cpp": "",
74 "x86.cpp": "",
75 "x86_64.cpp": "",
76 "foo-dir/a.h": "",
77 },
78 bp: soongCcLibraryPreamble + `
79cc_library_headers { name: "some-headers" }
80cc_library {
81 name: "foo-lib",
82 srcs: ["impl.cpp"],
83 cflags: ["-Wall"],
84 header_libs: ["some-headers"],
85 export_include_dirs: ["foo-dir"],
86 ldflags: ["-Wl,--exclude-libs=bar.a"],
87 arch: {
88 x86: {
89 ldflags: ["-Wl,--exclude-libs=baz.a"],
90 srcs: ["x86.cpp"],
91 },
92 x86_64: {
93 ldflags: ["-Wl,--exclude-libs=qux.a"],
94 srcs: ["x86_64.cpp"],
95 },
96 },
97 target: {
98 android: {
99 srcs: ["android.cpp"],
100 },
101 linux_glibc: {
102 srcs: ["linux.cpp"],
103 },
104 darwin: {
105 srcs: ["darwin.cpp"],
106 },
107 },
108}
109`,
110 expectedBazelTargets: []string{`cc_library(
111 name = "foo-lib",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000112 copts = ["-Wall"],
113 deps = [":some-headers"],
Jingwen Chen63930982021-03-24 10:04:33 -0400114 hdrs = [
115 "header.h",
116 "header.hh",
117 "header.hpp",
118 "header.hxx",
119 "header.h++",
120 "header.inl",
121 "header.inc",
122 "header.ipp",
123 "header.h.generic",
124 "foo-dir/a.h",
125 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000126 includes = ["foo-dir"],
127 linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
128 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
129 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400130 "//conditions:default": [],
131 }),
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000132 srcs = ["impl.cpp"] + select({
133 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
134 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400135 "//conditions:default": [],
136 }) + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000137 "//build/bazel/platforms/os:android": ["android.cpp"],
138 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
139 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400140 "//conditions:default": [],
141 }),
142)`},
143 },
144 {
145 description: "cc_library - trimmed example of //bionic/linker:ld-android",
146 moduleTypeUnderTest: "cc_library",
147 moduleTypeUnderTestFactory: cc.LibraryFactory,
148 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
149 filesystem: map[string]string{
150 "ld-android.cpp": "",
151 "linked_list.h": "",
152 "linker.h": "",
153 "linker_block_allocator.h": "",
154 "linker_cfi.h": "",
155 },
156 bp: soongCcLibraryPreamble + `
157cc_library_headers { name: "libc_headers" }
158cc_library {
159 name: "fake-ld-android",
160 srcs: ["ld_android.cpp"],
161 cflags: [
162 "-Wall",
163 "-Wextra",
164 "-Wunused",
165 "-Werror",
166 ],
167 header_libs: ["libc_headers"],
168 ldflags: [
169 "-Wl,--exclude-libs=libgcc.a",
170 "-Wl,--exclude-libs=libgcc_stripped.a",
171 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
172 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
173 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
174 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
175 ],
176 arch: {
177 x86: {
178 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
179 },
180 x86_64: {
181 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
182 },
183 },
184}
185`,
186 expectedBazelTargets: []string{`cc_library(
187 name = "fake-ld-android",
188 copts = [
189 "-Wall",
190 "-Wextra",
191 "-Wunused",
192 "-Werror",
193 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000194 deps = [":libc_headers"],
Jingwen Chen63930982021-03-24 10:04:33 -0400195 hdrs = [
196 "linked_list.h",
197 "linker.h",
198 "linker_block_allocator.h",
199 "linker_cfi.h",
200 ],
201 linkopts = [
202 "-Wl,--exclude-libs=libgcc.a",
203 "-Wl,--exclude-libs=libgcc_stripped.a",
204 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
205 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
206 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
207 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
208 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000209 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
210 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400211 "//conditions:default": [],
212 }),
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000213 srcs = ["ld_android.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400214)`},
215 },
216 }
217
218 dir := "."
219 for _, testCase := range testCases {
220 filesystem := make(map[string][]byte)
221 toParse := []string{
222 "Android.bp",
223 }
224 for f, content := range testCase.filesystem {
225 if strings.HasSuffix(f, "Android.bp") {
226 toParse = append(toParse, f)
227 }
228 filesystem[f] = []byte(content)
229 }
230 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
231 ctx := android.NewTestContext(config)
232
233 cc.RegisterCCBuildComponents(ctx)
234 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
235 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
236 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
237 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
238 ctx.RegisterBp2BuildConfig(bp2buildConfig) // TODO(jingwen): make this the default for all tests
239 ctx.RegisterForBazelConversion()
240
241 _, errs := ctx.ParseFileList(dir, toParse)
242 if Errored(t, testCase.description, errs) {
243 continue
244 }
245 _, errs = ctx.ResolveDependencies(config)
246 if Errored(t, testCase.description, errs) {
247 continue
248 }
249
250 checkDir := dir
251 if testCase.dir != "" {
252 checkDir = testCase.dir
253 }
254 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
255 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
256 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
257 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
258 } else {
259 for i, target := range bazelTargets {
260 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
261 t.Errorf(
262 "%s: Expected generated Bazel target to be '%s', got '%s'",
263 testCase.description,
264 w,
265 g,
266 )
267 }
268 }
269 }
270 }
271}