2021-02-18 09:21:34 +01:00
|
|
|
// Copyright 2021 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package bp2build
|
|
|
|
|
|
|
|
import (
|
2021-05-21 09:37:00 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-02-18 09:21:34 +01:00
|
|
|
"android/soong/android"
|
|
|
|
"android/soong/cc"
|
|
|
|
)
|
|
|
|
|
2021-05-21 09:37:00 +02:00
|
|
|
func registerCcObjectModuleTypes(ctx android.RegistrationContext) {
|
|
|
|
// Always register cc_defaults module factory
|
|
|
|
ctx.RegisterModuleType("cc_defaults", func() android.Module { return cc.DefaultsFactory() })
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCcObjectTestCase(t *testing.T, tc bp2buildTestCase) {
|
2021-05-25 16:39:35 +02:00
|
|
|
t.Helper()
|
2021-05-21 09:37:00 +02:00
|
|
|
runBp2BuildTestCase(t, registerCcObjectModuleTypes, tc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCcObjectSimple(t *testing.T) {
|
|
|
|
runCcObjectTestCase(t, bp2buildTestCase{
|
|
|
|
description: "simple cc_object generates cc_object with include header dep",
|
|
|
|
moduleTypeUnderTest: "cc_object",
|
|
|
|
moduleTypeUnderTestFactory: cc.ObjectFactory,
|
|
|
|
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
|
|
|
|
filesystem: map[string]string{
|
|
|
|
"a/b/foo.h": "",
|
|
|
|
"a/b/bar.h": "",
|
|
|
|
"a/b/exclude.c": "",
|
|
|
|
"a/b/c.c": "",
|
|
|
|
},
|
|
|
|
blueprint: `cc_object {
|
2021-02-18 09:21:34 +01:00
|
|
|
name: "foo",
|
|
|
|
local_include_dirs: ["include"],
|
|
|
|
cflags: [
|
|
|
|
"-Wno-gcc-compat",
|
|
|
|
"-Wall",
|
|
|
|
"-Werror",
|
|
|
|
],
|
|
|
|
srcs: [
|
2021-02-23 06:46:47 +01:00
|
|
|
"a/b/*.c"
|
2021-02-18 09:21:34 +01:00
|
|
|
],
|
2021-02-23 06:46:47 +01:00
|
|
|
exclude_srcs: ["a/b/exclude.c"],
|
2021-02-18 09:21:34 +01:00
|
|
|
}
|
|
|
|
`,
|
2021-05-21 09:37:00 +02:00
|
|
|
expectedBazelTargets: []string{`cc_object(
|
2021-02-18 09:21:34 +01:00
|
|
|
name = "foo",
|
|
|
|
copts = [
|
|
|
|
"-fno-addrsig",
|
|
|
|
"-Wno-gcc-compat",
|
|
|
|
"-Wall",
|
|
|
|
"-Werror",
|
2021-04-13 09:14:55 +02:00
|
|
|
"-Iinclude",
|
2021-05-13 21:13:04 +02:00
|
|
|
"-I$(BINDIR)/include",
|
2021-04-13 09:14:55 +02:00
|
|
|
"-I.",
|
2021-05-13 21:13:04 +02:00
|
|
|
"-I$(BINDIR)/.",
|
2021-02-18 09:21:34 +01:00
|
|
|
],
|
2021-04-27 07:54:20 +02:00
|
|
|
srcs = ["a/b/c.c"],
|
2021-02-18 09:21:34 +01:00
|
|
|
)`,
|
|
|
|
},
|
2021-05-21 09:37:00 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCcObjectDefaults(t *testing.T) {
|
|
|
|
runCcObjectTestCase(t, bp2buildTestCase{
|
|
|
|
description: "simple cc_object with defaults",
|
|
|
|
moduleTypeUnderTest: "cc_object",
|
|
|
|
moduleTypeUnderTestFactory: cc.ObjectFactory,
|
|
|
|
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
|
|
|
|
blueprint: `cc_object {
|
2021-02-18 09:21:34 +01:00
|
|
|
name: "foo",
|
|
|
|
local_include_dirs: ["include"],
|
|
|
|
srcs: [
|
|
|
|
"a/b/*.h",
|
|
|
|
"a/b/c.c"
|
|
|
|
],
|
|
|
|
|
|
|
|
defaults: ["foo_defaults"],
|
|
|
|
}
|
|
|
|
|
|
|
|
cc_defaults {
|
|
|
|
name: "foo_defaults",
|
|
|
|
defaults: ["foo_bar_defaults"],
|
|
|
|
}
|
|
|
|
|
|
|
|
cc_defaults {
|
|
|
|
name: "foo_bar_defaults",
|
|
|
|
cflags: [
|
|
|
|
"-Wno-gcc-compat",
|
|
|
|
"-Wall",
|
|
|
|
"-Werror",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
`,
|
2021-05-21 09:37:00 +02:00
|
|
|
expectedBazelTargets: []string{`cc_object(
|
2021-02-18 09:21:34 +01:00
|
|
|
name = "foo",
|
|
|
|
copts = [
|
|
|
|
"-Wno-gcc-compat",
|
|
|
|
"-Wall",
|
|
|
|
"-Werror",
|
|
|
|
"-fno-addrsig",
|
2021-04-13 09:14:55 +02:00
|
|
|
"-Iinclude",
|
2021-05-13 21:13:04 +02:00
|
|
|
"-I$(BINDIR)/include",
|
2021-04-13 09:14:55 +02:00
|
|
|
"-I.",
|
2021-05-13 21:13:04 +02:00
|
|
|
"-I$(BINDIR)/.",
|
2021-02-18 09:21:34 +01:00
|
|
|
],
|
2021-04-08 16:40:57 +02:00
|
|
|
srcs = ["a/b/c.c"],
|
2021-02-23 06:46:47 +01:00
|
|
|
)`,
|
2021-05-21 09:37:00 +02:00
|
|
|
}})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCcObjectCcObjetDepsInObjs(t *testing.T) {
|
|
|
|
runCcObjectTestCase(t, bp2buildTestCase{
|
|
|
|
description: "cc_object with cc_object deps in objs props",
|
|
|
|
moduleTypeUnderTest: "cc_object",
|
|
|
|
moduleTypeUnderTestFactory: cc.ObjectFactory,
|
|
|
|
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
|
|
|
|
filesystem: map[string]string{
|
|
|
|
"a/b/c.c": "",
|
|
|
|
"x/y/z.c": "",
|
2021-02-23 06:46:47 +01:00
|
|
|
},
|
2021-05-21 09:37:00 +02:00
|
|
|
blueprint: `cc_object {
|
2021-02-23 06:46:47 +01:00
|
|
|
name: "foo",
|
|
|
|
srcs: ["a/b/c.c"],
|
|
|
|
objs: ["bar"],
|
|
|
|
}
|
|
|
|
|
|
|
|
cc_object {
|
|
|
|
name: "bar",
|
|
|
|
srcs: ["x/y/z.c"],
|
|
|
|
}
|
|
|
|
`,
|
2021-05-21 09:37:00 +02:00
|
|
|
expectedBazelTargets: []string{`cc_object(
|
2021-02-23 06:46:47 +01:00
|
|
|
name = "bar",
|
2021-04-13 09:14:55 +02:00
|
|
|
copts = [
|
|
|
|
"-fno-addrsig",
|
|
|
|
"-I.",
|
2021-05-13 21:13:04 +02:00
|
|
|
"-I$(BINDIR)/.",
|
2021-04-13 09:14:55 +02:00
|
|
|
],
|
2021-04-08 16:40:57 +02:00
|
|
|
srcs = ["x/y/z.c"],
|
2021-02-23 06:46:47 +01:00
|
|
|
)`, `cc_object(
|
|
|
|
name = "foo",
|
2021-04-13 09:14:55 +02:00
|
|
|
copts = [
|
|
|
|
"-fno-addrsig",
|
|
|
|
"-I.",
|
2021-05-13 21:13:04 +02:00
|
|
|
"-I$(BINDIR)/.",
|
2021-04-13 09:14:55 +02:00
|
|
|
],
|
2021-04-08 16:40:57 +02:00
|
|
|
deps = [":bar"],
|
|
|
|
srcs = ["a/b/c.c"],
|
2021-03-18 21:56:36 +01:00
|
|
|
)`,
|
|
|
|
},
|
2021-05-21 09:37:00 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCcObjectIncludeBuildDirFalse(t *testing.T) {
|
|
|
|
runCcObjectTestCase(t, bp2buildTestCase{
|
|
|
|
description: "cc_object with include_build_dir: false",
|
|
|
|
moduleTypeUnderTest: "cc_object",
|
|
|
|
moduleTypeUnderTestFactory: cc.ObjectFactory,
|
|
|
|
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
|
|
|
|
filesystem: map[string]string{
|
|
|
|
"a/b/c.c": "",
|
|
|
|
"x/y/z.c": "",
|
|
|
|
},
|
|
|
|
blueprint: `cc_object {
|
2021-03-18 21:56:36 +01:00
|
|
|
name: "foo",
|
|
|
|
srcs: ["a/b/c.c"],
|
|
|
|
include_build_directory: false,
|
|
|
|
}
|
|
|
|
`,
|
2021-05-21 09:37:00 +02:00
|
|
|
expectedBazelTargets: []string{`cc_object(
|
2021-03-18 21:56:36 +01:00
|
|
|
name = "foo",
|
2021-04-08 16:40:57 +02:00
|
|
|
copts = ["-fno-addrsig"],
|
|
|
|
srcs = ["a/b/c.c"],
|
2021-03-24 15:14:47 +01:00
|
|
|
)`,
|
|
|
|
},
|
2021-05-21 09:37:00 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCcObjectProductVariable(t *testing.T) {
|
|
|
|
runCcObjectTestCase(t, bp2buildTestCase{
|
|
|
|
description: "cc_object with product variable",
|
|
|
|
moduleTypeUnderTest: "cc_object",
|
|
|
|
moduleTypeUnderTestFactory: cc.ObjectFactory,
|
|
|
|
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
|
|
|
|
blueprint: `cc_object {
|
2021-03-24 15:14:47 +01:00
|
|
|
name: "foo",
|
|
|
|
include_build_directory: false,
|
|
|
|
product_variables: {
|
|
|
|
platform_sdk_version: {
|
|
|
|
asflags: ["-DPLATFORM_SDK_VERSION=%d"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
`,
|
2021-05-21 09:37:00 +02:00
|
|
|
expectedBazelTargets: []string{`cc_object(
|
2021-03-24 15:14:47 +01:00
|
|
|
name = "foo",
|
2021-05-06 19:54:29 +02:00
|
|
|
asflags = select({
|
|
|
|
"//build/bazel/product_variables:platform_sdk_version": ["-DPLATFORM_SDK_VERSION={Platform_sdk_version}"],
|
|
|
|
"//conditions:default": [],
|
|
|
|
}),
|
2021-04-08 16:40:57 +02:00
|
|
|
copts = ["-fno-addrsig"],
|
2021-02-18 09:21:34 +01:00
|
|
|
)`,
|
|
|
|
},
|
2021-05-21 09:37:00 +02:00
|
|
|
})
|
2021-02-24 13:20:12 +01:00
|
|
|
}
|
|
|
|
|
2021-05-21 09:37:00 +02:00
|
|
|
func TestCcObjectCflagsOneArch(t *testing.T) {
|
|
|
|
runCcObjectTestCase(t, bp2buildTestCase{
|
|
|
|
description: "cc_object setting cflags for one arch",
|
|
|
|
moduleTypeUnderTest: "cc_object",
|
|
|
|
moduleTypeUnderTestFactory: cc.ObjectFactory,
|
|
|
|
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
|
|
|
|
blueprint: `cc_object {
|
2021-02-24 13:20:12 +01:00
|
|
|
name: "foo",
|
2021-03-15 11:02:43 +01:00
|
|
|
srcs: ["a.cpp"],
|
2021-02-24 13:20:12 +01:00
|
|
|
arch: {
|
|
|
|
x86: {
|
2021-03-15 11:02:43 +01:00
|
|
|
cflags: ["-fPIC"], // string list
|
|
|
|
},
|
|
|
|
arm: {
|
|
|
|
srcs: ["arch/arm/file.S"], // label list
|
2021-02-24 13:20:12 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
`,
|
2021-05-21 09:37:00 +02:00
|
|
|
expectedBazelTargets: []string{
|
|
|
|
`cc_object(
|
2021-02-24 13:20:12 +01:00
|
|
|
name = "foo",
|
2021-04-13 09:14:55 +02:00
|
|
|
copts = [
|
|
|
|
"-fno-addrsig",
|
|
|
|
"-I.",
|
2021-05-13 21:13:04 +02:00
|
|
|
"-I$(BINDIR)/.",
|
2021-04-13 09:14:55 +02:00
|
|
|
] + select({
|
2021-04-08 16:40:57 +02:00
|
|
|
"//build/bazel/platforms/arch:x86": ["-fPIC"],
|
2021-03-15 11:02:43 +01:00
|
|
|
"//conditions:default": [],
|
2021-02-24 13:20:12 +01:00
|
|
|
}),
|
2021-04-08 16:40:57 +02:00
|
|
|
srcs = ["a.cpp"] + select({
|
|
|
|
"//build/bazel/platforms/arch:arm": ["arch/arm/file.S"],
|
2021-03-15 11:02:43 +01:00
|
|
|
"//conditions:default": [],
|
|
|
|
}),
|
2021-02-24 13:20:12 +01:00
|
|
|
)`,
|
|
|
|
},
|
2021-05-21 09:37:00 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCcObjectCflagsFourArch(t *testing.T) {
|
|
|
|
runCcObjectTestCase(t, bp2buildTestCase{
|
|
|
|
description: "cc_object setting cflags for 4 architectures",
|
|
|
|
moduleTypeUnderTest: "cc_object",
|
|
|
|
moduleTypeUnderTestFactory: cc.ObjectFactory,
|
|
|
|
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
|
|
|
|
blueprint: `cc_object {
|
2021-02-24 13:20:12 +01:00
|
|
|
name: "foo",
|
2021-03-15 11:02:43 +01:00
|
|
|
srcs: ["base.cpp"],
|
2021-02-24 13:20:12 +01:00
|
|
|
arch: {
|
|
|
|
x86: {
|
2021-03-15 11:02:43 +01:00
|
|
|
srcs: ["x86.cpp"],
|
2021-02-24 13:20:12 +01:00
|
|
|
cflags: ["-fPIC"],
|
|
|
|
},
|
|
|
|
x86_64: {
|
2021-03-15 11:02:43 +01:00
|
|
|
srcs: ["x86_64.cpp"],
|
2021-02-24 13:20:12 +01:00
|
|
|
cflags: ["-fPIC"],
|
|
|
|
},
|
|
|
|
arm: {
|
2021-03-15 11:02:43 +01:00
|
|
|
srcs: ["arm.cpp"],
|
2021-02-24 13:20:12 +01:00
|
|
|
cflags: ["-Wall"],
|
|
|
|
},
|
|
|
|
arm64: {
|
2021-03-15 11:02:43 +01:00
|
|
|
srcs: ["arm64.cpp"],
|
2021-02-24 13:20:12 +01:00
|
|
|
cflags: ["-Wall"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
`,
|
2021-05-21 09:37:00 +02:00
|
|
|
expectedBazelTargets: []string{
|
|
|
|
`cc_object(
|
2021-02-24 13:20:12 +01:00
|
|
|
name = "foo",
|
2021-04-13 09:14:55 +02:00
|
|
|
copts = [
|
|
|
|
"-fno-addrsig",
|
|
|
|
"-I.",
|
2021-05-13 21:13:04 +02:00
|
|
|
"-I$(BINDIR)/.",
|
2021-04-13 09:14:55 +02:00
|
|
|
] + select({
|
2021-04-08 16:40:57 +02:00
|
|
|
"//build/bazel/platforms/arch:arm": ["-Wall"],
|
|
|
|
"//build/bazel/platforms/arch:arm64": ["-Wall"],
|
|
|
|
"//build/bazel/platforms/arch:x86": ["-fPIC"],
|
|
|
|
"//build/bazel/platforms/arch:x86_64": ["-fPIC"],
|
2021-03-15 11:02:43 +01:00
|
|
|
"//conditions:default": [],
|
2021-02-24 13:20:12 +01:00
|
|
|
}),
|
2021-04-08 16:40:57 +02:00
|
|
|
srcs = ["base.cpp"] + select({
|
|
|
|
"//build/bazel/platforms/arch:arm": ["arm.cpp"],
|
|
|
|
"//build/bazel/platforms/arch:arm64": ["arm64.cpp"],
|
|
|
|
"//build/bazel/platforms/arch:x86": ["x86.cpp"],
|
|
|
|
"//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
|
2021-03-15 11:02:43 +01:00
|
|
|
"//conditions:default": [],
|
|
|
|
}),
|
2021-04-05 12:35:13 +02:00
|
|
|
)`,
|
|
|
|
},
|
2021-05-21 09:37:00 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCcObjectCflagsMultiOs(t *testing.T) {
|
|
|
|
runCcObjectTestCase(t, bp2buildTestCase{
|
|
|
|
description: "cc_object setting cflags for multiple OSes",
|
|
|
|
moduleTypeUnderTest: "cc_object",
|
|
|
|
moduleTypeUnderTestFactory: cc.ObjectFactory,
|
|
|
|
moduleTypeUnderTestBp2BuildMutator: cc.ObjectBp2Build,
|
|
|
|
blueprint: `cc_object {
|
2021-04-05 12:35:13 +02:00
|
|
|
name: "foo",
|
|
|
|
srcs: ["base.cpp"],
|
|
|
|
target: {
|
|
|
|
android: {
|
|
|
|
cflags: ["-fPIC"],
|
|
|
|
},
|
|
|
|
windows: {
|
|
|
|
cflags: ["-fPIC"],
|
|
|
|
},
|
|
|
|
darwin: {
|
|
|
|
cflags: ["-Wall"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
`,
|
2021-05-21 09:37:00 +02:00
|
|
|
expectedBazelTargets: []string{
|
|
|
|
`cc_object(
|
2021-04-05 12:35:13 +02:00
|
|
|
name = "foo",
|
2021-04-13 09:14:55 +02:00
|
|
|
copts = [
|
|
|
|
"-fno-addrsig",
|
|
|
|
"-I.",
|
2021-05-13 21:13:04 +02:00
|
|
|
"-I$(BINDIR)/.",
|
2021-04-13 09:14:55 +02:00
|
|
|
] + select({
|
2021-04-08 16:40:57 +02:00
|
|
|
"//build/bazel/platforms/os:android": ["-fPIC"],
|
|
|
|
"//build/bazel/platforms/os:darwin": ["-Wall"],
|
|
|
|
"//build/bazel/platforms/os:windows": ["-fPIC"],
|
2021-04-05 12:35:13 +02:00
|
|
|
"//conditions:default": [],
|
|
|
|
}),
|
2021-04-08 16:40:57 +02:00
|
|
|
srcs = ["base.cpp"],
|
2021-02-24 13:20:12 +01:00
|
|
|
)`,
|
|
|
|
},
|
2021-05-21 09:37:00 +02:00
|
|
|
})
|
2021-02-18 09:21:34 +01:00
|
|
|
}
|