This project was moved to Airin repository.
A Kotlin tool that allows to:
- Generate Bazel projects
- Migrate Gradle projects to Bazel (coming soon)
A Kotlin DSL that closely resembles Starlark helps to describe Bazel build files in a type-safe manner.
Overview of the main DSL features
// declaring Bazel files
fun build_file(
/**
*
*/
) = BUILD(relativePath = "app") {
// declaring variable
"LIBRARIES" `=` list("//lib1")
// declaring rules from the available DSL library
android_binary {
name = "app"
// using standard functions
srcs = glob("src/main/java/**/*.java")
// using custom attributes
"manifest_values" `=` {
"minSdkVersion" to "23"
}
// referring to declared variables
deps = list(":app_custom") `+` "LIBRARIES".ref()
// shortcuts for common values
visibility = public
}
// declaring custom rules
"custom_rule" {
"name" `=` "app_custom"
}
}All Bazel files can be described inside builder function. For example, in order to create a BUILD there should be used
the following function:
BUILD {
...
}Bazel also supports files with .bazel extension, that can be described using the following function:
BUILD.bazel {
...
}It is also possible to specify the exact BUILD file location by setting the relativePath argument:
BUILD(relativePath = "app") {
...
}In order to declare a Bazel WORKSPACE file there should be used functions WORKSPACE or WORKSPACE.bazel.
It is also possible to use functions bazelrc and bazelversion to describe the appropriate files.
The bazel-gen Kotlin DSL has a library of commonly used Bazel rules. In order to declare a target in Bazel file there
should be used the following function
java_binary {
name = "application"
srcs = glob("src/main/java/**/*.java")
}If for some reason the DSL does not contain the needed param, it an be declared dynamically with using string name
android_binary {
name = "application"
"manifest_values" `=` {
"minSdkVersion" to "23"
}
}The same approach can be used for custom rules
"custom_rule" {
"name" `=` "application"
}The following snippet shows how to declare an assignment statement
"DEPENDENCIES" `=` list("//lib")In order to refer to the declared variable, there should be used ref() extension function. This helps to assign a
string variable reference to the filed that expects list as a param.
android_binary {
name = "application"
deps = "DEPENDENCIES".ref()
}Comprehensions are powerful construct in Starlark language that, for instance, can help to declare multiple targets for each list item.
The following list comprehension, described in Kotlin DSL
"FILE_NAMES" `=` list(...)
genrule {
name = "generate_" `+` "file".ref()
srcs = list("file".ref())
outs = list("file".strref `+` "_generated.kt")
cmd = """
...
"""
}(`for` = "file", `in` = "FILE_NAMES".ref())will generate the corresponding Starlark statement
FILE_NAMES = [...]
[
genrule(
name = "generate_" + file,
srcs = [file],
outs = [file + "_generated.kt"],
cmd = """
...
""",
)
for file in FILE_NAMES
]In order to describe .bazelrc files, the same approach is used
bazelrc {
build {
-"java_header_compilation=false"
}
"mobile-install" {
-"java_header_compilation=false"
-"start_app"
}
`try-import` { "local.bazelrc" }
}It is possible to customize the file name using the following approach.
"local".bazelrc {
...
}or
object local : bazelrc
...
local.bazelrc {
...
}bazelversion(4, 0, 0)fun main() {
val sourceRoot = "src/main/java"
val build = BUILD.bazel("app") {
java_binary {
name = "app"
srcs = glob("$sourceRoot/**/*.java")
}
}
val writer = BazelFileWriter()
writer.write(workspaceDir = "root/project/dir", file = build)
}This will create a Bazel BUILD file under the following location root/project/dir/app/BUILD.bazel.
// build.gradle
dependencies {
implementation 'org.morfly.bazelgen:generator:<version>'
}Note: to be uploaded to artifactory
// settings.gradle
includeBuild 'path/to/bazel-gen'// build.gradle.kts
dependencies {
implementation("org.morfly.bazelgen:generator:<version>")
}Note: to be uploaded to artifactory
// settings.gradle.kts
includeBuild("path/to/bazel-gen")Coming soon
Check out the directory with example projects for more details.