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

Commit 378fc1b4 authored by Rupert Shuttleworth's avatar Rupert Shuttleworth
Browse files

Add initial bp2build convert for prebuilt_etc modules.

Test: Added unit test.

Change-Id: Ie63674e4a867e355e4667b4240979b822f0f3c86
parent 8e5b4c50
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ bootstrap_go_package {
        "soong-bazel",
        "soong-cc",
        "soong-cc-config",
        "soong-etc",
        "soong-genrule",
        "soong-python",
        "soong-sh",
@@ -37,6 +38,7 @@ bootstrap_go_package {
        "cc_library_static_conversion_test.go",
        "cc_object_conversion_test.go",
        "conversion_test.go",
        "prebuilt_etc_conversion_test.go",
        "python_binary_conversion_test.go",
        "sh_conversion_test.go",
        "testing.go",
+55 −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 (
	"android/soong/android"
	"android/soong/etc"

	"testing"
)

func runPrebuiltEtcTestCase(t *testing.T, tc bp2buildTestCase) {
	t.Helper()
	runBp2BuildTestCase(t, registerPrebuiltEtcModuleTypes, tc)
}

func registerPrebuiltEtcModuleTypes(ctx android.RegistrationContext) {
}

func TestPrebuiltEtcSimple(t *testing.T) {
	runPrebuiltEtcTestCase(t, bp2buildTestCase{
		description:                        "prebuilt_etc - simple example",
		moduleTypeUnderTest:                "prebuilt_etc",
		moduleTypeUnderTestFactory:         etc.PrebuiltEtcFactory,
		moduleTypeUnderTestBp2BuildMutator: etc.PrebuiltEtcBp2Build,
		filesystem:                         map[string]string{},
		blueprint: `
prebuilt_etc {
    name: "apex_tz_version",
    src: "version/tz_version",
    filename: "tz_version",
    sub_dir: "tz",
    installable: false,
}
`,
		expectedBazelTargets: []string{`prebuilt_etc(
    name = "apex_tz_version",
    filename = "tz_version",
    installable = False,
    src = "version/tz_version",
    sub_dir = "tz",
)`}})
}
+84 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import (
	"github.com/google/blueprint/proptools"

	"android/soong/android"
	"android/soong/bazel"
	"android/soong/snapshot"
)

@@ -61,6 +62,8 @@ func RegisterPrebuiltEtcBuildComponents(ctx android.RegistrationContext) {
	ctx.RegisterModuleType("prebuilt_rfsa", PrebuiltRFSAFactory)

	ctx.RegisterModuleType("prebuilt_defaults", defaultsFactory)

	android.RegisterBp2BuildMutator("prebuilt_etc", PrebuiltEtcBp2Build)
}

var PrepareForTestWithPrebuiltEtc = android.FixtureRegisterWithContext(RegisterPrebuiltEtcBuildComponents)
@@ -131,6 +134,7 @@ type PrebuiltEtcModule interface {
type PrebuiltEtc struct {
	android.ModuleBase
	android.DefaultableModuleBase
	android.BazelModuleBase

	snapshot.VendorSnapshotModuleInterface
	snapshot.RecoverySnapshotModuleInterface
@@ -406,6 +410,7 @@ func PrebuiltEtcFactory() android.Module {
	// This module is device-only
	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
	android.InitDefaultableModule(module)
	android.InitBazelModule(module)
	return module
}

@@ -647,3 +652,82 @@ func generatePrebuiltSnapshot(s snapshot.SnapshotSingleton, ctx android.Singleto

	return snapshotOutputs
}

// For Bazel / bp2build

type bazelPrebuiltEtcAttributes struct {
	Src         bazel.LabelAttribute
	Filename    string
	Sub_dir     string
	Installable bazel.BoolAttribute
}

type bazelPrebuiltEtc struct {
	android.BazelTargetModuleBase
	bazelPrebuiltEtcAttributes
}

func BazelPrebuiltEtcFactory() android.Module {
	module := &bazelPrebuiltEtc{}
	module.AddProperties(&module.bazelPrebuiltEtcAttributes)
	android.InitBazelTargetModule(module)
	return module
}

func PrebuiltEtcBp2Build(ctx android.TopDownMutatorContext) {
	module, ok := ctx.Module().(*PrebuiltEtc)
	if !ok {
		// Not an prebuilt_etc
		return
	}
	if !module.ConvertWithBp2build(ctx) {
		return
	}
	if ctx.ModuleType() != "prebuilt_etc" {
		return
	}

	prebuiltEtcBp2BuildInternal(ctx, module)
}

func prebuiltEtcBp2BuildInternal(ctx android.TopDownMutatorContext, module *PrebuiltEtc) {
	var srcLabelAttribute bazel.LabelAttribute
	if module.properties.Src != nil {
		srcLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *module.properties.Src))
	}

	var filename string
	if module.properties.Filename != nil {
		filename = *module.properties.Filename
	}

	var subDir string
	if module.subdirProperties.Sub_dir != nil {
		subDir = *module.subdirProperties.Sub_dir
	}

	var installableBoolAttribute bazel.BoolAttribute
	if module.properties.Installable != nil {
		installableBoolAttribute.Value = module.properties.Installable
	}

	attrs := &bazelPrebuiltEtcAttributes{
		Src:         srcLabelAttribute,
		Filename:    filename,
		Sub_dir:     subDir,
		Installable: installableBoolAttribute,
	}

	props := bazel.BazelTargetModuleProperties{
		Rule_class:        "prebuilt_etc",
		Bzl_load_location: "//build/bazel/rules:prebuilt_etc.bzl",
	}

	ctx.CreateBazelTargetModule(BazelPrebuiltEtcFactory, module.Name(), props, attrs)
}

func (m *bazelPrebuiltEtc) Name() string {
	return m.BaseModuleName()
}

func (m *bazelPrebuiltEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) {}