1// Copyright 2023 Google Inc. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package etc 16 17import ( 18 "android/soong/android" 19 "path/filepath" 20 "strings" 21) 22 23func init() { 24 RegisterInstallSymlinkBuildComponents(android.InitRegistrationContext) 25} 26 27func RegisterInstallSymlinkBuildComponents(ctx android.RegistrationContext) { 28 ctx.RegisterModuleType("install_symlink", InstallSymlinkFactory) 29 ctx.RegisterModuleType("install_symlink_host", InstallSymlinkHostFactory) 30} 31 32// install_symlink can be used to install an symlink with an arbitrary target to an arbitrary path 33// on the device. 34func InstallSymlinkFactory() android.Module { 35 module := &InstallSymlink{} 36 module.AddProperties(&module.properties) 37 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) 38 return module 39} 40 41// install_symlink can be used to install an symlink to an arbitrary path on the host. 42func InstallSymlinkHostFactory() android.Module { 43 module := &InstallSymlink{} 44 module.AddProperties(&module.properties) 45 android.InitAndroidMultiTargetsArchModule(module, android.HostSupported, android.MultilibCommon) 46 return module 47} 48 49type InstallSymlinkProperties struct { 50 // Where to install this symlink, relative to the partition it's installed on. 51 // Which partition it's installed on can be controlled by the vendor, system_ext, ramdisk, etc. 52 // properties. 53 Installed_location string 54 // The target of the symlink, aka where the symlink points. 55 Symlink_target string 56} 57 58type InstallSymlink struct { 59 android.ModuleBase 60 properties InstallSymlinkProperties 61 62 output android.Path 63 installedPath android.InstallPath 64} 65 66func (m *InstallSymlink) GenerateAndroidBuildActions(ctx android.ModuleContext) { 67 if filepath.Clean(m.properties.Symlink_target) != m.properties.Symlink_target { 68 ctx.PropertyErrorf("symlink_target", "Should be a clean filepath") 69 return 70 } 71 if filepath.Clean(m.properties.Installed_location) != m.properties.Installed_location { 72 ctx.PropertyErrorf("installed_location", "Should be a clean filepath") 73 return 74 } 75 if strings.HasPrefix(m.properties.Installed_location, "../") || strings.HasPrefix(m.properties.Installed_location, "/") { 76 ctx.PropertyErrorf("installed_location", "Should not start with / or ../") 77 return 78 } 79 80 out := android.PathForModuleOut(ctx, "out.txt") 81 android.WriteFileRuleVerbatim(ctx, out, "") 82 m.output = out 83 84 name := filepath.Base(m.properties.Installed_location) 85 installDir := android.PathForModuleInstall(ctx, filepath.Dir(m.properties.Installed_location)) 86 m.installedPath = ctx.InstallAbsoluteSymlink(installDir, name, m.properties.Symlink_target) 87} 88 89func (m *InstallSymlink) AndroidMkEntries() []android.AndroidMkEntries { 90 return []android.AndroidMkEntries{{ 91 Class: "FAKE", 92 // Need at least one output file in order for this to take effect. 93 OutputFile: android.OptionalPathForPath(m.output), 94 Include: "$(BUILD_PHONY_PACKAGE)", 95 ExtraEntries: []android.AndroidMkExtraEntriesFunc{ 96 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { 97 entries.AddStrings("LOCAL_SOONG_INSTALL_SYMLINKS", m.installedPath.String()) 98 }, 99 }, 100 }} 101} 102