{"id":28519753,"url":"https://github.com/bazel-contrib/rules_cuda","last_synced_at":"2026-02-15T04:01:55.638Z","repository":{"id":37555675,"uuid":"435075903","full_name":"bazel-contrib/rules_cuda","owner":"bazel-contrib","description":"Starlark implementation of bazel rules for CUDA.","archived":false,"fork":false,"pushed_at":"2025-06-30T23:28:13.000Z","size":1224,"stargazers_count":106,"open_issues_count":41,"forks_count":58,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-07-01T00:34:11.519Z","etag":null,"topics":["bazel","cuda","starlark"],"latest_commit_sha":null,"homepage":"https://bazel-contrib.github.io/rules_cuda/","language":"Starlark","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bazel-contrib.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"open_collective":"bazel-rules-authors-sig"}},"created_at":"2021-12-05T04:49:07.000Z","updated_at":"2025-06-25T01:01:23.000Z","dependencies_parsed_at":"2023-12-02T16:22:48.498Z","dependency_job_id":"31d4a663-d7cc-41b0-9ab4-301d2e6fbf4f","html_url":"https://github.com/bazel-contrib/rules_cuda","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/bazel-contrib/rules_cuda","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazel-contrib%2Frules_cuda","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazel-contrib%2Frules_cuda/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazel-contrib%2Frules_cuda/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazel-contrib%2Frules_cuda/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bazel-contrib","download_url":"https://codeload.github.com/bazel-contrib/rules_cuda/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazel-contrib%2Frules_cuda/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263749811,"owners_count":23505454,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bazel","cuda","starlark"],"created_at":"2025-06-09T06:30:27.401Z","updated_at":"2026-02-15T04:01:55.627Z","avatar_url":"https://github.com/bazel-contrib.png","language":"Starlark","funding_links":["https://opencollective.com/bazel-rules-authors-sig"],"categories":[],"sub_categories":[],"readme":"# CUDA rules for [Bazel](https://bazel.build)\n\nThis repository contains [Starlark](https://github.com/bazelbuild/starlark) implementation of CUDA rules for Bazel.\n\nThese rules provide a set of rules and macros that make it easier to build CUDA with Bazel.\n\n## Getting Started\n\n### Bzlmod\n\nAdd the following to your `MODULE.bazel` file and replace the placeholders with actual values.\n\n```starlark\nbazel_dep(name = \"rules_cc\", version = \"{rules_cc_version}\")\nbazel_dep(name = \"rules_cuda\", version = \"0.2.5\")\n\n# pick a specific version (this is optional and can be skipped)\narchive_override(\n    module_name = \"rules_cuda\",\n    integrity = \"{SRI value}\",  # see https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity\n    url = \"https://github.com/bazel-contrib/rules_cuda/archive/{git_commit_hash}.tar.gz\",\n    strip_prefix = \"rules_cuda-{git_commit_hash}\",\n)\n\ncuda = use_extension(\"@rules_cuda//cuda:extensions.bzl\", \"toolchain\")\ncuda.toolkit(\n    name = \"cuda\",\n    toolkit_path = \"\",\n)\nuse_repo(cuda, \"cuda\")\n```\n\n`rules_cc` provides the C++ toolchain dependency for `rules_cuda`; in Bzlmod, the compatibility repository is handled by `rules_cc` itself.\n\n\u003cdetails\u003e\n\u003csummary\u003eTraditional WORKSPACE approach\u003c/summary\u003e\n\n### Traditional WORKSPACE approach\n\nAdd the following to your `WORKSPACE` file and replace the placeholders with actual values.\n\n```starlark\nload(\"@bazel_tools//tools/build_defs/repo:http.bzl\", \"http_archive\")\n\nhttp_archive(\n    name = \"rules_cc\",\n    sha256 = \"{rules_cc_sha256}\",\n    strip_prefix = \"rules_cc-{rules_cc_version}\",\n    urls = [\"https://github.com/bazelbuild/rules_cc/releases/download/{rules_cc_version}/rules_cc-{rules_cc_version}.tar.gz\"],\n)\nload(\"@rules_cc//cc:extensions.bzl\", \"compatibility_proxy_repo\")\ncompatibility_proxy_repo()\n\nhttp_archive(\n    name = \"rules_cuda\",\n    sha256 = \"{sha256_to_replace}\",\n    strip_prefix = \"rules_cuda-{git_commit_hash}\",\n    urls = [\"https://github.com/bazel-contrib/rules_cuda/archive/{git_commit_hash}.tar.gz\"],\n)\nload(\"@rules_cuda//cuda:repositories.bzl\", \"rules_cuda_dependencies\", \"rules_cuda_toolchains\")\nrules_cuda_dependencies()\nrules_cuda_toolchains(register_toolchains = True)\n```\n\n`rules_cc` needs to be available before loading `rules_cuda`, and `compatibility_proxy_repo()` must be called to populate the compatibility repository that `rules_cc` expects.\n\n**NOTE**: `rules_cuda_toolchains` implicitly calls `register_detected_cuda_toolchains`, and the use of\n`register_detected_cuda_toolchains` depends on the auto-detection of installed CUDA toolkits.\n\n\u003c/details\u003e\n\n### Toolchain Detection\n\nFor hermetic toolchains, the rules handle toolchain configuration and library downloading automatically.\nSee [cuda.redist_json integration test](tests/integration/toolchain_redist_json) for a comprehensible example.\n\nFor locally installed toolchains,\n[`_detect_local_cuda_toolkit`](https://github.com/bazel-contrib/rules_cuda/blob/ce98e4ae5c/cuda/private/repositories.bzl#L30-L45)\nand [`detect_clang`](https://github.com/bazel-contrib/rules_cuda/blob/ce98e4ae5c/cuda/private/repositories.bzl#L215-L256)\ndetermines how they are detected.\n\nEither situation depends on cc toolchain availability, so you must also ensure the cc compiler is properly configured.\nOn Windows, this means that you will also need to set the environment variable `BAZEL_VC` properly.\n\n### Rules\n\n- `cuda_library`: Can be used to compile and create static library for CUDA kernel code. The resulting targets can be\n  consumed by [C/C++ Rules](https://bazel.build/reference/be/c-cpp#rules).\n- `cuda_objects`: If you don't understand what _device link_ means, you must never use it. This rule produces incomplete\n  object files that can only be consumed by `cuda_library`. It is created for relocatable device code and device link\n  time optimization source files.\n\n### Macros\n\n- `cuda_binary`: A convenience macro for building CUDA-enabled executables.\n  It builds a `cc_binary`-style target from CUDA sources.\n- `cuda_test`: A convenience macro for CUDA-enabled tests.\n  It behaves like `cuda_binary` but creates a `cc_test`-style target that can be run with `bazel test`.\n\n### Flags\n\nSome flags are defined in [cuda/BUILD.bazel](cuda/BUILD.bazel). To use them, for example:\n\n```\nbazel build --@rules_cuda//cuda:archs=compute_61:compute_61,sm_61\n```\n\nIn `.bazelrc` file, you can define a shortcut alias for the flag, for example:\n\n```\n# Convenient flag shortcuts.\nbuild --flag_alias=cuda_archs=@rules_cuda//cuda:archs\n```\n\nand then you can use it as follows:\n\n```\nbazel build --cuda_archs=compute_61:compute_61,sm_61\n```\n\n#### Available flags\n\n- `@rules_cuda//cuda:enable`\n\n  Enable or disable all rules_cuda related rules. When disabled, the detected CUDA toolchains will also be disabled to avoid potential human error.\n  By default, rules_cuda rules are enabled. See `examples/if_cuda` for how to support both cuda-enabled and cuda-free builds.\n\n- `@rules_cuda//cuda:archs`\n\n  Select the CUDA archs to support. See [cuda_archs specification DSL grammar](https://github.com/bazel-contrib/rules_cuda/blob/5633f0c0f7/cuda/private/rules/flags.bzl#L14-L44).\n\n- `@rules_cuda//cuda:compiler`\n\n  Select the CUDA compiler; available options are `nvcc` or `clang`.\n\n- `@rules_cuda//cuda:copts`\n\n  Add copts to all CUDA compile actions.\n\n- `@rules_cuda//cuda:host_copts`\n\n  Add copts to the host compiler.\n\n- `@rules_cuda//cuda:runtime`\n\n  Set the default cudart to link; for example, `--@rules_cuda//cuda:runtime=@cuda//:cuda_runtime_static` links the static CUDA runtime.\n\n- `--features=cuda_device_debug`\n\n  Sets nvcc flags to enable debug information in device code.\n  Currently ignored for clang, where `--compilation_mode=debug` applies to both\n  host and device code.\n\n## Examples\n\nCheck out the examples to see if they fit your needs.\n\nSee [examples](./examples) for basic usage.\n\nSee [rules_cuda_examples](https://github.com/cloudhan/rules_cuda_examples) for extended real-world projects.\n\n## Known issue\n\nSometimes the following error occurs:\n\n```\ncc1plus: fatal error: /tmp/tmpxft_00000002_00000019-2.cpp: No such file or directory\n```\n\nThe problem is caused by nvcc using PIDs to determine temporary file names, and with `--spawn_strategy linux-sandbox`, which is the default strategy on Linux, the PIDs nvcc sees are all very small numbers (say 2~4) due to sandboxing. `linux-sandbox` is not hermetic because [it mounts root into the sandbox](https://docs.bazel.build/versions/main/command-line-reference.html#flag--experimental_use_hermetic_linux_sandbox), so `/tmp` is shared between sandboxes, which causes name conflicts under high parallelism. A similar problem has been reported on the [NVIDIA forums](https://forums.developer.nvidia.com/t/avoid-generating-temp-files-in-tmp-while-nvcc-compiling/197657/10).\n\nTo avoid it:\n\n- Update to Bazel 7 where `--incompatible_sandbox_hermetic_tmp` is enabled by default.\n- Using `--spawn_strategy local` should eliminate the case because it lets nvcc see the true PIDs.\n- Using `--experimental_use_hermetic_linux_sandbox` should eliminate the case because it avoids sharing `/tmp`.\n- Adding the `-objtemp` option should reduce the chance of this happening.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbazel-contrib%2Frules_cuda","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbazel-contrib%2Frules_cuda","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbazel-contrib%2Frules_cuda/lists"}