xref: /aosp_15_r20/external/bazelbuild-rules_cc/README.md (revision eed53cd41c5909d05eedc7ad9720bb158fd93452)
1*eed53cd4SHONG Yifan# C++ rules for Bazel
2*eed53cd4SHONG Yifan
3*eed53cd4SHONG Yifan* Postsubmit [![Build status](https://badge.buildkite.com/f03592ae2d7d25a2abc2a2ba776e704823fa17fd3e061f5103.svg?branch=main)](https://buildkite.com/bazel/rules-cc)
4*eed53cd4SHONG Yifan* Postsubmit + Current Bazel Incompatible flags [![Build status](https://badge.buildkite.com/5ba709cc33e5855078a1f8570adcf8e0a78ea93591bc0b4e81.svg?branch=master)](https://buildkite.com/bazel/rules-cc-plus-bazelisk-migrate)
5*eed53cd4SHONG Yifan
6*eed53cd4SHONG YifanThis repository contains a Starlark implementation of C++ rules in Bazel.
7*eed53cd4SHONG Yifan
8*eed53cd4SHONG YifanThe rules are being incrementally converted from their native implementations in the [Bazel source tree](https://source.bazel.build/bazel/+/master:src/main/java/com/google/devtools/build/lib/rules/cpp/).
9*eed53cd4SHONG Yifan
10*eed53cd4SHONG YifanFor the list of C++ rules, see the Bazel
11*eed53cd4SHONG Yifan[documentation](https://docs.bazel.build/versions/main/be/overview.html).
12*eed53cd4SHONG Yifan
13*eed53cd4SHONG Yifan# Getting Started
14*eed53cd4SHONG Yifan
15*eed53cd4SHONG YifanThere is no need to use rules from this repository just yet. If you want to use
16*eed53cd4SHONG Yifan`rules_cc` anyway, add the following to your `WORKSPACE` file:
17*eed53cd4SHONG Yifan
18*eed53cd4SHONG Yifan```starlark
19*eed53cd4SHONG Yifanload("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
20*eed53cd4SHONG Yifan
21*eed53cd4SHONG Yifanhttp_archive(
22*eed53cd4SHONG Yifan    name = "rules_cc",
23*eed53cd4SHONG Yifan    urls = ["https://github.com/bazelbuild/rules_cc/archive/refs/tags/<VERSION>.tar.gz"],
24*eed53cd4SHONG Yifan    sha256 = "...",
25*eed53cd4SHONG Yifan)
26*eed53cd4SHONG Yifan```
27*eed53cd4SHONG Yifan
28*eed53cd4SHONG YifanThen, in your `BUILD` files, import and use the rules:
29*eed53cd4SHONG Yifan
30*eed53cd4SHONG Yifan```starlark
31*eed53cd4SHONG Yifanload("@rules_cc//cc:defs.bzl", "cc_library")
32*eed53cd4SHONG Yifan
33*eed53cd4SHONG Yifancc_library(
34*eed53cd4SHONG Yifan    ...
35*eed53cd4SHONG Yifan)
36*eed53cd4SHONG Yifan```
37*eed53cd4SHONG Yifan
38*eed53cd4SHONG Yifan# Using the rules_cc toolchain
39*eed53cd4SHONG Yifan
40*eed53cd4SHONG YifanThis repo contains an auto-detecting toolchain that expects to find tools installed on your host machine.
41*eed53cd4SHONG YifanThis is non-hermetic, and may have varying behaviors depending on the versions of tools found.
42*eed53cd4SHONG Yifan
43*eed53cd4SHONG YifanThere are third-party contributed hermetic toolchains you may want to investigate:
44*eed53cd4SHONG Yifan
45*eed53cd4SHONG Yifan- LLVM: <https://github.com/grailbio/bazel-toolchain>
46*eed53cd4SHONG Yifan- GCC (Linux only): <https://github.com/aspect-build/gcc-toolchain>
47*eed53cd4SHONG Yifan- zig cc: <https://github.com/uber/hermetic_cc_toolchain>
48*eed53cd4SHONG Yifan
49*eed53cd4SHONG YifanIf you'd like to use the cc toolchain defined in this repo, add this to
50*eed53cd4SHONG Yifanyour WORKSPACE after you include rules_cc:
51*eed53cd4SHONG Yifan
52*eed53cd4SHONG Yifan```bzl
53*eed53cd4SHONG Yifanload("@rules_cc//cc:repositories.bzl", "rules_cc_dependencies", "rules_cc_toolchains")
54*eed53cd4SHONG Yifan
55*eed53cd4SHONG Yifanrules_cc_dependencies()
56*eed53cd4SHONG Yifan
57*eed53cd4SHONG Yifanrules_cc_toolchains()
58*eed53cd4SHONG Yifan```
59*eed53cd4SHONG Yifan
60*eed53cd4SHONG Yifan# Migration Tools
61*eed53cd4SHONG Yifan
62*eed53cd4SHONG YifanThis repository also contains migration tools that can be used to migrate your
63*eed53cd4SHONG Yifanproject for Bazel incompatible changes.
64*eed53cd4SHONG Yifan
65*eed53cd4SHONG Yifan## Legacy fields migrator
66*eed53cd4SHONG Yifan
67*eed53cd4SHONG YifanScript that migrates legacy crosstool fields into features
68*eed53cd4SHONG Yifan([incompatible flag](https://github.com/bazelbuild/bazel/issues/6861),
69*eed53cd4SHONG Yifan[tracking issue](https://github.com/bazelbuild/bazel/issues/5883)).
70*eed53cd4SHONG Yifan
71*eed53cd4SHONG YifanTLDR:
72*eed53cd4SHONG Yifan
73*eed53cd4SHONG Yifan```
74*eed53cd4SHONG Yifanbazel run @rules_cc//tools/migration:legacy_fields_migrator -- \
75*eed53cd4SHONG Yifan  --input=my_toolchain/CROSSTOOL \
76*eed53cd4SHONG Yifan  --inline
77*eed53cd4SHONG Yifan```
78*eed53cd4SHONG Yifan
79*eed53cd4SHONG Yifan# Contributing
80*eed53cd4SHONG Yifan
81*eed53cd4SHONG YifanBazel and `rules_cc` are the work of many contributors. We appreciate your help!
82*eed53cd4SHONG Yifan
83*eed53cd4SHONG YifanTo contribute, please read the contribution guidelines: [CONTRIBUTING.md](https://github.com/bazelbuild/rules_cc/blob/main/CONTRIBUTING.md).
84*eed53cd4SHONG Yifan
85*eed53cd4SHONG YifanNote that the `rules_cc` use the GitHub issue tracker for bug reports and feature requests only.
86*eed53cd4SHONG YifanFor asking questions see:
87*eed53cd4SHONG Yifan
88*eed53cd4SHONG Yifan* [Stack Overflow](https://stackoverflow.com/questions/tagged/bazel)
89*eed53cd4SHONG Yifan* [`rules_cc` mailing list](https://groups.google.com/forum/#!forum/cc-bazel-discuss)
90*eed53cd4SHONG Yifan* Slack channel `#cc` on [slack.bazel.build](https://slack.bazel.build)
91