1 2--- 3title: "Notes about Bazel Builds" 4linkTitle: "Notes about Bazel Builds" 5 6--- 7 8 9Skia cannot be built with Bazel yet. 10 11But you should be able to build and run the trivial `tools/bazel_test.cc`: 12 13 $ bazel test ... 14 15Dependencies 16------------ 17 18`WORKSPACE.bazel` acts like `DEPS`, listing external dependencies and how to 19fetch them. You can call `bazel sync`, or just let `bazel {build,test,run}` 20handle it as needed on its own. The easiest way to add a new dependency is to 21start using `tag="..."` or `branch="..."` and then follow the advice of Bazel 22to pin that to the `commit` and `shallow_since` it suggests. 23 24We must provide Bazel build configuration for dependencies like `libpng` that 25don't provide their own. For `libpng` that's `bazel/libpng.bazel`, linked by 26the `new_git_repository()` `build_file` argument, written relative to that 27fetched Git repo's root. Its resemblance to `third_party/libpng/BUILD.gn` is 28no coincidence... it's pretty much a 1:1 translation between GN and Bazel. 29 30Everything that's checked in builds external dependencies from source. I've 31not written an integrated system for substituting prebuilt versions of these 32dependencies (e.g. `/usr/include/png.h` and `/usr/lib/libpng.so`), instead 33leaving that up to users who want it. The process is not exactly trivial, but 34closer to tedious than difficult. Here's an example, overriding `libpng` to 35point to prebuilts from Homebrew in ~/brew: 36 37Each overridden dependency will need its own directory with a few files. 38 39 $ find overrides 40 overrides 41 overrides/libpng 42 overrides/libpng/include 43 overrides/libpng/WORKSPACE.bazel 44 overrides/libpng/BUILD.bazel 45 46`WORKSPACE.bazel` must be present, but in this case can be empty. 47 48 $ cat overrides/libpng/WORKSPACE.bazel 49 50`BUILD.bazel` is where it all happens: 51 52 $ cat overrides/libpng/BUILD.bazel 53 cc_library( 54 name = "libpng", 55 hdrs = ["include/png.h"], 56 srcs = ["include/pngconf.h", "include/pnglibconf.h"], 57 includes = ["include"], 58 linkopts = ["-lpng", "-L/Users/mtklein/brew/lib"], 59 visibility = ["//visibility:public"], 60 ) 61 62`include` is a symlink I've made to `~/brew/include` because Bazel doesn't like 63absolute paths in `hdrs` or `includes`. On the other hand, a symlink to 64`~/brew/lib` doesn't work here, though `-L/Users/mtklein/brew/lib` works fine. 65 66 $ readlink overrides/libpng/include 67 /Users/mtklein/brew/include/ 68 69Finally, we point Bazel at all that using `--override_repository`: 70 71 $ bazel test ... --override_repository libpng=/Users/mtklein/overrides/libpng 72 73I expect building from source to be the most common use case, and it's more or 74less enough to simply know that we can substitute prebuilts this way. The most 75interesting part to me is that we don't need to provide this mechanism... it's 76all there in stock Bazel. This plan may all want some rethinking in the future 77if we want to add the option to trim the dependency entirely and make this 78tristate (build it, use it prebuilt, or trim). 79 80.bazelrc 81-------- 82 83I have not (yet?) checked in a .bazelrc to the Skia repo, but have found it 84handy to write my own in ~/.bazelrc: 85 86 $ cat ~/.bazelrc 87 # Print more information on failures. 88 build --verbose_failures 89 test --test_output errors 90 91 # Create an ASAN config, try `bazel test --config asan ...`. 92 build:asan --copt -fsanitize=address 93 build:asan --copt -Wno-macro-redefined # (_FORTIFY_SOURCE redefined.) 94 build:asan --linkopt -fsanitize=address 95 96 # Flip on and off prebuilt overrides easily. 97 build --override_repository libpng=/Users/mtklein/overrides/libpng 98 99I'm impressed by how much you can configure via bazelrc, and I think this 100should let our Bazel build configuration stay mostly focused on the structure 101of the project, less cluttered by build settings. 102 103