Add bp2build converter for cc_genrule.
Test: cc_genrule_conversion_test.go
Change-Id: I19290b417d6336020a15ba7fa772ee0c76c58225
diff --git a/android/bazel.go b/android/bazel.go
index bfd0f90..6fbffcc 100644
--- a/android/bazel.go
+++ b/android/bazel.go
@@ -271,12 +271,16 @@
// Per-module denylist to opt modules out of mixed builds. Such modules will
// still be generated via bp2build.
mixedBuildsDisabledList = []string{
- "libbrotli", // http://b/198585397, ld.lld: error: bionic/libc/arch-arm64/generic/bionic/memmove.S:95:(.text+0x10): relocation R_AARCH64_CONDBR19 out of range: -1404176 is not in [-1048576, 1048575]; references __memcpy
- "libc++fs", // http://b/198403271, Missing symbols/members in the global namespace when referenced from headers in //external/libcxx/includes
- "libc++_experimental", // http://b/198403271, Missing symbols/members in the global namespace when referenced from headers in //external/libcxx/includes
- "libc++_static", // http://b/198403271, Missing symbols/members in the global namespace when referenced from headers in //external/libcxx/includes
- "libc++abi", // http://b/195970501, cc_library_static, duplicate symbols because it propagates libc objects.
- "libc++demangle", // http://b/195970501, cc_library_static, duplicate symbols because it propagates libc objects.
+ "libbrotli", // http://b/198585397, ld.lld: error: bionic/libc/arch-arm64/generic/bionic/memmove.S:95:(.text+0x10): relocation R_AARCH64_CONDBR19 out of range: -1404176 is not in [-1048576, 1048575]; references __memcpy
+ "libc++fs", // http://b/198403271, Missing symbols/members in the global namespace when referenced from headers in //external/libcxx/includes
+ "libc++_experimental", // http://b/198403271, Missing symbols/members in the global namespace when referenced from headers in //external/libcxx/includes
+ "libc++_static", // http://b/198403271, Missing symbols/members in the global namespace when referenced from headers in //external/libcxx/includes
+ "libc++abi", // http://b/195970501, cc_library_static, duplicate symbols because it propagates libc objects.
+ "libc++demangle", // http://b/195970501, cc_library_static, duplicate symbols because it propagates libc objects.
+ "func_to_syscall_nrs", // http://b/200899432, bazel-built cc_genrule does not work in mixed build when it is a dependency of another soong module.
+ "libseccomp_policy_app_zygote_sources", // http://b/200899432, bazel-built cc_genrule does not work in mixed build when it is a dependency of another soong module.
+ "libseccomp_policy_app_sources", // http://b/200899432, bazel-built cc_genrule does not work in mixed build when it is a dependency of another soong module.
+ "libseccomp_policy_system_sources", // http://b/200899432, bazel-built cc_genrule does not work in mixed build when it is a dependency of another soong module.
}
// Used for quicker lookups
diff --git a/bp2build/Android.bp b/bp2build/Android.bp
index 40526a6..1250e92 100644
--- a/bp2build/Android.bp
+++ b/bp2build/Android.bp
@@ -33,6 +33,7 @@
"apex_key_conversion_test.go",
"build_conversion_test.go",
"bzl_conversion_test.go",
+ "cc_genrule_conversion_test.go",
"cc_library_conversion_test.go",
"cc_library_headers_conversion_test.go",
"cc_library_static_conversion_test.go",
diff --git a/bp2build/cc_genrule_conversion_test.go b/bp2build/cc_genrule_conversion_test.go
new file mode 100644
index 0000000..a7e9cb2
--- /dev/null
+++ b/bp2build/cc_genrule_conversion_test.go
@@ -0,0 +1,258 @@
+// 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 (
+ "testing"
+
+ "android/soong/android"
+ "android/soong/cc"
+ "android/soong/genrule"
+)
+
+var otherCcGenruleBp = map[string]string{
+ "other/Android.bp": `cc_genrule {
+ name: "foo.tool",
+ out: ["foo_tool.out"],
+ srcs: ["foo_tool.in"],
+ cmd: "cp $(in) $(out)",
+}
+cc_genrule {
+ name: "other.tool",
+ out: ["other_tool.out"],
+ srcs: ["other_tool.in"],
+ cmd: "cp $(in) $(out)",
+}`,
+}
+
+func runCcGenruleTestCase(t *testing.T, tc bp2buildTestCase) {
+ t.Helper()
+ runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
+}
+
+func TestCliVariableReplacement(t *testing.T) {
+ runCcGenruleTestCase(t, bp2buildTestCase{
+ description: "cc_genrule with command line variable replacements",
+ moduleTypeUnderTest: "cc_genrule",
+ moduleTypeUnderTestFactory: cc.GenRuleFactory,
+ moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
+ blueprint: `cc_genrule {
+ name: "foo.tool",
+ out: ["foo_tool.out"],
+ srcs: ["foo_tool.in"],
+ cmd: "cp $(in) $(out)",
+ bazel_module: { bp2build_available: true },
+}
+
+cc_genrule {
+ name: "foo",
+ out: ["foo.out"],
+ srcs: ["foo.in"],
+ tools: [":foo.tool"],
+ cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
+ bazel_module: { bp2build_available: true },
+}`,
+ expectedBazelTargets: []string{
+ `genrule(
+ name = "foo",
+ cmd = "$(location :foo.tool) --genDir=$(RULEDIR) arg $(SRCS) $(OUTS)",
+ outs = ["foo.out"],
+ srcs = ["foo.in"],
+ tools = [":foo.tool"],
+)`,
+ `genrule(
+ name = "foo.tool",
+ cmd = "cp $(SRCS) $(OUTS)",
+ outs = ["foo_tool.out"],
+ srcs = ["foo_tool.in"],
+)`,
+ },
+ })
+}
+
+func TestUsingLocationsLabel(t *testing.T) {
+ runCcGenruleTestCase(t, bp2buildTestCase{
+ description: "cc_genrule using $(locations :label)",
+ moduleTypeUnderTest: "cc_genrule",
+ moduleTypeUnderTestFactory: cc.GenRuleFactory,
+ moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
+ blueprint: `cc_genrule {
+ name: "foo.tools",
+ out: ["foo_tool.out", "foo_tool2.out"],
+ srcs: ["foo_tool.in"],
+ cmd: "cp $(in) $(out)",
+ bazel_module: { bp2build_available: true },
+}
+
+cc_genrule {
+ name: "foo",
+ out: ["foo.out"],
+ srcs: ["foo.in"],
+ tools: [":foo.tools"],
+ cmd: "$(locations :foo.tools) -s $(out) $(in)",
+ bazel_module: { bp2build_available: true },
+}`,
+ expectedBazelTargets: []string{`genrule(
+ name = "foo",
+ cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
+ outs = ["foo.out"],
+ srcs = ["foo.in"],
+ tools = [":foo.tools"],
+)`,
+ `genrule(
+ name = "foo.tools",
+ cmd = "cp $(SRCS) $(OUTS)",
+ outs = [
+ "foo_tool.out",
+ "foo_tool2.out",
+ ],
+ srcs = ["foo_tool.in"],
+)`,
+ },
+ })
+}
+
+func TestUsingLocationsAbsoluteLabel(t *testing.T) {
+ runCcGenruleTestCase(t, bp2buildTestCase{
+ description: "cc_genrule using $(locations //absolute:label)",
+ moduleTypeUnderTest: "cc_genrule",
+ moduleTypeUnderTestFactory: cc.GenRuleFactory,
+ moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
+ blueprint: `cc_genrule {
+ name: "foo",
+ out: ["foo.out"],
+ srcs: ["foo.in"],
+ tool_files: [":foo.tool"],
+ cmd: "$(locations :foo.tool) -s $(out) $(in)",
+ bazel_module: { bp2build_available: true },
+}`,
+ expectedBazelTargets: []string{`genrule(
+ name = "foo",
+ cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
+ outs = ["foo.out"],
+ srcs = ["foo.in"],
+ tools = ["//other:foo.tool"],
+)`,
+ },
+ filesystem: otherCcGenruleBp,
+ })
+}
+
+func TestSrcsUsingAbsoluteLabel(t *testing.T) {
+ runCcGenruleTestCase(t, bp2buildTestCase{
+ description: "cc_genrule srcs using $(locations //absolute:label)",
+ moduleTypeUnderTest: "cc_genrule",
+ moduleTypeUnderTestFactory: cc.GenRuleFactory,
+ moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
+ blueprint: `cc_genrule {
+ name: "foo",
+ out: ["foo.out"],
+ srcs: [":other.tool"],
+ tool_files: [":foo.tool"],
+ cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
+ bazel_module: { bp2build_available: true },
+}`,
+ expectedBazelTargets: []string{`genrule(
+ name = "foo",
+ cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
+ outs = ["foo.out"],
+ srcs = ["//other:other.tool"],
+ tools = ["//other:foo.tool"],
+)`,
+ },
+ filesystem: otherCcGenruleBp,
+ })
+}
+
+func TestLocationsLabelUsesFirstToolFile(t *testing.T) {
+ runCcGenruleTestCase(t, bp2buildTestCase{
+ description: "cc_genrule using $(location) label should substitute first tool label automatically",
+ moduleTypeUnderTest: "cc_genrule",
+ moduleTypeUnderTestFactory: cc.GenRuleFactory,
+ moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
+ blueprint: `cc_genrule {
+ name: "foo",
+ out: ["foo.out"],
+ srcs: ["foo.in"],
+ tool_files: [":foo.tool", ":other.tool"],
+ cmd: "$(location) -s $(out) $(in)",
+ bazel_module: { bp2build_available: true },
+}`,
+ expectedBazelTargets: []string{`genrule(
+ name = "foo",
+ cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
+ outs = ["foo.out"],
+ srcs = ["foo.in"],
+ tools = [
+ "//other:foo.tool",
+ "//other:other.tool",
+ ],
+)`,
+ },
+ filesystem: otherCcGenruleBp,
+ })
+}
+
+func TestLocationsLabelUsesFirstTool(t *testing.T) {
+ runCcGenruleTestCase(t, bp2buildTestCase{
+ description: "cc_genrule using $(locations) label should substitute first tool label automatically",
+ moduleTypeUnderTest: "cc_genrule",
+ moduleTypeUnderTestFactory: cc.GenRuleFactory,
+ moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
+ blueprint: `cc_genrule {
+ name: "foo",
+ out: ["foo.out"],
+ srcs: ["foo.in"],
+ tools: [":foo.tool", ":other.tool"],
+ cmd: "$(locations) -s $(out) $(in)",
+ bazel_module: { bp2build_available: true },
+}`,
+ expectedBazelTargets: []string{`genrule(
+ name = "foo",
+ cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
+ outs = ["foo.out"],
+ srcs = ["foo.in"],
+ tools = [
+ "//other:foo.tool",
+ "//other:other.tool",
+ ],
+)`,
+ },
+ filesystem: otherCcGenruleBp,
+ })
+}
+
+func TestWithoutToolsOrToolFiles(t *testing.T) {
+ runCcGenruleTestCase(t, bp2buildTestCase{
+ description: "cc_genrule without tools or tool_files can convert successfully",
+ moduleTypeUnderTest: "cc_genrule",
+ moduleTypeUnderTestFactory: cc.GenRuleFactory,
+ moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
+ blueprint: `cc_genrule {
+ name: "foo",
+ out: ["foo.out"],
+ srcs: ["foo.in"],
+ cmd: "cp $(in) $(out)",
+ bazel_module: { bp2build_available: true },
+}`,
+ expectedBazelTargets: []string{`genrule(
+ name = "foo",
+ cmd = "cp $(SRCS) $(OUTS)",
+ outs = ["foo.out"],
+ srcs = ["foo.in"],
+)`,
+ },
+ })
+}
diff --git a/cc/genrule.go b/cc/genrule.go
index 0ca901e..9df5228 100644
--- a/cc/genrule.go
+++ b/cc/genrule.go
@@ -21,7 +21,7 @@
)
func init() {
- android.RegisterModuleType("cc_genrule", genRuleFactory)
+ android.RegisterModuleType("cc_genrule", GenRuleFactory)
}
type GenruleExtraProperties struct {
@@ -37,7 +37,7 @@
// cc_genrule is a genrule that can depend on other cc_* objects.
// The cmd may be run multiple times, once for each of the different arch/etc
// variations.
-func genRuleFactory() android.Module {
+func GenRuleFactory() android.Module {
module := genrule.NewGenRule()
extra := &GenruleExtraProperties{}
@@ -48,6 +48,7 @@
android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibBoth)
android.InitApexModule(module)
+ android.InitBazelModule(module)
return module
}
diff --git a/cc/genrule_test.go b/cc/genrule_test.go
index 45b343b..b6afb05 100644
--- a/cc/genrule_test.go
+++ b/cc/genrule_test.go
@@ -23,7 +23,7 @@
func testGenruleContext(config android.Config) *android.TestContext {
ctx := android.NewTestArchContext(config)
- ctx.RegisterModuleType("cc_genrule", genRuleFactory)
+ ctx.RegisterModuleType("cc_genrule", GenRuleFactory)
ctx.Register()
return ctx
diff --git a/cc/testing.go b/cc/testing.go
index d0dca6b..b0a220c 100644
--- a/cc/testing.go
+++ b/cc/testing.go
@@ -33,7 +33,7 @@
ctx.RegisterModuleType("toolchain_library", ToolchainLibraryFactory)
ctx.RegisterModuleType("cc_benchmark", BenchmarkFactory)
ctx.RegisterModuleType("cc_object", ObjectFactory)
- ctx.RegisterModuleType("cc_genrule", genRuleFactory)
+ ctx.RegisterModuleType("cc_genrule", GenRuleFactory)
ctx.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
ctx.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
ctx.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
diff --git a/genrule/genrule.go b/genrule/genrule.go
index bde6e97..f4bde70 100644
--- a/genrule/genrule.go
+++ b/genrule/genrule.go
@@ -69,6 +69,7 @@
})
android.RegisterBp2BuildMutator("genrule", GenruleBp2Build)
+ android.RegisterBp2BuildMutator("cc_genrule", CcGenruleBp2Build)
}
func RegisterGenruleBp2BuildDeps(ctx android.RegisterMutatorsContext) {
@@ -826,6 +827,22 @@
Cmd string
}
+// CcGenruleBp2Build is for cc_genrule.
+func CcGenruleBp2Build(ctx android.TopDownMutatorContext) {
+ m, ok := ctx.Module().(*Module)
+ if !ok || !m.ConvertWithBp2build(ctx) {
+ return
+ }
+
+ if ctx.ModuleType() != "cc_genrule" {
+ // Not a cc_genrule.
+ return
+ }
+
+ genruleBp2Build(ctx)
+}
+
+// GenruleBp2Build is used for genrule.
func GenruleBp2Build(ctx android.TopDownMutatorContext) {
m, ok := ctx.Module().(*Module)
if !ok || !m.ConvertWithBp2build(ctx) {
@@ -833,10 +850,15 @@
}
if ctx.ModuleType() != "genrule" {
- // Not a regular genrule. Could be a cc_genrule or java_genrule.
+ // Not a regular genrule.
return
}
+ genruleBp2Build(ctx)
+}
+
+func genruleBp2Build(ctx android.TopDownMutatorContext) {
+ m, _ := ctx.Module().(*Module)
// Bazel only has the "tools" attribute.
tools_prop := android.BazelLabelForModuleDeps(ctx, m.properties.Tools)
tool_files_prop := android.BazelLabelForModuleSrc(ctx, m.properties.Tool_files)
@@ -854,7 +876,11 @@
if m.properties.Cmd != nil {
cmd = strings.Replace(*m.properties.Cmd, "$(in)", "$(SRCS)", -1)
cmd = strings.Replace(cmd, "$(out)", "$(OUTS)", -1)
- cmd = strings.Replace(cmd, "$(genDir)", "$(GENDIR)", -1)
+ genDir := "$(GENDIR)"
+ if ctx.ModuleType() == "cc_genrule" {
+ genDir = "$(RULEDIR)"
+ }
+ cmd = strings.Replace(cmd, "$(genDir)", genDir, -1)
if len(tools.Value.Includes) > 0 {
cmd = strings.Replace(cmd, "$(location)", fmt.Sprintf("$(location %s)", tools.Value.Includes[0].Label), -1)
cmd = strings.Replace(cmd, "$(locations)", fmt.Sprintf("$(locations %s)", tools.Value.Includes[0].Label), -1)