• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1How to build Skia
2=================
3
4Make sure you have first followed the [instructions to download
5Skia](./download).
6
7Skia uses [GN](https://chromium.googlesource.com/chromium/src/tools/gn/) to
8configure its builds.
9
10  - [`is_official_build` and Third-party Dependencies](#third-party)
11  - [A note on software backend performance](#performance)
12  - [Quickstart](#quick)
13  - [Android](#android)
14  - [ChromeOS](#cros)
15  - [Mac](#macos)
16  - [iOS](#ios)
17  - [Windows](#windows)
18  - [Windows ARM64](#win-arm64)
19  - [CMake](#cmake)
20
21<span id="third-party">`is_official_build` and Third-party Dependencies</span>
22------------------------------------------------------------------------------
23
24Most users of Skia should set `is_official_build=true`, and most developers
25should leave it to its `false` default.
26
27This mode configures Skia in a way that's suitable to ship: an optimized build
28with no debug symbols, dynamically linked against its third-party dependencies
29using the ordinary library search path.
30
31In contrast, the developer-oriented default is an unoptimized build with full
32debug symbols and all third-party dependencies built from source and embedded
33into libskia.  This is how we do all our manual and automated testing.
34
35Skia offers several features that make use of third-party libraries, like
36libpng, libwebp, or libjpeg-turbo to decode images, or ICU and sftnly to subset
37fonts.  All these third-party dependencies are optional and can be controlled
38by a GN argument that looks something like `skia_use_foo` for appropriate
39`foo`.
40
41If `skia_use_foo` is enabled, enabling `skia_use_system_foo` will build and
42link Skia against the headers and libaries found on the system paths.
43`is_official_build=true` enables all `skia_use_system_foo` by default.  You can
44use `extra_cflags` and `extra_ldflags` to add include or library paths if
45needed.
46
47<span id="performance">A note on software backend performance</span>
48--------------------------------------------------------------------
49
50A number of routines in Skia's software backend have been written to run
51fastest when compiled by Clang.  If you depend on software rasterization, image
52decoding, or color space conversion and compile Skia with GCC, MSVC or another
53compiler, you will see dramatically worse performance than if you use Clang.
54
55This choice was only a matter of prioritization; there is nothing fundamentally
56wrong with non-Clang compilers.  So if this is a serious issue for you, please
57let us know on the mailing list.
58
59<span id="quick">Quickstart</span>
60----------------------------------
61
62Run GN to generate your build files.
63
64    bin/gn gen out/Static --args='is_official_build=true'
65    bin/gn gen out/Shared --args='is_official_build=true is_component_build=true'
66
67If you find you don't have `bin/gn`, make sure you've run
68
69    python2 tools/git-sync-deps
70
71GN allows fine-grained settings for developers and special situations.
72
73    bin/gn gen out/Debug
74    bin/gn gen out/Release  --args='is_debug=false'
75    bin/gn gen out/Clang    --args='cc="clang" cxx="clang++"'
76    bin/gn gen out/Cached   --args='cc_wrapper="ccache"'
77    bin/gn gen out/RTTI     --args='extra_cflags_cc=["-frtti"]'
78
79To see all the arguments available, you can run
80
81    bin/gn args out/Debug --list
82
83Having generated your build files, run Ninja to compile and link Skia.
84
85    ninja -C out/Static
86    ninja -C out/Shared
87    ninja -C out/Debug
88    ninja -C out/Release
89    ninja -C out/Clang
90    ninja -C out/Cached
91    ninja -C out/RTTI
92
93If some header files are missing, install the corresponding dependencies
94
95    tools/install_dependencies.sh
96
97<span id="android">Android</span>
98---------------------------------
99
100To build Skia for Android you need an [Android
101NDK](https://developer.android.com/ndk/index.html).
102
103If you do not have an NDK and have access to CIPD, you
104can use one of these commands to fetch the NDK our bots use:
105
106    python2 infra/bots/assets/android_ndk_linux/download.py  -t /tmp/ndk
107    python2 infra/bots/assets/android_ndk_darwin/download.py -t /tmp/ndk
108    python2 infra/bots/assets/android_ndk_windows/download.py -t C:/ndk
109
110When generating your GN build files, pass the path to your `ndk` and your
111desired `target_cpu`:
112
113    bin/gn gen out/arm   --args='ndk="/tmp/ndk" target_cpu="arm"'
114    bin/gn gen out/arm64 --args='ndk="/tmp/ndk" target_cpu="arm64"'
115    bin/gn gen out/x64   --args='ndk="/tmp/ndk" target_cpu="x64"'
116    bin/gn gen out/x86   --args='ndk="/tmp/ndk" target_cpu="x86"'
117
118Other arguments like `is_debug` and `is_component_build` continue to work.
119Tweaking `ndk_api` gives you access to newer Android features like Vulkan.
120
121To test on an Android device, push the binary and `resources` over,
122and run it as normal.  You may find `bin/droid` convenient.
123
124    ninja -C out/arm64
125    adb push out/arm64/dm /data/local/tmp
126    adb push resources /data/local/tmp
127    adb shell "cd /data/local/tmp; ./dm --src gm --config gl"
128
129
130<span id="cros">ChromeOS</span>
131-------------------------------
132
133To cross-compile Skia for arm ChromeOS devices the following is needed:
134
135  - Clang 4 or newer
136  - An armhf sysroot
137  - The (E)GL lib files on the arm chromebook to link against.
138
139To compile Skia for an x86 ChromeOS device, one only needs Clang and the lib files.
140
141If you have access to CIPD, you can fetch all of these as follows:
142
143    python2 infra/bots/assets/clang_linux/download.py  -t /opt/clang
144    python2 infra/bots/assets/armhf_sysroot/download.py -t /opt/armhf_sysroot
145    python2 infra/bots/assets/chromebook_arm_gles/download.py -t /opt/chromebook_arm_gles
146    python2 infra/bots/assets/chromebook_x86_64_gles/download.py -t /opt/chromebook_x86_64_gles
147
148If you don't have authorization to use those assets, then see the README.md files for
149[armhf_sysroot](https://skia.googlesource.com/skia/+/master/infra/bots/assets/armhf_sysroot/README.md),
150[chromebook_arm_gles](https://skia.googlesource.com/skia/+/master/infra/bots/assets/chromebook_arm_gles/README.md), and
151[chromebook_x86_64_gles](https://skia.googlesource.com/skia/+/master/infra/bots/assets/chromebook_x86_64_gles/README.md)
152for instructions on creating those assets.
153
154Once those files are in place, generate the GN args that resemble the following:
155
156    #ARM
157    cc= "/opt/clang/bin/clang"
158    cxx = "/opt/clang/bin/clang++"
159
160    extra_asmflags = [
161        "--target=armv7a-linux-gnueabihf",
162        "--sysroot=/opt/armhf_sysroot/",
163        "-march=armv7-a",
164        "-mfpu=neon",
165        "-mthumb",
166    ]
167    extra_cflags=[
168        "--target=armv7a-linux-gnueabihf",
169        "--sysroot=/opt/armhf_sysroot",
170        "-I/opt/chromebook_arm_gles/include",
171        "-I/opt/armhf_sysroot/include/",
172        "-I/opt/armhf_sysroot/include/c++/4.8.4/",
173        "-I/opt/armhf_sysroot/include/c++/4.8.4/arm-linux-gnueabihf/",
174        "-DMESA_EGL_NO_X11_HEADERS",
175        "-funwind-tables",
176    ]
177    extra_ldflags=[
178        "--sysroot=/opt/armhf_sysroot",
179        "-B/opt/armhf_sysroot/bin",
180        "-B/opt/armhf_sysroot/gcc-cross",
181        "-L/opt/armhf_sysroot/gcc-cross",
182        "-L/opt/armhf_sysroot/lib",
183        "-L/opt/chromebook_arm_gles/lib",
184        "--target=armv7a-linux-gnueabihf",
185    ]
186    target_cpu="arm"
187    skia_use_fontconfig = false
188    skia_use_system_freetype2 = false
189    skia_use_egl = true
190
191
192    # x86_64
193    cc= "/opt/clang/bin/clang"
194    cxx = "/opt/clang/bin/clang++"
195    extra_cflags=[
196        "-I/opt/clang/include/c++/v1/",
197        "-I/opt/chromebook_x86_64_gles/include",
198        "-DMESA_EGL_NO_X11_HEADERS",
199        "-DEGL_NO_IMAGE_EXTERNAL",
200    ]
201    extra_ldflags=[
202        "-stdlib=libc++",
203        "-fuse-ld=lld",
204        "-L/opt/chromebook_x86_64_gles/lib",
205    ]
206    target_cpu="x64"
207    skia_use_fontconfig = false
208    skia_use_system_freetype2 = false
209    skia_use_egl = true
210
211Compile dm (or another executable of your choice) with ninja, as per usual.
212
213Push the binary to a chromebook via ssh and [run dm as normal](https://skia.org/dev/testing/tests)
214using the gles GPU config.
215
216Most chromebooks by default have their home directory partition marked as noexec.
217To avoid "permission denied" errors, remember to run something like:
218
219    sudo mount -i -o remount,exec /home/chronos
220
221<span id="macos">Mac</span>
222---------------------------
223
224Mac users may want to pass `--ide=xcode` to `bin/gn gen` to generate an Xcode project.
225
226<span id="ios">iOS</span>
227-------------------------
228
229Run GN to generate your build files.  Set `target_os="ios"` to build for iOS.
230This defaults to `target_cpu="arm64"`.  Choosing `x64` targets the iOS simulator.
231
232    bin/gn gen out/ios64  --args='target_os="ios"'
233    bin/gn gen out/ios32  --args='target_os="ios" target_cpu="arm"'
234    bin/gn gen out/iossim --args='target_os="ios" target_cpu="x64"'
235
236This will also package (and for devices, sign) iOS test binaries. This defaults to a
237Google signing identity and provisioning profile. To use a different one set `skia_ios_identity`
238to match your code signing identity and `skia_ios_profile` to the name of your provisioning
239profile, e.g. `skia_ios_identity=".*Jane Doe.*" skia_ios_profile="iPad Profile"`. A list of
240identities can be found by typing `security find-identity` on the command line. The name of the
241provisioning profile should be available on the Apple Developer site.
242
243For signed packages `ios-deploy` makes installing and running them on a device easy:
244
245    ios-deploy -b out/Debug/dm.app -d --args "--match foo"
246
247Alternatively you can generate an Xcode project by passing `--ide=xcode` to `bin/gn gen`.
248
249If you find yourself missing a Google signing identity or provisioning profile,
250you'll want to have a read through go/appledev.
251
252Deploying to a device with an OS older than the current SDK doesn't currently work through Xcode,
253but can be done on the command line by setting the environment variable IPHONEOS_DEPLOYMENT_TARGET
254to the desired OS version.
255
256<span id="windows">Windows</span>
257---------------------------------
258
259Skia can build on Windows with Visual Studio 2017 or 2019.
260If GN is unable to locate either of those, it will print an error message. In that
261case, you can pass your `VC` path to GN via `win_vc`.
262
263Skia can be compiled with the free [Build Tools for Visual Studio 2017 or
2642019](https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2019).
265
266The bots use a packaged 2017 toolchain, which Googlers can download like this:
267
268    python2 infra/bots/assets/win_toolchain/download.py -t C:/toolchain
269
270You can then pass the VC and SDK paths to GN by setting your GN args:
271
272    win_vc = "C:\toolchain\VC"
273    win_sdk = "C:\toolchain\win_sdk"
274
275This toolchain is the only way we support 32-bit builds, by also setting `target_cpu="x86"`.
276
277The Skia build assumes that the PATHEXT environment variable contains ".EXE".
278
279### **Highly Recommended**: Build with clang-cl
280
281Skia uses generated code that is only optimized when Skia is built with clang. Other compilers get generic
282unoptimized code.
283
284Setting the `cc` and `cxx` gn args is _not_ sufficient to build with clang-cl. These variables
285are ignored on Windows. Instead set the variable `clang_win` to your LLVM installation directory.
286If you installed the prebuilt LLVM downloaded from [here](https://releases.llvm.org/download.html
287"LLVM Download") in the default location that would be:
288
289    clang_win = "C:\Program Files\LLVM"
290
291Follow the standard Windows path specification and not MinGW convention (e.g.
292`C:\Program Files\LLVM` not ~~`/c/Program Files/LLVM`~~).
293
294### Visual Studio Solutions
295
296If you use Visual Studio, you may want to pass `--ide=vs` to `bin/gn gen` to
297generate `all.sln`.  That solution will exist within the GN directory for the
298specific configuration, and will only build/run that configuration.
299
300If you want a Visual Studio Solution that supports multiple GN configurations,
301there is a helper script. It requires that all of your GN directories be inside
302the `out` directory. First, create all of your GN configurations as usual.
303Pass `--ide=vs` when running `bin/gn gen` for each one. Then:
304
305    python2 gn/gn_meta_sln.py
306
307This creates a new dedicated output directory and solution file
308`out/sln/skia.sln`. It has one solution configuration for each GN configuration,
309and supports building and running any of them. It also adjusts syntax highlighting
310of inactive code blocks based on preprocessor definitions from the selected
311solution configuration.
312
313<span id="win-arm64">Windows ARM64</span>
314-----------------------------------------
315
316There is early, experimental support for [Windows 10 on ARM](https://docs.microsoft.com/en-us/windows/arm/).
317This currently requires (a recent version of) MSVC, and the `Visual C++ compilers and libraries for ARM64`
318individual component in the Visual Studio Installer. For Googlers, the win_toolchain asset includes the
319ARM64 compiler.
320
321To use that toolchain, set the `target_cpu` GN argument to `"arm64"`. Note that OpenGL is not supported
322by Windows 10 on ARM, so Skia's GL backends are stubbed out, and will not work. ANGLE is supported:
323
324    bin/gn gen out/win-arm64 --args='target_cpu="arm64" skia_use_angle=true'
325
326This will produce a build of Skia that can use the software or ANGLE backends, in DM. Viewer only works
327when launched with `--backend angle`, because the software backend tries to use OpenGL to display the
328window contents.
329
330<span id="cmake">CMake</span>
331-----------------------------
332
333We have added a GN-to-CMake translator mainly for use with IDEs that like CMake
334project descriptions.  This is not meant for any purpose beyond development.
335
336    bin/gn gen out/config --ide=json --json-ide-script=../../gn/gn_to_cmake.py
337