Bazel rules for compiling OpenCL source files (.cl) to C++ header files (.cl.h).
rules_opencl provides Bazel rules for compiling OpenCL source files into C++ header files. The generated header files include:
- OpenCL source code as C++ string constants
- Automatically extracted kernel name arrays
- Preserved original comments (optional)
Add the following to your MODULE.bazel file:
bazel_dep(name = "rules_opencl", version = "1.0.0")
# Use git_override to point to GitHub repository
git_override(
module_name = "rules_opencl",
remote = "https://github.com/SilvesterHsu/rules_opencl.git",
commit = "your-commit-hash", # Replace with actual commit hash
)
# Or use archive_override (recommended for releases)
# archive_override(
# module_name = "rules_opencl",
# urls = ["https://github.com/SilvesterHsu/rules_opencl/archive/v1.0.0.tar.gz"],
# strip_prefix = "rules_opencl-1.0.0",
# integrity = "sha256-...", # Calculate actual sha256
# )In your BUILD.bazel file:
load("@rules_opencl//:opencl.bzl", "opencl_library")
opencl_library(
name = "my_kernel_cl",
srcs = ["my_kernel.cl"],
)
cc_library(
name = "my_kernel_cl_hdr",
hdrs = [":my_kernel_cl"],
)
cc_binary(
name = "my_program",
srcs = ["my_program.cc"],
deps = [":my_kernel_cl_hdr"],
)opencl_library(
name = "my_kernel_cl",
srcs = ["my_kernel.cl"],
include_dirs = ["kernels/common", "kernels/utils"],
hdrs = ["kernels/common/definitions.cl"],
)opencl_library(
name = "my_kernel_cl",
srcs = ["my_kernel.cl"],
var = "my_custom_kernel_source",
)The generated header file will contain:
// Auto-generated variable name (based on filename)
constexpr const char* my_kernel = "..."; // OpenCL source code
// Kernel name array (if kernel functions are detected)
constexpr const char* my_kernel_kernels[] = {
"kernel_name_1",
"kernel_name_2"
};
constexpr unsigned int my_kernel_kernel_count = 2;- Bazel 6.0 or higher (with MODULE.bazel support)
rules_python(automatically included as a dependency)
Compiles OpenCL source files to C++ header files.
Parameters:
name(required): Target namesrcs(required): List of OpenCL source files (.clfiles), must contain exactly one fileinclude_dirs(optional): List of include directories for#includedirectivesvar(optional): C++ variable name, derived from filename if not specifiedhdrs(optional): List of.clheader files that may be included by the source file
Output:
- Generates
.cl.hheader file containing OpenCL source code as C++ string constants
See test cases in the project for complete examples.