bp2build: refactor/standardize cc_* bp2build converters am: ed9c17d033

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/1674326

Change-Id: I1cc8f6c9681346d3d7dff316d2b37e5bdaccf30d
This commit is contained in:
Jingwen Chen 2021-04-22 10:36:37 +00:00 committed by Automerger Merge Worker
commit 0a52f7276e
11 changed files with 579 additions and 224 deletions

View File

@ -125,7 +125,9 @@ func (ll *LabelList) Append(other LabelList) {
} }
} }
func UniqueBazelLabels(originalLabels []Label) []Label { // UniqueSortedBazelLabels takes a []Label and deduplicates the labels, and returns
// the slice in a sorted order.
func UniqueSortedBazelLabels(originalLabels []Label) []Label {
uniqueLabelsSet := make(map[Label]bool) uniqueLabelsSet := make(map[Label]bool)
for _, l := range originalLabels { for _, l := range originalLabels {
uniqueLabelsSet[l] = true uniqueLabelsSet[l] = true
@ -142,8 +144,8 @@ func UniqueBazelLabels(originalLabels []Label) []Label {
func UniqueBazelLabelList(originalLabelList LabelList) LabelList { func UniqueBazelLabelList(originalLabelList LabelList) LabelList {
var uniqueLabelList LabelList var uniqueLabelList LabelList
uniqueLabelList.Includes = UniqueBazelLabels(originalLabelList.Includes) uniqueLabelList.Includes = UniqueSortedBazelLabels(originalLabelList.Includes)
uniqueLabelList.Excludes = UniqueBazelLabels(originalLabelList.Excludes) uniqueLabelList.Excludes = UniqueSortedBazelLabels(originalLabelList.Excludes)
return uniqueLabelList return uniqueLabelList
} }
@ -292,7 +294,7 @@ func MakeLabelListAttribute(value LabelList) LabelListAttribute {
return LabelListAttribute{Value: UniqueBazelLabelList(value)} return LabelListAttribute{Value: UniqueBazelLabelList(value)}
} }
// Append appends all values, including os and arch specific ones, from another // Append all values, including os and arch specific ones, from another
// LabelListAttribute to this LabelListAttribute. // LabelListAttribute to this LabelListAttribute.
func (attrs *LabelListAttribute) Append(other LabelListAttribute) { func (attrs *LabelListAttribute) Append(other LabelListAttribute) {
for arch := range PlatformArchMap { for arch := range PlatformArchMap {
@ -500,6 +502,26 @@ func (attrs *StringListAttribute) SetValueForOS(os string, value []string) {
*v = value *v = value
} }
// Append appends all values, including os and arch specific ones, from another
// StringListAttribute to this StringListAttribute
func (attrs *StringListAttribute) Append(other StringListAttribute) {
for arch := range PlatformArchMap {
this := attrs.GetValueForArch(arch)
that := other.GetValueForArch(arch)
this = append(this, that...)
attrs.SetValueForArch(arch, this)
}
for os := range PlatformOsMap {
this := attrs.GetValueForOS(os)
that := other.GetValueForOS(os)
this = append(this, that...)
attrs.SetValueForOS(os, this)
}
attrs.Value = append(attrs.Value, other.Value...)
}
// TryVariableSubstitution, replace string substitution formatting within each string in slice with // TryVariableSubstitution, replace string substitution formatting within each string in slice with
// Starlark string.format compatible tag for productVariable. // Starlark string.format compatible tag for productVariable.
func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) { func TryVariableSubstitutions(slice []string, productVariable string) ([]string, bool) {

View File

@ -39,7 +39,7 @@ func TestUniqueBazelLabels(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
actualUniqueLabels := UniqueBazelLabels(tc.originalLabels) actualUniqueLabels := UniqueSortedBazelLabels(tc.originalLabels)
if !reflect.DeepEqual(tc.expectedUniqueLabels, actualUniqueLabels) { if !reflect.DeepEqual(tc.expectedUniqueLabels, actualUniqueLabels) {
t.Fatalf("Expected %v, got %v", tc.expectedUniqueLabels, actualUniqueLabels) t.Fatalf("Expected %v, got %v", tc.expectedUniqueLabels, actualUniqueLabels)
} }

View File

@ -109,10 +109,22 @@ cc_library {
`, `,
expectedBazelTargets: []string{`cc_library( expectedBazelTargets: []string{`cc_library(
name = "foo-lib", name = "foo-lib",
copts = ["-Wall"], copts = [
"-Wall",
"-I.",
],
deps = [":some-headers"], deps = [":some-headers"],
hdrs = [ hdrs = ["foo-dir/a.h"],
includes = ["foo-dir"],
linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
"//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
"//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
"//conditions:default": [],
}),
srcs = [
"impl.cpp",
"header.h", "header.h",
"foo-dir/a.h",
"header.hh", "header.hh",
"header.hpp", "header.hpp",
"header.hxx", "header.hxx",
@ -121,15 +133,7 @@ cc_library {
"header.inc", "header.inc",
"header.ipp", "header.ipp",
"header.h.generic", "header.h.generic",
"foo-dir/a.h", ] + select({
],
includes = ["foo-dir"],
linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
"//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
"//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
"//conditions:default": [],
}),
srcs = ["impl.cpp"] + select({
"//build/bazel/platforms/arch:x86": ["x86.cpp"], "//build/bazel/platforms/arch:x86": ["x86.cpp"],
"//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"], "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
"//conditions:default": [], "//conditions:default": [],
@ -190,14 +194,9 @@ cc_library {
"-Wextra", "-Wextra",
"-Wunused", "-Wunused",
"-Werror", "-Werror",
"-I.",
], ],
deps = [":libc_headers"], deps = [":libc_headers"],
hdrs = [
"linked_list.h",
"linker.h",
"linker_block_allocator.h",
"linker_cfi.h",
],
linkopts = [ linkopts = [
"-Wl,--exclude-libs=libgcc.a", "-Wl,--exclude-libs=libgcc.a",
"-Wl,--exclude-libs=libgcc_stripped.a", "-Wl,--exclude-libs=libgcc_stripped.a",
@ -210,7 +209,13 @@ cc_library {
"//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"], "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
"//conditions:default": [], "//conditions:default": [],
}), }),
srcs = ["ld_android.cpp"], srcs = [
"ld_android.cpp",
"linked_list.h",
"linker.h",
"linker_block_allocator.h",
"linker_cfi.h",
],
)`}, )`},
}, },
} }

View File

@ -131,6 +131,7 @@ cc_library_headers {
}`, }`,
expectedBazelTargets: []string{`cc_library_headers( expectedBazelTargets: []string{`cc_library_headers(
name = "foo_headers", name = "foo_headers",
copts = ["-I."],
deps = [ deps = [
":lib-1", ":lib-1",
":lib-2", ":lib-2",
@ -157,6 +158,7 @@ cc_library_headers {
}), }),
)`, `cc_library_headers( )`, `cc_library_headers(
name = "lib-1", name = "lib-1",
copts = ["-I."],
hdrs = [ hdrs = [
"lib-1/lib1a.h", "lib-1/lib1a.h",
"lib-1/lib1b.h", "lib-1/lib1b.h",
@ -164,6 +166,7 @@ cc_library_headers {
includes = ["lib-1"], includes = ["lib-1"],
)`, `cc_library_headers( )`, `cc_library_headers(
name = "lib-2", name = "lib-2",
copts = ["-I."],
hdrs = [ hdrs = [
"lib-2/lib2a.h", "lib-2/lib2a.h",
"lib-2/lib2b.h", "lib-2/lib2b.h",
@ -201,12 +204,16 @@ cc_library_headers {
}`, }`,
expectedBazelTargets: []string{`cc_library_headers( expectedBazelTargets: []string{`cc_library_headers(
name = "android-lib", name = "android-lib",
copts = ["-I."],
)`, `cc_library_headers( )`, `cc_library_headers(
name = "base-lib", name = "base-lib",
copts = ["-I."],
)`, `cc_library_headers( )`, `cc_library_headers(
name = "darwin-lib", name = "darwin-lib",
copts = ["-I."],
)`, `cc_library_headers( )`, `cc_library_headers(
name = "foo_headers", name = "foo_headers",
copts = ["-I."],
deps = [":base-lib"] + select({ deps = [":base-lib"] + select({
"//build/bazel/platforms/os:android": [":android-lib"], "//build/bazel/platforms/os:android": [":android-lib"],
"//build/bazel/platforms/os:darwin": [":darwin-lib"], "//build/bazel/platforms/os:darwin": [":darwin-lib"],
@ -218,12 +225,16 @@ cc_library_headers {
}), }),
)`, `cc_library_headers( )`, `cc_library_headers(
name = "fuchsia-lib", name = "fuchsia-lib",
copts = ["-I."],
)`, `cc_library_headers( )`, `cc_library_headers(
name = "linux-lib", name = "linux-lib",
copts = ["-I."],
)`, `cc_library_headers( )`, `cc_library_headers(
name = "linux_bionic-lib", name = "linux_bionic-lib",
copts = ["-I."],
)`, `cc_library_headers( )`, `cc_library_headers(
name = "windows-lib", name = "windows-lib",
copts = ["-I."],
)`}, )`},
}, },
{ {
@ -244,10 +255,13 @@ cc_library_headers {
}`, }`,
expectedBazelTargets: []string{`cc_library_headers( expectedBazelTargets: []string{`cc_library_headers(
name = "android-lib", name = "android-lib",
copts = ["-I."],
)`, `cc_library_headers( )`, `cc_library_headers(
name = "exported-lib", name = "exported-lib",
copts = ["-I."],
)`, `cc_library_headers( )`, `cc_library_headers(
name = "foo_headers", name = "foo_headers",
copts = ["-I."],
deps = select({ deps = select({
"//build/bazel/platforms/os:android": [ "//build/bazel/platforms/os:android": [
":android-lib", ":android-lib",

View File

@ -119,71 +119,70 @@ cc_library_headers {
cc_library_static { cc_library_static {
name: "static_lib_1", name: "static_lib_1",
srcs: ["static_lib_1.cc"], srcs: ["static_lib_1.cc"],
bazel_module: { bp2build_available: true },
} }
cc_library_static { cc_library_static {
name: "static_lib_2", name: "static_lib_2",
srcs: ["static_lib_2.cc"], srcs: ["static_lib_2.cc"],
bazel_module: { bp2build_available: true },
} }
cc_library_static { cc_library_static {
name: "whole_static_lib_1", name: "whole_static_lib_1",
srcs: ["whole_static_lib_1.cc"], srcs: ["whole_static_lib_1.cc"],
bazel_module: { bp2build_available: true },
} }
cc_library_static { cc_library_static {
name: "whole_static_lib_2", name: "whole_static_lib_2",
srcs: ["whole_static_lib_2.cc"], srcs: ["whole_static_lib_2.cc"],
bazel_module: { bp2build_available: true },
} }
cc_library_static { cc_library_static {
name: "foo_static", name: "foo_static",
srcs: [ srcs: [
"foo_static1.cc", "foo_static1.cc",
"foo_static2.cc", "foo_static2.cc",
], ],
cflags: [ cflags: [
"-Dflag1", "-Dflag1",
"-Dflag2" "-Dflag2"
], ],
static_libs: [ static_libs: [
"static_lib_1", "static_lib_1",
"static_lib_2" "static_lib_2"
], ],
whole_static_libs: [ whole_static_libs: [
"whole_static_lib_1", "whole_static_lib_1",
"whole_static_lib_2" "whole_static_lib_2"
], ],
include_dirs: [ include_dirs: [
"include_dir_1", "include_dir_1",
"include_dir_2", "include_dir_2",
], ],
local_include_dirs: [ local_include_dirs: [
"local_include_dir_1", "local_include_dir_1",
"local_include_dir_2", "local_include_dir_2",
], ],
export_include_dirs: [ export_include_dirs: [
"export_include_dir_1", "export_include_dir_1",
"export_include_dir_2" "export_include_dir_2"
], ],
header_libs: [ header_libs: [
"header_lib_1", "header_lib_1",
"header_lib_2" "header_lib_2"
], ],
// TODO: Also support export_header_lib_headers // TODO: Also support export_header_lib_headers
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`cc_library_static( expectedBazelTargets: []string{`cc_library_static(
name = "foo_static", name = "foo_static",
copts = [ copts = [
"-Dflag1", "-Dflag1",
"-Dflag2", "-Dflag2",
"-Iinclude_dir_1",
"-Iinclude_dir_2",
"-Ilocal_include_dir_1",
"-Ilocal_include_dir_2",
"-I.",
], ],
deps = [ deps = [
":header_lib_1", ":header_lib_1",
@ -194,8 +193,6 @@ cc_library_static {
":whole_static_lib_2", ":whole_static_lib_2",
], ],
hdrs = [ hdrs = [
"implicit_include_1.h",
"implicit_include_2.h",
"export_include_dir_1/export_include_dir_1_a.h", "export_include_dir_1/export_include_dir_1_a.h",
"export_include_dir_1/export_include_dir_1_b.h", "export_include_dir_1/export_include_dir_1_b.h",
"export_include_dir_2/export_include_dir_2_a.h", "export_include_dir_2/export_include_dir_2_a.h",
@ -204,16 +201,17 @@ cc_library_static {
includes = [ includes = [
"export_include_dir_1", "export_include_dir_1",
"export_include_dir_2", "export_include_dir_2",
"include_dir_1",
"include_dir_2",
"local_include_dir_1",
"local_include_dir_2",
".",
], ],
linkstatic = True, linkstatic = True,
srcs = [ srcs = [
"foo_static1.cc", "foo_static1.cc",
"foo_static2.cc", "foo_static2.cc",
"implicit_include_1.h",
"implicit_include_2.h",
"export_include_dir_1/export_include_dir_1_a.h",
"export_include_dir_1/export_include_dir_1_b.h",
"export_include_dir_2/export_include_dir_2_a.h",
"export_include_dir_2/export_include_dir_2_b.h",
"include_dir_1/include_dir_1_a.h", "include_dir_1/include_dir_1_a.h",
"include_dir_1/include_dir_1_b.h", "include_dir_1/include_dir_1_b.h",
"include_dir_2/include_dir_2_a.h", "include_dir_2/include_dir_2_a.h",
@ -222,60 +220,90 @@ cc_library_static {
"local_include_dir_1/local_include_dir_1_b.h", "local_include_dir_1/local_include_dir_1_b.h",
"local_include_dir_2/local_include_dir_2_a.h", "local_include_dir_2/local_include_dir_2_a.h",
"local_include_dir_2/local_include_dir_2_b.h", "local_include_dir_2/local_include_dir_2_b.h",
"implicit_include_1.h",
"implicit_include_2.h",
], ],
)`, `cc_library_static( )`, `cc_library_static(
name = "static_lib_1", name = "static_lib_1",
hdrs = [ copts = ["-I."],
"implicit_include_1.h",
"implicit_include_2.h",
],
includes = ["."],
linkstatic = True, linkstatic = True,
srcs = [ srcs = [
"static_lib_1.cc", "static_lib_1.cc",
"implicit_include_1.h", "implicit_include_1.h",
"implicit_include_2.h", "implicit_include_2.h",
"export_include_dir_1/export_include_dir_1_a.h",
"export_include_dir_1/export_include_dir_1_b.h",
"export_include_dir_2/export_include_dir_2_a.h",
"export_include_dir_2/export_include_dir_2_b.h",
"include_dir_1/include_dir_1_a.h",
"include_dir_1/include_dir_1_b.h",
"include_dir_2/include_dir_2_a.h",
"include_dir_2/include_dir_2_b.h",
"local_include_dir_1/local_include_dir_1_a.h",
"local_include_dir_1/local_include_dir_1_b.h",
"local_include_dir_2/local_include_dir_2_a.h",
"local_include_dir_2/local_include_dir_2_b.h",
], ],
)`, `cc_library_static( )`, `cc_library_static(
name = "static_lib_2", name = "static_lib_2",
hdrs = [ copts = ["-I."],
"implicit_include_1.h",
"implicit_include_2.h",
],
includes = ["."],
linkstatic = True, linkstatic = True,
srcs = [ srcs = [
"static_lib_2.cc", "static_lib_2.cc",
"implicit_include_1.h", "implicit_include_1.h",
"implicit_include_2.h", "implicit_include_2.h",
"export_include_dir_1/export_include_dir_1_a.h",
"export_include_dir_1/export_include_dir_1_b.h",
"export_include_dir_2/export_include_dir_2_a.h",
"export_include_dir_2/export_include_dir_2_b.h",
"include_dir_1/include_dir_1_a.h",
"include_dir_1/include_dir_1_b.h",
"include_dir_2/include_dir_2_a.h",
"include_dir_2/include_dir_2_b.h",
"local_include_dir_1/local_include_dir_1_a.h",
"local_include_dir_1/local_include_dir_1_b.h",
"local_include_dir_2/local_include_dir_2_a.h",
"local_include_dir_2/local_include_dir_2_b.h",
], ],
)`, `cc_library_static( )`, `cc_library_static(
name = "whole_static_lib_1", name = "whole_static_lib_1",
hdrs = [ copts = ["-I."],
"implicit_include_1.h",
"implicit_include_2.h",
],
includes = ["."],
linkstatic = True, linkstatic = True,
srcs = [ srcs = [
"whole_static_lib_1.cc", "whole_static_lib_1.cc",
"implicit_include_1.h", "implicit_include_1.h",
"implicit_include_2.h", "implicit_include_2.h",
"export_include_dir_1/export_include_dir_1_a.h",
"export_include_dir_1/export_include_dir_1_b.h",
"export_include_dir_2/export_include_dir_2_a.h",
"export_include_dir_2/export_include_dir_2_b.h",
"include_dir_1/include_dir_1_a.h",
"include_dir_1/include_dir_1_b.h",
"include_dir_2/include_dir_2_a.h",
"include_dir_2/include_dir_2_b.h",
"local_include_dir_1/local_include_dir_1_a.h",
"local_include_dir_1/local_include_dir_1_b.h",
"local_include_dir_2/local_include_dir_2_a.h",
"local_include_dir_2/local_include_dir_2_b.h",
], ],
)`, `cc_library_static( )`, `cc_library_static(
name = "whole_static_lib_2", name = "whole_static_lib_2",
hdrs = [ copts = ["-I."],
"implicit_include_1.h",
"implicit_include_2.h",
],
includes = ["."],
linkstatic = True, linkstatic = True,
srcs = [ srcs = [
"whole_static_lib_2.cc", "whole_static_lib_2.cc",
"implicit_include_1.h", "implicit_include_1.h",
"implicit_include_2.h", "implicit_include_2.h",
"export_include_dir_1/export_include_dir_1_a.h",
"export_include_dir_1/export_include_dir_1_b.h",
"export_include_dir_2/export_include_dir_2_a.h",
"export_include_dir_2/export_include_dir_2_b.h",
"include_dir_1/include_dir_1_a.h",
"include_dir_1/include_dir_1_b.h",
"include_dir_2/include_dir_2_a.h",
"include_dir_2/include_dir_2_b.h",
"local_include_dir_1/local_include_dir_1_a.h",
"local_include_dir_1/local_include_dir_1_b.h",
"local_include_dir_2/local_include_dir_2_a.h",
"local_include_dir_2/local_include_dir_2_b.h",
], ],
)`}, )`},
}, },
@ -306,14 +334,12 @@ cc_library_static {
include_dirs: [ include_dirs: [
"subpackage", "subpackage",
], ],
bazel_module: { bp2build_available: true },
}`, }`,
expectedBazelTargets: []string{`cc_library_static( expectedBazelTargets: []string{`cc_library_static(
name = "foo_static", name = "foo_static",
includes = [ copts = [
"subpackage", "-Isubpackage",
".", "-I.",
], ],
linkstatic = True, linkstatic = True,
srcs = [ srcs = [
@ -324,6 +350,299 @@ cc_library_static {
"//subpackage/subsubpackage/subsubsubpackage:subsubsubpackage_header.h", "//subpackage/subsubpackage/subsubsubpackage:subsubsubpackage_header.h",
"//subpackage/subsubpackage/subsubsubpackage:subdirectory/subdirectory_header.h", "//subpackage/subsubpackage/subsubsubpackage:subdirectory/subdirectory_header.h",
], ],
)`},
},
{
description: "cc_library_static export include dir",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
filesystem: map[string]string{
// subpackage with subdirectory
"subpackage/Android.bp": "",
"subpackage/subpackage_header.h": "",
"subpackage/subdirectory/subdirectory_header.h": "",
},
bp: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
export_include_dirs: ["subpackage"],
}`,
expectedBazelTargets: []string{`cc_library_static(
name = "foo_static",
copts = ["-I."],
hdrs = [
"//subpackage:subdirectory/subdirectory_header.h",
"//subpackage:subpackage_header.h",
],
includes = ["subpackage"],
linkstatic = True,
srcs = [
"//subpackage:subpackage_header.h",
"//subpackage:subdirectory/subdirectory_header.h",
],
)`},
},
{
description: "cc_library_static export system include dir",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
filesystem: map[string]string{
// subpackage with subdirectory
"subpackage/Android.bp": "",
"subpackage/subpackage_header.h": "",
"subpackage/subdirectory/subdirectory_header.h": "",
},
bp: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
export_system_include_dirs: ["subpackage"],
}`,
expectedBazelTargets: []string{`cc_library_static(
name = "foo_static",
copts = ["-I."],
hdrs = [
"//subpackage:subdirectory/subdirectory_header.h",
"//subpackage:subpackage_header.h",
],
includes = ["subpackage"],
linkstatic = True,
srcs = [
"//subpackage:subpackage_header.h",
"//subpackage:subdirectory/subdirectory_header.h",
],
)`},
},
{
description: "cc_library_static include_dirs, local_include_dirs, export_include_dirs (b/183742505)",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
dir: "subpackage",
filesystem: map[string]string{
// subpackage with subdirectory
"subpackage/Android.bp": `
cc_library_static {
name: "foo_static",
// include_dirs are workspace/root relative
include_dirs: [
"subpackage/subsubpackage",
"subpackage2",
"subpackage3/subsubpackage"
],
local_include_dirs: ["subsubpackage2"], // module dir relative
export_include_dirs: ["./exported_subsubpackage"], // module dir relative
include_build_directory: true,
bazel_module: { bp2build_available: true },
}`,
"subpackage/subsubpackage/header.h": "",
"subpackage/subsubpackage2/header.h": "",
"subpackage/exported_subsubpackage/header.h": "",
"subpackage2/header.h": "",
"subpackage3/subsubpackage/header.h": "",
},
bp: soongCcLibraryStaticPreamble,
expectedBazelTargets: []string{`cc_library_static(
name = "foo_static",
copts = [
"-Isubpackage/subsubpackage",
"-Isubpackage2",
"-Isubpackage3/subsubpackage",
"-Isubpackage/subsubpackage2",
"-Isubpackage",
],
hdrs = ["exported_subsubpackage/header.h"],
includes = ["./exported_subsubpackage"],
linkstatic = True,
srcs = [
"exported_subsubpackage/header.h",
"subsubpackage/header.h",
"subsubpackage2/header.h",
],
)`},
},
{
description: "cc_library_static include_build_directory disabled",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
filesystem: map[string]string{
// subpackage with subdirectory
"subpackage/Android.bp": "",
"subpackage/subpackage_header.h": "",
"subpackage/subdirectory/subdirectory_header.h": "",
},
bp: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
local_include_dirs: ["subpackage2"],
include_build_directory: false,
}`,
expectedBazelTargets: []string{`cc_library_static(
name = "foo_static",
copts = [
"-Isubpackage",
"-Isubpackage2",
],
linkstatic = True,
)`},
},
{
description: "cc_library_static include_build_directory enabled",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
filesystem: map[string]string{
// subpackage with subdirectory
"subpackage/Android.bp": "",
"subpackage/subpackage_header.h": "",
"subpackage2/Android.bp": "",
"subpackage2/subpackage2_header.h": "",
"subpackage/subdirectory/subdirectory_header.h": "",
},
bp: soongCcLibraryStaticPreamble + `
cc_library_static {
name: "foo_static",
include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
local_include_dirs: ["subpackage2"],
include_build_directory: true,
}`,
expectedBazelTargets: []string{`cc_library_static(
name = "foo_static",
copts = [
"-Isubpackage",
"-Isubpackage2",
"-I.",
],
linkstatic = True,
srcs = [
"//subpackage:subpackage_header.h",
"//subpackage:subdirectory/subdirectory_header.h",
"//subpackage2:subpackage2_header.h",
],
)`},
},
{
description: "cc_library_static arch-specific static_libs",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
bp: soongCcLibraryStaticPreamble + `
cc_library_static { name: "static_dep" }
cc_library_static { name: "static_dep2" }
cc_library_static {
name: "foo_static",
arch: { arm64: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
}`,
expectedBazelTargets: []string{`cc_library_static(
name = "foo_static",
copts = ["-I."],
deps = select({
"//build/bazel/platforms/arch:arm64": [
":static_dep",
":static_dep2",
],
"//conditions:default": [],
}),
linkstatic = True,
)`, `cc_library_static(
name = "static_dep",
copts = ["-I."],
linkstatic = True,
)`, `cc_library_static(
name = "static_dep2",
copts = ["-I."],
linkstatic = True,
)`},
},
{
description: "cc_library_static os-specific static_libs",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
bp: soongCcLibraryStaticPreamble + `
cc_library_static { name: "static_dep" }
cc_library_static { name: "static_dep2" }
cc_library_static {
name: "foo_static",
target: { android: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
}`,
expectedBazelTargets: []string{`cc_library_static(
name = "foo_static",
copts = ["-I."],
deps = select({
"//build/bazel/platforms/os:android": [
":static_dep",
":static_dep2",
],
"//conditions:default": [],
}),
linkstatic = True,
)`, `cc_library_static(
name = "static_dep",
copts = ["-I."],
linkstatic = True,
)`, `cc_library_static(
name = "static_dep2",
copts = ["-I."],
linkstatic = True,
)`},
},
{
description: "cc_library_static base, arch and os-specific static_libs",
moduleTypeUnderTest: "cc_library_static",
moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
filesystem: map[string]string{},
bp: soongCcLibraryStaticPreamble + `
cc_library_static { name: "static_dep" }
cc_library_static { name: "static_dep2" }
cc_library_static { name: "static_dep3" }
cc_library_static { name: "static_dep4" }
cc_library_static {
name: "foo_static",
static_libs: ["static_dep"],
whole_static_libs: ["static_dep2"],
target: { android: { static_libs: ["static_dep3"] } },
arch: { arm64: { static_libs: ["static_dep4"] } },
}`,
expectedBazelTargets: []string{`cc_library_static(
name = "foo_static",
copts = ["-I."],
deps = [
":static_dep",
":static_dep2",
] + select({
"//build/bazel/platforms/arch:arm64": [":static_dep4"],
"//conditions:default": [],
}) + select({
"//build/bazel/platforms/os:android": [":static_dep3"],
"//conditions:default": [],
}),
linkstatic = True,
)`, `cc_library_static(
name = "static_dep",
copts = ["-I."],
linkstatic = True,
)`, `cc_library_static(
name = "static_dep2",
copts = ["-I."],
linkstatic = True,
)`, `cc_library_static(
name = "static_dep3",
copts = ["-I."],
linkstatic = True,
)`, `cc_library_static(
name = "static_dep4",
copts = ["-I."],
linkstatic = True,
)`}, )`},
}, },
} }
@ -352,6 +671,7 @@ cc_library_static {
ctx.DepsBp2BuildMutators(m) ctx.DepsBp2BuildMutators(m)
} }
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterBp2BuildConfig(bp2buildConfig)
ctx.RegisterForBazelConversion() ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, toParse) _, errs := ctx.ParseFileList(dir, toParse)

View File

@ -55,8 +55,6 @@ func TestCcObjectBp2Build(t *testing.T) {
"a/b/*.c" "a/b/*.c"
], ],
exclude_srcs: ["a/b/exclude.c"], exclude_srcs: ["a/b/exclude.c"],
bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{`cc_object(
@ -66,16 +64,14 @@ func TestCcObjectBp2Build(t *testing.T) {
"-Wno-gcc-compat", "-Wno-gcc-compat",
"-Wall", "-Wall",
"-Werror", "-Werror",
"-Iinclude",
"-I.",
], ],
hdrs = [ srcs = [
"a/b/c.c",
"a/b/bar.h", "a/b/bar.h",
"a/b/foo.h", "a/b/foo.h",
], ],
local_include_dirs = [
"include",
".",
],
srcs = ["a/b/c.c"],
)`, )`,
}, },
}, },
@ -93,7 +89,6 @@ func TestCcObjectBp2Build(t *testing.T) {
], ],
defaults: ["foo_defaults"], defaults: ["foo_defaults"],
bazel_module: { bp2build_available: true },
} }
cc_defaults { cc_defaults {
@ -117,10 +112,8 @@ cc_defaults {
"-Wall", "-Wall",
"-Werror", "-Werror",
"-fno-addrsig", "-fno-addrsig",
], "-Iinclude",
local_include_dirs = [ "-I.",
"include",
".",
], ],
srcs = ["a/b/c.c"], srcs = ["a/b/c.c"],
)`, )`,
@ -139,27 +132,27 @@ cc_defaults {
name: "foo", name: "foo",
srcs: ["a/b/c.c"], srcs: ["a/b/c.c"],
objs: ["bar"], objs: ["bar"],
bazel_module: { bp2build_available: true },
} }
cc_object { cc_object {
name: "bar", name: "bar",
srcs: ["x/y/z.c"], srcs: ["x/y/z.c"],
bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{`cc_object(
name = "bar", name = "bar",
copts = ["-fno-addrsig"], copts = [
local_include_dirs = ["."], "-fno-addrsig",
"-I.",
],
srcs = ["x/y/z.c"], srcs = ["x/y/z.c"],
)`, `cc_object( )`, `cc_object(
name = "foo", name = "foo",
copts = ["-fno-addrsig"], copts = [
"-fno-addrsig",
"-I.",
],
deps = [":bar"], deps = [":bar"],
local_include_dirs = ["."],
srcs = ["a/b/c.c"], srcs = ["a/b/c.c"],
)`, )`,
}, },
@ -177,8 +170,6 @@ cc_object {
name: "foo", name: "foo",
srcs: ["a/b/c.c"], srcs: ["a/b/c.c"],
include_build_directory: false, include_build_directory: false,
bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{`cc_object(
@ -201,8 +192,6 @@ cc_object {
asflags: ["-DPLATFORM_SDK_VERSION=%d"], asflags: ["-DPLATFORM_SDK_VERSION=%d"],
}, },
}, },
bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTargets: []string{`cc_object( expectedBazelTargets: []string{`cc_object(
@ -233,6 +222,7 @@ cc_object {
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterBp2BuildConfig(bp2buildConfig)
ctx.RegisterForBazelConversion() ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, toParse) _, errs := ctx.ParseFileList(dir, toParse)
@ -290,17 +280,18 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
srcs: ["arch/arm/file.S"], // label list srcs: ["arch/arm/file.S"], // label list
}, },
}, },
bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`cc_object( `cc_object(
name = "foo", name = "foo",
copts = ["-fno-addrsig"] + select({ copts = [
"-fno-addrsig",
"-I.",
] + select({
"//build/bazel/platforms/arch:x86": ["-fPIC"], "//build/bazel/platforms/arch:x86": ["-fPIC"],
"//conditions:default": [], "//conditions:default": [],
}), }),
local_include_dirs = ["."],
srcs = ["a.cpp"] + select({ srcs = ["a.cpp"] + select({
"//build/bazel/platforms/arch:arm": ["arch/arm/file.S"], "//build/bazel/platforms/arch:arm": ["arch/arm/file.S"],
"//conditions:default": [], "//conditions:default": [],
@ -334,20 +325,21 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
cflags: ["-Wall"], cflags: ["-Wall"],
}, },
}, },
bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`cc_object( `cc_object(
name = "foo", name = "foo",
copts = ["-fno-addrsig"] + select({ copts = [
"-fno-addrsig",
"-I.",
] + select({
"//build/bazel/platforms/arch:arm": ["-Wall"], "//build/bazel/platforms/arch:arm": ["-Wall"],
"//build/bazel/platforms/arch:arm64": ["-Wall"], "//build/bazel/platforms/arch:arm64": ["-Wall"],
"//build/bazel/platforms/arch:x86": ["-fPIC"], "//build/bazel/platforms/arch:x86": ["-fPIC"],
"//build/bazel/platforms/arch:x86_64": ["-fPIC"], "//build/bazel/platforms/arch:x86_64": ["-fPIC"],
"//conditions:default": [], "//conditions:default": [],
}), }),
local_include_dirs = ["."],
srcs = ["base.cpp"] + select({ srcs = ["base.cpp"] + select({
"//build/bazel/platforms/arch:arm": ["arm.cpp"], "//build/bazel/platforms/arch:arm": ["arm.cpp"],
"//build/bazel/platforms/arch:arm64": ["arm64.cpp"], "//build/bazel/platforms/arch:arm64": ["arm64.cpp"],
@ -377,19 +369,20 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
cflags: ["-Wall"], cflags: ["-Wall"],
}, },
}, },
bazel_module: { bp2build_available: true },
} }
`, `,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
`cc_object( `cc_object(
name = "foo", name = "foo",
copts = ["-fno-addrsig"] + select({ copts = [
"-fno-addrsig",
"-I.",
] + select({
"//build/bazel/platforms/os:android": ["-fPIC"], "//build/bazel/platforms/os:android": ["-fPIC"],
"//build/bazel/platforms/os:darwin": ["-Wall"], "//build/bazel/platforms/os:darwin": ["-Wall"],
"//build/bazel/platforms/os:windows": ["-fPIC"], "//build/bazel/platforms/os:windows": ["-fPIC"],
"//conditions:default": [], "//conditions:default": [],
}), }),
local_include_dirs = ["."],
srcs = ["base.cpp"], srcs = ["base.cpp"],
)`, )`,
}, },
@ -409,6 +402,7 @@ func TestCcObjectConfigurableAttributesBp2Build(t *testing.T) {
ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator) ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
ctx.RegisterBp2BuildConfig(bp2buildConfig)
ctx.RegisterForBazelConversion() ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, toParse) _, errs := ctx.ParseFileList(dir, toParse)

View File

@ -110,7 +110,11 @@ func prettyPrintSelectMap(selectMap map[string]reflect.Value, defaultValue strin
if err != nil { if err != nil {
return "", err return "", err
} }
selects += s + ",\n" // s could still be an empty string, e.g. unset slices of structs with
// length of 0.
if s != "" {
selects += s + ",\n"
}
} }
if len(selects) == 0 { if len(selects) == 0 {
@ -137,6 +141,9 @@ func prettyPrintSelectEntry(value reflect.Value, key string, indent int) (string
if err != nil { if err != nil {
return "", err return "", err
} }
if v == "" {
return "", nil
}
s += fmt.Sprintf("\"%s\": %s", key, v) s += fmt.Sprintf("\"%s\": %s", key, v)
return s, nil return s, nil
} }

View File

@ -16,7 +16,7 @@ package cc
import ( import (
"android/soong/android" "android/soong/android"
"android/soong/bazel" "android/soong/bazel"
"strings" "path/filepath"
) )
// bp2build functions and helpers for converting cc_* modules to Bazel. // bp2build functions and helpers for converting cc_* modules to Bazel.
@ -53,6 +53,18 @@ func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) {
if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok { if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
allDeps = append(allDeps, baseLinkerProps.Header_libs...) allDeps = append(allDeps, baseLinkerProps.Header_libs...)
allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...) allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
allDeps = append(allDeps, baseLinkerProps.Static_libs...)
allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
}
}
for _, p := range module.GetArchProperties(&BaseLinkerProperties{}) {
// arch specific linker props
if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
allDeps = append(allDeps, baseLinkerProps.Header_libs...)
allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
allDeps = append(allDeps, baseLinkerProps.Static_libs...)
allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
} }
} }
@ -61,51 +73,80 @@ func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) {
// Convenience struct to hold all attributes parsed from compiler properties. // Convenience struct to hold all attributes parsed from compiler properties.
type compilerAttributes struct { type compilerAttributes struct {
copts bazel.StringListAttribute copts bazel.StringListAttribute
srcs bazel.LabelListAttribute srcs bazel.LabelListAttribute
hdrs bazel.LabelListAttribute includes bazel.StringListAttribute
} }
// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes { func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
var hdrs, srcs bazel.LabelListAttribute var localHdrs, srcs bazel.LabelListAttribute
var copts bazel.StringListAttribute var copts bazel.StringListAttribute
hdrsAndSrcs := func(baseCompilerProps *BaseCompilerProperties) (bazel.LabelList, bazel.LabelList) { // Creates the -I flag for a directory, while making the directory relative
srcsList := android.BazelLabelForModuleSrcExcludes( // to the exec root for Bazel to work.
ctx, baseCompilerProps.Srcs, baseCompilerProps.Exclude_srcs) includeFlag := func(dir string) string {
hdrsList := android.BazelLabelForModuleSrc(ctx, srcsList.LooseHdrsGlobs(headerExts)) // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements.
return hdrsList, srcsList return "-I" + filepath.Join(ctx.ModuleDir(), dir)
}
// Parse the list of srcs, excluding files from exclude_srcs.
parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
return android.BazelLabelForModuleSrcExcludes(ctx, baseCompilerProps.Srcs, baseCompilerProps.Exclude_srcs)
}
// Parse the list of module-relative include directories (-I).
parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
// include_dirs are root-relative, not module-relative.
includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
return append(includeDirs, baseCompilerProps.Local_include_dirs...)
}
// Parse the list of copts.
parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string {
copts := append([]string{}, baseCompilerProps.Cflags...)
for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
copts = append(copts, includeFlag(dir))
}
return copts
} }
for _, props := range module.compiler.compilerProps() { for _, props := range module.compiler.compilerProps() {
if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
hdrs.Value, srcs.Value = hdrsAndSrcs(baseCompilerProps) srcs.Value = parseSrcs(baseCompilerProps)
copts.Value = baseCompilerProps.Cflags copts.Value = parseCopts(baseCompilerProps)
break break
} }
} }
if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
copts.Value = append(copts.Value, includeFlag("."))
localHdrs.Value = bp2BuildListHeadersInDir(ctx, ".")
} else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() {
copts.Value = append(copts.Value, includeFlag("."))
localHdrs.Value = bp2BuildListHeadersInDir(ctx, ".")
}
for arch, props := range module.GetArchProperties(&BaseCompilerProperties{}) { for arch, props := range module.GetArchProperties(&BaseCompilerProperties{}) {
if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
hdrsList, srcsList := hdrsAndSrcs(baseCompilerProps) srcsList := parseSrcs(baseCompilerProps)
hdrs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(hdrsList, hdrs.Value)) srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcsList, srcs.Value))
srcs.SetValueForArch(arch.Name, srcsList) copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps))
copts.SetValueForArch(arch.Name, baseCompilerProps.Cflags)
} }
} }
for os, props := range module.GetTargetProperties(&BaseCompilerProperties{}) { for os, props := range module.GetTargetProperties(&BaseCompilerProperties{}) {
if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
hdrsList, srcsList := hdrsAndSrcs(baseCompilerProps) srcsList := parseSrcs(baseCompilerProps)
hdrs.SetValueForOS(os.Name, bazel.SubtractBazelLabelList(hdrsList, hdrs.Value)) srcs.SetValueForOS(os.Name, bazel.SubtractBazelLabelList(srcsList, srcs.Value))
srcs.SetValueForOS(os.Name, srcsList) copts.SetValueForOS(os.Name, parseCopts(baseCompilerProps))
copts.SetValueForOS(os.Name, baseCompilerProps.Cflags)
} }
} }
// Combine local, non-exported hdrs into srcs
srcs.Append(localHdrs)
return compilerAttributes{ return compilerAttributes{
hdrs: hdrs,
srcs: srcs, srcs: srcs,
copts: copts, copts: copts,
} }
@ -120,7 +161,6 @@ type linkerAttributes struct {
// bp2BuildParseLinkerProps creates a label list attribute containing the header library deps of a module, including // bp2BuildParseLinkerProps creates a label list attribute containing the header library deps of a module, including
// configurable attribute values. // configurable attribute values.
func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes { func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
var deps bazel.LabelListAttribute var deps bazel.LabelListAttribute
var linkopts bazel.StringListAttribute var linkopts bazel.StringListAttribute
@ -128,8 +168,10 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module)
if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok { if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
libs := baseLinkerProps.Header_libs libs := baseLinkerProps.Header_libs
libs = append(libs, baseLinkerProps.Export_header_lib_headers...) libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
deps = bazel.MakeLabelListAttribute( libs = append(libs, baseLinkerProps.Static_libs...)
android.BazelLabelForModuleDeps(ctx, android.SortedUniqueStrings(libs))) libs = append(libs, baseLinkerProps.Whole_static_libs...)
libs = android.SortedUniqueStrings(libs)
deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
linkopts.Value = baseLinkerProps.Ldflags linkopts.Value = baseLinkerProps.Ldflags
break break
} }
@ -139,6 +181,8 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module)
if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok { if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
libs := baseLinkerProps.Header_libs libs := baseLinkerProps.Header_libs
libs = append(libs, baseLinkerProps.Export_header_lib_headers...) libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
libs = append(libs, baseLinkerProps.Static_libs...)
libs = append(libs, baseLinkerProps.Whole_static_libs...)
libs = android.SortedUniqueStrings(libs) libs = android.SortedUniqueStrings(libs)
deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs)) deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
linkopts.SetValueForArch(arch.Name, baseLinkerProps.Ldflags) linkopts.SetValueForArch(arch.Name, baseLinkerProps.Ldflags)
@ -149,6 +193,8 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module)
if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok { if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
libs := baseLinkerProps.Header_libs libs := baseLinkerProps.Header_libs
libs = append(libs, baseLinkerProps.Export_header_lib_headers...) libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
libs = append(libs, baseLinkerProps.Static_libs...)
libs = append(libs, baseLinkerProps.Whole_static_libs...)
libs = android.SortedUniqueStrings(libs) libs = android.SortedUniqueStrings(libs)
deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs)) deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
linkopts.SetValueForOS(os.Name, baseLinkerProps.Ldflags) linkopts.SetValueForOS(os.Name, baseLinkerProps.Ldflags)
@ -162,28 +208,44 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module)
} }
func bp2BuildListHeadersInDir(ctx android.TopDownMutatorContext, includeDir string) bazel.LabelList { func bp2BuildListHeadersInDir(ctx android.TopDownMutatorContext, includeDir string) bazel.LabelList {
globs := bazel.GlobsInDir(includeDir, includeDir != ".", headerExts) globs := bazel.GlobsInDir(includeDir, true, headerExts)
return android.BazelLabelForModuleSrc(ctx, globs) return android.BazelLabelForModuleSrc(ctx, globs)
} }
// Bazel wants include paths to be relative to the module // Relativize a list of root-relative paths with respect to the module's
func bp2BuildMakePathsRelativeToModule(ctx android.TopDownMutatorContext, paths []string) []string { // directory.
//
// include_dirs Soong prop are root-relative (b/183742505), but
// local_include_dirs, export_include_dirs and export_system_include_dirs are
// module dir relative. This function makes a list of paths entirely module dir
// relative.
//
// For the `include` attribute, Bazel wants the paths to be relative to the
// module.
func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
var relativePaths []string var relativePaths []string
for _, path := range paths { for _, path := range paths {
relativePath := strings.TrimPrefix(path, ctx.ModuleDir()+"/") // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
if err != nil {
panic(err)
}
relativePaths = append(relativePaths, relativePath) relativePaths = append(relativePaths, relativePath)
} }
return relativePaths return relativePaths
} }
// bp2BuildParseExportedIncludes creates a label list attribute contains the // bp2BuildParseExportedIncludes creates a string list attribute contains the
// exported included directories of a module. // exported included directories of a module, and a label list attribute
// containing the exported headers of a module.
func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) (bazel.StringListAttribute, bazel.LabelListAttribute) { func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) (bazel.StringListAttribute, bazel.LabelListAttribute) {
libraryDecorator := module.linker.(*libraryDecorator) libraryDecorator := module.linker.(*libraryDecorator)
// Export_system_include_dirs and export_include_dirs are already module dir
// relative, so they don't need to be relativized like include_dirs, which
// are root-relative.
includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...) includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
includeDirs = bp2BuildMakePathsRelativeToModule(ctx, includeDirs)
includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs) includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
var headersAttribute bazel.LabelListAttribute var headersAttribute bazel.LabelListAttribute
@ -198,7 +260,6 @@ func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Mo
if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
archIncludeDirs := flagExporterProperties.Export_system_include_dirs archIncludeDirs := flagExporterProperties.Export_system_include_dirs
archIncludeDirs = append(archIncludeDirs, flagExporterProperties.Export_include_dirs...) archIncludeDirs = append(archIncludeDirs, flagExporterProperties.Export_include_dirs...)
archIncludeDirs = bp2BuildMakePathsRelativeToModule(ctx, archIncludeDirs)
// To avoid duplicate includes when base includes + arch includes are combined // To avoid duplicate includes when base includes + arch includes are combined
archIncludeDirs = bazel.SubtractStrings(archIncludeDirs, includeDirs) archIncludeDirs = bazel.SubtractStrings(archIncludeDirs, includeDirs)

