chromium/third_party/r8/README.chromium

Name: R8
URL: https://r8.googlesource.com/r8
Version: unknown
License: BSD 3-Clause
License File: LICENSE
Security Critical: no
Shipped: no

Description:
R8 is a whole-program optimizer and dexer. It takes in .class files and outputs
.dex files.

Directory overview:
* lib/r8.jar
  * The copy of R8 that is used for proguarding.
  * Updated daily by an auto-roller.
* d8/lib/r8.jar
  * The copy of R8 that is used for dexing.
  * Updated manually (and infrequently) in order to avoid having to re-dex all
    library targets every day.
* 3pp
  * Contains code for 3pp-linux-amd64-packager to package and upload new
    versions of r8.
* print_version.sh
  * Print the R8 commit for the current version.sh.

Local Modifications:
* Refer to commit descriptions within "3pp/patches" directory.
* Added "playground" directory for quick "how does this optimize" tests.
* Added "dump_inputs.py" and "print_version.sh" scripts.
* Added "java/src/org/chromium/build/CustomD8.java", custom_d8.jar, and BUILD.gn.
  * Used in "build/android/gyp/dex.py" to enable desugar dependencies.

Update Instructions:
* For the custom r8/d8 jar:
  * This is required only when Custom{R,D}8.java changes.
  * Uncomment the flag --skip-custom-d8 in internal_rules.gni to avoid depending
    on the previously built custom_d8.jar.
autoninja -C out/Debug third_party/r8:custom_r8_java
cp out/Debug/obj/third_party/r8/custom_r8_java.javac.jar third_party/r8/custom_r8.jar
OR
autoninja -C out/Debug third_party/r8:custom_d8_java
cp out/Debug/obj/third_party/r8/custom_d8_java.javac.jar third_party/r8/custom_d8.jar

* For r8:
  * R8 is built daily and uploaded to CIPD.
  * New instances are uploaded by the packager bot:
    https://ci.chromium.org/p/chromium/builders/ci/3pp-linux-amd64-packager
  * The bot autoruns every 6 hours. Ping a trooper or a clank build core dev to
    trigger it if you need it sooner:
    https://luci-scheduler.appspot.com/jobs/chromium/3pp-linux-amd64-packager
  * The autoroller runs automatically when a new cipd instance is uploaded.
    * Since r8 rolls affect downstream, the roller is only accessible to Googlers.
    * See its status here: https://skia-autoroll.corp.goog/r/r8-chromium

* If patching fails:
git clone https://r8.googlesource.com/r8
cd r8
  * Pull the latest code:
git pull
  * Apply patches:
git checkout -b my_branch
git am $CHROMIUM_SRC/third_party/r8/3pp/patches/*.patch
  * Fix patch conflicts.
  * Save new patches. Change number if expecting more or fewer patches:
rm $CHROMIUM_SRC/third_party/r8/3pp/patches/*
git format-patch origin/main..HEAD -o $CHROMIUM_SRC/third_party/r8/3pp/patches
  * Submit these fixed patches and the bot will automatically retry.

* Local development (see above for setting up the r8 repo and applying patches):
  * Build
tools/gradle.py r8
  * Shrink (improves r8/d8 launch time):
java -jar build/libs/r8.jar --debug --classfile --output r8.jar \
    --lib $CHROMIUM_SRC/third_party/jdk/current --pg-conf src/main/keep.txt \
    --no-minification --no-desugaring build/libs/r8.jar
mv $CHROMIUM_SRC/third_party/r8/lib/r8.jar{,.bak}
cp r8.jar $CHROMIUM_SRC/third_party/r8/lib/r8.jar

* Update backported methods list (used by lint):
cd $CHROMIUM_SRC
java -cp third_party/r8/lib/r8.jar com.android.tools.r8.BackportedMethodList --min-api 26 > third_party/r8/backported_methods.txt

How to file bugs against R8:
* Copy & paste the failing ninja command (starts with proguard.py), and add --dump-inputs.
  * On siso you may need to find it at the beginning of `out/<Directory>/siso_output`.
  * This also works for dex.py, it produces d8inputs.zip
  * As a shortcut: third_party/r8/dump_inputs.py out/Release/apks/ChromePublic.apk.mapping
* File bug at go/r8bug
* Things to include (use discretion if some are not relevant):
  * src revision bug reproduces at
  * Repro steps (gn gen & autoninja commands)
    * Prefer enable_chrome_android_internal=false
  * The r8inputs.zip from --dump-inputs
  * Any relevant dexdump analysis

Example R8 git-bisect script:
```
set -x
CHROMIUM_SRC=/media/mheikal/code/clankium/src
tools/gradle.py r8 || exit -1
chmod +r $CHROMIUM_SRC/third_party/r8/lib/r8.jar
rm -f $CHROMIUM_SRC/third_party/r8/lib/r8.jar
cp build/libs/r8.jar $CHROMIUM_SRC/third_party/r8/lib/r8.jar

cd $CHROMIUM_SRC
exec tools/autotest.py --out-dir out/Release <Test Here>
```

You can run this script like so:
```
cd $R8_REPO
git bisect start <known_bad_rev> <known_good_rev>
git bisect run bash <path_to_script.sh>
```
Tip: When you are done, you can delete the r8 jar and gclient sync to get back
     to ToT R8.
     `rm -f $CHROMIUM_SRC/third_party/r8/lib/r8.jar && gclient sync`

How to submit CLs to R8:
* Request to be added to their allowlist in order to upload CLs.
* After CLs are submitted, check the bots for build breakage.
  * https://ci.chromium.org/p/r8/g/main_all/console

How to check the size impact of recent R8 rolls:
# To public APK:
tools/binary_size/print_trybot_sizes.py --grep "Roll R8" -n 20
# To internal APK:
tools/binary_size/print_trybot_sizes.py --grep "Roll R8" -n 20 --internal