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

Commit 7ae2b0f2 authored by Cole Faust's avatar Cole Faust Committed by Mohammed Althaf T
Browse files

Add install_symlink soong module type

This can be used to install symlinks to arbitrary locations/targets
on the device. Used to replace a make-built symlink.

Bug: 205632228
Test: built and ran the emulator observed the /system/bin/hwservicemanager symlink is still there
Change-Id: I6df922c8d919e6d56fa79702815a89c98f4d65ed
parent 20bb2ad1
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -12,9 +12,11 @@ bootstrap_go_package {
    ],
    srcs: [
        "prebuilt_etc.go",
        "install_symlink.go",
    ],
    testSrcs: [
        "prebuilt_etc_test.go",
        "install_symlink_test.go",
    ],
    pluginFor: ["soong_build"],
}

etc/install_symlink.go

0 → 100644
+92 −0
Original line number Diff line number Diff line
// Copyright 2023 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 etc

import (
	"android/soong/android"
	"path/filepath"
	"strings"
)

func init() {
	RegisterInstallSymlinkBuildComponents(android.InitRegistrationContext)
}

func RegisterInstallSymlinkBuildComponents(ctx android.RegistrationContext) {
	ctx.RegisterModuleType("install_symlink", InstallSymlinkFactory)
}

// install_symlink can be used to install an symlink with an arbitrary target to an arbitrary path
// on the device.
func InstallSymlinkFactory() android.Module {
	module := &InstallSymlink{}
	module.AddProperties(&module.properties)
	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
	return module
}

type InstallSymlinkProperties struct {
	// Where to install this symlink, relative to the partition it's installed on.
	// Which partition it's installed on can be controlled by the vendor, system_ext, ramdisk, etc.
	// properties.
	Installed_location string
	// The target of the symlink, aka where the symlink points.
	Symlink_target string
}

type InstallSymlink struct {
	android.ModuleBase
	properties InstallSymlinkProperties

	output        android.Path
	installedPath android.InstallPath
}

func (m *InstallSymlink) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	if filepath.Clean(m.properties.Symlink_target) != m.properties.Symlink_target {
		ctx.PropertyErrorf("symlink_target", "Should be a clean filepath")
		return
	}
	if filepath.Clean(m.properties.Installed_location) != m.properties.Installed_location {
		ctx.PropertyErrorf("installed_location", "Should be a clean filepath")
		return
	}
	if strings.HasPrefix(m.properties.Installed_location, "../") || strings.HasPrefix(m.properties.Installed_location, "/") {
		ctx.PropertyErrorf("installed_location", "Should not start with / or ../")
		return
	}

	out := android.PathForModuleOut(ctx, "out.txt")
	android.WriteFileRuleVerbatim(ctx, out, "")
	m.output = out

	name := filepath.Base(m.properties.Installed_location)
	installDir := android.PathForModuleInstall(ctx, filepath.Dir(m.properties.Installed_location))
	m.installedPath = ctx.InstallAbsoluteSymlink(installDir, name, m.properties.Symlink_target)
}

func (m *InstallSymlink) AndroidMkEntries() []android.AndroidMkEntries {
	return []android.AndroidMkEntries{{
		Class: "FAKE",
		// Need at least one output file in order for this to take effect.
		OutputFile: android.OptionalPathForPath(m.output),
		Include:    "$(BUILD_PHONY_PACKAGE)",
		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
				entries.AddStrings("LOCAL_SOONG_INSTALL_SYMLINKS", m.installedPath.String())
			},
		},
	}}
}
+135 −0
Original line number Diff line number Diff line
// Copyright 2023 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 etc

import (
	"android/soong/android"
	"strings"
	"testing"
)

var prepareForInstallSymlinkTest = android.GroupFixturePreparers(
	android.PrepareForTestWithArchMutator,
	android.FixtureRegisterWithContext(RegisterInstallSymlinkBuildComponents),
)

