Configuring a custom C toolchain ================================ .. External links are here .. _Configuring CROSSTOOL: https://docs.bazel.build/versions/0.23.0/tutorial/crosstool.html .. _Understanding CROSSTOOL: https://docs.bazel.build/versions/0.23.0/crosstool-reference.html .. _Configuring C++ toolchains: https://docs.bazel.build/versions/master/tutorial/cc-toolchain-config.html .. _cc_library: https://docs.bazel.build/versions/master/be/c-cpp.html#cc_library .. _crosstool_config.proto: https://github.com/bazelbuild/bazel/blob/master/src/main/protobuf/crosstool_config.proto .. _go_binary: docs/go/core/rules.md#go_binary .. _go_library: docs/go/core/rules.md#go_library .. _toolchain: https://docs.bazel.build/versions/master/be/platform.html#toolchain .. _#1642: https://github.com/bazelbuild/rules_go/issues/1642 References ---------- * `Configuring CROSSTOOL`_ * `Understanding CROSSTOOL`_ * `Configuring C++ toolchains`_ Introduction ------------ **WARNING:** This documentation is out of date. Some of the linked Bazel documentation has been deleted in later versions, and there are a number of TODOs. In particular, building and configuring a cross-compiling C++ toolchain and testing it with cgo should be covered. `#1642`_ tracks progress on this. The Go toolchain sometimes needs access to a working C/C++ toolchain in order to produce binaries that contain cgo code or require external linking. rules_go uses whatever C/C++ toolchain Bazel is configured to use. This means `go_library`_ and `cc_library`_ rules can be linked into the same binary (via the ``cdeps`` attribute in Go rules). Bazel uses a CROSSTOOL file to configure the C/C++ toolchain, plus a few build rules that declare constraints, dependencies, and file groups. By default, Bazel will attempt to detect the toolchain installed on the host machine. This works in most cases, but it's not hermetic (developers may have completely different toolchains installed), and it doesn't always work with remote execution. It also doesn't work with cross-compilation. Explicit configuration is required in these situations. This documented is intended to serve as a walk-through for configuring a custom C/C++ toolchain for use with rules_go. NOTE: The Go toolchain requires gcc, clang, or something that accepts the same command-line arguments and produce the same error messages. MSVC is not supported. This is a limitation of the Go toolchain, not rules_go. cgo infers the types of C definitions based on the text of error messages. TODO: Change the example to use a cross-compiling toolchain. TODO: Add instructions for building a C compiler from scratch. TODO: Build the standard C library and binutils for use with this toolchain. Tutorial -------- In this tutorial, we'll download a binary Clang release and install it into a new workspace. This workspace can be uploaded into a new repository and referenced from other Bazel workspaces. You can find a copy of the example repository described here at `https://github.com/jayconrod/bazel_cc_toolchains `_. Step 1: Create the repository ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Create a new repository and add a WORKSPACE file to the root directory. It may be empty, but it's probably a good idea to give it a name. .. code:: bash $ cat >WORKSPACE <example/hello.c < int main() { printf("Hello, world!\n"); return 0; } EOF $ cat >example/BUILD.bazel <>.bazelrc <hello.go < void say_hello() { printf("Hello, world!\n"); } */ import "C" func main() { C.say_hello() } EOF $ cat >BUILD.bazel <