Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bcd39947 authored by Wei Li's avatar Wei Li
Browse files

Add bp2build converter for cc_genrule.

Test: cc_genrule_conversion_test.go

Change-Id: I19290b417d6336020a15ba7fa772ee0c76c58225
parent 66213a64
Loading
Loading
Loading
Loading
+10 −6
Original line number Diff line number Diff line
@@ -277,6 +277,10 @@ var (
		"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
+1 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ bootstrap_go_package {
        "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",
+258 −0
Original line number Diff line number Diff line
// 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"],
)`,
		},
	})
}
+3 −2
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import (
)

func init() {
	android.RegisterModuleType("cc_genrule", genRuleFactory)
	android.RegisterModuleType("cc_genrule", GenRuleFactory)
}

type GenruleExtraProperties struct {
@@ -37,7 +37,7 @@ type GenruleExtraProperties struct {
// 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 @@ func genRuleFactory() android.Module {
	android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibBoth)

	android.InitApexModule(module)
	android.InitBazelModule(module)

	return module
}
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ import (

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
Loading