blob: 514caf77dac90efdc7dbc08f0c2197db57908ffb [file] [log] [blame] [view]
Andrew Grieve860b1552017-09-06 14:50:061# Static Initializers
2
3[TOC]
4
5Some background on the original decision to ban static initializers:
6
7http://neugierig.org/software/chromium/notes/2011/08/static-initializers.html
8
Andrew Grieve5e704682021-02-19 23:35:249Note: Another name for static initializers is "global constructors".
10
Andrew Grieve860b1552017-09-06 14:50:0611# How Static Initializers are Checked
12
13* For Linux and Mac:
Matthew Denton43cafd52021-05-26 15:42:1414 * The expected count is stored in [//testing/scripts/check_static_initializers.py](https://source.chromium.org/chromium/chromium/src/+/main:testing/scripts/check_static_initializers.py)
Andrew Grieve860b1552017-09-06 14:50:0615* For Android:
16 * The expected count is stored in the build target [//chrome/android:monochrome_static_initializers](https://cs.chromium.org/chromium/src/chrome/android/BUILD.gn)
17
18## Removing Static Initializers
19
20Common fixes include:
21
Lei Zhangfafa2b72020-09-29 21:58:4622* Add constexpr.
23* Move global variable to be a static variable within a function that returns
24 it, often wrapped in `base::NoDestructor`.
Andrew Grieve860b1552017-09-06 14:50:0625
26## Listing Static Initializers
27
Andrew Grieve5e704682021-02-19 23:35:2428### Step 1 - Use objdump to report them
Andrew Grieve860b1552017-09-06 14:50:0629For Linux:
30
31 tools/linux/dump-static-initializers.py out/Release/chrome
32
Andrew Grieve3c2c4202020-04-16 01:55:0533For Android (from easiest to hardest):
Andrew Grieve860b1552017-09-06 14:50:0634
Andrew Grieve3c2c4202020-04-16 01:55:0535 # Build with: is_official_build=true is_chrome_branded=true
36 # This will dump the list of SI's only when they don't match the expected
37 # number in static_initializers.gni (this is what the bots use).
38 ninja chrome/android:monochrome_static_initializers
39 # or:
40 tools/binary_size/diagnose_bloat.py HEAD # See README.md for flags.
Andrew Grieve5e704682021-02-19 23:35:2441 # or (the other two use this under the hood):
42 tools/linux/dump-static-initializers.py --toolchain-prefix third_party/android_ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi- out/Release/lib.unstripped/libmonochrome.so
43 # arm32 ^^ vv arm64
44 tools/linux/dump-static-initializers.py --toolchain-prefix third_party/android_ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android- out/Release/lib.unstripped/libmonochrome.so
45 # Note: For arm64, having use_thin_lto=true seems to dump a couple extra
46 # initializers that don't actually exist.
47
48The last one may actually be the easiest if you've already properly built
49`libmonochrome.so` with `is_official_build=true`.
50
51### Step 2 - Ask compiler to report them
52
53If the source of the new initiazers is not obvious from Step 1, you can ask the
54compiler to pinpoint the exact source line.
55
561. Edit [//build/config/BUILDCONFIG.gn](https://cs.chromium.org/chromium/src/build/config/BUILDCONFIG.gn)
57and add `"//build/config/compiler:wglobal_constructors"` to `default_compiler_configs`
582. Remove the config from the `configs` in `//base:base`
593. Set GN arg `treat_warnings_as_errors=false`
604. Compile and look for warnings **from the files identified by step 1** (may want to pipe ninja output to a file).
61
62*** note
63The compiler warning triggers for every static initializer that exists
64*before optimization*. We care only about those that survive optimization.
65More details in [crbug/1136086](https://bugs.chromium.org/p/chromium/issues/detail?id=1136086).
66***
Andrew Grieve860b1552017-09-06 14:50:0667
Andrew Grieve3c2c4202020-04-16 01:55:0568* For more information about `diagnose_bloat.py`, refer to its [README.md](/tools/binary_size/README.md#diagnose_bloat.py)
69* List of existing static initializers documented in [static_initializers.gni](/chrome/android/static_initializers.gni)