blob: 4b6e888de8523cc4b79add9d48dfbfa5a51365a1 [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 Chened9c17d2021-04-13 07:14:55 +0000112 copts = [
113 "-Wall",
114 "-I.",
115 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000116 deps = [":some-headers"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000117 hdrs = ["foo-dir/a.h"],
118 includes = ["foo-dir"],
119 linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
120 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
121 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
122 "//conditions:default": [],
123 }),
124 srcs = [
125 "impl.cpp",
Jingwen Chen63930982021-03-24 10:04:33 -0400126 "header.h",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000127 "foo-dir/a.h",
Jingwen Chen63930982021-03-24 10:04:33 -0400128 "header.hh",
129 "header.hpp",
130 "header.hxx",
131 "header.h++",
132 "header.inl",
133 "header.inc",
134 "header.ipp",
135 "header.h.generic",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000136 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000137 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
138 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400139 "//conditions:default": [],
140 }) + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000141 "//build/bazel/platforms/os:android": ["android.cpp"],
142 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
143 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400144 "//conditions:default": [],
145 }),
146)`},
147 },
148 {
149 description: "cc_library - trimmed example of //bionic/linker:ld-android",
150 moduleTypeUnderTest: "cc_library",
151 moduleTypeUnderTestFactory: cc.LibraryFactory,
152 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
153 filesystem: map[string]string{
154 "ld-android.cpp": "",
155 "linked_list.h": "",
156 "linker.h": "",
157 "linker_block_allocator.h": "",
158 "linker_cfi.h": "",
159 },
160 bp: soongCcLibraryPreamble + `
161cc_library_headers { name: "libc_headers" }
162cc_library {
163 name: "fake-ld-android",
164 srcs: ["ld_android.cpp"],
165 cflags: [
166 "-Wall",
167 "-Wextra",
168 "-Wunused",
169 "-Werror",
170 ],
171 header_libs: ["libc_headers"],
172 ldflags: [
173 "-Wl,--exclude-libs=libgcc.a",
174 "-Wl,--exclude-libs=libgcc_stripped.a",
175 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
176 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
177 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
178 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
179 ],
180 arch: {
181 x86: {
182 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
183 },
184 x86_64: {
185 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
186 },
187 },
188}
189`,
190 expectedBazelTargets: []string{`cc_library(
191 name = "fake-ld-android",
192 copts = [
193 "-Wall",
194 "-Wextra",
195 "-Wunused",
196 "-Werror",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000197 "-I.",
Jingwen Chen63930982021-03-24 10:04:33 -0400198 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000199 deps = [":libc_headers"],
Jingwen Chen63930982021-03-24 10:04:33 -0400200 linkopts = [
201 "-Wl,--exclude-libs=libgcc.a",
202 "-Wl,--exclude-libs=libgcc_stripped.a",
203 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
204 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
205 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
206 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
207 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000208 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
209 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400210 "//conditions:default": [],
211 }),
Jingwen Chened9c17d2021-04-13 07:14:55 +0000212 srcs = [
213 "ld_android.cpp",
214 "linked_list.h",
215 "linker.h",
216 "linker_block_allocator.h",
217 "linker_cfi.h",
218 ],
Jingwen Chen63930982021-03-24 10:04:33 -0400219)`},
220 },
221 }
222
223 dir := "."
224 for _, testCase := range testCases {
225 filesystem := make(map[string][]byte)
226 toParse := []string{
227 "Android.bp",
228 }
229 for f, content := range testCase.filesystem {
230 if strings.HasSuffix(f, "Android.bp") {
231 toParse = append(toParse, f)
232 }
233 filesystem[f] = []byte(content)
234 }
235 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
236 ctx := android.NewTestContext(config)
237
238 cc.RegisterCCBuildComponents(ctx)
239 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
240 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
241 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
242 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
243 ctx.RegisterBp2BuildConfig(bp2buildConfig) // TODO(jingwen): make this the default for all tests
244 ctx.RegisterForBazelConversion()
245
246 _, errs := ctx.ParseFileList(dir, toParse)
247 if Errored(t, testCase.description, errs) {
248 continue
249 }
250 _, errs = ctx.ResolveDependencies(config)
251 if Errored(t, testCase.description, errs) {
252 continue
253 }
254
255 checkDir := dir
256 if testCase.dir != "" {
257 checkDir = testCase.dir
258 }
259 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
260 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
261 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
262 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
263 } else {
264 for i, target := range bazelTargets {
265 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
266 t.Errorf(
267 "%s: Expected generated Bazel target to be '%s', got '%s'",
268 testCase.description,
269 w,
270 g,
271 )
272 }
273 }
274 }
275 }
276}