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, the standalone workflow is faster to iterate on 11and the suggested way to work on Perfetto, unless you are working on code that 12has non-NDK depedencies into Android internals. Profilers and internal HAL/AIDL 13dependencies will not be built in the standalone build. 14 15If you are chromium contributor, AOSP is still the place you should send CLs to. 16The code inside chromium's 17[third_party/perfetto](https://source.chromium.org/chromium/chromium/src/+/main:third_party/perfetto/?q=f:third_party%2Fperfetto&ss=chromium) 18is a direct mirror of the AOSP repo. The 19[AOSP->Chromium autoroller](https://autoroll.skia.org/r/perfetto-chromium-autoroll) 20takes care of keeping chromium's DEPS up to date. 21 22## Standalone builds 23 24#### Get the code 25 26```bash 27git clone https://android.googlesource.com/platform/external/perfetto/ 28``` 29 30#### Pull dependent libraries and toolchains 31 32```bash 33tools/install-build-deps [--android] [--ui] [--linux-arm] 34``` 35 36`--android` will pull the Android NDK, emulator and other deps required 37to build for `target_os = "android"`. 38 39`--ui` will pull NodeJS and all the NPM modules required to build the 40Web UI. See the [UI Development](#ui-development) section below for more. 41 42`--linux-arm` will pull the sysroots for cross-compiling for Linux ARM/64. 43 44WARNING: Note that if you're using an M1 or any later ARM Mac, your Python 45version should be at least 3.9.1 to work around 46[this Python Bug](https://bugs.python.org/issue42704). 47 48#### Generate the build files via GN 49 50Perfetto uses [GN](https://gn.googlesource.com/gn/+/HEAD/docs/quick_start.md) 51as primary build system. See the [Build files](#build-files) section below for 52more. 53 54```bash 55tools/gn args out/android 56``` 57 58This will open an editor to customize the GN args. Enter: 59 60```python 61# Set only when building for Android, omit when building for linux, mac or win. 62target_os = "android" 63target_cpu = "arm" / "arm64" / "x64" 64 65is_debug = true / false 66cc_wrapper = "ccache" # [Optional] speed up rebuilds with ccache. 67``` 68 69See the [Build Configurations](#build-configurations) and 70[Building on Windows](#building-on-windows) sections below for more. 71 72TIP: If you are a chromium developer and have depot_tools installed you can 73avoid the `tools/` prefix below and just use gn/ninja from depot_tools. 74 75#### Build native C/C++ targets 76 77```bash 78# This will build all the targets. 79tools/ninja -C out/android 80 81# Alternatively, list targets explicitly. 82tools/ninja -C out/android \ 83 traced \ # Tracing service. 84 traced_probes \ # Ftrace interop and /proc poller. 85 perfetto \ # Cmdline client. 86 trace_processor_shell \ # Trace parsing. 87 trace_to_text # Trace conversion. 88... 89``` 90 91## Android tree builds 92 93Follow these instructions if you are an AOSP contributor. 94 95The source code lives in [`external/perfetto` in the AOSP tree](https://cs.android.com/android/platform/superproject/+/master:external/perfetto/). 96 97Follow the instructions on https://source.android.com/setup/build/building . 98 99Then: 100 101```bash 102mmma external/perfetto 103# or 104m traced traced_probes perfetto 105``` 106 107This will generate artifacts `out/target/product/XXX/system/`. 108 109Executables and shared libraries are stripped by default by the Android build 110system. The unstripped artifacts are kept into `out/target/product/XXX/symbols`. 111 112## UI development 113 114This command pulls the UI-related dependencies (notably, the NodeJS binary) 115and installs the `node_modules` in `ui/node_modules`: 116 117```bash 118tools/install-build-deps --ui 119``` 120 121Build the UI: 122 123```bash 124# Will build into ./out/ui by default. Can be changed with --out path/ 125# The final bundle will be available at ./ui/out/dist/. 126# The build script creates a symlink from ./ui/out to $OUT_PATH/ui/. 127ui/build 128``` 129 130Test your changes on a local server using: 131 132```bash 133# This will automatically build the UI. There is no need to manually run 134# ui/build before running ui/run-dev-server. 135ui/run-dev-server 136``` 137 138Navigate to http://localhost:10000/ to see the changes. 139 140The server supports live reloading of CSS and TS/JS contents. Whenever a ui 141source file is changed it, the script will automatically re-build it and show a 142prompt in the web page. 143 144## Build files 145 146The source of truth of our build file is in the BUILD.gn files, which are based 147on [GN][gn-quickstart]. 148The Android build file ([Android.bp](/Android.bp)) is autogenerated from the GN 149files through `tools/gen_android_bp`, which needs to be invoked whenever a 150change touches GN files or introduces new ones. 151Likewise, the Bazel build file ([BUILD](/BUILD)) is autogenerated through the 152`tools/gen_bazel` script. 153 154A presubmit check checks that the Android.bp is consistent with GN files when 155submitting a CL through `git cl upload`. 156 157The generator has a list of root targets that will be translated into the 158Android.bp file. If you are adding a new target, add a new entry to the 159`default_targets` variable in [`tools/gen_android_bp`](/tools/gen_android_bp). 160 161## Supported platforms 162 163**Linux desktop** (Debian Testing/Rodete) 164 165- Hermetic clang + libcxx toolchain (both following chromium's revisions) 166- GCC-7 and libstdc++ 6 167- Cross-compiling for arm and arm64 (more below). 168 169**Android** 170 171- Android's NDK r15c (using NDK's libcxx) 172- AOSP's in-tree clang (using in-tree libcxx) 173 174**Mac** 175 176- XCode 9 / clang (maintained best-effort). 177 178**Windows** 179 180- Windows 10 with either MSVC 2019 or clang-cl (maintained best-effort). 181 182### Building on Windows 183 184Building on Windows is possible using both the MSVC 2019 compiler (you don't 185need the full IDE, just the build tools) or the LLVM clang-cl compiler. 186 187The Windows support in standalone builds has been introduced in v16 by 188[r.android.com/1711913](https://r.android.com/1711913). 189 190clang-cl support is more stable because that build configuration is actively 191covered by the Chromium project (Perfetto rolls into chromium and underpins 192chrome://tracing). The MSVC build is maintained best-effort. 193 194The following targets are supported on Windows: 195 196- `trace_processor_shell`: the trace importer and SQL query engine. 197- `trace_to_text`: the trace conversion tool. 198- `traced` and `perfetto`: the tracing service and cmdline client. They use an 199 alternative implementation of the [inter-process tracing protocol](/docs/design-docs/api-and-abi.md#tracing-protocol-abi) 200 based on a TCP socket and named shared memory. This configuration is only for 201 testing / benchmarks and is not shipped in production. 202 Googlers: see [go/perfetto-win](http://go/perfetto-win) for details. 203- `perfetto_unittests` / `perfetto_integrationtests`: although they support only 204 the subset of code that is supported on Windows (e.g. no ftrace). 205 206It is NOT possible to build the Perfetto UI from Windows. 207 208#### Prerequisites 209 210You need all of these both for MSVC and clang-cl: 211 212- [Build Tools for Visual Studio 2019](https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019) 213- [Windows 10 SDK](https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk/) 214- [Python 3](https://www.python.org/downloads/windows/) 215 216The [`win_find_msvc.py`](/gn/standalone/toolchain/win_find_msvc.py) script will 217locate the higest version numbers available from 218`C:\Program Files (x86)\Windows Kits\10` and 219`C:\Program Files (x86)\Microsoft Visual Studio\2019`. 220 221#### Pull dependent libraries and toolchains 222 223```bash 224# This will download also the LLVM clang-cl prebuilt used by chromium. 225python3 tools/install-build-deps 226``` 227 228#### Generate build files 229 230```bash 231python3 tools/gn gen out/win 232``` 233 234In the editor type: 235 236```bash 237is_debug = true | false 238 239is_clang = true # Will use the hermetic clang-cl toolchain. 240# or 241is_clang = false # Will use MSVC 2019. 242``` 243 244#### Build 245 246```bash 247python3 tools/ninja -C out/win perfetto traced trace_processor_shell 248``` 249 250### Cross-compiling for Linux ARM/64 251 252When cross-compiling for Linux you will need a sysroot. You have two options: 253 254#### 1. Use the built-in sysroots based on Debian Sid 255 256```bash 257tools/install-build-deps --linux-arm 258``` 259 260Then set the following GN args: 261 262```python 263target_os = "linux" 264target_cpu = "arm" 265# or 266target_cpu = "arm64" 267``` 268 269#### 2. Use your own sysroot 270 271In this case you need to manually specify the sysroot location and the 272toolchain prefix triplet to use. 273 274```python 275target_os = "linux" 276target_sysroot = "/path/to/sysroot" 277target_triplet = "aarch64-linux-gnu" # Or any other supported triplet. 278``` 279 280For more details see the [Using cutom toolchains](#custom-toolchain) section 281below. 282 283## Build configurations 284 285TIP: `tools/build_all_configs.py` can be used to generate out/XXX folders for 286most of the supported configurations. 287 288The following [GN args][gn-quickstart] are supported: 289 290`target_os = "android" | "linux" | "mac"`: 291 292Defaults to the current host, set "android" to build for Android. 293 294`target_cpu = "arm" | "arm64" | "x64"` 295 296Defaults to `"arm"` when `target_os` == `"android"`, `"x64"` when targeting the 297host. 32-bit host builds are not supported. 298Note: x64 here really means x86_64. This is to keep it consistent with 299Chromium's choice, which in turn follows Windows naming convention. 300 301`is_debug = true | false` 302 303Toggles Debug (default) / Release mode. This affects, among other things: 304(i) the `-g` compiler flag; (ii) setting/unsetting `-DNDEBUG`; (iii) turning 305on/off `DCHECK` and `DLOG`. 306Note that debug builds of Perfetto are sensibly slower than release versions. We 307strongly encourage using debug builds only for local development. 308 309`is_clang = true | false` 310 311Use Clang (default: true) or GCC (false). 312On Linux, by default it uses the self-hosted clang (see `is_hermetic_clang`). 313On Android, by default it uses clang from the NDK (in `buildtools/ndk`). 314On Mac, by default it uses the system version of clang (requires Xcode). 315See also the [custom toolchain](#custom-toolchain) section below. 316 317`is_hermetic_clang = true | false` 318 319Use bundled toolchain from `buildtools/` rather than system-wide one. 320 321`cc = "gcc" / cxx = "g++"` 322 323Uses a different compiler binary (default: autodetected depending on is_clang). 324See also the [custom toolchain](#custom-toolchain) section below. 325 326`cc_wrapper = "tool_name"` 327 328Prepends all build commands with a wrapper command. Using `"ccache"` here 329enables the [ccache](https://github.com/ccache/ccache) caching compiler, 330which can considerably speed up repeat builds. 331 332`is_asan = true` 333 334Enables [Address Sanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer) 335 336`is_lsan = true` 337 338Enables [Leak Sanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer) 339(Linux/Mac only) 340 341`is_msan = true` 342 343Enables [Memory Sanitizer](https://github.com/google/sanitizers/wiki/MemorySanitizer) 344(Linux only) 345 346`is_tsan = true` 347 348Enables [Thread Sanitizer](https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual) 349(Linux/Mac only) 350 351`is_ubsan = true` 352 353Enables [Undefined Behavior Sanitizer](https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html) 354 355### {#custom-toolchain} Using custom toolchains and CC / CXX / CFLAGS env vars 356 357When building Perfetto as part of some other build environment it might be 358necessary to switch off all the built-in toolchain-related path-guessing scripts 359and manually specify the path of the toolchains. 360 361```python 362# Disable the scripts that guess the path of the toolchain. 363is_system_compiler = true 364 365ar = "/path/to/ar" 366cc = "/path/to/gcc-like-compiler" 367cxx = "/path/to/g++-like-compiler" 368linker = "" # This is passed to -fuse-ld=... 369``` 370 371If you are using a build system that keeps the toolchain settings in 372environment variables, you can set: 373 374```python 375is_system_compiler = true 376ar="${AR}" 377cc="${CC}" 378cxx="${CXX}" 379``` 380 381`is_system_compiler = true` can be used also for cross-compilation. 382In case of cross-compilation, the GN variables have the following semantic: 383`ar`, `cc`, `cxx`, `linker` refer to the _host_ toolchain (sometimes also called 384_build_ toolchain). This toolchain is used to build: (i) auxiliary tools 385(e.g. the `traceconv` conversion util) and (ii) executable artifacts that are 386used during the rest of the build process for the target (e.g., the `protoc` 387compiler or the `protozero_plugin` protoc compiler plugin). 388 389The cross-toolchain used to build the artifacts that run on the device is 390prefixed by `target_`: `target_ar`, `target_cc`, `target_cxx`, `target_linker`. 391 392```python 393# Cross compilation kicks in when at least one of these three variables is set 394# to a value != than the host defaults. 395 396target_cpu = "x86" | "x64" | "arm" | "arm64" 397target_os = "linux" | "android" 398target_triplet = "arm-linux-gnueabi" | "x86_64-linux-gnu" | ... 399``` 400 401When integrating with GNU Makefile cross-toolchains build environments, a 402typical mapping of the corresponding environment variables is: 403 404```python 405ar="${BUILD_AR}" 406cc="${BUILD_CC}" 407cxx="${BUILD_CXX}" 408target_ar="${AR}" 409target_cc="${CC}" 410target_cxx="${CXX}" 411``` 412 413It is possible to extend the set of `CFLAGS` and `CXXFLAGS` through the 414`extra_xxxflags` GN variables as follows. The extra flags are always appended 415(hence, take precedence) to the set of flags that the GN build files generate. 416 417```python 418# These apply both to host and target toolchain. 419extra_cflags="${CFLAGS}" 420extra_cxxflags="${CXXFLAGS}" 421extra_ldflags="${LDFLAGS}" 422 423# These apply only to the host toolchain. 424extra_host_cflags="${BUILD_CFLAGS}" 425extra_host_cxxflags="${BUILD_CXXFLAGS}" 426extra_host_ldflags="${BUILD_LDFLAGS}" 427 428# These apply only to the target toolchain. 429extra_target_cflags="${CFLAGS}" 430extra_target_cxxflags="${CXXFLAGS} ${debug_flags}" 431extra_target_ldflags="${LDFLAGS}" 432``` 433 434[gn-quickstart]: https://gn.googlesource.com/gn/+/master/docs/quick_start.md 435 436## IDE setup 437 438Use a following command in the checkout directory in order to generate the 439compilation database file: 440 441```bash 442tools/gn gen out/default --export-compile-commands 443``` 444 445After generating, it can be used in CLion (File -> Open -> Open As Project), 446Visual Studio Code with C/C++ extension and any other tool and editor that 447supports the compilation database format. 448 449#### Useful extensions 450 451If you are using VS Code we suggest the following extensions: 452 453- [Clang-Format](https://marketplace.visualstudio.com/items?itemName=xaver.clang-format) 454- [C/C++](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) 455- [clangd](https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd) 456- [Native Debug](https://marketplace.visualstudio.com/items?itemName=webfreak.debug) 457- [GNFormat](https://marketplace.visualstudio.com/items?itemName=persidskiy.vscode-gnformat) 458- [ESlint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) 459- [markdownlint](https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint) 460 461#### Useful settings 462 463In `.vscode/settings.json`: 464 465```json 466{ 467 "C_Cpp.clang_format_path": "${workspaceRoot}/buildtools/mac/clang-format", 468 "C_Cpp.clang_format_sortIncludes": true, 469 "files.exclude": { 470 "out/*/obj": true, 471 "out/*/gen": true, 472 }, 473 "clangd.arguments": [ 474 "--compile-commands-dir=${workspaceFolder}/out/mac_debug", 475 "--completion-style=detailed", 476 "--header-insertion=never" 477 ], 478} 479``` 480 481Replace `/mac/` with `/linux64/` on Linux. 482 483### Debugging with VSCode 484 485Edit `.vscode/launch.json`: 486 487```json 488{ 489 "version": "0.2.0", 490 "configurations": [ 491 { 492 "request": "launch", 493 "type": "cppdbg", 494 "name": "Perfetto unittests", 495 "program": "${workspaceRoot}/out/mac_debug/perfetto_unittests", 496 "args": ["--gtest_filter=TracingServiceImplTest.StopTracingTriggerRingBuffer"], 497 "cwd": "${workspaceFolder}/out/mac_debug", 498 "MIMode": "lldb", 499 }, 500 ] 501} 502``` 503 504Then open the command palette `Meta`+`Shift`+`P` -> `Debug: Start debugging`. 505