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