Bazel BUILD
files are similar to Package.swift
files. They use a Python-like syntax (called Starlark) to define the build targets and their dependencies. Instead of a single Package.swift
, Bazel builds are defined modularly across many BUILD
files.
Compare the Package.swift
to these swift-driver BUILD
files:
External dependency BUILD
files:
Bazel's WORKSPACE
is where external dependencies are defined and loaded. Pinning exact dependencies is done in the WORKSPACE
.
The .bazelrc
file is a place where customized Bazel settings can be set. The .bazelrc
is also used to define build configurations, in this demo the build configurations are mapped one-to-one with Xcode.
XcodeGen is an Xcode project generator. This demo uses XcodeGen to define the .xcodeproj
. See XcodeGen docs
At a high level, building with Bazel is achieved by:
- Adding a "Run Script" phase that calls
bazel build
- Disabling Xcode's build
For each top level target, such as apps and test, add a Run Script that calls bazel build
. For example with XcodeGen:
preBuildScripts:
- name: Build with bazel
script: bazel build --config=$CONFIGURATION swift-driver
See project.yml
for a more complete example.
Xcode's build can be disabled by stubbing out various build settings to /usr/bin/true
:
SWIFT_EXEC=/usr/bin/true
CC=/usr/bin/true
LIBTOOL=/usr/bin/true
LD=/usr/bin/true
These look just like make
environment variables, but they are Xcode build settings.
After calling bazel build
, there is a series of post-build steps that take place. These steps are called from bazel/install.sh
.
- Rsync runnable build products to
DerivedData
- Copy intermediates required for build completion
- Copy generated
.swiftmodule
files toDerivedData
- Import indexes to
DerivedData
- Generate an
lldb
settings file
Importing Bazel built indexes into Xcode is done using lyft/index-import
.
These steps all make use of the environment variables Xcode provides. See Xcode's Build Setting Reference.
Xcode can be configured to use the full Xcode index, not just the index of the files in the project. This demo project takes advantage of that by building and importing the full index, not just the swift-driver sources.
Bazel can produce a build graph visualization. Requires Graphviz.