Add "Optimize_for_size" build property
Projects with this property will be aggresively optimized for size. This
is useful for large binaries that are rarely executed and performance
insensitive (e.g. debugging tools, deprecated libraries).
This changeset reduces total system native binary size by 1.1%.
Test: presubmit
Bug: 342090838
Change-Id: I46a1db0c44e2e7d482b14eca1299f8ad48661d2d
diff --git a/cc/cc.go b/cc/cc.go
index cb82f86..0db1bd6 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -286,6 +286,11 @@
// Deprecated. true is the default, false is invalid.
Clang *bool `android:"arch_variant"`
+ // Aggresively trade performance for smaller binary size.
+ // This should only be used for on-device binaries that are rarely executed and not
+ // performance critical.
+ Optimize_for_size *bool `android:"arch_variant"`
+
// The API level that this module is built against. The APIs of this API level will be
// visible at build time, but use of any APIs newer than min_sdk_version will render the
// module unloadable on older devices. In the future it will be possible to weakly-link new
@@ -546,6 +551,7 @@
isCfiAssemblySupportEnabled() bool
getSharedFlags() *SharedFlags
notInPlatform() bool
+ optimizeForSize() bool
}
type SharedFlags struct {
@@ -985,6 +991,7 @@
"WinMsgSrcs": hasWinMsg,
"YaccSrsc": hasYacc,
"OnlyCSrcs": !(hasAidl || hasLex || hasProto || hasRenderscript || hasSysprop || hasWinMsg || hasYacc),
+ "OptimizeForSize": c.OptimizeForSize(),
}
}
@@ -1070,6 +1077,10 @@
return false
}
+func (c *Module) OptimizeForSize() bool {
+ return Bool(c.Properties.Optimize_for_size)
+}
+
func (c *Module) SdkVersion() string {
return String(c.Properties.Sdk_version)
}
@@ -1613,6 +1624,10 @@
return ctx.mod.Object()
}
+func (ctx *moduleContextImpl) optimizeForSize() bool {
+ return ctx.mod.OptimizeForSize()
+}
+
func (ctx *moduleContextImpl) canUseSdk() bool {
return ctx.mod.canUseSdk()
}
diff --git a/cc/compiler.go b/cc/compiler.go
index 34d98c0..d13cd95 100644
--- a/cc/compiler.go
+++ b/cc/compiler.go
@@ -693,6 +693,10 @@
flags.Local.CFlags = append(flags.Local.CFlags, "-fopenmp")
}
+ if ctx.optimizeForSize() {
+ flags.Local.CFlags = append(flags.Local.CFlags, "-Oz")
+ }
+
// Exclude directories from manual binder interface allowed list.
//TODO(b/145621474): Move this check into IInterface.h when clang-tidy no longer uses absolute paths.
if android.HasAnyPrefix(ctx.ModuleDir(), allowedManualInterfacePaths) {