Skip to content

Custom Bazel Rules

Organizations often have custom Bazel rules that wrap cc_library, cc_binary, etc. with additional functionality. Rebaze can generate calls to your custom rules.

Custom rules can:

  • Enforce coding standards
  • Add default compiler flags
  • Integrate with internal tooling
  • Provide consistent build configuration

In rebaze.toml:

[build.rules]
library_rule = "my_cc_library"
binary_rule = "my_cc_binary"
test_rule = "my_cc_test"
[[build.rules.extra_loads]]
bzl = "@mycompany//build:rules.bzl"
items = ["my_cc_library", "my_cc_binary", "my_cc_test"]

Instead of:

load("@rules_cc//cc:defs.bzl", "cc_library", "cc_binary")
cc_library(
name = "mylib",
srcs = ["lib.cpp"],
)
cc_binary(
name = "myapp",
srcs = ["main.cpp"],
deps = [":mylib"],
)

Rebaze generates:

load("@rules_cc//cc:defs.bzl", "cc_library", "cc_binary")
load("@mycompany//build:rules.bzl", "my_cc_library", "my_cc_binary")
my_cc_library(
name = "mylib",
srcs = ["lib.cpp"],
)
my_cc_binary(
name = "myapp",
srcs = ["main.cpp"],
deps = [":mylib"],
)
[build.rules]
library_rule = "spotify_cc_library"
binary_rule = "spotify_cc_binary"
test_rule = "spotify_cc_test"
[[build.rules.extra_loads]]
bzl = "@spotify//build:cc.bzl"
items = ["spotify_cc_library", "spotify_cc_binary", "spotify_cc_test"]
[[build.rules.extra_loads]]
bzl = "@mycompany//build:cc.bzl"
items = ["my_cc_library", "my_cc_binary"]
[[build.rules.extra_loads]]
bzl = "@mycompany//build:test.bzl"
items = ["my_cc_test"]

Customize only some rules:

[build.rules]
library_rule = "my_cc_library" # Custom
binary_rule = "cc_binary" # Standard
test_rule = "cc_test" # Standard
[[build.rules.extra_loads]]
bzl = "@mycompany//build:rules.bzl"
items = ["my_cc_library"]