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