{"id":28534911,"url":"https://github.com/bazelembedded/rules_cc_toolchain","last_synced_at":"2025-07-23T15:33:58.838Z","repository":{"id":40672725,"uuid":"377435612","full_name":"bazelembedded/rules_cc_toolchain","owner":"bazelembedded","description":"A new hermetic toolchain for bazel","archived":false,"fork":false,"pushed_at":"2023-02-03T00:52:16.000Z","size":99,"stargazers_count":34,"open_issues_count":13,"forks_count":10,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-07-19T03:34:33.082Z","etag":null,"topics":["bazel","llvm"],"latest_commit_sha":null,"homepage":"https://www.nb.rough.run/tags/bazel/","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/bazelembedded.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":null,"security":null,"support":null}},"created_at":"2021-06-16T09:06:10.000Z","updated_at":"2024-06-10T08:30:39.000Z","dependencies_parsed_at":"2023-02-16T23:00:31.584Z","dependency_job_id":null,"html_url":"https://github.com/bazelembedded/rules_cc_toolchain","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bazelembedded/rules_cc_toolchain","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazelembedded%2Frules_cc_toolchain","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazelembedded%2Frules_cc_toolchain/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazelembedded%2Frules_cc_toolchain/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazelembedded%2Frules_cc_toolchain/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bazelembedded","download_url":"https://codeload.github.com/bazelembedded/rules_cc_toolchain/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bazelembedded%2Frules_cc_toolchain/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266704332,"owners_count":23971459,"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","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","llvm"],"created_at":"2025-06-09T17:13:24.142Z","updated_at":"2025-07-23T15:33:58.815Z","avatar_url":"https://github.com/bazelembedded.png","language":"Starlark","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rules_cc_toolchain\n[![CI](https://github.com/silvergasp/bazel_rules_cc_toolchain/actions/workflows/blank.yml/badge.svg)](https://github.com/silvergasp/bazel_rules_cc_toolchain/actions/workflows/blank.yml)\n\nAn opinionated hermetic host toolchain for Bazel and C++. Currently this\ntoolchain supports;\n- Completely sandboxed linux builds (i.e. no system deps).\n- Code coverage and combined lcov reports e.g.\n  `bazel coverage //...`\n- Static analysis (with clang-tidy). Read the\n  [docs here](tools/clang_tidy/README.md).\n\nThe toolchain is modular enough that you should be able to BYO;\n- Compiler and runtime\n- libc\n- libc++ / libc++abi\n- Startup libraries (e.g. crt1.o)\n- Injected toolchain headers and libs\n\n\n## Getting Started\nAdd the following to your workspace file;\n\n```py\n# WORKSPACE\nload(\"@bazel_tools//tools/build_defs/repo:git.bzl\", \"git_repository\")\n\n# Set up host hermetic host toolchain.\ngit_repository(\n    name = \"rules_cc_toolchain\",\n    commit = \"dd9265e3ce0daa444911040430bd716076869b34\",\n    remote = \"https://github.com/bazelembedded/rules_cc_toolchain.git\",\n)\n\nload(\"@rules_cc_toolchain//:rules_cc_toolchain_deps.bzl\", \"rules_cc_toolchain_deps\")\n\nrules_cc_toolchain_deps()\n\nload(\"@rules_cc_toolchain//cc_toolchain:cc_toolchain.bzl\", \"register_cc_toolchains\")\n\nregister_cc_toolchains()\n```\n\nand the following to your '.bazelrc';\n\n```\n# Enforce strict checks of deprecated toolchain api.\nbuild --incompatible_require_linker_input_cc_api\n\n# Use new cc toolchain resolution api\nbuild --incompatible_enable_cc_toolchain_resolution\n\n# Code coverage support\ncoverage --combined_report=lcov\ncoverage --experimental_use_llvm_covmap\ncoverage --experimental_generate_llvm_lcov\ncoverage --incompatible_cc_coverage\n```\n\n## Defaults and config\nThis repository provides a set of sane defaults to make up a\n[complete toolchain](https://clang.llvm.org/docs/Toolchain.html). By default;\n\n| Library           | Provider                                           |\n| ----------------- | -------------------------------------------------- |\n| libc              | Debian stretch sysroot (GNU glibc6)                |\n| libc++            | LLVM 15.0.6 libc++                                 |\n| libc++abi         | LLVM 15.0.6 libc++abi                              |\n| libunwind         | Debian stretch sysroot (GNU gcc6 compiler runtime) |\n| Startup Libraries | Debian stretch libcrt (GNU glibc6)                 |\n\nThis can be viewed in more detail by inspecting `//config/config:BUILD.bazel`.\n\n\n## Bring your own system libraries (Advanced)\nThis toolchain supports bringing your own precompiled system libraries.\nImplementing this can be somewhat complicated and it is unlikely that we will\nsupport specific combinations of libraries on request, although PR's here are\nwelcome.\n\nAn example of how you might include your own version of libc++ is shown below.\n\nAdd a third_party dependency directory for your libc++ build definitions.\n``` sh\nmkdir third_party\ntouch third_party/clang_llvm_x86_64_linux_gnu_ubuntu.BUILD\n```\n\nAdd the following to your WORKSPACE file.\n``` py\n# WORKSPACE\nhttp_archive(\n    name = \"clang_llvm_x86_64_linux_gnu_ubuntu\",\n    build_file = \"//third_party:clang_llvm_x86_64_linux_gnu_ubuntu.BUILD\",\n    sha256 = \"9694f4df031c614dbe59b8431f94c68631971ad44173eecc1ea1a9e8ee27b2a3\",\n    strip_prefix = \"clang+llvm-15.0.6-x86_64-linux-gnu-ubuntu-18.04\",\n    url = \"https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.6/clang+llvm-15.0.6-x86_64-linux-gnu-ubuntu-18.04.tar.xz\",\n)\n```\n\nFrom here you will need to add in your definitions that specify the library\nfiles that are provided by this implementation of libc++.\n``` py\n# third_party:clang_llvm_x86_64_linux_gnu_ubuntu.BUILD\nload(\n    \"@rules_cc_toolchain//cc_toolchain:cc_toolchain_import.bzl\",\n    \"cc_toolchain_import\",\n)\n\ncc_toolchain_import(\n    name = \"llvm_libcxx\",\n    # All the headers to be included with this library\n    hdrs = glob([\"include/c++/v1/**\"]),\n    # It is common for a library e.g. libc++.so to actually just be a linker\n    # script that points to a different library as is the case here where,\n    # 'lib/libc++.so' is a linker script that points to 'lib/libc++.so.1'.\n    # This is done so that dynamic linker can ensure that it is linking\n    # against a compatible version of the library ABI (in this case version 1).\n    additional_libs = [\n        \"lib/libc++.so.1\",\n    ],\n    includes = [\"include/c++/v1\"],\n\n    # NOTE: If one of static_library or shared_library is omitted this toolchain\n    # will default to the other. This is useful if you want to static link a\n    # particular library. It is also possible to omit both static_library and\n    # shared_library, creating a header only toolchain lib.\n    # Use with statically linkage.\n    static_library = \"lib/x86_64-unknown-linux-gnu/libc++.a\",\n    # Use with shared linkage.\n    shared_library = \"lib/libc++.so\",\n\n    # This is a usefult sanity check to say that this library can only be\n    # targetted at Linux on an x86 machine.\n    target_compatible_with = select({\n        \"@platforms//os:linux\": [\"@platforms//cpu:x86_64\"],\n        \"//conditions:default\": [\"@platforms//:incompatible\"],\n    }),\n\n    # Here we make sure that this target is visible from the configuration layer.\n    visibility = [\"@rules_cc_toolchain_config//:__pkg__\"],\n\n    # This version of libc++ depends on libc and libunwind, by specifying the\n    # dependency on the configuration layer rather than directly on the imported\n    # library itself we can ensure that we can swap out the version of libc with\n    # little effort.\n    deps = [\n        \"@rules_cc_toolchain_config//:libc\",\n        \"@rules_cc_toolchain_config//:libunwind\",\n    ],\n)\n```\n\nNow we can test the new toolchain to ensure that it functions correctly.\n``` sh\nbazel coverage @rules_cc_toolchain//tests/... \\\n --@rules_cc_toolchain_config//:libc++=@clang_llvm_x86_64_linux_gnu_ubuntu//:llvm_libcxx\n```\n\n**NOTE:** While the toolchain itself is hermetic the runtime linkage is not in\nthis example you will need to make sure that you have a recent version of LLVMs\nlibc++ installed on your system. If you would like to make sure that your\nruntime is hermetic use the static linking mode in Bazel, or simply omit the\nshared_library attribute to disable shared linkage for that specific library.\n\nYou can also opt to make these changes permanent by overriding the\n`rules_cc_toolchain_config` repository. e.g.\n\nCopy this repositories `config/rules_cc_toolchain_config.BUILD` to your own\nrepository. You can now update the `build_setting_default` for `libc++` to point\nto your implementation. To make this change final you can make use of the\n\n```py\n# WORKSPACE\nload(\"@bazel_tools//tools/build_defs/repo:git.bzl\", \"git_repository\")\nload(\"@bazel_tools//tools/build_defs/repo:http.bzl\", \"http_archive\")\n\nhttp_archive(\n    name = \"clang_llvm_x86_64_linux_gnu_ubuntu\",\n    build_file = \"//third_party:clang_llvm_x86_64_linux_gnu_ubuntu.BUILD\",\n    sha256 = \"9694f4df031c614dbe59b8431f94c68631971ad44173eecc1ea1a9e8ee27b2a3\",\n    strip_prefix = \"clang+llvm-15.0.6-x86_64-linux-gnu-ubuntu-18.04\",\n    url = \"https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.6/clang+llvm-15.0.6-x86_64-linux-gnu-ubuntu-18.04.tar.xz\",\n)\n\n# Set up host hermetic host toolchain.\ngit_repository(\n    name = \"rules_cc_toolchain\",\n    commit = \"dd9265e3ce0daa444911040430bd716076869b34\",\n    remote = \"https://github.com/silvergasp/rules_cc_toolchain.git\",\n)\n\n# (NEW)\nload(\"@rules_cc_toolchain//config:rules_cc_toolchain_config_repository.bzl\",\n    \"rules_cc_toolchain_config\")\n\n# (NEW) Must be called before rules_cc_toolchain_deps.\nrules_cc_toolchain_config(\n    name = \"rules_cc_toolchain_config\",\n    build_file = \"//config:rules_cc_toolchain_config.BUILD\",\n)\n\nload(\"@rules_cc_toolchain//:rules_cc_toolchain_deps.bzl\", \"rules_cc_toolchain_deps\")\n\nrules_cc_toolchain_deps()\n\nload(\"@rules_cc_toolchain//cc_toolchain:cc_toolchain.bzl\", \"register_cc_toolchains\")\n\nregister_cc_toolchains()\n```\n\n## Upgrading clang version\n\nThis repository hardcodes a particular clang version, currently 15.0.6. To\nupgrade, you must:\n\n1.  Update the `CLANG_VERSION` variable in\n    `third_party/clang_llvm_x86_64_linux_gnu_ubuntu.BUILD`.\n2. Update the `clang_llvm_x86_64_linux_gnu_ubuntu` `http_archive` in\n    `rules_cc_toolchain_deps.bzl`.\n\nIdeally, that would be it. Unfortunately, the exact locations of relevant files\n(e.g., static libraries) vary from one clang release to the next. In fact,\nit's not even guaranteed that a x86 Linux binary release will exist for a\ngiven clang version (https://github.com/llvm/llvm-project/issues/60201)!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbazelembedded%2Frules_cc_toolchain","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbazelembedded%2Frules_cc_toolchain","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbazelembedded%2Frules_cc_toolchain/lists"}