Leave a rating/review
Episode 9 - Create a Workspace
Demo
In the last episode, we moved the JokeGenerator to the root level of the monorepo. The workspace was empty since we didn’t need to do anything outside of compiling the program, so we can delete all the existing content.
Now let’s write the rules for building our iOS app. We need to incorporate a few rules into the workspace when building an iOS app.
https://github.com/bazelbuild
Now if we head over Bazel’s official repository, you’ll find that we have a ton of rules for different languages and environments. We’re interested in the rules_apple repository since that allows us to compile code for Apple devices. But also, we’re interested in the rules_swift. That allows us to compile our Swift code. By having Swift as a separate target, we can use Bazel to compile Swift for other Swift supported platforms.
Okay, back to our Workspace. First, lets add a comment for the iOS app.
# Rules for building the Bullseye for iOS
###
There are a few ways to handle dependencies which we will cover soon enough. For now, we just want to includes the rules as a download. To do this, we can use the http archive function. This is included with bazel tools.
Add the following:
load("@bazel_tools//tools/build_defs/repo:http.bzl")
Here are accessing the http rules file included with bazel. Yet, we want to access the http_archive function, so we must designate it after the rules address.
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
This imports the http_archive function and we access it using the variable http_archive. Now lets get our swift rules.
http_archive(
)
First lets provide a name that we can use to reference.
http_archive(
name = "build_bazel_rules_swift",
)
Next, we’ll provide the SHA value. This ensures we are using the correct file. If the file changes, we’ll receive a different SHA value resulting in a build failure. For the sake of accuracy, I’m pasting in the SHA. If you are following along, make sure to pause the video.
sha256 = "a2fd565e527f83fb3f9eb07eb9737240e668c9242d3bc318712efa54a7deda97",
Finally, let’s provide the url for the rules.
url = "https://github.com/bazelbuild/rules_swift/releases/download/0.27.0/rules_swift.0.27.0.tar.gz",
The http archive just downloads, extracts the rules and makes the targets available. We need to run two specific functions to load the dependencies. Open up GitHub repo for the swift rules.
https://github.com/bazelbuild/rules_swift
The rules are contained in the swift subfolder. Click on it. Then click on repositories.bzl. The first function is called swift_rules_dependencies. This is located in the repositories.bzl file. If you scroll down, you’ll see the swift_rules_dependencies function. We can reference this from the load method.
load(
)
To get repositories.bzl, we reference the label we defined in the previous load statement.
load(
"@build_bazel_rules_swift//"
)
This was in the swift folder in the repositories.bzl file.
load(
"@build_bazel_rules_swift//swift:repositories.bzl",
)
Finally, we need to load the swift_rules_dependencies
method.
load(
"@build_bazel_rules_swift//swift:extras.bzl",
"swift_rules_dependencies",
)
Finally, we’ll call the function.
swift_rules_dependencies()
We’ll do the same for swift rules extra dependencies. We get this from the extras.bzl file then call it.
load(
"@build_bazel_rules_swift//swift:extras.bzl",
"swift_rules_extra_dependencies",
)
swift_rules_extra_dependencies()
That allows to compile Swift. Now comes the Apple rules. First, we’ll get the rules much like we did for Swift.
http_archive(
name = "build_bazel_rules_apple",
sha256 = "a5f00fd89eff67291f6cd3efdc8fad30f4727e6ebb90718f3f05bbf3c3dd5ed7",
url = "https://github.com/bazelbuild/rules_apple/releases/download/0.33.0/rules_apple.0.33.0.tar.gz",
)
Next, we’ll load the apple_rules_dependencies function.
load(
"@build_bazel_rules_apple//apple:repositories.bzl",
"apple_rules_dependencies",
)
Finally, we’ll call the apple_rules_dependencies.
apple_rules_dependencies()
Congrats! We have all the resources set to run build iOS apps. Now we have to write the rules for our Android app except before we start, you should have a good understanding of dependencies work with Bazel.