1# Perfetto build instructions 2 3The source of truth for the Perfetto codebase lives in AOSP: 4https://android.googlesource.com/platform/external/perfetto/ 5 6A read-only mirror is also available at https://github.com/google/perfetto . 7 8Perfetto can be built both from the Android tree (AOSP) and standalone. 9Standalone builds are meant only for local testing and are not shipped. 10Due to the reduced dependencies they are faster to iterate on and the 11suggested way to work on Perfetto. 12 13## Get the code 14 15**Standalone checkout**: 16 17```bash 18git clone https://android.googlesource.com/platform/external/perfetto/ 19``` 20 21**Android tree**: 22 23Perfetto lives in [`external/perfetto` in the AOSP tree](https://cs.android.com/android/platform/superproject/+/master:external/perfetto/). 24 25## Prerequisites 26 27**Standalone checkout**: 28 29All dependent libraries are self-hosted and pulled through: 30 31```bash 32tools/install-build-deps [--android] [--ui] 33``` 34 35**Android tree**: 36 37See https://source.android.com/setup 38 39## Building 40 41**Standalone checkout**: 42 43If you are a chromium developer and have depot_tools installed you can avoid 44the `tools/` prefix below and just use gn/ninja from depot_tools. 45 46`$ tools/gn args out/android` to generate build files and enter in the editor: 47 48```python 49target_os = "android" # Only when building for Android 50target_cpu = "arm" / "arm64" / "x64" 51is_debug = true / false 52cc_wrapper = "ccache" # Optionally speed repeated builds with ccache 53``` 54 55(See the [Build Configurations](#build-configurations) section below for more) 56 57```bash 58tools/ninja -C out/android 59``` 60 61**Android tree** 62 63`mmma external/perfetto` 64or 65`m perfetto traced traced_probes` 66 67This will generate artifacts `out/target/product/XXX/system/`. 68Executables and shared libraries are stripped by default by the Android build 69system. The unstripped artifacts are kept into `out/target/product/XXX/symbols`. 70 71## UI development 72 73This command pulls the UI-related dependencies (notably, the NodeJS binary) 74and installs the `node_modules` in `ui/node_modules`: 75 76```bash 77tools/install-build-deps --ui 78``` 79 80Build the UI: 81 82```bash 83# Will build into ./out/ui by default. Can be changed with --out path/ 84# The final bundle will be available at ./ui/out/dist/. 85# The build script creates a symlink from ./ui/out to $OUT_PATH/ui/. 86ui/build 87``` 88 89Test your changes on a local server using: 90 91```bash 92# This will automatically build the UI. There is no need to manually run 93# ui/build before running ui/run-dev-server. 94ui/run-dev-server 95``` 96 97Navigate to http://localhost:10000/ to see the changes. 98 99The server supports live reloading of CSS and TS/JS contents. Whenever a ui 100source file is changed it, the script will automatically re-build it and show a 101prompt in the web page. 102 103## IDE setup 104 105Use a following command in the checkout directory in order to generate the 106compilation database file: 107 108```bash 109tools/gn gen out/default --export-compile-commands 110``` 111 112After generating, it can be used in CLion (File -> Open -> Open As Project), 113Visual Studio Code with C/C++ extension and any other tool and editor that 114supports the compilation database format. 115 116## Build files 117 118The source of truth of our build file is in the BUILD.gn files, which are based 119on [GN][gn-quickstart]. 120The Android build file ([Android.bp](/Android.bp)) is autogenerated from the GN 121files through `tools/gen_android_bp`, which needs to be invoked whenever a 122change touches GN files or introduces new ones. 123 124A presubmit check checks that the Android.bp is consistent with GN files when 125submitting a CL through `git cl upload`. 126 127The generator has a list of root targets that will be translated into the 128Android.bp file. If you are adding a new target, add a new entry to the 129`default_targets` variable in [`tools/gen_android_bp`](/tools/gen_android_bp). 130 131## Supported platforms 132 133**Linux desktop** (Debian Rodete) 134 135- Hermetic clang + libcxx toolchain (both following chromium's revisions) 136- GCC-7 and libstdc++ 6 137 138**Android** 139 140- Android's NDK r15c (using NDK's libcxx) 141- AOSP's in-tree clang (using in-tree libcxx) 142 143**Mac** 144 145- XCode 9 / clang (currently maintained best-effort). 146 147**Windows** 148 149Windows builds are not currently supported when using the standalone checkout 150and GN. Windows is supported only for a subset of the targets (mainly 151`trace_processor` and the in-process version of the 152[Tracing SDK](/docs/instrumentation/tracing-sdk.md)) in two ways: 153(1) when building through Bazel; (2) when building as part of Chromium. 154 155## Build configurations 156 157TIP: `tools/build_all_configs.py` can be used to generate out/XXX folders for 158most of the supported configurations. 159 160The following [GN args][gn-quickstart] are supported: 161 162`target_os = "android" | "linux" | "mac"`: 163 164Defaults to the current host, set "android" to build for Android. 165 166`target_cpu = "arm" | "arm64" | "x64"` 167 168Defaults to `"arm"` when `target_os` == `"android"`, `"x64"` when targeting the 169host. 32-bit host builds are not supported. 170Note: x64 here really means x86_64. This is to keep it consistent with 171Chromium's choice, which in turn follows Windows naming convention. 172 173`is_debug = true | false` 174 175Toggles Debug (default) / Release mode. This affects, among other things: 176(i) the `-g` compiler flag; (ii) setting/unsetting `-DNDEBUG`; (iii) turning 177on/off `DCHECK` and `DLOG`. 178Note that debug builds of Perfetto are sensibly slower than release versions. We 179strongly encourage using debug builds only for local development. 180 181`is_clang = true | false` 182 183Use Clang (default: true) or GCC (false). 184On Linux, by default it uses the self-hosted clang (see `is_hermetic_clang`). 185On Android, by default it uses clang from the NDK (in `buildtools/ndk`). 186On Mac, by default it uses the system version of clang (requires Xcode). 187See also the [custom toolchain](#custom-toolchain) section below. 188 189`is_hermetic_clang = true | false` 190 191Use bundled toolchain from `buildtools/` rather than system-wide one. 192 193`cc = "gcc" / cxx = "g++"` 194 195Uses a different compiler binary (default: autodetected depending on is_clang). 196See also the [custom toolchain](#custom-toolchain) section below. 197 198`cc_wrapper = "tool_name"` 199 200Prepends all build commands with a wrapper command. Using `"ccache"` here 201enables the [ccache](https://github.com/ccache/ccache) caching compiler, 202which can considerably speed up repeat builds. 203 204`is_asan = true` 205 206Enables [Address Sanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer) 207 208`is_lsan = true` 209 210Enables [Leak Sanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer) 211(Linux/Mac only) 212 213`is_msan = true` 214 215Enables [Memory Sanitizer](https://github.com/google/sanitizers/wiki/MemorySanitizer) 216(Linux only) 217 218`is_tsan = true` 219 220Enables [Thread Sanitizer](https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual) 221(Linux/Mac only) 222 223`is_ubsan = true` 224 225Enables [Undefined Behavior Sanitizer](https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html) 226 227### {#custom-toolchain} Using custom toolchains and CC / CXX / CFLAGS env vars 228 229When building Perfetto as part of some other build environment it might be 230necessary to switch off all the built-in toolchain-related path-guessing scripts 231and manually specify the path of the toolchains. 232 233```python 234# Disable the scripts that guess the path of the toolchain. 235is_system_compiler = true 236 237ar = "/path/to/ar" 238cc = "/path/to/gcc-like-compiler" 239cxx = "/path/to/g++-like-compiler" 240linker = "" # This is passed to -fuse-ld=... 241``` 242 243If you are using a build system that keeps the toolchain settings in 244environment variables, you can set: 245 246```python 247is_system_compiler = true 248ar="${AR}" 249cc="${CC}" 250cxx="${CXX}" 251``` 252 253`is_system_compiler = true` can be used also for cross-compilation. 254In case of cross-compilation, the GN variables have the following semantic: 255`ar`, `cc`, `cxx`, `linker` refer to the _host_ toolchain (sometimes also called 256_build_ toolchain). This toolchain is used to build: (i) auxiliary tools 257(e.g. the `traceconv` conversion util) and (ii) executable artifacts that are 258used during the rest of the build process for the target (e.g., the `protoc` 259compiler or the `protozero_plugin` protoc compiler plugin). 260 261The cross-toolchain used to build the artifacts that run on the device is 262prefixed by `target_`: `target_ar`, `target_cc`, `target_cxx`, `target_linker`. 263 264```python 265# Cross compilation kicks in when at least one of these three variables is set 266# to a value != than the host defaults. 267 268target_cpu = "x86" | "x64" | "arm" | "arm64" 269target_os = "linux" | "android" 270target_triplet = "arm-linux-gnueabi" | "x86_64-linux-gnu" | ... 271``` 272 273When integrating with GNU Makefile cross-toolchains build environments, a 274typical mapping of the corresponding environment variables is: 275 276```python 277ar="${BUILD_AR}" 278cc="${BUILD_CC}" 279cxx="${BUILD_CXX}" 280target_ar="${AR}" 281target_cc="${CC}" 282target_cxx="${CXX}" 283``` 284 285It is possible to extend the set of `CFLAGS` and `CXXFLAGS` through the 286`extra_xxxflags` GN variables as follows. The extra flags are always appended 287(hence, take precedence) to the set of flags that the GN build files generate. 288 289```python 290# These apply both to host and target toolchain. 291extra_cflags="${CFLAGS}" 292extra_cxxflags="${CXXFLAGS}" 293extra_ldflags="${LDFLAGS}" 294 295# These apply only to the host toolchain. 296extra_host_cflags="${BUILD_CFLAGS}" 297extra_host_cxxflags="${BUILD_CXXFLAGS}" 298extra_host_ldflags="${BUILD_LDFLAGS}" 299 300# These apply only to the target toolchain. 301extra_target_cflags="${CFLAGS}" 302extra_target_cxxflags="${CXXFLAGS} ${debug_flags}" 303extra_target_ldflags="${LDFLAGS}" 304``` 305 306[gn-quickstart]: https://gn.googlesource.com/gn/+/master/docs/quick_start.md 307