From 502679e0619ca0266d33ff0665217f97f15d2cdc Mon Sep 17 00:00:00 2001 From: Inseob Kim Date: Mon, 1 Jun 2020 23:23:05 +0900 Subject: [PATCH] Implement cc_object vendor snapshot cc_object modules are also necessary for vendor snapshot. Bug: 157106227 Test: m vendor-snapshot Change-Id: Idf4fd37a26f6f712f3cbab43133622f9f9bd9372 --- cc/androidmk.go | 19 +++++++ cc/cc.go | 30 ++++++---- cc/cc_test.go | 11 ++++ cc/object.go | 4 ++ cc/prebuilt.go | 4 ++ cc/vendor_snapshot.go | 129 ++++++++++++++++++++++++++++++++++++++++-- cc/vndk_prebuilt.go | 4 ++ 7 files changed, 186 insertions(+), 15 deletions(-) diff --git a/cc/androidmk.go b/cc/androidmk.go index 5438b149d..fede601b4 100644 --- a/cc/androidmk.go +++ b/cc/androidmk.go @@ -540,6 +540,25 @@ func (c *vendorSnapshotBinaryDecorator) AndroidMkEntries(ctx AndroidMkContext, e }) } +func (c *vendorSnapshotObjectLinker) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) { + entries.Class = "STATIC_LIBRARIES" + + if c.androidMkVendorSuffix { + entries.SubName = vendorSuffix + } else { + entries.SubName = "" + } + + entries.ExtraFooters = append(entries.ExtraFooters, + func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) { + out := entries.OutputFile.Path() + varname := fmt.Sprintf("SOONG_%sOBJECT_%s%s", prefix, name, entries.SubName) + + fmt.Fprintf(w, "\n%s := %s\n", varname, out.String()) + fmt.Fprintln(w, ".KATI_READONLY: "+varname) + }) +} + func (c *ndkPrebuiltStlLinker) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) { entries.Class = "SHARED_LIBRARIES" } diff --git a/cc/cc.go b/cc/cc.go index 150843cd5..a09b9225e 100644 --- a/cc/cc.go +++ b/cc/cc.go @@ -310,6 +310,7 @@ type ModuleContextIntf interface { staticBinary() bool header() bool binary() bool + object() bool toolchain() config.Toolchain canUseSdk() bool useSdk() bool @@ -1003,14 +1004,8 @@ func (c *Module) nativeCoverage() bool { } func (c *Module) isSnapshotPrebuilt() bool { - if _, ok := c.linker.(*vndkPrebuiltLibraryDecorator); ok { - return true - } - if _, ok := c.linker.(*vendorSnapshotLibraryDecorator); ok { - return true - } - if _, ok := c.linker.(*vendorSnapshotBinaryDecorator); ok { - return true + if p, ok := c.linker.(interface{ isSnapshotPrebuilt() bool }); ok { + return p.isSnapshotPrebuilt() } return false } @@ -1119,6 +1114,10 @@ func (ctx *moduleContextImpl) binary() bool { return ctx.mod.binary() } +func (ctx *moduleContextImpl) object() bool { + return ctx.mod.object() +} + func (ctx *moduleContextImpl) canUseSdk() bool { return ctx.mod.canUseSdk() } @@ -1982,11 +1981,13 @@ func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) { actx.AddVariationDependencies(nil, objDepTag, deps.ObjFiles...) + vendorSnapshotObjects := vendorSnapshotObjects(actx.Config()) + if deps.CrtBegin != "" { - actx.AddVariationDependencies(nil, CrtBeginDepTag, deps.CrtBegin) + actx.AddVariationDependencies(nil, CrtBeginDepTag, rewriteSnapshotLibs(deps.CrtBegin, vendorSnapshotObjects)) } if deps.CrtEnd != "" { - actx.AddVariationDependencies(nil, CrtEndDepTag, deps.CrtEnd) + actx.AddVariationDependencies(nil, CrtEndDepTag, rewriteSnapshotLibs(deps.CrtEnd, vendorSnapshotObjects)) } if deps.LinkerFlagsFile != "" { actx.AddDependency(c, linkerFlagsDepTag, deps.LinkerFlagsFile) @@ -2727,6 +2728,15 @@ func (c *Module) binary() bool { return false } +func (c *Module) object() bool { + if o, ok := c.linker.(interface { + object() bool + }); ok { + return o.object() + } + return false +} + func (c *Module) getMakeLinkType(actx android.ModuleContext) string { if c.UseVndk() { if lib, ok := c.linker.(*llndkStubDecorator); ok { diff --git a/cc/cc_test.go b/cc/cc_test.go index 0a25b5e02..76b4e38e2 100644 --- a/cc/cc_test.go +++ b/cc/cc_test.go @@ -845,6 +845,11 @@ func TestVendorSnapshot(t *testing.T) { vendor_available: true, src: "libb.a", } + + cc_object { + name: "obj", + vendor_available: true, + } ` config := TestConfig(buildDir, android.Android, nil, bp, nil) config.TestProductVariables.DeviceVndkVersion = StringPtr("current") @@ -903,6 +908,12 @@ func TestVendorSnapshot(t *testing.T) { // For header libraries, all vendor:true and vendor_available modules are captured. headerDir := filepath.Join(snapshotVariantPath, archDir, "header") jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json")) + + // For object modules, all vendor:true and vendor_available modules are captured. + objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant) + objectDir := filepath.Join(snapshotVariantPath, archDir, "object") + checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant) + jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json")) } for _, jsonFile := range jsonFiles { diff --git a/cc/object.go b/cc/object.go index 19decec37..15a529e85 100644 --- a/cc/object.go +++ b/cc/object.go @@ -158,3 +158,7 @@ func (object *objectLinker) nativeCoverage() bool { func (object *objectLinker) coverageOutputFilePath() android.OptionalPath { return android.OptionalPath{} } + +func (object *objectLinker) object() bool { + return true +} diff --git a/cc/prebuilt.go b/cc/prebuilt.go index d22838add..b7c0bf2fd 100644 --- a/cc/prebuilt.go +++ b/cc/prebuilt.go @@ -248,6 +248,10 @@ func (p *prebuiltObjectLinker) link(ctx ModuleContext, return nil } +func (p *prebuiltObjectLinker) object() bool { + return true +} + func newPrebuiltObject() *Module { module := newObject() prebuilt := &prebuiltObjectLinker{ diff --git a/cc/vendor_snapshot.go b/cc/vendor_snapshot.go index 13a6b055d..898ac2ab6 100644 --- a/cc/vendor_snapshot.go +++ b/cc/vendor_snapshot.go @@ -30,6 +30,7 @@ const ( vendorSnapshotSharedSuffix = ".vendor_shared." vendorSnapshotStaticSuffix = ".vendor_static." vendorSnapshotBinarySuffix = ".vendor_binary." + vendorSnapshotObjectSuffix = ".vendor_object." ) var ( @@ -39,6 +40,7 @@ var ( vendorSnapshotStaticLibsKey = android.NewOnceKey("vendorSnapshotStaticLibs") vendorSnapshotSharedLibsKey = android.NewOnceKey("vendorSnapshotSharedLibs") vendorSnapshotBinariesKey = android.NewOnceKey("vendorSnapshotBinaries") + vendorSnapshotObjectsKey = android.NewOnceKey("vendorSnapshotObjects") ) // vendor snapshot maps hold names of vendor snapshot modules per arch @@ -72,6 +74,12 @@ func vendorSnapshotBinaries(config android.Config) *snapshotMap { }).(*snapshotMap) } +func vendorSnapshotObjects(config android.Config) *snapshotMap { + return config.Once(vendorSnapshotObjectsKey, func() interface{} { + return newSnapshotMap() + }).(*snapshotMap) +} + type vendorSnapshotLibraryProperties struct { // snapshot version. Version string @@ -185,6 +193,10 @@ func (p *vendorSnapshotLibraryDecorator) nativeCoverage() bool { return false } +func (p *vendorSnapshotLibraryDecorator) isSnapshotPrebuilt() bool { + return true +} + func (p *vendorSnapshotLibraryDecorator) install(ctx ModuleContext, file android.Path) { if p.matchesWithDevice(ctx.DeviceConfig()) && (p.shared() || p.static()) { p.baseInstaller.install(ctx, file) @@ -337,6 +349,10 @@ func (p *vendorSnapshotBinaryDecorator) link(ctx ModuleContext, return outputFile } +func (p *vendorSnapshotBinaryDecorator) isSnapshotPrebuilt() bool { + return true +} + func VendorSnapshotBinaryFactory() android.Module { module, binary := NewBinary(android.DeviceSupported) binary.baseLinker.Properties.No_libcrt = BoolPtr(true) @@ -364,12 +380,98 @@ func VendorSnapshotBinaryFactory() android.Module { return module.Init() } +type vendorSnapshotObjectProperties struct { + // snapshot version. + Version string + + // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64_ab') + Target_arch string + + // Prebuilt file for each arch. + Src *string `android:"arch_variant"` +} + +type vendorSnapshotObjectLinker struct { + objectLinker + properties vendorSnapshotObjectProperties + androidMkVendorSuffix bool +} + +func (p *vendorSnapshotObjectLinker) Name(name string) string { + return name + p.NameSuffix() +} + +func (p *vendorSnapshotObjectLinker) NameSuffix() string { + versionSuffix := p.version() + if p.arch() != "" { + versionSuffix += "." + p.arch() + } + return vendorSnapshotObjectSuffix + versionSuffix +} + +func (p *vendorSnapshotObjectLinker) version() string { + return p.properties.Version +} + +func (p *vendorSnapshotObjectLinker) arch() string { + return p.properties.Target_arch +} + +func (p *vendorSnapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool { + if config.DeviceArch() != p.arch() { + return false + } + if p.properties.Src == nil { + return false + } + return true +} + +func (p *vendorSnapshotObjectLinker) link(ctx ModuleContext, + flags Flags, deps PathDeps, objs Objects) android.Path { + if !p.matchesWithDevice(ctx.DeviceConfig()) { + return nil + } + + m := ctx.Module().(*Module) + p.androidMkVendorSuffix = vendorSuffixModules(ctx.Config())[m.BaseModuleName()] + + return android.PathForModuleSrc(ctx, *p.properties.Src) +} + +func (p *vendorSnapshotObjectLinker) nativeCoverage() bool { + return false +} + +func (p *vendorSnapshotObjectLinker) isSnapshotPrebuilt() bool { + return true +} + +func VendorSnapshotObjectFactory() android.Module { + module := newObject() + + prebuilt := &vendorSnapshotObjectLinker{ + objectLinker: objectLinker{ + baseLinker: NewBaseLinker(nil), + }, + } + module.linker = prebuilt + + android.AddLoadHook(module, func(ctx android.LoadHookContext) { + vendorSnapshotLoadHook(ctx, prebuilt) + }) + + module.AddProperties(&prebuilt.properties) + return module.Init() +} + func init() { android.RegisterSingletonType("vendor-snapshot", VendorSnapshotSingleton) android.RegisterModuleType("vendor_snapshot_shared", VendorSnapshotSharedFactory) android.RegisterModuleType("vendor_snapshot_static", VendorSnapshotStaticFactory) android.RegisterModuleType("vendor_snapshot_header", VendorSnapshotHeaderFactory) android.RegisterModuleType("vendor_snapshot_binary", VendorSnapshotBinaryFactory) + android.RegisterModuleType("vendor_snapshot_object", VendorSnapshotObjectFactory) } func VendorSnapshotSingleton() android.Singleton { @@ -468,8 +570,8 @@ func isVendorSnapshotModule(m *Module, moduleDir string) bool { return true } - // Binaries - if m.binary() { + // Binaries and Objects + if m.binary() || m.object() { return m.outputFile.Valid() && proptools.BoolDefault(m.VendorProperties.Vendor_available, true) } @@ -496,6 +598,8 @@ func (c *vendorSnapshotSingleton) GenerateBuildActions(ctx android.SingletonCont (header only libraries) binary/ (executable binaries) + object/ + (.o object files) arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/ shared/ (.so shared libraries) @@ -505,6 +609,8 @@ func (c *vendorSnapshotSingleton) GenerateBuildActions(ctx android.SingletonCont (header only libraries) binary/ (executable binaries) + object/ + (.o object files) NOTICE_FILES/ (notice files, e.g. libbase.txt) configs/ @@ -630,6 +736,14 @@ func (c *vendorSnapshotSingleton) GenerateBuildActions(ctx android.SingletonCont snapshotBinOut := filepath.Join(snapshotArchDir, targetArch, "binary", binPath.Base()) ret = append(ret, copyFile(ctx, binPath, snapshotBinOut)) propOut = snapshotBinOut + ".json" + } else if m.object() { + // object files aren't installed to the device, so their names can conflict. + // Use module name as stem. + objPath := m.outputFile.Path() + snapshotObjOut := filepath.Join(snapshotArchDir, targetArch, "object", + ctx.ModuleName(m)+filepath.Ext(objPath.Base())) + ret = append(ret, copyFile(ctx, objPath, snapshotObjOut)) + propOut = snapshotObjOut + ".json" } else { ctx.Errorf("unknown module %q in vendor snapshot", m.String()) return nil @@ -719,6 +833,7 @@ type snapshotInterface interface { var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil) var _ snapshotInterface = (*vendorSnapshotLibraryDecorator)(nil) var _ snapshotInterface = (*vendorSnapshotBinaryDecorator)(nil) +var _ snapshotInterface = (*vendorSnapshotObjectLinker)(nil) // gathers all snapshot modules for vendor, and disable unnecessary snapshots // TODO(b/145966707): remove mutator and utilize android.Prebuilt to override source modules @@ -734,12 +849,12 @@ func VendorSnapshotMutator(ctx android.BottomUpMutatorContext) { return } - snapshot, ok := module.linker.(snapshotInterface) - if !ok { + if !module.isSnapshotPrebuilt() { return } - if !snapshot.matchesWithDevice(ctx.DeviceConfig()) { + // isSnapshotPrebuilt ensures snapshotInterface + if !module.linker.(snapshotInterface).matchesWithDevice(ctx.DeviceConfig()) { // Disable unnecessary snapshot module, but do not disable // vndk_prebuilt_shared because they might be packed into vndk APEX if !module.IsVndk() { @@ -761,6 +876,8 @@ func VendorSnapshotMutator(ctx android.BottomUpMutatorContext) { } } else if _, ok := module.linker.(*vendorSnapshotBinaryDecorator); ok { snapshotMap = vendorSnapshotBinaries(ctx.Config()) + } else if _, ok := module.linker.(*vendorSnapshotObjectLinker); ok { + snapshotMap = vendorSnapshotObjects(ctx.Config()) } else { return } @@ -820,6 +937,8 @@ func VendorSnapshotSourceMutator(ctx android.BottomUpMutatorContext) { } } else if module.binary() { snapshotMap = vendorSnapshotBinaries(ctx.Config()) + } else if module.object() { + snapshotMap = vendorSnapshotObjects(ctx.Config()) } else { return } diff --git a/cc/vndk_prebuilt.go b/cc/vndk_prebuilt.go index 53b518186..5a44c4663 100644 --- a/cc/vndk_prebuilt.go +++ b/cc/vndk_prebuilt.go @@ -186,6 +186,10 @@ func (p *vndkPrebuiltLibraryDecorator) nativeCoverage() bool { return false } +func (p *vndkPrebuiltLibraryDecorator) isSnapshotPrebuilt() bool { + return true +} + func (p *vndkPrebuiltLibraryDecorator) install(ctx ModuleContext, file android.Path) { arches := ctx.DeviceConfig().Arches() if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {