xref: /aosp_15_r20/external/skia/site/docs/user/build.md (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1---
2title: 'How to build Skia'
3linkTitle: 'How to build Skia'
4
5weight: 20
6---
7
8Make sure you have first followed the
9[instructions to download Skia](../download).
10
11Skia uses [GN](https://chromium.googlesource.com/chromium/src/tools/gn/) to
12configure its builds.
13
14## `is_official_build` and Third-party Dependencies
15
16Most users of Skia should set `is_official_build=true`, and most developers
17should leave it to its `false` default.
18
19This mode configures Skia in a way that's suitable to ship: an optimized build
20with no debug symbols, dynamically linked against its third-party dependencies
21using the ordinary library search path.
22
23In contrast, the developer-oriented default is an unoptimized build with full
24debug symbols and all third-party dependencies built from source and embedded
25into libskia. This is how we do all our manual and automated testing.
26
27Skia offers several features that make use of third-party libraries, like
28libpng, libwebp, or libjpeg-turbo to decode images, or ICU and sftnly to subset
29fonts. All these third-party dependencies are optional and can be controlled by
30a GN argument that looks something like `skia_use_foo` for appropriate `foo`.
31
32If `skia_use_foo` is enabled, enabling `skia_use_system_foo` will build and link
33Skia against the headers and libraries found on the system paths.
34`is_official_build=true` enables all `skia_use_system_foo` by default. You can
35use `extra_cflags` and `extra_ldflags` to add include or library paths if
36needed.
37
38## Supported and Preferred Compilers
39
40While Skia should compile with GCC, MSVC, and other compilers, a number of
41routines in Skia's software backend have been written to run fastest when
42compiled with Clang. If you depend on software rasterization, image decoding, or
43color space conversion and compile Skia with a compiler other than Clang, you
44will see dramatically worse performance. This choice was only a matter of
45prioritization; there is nothing fundamentally wrong with non-Clang compilers.
46So if this is a serious issue for you, please let us know on the mailing list.
47
48Skia makes use of C++17 language features (compiles with `-std=c++17` flag) and
49thus requires a C++17 compatible compiler. Clang 5 and later implement all of
50the features of the c++17 standard. Older compilers that lack C++17 support may
51produce non-obvious compilation errors. You can configure your build to use
52specific executables for `cc` and `cxx` invocations using e.g.
53`--args='cc="clang" cxx="clang++"'` GN build arguments, as illustrated in
54[Quickstart](#quickstart). This can be useful for building Skia without needing to
55modify your machine's default compiler toolchain.
56
57If you do not specify `cc` and `cxx` in your gn arguments, Skia will default to
58`cc` and `c++`. This is often GCC by default on many platforms, not Clang.
59
60## Quickstart
61
62Run `gn gen` to generate your build files. As arguments to `gn gen`, pass a name
63for your build directory, and optionally `--args=` to configure the build type.
64
65To build Skia as a static library in a build directory named `out/Static`:
66
67```
68bin/gn gen out/Static --args='is_official_build=true'
69```
70
71To build Skia as a shared library (DLL) in a build directory named `out/Shared`:
72
73```
74bin/gn gen out/Shared --args='is_official_build=true is_component_build=true'
75```
76
77If you find that you don't have `bin/gn`, make sure you've run:
78
79```
80python3 tools/git-sync-deps
81```
82
83For a list of available build arguments, take a look at `gn/skia.gni`, or run:
84
85```
86bin/gn args out/Debug --list
87```
88
89GN allows multiple build folders to coexist; each build can be configured
90separately as desired. For example:
91
92```
93bin/gn gen out/Debug
94bin/gn gen out/Release  --args='is_debug=false'
95bin/gn gen out/Clang    --args='cc="clang" cxx="clang++"'
96bin/gn gen out/Cached   --args='cc_wrapper="ccache"'
97bin/gn gen out/RTTI     --args='extra_cflags_cc=["-frtti"]'
98```
99
100Once you have generated your build files, run Ninja to compile and link all of Skia:
101
102```
103ninja -C out/Static
104```
105
106To avoid building everything, include the target or targets after the ninja command. For example:
107
108```
109ninja -C out/Debug skia
110ninja -C out/Debug viewer dm
111```
112
113Not all targets are available for all sets of build arguments. For a list of all available targets
114for a given build directory, run:
115
116```
117gn ls out/Debug
118```
119
120If some header files are missing, install the corresponding dependencies:
121
122```
123tools/install_dependencies.sh
124```
125
126To pull new changes and rebuild:
127
128```
129git pull
130python3 tools/git-sync-deps
131ninja -C out/Static
132```
133
134## Android
135
136To build Skia for Android you need a recent version of
137[Java](https://www.oracle.com/java/technologies/downloads/) and a recent
138[Android NDK](https://developer.android.com/ndk/index.html).
139
140If you do not have an NDK and have access to CIPD, you can use one of these
141commands to fetch the NDK our bots use:
142
143```
144./bin/fetch-sk
145./bin/sk asset download android_ndk_linux /tmp/ndk     # on Linux
146./bin/sk asset download android_ndk_darwin /tmp/ndk    # on Mac
147./bin/sk.exe asset download android_ndk_windows C:/ndk # on Windows
148```
149
150When generating your GN build files, pass the path to your `ndk` and your
151desired `target_cpu`:
152
153```
154bin/gn gen out/arm   --args='ndk="/tmp/ndk" target_cpu="arm"'
155bin/gn gen out/arm64 --args='ndk="/tmp/ndk" target_cpu="arm64"'
156bin/gn gen out/x64   --args='ndk="/tmp/ndk" target_cpu="x64"'
157bin/gn gen out/x86   --args='ndk="/tmp/ndk" target_cpu="x86"'
158```
159
160Other arguments like `is_debug` and `is_component_build` continue to work.
161Tweaking `ndk_api` gives you access to newer Android features like Vulkan.
162
163To test on an Android device, push the binary and `resources` over, and run it
164as normal. You may find `bin/droid` convenient.
165
166```
167ninja -C out/arm64
168adb push out/arm64/dm /data/local/tmp
169adb push resources /data/local/tmp
170adb shell "cd /data/local/tmp; ./dm --src gm --config gl"
171```
172
173## ChromeOS
174
175To cross-compile Skia for arm ChromeOS devices the following is needed:
176
177- Clang 4 or newer
178- An armhf sysroot
179- The (E)GL lib files on the arm chromebook to link against.
180
181To compile Skia for an x86 ChromeOS device, one only needs Clang and the lib
182files.
183
184If you have access to CIPD, you can fetch all of these as follows:
185
186```
187./bin/sk asset download clang_linux /opt/clang
188./bin/sk asset download armhf_sysroot /opt/armhf_sysroot
189./bin/sk asset download chromebook_arm_gles /opt/chromebook_arm_gles
190./bin/sk asset download chromebook_x86_64_gles /opt/chromebook_x86_64_gles
191```
192
193If you don't have authorization to use those assets, then see the README.md
194files for
195[armhf_sysroot](https://skia.googlesource.com/skia/+/main/infra/bots/assets/armhf_sysroot/README.md),
196[chromebook_arm_gles](https://skia.googlesource.com/skia/+/main/infra/bots/assets/chromebook_arm_gles/README.md),
197and
198[chromebook_x86_64_gles](https://skia.googlesource.com/skia/+/main/infra/bots/assets/chromebook_x86_64_gles/README.md)
199for instructions on creating those assets.
200
201Once those files are in place, generate the GN args that resemble the following:
202
203```
204#ARM
205cc= "/opt/clang/bin/clang"
206cxx = "/opt/clang/bin/clang++"
207
208extra_asmflags = [
209    "--target=armv7a-linux-gnueabihf",
210    "--sysroot=/opt/armhf_sysroot/",
211    "-march=armv7-a",
212    "-mfpu=neon",
213    "-mthumb",
214]
215extra_cflags=[
216    "--target=armv7a-linux-gnueabihf",
217    "--sysroot=/opt/armhf_sysroot",
218    "-I/opt/chromebook_arm_gles/include",
219    "-I/opt/armhf_sysroot/include/",
220    "-I/opt/armhf_sysroot/include/c++/4.8.4/",
221    "-I/opt/armhf_sysroot/include/c++/4.8.4/arm-linux-gnueabihf/",
222    "-DMESA_EGL_NO_X11_HEADERS",
223    "-funwind-tables",
224]
225extra_ldflags=[
226    "--sysroot=/opt/armhf_sysroot",
227    "-B/opt/armhf_sysroot/bin",
228    "-B/opt/armhf_sysroot/gcc-cross",
229    "-L/opt/armhf_sysroot/gcc-cross",
230    "-L/opt/armhf_sysroot/lib",
231    "-L/opt/chromebook_arm_gles/lib",
232    "--target=armv7a-linux-gnueabihf",
233]
234target_cpu="arm"
235skia_use_fontconfig = false
236skia_use_system_freetype2 = false
237skia_use_egl = true
238
239
240# x86_64
241cc= "/opt/clang/bin/clang"
242cxx = "/opt/clang/bin/clang++"
243extra_cflags=[
244    "-I/opt/clang/include/c++/v1/",
245    "-I/opt/chromebook_x86_64_gles/include",
246    "-DMESA_EGL_NO_X11_HEADERS",
247    "-DEGL_NO_IMAGE_EXTERNAL",
248]
249extra_ldflags=[
250    "-stdlib=libc++",
251    "-fuse-ld=lld",
252    "-L/opt/chromebook_x86_64_gles/lib",
253]
254target_cpu="x64"
255skia_use_fontconfig = false
256skia_use_system_freetype2 = false
257skia_use_egl = true
258```
259
260Compile dm (or another executable of your choice) with ninja, as per usual.
261
262Push the binary to a chromebook via ssh and
263[run dm as normal](/docs/dev/testing/tests) using the gles GPU config.
264
265Most chromebooks by default have their home directory partition marked as
266noexec. To avoid "permission denied" errors, remember to run something like:
267
268```
269sudo mount -i -o remount,exec /home/chronos
270```
271
272## Mac
273
274Mac users may want to pass `--ide=xcode` to `bin/gn gen` to generate an Xcode
275project.
276
277Mac GN builds assume an Intel CPU by default. If you are building for Apple
278Silicon (M1 and newer) instead, add a gn arg to set `target_cpu="arm64"`:
279
280```
281bin/gn gen out/AppleSilicon --args='target_cpu="arm64"'
282```
283
284Googlers should see [go/skia-corp-xcode](http://go/skia-corp-xcode) for
285instructions on setting up Xcode on a corp machine.
286
287### Python
288
289The version of Python supplied by Apple is a few versions out of date,
290and it is known to interact poorly with our build system. We recommend
291installing the latest official version of Python from
292https://www.python.org/downloads/. Then run
293"Applications/Python 3.11/Install Certificates.command".
294
295## iOS
296
297Run GN to generate your build files. Set `target_os="ios"` to build for iOS.
298This defaults to `target_cpu="arm64"`. To use the iOS simulator, set
299`ios_use_simulator=true` and set `target_cpu` to your Mac's architecture.
300On an Intel Mac, setting `target_cpu="x64"` alone will also target the iOS
301simulator.
302
303```
304bin/gn gen out/ios64  --args='target_os="ios"'
305bin/gn gen out/ios32  --args='target_os="ios" target_cpu="arm"'
306bin/gn gen out/iossim-apple --args='target_os="ios" target_cpu="arm64" ios_use_simulator=true'
307bin/gn gen out/iossim-intel --args='target_os="ios" target_cpu="x64"'
308```
309
310By default this will also package (and for non-simulator devices, sign) iOS test binaries.
311If you wish to skip signing (for testing compilation alone, for example), you can disable it by
312setting `skia_ios_use_signing` to `false`.
313
314When signing, the build defaults to a Google signing identity and provisioning profile.
315To use a different one
316set the GN args `skia_ios_identity` to match your code signing identity and
317`skia_ios_profile` to the name of your provisioning profile, e.g.
318
319```
320skia_ios_identity=".*Jane Doe.*"
321skia_ios_profile="iPad Profile"`
322```
323
324A list of identities can be found by typing `security find-identity` on the
325command line. The name of the provisioning profile should be available on the
326Apple Developer site. Alternatively, you can examine the installed provisioning profile files in the Finder
327by going to `~/Library/MobileDevice/Provisioning Profiles`, selecting a `.mobileprovision` file,
328and hitting space. The value of `skia_ios_profile` can either be the string
329given at the top of that file or on the Developer site, or the absolute path
330to the file.
331
332If you find yourself missing a Google signing identity or provisioning profile,
333you'll want to have a read through go/appledev and go/ios-signing.
334
335For signed packages `ios-deploy` makes installing and running them on a device
336easy:
337
338```
339ios-deploy -b out/Debug/dm.app -d --args "--match foo"
340```
341
342If you wish to deploy through Xcode you can generate a project by passing `--ide=xcode` to
343`bin/gn gen`. If you are using Xcode version 10 or later, you may need to go to
344`Project Settings...` and verify that `Build System:` is set to
345`Legacy Build System`.
346
347Deploying to a device with an OS older than the current SDK can be done by
348setting the `ios_min_target` arg:
349
350```
351ios_min_target = "<major>.<minor>"
352```
353
354where `<major>.<minor>` is the iOS version on the device, e.g., 12.0 or 11.4.
355
356## Windows
357
358Skia can build on Windows with Visual Studio 2017 or 2019. If GN is unable to
359locate either of those, it will print an error message. In that case, you can
360pass your `VC` path to GN via `win_vc`.
361
362Skia can be compiled with the free
363[Build Tools for Visual Studio 2017 or 2019](https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2019).
364
365The bots use a packaged 2019 toolchain, which Googlers can download like this:
366
367```
368./bin/sk.exe asset download win_toolchain C:/toolchain
369```
370
371You can then pass the VC and SDK paths to GN by setting your GN args:
372
373```
374win_vc = "C:\toolchain\VC"
375win_sdk = "C:\toolchain\win_sdk"
376```
377
378This toolchain is the only way we support 32-bit builds, by also setting
379`target_cpu="x86"`.
380
381The Skia build assumes that the PATHEXT environment variable contains ".EXE".
382
383### **Highly Recommended**: Build with clang-cl
384
385Skia uses generated code that is only optimized when Skia is built with clang.
386Other compilers get generic unoptimized code.
387
388Setting the `cc` and `cxx` gn args is _not_ sufficient to build with clang-cl.
389These variables are ignored on Windows. Instead set the variable `clang_win` to
390your LLVM installation directory. If you installed the prebuilt LLVM downloaded
391from [here](https://releases.llvm.org/download.html 'LLVM Download') in the
392default location, that would be:
393
394```
395clang_win = "C:\Program Files\LLVM"
396```
397
398Follow the standard Windows path specification and not MinGW convention (e.g.
399`C:\Program Files\LLVM` not ~~`/c/Program Files/LLVM`~~).
400
401If you will be compiling the rest of your program with a compiler other than
402Clang, add this GN argument as well:
403
404```
405is_trivial_abi = false
406```
407
408### Visual Studio Solutions
409
410If you use Visual Studio, you may want to pass `--ide=vs` to `bin/gn gen` to
411generate `all.sln`. That solution will exist within the GN directory for the
412specific configuration, and will only build/run that configuration.
413
414If you want a Visual Studio Solution that supports multiple GN configurations,
415there is a helper script. It requires that all of your GN directories be inside
416the `out` directory. First, create all of your GN configurations as usual. Pass
417`--ide=vs` when running `bin/gn gen` for each one. Then:
418
419```
420python3 gn/gn_meta_sln.py
421```
422
423This creates a new dedicated output directory and solution file
424`out/sln/skia.sln`. It has one solution configuration for each GN configuration,
425and supports building and running any of them. It also adjusts syntax
426highlighting of inactive code blocks based on preprocessor definitions from the
427selected solution configuration.
428
429## Windows ARM64
430
431There is early, experimental support for
432[Windows 10 on ARM](https://docs.microsoft.com/en-us/windows/arm/). This
433currently requires (a recent version of) MSVC, and the
434`Visual C++ compilers and libraries for ARM64` individual component in the
435Visual Studio Installer. For Googlers, the win_toolchain asset includes the
436ARM64 compiler.
437
438To use that toolchain, set the `target_cpu` GN argument to `"arm64"`. Note that
439OpenGL is not supported by Windows 10 on ARM, so Skia's GL backends are stubbed
440out, and will not work. ANGLE is supported:
441
442```
443bin/gn gen out/win-arm64 --args='target_cpu="arm64" skia_use_angle=true'
444```
445
446This will produce a build of Skia that can use the software or ANGLE backends,
447in DM. Viewer only works when launched with `--backend angle`, because the
448software backend tries to use OpenGL to display the window contents.
449
450## CMake
451
452We have added a GN-to-CMake translator mainly for use with IDEs that like CMake
453project descriptions. This is not meant for any purpose beyond development.
454
455```
456bin/gn gen out/config --ide=json --json-ide-script=../../gn/gn_to_cmake.py
457```
458