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-6.0" cxx="clang++6.0"'` GN build arguments, as illustrated in 54[Quickstart](#quick). This can be useful for building Skia without needing to 55modify your machine's default compiler toolchain. 56 57## Quickstart 58 59Run `gn gen` to generate your build files. As arguments to `gn gen`, pass a name 60for your build directory, and optionally `--args=` to configure the build type. 61 62To build Skia as a static library in a build directory named `out/Static`: 63 64``` 65bin/gn gen out/Static --args='is_official_build=true' 66``` 67 68To build Skia as a shared library (DLL) in a build directory named `out/Shared`: 69 70``` 71bin/gn gen out/Shared --args='is_official_build=true is_component_build=true' 72``` 73 74If you find that you don't have `bin/gn`, make sure you've run: 75 76``` 77python2 tools/git-sync-deps 78``` 79 80For a list of available build arguments, take a look at `gn/skia.gni`, or run: 81 82``` 83bin/gn args out/Debug --list 84``` 85 86GN allows multiple build folders to coexist; each build can be configured 87separately as desired. For example: 88 89``` 90bin/gn gen out/Debug 91bin/gn gen out/Release --args='is_debug=false' 92bin/gn gen out/Clang --args='cc="clang" cxx="clang++"' 93bin/gn gen out/Cached --args='cc_wrapper="ccache"' 94bin/gn gen out/RTTI --args='extra_cflags_cc=["-frtti"]' 95``` 96 97Once you have generated your build files, run Ninja to compile and link Skia: 98 99``` 100ninja -C out/Static 101``` 102 103If some header files are missing, install the corresponding dependencies: 104 105``` 106tools/install_dependencies.sh 107``` 108 109To pull new changes and rebuild: 110 111``` 112git pull 113python tools/git-sync-deps 114ninja -C out/Static 115``` 116 117## Android 118 119To build Skia for Android you need an 120[Android NDK](https://developer.android.com/ndk/index.html). 121 122If you do not have an NDK and have access to CIPD, you can use one of these 123commands to fetch the NDK our bots use: 124 125``` 126./bin/sk asset download android_ndk_linux /tmp/ndk 127./bin/sk asset download android_ndk_darwin /tmp/ndk 128./bin/sk.exe asset download android_ndk_windows C:/ndk 129``` 130 131When generating your GN build files, pass the path to your `ndk` and your 132desired `target_cpu`: 133 134``` 135bin/gn gen out/arm --args='ndk="/tmp/ndk" target_cpu="arm"' 136bin/gn gen out/arm64 --args='ndk="/tmp/ndk" target_cpu="arm64"' 137bin/gn gen out/x64 --args='ndk="/tmp/ndk" target_cpu="x64"' 138bin/gn gen out/x86 --args='ndk="/tmp/ndk" target_cpu="x86"' 139``` 140 141Other arguments like `is_debug` and `is_component_build` continue to work. 142Tweaking `ndk_api` gives you access to newer Android features like Vulkan. 143 144To test on an Android device, push the binary and `resources` over, and run it 145as normal. You may find `bin/droid` convenient. 146 147``` 148ninja -C out/arm64 149adb push out/arm64/dm /data/local/tmp 150adb push resources /data/local/tmp 151adb shell "cd /data/local/tmp; ./dm --src gm --config gl" 152``` 153 154## ChromeOS 155 156To cross-compile Skia for arm ChromeOS devices the following is needed: 157 158- Clang 4 or newer 159- An armhf sysroot 160- The (E)GL lib files on the arm chromebook to link against. 161 162To compile Skia for an x86 ChromeOS device, one only needs Clang and the lib 163files. 164 165If you have access to CIPD, you can fetch all of these as follows: 166 167``` 168./bin/sk asset download clang_linux /opt/clang 169./bin/sk asset download armhf_sysroot /opt/armhf_sysroot 170./bin/sk asset download chromebook_arm_gles /opt/chromebook_arm_gles 171./bin/sk asset download chromebook_x86_64_gles /opt/chromebook_x86_64_gles 172``` 173 174If you don't have authorization to use those assets, then see the README.md 175files for 176[armhf_sysroot](https://skia.googlesource.com/skia/+/main/infra/bots/assets/armhf_sysroot/README.md), 177[chromebook_arm_gles](https://skia.googlesource.com/skia/+/main/infra/bots/assets/chromebook_arm_gles/README.md), 178and 179[chromebook_x86_64_gles](https://skia.googlesource.com/skia/+/main/infra/bots/assets/chromebook_x86_64_gles/README.md) 180for instructions on creating those assets. 181 182Once those files are in place, generate the GN args that resemble the following: 183 184``` 185#ARM 186cc= "/opt/clang/bin/clang" 187cxx = "/opt/clang/bin/clang++" 188 189extra_asmflags = [ 190 "--target=armv7a-linux-gnueabihf", 191 "--sysroot=/opt/armhf_sysroot/", 192 "-march=armv7-a", 193 "-mfpu=neon", 194 "-mthumb", 195] 196extra_cflags=[ 197 "--target=armv7a-linux-gnueabihf", 198 "--sysroot=/opt/armhf_sysroot", 199 "-I/opt/chromebook_arm_gles/include", 200 "-I/opt/armhf_sysroot/include/", 201 "-I/opt/armhf_sysroot/include/c++/4.8.4/", 202 "-I/opt/armhf_sysroot/include/c++/4.8.4/arm-linux-gnueabihf/", 203 "-DMESA_EGL_NO_X11_HEADERS", 204 "-funwind-tables", 205] 206extra_ldflags=[ 207 "--sysroot=/opt/armhf_sysroot", 208 "-B/opt/armhf_sysroot/bin", 209 "-B/opt/armhf_sysroot/gcc-cross", 210 "-L/opt/armhf_sysroot/gcc-cross", 211 "-L/opt/armhf_sysroot/lib", 212 "-L/opt/chromebook_arm_gles/lib", 213 "--target=armv7a-linux-gnueabihf", 214] 215target_cpu="arm" 216skia_use_fontconfig = false 217skia_use_system_freetype2 = false 218skia_use_egl = true 219 220 221# x86_64 222cc= "/opt/clang/bin/clang" 223cxx = "/opt/clang/bin/clang++" 224extra_cflags=[ 225 "-I/opt/clang/include/c++/v1/", 226 "-I/opt/chromebook_x86_64_gles/include", 227 "-DMESA_EGL_NO_X11_HEADERS", 228 "-DEGL_NO_IMAGE_EXTERNAL", 229] 230extra_ldflags=[ 231 "-stdlib=libc++", 232 "-fuse-ld=lld", 233 "-L/opt/chromebook_x86_64_gles/lib", 234] 235target_cpu="x64" 236skia_use_fontconfig = false 237skia_use_system_freetype2 = false 238skia_use_egl = true 239``` 240 241Compile dm (or another executable of your choice) with ninja, as per usual. 242 243Push the binary to a chromebook via ssh and 244[run dm as normal](/docs/dev/testing/tests) using the gles GPU config. 245 246Most chromebooks by default have their home directory partition marked as 247noexec. To avoid "permission denied" errors, remember to run something like: 248 249``` 250sudo mount -i -o remount,exec /home/chronos 251``` 252 253## Mac 254 255Mac users may want to pass `--ide=xcode` to `bin/gn gen` to generate an Xcode 256project. 257 258## iOS 259 260Run GN to generate your build files. Set `target_os="ios"` to build for iOS. 261This defaults to `target_cpu="arm64"`. Choosing `x64` targets the iOS simulator. 262 263``` 264bin/gn gen out/ios64 --args='target_os="ios"' 265bin/gn gen out/ios32 --args='target_os="ios" target_cpu="arm"' 266bin/gn gen out/iossim --args='target_os="ios" target_cpu="x64"' 267``` 268 269This will also package (and for devices, sign) iOS test binaries. This defaults 270to a Google signing identity and provisioning profile. To use a different one 271set the GN args `skia_ios_identity` to match your code signing identity and 272`skia_ios_profile` to the name of your provisioning profile, e.g. 273 274``` 275skia_ios_identity=".*Jane Doe.*" 276skia_ios_profile="iPad Profile"` 277``` 278 279A list of identities can be found by typing `security find-identity` on the 280command line. The name of the provisioning profile should be available on the 281Apple Developer site. Alternatively, `skia_ios_profile` can be the absolute path 282to the mobileprovision file. 283 284If you find yourself missing a Google signing identity or provisioning profile, 285you'll want to have a read through go/appledev. 286 287For signed packages `ios-deploy` makes installing and running them on a device 288easy: 289 290``` 291ios-deploy -b out/Debug/dm.app -d --args "--match foo" 292``` 293 294Alternatively you can generate an Xcode project by passing `--ide=xcode` to 295`bin/gn gen`. If you are using Xcode version 10 or later, you may need to go to 296`Project Settings...` and verify that `Build System:` is set to 297`Legacy Build System`. 298 299Deploying to a device with an OS older than the current SDK can be done by 300setting the `ios_min_target` arg: 301 302``` 303ios_min_target = "<major>.<minor>" 304``` 305 306where `<major>.<minor>` is the iOS version on the device, e.g., 12.0 or 11.4. 307 308## Windows 309 310Skia can build on Windows with Visual Studio 2017 or 2019. If GN is unable to 311locate either of those, it will print an error message. In that case, you can 312pass your `VC` path to GN via `win_vc`. 313 314Skia can be compiled with the free 315[Build Tools for Visual Studio 2017 or 2019](https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2019). 316 317The bots use a packaged 2019 toolchain, which Googlers can download like this: 318 319``` 320./bin/sk.exe asset download win_toolchain C:/toolchain 321``` 322 323You can then pass the VC and SDK paths to GN by setting your GN args: 324 325``` 326win_vc = "C:\toolchain\VC" 327win_sdk = "C:\toolchain\win_sdk" 328``` 329 330This toolchain is the only way we support 32-bit builds, by also setting 331`target_cpu="x86"`. 332 333The Skia build assumes that the PATHEXT environment variable contains ".EXE". 334 335### **Highly Recommended**: Build with clang-cl 336 337Skia uses generated code that is only optimized when Skia is built with clang. 338Other compilers get generic unoptimized code. 339 340Setting the `cc` and `cxx` gn args is _not_ sufficient to build with clang-cl. 341These variables are ignored on Windows. Instead set the variable `clang_win` to 342your LLVM installation directory. If you installed the prebuilt LLVM downloaded 343from [here](https://releases.llvm.org/download.html 'LLVM Download') in the 344default location that would be: 345 346``` 347clang_win = "C:\Program Files\LLVM" 348``` 349 350Follow the standard Windows path specification and not MinGW convention (e.g. 351`C:\Program Files\LLVM` not ~~`/c/Program Files/LLVM`~~). 352 353### Visual Studio Solutions 354 355If you use Visual Studio, you may want to pass `--ide=vs` to `bin/gn gen` to 356generate `all.sln`. That solution will exist within the GN directory for the 357specific configuration, and will only build/run that configuration. 358 359If you want a Visual Studio Solution that supports multiple GN configurations, 360there is a helper script. It requires that all of your GN directories be inside 361the `out` directory. First, create all of your GN configurations as usual. Pass 362`--ide=vs` when running `bin/gn gen` for each one. Then: 363 364``` 365python2 gn/gn_meta_sln.py 366``` 367 368This creates a new dedicated output directory and solution file 369`out/sln/skia.sln`. It has one solution configuration for each GN configuration, 370and supports building and running any of them. It also adjusts syntax 371highlighting of inactive code blocks based on preprocessor definitions from the 372selected solution configuration. 373 374## Windows ARM64 375 376There is early, experimental support for 377[Windows 10 on ARM](https://docs.microsoft.com/en-us/windows/arm/). This 378currently requires (a recent version of) MSVC, and the 379`Visual C++ compilers and libraries for ARM64` individual component in the 380Visual Studio Installer. For Googlers, the win_toolchain asset includes the 381ARM64 compiler. 382 383To use that toolchain, set the `target_cpu` GN argument to `"arm64"`. Note that 384OpenGL is not supported by Windows 10 on ARM, so Skia's GL backends are stubbed 385out, and will not work. ANGLE is supported: 386 387``` 388bin/gn gen out/win-arm64 --args='target_cpu="arm64" skia_use_angle=true' 389``` 390 391This will produce a build of Skia that can use the software or ANGLE backends, 392in DM. Viewer only works when launched with `--backend angle`, because the 393software backend tries to use OpenGL to display the window contents. 394 395## CMake 396 397We have added a GN-to-CMake translator mainly for use with IDEs that like CMake 398project descriptions. This is not meant for any purpose beyond development. 399 400``` 401bin/gn gen out/config --ide=json --json-ide-script=../../gn/gn_to_cmake.py 402``` 403