blob: 869be20c245869e26e1eff2d73d751865fe2c6da [file] [log] [blame]
Jingwen Chen8c1b97e2021-02-18 03:21:34 -05001// 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 (
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020018 "testing"
19
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050020 "android/soong/android"
21 "android/soong/cc"
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050022)
23
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020024func registerCcObjectModuleTypes(ctx android.RegistrationContext) {
25 // Always register cc_defaults module factory
26 ctx.RegisterModuleType("cc_defaults", func() android.Module { return cc.DefaultsFactory() })
27}
28
29func runCcObjectTestCase(t *testing.T, tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040030 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020031 runBp2BuildTestCase(t, registerCcObjectModuleTypes, tc)
32}
33
34func TestCcObjectSimple(t *testing.T) {
35 runCcObjectTestCase(t, bp2buildTestCase{
36 description: "simple cc_object generates cc_object with include header dep",
37 moduleTypeUnderTest: "cc_object",
38 moduleTypeUnderTestFactory: cc.ObjectFactory,
39 moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
40 filesystem: map[string]string{
41 "a/b/foo.h": "",
42 "a/b/bar.h": "",
43 "a/b/exclude.c": "",
44 "a/b/c.c": "",
45 },
46 blueprint: `cc_object {
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050047 name: "foo",
48 local_include_dirs: ["include"],
Colin Cross6b8f4252021-07-22 11:39:44 -070049 system_shared_libs: [],
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050050 cflags: [
51 "-Wno-gcc-compat",
52 "-Wall",
53 "-Werror",
54 ],
55 srcs: [
Jingwen Chendb120242021-02-23 00:46:47 -050056 "a/b/*.c"
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050057 ],
Jingwen Chendb120242021-02-23 00:46:47 -050058 exclude_srcs: ["a/b/exclude.c"],
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050059}
60`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020061 expectedBazelTargets: []string{`cc_object(
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050062 name = "foo",
63 copts = [
64 "-fno-addrsig",
65 "-Wno-gcc-compat",
66 "-Wall",
67 "-Werror",
Jingwen Chened9c17d2021-04-13 07:14:55 +000068 "-Iinclude",
Chris Parsons484e50a2021-05-13 15:13:04 -040069 "-I$(BINDIR)/include",
Jingwen Chened9c17d2021-04-13 07:14:55 +000070 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -040071 "-I$(BINDIR)/.",
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050072 ],
Jingwen Chen882bcc12021-04-27 05:54:20 +000073 srcs = ["a/b/c.c"],
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050074)`,
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050075 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020076 })
77}
78
79func TestCcObjectDefaults(t *testing.T) {
80 runCcObjectTestCase(t, bp2buildTestCase{
81 description: "simple cc_object with defaults",
82 moduleTypeUnderTest: "cc_object",
83 moduleTypeUnderTestFactory: cc.ObjectFactory,
84 moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
85 blueprint: `cc_object {
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050086 name: "foo",
Colin Cross6b8f4252021-07-22 11:39:44 -070087 system_shared_libs: [],
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050088 local_include_dirs: ["include"],
89 srcs: [
90 "a/b/*.h",
91 "a/b/c.c"
92 ],
93
94 defaults: ["foo_defaults"],
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050095}
96
97cc_defaults {
98 name: "foo_defaults",
99 defaults: ["foo_bar_defaults"],
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500100}
101
102cc_defaults {
103 name: "foo_bar_defaults",
104 cflags: [
105 "-Wno-gcc-compat",
106 "-Wall",
107 "-Werror",
108 ],
109}
110`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200111 expectedBazelTargets: []string{`cc_object(
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500112 name = "foo",
113 copts = [
114 "-Wno-gcc-compat",
115 "-Wall",
116 "-Werror",
117 "-fno-addrsig",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000118 "-Iinclude",
Chris Parsons484e50a2021-05-13 15:13:04 -0400119 "-I$(BINDIR)/include",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000120 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400121 "-I$(BINDIR)/.",
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500122 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000123 srcs = ["a/b/c.c"],
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500124)`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200125 }})
126}
127
128func TestCcObjectCcObjetDepsInObjs(t *testing.T) {
129 runCcObjectTestCase(t, bp2buildTestCase{
130 description: "cc_object with cc_object deps in objs props",
131 moduleTypeUnderTest: "cc_object",
132 moduleTypeUnderTestFactory: cc.ObjectFactory,
133 moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
134 filesystem: map[string]string{
135 "a/b/c.c": "",
136 "x/y/z.c": "",
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500137 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200138 blueprint: `cc_object {
Jingwen Chendb120242021-02-23 00:46:47 -0500139 name: "foo",
Colin Cross6b8f4252021-07-22 11:39:44 -0700140 system_shared_libs: [],
Jingwen Chendb120242021-02-23 00:46:47 -0500141 srcs: ["a/b/c.c"],
142 objs: ["bar"],
Jingwen Chendb120242021-02-23 00:46:47 -0500143}
144
145cc_object {
146 name: "bar",
Colin Cross6b8f4252021-07-22 11:39:44 -0700147 system_shared_libs: [],
Jingwen Chendb120242021-02-23 00:46:47 -0500148 srcs: ["x/y/z.c"],
Jingwen Chendb120242021-02-23 00:46:47 -0500149}
150`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200151 expectedBazelTargets: []string{`cc_object(
Jingwen Chendb120242021-02-23 00:46:47 -0500152 name = "bar",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000153 copts = [
154 "-fno-addrsig",
155 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400156 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000157 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000158 srcs = ["x/y/z.c"],
Jingwen Chendb120242021-02-23 00:46:47 -0500159)`, `cc_object(
160 name = "foo",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000161 copts = [
162 "-fno-addrsig",
163 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400164 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000165 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000166 deps = [":bar"],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000167 srcs = ["a/b/c.c"],
Liz Kammera4aa4302021-03-18 16:56:36 -0400168)`,
Liz Kammera4aa4302021-03-18 16:56:36 -0400169 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200170 })
171}
172
173func TestCcObjectIncludeBuildDirFalse(t *testing.T) {
174 runCcObjectTestCase(t, bp2buildTestCase{
175 description: "cc_object with include_build_dir: false",
176 moduleTypeUnderTest: "cc_object",
177 moduleTypeUnderTestFactory: cc.ObjectFactory,
178 moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
179 filesystem: map[string]string{
180 "a/b/c.c": "",
181 "x/y/z.c": "",
182 },
183 blueprint: `cc_object {
Liz Kammera4aa4302021-03-18 16:56:36 -0400184 name: "foo",
Colin Cross6b8f4252021-07-22 11:39:44 -0700185 system_shared_libs: [],
Liz Kammera4aa4302021-03-18 16:56:36 -0400186 srcs: ["a/b/c.c"],
187 include_build_directory: false,
Liz Kammera4aa4302021-03-18 16:56:36 -0400188}
189`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200190 expectedBazelTargets: []string{`cc_object(
Liz Kammera4aa4302021-03-18 16:56:36 -0400191 name = "foo",
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000192 copts = ["-fno-addrsig"],
193 srcs = ["a/b/c.c"],
Jingwen Chendb120242021-02-23 00:46:47 -0500194)`,
Jingwen Chendb120242021-02-23 00:46:47 -0500195 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200196 })
197}
198
199func TestCcObjectProductVariable(t *testing.T) {
200 runCcObjectTestCase(t, bp2buildTestCase{
201 description: "cc_object with product variable",
202 moduleTypeUnderTest: "cc_object",
203 moduleTypeUnderTestFactory: cc.ObjectFactory,
204 moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
205 blueprint: `cc_object {
Liz Kammera060c452021-03-24 10:14:47 -0400206 name: "foo",
Colin Cross6b8f4252021-07-22 11:39:44 -0700207 system_shared_libs: [],
Liz Kammera060c452021-03-24 10:14:47 -0400208 include_build_directory: false,
209 product_variables: {
210 platform_sdk_version: {
211 asflags: ["-DPLATFORM_SDK_VERSION=%d"],
212 },
213 },
Liz Kammera060c452021-03-24 10:14:47 -0400214}
215`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200216 expectedBazelTargets: []string{`cc_object(
Liz Kammera060c452021-03-24 10:14:47 -0400217 name = "foo",
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400218 asflags = select({
Liz Kammerba7a9c52021-05-26 08:45:30 -0400219 "//build/bazel/product_variables:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"],
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400220 "//conditions:default": [],
221 }),
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000222 copts = ["-fno-addrsig"],
Liz Kammera060c452021-03-24 10:14:47 -0400223)`,
Liz Kammera060c452021-03-24 10:14:47 -0400224 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200225 })
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500226}
Jingwen Chen5d864492021-02-24 07:20:12 -0500227
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200228func TestCcObjectCflagsOneArch(t *testing.T) {
229 runCcObjectTestCase(t, bp2buildTestCase{
230 description: "cc_object setting cflags for one arch",
231 moduleTypeUnderTest: "cc_object",
232 moduleTypeUnderTestFactory: cc.ObjectFactory,
233 moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
234 blueprint: `cc_object {
Jingwen Chen5d864492021-02-24 07:20:12 -0500235 name: "foo",
Colin Cross6b8f4252021-07-22 11:39:44 -0700236 system_shared_libs: [],
Jingwen Chen07027912021-03-15 06:02:43 -0400237 srcs: ["a.cpp"],
Jingwen Chen5d864492021-02-24 07:20:12 -0500238 arch: {
239 x86: {
Jingwen Chen07027912021-03-15 06:02:43 -0400240 cflags: ["-fPIC"], // string list
241 },
242 arm: {
243 srcs: ["arch/arm/file.S"], // label list
Jingwen Chen5d864492021-02-24 07:20:12 -0500244 },
245 },
Jingwen Chen5d864492021-02-24 07:20:12 -0500246}
247`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200248 expectedBazelTargets: []string{
249 `cc_object(
Jingwen Chen5d864492021-02-24 07:20:12 -0500250 name = "foo",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000251 copts = [
252 "-fno-addrsig",
253 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400254 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000255 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000256 "//build/bazel/platforms/arch:x86": ["-fPIC"],
Jingwen Chen07027912021-03-15 06:02:43 -0400257 "//conditions:default": [],
Jingwen Chen5d864492021-02-24 07:20:12 -0500258 }),
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000259 srcs = ["a.cpp"] + select({
260 "//build/bazel/platforms/arch:arm": ["arch/arm/file.S"],
Jingwen Chen07027912021-03-15 06:02:43 -0400261 "//conditions:default": [],
262 }),
Jingwen Chen5d864492021-02-24 07:20:12 -0500263)`,
Jingwen Chen5d864492021-02-24 07:20:12 -0500264 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200265 })
266}
267
268func TestCcObjectCflagsFourArch(t *testing.T) {
269 runCcObjectTestCase(t, bp2buildTestCase{
270 description: "cc_object setting cflags for 4 architectures",
271 moduleTypeUnderTest: "cc_object",
272 moduleTypeUnderTestFactory: cc.ObjectFactory,
273 moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
274 blueprint: `cc_object {
Jingwen Chen5d864492021-02-24 07:20:12 -0500275 name: "foo",
Colin Cross6b8f4252021-07-22 11:39:44 -0700276 system_shared_libs: [],
Jingwen Chen07027912021-03-15 06:02:43 -0400277 srcs: ["base.cpp"],
Jingwen Chen5d864492021-02-24 07:20:12 -0500278 arch: {
279 x86: {
Jingwen Chen07027912021-03-15 06:02:43 -0400280 srcs: ["x86.cpp"],
Jingwen Chen5d864492021-02-24 07:20:12 -0500281 cflags: ["-fPIC"],
282 },
283 x86_64: {
Jingwen Chen07027912021-03-15 06:02:43 -0400284 srcs: ["x86_64.cpp"],
Jingwen Chen5d864492021-02-24 07:20:12 -0500285 cflags: ["-fPIC"],
286 },
287 arm: {
Jingwen Chen07027912021-03-15 06:02:43 -0400288 srcs: ["arm.cpp"],
Jingwen Chen5d864492021-02-24 07:20:12 -0500289 cflags: ["-Wall"],
290 },
291 arm64: {
Jingwen Chen07027912021-03-15 06:02:43 -0400292 srcs: ["arm64.cpp"],
Jingwen Chen5d864492021-02-24 07:20:12 -0500293 cflags: ["-Wall"],
294 },
295 },
Jingwen Chen5d864492021-02-24 07:20:12 -0500296}
297`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200298 expectedBazelTargets: []string{
299 `cc_object(
Jingwen Chen5d864492021-02-24 07:20:12 -0500300 name = "foo",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000301 copts = [
302 "-fno-addrsig",
303 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400304 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000305 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000306 "//build/bazel/platforms/arch:arm": ["-Wall"],
307 "//build/bazel/platforms/arch:arm64": ["-Wall"],
308 "//build/bazel/platforms/arch:x86": ["-fPIC"],
309 "//build/bazel/platforms/arch:x86_64": ["-fPIC"],
Jingwen Chen07027912021-03-15 06:02:43 -0400310 "//conditions:default": [],
Jingwen Chen5d864492021-02-24 07:20:12 -0500311 }),
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000312 srcs = ["base.cpp"] + select({
313 "//build/bazel/platforms/arch:arm": ["arm.cpp"],
314 "//build/bazel/platforms/arch:arm64": ["arm64.cpp"],
315 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
316 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen07027912021-03-15 06:02:43 -0400317 "//conditions:default": [],
318 }),
Jingwen Chen5d864492021-02-24 07:20:12 -0500319)`,
Jingwen Chen5d864492021-02-24 07:20:12 -0500320 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200321 })
322}
323
324func TestCcObjectCflagsMultiOs(t *testing.T) {
325 runCcObjectTestCase(t, bp2buildTestCase{
326 description: "cc_object setting cflags for multiple OSes",
327 moduleTypeUnderTest: "cc_object",
328 moduleTypeUnderTestFactory: cc.ObjectFactory,
329 moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
330 blueprint: `cc_object {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000331 name: "foo",
Colin Cross6b8f4252021-07-22 11:39:44 -0700332 system_shared_libs: [],
Jingwen Chenc1c26502021-04-05 10:35:13 +0000333 srcs: ["base.cpp"],
334 target: {
335 android: {
336 cflags: ["-fPIC"],
337 },
338 windows: {
339 cflags: ["-fPIC"],
340 },
341 darwin: {
342 cflags: ["-Wall"],
343 },
344 },
Jingwen Chenc1c26502021-04-05 10:35:13 +0000345}
346`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200347 expectedBazelTargets: []string{
348 `cc_object(
Jingwen Chenc1c26502021-04-05 10:35:13 +0000349 name = "foo",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000350 copts = [
351 "-fno-addrsig",
352 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400353 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000354 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000355 "//build/bazel/platforms/os:android": ["-fPIC"],
356 "//build/bazel/platforms/os:darwin": ["-Wall"],
357 "//build/bazel/platforms/os:windows": ["-fPIC"],
Jingwen Chenc1c26502021-04-05 10:35:13 +0000358 "//conditions:default": [],
359 }),
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000360 srcs = ["base.cpp"],
Jingwen Chenc1c26502021-04-05 10:35:13 +0000361)`,
Jingwen Chenc1c26502021-04-05 10:35:13 +0000362 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200363 })
Jingwen Chen5d864492021-02-24 07:20:12 -0500364}