Merge "Add synopsis for prebuilt_apis."
diff --git a/Android.bp b/Android.bp
index 84ac82f..7465341 100644
--- a/Android.bp
+++ b/Android.bp
@@ -55,6 +55,7 @@
"android/namespace.go",
"android/neverallow.go",
"android/onceper.go",
+ "android/override_module.go",
"android/package_ctx.go",
"android/path_properties.go",
"android/paths.go",
diff --git a/android/mutator.go b/android/mutator.go
index 6c9f17a..71237a1 100644
--- a/android/mutator.go
+++ b/android/mutator.go
@@ -79,6 +79,7 @@
RegisterNamespaceMutator,
RegisterPrebuiltsPreArchMutators,
RegisterDefaultsPreArchMutators,
+ RegisterOverridePreArchMutators,
}
func registerArchMutator(ctx RegisterMutatorsContext) {
diff --git a/android/override_module.go b/android/override_module.go
new file mode 100644
index 0000000..02db359
--- /dev/null
+++ b/android/override_module.go
@@ -0,0 +1,213 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// 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 android
+
+// This file contains all the foundation components for override modules and their base module
+// types. Override modules are a kind of opposite of default modules in that they override certain
+// properties of an existing base module whereas default modules provide base module data to be
+// overridden. However, unlike default and defaultable module pairs, both override and overridable
+// modules generate and output build actions, and it is up to product make vars to decide which one
+// to actually build and install in the end. In other words, default modules and defaultable modules
+// can be compared to abstract classes and concrete classes in C++ and Java. By the same analogy,
+// both override and overridable modules act like concrete classes.
+//
+// There is one more crucial difference from the logic perspective. Unlike default pairs, most Soong
+// actions happen in the base (overridable) module by creating a local variant for each override
+// module based on it.
+
+import (
+ "sync"
+
+ "github.com/google/blueprint"
+ "github.com/google/blueprint/proptools"
+)
+
+// Interface for override module types, e.g. override_android_app, override_apex
+type OverrideModule interface {
+ Module
+
+ getOverridingProperties() []interface{}
+ setOverridingProperties(properties []interface{})
+
+ getOverrideModuleProperties() *OverrideModuleProperties
+}
+
+// Base module struct for override module types
+type OverrideModuleBase struct {
+ moduleProperties OverrideModuleProperties
+
+ overridingProperties []interface{}
+}
+
+type OverrideModuleProperties struct {
+ // Name of the base module to be overridden
+ Base *string
+
+ // TODO(jungjw): Add an optional override_name bool flag.
+}
+
+func (o *OverrideModuleBase) getOverridingProperties() []interface{} {
+ return o.overridingProperties
+}
+
+func (o *OverrideModuleBase) setOverridingProperties(properties []interface{}) {
+ o.overridingProperties = properties
+}
+
+func (o *OverrideModuleBase) getOverrideModuleProperties() *OverrideModuleProperties {
+ return &o.moduleProperties
+}
+
+func InitOverrideModule(m OverrideModule) {
+ m.setOverridingProperties(m.GetProperties())
+
+ m.AddProperties(m.getOverrideModuleProperties())
+}
+
+// Interface for overridable module types, e.g. android_app, apex
+type OverridableModule interface {
+ setOverridableProperties(prop []interface{})
+
+ addOverride(o OverrideModule)
+ getOverrides() []OverrideModule
+
+ override(ctx BaseModuleContext, o OverrideModule)
+
+ setOverridesProperty(overridesProperties *[]string)
+}
+
+// Base module struct for overridable module types
+type OverridableModuleBase struct {
+ ModuleBase
+
+ // List of OverrideModules that override this base module
+ overrides []OverrideModule
+ // Used to parallelize registerOverrideMutator executions. Note that only addOverride locks this
+ // mutex. It is because addOverride and getOverride are used in different mutators, and so are
+ // guaranteed to be not mixed. (And, getOverride only reads from overrides, and so don't require
+ // mutex locking.)
+ overridesLock sync.Mutex
+
+ overridableProperties []interface{}
+
+ // If an overridable module has a property to list other modules that itself overrides, it should
+ // set this to a pointer to the property through the InitOverridableModule function, so that
+ // override information is propagated and aggregated correctly.
+ overridesProperty *[]string
+}
+
+func InitOverridableModule(m OverridableModule, overridesProperty *[]string) {
+ m.setOverridableProperties(m.(Module).GetProperties())
+ m.setOverridesProperty(overridesProperty)
+}
+
+func (b *OverridableModuleBase) setOverridableProperties(prop []interface{}) {
+ b.overridableProperties = prop
+}
+
+func (b *OverridableModuleBase) addOverride(o OverrideModule) {
+ b.overridesLock.Lock()
+ b.overrides = append(b.overrides, o)
+ b.overridesLock.Unlock()
+}
+
+// Should NOT be used in the same mutator as addOverride.
+func (b *OverridableModuleBase) getOverrides() []OverrideModule {
+ return b.overrides
+}
+
+func (b *OverridableModuleBase) setOverridesProperty(overridesProperty *[]string) {
+ b.overridesProperty = overridesProperty
+}
+
+// Overrides a base module with the given OverrideModule.
+func (b *OverridableModuleBase) override(ctx BaseModuleContext, o OverrideModule) {
+ for _, p := range b.overridableProperties {
+ for _, op := range o.getOverridingProperties() {
+ if proptools.TypeEqual(p, op) {
+ err := proptools.PrependProperties(p, op, nil)
+ if err != nil {
+ if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
+ ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
+ } else {
+ panic(err)
+ }
+ }
+ }
+ }
+ }
+ // Adds the base module to the overrides property, if exists, of the overriding module. See the
+ // comment on OverridableModuleBase.overridesProperty for details.
+ if b.overridesProperty != nil {
+ *b.overridesProperty = append(*b.overridesProperty, b.Name())
+ }
+ // The base module name property has to be updated separately for Name() to work as intended.
+ b.module.base().nameProperties.Name = proptools.StringPtr(o.Name())
+}
+
+// Mutators for override/overridable modules. All the fun happens in these functions. It is critical
+// to keep them in this order and not put any order mutators between them.
+func RegisterOverridePreArchMutators(ctx RegisterMutatorsContext) {
+ ctx.BottomUp("override_deps", overrideModuleDepsMutator).Parallel()
+ ctx.TopDown("register_override", registerOverrideMutator).Parallel()
+ ctx.BottomUp("perform_override", performOverrideMutator).Parallel()
+}
+
+type overrideBaseDependencyTag struct {
+ blueprint.BaseDependencyTag
+}
+
+var overrideBaseDepTag overrideBaseDependencyTag
+
+// Adds dependency on the base module to the overriding module so that they can be visited in the
+// next phase.
+func overrideModuleDepsMutator(ctx BottomUpMutatorContext) {
+ if module, ok := ctx.Module().(OverrideModule); ok {
+ ctx.AddDependency(ctx.Module(), overrideBaseDepTag, *module.getOverrideModuleProperties().Base)
+ }
+}
+
+// Visits the base module added as a dependency above, checks the module type, and registers the
+// overriding module.
+func registerOverrideMutator(ctx TopDownMutatorContext) {
+ ctx.VisitDirectDepsWithTag(overrideBaseDepTag, func(base Module) {
+ if o, ok := base.(OverridableModule); ok {
+ o.addOverride(ctx.Module().(OverrideModule))
+ } else {
+ ctx.PropertyErrorf("base", "unsupported base module type")
+ }
+ })
+}
+
+// Now, goes through all overridable modules, finds all modules overriding them, creates a local
+// variant for each of them, and performs the actual overriding operation by calling override().
+func performOverrideMutator(ctx BottomUpMutatorContext) {
+ if b, ok := ctx.Module().(OverridableModule); ok {
+ overrides := b.getOverrides()
+ if len(overrides) == 0 {
+ return
+ }
+ variants := make([]string, len(overrides)+1)
+ // The first variant is for the original, non-overridden, base module.
+ variants[0] = ""
+ for i, o := range overrides {
+ variants[i+1] = o.(Module).Name()
+ }
+ mods := ctx.CreateLocalVariations(variants...)
+ for i, o := range overrides {
+ mods[i+1].(OverridableModule).override(ctx, o)
+ }
+ }
+}
diff --git a/android/sh_binary.go b/android/sh_binary.go
index bee61ef..cf415c5 100644
--- a/android/sh_binary.go
+++ b/android/sh_binary.go
@@ -139,6 +139,7 @@
func (s *ShTest) AndroidMk() AndroidMkData {
data := s.ShBinary.AndroidMk()
+ data.Class = "NATIVE_TESTS"
data.Extra = append(data.Extra, func(w io.Writer, outputFile Path) {
fmt.Fprintln(w, "LOCAL_COMPATIBILITY_SUITE :=",
strings.Join(s.testProperties.Test_suites, " "))
diff --git a/apex/apex_test.go b/apex/apex_test.go
index ac2701f..8a2e55a 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -161,6 +161,8 @@
"vendor/foo/devkeys/testkey.pem": nil,
"NOTICE": nil,
"custom_notice": nil,
+ "testkey2.avbpubkey": nil,
+ "testkey2.pem": nil,
})
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
android.FailIfErrored(t, errs)
@@ -1194,3 +1196,36 @@
}
}
+
+func TestApexKeyFromOtherModule(t *testing.T) {
+ ctx := testApex(t, `
+ apex_key {
+ name: "myapex.key",
+ public_key: ":my.avbpubkey",
+ private_key: ":my.pem",
+ product_specific: true,
+ }
+
+ filegroup {
+ name: "my.avbpubkey",
+ srcs: ["testkey2.avbpubkey"],
+ }
+
+ filegroup {
+ name: "my.pem",
+ srcs: ["testkey2.pem"],
+ }
+ `)
+
+ apex_key := ctx.ModuleForTests("myapex.key", "android_common").Module().(*apexKey)
+ expected_pubkey := "testkey2.avbpubkey"
+ actual_pubkey := apex_key.public_key_file.String()
+ if actual_pubkey != expected_pubkey {
+ t.Errorf("wrong public key path. expected %q. actual %q", expected_pubkey, actual_pubkey)
+ }
+ expected_privkey := "testkey2.pem"
+ actual_privkey := apex_key.private_key_file.String()
+ if actual_privkey != expected_privkey {
+ t.Errorf("wrong private key path. expected %q. actual %q", expected_privkey, actual_privkey)
+ }
+}
diff --git a/apex/key.go b/apex/key.go
index 07c3105..848e8ce 100644
--- a/apex/key.go
+++ b/apex/key.go
@@ -45,11 +45,11 @@
}
type apexKeyProperties struct {
- // Path to the public key file in avbpubkey format. Installed to the device.
+ // Path or module to the public key file in avbpubkey format. Installed to the device.
// Base name of the file is used as the ID for the key.
- Public_key *string
- // Path to the private key file in pem format. Used to sign APEXs.
- Private_key *string
+ Public_key *string `android:"path"`
+ // Path or module to the private key file in pem format. Used to sign APEXs.
+ Private_key *string `android:"path"`
// Whether this key is installable to one of the partitions. Defualt: true.
Installable *bool
@@ -68,15 +68,26 @@
}
func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
- m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
- m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
-
- // If not found, fall back to the local key pairs
- if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() {
+ // If the keys are from other modules (i.e. :module syntax) respect it.
+ // Otherwise, try to locate the key files in the default cert dir or
+ // in the local module dir
+ if android.SrcIsModule(String(m.properties.Public_key)) != "" {
m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
+ } else {
+ m.public_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
+ // If not found, fall back to the local key pairs
+ if !android.ExistentPathForSource(ctx, m.public_key_file.String()).Valid() {
+ m.public_key_file = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
+ }
}
- if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() {
+
+ if android.SrcIsModule(String(m.properties.Private_key)) != "" {
m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
+ } else {
+ m.private_key_file = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
+ if !android.ExistentPathForSource(ctx, m.private_key_file.String()).Valid() {
+ m.private_key_file = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
+ }
}
pubKeyName := m.public_key_file.Base()[0 : len(m.public_key_file.Base())-len(m.public_key_file.Ext())]
diff --git a/cc/sanitize.go b/cc/sanitize.go
index 26418ad..2d80c22 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -39,7 +39,8 @@
// the default.
hwasanCflags = []string{"-fno-omit-frame-pointer", "-Wno-frame-larger-than=",
"-mllvm", "-hwasan-create-frame-descriptions=0",
- "-mllvm", "-hwasan-allow-ifunc"}
+ "-mllvm", "-hwasan-allow-ifunc",
+ "-fsanitize-hwaddress-abi=platform"}
cfiCflags = []string{"-flto", "-fsanitize-cfi-cross-dso",
"-fsanitize-blacklist=external/compiler-rt/lib/cfi/cfi_blacklist.txt"}
diff --git a/java/app.go b/java/app.go
index 8f5d010..b31f232 100644
--- a/java/app.go
+++ b/java/app.go
@@ -33,16 +33,13 @@
android.RegisterModuleType("android_test", AndroidTestFactory)
android.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
android.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
+ android.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
}
// AndroidManifest.xml merging
// package splits
type appProperties struct {
- // The name of a certificate in the default certificate directory, blank to use the default product certificate,
- // or an android_app_certificate module name in the form ":module".
- Certificate *string
-
// Names of extra android_app_certificate modules to sign the apk with in the form ":module".
Additional_certificates []string
@@ -79,14 +76,24 @@
Use_embedded_dex *bool
}
+// android_app properties that can be overridden by override_android_app
+type overridableAppProperties struct {
+ // The name of a certificate in the default certificate directory, blank to use the default product certificate,
+ // or an android_app_certificate module name in the form ":module".
+ Certificate *string
+}
+
type AndroidApp struct {
Library
aapt
+ android.OverridableModuleBase
certificate Certificate
appProperties appProperties
+ overridableAppProperties overridableAppProperties
+
installJniLibs []jniLib
bundleFile android.Path
@@ -320,7 +327,7 @@
func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
// Check if the install APK name needs to be overridden.
- a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(ctx.ModuleName())
+ a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
// Process all building blocks, from AAPT to certificates.
a.aaptBuildActions(ctx)
@@ -339,6 +346,7 @@
certificates := a.certificateBuildActions(certificateDeps, ctx)
// Build a final signed app package.
+ // TODO(jungjw): Consider changing this to installApkName.
packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk")
CreateAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates)
a.outputFile = packageFile
@@ -413,7 +421,7 @@
if overridden {
return ":" + certificate
}
- return String(a.appProperties.Certificate)
+ return String(a.overridableAppProperties.Certificate)
}
// android_app compiles sources and Android resources into an Android application package `.apk` file.
@@ -432,7 +440,8 @@
&module.Module.dexpreoptProperties,
&module.Module.protoProperties,
&module.aaptProperties,
- &module.appProperties)
+ &module.appProperties,
+ &module.overridableAppProperties)
module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
return class == android.Device && ctx.Config().DevicePrefer32BitApps()
@@ -440,6 +449,7 @@
android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
android.InitDefaultableModule(module)
+ android.InitOverridableModule(module, &module.appProperties.Overrides)
return module
}
@@ -503,6 +513,7 @@
&module.aaptProperties,
&module.appProperties,
&module.appTestProperties,
+ &module.overridableAppProperties,
&module.testProperties)
android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
@@ -541,7 +552,8 @@
&module.Module.protoProperties,
&module.aaptProperties,
&module.appProperties,
- &module.appTestHelperAppProperties)
+ &module.appTestHelperAppProperties,
+ &module.overridableAppProperties)
android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
android.InitDefaultableModule(module)
@@ -575,3 +587,24 @@
android.PathForModuleSrc(ctx, cert+".pk8"),
}
}
+
+type OverrideAndroidApp struct {
+ android.ModuleBase
+ android.OverrideModuleBase
+}
+
+func (i *OverrideAndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+ // All the overrides happen in the base module.
+ // TODO(jungjw): Check the base module type.
+}
+
+// override_android_app is used to create an android_app module based on another android_app by overriding
+// some of its properties.
+func OverrideAndroidAppModuleFactory() android.Module {
+ m := &OverrideAndroidApp{}
+ m.AddProperties(&overridableAppProperties{})
+
+ android.InitAndroidModule(m)
+ android.InitOverrideModule(m)
+ return m
+}
diff --git a/java/app_test.go b/java/app_test.go
index 1bad123..cf57c80 100644
--- a/java/app_test.go
+++ b/java/app_test.go
@@ -827,3 +827,75 @@
t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
}
}
+
+func TestOverrideAndroidApp(t *testing.T) {
+ ctx := testJava(t, `
+ android_app {
+ name: "foo",
+ srcs: ["a.java"],
+ overrides: ["baz"],
+ }
+
+ override_android_app {
+ name: "bar",
+ base: "foo",
+ certificate: ":new_certificate",
+ }
+
+ android_app_certificate {
+ name: "new_certificate",
+ certificate: "cert/new_cert",
+ }
+ `)
+
+ expectedVariants := []struct {
+ variantName string
+ apkName string
+ apkPath string
+ signFlag string
+ overrides []string
+ }{
+ {
+ variantName: "android_common",
+ apkPath: "/target/product/test_device/system/app/foo/foo.apk",
+ signFlag: "build/target/product/security/testkey.x509.pem build/target/product/security/testkey.pk8",
+ overrides: []string{"baz"},
+ },
+ {
+ variantName: "bar_android_common",
+ apkPath: "/target/product/test_device/system/app/bar/bar.apk",
+ signFlag: "cert/new_cert.x509.pem cert/new_cert.pk8",
+ overrides: []string{"baz", "foo"},
+ },
+ }
+ for _, expected := range expectedVariants {
+ variant := ctx.ModuleForTests("foo", expected.variantName)
+
+ // Check the final apk name
+ outputs := variant.AllOutputs()
+ expectedApkPath := buildDir + expected.apkPath
+ found := false
+ for _, o := range outputs {
+ if o == expectedApkPath {
+ found = true
+ break
+ }
+ }
+ if !found {
+ t.Errorf("Can't find %q in output files.\nAll outputs:%v", expectedApkPath, outputs)
+ }
+
+ // Check the certificate paths
+ signapk := variant.Output("foo.apk")
+ signFlag := signapk.Args["certificates"]
+ if expected.signFlag != signFlag {
+ t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected.signFlag, signFlag)
+ }
+
+ mod := variant.Module().(*AndroidApp)
+ if !reflect.DeepEqual(expected.overrides, mod.appProperties.Overrides) {
+ t.Errorf("Incorrect overrides property value, expected: %q, got: %q",
+ expected.overrides, mod.appProperties.Overrides)
+ }
+ }
+}
diff --git a/java/java.go b/java/java.go
index e0da006..3eae932 100644
--- a/java/java.go
+++ b/java/java.go
@@ -2068,6 +2068,7 @@
&androidLibraryProperties{},
&appProperties{},
&appTestProperties{},
+ &overridableAppProperties{},
&ImportProperties{},
&AARImportProperties{},
&sdkLibraryProperties{},
diff --git a/java/java_test.go b/java/java_test.go
index 35dd696..ec6d27a 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -86,10 +86,12 @@
ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory))
ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(SdkLibraryFactory))
+ ctx.RegisterModuleType("override_android_app", android.ModuleFactoryAdaptor(OverrideAndroidAppModuleFactory))
ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(PrebuiltApisFactory))
ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
+ ctx.PreArchMutators(android.RegisterOverridePreArchMutators)
ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
ctx.TopDown("java_sdk_library", SdkLibraryMutator).Parallel()
diff --git a/java/sdk_library.go b/java/sdk_library.go
index 18866d5..72cce57 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -668,11 +668,11 @@
}
func (module *SdkLibrary) createInternalModules(mctx android.TopDownMutatorContext) {
- if module.Library.Module.properties.Srcs == nil {
+ if len(module.Library.Module.properties.Srcs) == 0 {
mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
}
- if module.sdkLibraryProperties.Api_packages == nil {
+ if len(module.sdkLibraryProperties.Api_packages) == 0 {
mctx.PropertyErrorf("api_packages", "java_sdk_library must specify api_packages")
}
diff --git a/sysprop/sysprop_library.go b/sysprop/sysprop_library.go
index 6e8e306..48078d8 100644
--- a/sysprop/sysprop_library.go
+++ b/sysprop/sysprop_library.go
@@ -82,7 +82,11 @@
}
func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
- if m.syspropLibraryProperties.Api_packages == nil {
+ if len(m.commonProperties.Srcs) == 0 {
+ ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
+ }
+
+ if len(m.syspropLibraryProperties.Api_packages) == 0 {
ctx.PropertyErrorf("api_packages", "sysprop_library must specify api_packages")
}
diff --git a/ui/build/ninja.go b/ui/build/ninja.go
index cb41579..7994f3a 100644
--- a/ui/build/ninja.go
+++ b/ui/build/ninja.go
@@ -31,7 +31,8 @@
defer ctx.EndTrace()
fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
- status.NinjaReader(ctx, ctx.Status.StartTool(), fifo)
+ nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo)
+ defer nr.Close()
executable := config.PrebuiltBuildTool("ninja")
args := []string{
diff --git a/ui/build/soong.go b/ui/build/soong.go
index c89f0d5..2ce1ac9 100644
--- a/ui/build/soong.go
+++ b/ui/build/soong.go
@@ -109,7 +109,8 @@
defer ctx.EndTrace()
fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
- status.NinjaReader(ctx, ctx.Status.StartTool(), fifo)
+ nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo)
+ defer nr.Close()
cmd := Command(ctx, config, "soong "+name,
config.PrebuiltBuildTool("ninja"),
diff --git a/ui/logger/logger.go b/ui/logger/logger.go
index 58890e9..9b26ae8 100644
--- a/ui/logger/logger.go
+++ b/ui/logger/logger.go
@@ -180,12 +180,16 @@
return s
}
+type panicWriter struct{}
+
+func (panicWriter) Write([]byte) (int, error) { panic("write to panicWriter") }
+
// Close disables logging to the file and closes the file handle.
func (s *stdLogger) Close() {
s.mutex.Lock()
defer s.mutex.Unlock()
if s.file != nil {
- s.fileLogger.SetOutput(ioutil.Discard)
+ s.fileLogger.SetOutput(panicWriter{})
s.file.Close()
s.file = nil
}
diff --git a/ui/status/Android.bp b/ui/status/Android.bp
index 76caaef..901a713 100644
--- a/ui/status/Android.bp
+++ b/ui/status/Android.bp
@@ -28,6 +28,7 @@
],
testSrcs: [
"kati_test.go",
+ "ninja_test.go",
"status_test.go",
],
}
diff --git a/ui/status/ninja.go b/ui/status/ninja.go
index 4ceb5ef..ee2a2da 100644
--- a/ui/status/ninja.go
+++ b/ui/status/ninja.go
@@ -20,6 +20,7 @@
"io"
"os"
"syscall"
+ "time"
"github.com/golang/protobuf/proto"
@@ -27,9 +28,9 @@
"android/soong/ui/status/ninja_frontend"
)
-// NinjaReader reads the protobuf frontend format from ninja and translates it
+// NewNinjaReader reads the protobuf frontend format from ninja and translates it
// into calls on the ToolStatus API.
-func NinjaReader(ctx logger.Logger, status ToolStatus, fifo string) {
+func NewNinjaReader(ctx logger.Logger, status ToolStatus, fifo string) *NinjaReader {
os.Remove(fifo)
err := syscall.Mkfifo(fifo, 0666)
@@ -37,14 +38,69 @@
ctx.Fatalf("Failed to mkfifo(%q): %v", fifo, err)
}
- go ninjaReader(status, fifo)
+ n := &NinjaReader{
+ status: status,
+ fifo: fifo,
+ done: make(chan bool),
+ cancel: make(chan bool),
+ }
+
+ go n.run()
+
+ return n
}
-func ninjaReader(status ToolStatus, fifo string) {
- f, err := os.Open(fifo)
- if err != nil {
- status.Error(fmt.Sprintf("Failed to open fifo: %v", err))
+type NinjaReader struct {
+ status ToolStatus
+ fifo string
+ done chan bool
+ cancel chan bool
+}
+
+const NINJA_READER_CLOSE_TIMEOUT = 5 * time.Second
+
+// Close waits for NinjaReader to finish reading from the fifo, or 5 seconds.
+func (n *NinjaReader) Close() {
+ // Signal the goroutine to stop if it is blocking opening the fifo.
+ close(n.cancel)
+
+ timeoutCh := time.After(NINJA_READER_CLOSE_TIMEOUT)
+
+ select {
+ case <-n.done:
+ // Nothing
+ case <-timeoutCh:
+ n.status.Error(fmt.Sprintf("ninja fifo didn't finish after %s", NINJA_READER_CLOSE_TIMEOUT.String()))
}
+
+ return
+}
+
+func (n *NinjaReader) run() {
+ defer close(n.done)
+
+ // Opening the fifo can block forever if ninja never opens the write end, do it in a goroutine so this
+ // method can exit on cancel.
+ fileCh := make(chan *os.File)
+ go func() {
+ f, err := os.Open(n.fifo)
+ if err != nil {
+ n.status.Error(fmt.Sprintf("Failed to open fifo: %v", err))
+ close(fileCh)
+ return
+ }
+ fileCh <- f
+ }()
+
+ var f *os.File
+
+ select {
+ case f = <-fileCh:
+ // Nothing
+ case <-n.cancel:
+ return
+ }
+
defer f.Close()
r := bufio.NewReader(f)
@@ -55,7 +111,7 @@
size, err := readVarInt(r)
if err != nil {
if err != io.EOF {
- status.Error(fmt.Sprintf("Got error reading from ninja: %s", err))
+ n.status.Error(fmt.Sprintf("Got error reading from ninja: %s", err))
}
return
}
@@ -64,9 +120,9 @@
_, err = io.ReadFull(r, buf)
if err != nil {
if err == io.EOF {
- status.Print(fmt.Sprintf("Missing message of size %d from ninja\n", size))
+ n.status.Print(fmt.Sprintf("Missing message of size %d from ninja\n", size))
} else {
- status.Error(fmt.Sprintf("Got error reading from ninja: %s", err))
+ n.status.Error(fmt.Sprintf("Got error reading from ninja: %s", err))
}
return
}
@@ -74,13 +130,13 @@
msg := &ninja_frontend.Status{}
err = proto.Unmarshal(buf, msg)
if err != nil {
- status.Print(fmt.Sprintf("Error reading message from ninja: %v", err))
+ n.status.Print(fmt.Sprintf("Error reading message from ninja: %v", err))
continue
}
// Ignore msg.BuildStarted
if msg.TotalEdges != nil {
- status.SetTotalActions(int(msg.TotalEdges.GetTotalEdges()))
+ n.status.SetTotalActions(int(msg.TotalEdges.GetTotalEdges()))
}
if msg.EdgeStarted != nil {
action := &Action{
@@ -88,7 +144,7 @@
Outputs: msg.EdgeStarted.Outputs,
Command: msg.EdgeStarted.GetCommand(),
}
- status.StartAction(action)
+ n.status.StartAction(action)
running[msg.EdgeStarted.GetId()] = action
}
if msg.EdgeFinished != nil {
@@ -101,7 +157,7 @@
err = fmt.Errorf("exited with code: %d", exitCode)
}
- status.FinishAction(ActionResult{
+ n.status.FinishAction(ActionResult{
Action: started,
Output: msg.EdgeFinished.GetOutput(),
Error: err,
@@ -112,17 +168,17 @@
message := "ninja: " + msg.Message.GetMessage()
switch msg.Message.GetLevel() {
case ninja_frontend.Status_Message_INFO:
- status.Status(message)
+ n.status.Status(message)
case ninja_frontend.Status_Message_WARNING:
- status.Print("warning: " + message)
+ n.status.Print("warning: " + message)
case ninja_frontend.Status_Message_ERROR:
- status.Error(message)
+ n.status.Error(message)
default:
- status.Print(message)
+ n.status.Print(message)
}
}
if msg.BuildFinished != nil {
- status.Finish()
+ n.status.Finish()
}
}
}
diff --git a/ui/status/ninja_test.go b/ui/status/ninja_test.go
new file mode 100644
index 0000000..c400c97
--- /dev/null
+++ b/ui/status/ninja_test.go
@@ -0,0 +1,45 @@
+// Copyright 2019 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 status
+
+import (
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+
+ "android/soong/ui/logger"
+)
+
+// Tests that closing the ninja reader when nothing has opened the other end of the fifo is fast.
+func TestNinjaReader_Close(t *testing.T) {
+ tempDir, err := ioutil.TempDir("", "ninja_test")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(tempDir)
+
+ stat := &Status{}
+ nr := NewNinjaReader(logger.New(ioutil.Discard), stat.StartTool(), filepath.Join(tempDir, "fifo"))
+
+ start := time.Now()
+
+ nr.Close()
+
+ if g, w := time.Since(start), NINJA_READER_CLOSE_TIMEOUT; g >= w {
+ t.Errorf("nr.Close timed out, %s > %s", g, w)
+ }
+}