Skip to content

Commit

Permalink
Adding a no-debug target into the tv apps (#25208)
Browse files Browse the repository at this point in the history
The no-debug target significantly reduces the size of the binaries by
stripping out the symbols from the `.so` files and also ensuring the
is_debug flag is set to false.
  • Loading branch information
cliffamzn authored and pull[bot] committed Oct 23, 2023
1 parent 3200284 commit bb9dbbf
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
3 changes: 3 additions & 0 deletions examples/tv-casting-app/android/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ the top Matter directory:
./scripts/build/build_examples.py --target android-arm64-tv-casting-app build
```

(To build this app with no debugging hooks, use the
`android-arm64-tv-casting-app-no-debug` target)

See the table above for other values of `TARGET_CPU`.

The debug Android package `app-debug.apk` will be generated at
Expand Down
5 changes: 4 additions & 1 deletion scripts/build/build/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from typing import Any, List, Optional

from builders.ameba import AmebaApp, AmebaBoard, AmebaBuilder
from builders.android import AndroidApp, AndroidBoard, AndroidBuilder
from builders.android import AndroidApp, AndroidBoard, AndroidBuilder, AndroidProfile
from builders.bouffalolab import BouffalolabApp, BouffalolabBoard, BouffalolabBuilder
from builders.cc13x2x7_26x2x7 import cc13x2x7_26x2x7App, cc13x2x7_26x2x7Builder
from builders.cc32xx import cc32xxApp, cc32xxBuilder
Expand Down Expand Up @@ -297,6 +297,9 @@ def BuildAndroidTarget():
TargetPart('java-matter-controller', app=AndroidApp.JAVA_MATTER_CONTROLLER),
])

# Modifiers
target.AppendModifier('no-debug', profile=AndroidProfile.RELEASE)

return target


Expand Down
51 changes: 46 additions & 5 deletions scripts/build/builders/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def AppName(self):

def AppGnArgs(self):
gn_args = {}

if self == AndroidApp.TV_SERVER:
gn_args["chip_config_network_layer_ble"] = False
elif self == AndroidApp.TV_CASTING_APP:
Expand All @@ -107,11 +108,31 @@ def Modules(self):
return None


class AndroidProfile(Enum):
RELEASE = auto()
DEBUG = auto()

@property
def ProfileName(self):
if self == AndroidProfile.RELEASE:
return 'release'
elif self == AndroidProfile.DEBUG:
return 'debug'
else:
raise Exception('Unknown profile type: %r' % self)


class AndroidBuilder(Builder):
def __init__(self, root, runner, board: AndroidBoard, app: AndroidApp):
def __init__(self,
root,
runner,
board: AndroidBoard,
app: AndroidApp,
profile: AndroidProfile = AndroidProfile.DEBUG):
super(AndroidBuilder, self).__init__(root, runner)
self.board = board
self.app = app
self.profile = profile

def validate_build_environment(self):
for k in ["ANDROID_NDK_HOME", "ANDROID_HOME"]:
Expand Down Expand Up @@ -258,6 +279,7 @@ def gradlewBuildSrcAndroid(self):
)

def gradlewBuildExampleAndroid(self):

# Example compilation
if self.app.Modules():
for module in self.app.Modules():
Expand Down Expand Up @@ -311,6 +333,8 @@ def generate(self):
gn_args["target_cpu"] = self.board.TargetCpuName()
gn_args["android_ndk_root"] = os.environ["ANDROID_NDK_HOME"]
gn_args["android_sdk_root"] = os.environ["ANDROID_HOME"]
if self.profile != AndroidProfile.DEBUG:
gn_args["is_debug"] = False
gn_args.update(self.app.AppGnArgs())

args_str = ""
Expand Down Expand Up @@ -368,6 +392,22 @@ def generate(self):

app_dir = os.path.join(self.root, "examples/", self.app.AppName())

def stripSymbols(self):
output_libs_dir = os.path.join(
self.output_dir,
"lib",
"jni",
self.board.AbiName())
for lib in os.listdir(output_libs_dir):
if (lib.endswith(".so")):
self._Execute(
["llvm-strip",
"-s",
os.path.join(output_libs_dir, lib)
],
"Stripping symbols from " + lib
)

def _build(self):
if self.board.IsIde():
# App compilation IDE
Expand Down Expand Up @@ -442,6 +482,9 @@ def _build(self):
self.copyToExampleApp(jnilibs_dir, libs_dir, libs, jars)
self.gradlewBuildExampleAndroid()

if (self.profile != AndroidProfile.DEBUG):
self.stripSymbols()

def build_outputs(self):
if self.board.IsIde():
outputs = {
Expand All @@ -465,15 +508,13 @@ def build_outputs(self):
}
else:
outputs = {
self.app.AppName()
+ "app-debug.apk": os.path.join(
self.app.AppName() + "app-debug.apk": os.path.join(
self.output_dir, "outputs", "apk", "debug", "app-debug.apk"
)
}
else:
outputs = {
self.app.AppName()
+ "app-debug.apk": os.path.join(
self.app.AppName() + "app-debug.apk": os.path.join(
self.output_dir, "outputs", "apk", "debug", "app-debug.apk"
),
"CHIPController.jar": os.path.join(
Expand Down
2 changes: 1 addition & 1 deletion scripts/build/testdata/all_targets_linux_x64.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ameba-amebad-{all-clusters,all-clusters-minimal,light,light-switch,pigweed}
android-{arm,arm64,x86,x64,androidstudio-arm,androidstudio-arm64,androidstudio-x86,androidstudio-x64}-{chip-tool,chip-test,tv-server,tv-casting-app,java-matter-controller}
android-{arm,arm64,x86,x64,androidstudio-arm,androidstudio-arm64,androidstudio-x86,androidstudio-x64}-{chip-tool,chip-test,tv-server,tv-casting-app,java-matter-controller}[-no-debug]
bouffalolab-{bl602-iot-matter-v1,bl602-iot-dvk-3s,bl602-night-light,xt-zb6-devkit,bl706-iot-dvk,bl706-night-light}-light[-shell][-115200][-rpc]
cc13x2x7_26x2x7-{all-clusters,all-clusters-minimal,lock,pump,pump-controller,shell}[-ftd][-mtd]
cc32xx-lock
Expand Down

0 comments on commit bb9dbbf

Please sign in to comment.