$npx -y skills add mohitmishra786/low-level-dev-skills --skill bazelBazel build system skill for C/C++ projects. Use when writing BUILD files with cc_library and cc_binary rules, registering toolchains, configuring remote execution, debugging sandbox issues, using query and cquery for dependency graphs, or migrating from CMake to Bazel. Activates
| 1 | # Bazel |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Guide agents through Bazel for C/C++ projects: writing BUILD files, cc_library/cc_binary rules, toolchain registration, remote execution, dependency graph queries, Bzlmod dependency management, and sandbox debugging. |
| 6 | |
| 7 | ## Triggers |
| 8 | |
| 9 | - "How do I write a Bazel BUILD file for a C++ library?" |
| 10 | - "How do I add external dependencies with Bzlmod?" |
| 11 | - "How do I debug Bazel sandbox errors?" |
| 12 | - "How do I query the Bazel dependency graph?" |
| 13 | - "How do I set up remote execution with Bazel?" |
| 14 | - "How do I register a custom C++ toolchain?" |
| 15 | |
| 16 | ## Workflow |
| 17 | |
| 18 | ### 1. Workspace structure |
| 19 | |
| 20 | ``` |
| 21 | my-project/ |
| 22 | ├── MODULE.bazel # Bzlmod dependency file (Bazel ≥6) |
| 23 | ├── WORKSPACE # legacy (still needed for some features) |
| 24 | ├── BUILD # root build file |
| 25 | ├── src/ |
| 26 | │ ├── BUILD |
| 27 | │ └── main.cc |
| 28 | └── lib/ |
| 29 | ├── BUILD |
| 30 | ├── mylib.cc |
| 31 | └── mylib.h |
| 32 | ``` |
| 33 | |
| 34 | ### 2. Basic BUILD file — cc_library / cc_binary |
| 35 | |
| 36 | ```python |
| 37 | # lib/BUILD |
| 38 | cc_library( |
| 39 | name = "mylib", |
| 40 | srcs = ["mylib.cc"], |
| 41 | hdrs = ["mylib.h"], |
| 42 | copts = ["-Wall", "-Wextra", "-std=c++17"], |
| 43 | visibility = ["//visibility:public"], |
| 44 | deps = [ |
| 45 | "@com_google_absl//absl/strings", # external dep |
| 46 | "//util:helpers", # internal dep |
| 47 | ], |
| 48 | ) |
| 49 | |
| 50 | cc_test( |
| 51 | name = "mylib_test", |
| 52 | srcs = ["mylib_test.cc"], |
| 53 | deps = [ |
| 54 | ":mylib", |
| 55 | "@com_google_googletest//:gtest_main", |
| 56 | ], |
| 57 | ) |
| 58 | ``` |
| 59 | |
| 60 | ```python |
| 61 | # src/BUILD |
| 62 | cc_binary( |
| 63 | name = "main", |
| 64 | srcs = ["main.cc"], |
| 65 | deps = ["//lib:mylib"], |
| 66 | linkopts = ["-lpthread"], |
| 67 | ) |
| 68 | ``` |
| 69 | |
| 70 | ```bash |
| 71 | # Build |
| 72 | bazel build //src:main |
| 73 | bazel build //... # build everything |
| 74 | |
| 75 | # Test |
| 76 | bazel test //lib:mylib_test |
| 77 | bazel test //... # run all tests |
| 78 | |
| 79 | # Run |
| 80 | bazel run //src:main -- arg1 arg2 |
| 81 | |
| 82 | # Output path |
| 83 | bazel-bin/src/main |
| 84 | ``` |
| 85 | |
| 86 | ### 3. Bzlmod — modern dependency management |
| 87 | |
| 88 | ```python |
| 89 | # MODULE.bazel |
| 90 | module( |
| 91 | name = "my_project", |
| 92 | version = "1.0", |
| 93 | ) |
| 94 | |
| 95 | bazel_dep(name = "abseil-cpp", version = "20240116.2") |
| 96 | bazel_dep(name = "googletest", version = "1.14.0") |
| 97 | bazel_dep(name = "rules_cc", version = "0.0.9") |
| 98 | bazel_dep(name = "platforms", version = "0.0.8") |
| 99 | |
| 100 | # For http_archive deps not yet in BCR (Bazel Central Registry) |
| 101 | # Use module extensions |
| 102 | ``` |
| 103 | |
| 104 | ```bash |
| 105 | # Check available versions |
| 106 | bazel mod graph | head -30 |
| 107 | |
| 108 | # Show dependency tree |
| 109 | bazel mod deps --depth=2 |
| 110 | ``` |
| 111 | |
| 112 | ### 4. Dependency graph queries |
| 113 | |
| 114 | ```bash |
| 115 | # Show all dependencies of a target |
| 116 | bazel query "deps(//src:main)" |
| 117 | |
| 118 | # Find reverse dependencies (who depends on this?) |
| 119 | bazel query "rdeps(//..., //lib:mylib)" |
| 120 | |
| 121 | # Find what changed between builds |
| 122 | bazel query "filter('//lib', deps(//src:main))" |
| 123 | |
| 124 | # cquery — configuration-aware query |
| 125 | bazel cquery "deps(//src:main)" --output=files |
| 126 | |
| 127 | # Show include paths for a target |
| 128 | bazel cquery "//lib:mylib" --output=build |
| 129 | |
| 130 | # Find cycles in the build graph |
| 131 | bazel query --nohost_deps --noimplicit_deps \ |
| 132 | "somepath(//src:main, //lib:mylib)" |
| 133 | |
| 134 | # aquery — action graph (shows compiler flags, inputs, outputs) |
| 135 | bazel aquery "//src:main" |
| 136 | bazel aquery "//src:main" --output=jsonproto | jq '.actions[0].arguments' |
| 137 | ``` |
| 138 | |
| 139 | ### 5. Toolchain registration |
| 140 | |
| 141 | ```python |
| 142 | # platforms/BUILD |
| 143 | platform( |
| 144 | name = "linux_x86_64", |
| 145 | constraint_values = [ |
| 146 | "@platforms//os:linux", |
| 147 | "@platforms//cpu:x86_64", |
| 148 | ], |
| 149 | ) |
| 150 | |
| 151 | # toolchains/BUILD |
| 152 | cc_toolchain_suite( |
| 153 | name = "my_toolchain", |
| 154 | toolchains = { |
| 155 | "k8": ":my_k8_toolchain", |
| 156 | }, |
| 157 | ) |
| 158 | |
| 159 | cc_toolchain( |
| 160 | name = "my_k8_toolchain", |
| 161 | all_files = ":empty", |
| 162 | compiler_files = ":compiler_wrapper", |
| 163 | # ... other file groups |
| 164 | ) |
| 165 | |
| 166 | toolchain( |
| 167 | name = "my_cc_toolchain", |
| 168 | toolchain = ":my_k8_toolchain", |
| 169 | toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", |
| 170 | target_compatible_with = ["@platforms//os:linux"], |
| 171 | ) |
| 172 | ``` |
| 173 | |
| 174 | ```python |
| 175 | # MODULE.bazel — register toolchain |
| 176 | register_toolchains("//toolchains:my_cc_toolchain") |
| 177 | ``` |
| 178 | |
| 179 | ### 6. Remote execution |
| 180 | |
| 181 | ```bash |
| 182 | # Use --remote_executor flag |
| 183 | bazel build //... \ |
| 184 | --remote_executor=grpc://buildgrid.example.com:50051 \ |
| 185 | --remote_instance_name=main |
| 186 | |
| 187 | # Google Cloud Build Remote Execution |
| 188 | bazel build //... \ |
| 189 | --remote_executor=remotebuildexecution.googleapis.com \ |
| 190 | --remote_instance_name=projects/my-project/instances/default \ |
| 191 | --google_default_credentials |
| 192 | |
| 193 | # Caching (without remote execution) |
| 194 | bazel build //... \ |
| 195 | --remote_cache=grpc://cache.example.com:9092 |
| 196 | |
| 197 | # Show what's cached |
| 198 | bazel build //... --remote_upload_local_results=true |
| 199 | ``` |
| 200 | |
| 201 | ### 7. Sandbox debugging |
| 202 | |
| 203 | ```bash |
| 204 | # Show sandbox inputs and outputs |
| 205 | bazel build //src:main --sandbox_debug |
| 206 | |
| 207 | # Run build with verbos |