func TestInstallSymlinkBasic(t *testing.T) {
	result := prepareForInstallSymlinkTest.RunTestWithBp(t, `
		install_symlink {
			name: "foo",
			installed_location: "bin/foo",
			symlink_target: "/system/system_ext/bin/foo",
		}
	`)

	foo_variants := result.ModuleVariantsForTests("foo")
	if len(foo_variants) != 1 {
		t.Fatalf("expected 1 variant, got %#v", foo_variants)
	}

	foo := result.ModuleForTests("foo", "android_common").Module()
	androidMkEntries := android.AndroidMkEntriesForTest(t, result.TestContext, foo)
	if len(androidMkEntries) != 1 {
		t.Fatalf("expected 1 androidmkentry, got %d", len(androidMkEntries))
	}

	symlinks := androidMkEntries[0].EntryMap["LOCAL_SOONG_INSTALL_SYMLINKS"]
	if len(symlinks) != 1 {
		t.Fatalf("Expected 1 symlink, got %d", len(symlinks))
	}

	if !strings.HasSuffix(symlinks[0], "system/bin/foo") {
		t.Fatalf("Expected symlink install path to end in system/bin/foo, got: %s", symlinks[0])
	}
}

func TestInstallSymlinkToRecovery(t *testing.T) {
	result := prepareForInstallSymlinkTest.RunTestWithBp(t, `
		install_symlink {
			name: "foo",
			installed_location: "bin/foo",
			symlink_target: "/system/system_ext/bin/foo",
			recovery: true,
		}
	`)

	foo_variants := result.ModuleVariantsForTests("foo")
	if len(foo_variants) != 1 {
		t.Fatalf("expected 1 variant, got %#v", foo_variants)
	}

	foo := result.ModuleForTests("foo", "android_common").Module()
	androidMkEntries := android.AndroidMkEntriesForTest(t, result.TestContext, foo)
	if len(androidMkEntries) != 1 {
		t.Fatalf("expected 1 androidmkentry, got %d", len(androidMkEntries))
	}

	symlinks := androidMkEntries[0].EntryMap["LOCAL_SOONG_INSTALL_SYMLINKS"]
	if len(symlinks) != 1 {
		t.Fatalf("Expected 1 symlink, got %d", len(symlinks))
	}

	if !strings.HasSuffix(symlinks[0], "recovery/root/system/bin/foo") {
		t.Fatalf("Expected symlink install path to end in recovery/root/system/bin/foo, got: %s", symlinks[0])
	}
}

func TestErrorOnNonCleanTarget(t *testing.T) {
	prepareForInstallSymlinkTest.
		ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("Should be a clean filepath")).
		RunTestWithBp(t, `
		install_symlink {
			name: "foo",
			installed_location: "bin/foo",
			symlink_target: "/system/system_ext/../bin/foo",
		}
	`)
}

func TestErrorOnNonCleanInstalledLocation(t *testing.T) {
	prepareForInstallSymlinkTest.
		ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("Should be a clean filepath")).
		RunTestWithBp(t, `
		install_symlink {
			name: "foo",
			installed_location: "bin/../foo",
			symlink_target: "/system/system_ext/bin/foo",
		}
	`)
}

func TestErrorOnInstalledPathStartingWithDotDot(t *testing.T) {
	prepareForInstallSymlinkTest.
		ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("Should not start with / or \\.\\./")).
		RunTestWithBp(t, `
		install_symlink {
			name: "foo",
			installed_location: "../bin/foo",
			symlink_target: "/system/system_ext/bin/foo",
		}
	`)
}

func TestErrorOnInstalledPathStartingWithSlash(t *testing.T) {
	prepareForInstallSymlinkTest.
		ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("Should not start with / or \\.\\./")).
		RunTestWithBp(t, `
		install_symlink {
			name: "foo",
			installed_location: "/bin/foo",
			symlink_target: "/system/system_ext/bin/foo",
		}
	`)
}
+1 −1
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ func TestPrebuiltEtcVariants(t *testing.T) {

	baz_variants := result.ModuleVariantsForTests("baz.conf")
	if len(baz_variants) != 1 {
		t.Errorf("expected 1, got %#v", bar_variants)
		t.Errorf("expected 1, got %#v", baz_variants)
	}
}