• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  traceconv                # 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
144UI unit tests are located next to the functionality being tested, and have
145`_unittest.ts` or `_jsdomtest.ts` suffixes. The following command runs all unit
146tests:
147
148```bash
149ui/run-unittests
150```
151
152This command will perform the build first; which is not necessary if you
153already have a development server running. In this case, to avoid interference
154with the rebuild done by development server and to get the results faster, you
155can use
156
157```bash
158ui/run-unittests --no-build
159```
160
161to skip the build steps.
162
163Script `ui/run-unittests` also supports `--watch` parameter, which would
164restart the testing when the underlying source files are changed. This can be
165used in conjunction with `--no-build`, and on its own as well.
166
167## Build files
168
169The source of truth of our build file is in the BUILD.gn files, which are based
170on [GN][gn-quickstart].
171The Android build file ([Android.bp](/Android.bp)) is autogenerated from the GN
172files through `tools/gen_android_bp`, which needs to be invoked whenever a
173change touches GN files or introduces new ones.
174Likewise, the Bazel build file ([BUILD](/BUILD)) is autogenerated through the
175`tools/gen_bazel` script.
176
177A presubmit check checks that the Android.bp is consistent with GN files when
178submitting a CL through `git cl upload`.
179
180The generator has a list of root targets that will be translated into the
181Android.bp file. If you are adding a new target, add a new entry to the
182`default_targets` variable in [`tools/gen_android_bp`](/tools/gen_android_bp).
183
184## Supported platforms
185
186**Linux desktop** (Debian Testing/Rodete)
187
188- Hermetic clang + libcxx toolchain (both following chromium's revisions)
189- GCC-7 and libstdc++ 6
190- Cross-compiling for arm and arm64 (more below).
191
192**Android**
193
194- Android's NDK r15c (using NDK's libcxx)
195- AOSP's in-tree clang (using in-tree libcxx)
196
197**Mac**
198
199- XCode 9 / clang (maintained best-effort).
200
201**Windows**
202
203- Windows 10 with either MSVC 2019 or clang-cl (maintained best-effort).
204
205### Building on Windows
206
207Building on Windows is possible using both the MSVC 2019 compiler (you don't
208need the full IDE, just the build tools) or the LLVM clang-cl compiler.
209
210The Windows support in standalone builds has been introduced in v16 by
211[r.android.com/1711913](https://r.android.com/1711913).
212
213clang-cl support is more stable because that build configuration is actively
214covered by the Chromium project (Perfetto rolls into chromium and underpins
215chrome://tracing). The MSVC build is maintained best-effort.
216
217The following targets are supported on Windows:
218
219- `trace_processor_shell`: the trace importer and SQL query engine.
220- `traceconv`: the trace conversion tool.
221- `traced` and `perfetto`: the tracing service and cmdline client. They use an
222  alternative implementation of the [inter-process tracing protocol](/docs/design-docs/api-and-abi.md#tracing-protocol-abi)
223  based on a TCP socket and named shared memory. This configuration is only for
224  testing / benchmarks and is not shipped in production.
225  Googlers: see [go/perfetto-win](http://go/perfetto-win) for details.
226- `perfetto_unittests` / `perfetto_integrationtests`: although they support only
227  the subset of code that is supported on Windows (e.g. no ftrace).
228
229It is NOT possible to build the Perfetto UI from Windows.
230
231#### Prerequisites
232
233You need all of these both for MSVC and clang-cl:
234
235- [Build Tools for Visual Studio 2019](https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019)
236- [Windows 10 SDK](https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk/)
237- [Python 3](https://www.python.org/downloads/windows/)
238
239The [`win_find_msvc.py`](/gn/standalone/toolchain/win_find_msvc.py) script will
240locate the higest version numbers available from
241`C:\Program Files (x86)\Windows Kits\10` and
242`C:\Program Files (x86)\Microsoft Visual Studio\2019`.
243
244#### Pull dependent libraries and toolchains
245
246```bash
247# This will download also the LLVM clang-cl prebuilt used by chromium.
248python3 tools/install-build-deps
249```
250
251#### Generate build files
252
253```bash
254python3 tools/gn gen out/win
255```
256
257In the editor type:
258
259```bash
260is_debug = true | false
261
262is_clang = true  # Will use the hermetic clang-cl toolchain.
263# or
264is_clang = false  # Will use MSVC 2019.
265```
266
267#### Build
268
269```bash
270python3 tools/ninja -C out/win perfetto traced trace_processor_shell
271```
272
273### Cross-compiling for Linux ARM/64
274
275When cross-compiling for Linux you will need a sysroot. You have two options:
276
277#### 1. Use the built-in sysroots based on Debian Sid
278
279```bash
280tools/install-build-deps --linux-arm
281```
282
283Then set the following GN args:
284
285```python
286target_os = "linux"
287target_cpu = "arm"
288# or
289target_cpu = "arm64"
290```
291
292#### 2. Use your own sysroot
293
294In this case you need to manually specify the sysroot location and the
295toolchain prefix triplet to use.
296
297```python
298target_os = "linux"
299target_sysroot = "/path/to/sysroot"
300target_triplet = "aarch64-linux-gnu"  # Or any other supported triplet.
301```
302
303For more details see the [Using cutom toolchains](#custom-toolchain) section
304below.
305
306## Build configurations
307
308TIP: `tools/setup_all_configs.py` can be used to generate out/XXX folders for
309most of the supported configurations.
310
311The following [GN args][gn-quickstart] are supported:
312
313`target_os = "android" | "linux" | "mac"`:
314
315Defaults to the current host, set "android" to build for Android.
316
317`target_cpu = "arm" | "arm64" | "x64"`
318
319Defaults to `"arm"` when `target_os` == `"android"`, `"x64"` when targeting the
320host. 32-bit host builds are not supported.
321Note: x64 here really means x86_64. This is to keep it consistent with
322Chromium's choice, which in turn follows Windows naming convention.
323
324`is_debug = true | false`
325
326Toggles Debug (default) / Release mode. This affects, among other things:
327(i) the `-g` compiler flag; (ii) setting/unsetting `-DNDEBUG`; (iii) turning
328on/off `DCHECK` and `DLOG`.
329Note that debug builds of Perfetto are sensibly slower than release versions. We
330strongly encourage using debug builds only for local development.
331
332`is_clang = true | false`
333
334Use Clang (default: true) or GCC (false).
335On Linux, by default it uses the self-hosted clang (see `is_hermetic_clang`).
336On Android, by default it uses clang from the NDK (in `buildtools/ndk`).
337On Mac, by default it uses the system version of clang (requires Xcode).
338See also the [custom toolchain](#custom-toolchain) section below.
339
340`is_hermetic_clang = true | false`
341
342Use bundled toolchain from `buildtools/` rather than system-wide one.
343
344`non_hermetic_clang_stdlib = libc++ | libstdc++`
345
346If `is_hermetic_clang` is `false`, sets the `-stdlib` flag for clang
347invocations. `libstdc++` is default on Linux hosts and `libc++` is
348default everywhere else.
349
350`cc = "gcc" / cxx = "g++"`
351
352Uses a different compiler binary (default: autodetected depending on is_clang).
353See also the [custom toolchain](#custom-toolchain) section below.
354
355`cc_wrapper = "tool_name"`
356
357Prepends all build commands with a wrapper command. Using `"ccache"` here
358enables the [ccache](https://github.com/ccache/ccache) caching compiler,
359which can considerably speed up repeat builds.
360
361`is_asan = true`
362
363Enables [Address Sanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer)
364
365`is_lsan = true`
366
367Enables [Leak Sanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer)
368(Linux/Mac only)
369
370`is_msan = true`
371
372Enables [Memory Sanitizer](https://github.com/google/sanitizers/wiki/MemorySanitizer)
373(Linux only)
374
375`is_tsan = true`
376
377Enables [Thread Sanitizer](https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual)
378(Linux/Mac only)
379
380`is_ubsan = true`
381
382Enables [Undefined Behavior Sanitizer](https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html)
383
384### {#custom-toolchain} Using custom toolchains and CC / CXX / CFLAGS env vars
385
386When building Perfetto as part of some other build environment it might be
387necessary to switch off all the built-in toolchain-related path-guessing scripts
388and manually specify the path of the toolchains.
389
390```python
391# Disable the scripts that guess the path of the toolchain.
392is_system_compiler = true
393
394ar = "/path/to/ar"
395cc = "/path/to/gcc-like-compiler"
396cxx = "/path/to/g++-like-compiler"
397linker = ""  # This is passed to -fuse-ld=...
398```
399
400If you are using a build system that keeps the toolchain settings in
401environment variables, you can set:
402
403```python
404is_system_compiler = true
405ar="${AR}"
406cc="${CC}"
407cxx="${CXX}"
408```
409
410`is_system_compiler = true` can be used also for cross-compilation.
411In case of cross-compilation, the GN variables have the following semantic:
412`ar`, `cc`, `cxx`, `linker` refer to the _host_ toolchain (sometimes also called
413_build_ toolchain). This toolchain is used to build: (i) auxiliary tools
414(e.g. the `traceconv` conversion util) and (ii) executable artifacts that are
415used during the rest of the build process for the target (e.g., the `protoc`
416compiler or the `protozero_plugin` protoc compiler plugin).
417
418The cross-toolchain used to build the artifacts that run on the device is
419prefixed by `target_`: `target_ar`, `target_cc`, `target_cxx`, `target_linker`.
420
421```python
422# Cross compilation kicks in when at least one of these three variables is set
423# to a value != than the host defaults.
424
425target_cpu = "x86" | "x64" | "arm" | "arm64"
426target_os = "linux" | "android"
427target_triplet =  "arm-linux-gnueabi" | "x86_64-linux-gnu" | ...
428```
429
430When integrating with GNU Makefile cross-toolchains build environments, a
431typical mapping of the corresponding environment variables is:
432
433```python
434ar="${BUILD_AR}"
435cc="${BUILD_CC}"
436cxx="${BUILD_CXX}"
437target_ar="${AR}"
438target_cc="${CC}"
439target_cxx="${CXX}"
440```
441
442It is possible to extend the set of `CFLAGS` and `CXXFLAGS` through the
443`extra_xxxflags` GN variables as follows. The extra flags are always appended
444(hence, take precedence) to the set of flags that the GN build files generate.
445
446```python
447# These apply both to host and target toolchain.
448extra_cflags="${CFLAGS}"
449extra_cxxflags="${CXXFLAGS}"
450extra_ldflags="${LDFLAGS}"
451
452# These apply only to the host toolchain.
453extra_host_cflags="${BUILD_CFLAGS}"
454extra_host_cxxflags="${BUILD_CXXFLAGS}"
455extra_host_ldflags="${BUILD_LDFLAGS}"
456
457# These apply only to the target toolchain.
458extra_target_cflags="${CFLAGS}"
459extra_target_cxxflags="${CXXFLAGS} ${debug_flags}"
460extra_target_ldflags="${LDFLAGS}"
461```
462
463[gn-quickstart]: https://gn.googlesource.com/gn/+/master/docs/quick_start.md
464
465## IDE setup
466
467Use a following command in the checkout directory in order to generate the
468compilation database file:
469
470```bash
471tools/gn gen out/default --export-compile-commands
472```
473
474After generating, it can be used in CLion (File -> Open -> Open As Project),
475Visual Studio Code with C/C++ extension and any other tool and editor that
476supports the compilation database format.
477
478#### Useful extensions
479
480If you are using VS Code we suggest the following extensions:
481
482- [Clang-Format](https://marketplace.visualstudio.com/items?itemName=xaver.clang-format)
483- [C/C++](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)
484- [clangd](https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd)
485- [Native Debug](https://marketplace.visualstudio.com/items?itemName=webfreak.debug)
486- [GNFormat](https://marketplace.visualstudio.com/items?itemName=persidskiy.vscode-gnformat)
487- [ESlint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint)
488- [markdownlint](https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint)
489
490#### Useful settings
491
492In `.vscode/settings.json`:
493
494```json
495{
496  "C_Cpp.clang_format_path": "${workspaceRoot}/buildtools/mac/clang-format",
497  "C_Cpp.clang_format_sortIncludes": true,
498  "files.exclude": {
499    "out/*/obj": true,
500    "out/*/gen": true,
501  },
502  "clangd.arguments": [
503    "--compile-commands-dir=${workspaceFolder}/out/mac_debug",
504    "--completion-style=detailed",
505    "--header-insertion=never"
506  ],
507}
508```
509
510Replace `/mac/` with `/linux64/` on Linux.
511
512### Debugging with VSCode
513
514Edit `.vscode/launch.json`:
515
516```json
517{
518  "version": "0.2.0",
519  "configurations": [
520    {
521      "request": "launch",
522      "type": "cppdbg",
523      "name": "Perfetto unittests",
524      "program": "${workspaceRoot}/out/mac_debug/perfetto_unittests",
525      "args": ["--gtest_filter=TracingServiceImplTest.StopTracingTriggerRingBuffer"],
526      "cwd": "${workspaceFolder}/out/mac_debug",
527      "MIMode": "lldb",
528    },
529  ]
530}
531```
532
533Then open the command palette `Meta`+`Shift`+`P` -> `Debug: Start debugging`.
534