DEV Community

Aleksandr
Aleksandr

Posted on

Using variables in Bazel

Image description

Bazel is a free and open-source build and test tool developed by Google. Bazel uses a declarative language (Starlark) to describe the build and test steps.

This simple guide shows how to use variables in Bazel. This can be simple done in 3 steps.

Image description

We'll use an fictional example of setting Java 17 as the default version of the project to show you how to use variables in Bazel.

1: Define the Variable in a .bzl File

In this example we define the Variable in rules/jdk/compatibility.bzl



JDK17_UPWARDS = select({
    "@nasa_atlantis//rules/jdk:jdk11": ["@platforms//:incompatible"],
    "@nasa_atlantis//rules/jdk:jdk8": ["@platforms//:incompatible"],
    "//conditions:default": [],
})



Enter fullscreen mode Exit fullscreen mode

We use the select function to choose the suitable value for the JDK17_UPWARDS variable based on the current platform. In the example, we use Java 17 upwards if the platform supports it, or an empty list otherwise.

2: Import the Variable in your BUILD File

We use the load function to load specific Bazel file and import the variable from it. So, to do this we just add the line below to the BUILD file:



load("//rules/jdk:compatibility.bzl", "JDK17_UPWARDS")


Enter fullscreen mode Exit fullscreen mode

It loads the compatibility.bzl file and imports the JDK17_UPWARDS variable.

3: Use the Variable

Just use the variable in your BUILD file:



java_library(
    name = "nasa_atlantis-web",
    srcs = glob(["src/main/java/**/*.java"]),
    resources = glob(["src/main/resources/**/*"]),
    tags = [
        "maven_coordinates=com.nasa.spring:nasa_atlantis-web:{nasa-project}",
    ],
    target_compatible_with = JDK17_UPWARDS,
    visibility = ["//visibility:public"],
    deps = [
        "//nasa-core/nasa-events:nasa-events-sb3",
        "//libs/b-spring/nasa_atlantis-core",
        "//libs/nasa-tracing:nasa-tracing-sb3",
        "@maven_spring//:jakarta_servlet_jakarta_servlet_api",
        "@maven_spring//:org_slf4j_slf4j_api",
        "@maven_spring//:org_springframework_boot_spring_boot",
        "@maven_spring//:org_springframework_boot_spring_boot_autoconfigure",
        "@maven_spring//:org_springframework_boot_spring_boot_starter",
        "@maven_spring//:org_springframework_boot_spring_boot_starter_web",
        "@maven_spring//:org_springframework_spring_beans",
        "@maven_spring//:org_springframework_spring_context",
        "@maven_spring//:org_springframework_spring_core",
    ],
)



Enter fullscreen mode Exit fullscreen mode

In our example, we use the JDK17_UPWARDS variable in the java_library rule as the attribute named target_compatible_with. This attribute specifies the versions of the Java language that the target is compatible with.

Image description

ERROR: name is not defined

You could have the issue below:



ERROR: /builds/platform/nasa/libs/nasa_atlantis-web/BUILD:22:30: name 'JDK17_UPWARDS' is not defined


Enter fullscreen mode Exit fullscreen mode

The reason is likely because you forgot to import the variable in your BUILD file or the path is wrong. For example you use



load("///rules/jdk:compatibility.bzl", "JDK17_UPWARDS")


Enter fullscreen mode Exit fullscreen mode

instead of



load("//rules/jdk:compatibility.bzl", "JDK17_UPWARDS")


Enter fullscreen mode Exit fullscreen mode

Thats it.

Follow me in the group in telegram

My blog

Top comments (0)