Migrating iOS Project to Bazel – Part 1

Today, I will write about a new subject. It is a build system used in too many big tech companies. The series contains many articles, follow that if you interest.

The first part focus on intro Bazel, history, and the difference with Xcode build system.

1. What is Bazel

Bazel is an open-source of Google written by Java. It helps we build and test software project. Like Make, Maven, or many build systems, Bazel can build Java, C/C++, Swift, Objective C, Go, etc projects.

Bazel was created based on another Google’s build system named Blaze. The Bazel name is created by moving the “l” character from Blaze 😂

Bazel builds source code by a set of rules. Rules were written in a language called Starlark(the old name is Skylark) – a language that uses Python syntax. Rules will define steps to tell the system how to build source code.

  • Pros
    • Support multi programming language
    • Build faster
    • Have remote cache and remote excution ability
    • Starlark is strong programming language to write more rules or support build a new programming language.
  • Cons
    • Complex
    • Small community
    • Need use modular architect to use.
    • Some rules don’t have official support. Engineer must find others source or write custom rules.

2. Starlark language

Starlark is a programming language used in Bazel to write compile rules and define build targets. It uses the subset syntax of Python and is easy to approach.

def fizz_buzz(n):
  """Print Fizz Buzz numbers from 1 to n."""
  for i in range(1, n + 1):
    s = ""
    if i % 3 == 0:
      s += "Fizz"
    if i % 5 == 0:
      s += "Buzz"
    print(s if s else i)

fizz_buzz(20)

Bazel will appear in two places in your projects. That is in BUILD and .bzl file. The BUILD file contains the build target by call rules defined in .bzl file.

An example of BUILD file

load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")

swift_library(
    name = "app_classes",
    srcs = glob([
        "*.swift"
    ])
)

In normal cases, we only need to create BUILD files (in the next article I will talk more detail) to define build targets by call function defined in bzl file.

3. What companies used Bazel?

Bazel home page has lists some company used Bazel. There are so many companies that used Bazel but not listed here is LINE, Grab, Telegram, Pinterest, Binance, etc. Vietnamese have VinID and my company.

4. Some differences in your work between Xcode and Bazel.

  • Bazel needs you specifically define the build targets by the configuration file and the dependencies graph between build targets.
  • To add more files to the project, you need define them in a configuration file instead of drag and drop to Xcode.
  • If you want to use the Xcode project for code completion, you need to use the Tulsi tool to generate the Xcode project for you.
  • Build flag that you used in Xcode, you must specify in the configuration file.

In the next parts, I will talk more detail about Bazel, how to config a sample project from scratch.

Learn more:
https://bazel.build/
https://docs.bazel.build/versions/main/migrate-xcode.html
https://docs.bazel.build/versions/main/skylark/language.html

Leave a Reply