Building with Bazel
Part 1: Learning Bazel Generate an Xcode Project
— iOS & Swift, Android & Kotlin

Lesson Complete

Play Next Lesson
Next

Generate an Xcode Project

This video was last updated on Jul 8 2022

Learn how to use custom rules with Bazel to generate an Xcode project for your iOS app.

Contributors

Episode 18 - Generate an Xcode Project

If you are working as a part of team and need to have an Xcode project generated for you, you have a number of options when working with Bazel. First, you can use Tulsi. Tulsi is a project that works with Bazel and generates your Xcode project for you. The app is in beta status but is used within Google.

Another option is to use one of the rulesets provided by the community. There are various rulesets developed by various companies who have already adopted Bazel and were kind enough to share their solutions. Some examples are XCHammer, rules_ios and rules_xcodeproj. All projects have some benefits and downsides to each of them, but they all have one single goal in common: make it easier to use all the power of Bazel inside of Xcode. Let’s try to use rules_xcodeproj to generate our Xcode project.

Demo

To get started, we’re going to have the xcodeproj rules generate our Xcode project. First, navigate to your project files. Navigate into your iOS folder. Find your Xcode project and delete it.

Next, since we aren’t building remotely, open up your bazelrc file and delete all the remote

Now we’ll write the code to generate it from the Bazel build definitions. Open up your workspace file. First, we’ll make sure the ruleset is downloaded. Add the following to the top of your workspace:

http_archive(
    name = "com_github_buildbuddy_io_rules_xcodeproj",
    sha256 = "0b24086579b3824da24031d1f0092df52acd05ad2b6b394989d5cb10021ed2cd",
    strip_prefix = "rules_xcodeproj-df53dd2f7d9d5be218e9016537927bd26cc37594",
    urls = [
        "https://github.com/buildbuddy-io/rules_xcodeproj/archive/df53dd2f7d9d5be218e9016537927bd26cc37594.tar.gz",
    ],
)

Next, we’ll load the rules, exposing the xcodeproj_rules_dependencies function.

load(
    "@com_github_buildbuddy_io_rules_xcodeproj//xcodeproj:repositories.bzl",
    "xcodeproj_rules_dependencies",
)

Then we’ll call the function.

xcodeproj_rules_dependencies()

And that’s all it takes. Next, we’ll update our build. Open up your build file. First we’ll load in our rules:

load(
    "@com_github_buildbuddy_io_rules_xcodeproj//xcodeproj:xcodeproj.bzl",
    "xcodeproj",
)

Now We need to add a new target. Add the following target.

xcodeproj(
    name = "xcodeproj",
    project_name = "Bullseye",
    targets = [":yourfirstapp"]
)

Back in the terminal, I run the following command and this creates the actual Xcode project.

bazel run //bullseye:xcodeproj

This is the target that will generate the actual project. We can then open the generated Xcode project and build it in Xcode.

By default, the project will build with the Xcode build system. As is, the rule_xcodeproj is moving towards a 1.0 release. With this release, it will support all core c, C++, objective C and both the apple and swift bazel rules. Better still, you’ll be able to build specific bazel targets in Xcode using Bazel itself exactly like the Android Studio plugin. For more information, check out the documentation for rules_xcodeproj. If you want to see this in action, download the 0.30 preview whereby there is initial support for building with Bazel inside of Xcode.

Reviews

Comments