xref: /aosp_15_r20/external/toolchain-utils/binary_search_tool/ndk/README.md (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1*760c253cSXin Li# NDK Bisection tool
2*760c253cSXin Li
3*760c253cSXin LiThis is an example bisection for an NDK build system. This example specifically
4*760c253cSXin Libisects the sample NDK Teapot app. All steps (setup and otherwise) for bisection
5*760c253cSXin Lican be found in `DO_BISECTION.sh`. This shell script is meant to show the
6*760c253cSXin Liprocess required to bisect a compiler problem in an arbitrary NDK app build
7*760c253cSXin Lisystem.
8*760c253cSXin Li
9*760c253cSXin LiThere are three necessary setup steps to run this example:
10*760c253cSXin Li
11*760c253cSXin Li1.  Install the NDK (known to work with r12b)
12*760c253cSXin Li
13*760c253cSXin Li    1. See here for NDK: https://developer.android.com/ndk/index.html
14*760c253cSXin Li    2. Go here for older NDK downloads: https://github.com/android-ndk/ndk/wiki
15*760c253cSXin Li
16*760c253cSXin Li1.  Install the compiler wrapper provided with this repo. See
17*760c253cSXin Li    compiler_wrapper.py for more details.
18*760c253cSXin Li
19*760c253cSXin Li    1.  Essentially you must go into the NDK source (or where you build system
20*760c253cSXin Li        stores its toolchain), rename your compilers to <compiler>.real, and
21*760c253cSXin Li        create a symlink pointing to compiler_wrapper.py named <compiler>
22*760c253cSXin Li        (where your compiler used to be).
23*760c253cSXin Li
24*760c253cSXin Li    2.  If you're using the toolchains that come with the NDK they live at:
25*760c253cSXin Li        `<ndk_path>/toolchains/<arch>/prebuilt/<host>/bin`
26*760c253cSXin Li        example:
27*760c253cSXin Li        `<ndk_path>/toolchains/llvm/prebuilt/linux-x86_64/bin/clang`
28*760c253cSXin Li
29*760c253cSXin Li1.  Plug in an Arm7 compatible Android device with usb debugging enabled.
30*760c253cSXin Li
31*760c253cSXin Li    1. This bisection example was tested with a Nexus 5X
32*760c253cSXin Li
33*760c253cSXin Li    2. It should be relatively simple to change the example to work with other
34*760c253cSXin Li       types of devices. Just change the scripts, and change PATCH1 to use a
35*760c253cSXin Li       different build flavor (like x86). See below for more details.
36*760c253cSXin Li
37*760c253cSXin LiThis example contains two patches:
38*760c253cSXin Li
39*760c253cSXin Li`PATCH1` - This is the necessary changes to the build system to make the
40*760c253cSXin Libisection easier. More specifically, it adds an arm7 build flavor to gradle.
41*760c253cSXin LiBy default, this project will build objects for all possible architectures and
42*760c253cSXin Lipackage them into one big apk. These object files meant for another
43*760c253cSXin Liarchitecture just sit there and don't actually execute. By adding a build
44*760c253cSXin Liflavor for arm7, our compiler wrapper won't try to bisect object files meant
45*760c253cSXin Lifor another device.
46*760c253cSXin Li
47*760c253cSXin Li`PATCH2` - This patch is what inserts the "compiler error". This is a simple
48*760c253cSXin Linullptr error in one of the source files, but it is meant to mimic bad code
49*760c253cSXin Ligeneration. The result of the error is the app simply crashes immediately
50*760c253cSXin Liafter starting.
51*760c253cSXin Li
52*760c253cSXin Li## Using another device architecture
53*760c253cSXin Li
54*760c253cSXin LiIf we want to bisect for an x86-64 device we first need to provide a arch
55*760c253cSXin Lispecific build flavor in our app/build.gradle file:
56*760c253cSXin Li
57*760c253cSXin Li```
58*760c253cSXin Licreate("x86-64") {
59*760c253cSXin Li  ndk.abiFilters.add("x86_64")
60*760c253cSXin Li}
61*760c253cSXin Li```
62*760c253cSXin Li
63*760c253cSXin LiWe want to add this under the same "productFlavors" section that our arm7
64*760c253cSXin Libuild flavor is in (see PATCH1). Now we should have the "installx86-64Debug"
65*760c253cSXin Litask in our build system. We can use this to build and install an x86-64
66*760c253cSXin Liversion of our app.
67*760c253cSXin Li
68*760c253cSXin LiNow we want to change our `test_setup.sh` script to run our new gradle task:
69*760c253cSXin Li```
70*760c253cSXin Li./gradlew installx86-64Debug
71*760c253cSXin Li```
72*760c253cSXin Li
73*760c253cSXin LiKeep in mind, these specific build flavors are not required. If your build
74*760c253cSXin Lisystem makes these device specific builds difficult to implement, the
75*760c253cSXin Libisection tool will function perfectly fine without them. However, the
76*760c253cSXin Lidownside of not having targetting a single architecture is the bisection will
77*760c253cSXin Litake longer (as it will need to search across more object files).
78*760c253cSXin Li
79*760c253cSXin Li## Additional Documentation
80*760c253cSXin Li
81*760c253cSXin LiThese are internal Google documents, if you are a developer external to
82*760c253cSXin LiGoogle please ask whoever gave you this sample for access or copies to the
83*760c253cSXin Lidocumentation. If you cannot gain access, the various READMEs paired with the
84*760c253cSXin Libisector should help you.
85*760c253cSXin Li
86*760c253cSXin Li* Ahmad's original presentation: https://goto.google.com/zxdfyi
87*760c253cSXin Li* Bisection tool update design doc: https://goto.google.com/zcwei
88*760c253cSXin Li* Bisection tool webpage: https://goto.google.com/ruwpyi
89*760c253cSXin Li* Compiler wrapper webpage: https://goto.google.com/xossn
90