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

Commit 98716bdf authored by Jaewoong Jung's avatar Jaewoong Jung
Browse files

Add defaults support to genrule.

Bug: 119635195
Test: genrule_test.go
Change-Id: I8aeff18760b031491012dd4864985b4ed01dbf3a
parent 9b84d34b
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ import (
)

func init() {
	android.RegisterModuleType("genrule_defaults", defaultsFactory)

	android.RegisterModuleType("gensrcs", GenSrcsFactory)
	android.RegisterModuleType("genrule", GenRuleFactory)
}
@@ -95,6 +97,7 @@ type generatorProperties struct {

type Module struct {
	android.ModuleBase
	android.DefaultableModuleBase

	// For other packages to make their own genrules with extra
	// properties
@@ -502,6 +505,7 @@ func NewGenRule() *Module {
func GenRuleFactory() android.Module {
	m := NewGenRule()
	android.InitAndroidModule(m)
	android.InitDefaultableModule(m)
	return m
}

@@ -512,3 +516,35 @@ type genRuleProperties struct {

var Bool = proptools.Bool
var String = proptools.String

//
// Defaults
//
type Defaults struct {
	android.ModuleBase
	android.DefaultsModuleBase
}

func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
}

func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
}

func defaultsFactory() android.Module {
	return DefaultsFactory()
}

func DefaultsFactory(props ...interface{}) android.Module {
	module := &Defaults{}

	module.AddProperties(props...)
	module.AddProperties(
		&generatorProperties{},
		&genRuleProperties{},
	)

	android.InitDefaultsModule(module)

	return module
}
+43 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import (
	"testing"

	"android/soong/android"
	"reflect"
)

var buildDir string
@@ -54,7 +55,9 @@ func testContext(config android.Config, bp string,
	ctx := android.NewTestArchContext()
	ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
	ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(GenRuleFactory))
	ctx.RegisterModuleType("genrule_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
	ctx.RegisterModuleType("tool", android.ModuleFactoryAdaptor(toolFactory))
	ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
	ctx.Register()

	bp += `
@@ -465,6 +468,46 @@ func TestGenruleCmd(t *testing.T) {

}

func TestGenruleDefaults(t *testing.T) {
	config := android.TestArchConfig(buildDir, nil)
	bp := `
				genrule_defaults {
					name: "gen_defaults1",
					cmd: "cp $(in) $(out)",
				}

				genrule_defaults {
					name: "gen_defaults2",
					srcs: ["in1"],
				}

				genrule {
					name: "gen",
					out: ["out"],
					defaults: ["gen_defaults1", "gen_defaults2"],
				}
			`
	ctx := testContext(config, bp, nil)
	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
	if errs == nil {
		_, errs = ctx.PrepareBuildActions(config)
	}
	if errs != nil {
		t.Fatal(errs)
	}
	gen := ctx.ModuleForTests("gen", "").Module().(*Module)

	expectedCmd := "'cp ${in} __SBOX_OUT_FILES__'"
	if gen.rawCommand != expectedCmd {
		t.Errorf("Expected cmd: %q, actual: %q", expectedCmd, gen.rawCommand)
	}

	expectedSrcs := []string{"in1"}
	if !reflect.DeepEqual(expectedSrcs, gen.properties.Srcs) {
		t.Errorf("Expected srcs: %q, actual: %q", expectedSrcs, gen.properties.Srcs)
	}
}

type testTool struct {
	android.ModuleBase
	outputFile android.Path