Merge changes from topic "remove_suffixes"

* changes:
  Propagate shared library destination path through FilesToZip
  Add -e argument to soong_zip to allow setting an explicit filename
  Support removing suffix for device specific prebuilt during build
This commit is contained in:
Treehugger Robot
2023-05-10 20:30:11 +00:00
committed by Gerrit Code Review
10 changed files with 153 additions and 33 deletions

View File

@@ -1086,7 +1086,7 @@ func (c *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", c.BaseModuleName())) panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", c.BaseModuleName()))
} }
func (c *Module) FuzzSharedLibraries() android.Paths { func (c *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
if fuzzer, ok := c.compiler.(*fuzzBinary); ok { if fuzzer, ok := c.compiler.(*fuzzBinary); ok {
return fuzzer.sharedLibraries return fuzzer.sharedLibraries
} }

View File

@@ -103,7 +103,7 @@ type fuzzBinary struct {
*baseCompiler *baseCompiler
fuzzPackagedModule fuzz.FuzzPackagedModule fuzzPackagedModule fuzz.FuzzPackagedModule
installedSharedDeps []string installedSharedDeps []string
sharedLibraries android.Paths sharedLibraries android.RuleBuilderInstalls
} }
func (fuzz *fuzzBinary) fuzzBinary() bool { func (fuzz *fuzzBinary) fuzzBinary() bool {
@@ -213,19 +213,19 @@ func IsValidSharedDependency(dependency android.Module) bool {
} }
func SharedLibraryInstallLocation( func SharedLibraryInstallLocation(
libraryPath android.Path, isHost bool, fuzzDir string, archString string) string { libraryBase string, isHost bool, fuzzDir string, archString string) string {
installLocation := "$(PRODUCT_OUT)/data" installLocation := "$(PRODUCT_OUT)/data"
if isHost { if isHost {
installLocation = "$(HOST_OUT)" installLocation = "$(HOST_OUT)"
} }
installLocation = filepath.Join( installLocation = filepath.Join(
installLocation, fuzzDir, archString, "lib", libraryPath.Base()) installLocation, fuzzDir, archString, "lib", libraryBase)
return installLocation return installLocation
} }
// Get the device-only shared library symbols install directory. // Get the device-only shared library symbols install directory.
func SharedLibrarySymbolsInstallLocation(libraryPath android.Path, fuzzDir string, archString string) string { func SharedLibrarySymbolsInstallLocation(libraryBase string, fuzzDir string, archString string) string {
return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, "/lib/", libraryPath.Base()) return filepath.Join("$(PRODUCT_OUT)/symbols/data/", fuzzDir, archString, "/lib/", libraryBase)
} }
func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) { func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) {
@@ -242,15 +242,16 @@ func (fuzzBin *fuzzBinary) install(ctx ModuleContext, file android.Path) {
// Grab the list of required shared libraries. // Grab the list of required shared libraries.
fuzzBin.sharedLibraries, _ = CollectAllSharedDependencies(ctx) fuzzBin.sharedLibraries, _ = CollectAllSharedDependencies(ctx)
for _, lib := range fuzzBin.sharedLibraries { for _, ruleBuilderInstall := range fuzzBin.sharedLibraries {
install := ruleBuilderInstall.To
fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps, fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
SharedLibraryInstallLocation( SharedLibraryInstallLocation(
lib, ctx.Host(), installBase, ctx.Arch().ArchType.String())) install, ctx.Host(), installBase, ctx.Arch().ArchType.String()))
// Also add the dependency on the shared library symbols dir. // Also add the dependency on the shared library symbols dir.
if !ctx.Host() { if !ctx.Host() {
fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps, fuzzBin.installedSharedDeps = append(fuzzBin.installedSharedDeps,
SharedLibrarySymbolsInstallLocation(lib, installBase, ctx.Arch().ArchType.String())) SharedLibrarySymbolsInstallLocation(install, installBase, ctx.Arch().ArchType.String()))
} }
} }
} }
@@ -422,7 +423,7 @@ func (s *ccRustFuzzPackager) GenerateBuildActions(ctx android.SingletonContext)
files = append(files, GetSharedLibsToZip(ccModule.FuzzSharedLibraries(), ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...) files = append(files, GetSharedLibsToZip(ccModule.FuzzSharedLibraries(), ccModule, &s.FuzzPackager, archString, sharedLibsInstallDirPrefix, &sharedLibraryInstalled)...)
// The executable. // The executable.
files = append(files, fuzz.FileToZip{android.OutputFileForModule(ctx, ccModule, "unstripped"), ""}) files = append(files, fuzz.FileToZip{SourceFilePath: android.OutputFileForModule(ctx, ccModule, "unstripped")})
archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs) archDirs[archOs], ok = s.BuildZipFile(ctx, module, fpm, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)
if !ok { if !ok {
@@ -453,19 +454,25 @@ func (s *ccRustFuzzPackager) MakeVars(ctx android.MakeVarsContext) {
// GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for // GetSharedLibsToZip finds and marks all the transiently-dependent shared libraries for
// packaging. // packaging.
func GetSharedLibsToZip(sharedLibraries android.Paths, module LinkableInterface, s *fuzz.FuzzPackager, archString string, destinationPathPrefix string, sharedLibraryInstalled *map[string]bool) []fuzz.FileToZip { func GetSharedLibsToZip(sharedLibraries android.RuleBuilderInstalls, module LinkableInterface, s *fuzz.FuzzPackager, archString string, destinationPathPrefix string, sharedLibraryInstalled *map[string]bool) []fuzz.FileToZip {
var files []fuzz.FileToZip var files []fuzz.FileToZip
fuzzDir := "fuzz" fuzzDir := "fuzz"
for _, library := range sharedLibraries { for _, ruleBuilderInstall := range sharedLibraries {
files = append(files, fuzz.FileToZip{library, destinationPathPrefix}) library := ruleBuilderInstall.From
install := ruleBuilderInstall.To
files = append(files, fuzz.FileToZip{
SourceFilePath: library,
DestinationPathPrefix: destinationPathPrefix,
DestinationPath: install,
})
// For each architecture-specific shared library dependency, we need to // For each architecture-specific shared library dependency, we need to
// install it to the output directory. Setup the install destination here, // install it to the output directory. Setup the install destination here,
// which will be used by $(copy-many-files) in the Make backend. // which will be used by $(copy-many-files) in the Make backend.
installDestination := SharedLibraryInstallLocation( installDestination := SharedLibraryInstallLocation(
library, module.Host(), fuzzDir, archString) install, module.Host(), fuzzDir, archString)
if (*sharedLibraryInstalled)[installDestination] { if (*sharedLibraryInstalled)[installDestination] {
continue continue
} }
@@ -483,7 +490,7 @@ func GetSharedLibsToZip(sharedLibraries android.Paths, module LinkableInterface,
// we want symbolization tools (like `stack`) to be able to find the symbols // we want symbolization tools (like `stack`) to be able to find the symbols
// in $ANDROID_PRODUCT_OUT/symbols automagically. // in $ANDROID_PRODUCT_OUT/symbols automagically.
if !module.Host() { if !module.Host() {
symbolsInstallDestination := SharedLibrarySymbolsInstallLocation(library, fuzzDir, archString) symbolsInstallDestination := SharedLibrarySymbolsInstallLocation(install, fuzzDir, archString)
symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$") symbolsInstallDestination = strings.ReplaceAll(symbolsInstallDestination, "$", "$$")
s.SharedLibInstallStrings = append(s.SharedLibInstallStrings, s.SharedLibInstallStrings = append(s.SharedLibInstallStrings,
library.String()+":"+symbolsInstallDestination) library.String()+":"+symbolsInstallDestination)
@@ -497,12 +504,12 @@ func GetSharedLibsToZip(sharedLibraries android.Paths, module LinkableInterface,
// VisitDirectDeps is used first to avoid incorrectly using the core libraries (sanitizer // VisitDirectDeps is used first to avoid incorrectly using the core libraries (sanitizer
// runtimes, libc, libdl, etc.) from a dependency. This may cause issues when dependencies // runtimes, libc, libdl, etc.) from a dependency. This may cause issues when dependencies
// have explicit sanitizer tags, as we may get a dependency on an unsanitized libc, etc. // have explicit sanitizer tags, as we may get a dependency on an unsanitized libc, etc.
func CollectAllSharedDependencies(ctx android.ModuleContext) (android.Paths, []android.Module) { func CollectAllSharedDependencies(ctx android.ModuleContext) (android.RuleBuilderInstalls, []android.Module) {
seen := make(map[string]bool) seen := make(map[string]bool)
recursed := make(map[string]bool) recursed := make(map[string]bool)
deps := []android.Module{} deps := []android.Module{}
var sharedLibraries android.Paths var sharedLibraries android.RuleBuilderInstalls
// Enumerate the first level of dependencies, as we discard all non-library // Enumerate the first level of dependencies, as we discard all non-library
// modules in the BFS loop below. // modules in the BFS loop below.
@@ -510,22 +517,36 @@ func CollectAllSharedDependencies(ctx android.ModuleContext) (android.Paths, []a
if !IsValidSharedDependency(dep) { if !IsValidSharedDependency(dep) {
return return
} }
if !ctx.OtherModuleHasProvider(dep, SharedLibraryInfoProvider) {
return
}
if seen[ctx.OtherModuleName(dep)] { if seen[ctx.OtherModuleName(dep)] {
return return
} }
seen[ctx.OtherModuleName(dep)] = true seen[ctx.OtherModuleName(dep)] = true
deps = append(deps, dep) deps = append(deps, dep)
sharedLibraries = append(sharedLibraries, android.OutputFileForModule(ctx, dep, "unstripped"))
sharedLibraryInfo := ctx.OtherModuleProvider(dep, SharedLibraryInfoProvider).(SharedLibraryInfo)
installDestination := sharedLibraryInfo.SharedLibrary.Base()
ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, dep, "unstripped"), installDestination}
sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
}) })
ctx.WalkDeps(func(child, parent android.Module) bool { ctx.WalkDeps(func(child, parent android.Module) bool {
if !IsValidSharedDependency(child) { if !IsValidSharedDependency(child) {
return false return false
} }
if !ctx.OtherModuleHasProvider(child, SharedLibraryInfoProvider) {
return false
}
if !seen[ctx.OtherModuleName(child)] { if !seen[ctx.OtherModuleName(child)] {
seen[ctx.OtherModuleName(child)] = true seen[ctx.OtherModuleName(child)] = true
deps = append(deps, child) deps = append(deps, child)
sharedLibraries = append(sharedLibraries, android.OutputFileForModule(ctx, child, "unstripped"))
sharedLibraryInfo := ctx.OtherModuleProvider(child, SharedLibraryInfoProvider).(SharedLibraryInfo)
installDestination := sharedLibraryInfo.SharedLibrary.Base()
ruleBuilderInstall := android.RuleBuilderInstall{android.OutputFileForModule(ctx, child, "unstripped"), installDestination}
sharedLibraries = append(sharedLibraries, ruleBuilderInstall)
} }
if recursed[ctx.OtherModuleName(child)] { if recursed[ctx.OtherModuleName(child)] {

View File

@@ -130,7 +130,7 @@ type LinkableInterface interface {
// FuzzSharedLibraries returns the shared library dependencies for this module. // FuzzSharedLibraries returns the shared library dependencies for this module.
// Expects that IsFuzzModule returns true. // Expects that IsFuzzModule returns true.
FuzzSharedLibraries() android.Paths FuzzSharedLibraries() android.RuleBuilderInstalls
Device() bool Device() bool
Host() bool Host() bool

View File

@@ -61,6 +61,7 @@ type FuzzPackager struct {
type FileToZip struct { type FileToZip struct {
SourceFilePath android.Path SourceFilePath android.Path
DestinationPathPrefix string DestinationPathPrefix string
DestinationPath string
} }
type ArchOs struct { type ArchOs struct {
@@ -443,7 +444,7 @@ func (s *FuzzPackager) PackageArtifacts(ctx android.SingletonContext, module and
FlagWithOutput("-o ", corpusZip) FlagWithOutput("-o ", corpusZip)
rspFile := corpusZip.ReplaceExtension(ctx, "rsp") rspFile := corpusZip.ReplaceExtension(ctx, "rsp")
command.FlagWithRspFileInputList("-r ", rspFile, fuzzModule.Corpus) command.FlagWithRspFileInputList("-r ", rspFile, fuzzModule.Corpus)
files = append(files, FileToZip{corpusZip, ""}) files = append(files, FileToZip{SourceFilePath: corpusZip})
} }
// Package the data into a zipfile. // Package the data into a zipfile.
@@ -456,17 +457,17 @@ func (s *FuzzPackager) PackageArtifacts(ctx android.SingletonContext, module and
command.FlagWithArg("-C ", intermediateDir) command.FlagWithArg("-C ", intermediateDir)
command.FlagWithInput("-f ", f) command.FlagWithInput("-f ", f)
} }
files = append(files, FileToZip{dataZip, ""}) files = append(files, FileToZip{SourceFilePath: dataZip})
} }
// The dictionary. // The dictionary.
if fuzzModule.Dictionary != nil { if fuzzModule.Dictionary != nil {
files = append(files, FileToZip{fuzzModule.Dictionary, ""}) files = append(files, FileToZip{SourceFilePath: fuzzModule.Dictionary})
} }
// Additional fuzz config. // Additional fuzz config.
if fuzzModule.Config != nil && IsValidConfig(fuzzModule, module.Name()) { if fuzzModule.Config != nil && IsValidConfig(fuzzModule, module.Name()) {
files = append(files, FileToZip{fuzzModule.Config, ""}) files = append(files, FileToZip{SourceFilePath: fuzzModule.Config})
} }
return files return files
@@ -485,6 +486,9 @@ func (s *FuzzPackager) BuildZipFile(ctx android.SingletonContext, module android
} else { } else {
command.Flag("-P ''") command.Flag("-P ''")
} }
if file.DestinationPath != "" {
command.FlagWithArg("-e ", file.DestinationPath)
}
command.FlagWithInput("-f ", file.SourceFilePath) command.FlagWithInput("-f ", file.SourceFilePath)
} }
@@ -502,7 +506,7 @@ func (s *FuzzPackager) BuildZipFile(ctx android.SingletonContext, module android
} }
s.FuzzTargets[module.Name()] = true s.FuzzTargets[module.Name()] = true
archDirs[archOs] = append(archDirs[archOs], FileToZip{fuzzZip, ""}) archDirs[archOs] = append(archDirs[archOs], FileToZip{SourceFilePath: fuzzZip})
return archDirs[archOs], true return archDirs[archOs], true
} }

View File

@@ -250,11 +250,11 @@ func generateBuildActions(s *fuzz.FuzzPackager, hostOrTargetString string, ctx a
files = s.PackageArtifacts(ctx, module, javaFuzzModule.fuzzPackagedModule, archDir, builder) files = s.PackageArtifacts(ctx, module, javaFuzzModule.fuzzPackagedModule, archDir, builder)
// Add .jar // Add .jar
files = append(files, fuzz.FileToZip{javaFuzzModule.implementationJarFile, ""}) files = append(files, fuzz.FileToZip{SourceFilePath: javaFuzzModule.implementationJarFile})
// Add jni .so files // Add jni .so files
for _, fPath := range javaFuzzModule.jniFilePaths { for _, fPath := range javaFuzzModule.jniFilePaths {
files = append(files, fuzz.FileToZip{fPath, ""}) files = append(files, fuzz.FileToZip{SourceFilePath: fPath})
} }
archDirs[archOs], ok = s.BuildZipFile(ctx, module, javaFuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs) archDirs[archOs], ok = s.BuildZipFile(ctx, module, javaFuzzModule.fuzzPackagedModule, files, builder, archDir, archString, hostOrTargetString, archOs, archDirs)

View File

@@ -31,7 +31,7 @@ type fuzzDecorator struct {
*binaryDecorator *binaryDecorator
fuzzPackagedModule fuzz.FuzzPackagedModule fuzzPackagedModule fuzz.FuzzPackagedModule
sharedLibraries android.Paths sharedLibraries android.RuleBuilderInstalls
installedSharedDeps []string installedSharedDeps []string
} }
@@ -119,15 +119,17 @@ func (fuzz *fuzzDecorator) install(ctx ModuleContext) {
// Grab the list of required shared libraries. // Grab the list of required shared libraries.
fuzz.sharedLibraries, _ = cc.CollectAllSharedDependencies(ctx) fuzz.sharedLibraries, _ = cc.CollectAllSharedDependencies(ctx)
for _, lib := range fuzz.sharedLibraries { for _, ruleBuilderInstall := range fuzz.sharedLibraries {
install := ruleBuilderInstall.To
fuzz.installedSharedDeps = append(fuzz.installedSharedDeps, fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
cc.SharedLibraryInstallLocation( cc.SharedLibraryInstallLocation(
lib, ctx.Host(), installBase, ctx.Arch().ArchType.String())) install, ctx.Host(), installBase, ctx.Arch().ArchType.String()))
// Also add the dependency on the shared library symbols dir. // Also add the dependency on the shared library symbols dir.
if !ctx.Host() { if !ctx.Host() {
fuzz.installedSharedDeps = append(fuzz.installedSharedDeps, fuzz.installedSharedDeps = append(fuzz.installedSharedDeps,
cc.SharedLibrarySymbolsInstallLocation(lib, installBase, ctx.Arch().ArchType.String())) cc.SharedLibrarySymbolsInstallLocation(install, installBase, ctx.Arch().ArchType.String()))
} }
} }
} }

View File

@@ -649,7 +649,7 @@ func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName())) panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
} }
func (mod *Module) FuzzSharedLibraries() android.Paths { func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok { if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
return fuzzer.sharedLibraries return fuzzer.sharedLibraries
} }

View File

@@ -78,6 +78,15 @@ func (rspFiles) Set(s string) error {
return nil return nil
} }
type explicitFile struct{}
func (explicitFile) String() string { return `""` }
func (explicitFile) Set(s string) error {
fileArgsBuilder.ExplicitPathInZip(s)
return nil
}
type dir struct{} type dir struct{}
func (dir) String() string { return `""` } func (dir) String() string { return `""` }
@@ -173,6 +182,7 @@ func main() {
flags.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression") flags.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression")
flags.Var(&relativeRoot{}, "C", "path to use as relative root of files in following -f, -l, or -D arguments") flags.Var(&relativeRoot{}, "C", "path to use as relative root of files in following -f, -l, or -D arguments")
flags.Var(&junkPaths{}, "j", "junk paths, zip files without directory names") flags.Var(&junkPaths{}, "j", "junk paths, zip files without directory names")
flags.Var(&explicitFile{}, "e", "filename to use in the zip file for the next -f argument")
flags.Parse(expandedArgs[1:]) flags.Parse(expandedArgs[1:])

View File

@@ -80,6 +80,7 @@ type pathMapping struct {
type FileArg struct { type FileArg struct {
PathPrefixInZip, SourcePrefixToStrip string PathPrefixInZip, SourcePrefixToStrip string
ExplicitPathInZip string
SourceFiles []string SourceFiles []string
JunkPaths bool JunkPaths bool
GlobDir string GlobDir string
@@ -124,6 +125,10 @@ func (b *FileArgsBuilder) File(name string) *FileArgsBuilder {
arg := b.state arg := b.state
arg.SourceFiles = []string{name} arg.SourceFiles = []string{name}
b.fileArgs = append(b.fileArgs, arg) b.fileArgs = append(b.fileArgs, arg)
if b.state.ExplicitPathInZip != "" {
b.state.ExplicitPathInZip = ""
}
return b return b
} }
@@ -189,6 +194,12 @@ func (b *FileArgsBuilder) RspFile(name string) *FileArgsBuilder {
return b return b
} }
// ExplicitPathInZip sets the path in the zip file for the next File call.
func (b *FileArgsBuilder) ExplicitPathInZip(s string) *FileArgsBuilder {
b.state.ExplicitPathInZip = s
return b
}
func (b *FileArgsBuilder) Error() error { func (b *FileArgsBuilder) Error() error {
if b == nil { if b == nil {
return nil return nil
@@ -425,7 +436,9 @@ func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping,
var dest string var dest string
if fa.JunkPaths { if fa.ExplicitPathInZip != "" {
dest = fa.ExplicitPathInZip
} else if fa.JunkPaths {
dest = filepath.Base(src) dest = filepath.Base(src)
} else { } else {
var err error var err error

View File

@@ -454,6 +454,60 @@ func TestZip(t *testing.T) {
fhWithSHA256("c", fileC, zip.Deflate, sha256FileC), fhWithSHA256("c", fileC, zip.Deflate, sha256FileC),
}, },
}, },
{
name: "explicit path",
args: fileArgsBuilder().
ExplicitPathInZip("foo").
File("a/a/a").
File("a/a/b"),
compressionLevel: 9,
files: []zip.FileHeader{
fh("foo", fileA, zip.Deflate),
fh("a/a/b", fileB, zip.Deflate),
},
},
{
name: "explicit path with prefix",
args: fileArgsBuilder().
PathPrefixInZip("prefix").
ExplicitPathInZip("foo").
File("a/a/a").
File("a/a/b"),
compressionLevel: 9,
files: []zip.FileHeader{
fh("prefix/foo", fileA, zip.Deflate),
fh("prefix/a/a/b", fileB, zip.Deflate),
},
},
{
name: "explicit path with glob",
args: fileArgsBuilder().
ExplicitPathInZip("foo").
File("a/a/a*").
File("a/a/b"),
compressionLevel: 9,
files: []zip.FileHeader{
fh("foo", fileA, zip.Deflate),
fh("a/a/b", fileB, zip.Deflate),
},
},
{
name: "explicit path with junk paths",
args: fileArgsBuilder().
JunkPaths(true).
ExplicitPathInZip("foo/bar").
File("a/a/a*").
File("a/a/b"),
compressionLevel: 9,
files: []zip.FileHeader{
fh("foo/bar", fileA, zip.Deflate),
fh("b", fileB, zip.Deflate),
},
},
// errors // errors
{ {
@@ -490,6 +544,22 @@ func TestZip(t *testing.T) {
File("d/a/a"), File("d/a/a"),
err: ConflictingFileError{}, err: ConflictingFileError{},
}, },
{
name: "error explicit path conflicting",
args: fileArgsBuilder().
ExplicitPathInZip("foo").
File("a/a/a").
ExplicitPathInZip("foo").
File("a/a/b"),
err: ConflictingFileError{},
},
{
name: "error explicit path conflicting glob",
args: fileArgsBuilder().
ExplicitPathInZip("foo").
File("a/a/*"),
err: ConflictingFileError{},
},
} }
for _, test := range testCases { for _, test := range testCases {