View File

@ -260,11 +260,10 @@ func CcLibraryBp2Build(ctx android.TopDownMutatorContext) {
compilerAttrs := bp2BuildParseCompilerProps(ctx, m) compilerAttrs := bp2BuildParseCompilerProps(ctx, m)
linkerAttrs := bp2BuildParseLinkerProps(ctx, m) linkerAttrs := bp2BuildParseLinkerProps(ctx, m)
exportedIncludes, exportedIncludesHeaders := bp2BuildParseExportedIncludes(ctx, m) exportedIncludes, exportedIncludesHeaders := bp2BuildParseExportedIncludes(ctx, m)
compilerAttrs.hdrs.Append(exportedIncludesHeaders)
attrs := &bazelCcLibraryAttributes{ attrs := &bazelCcLibraryAttributes{
Srcs: compilerAttrs.srcs, Srcs: compilerAttrs.srcs,
Hdrs: compilerAttrs.hdrs, Hdrs: exportedIncludesHeaders,
Copts: compilerAttrs.copts, Copts: compilerAttrs.copts,
Linkopts: linkerAttrs.linkopts, Linkopts: linkerAttrs.linkopts,
Deps: linkerAttrs.deps, Deps: linkerAttrs.deps,
@ -2163,69 +2162,16 @@ func CcLibraryStaticBp2Build(ctx android.TopDownMutatorContext) {
} }
compilerAttrs := bp2BuildParseCompilerProps(ctx, module) compilerAttrs := bp2BuildParseCompilerProps(ctx, module)
var includeDirs []string
var localIncludeDirs []string
for _, props := range module.compiler.compilerProps() {
if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
// TODO: these should be arch and os specific.
includeDirs = bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
localIncludeDirs = bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Local_include_dirs)
break
}
}
// Soong implicitly includes headers from the module's directory.
// For Bazel builds to work we have to make these header includes explicit.
if module.compiler.(*libraryDecorator).includeBuildDirectory() {
localIncludeDirs = append(localIncludeDirs, ".")
}
// For Bazel, be more explicit about headers - list all header files in include dirs as srcs
for _, includeDir := range includeDirs {
compilerAttrs.srcs.Value.Append(bp2BuildListHeadersInDir(ctx, includeDir))
}
for _, localIncludeDir := range localIncludeDirs {
compilerAttrs.srcs.Value.Append(bp2BuildListHeadersInDir(ctx, localIncludeDir))
}
var staticLibs []string
var wholeStaticLibs []string
for _, props := range module.linker.linkerProps() {
// TODO: move this into bp2buildParseLinkerProps
if baseLinkerProperties, ok := props.(*BaseLinkerProperties); ok {
staticLibs = baseLinkerProperties.Static_libs
wholeStaticLibs = baseLinkerProperties.Whole_static_libs
break
}
}
// FIXME: Treat Static_libs and Whole_static_libs differently?
allDeps := staticLibs
allDeps = append(allDeps, wholeStaticLibs...)
depsLabels := android.BazelLabelForModuleDeps(ctx, allDeps)
exportedIncludes, exportedIncludesHeaders := bp2BuildParseExportedIncludes(ctx, module)
// FIXME: Unify absolute vs relative paths
// FIXME: Use -I copts instead of setting includes= ?
allIncludes := exportedIncludes
allIncludes.Value = append(allIncludes.Value, includeDirs...)
allIncludes.Value = append(allIncludes.Value, localIncludeDirs...)
compilerAttrs.hdrs.Append(exportedIncludesHeaders)
linkerAttrs := bp2BuildParseLinkerProps(ctx, module) linkerAttrs := bp2BuildParseLinkerProps(ctx, module)
depsLabels.Append(linkerAttrs.deps.Value) exportedIncludes, exportedIncludesHeaders := bp2BuildParseExportedIncludes(ctx, module)
attrs := &bazelCcLibraryStaticAttributes{ attrs := &bazelCcLibraryStaticAttributes{
Copts: compilerAttrs.copts, Copts: compilerAttrs.copts,
Srcs: compilerAttrs.srcs, Srcs: compilerAttrs.srcs,
Deps: bazel.MakeLabelListAttribute(depsLabels), Deps: linkerAttrs.deps,
Linkstatic: true, Linkstatic: true,
Includes: allIncludes, Includes: exportedIncludes,
Hdrs: compilerAttrs.hdrs, Hdrs: exportedIncludesHeaders,
} }
props := bazel.BazelTargetModuleProperties{ props := bazel.BazelTargetModuleProperties{

View File

@ -95,14 +95,14 @@ func CcLibraryHeadersBp2Build(ctx android.TopDownMutatorContext) {
return return
} }
exportedIncludes, exportedIncludesHeaders := bp2BuildParseExportedIncludes(ctx, module) exportedIncludes, exportedHdrs := bp2BuildParseExportedIncludes(ctx, module)
compilerAttrs := bp2BuildParseCompilerProps(ctx, module) compilerAttrs := bp2BuildParseCompilerProps(ctx, module)
linkerAttrs := bp2BuildParseLinkerProps(ctx, module) linkerAttrs := bp2BuildParseLinkerProps(ctx, module)
attrs := &bazelCcLibraryHeadersAttributes{ attrs := &bazelCcLibraryHeadersAttributes{
Copts: compilerAttrs.copts, Copts: compilerAttrs.copts,
Includes: exportedIncludes, Includes: exportedIncludes,
Hdrs: exportedIncludesHeaders, Hdrs: exportedHdrs,
Deps: linkerAttrs.deps, Deps: linkerAttrs.deps,
} }

View File

@ -112,12 +112,11 @@ func ObjectFactory() android.Module {
// For bp2build conversion. // For bp2build conversion.
type bazelObjectAttributes struct { type bazelObjectAttributes struct {
Srcs bazel.LabelListAttribute Srcs bazel.LabelListAttribute
Hdrs bazel.LabelListAttribute Hdrs bazel.LabelListAttribute
Deps bazel.LabelListAttribute Deps bazel.LabelListAttribute
Copts bazel.StringListAttribute Copts bazel.StringListAttribute
Asflags []string Asflags []string
Local_include_dirs []string
} }
type bazelObject struct { type bazelObject struct {
@ -158,18 +157,7 @@ func ObjectBp2Build(ctx android.TopDownMutatorContext) {
// Set arch-specific configurable attributes // Set arch-specific configurable attributes
compilerAttrs := bp2BuildParseCompilerProps(ctx, m) compilerAttrs := bp2BuildParseCompilerProps(ctx, m)
var localIncludeDirs []string
var asFlags []string var asFlags []string
for _, props := range m.compiler.compilerProps() {
if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
localIncludeDirs = baseCompilerProps.Local_include_dirs
break
}
}
if c, ok := m.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
localIncludeDirs = append(localIncludeDirs, ".")
}
var deps bazel.LabelListAttribute var deps bazel.LabelListAttribute
for _, props := range m.linker.linkerProps() { for _, props := range m.linker.linkerProps() {
@ -197,12 +185,10 @@ func ObjectBp2Build(ctx android.TopDownMutatorContext) {
// TODO(b/183595872) warn/error if we're not handling product variables // TODO(b/183595872) warn/error if we're not handling product variables
attrs := &bazelObjectAttributes{ attrs := &bazelObjectAttributes{
Srcs: compilerAttrs.srcs, Srcs: compilerAttrs.srcs,
Hdrs: compilerAttrs.hdrs, Deps: deps,
Deps: deps, Copts: compilerAttrs.copts,
Copts: compilerAttrs.copts, Asflags: asFlags,
Asflags: asFlags,
Local_include_dirs: localIncludeDirs,
} }
props := bazel.BazelTargetModuleProperties{ props := bazel.BazelTargetModuleProperties{