Skip to content

Bazel toolchain configuration GCC 10

Razvan STANCIOIU edited this page Jan 28, 2021 · 1 revision

Bazel custom toolchain configuration with GCC10 (for C++20)

Configure packages


  • Install gcc-10 and g++-10:
> sudo apt-get install gcc-10 g++-10

Create toolchain/BUILD


  • Create a toolchain file BUILD. This file will contain the configuration of the toolchain. Let's place it in a folder named toolchain:
📦SR2TR
 ┣ 📂lib
 ┃ ┣ 📜BUILD
 ┃ ┣ 📜*.cpp
 ┃ ┗ 📜*.hpp
 ┣ 📂toolchain
 ┃ ┗ 📜BUILD
 ┣ 📜.bazelrc
 ┣ 📜.bazelversion
 ┗ 📜WORKSPACE

Create Starlark rules in toolchain/BUILD


  • Define the package vizibility so that it can be seen outside of the toolchain directory
package(default_visibility = ["//visibility:public"])
  • Load the rules needed to create the toolchain: cc_toolchain, cc_toolchain_suite, cc_toolchain_config
load("@rules_cc//cc:defs.bzl", "cc_toolchain", "cc_toolchain_suite")
load("@local_config_cc//:cc_toolchain_config.bzl", "cc_toolchain_config")
  • Define the toolchain suite rule cc_toolchain_suite and link gcc10_toolchain to it (cc_toolchain rule explained below):
cc_toolchain_suite(
    name = "gcc10_toolchain_suite",
    toolchains = {
        "k8": ":gcc10_toolchain",
    },
)
  • Define the toolchain rule *cc_toolchain. The file group empty is the declaration of an empty group of files which are associated to the different parameters of the toolchain. The toolchain config gcc10_toolchain_config is linked to the toolchain rule:
filegroup(
    name = "empty",
    srcs = []
)

cc_toolchain(
    name = "gcc10_toolchain",
    toolchain_identifier = "gcc10-toolchain",
    toolchain_config = ":gcc10_toolchain_config",
    all_files = ":empty",
    compiler_files = ":empty",
    dwp_files = ":empty",
    linker_files = ":empty",
    objcopy_files = ":empty",
    strip_files = ":empty",
    supports_param_files = 0,
)
cc_toolchain_config(
    name = "gcc10_toolchain_config",
    cpu = "k8",
    compiler = "compiler",
    toolchain_identifier = "local",
    host_system_name = "local",
    target_system_name = "local",
    target_libc = "local",
    abi_version = "local",
    abi_libc_version = "local",
    cxx_builtin_include_directories = ["/usr/lib/gcc/x86_64-linux-gnu/10/include",
    "/usr/local/include",
    "/usr/include/x86_64-linux-gnu",
    "/usr/include",
    "/usr/include/c++/10",
    "/usr/include/x86_64-linux-gnu/c++/10",
    "/usr/include/c++/10/backward"],
    tool_paths = {
        "ar": "/usr/bin/ar",
        "ld": "/usr/bin/ld",
        "llvm-cov": "/usr/bin/llvm-cov-10",
        "cpp": "/usr/bin/cpp-10",
        "gcc": "/usr/bin/gcc-10",
        "dwp": "/usr/bin/dwp",
        "gcov": "/usr/bin/gcov-10",
        "nm": "/usr/bin/nm",
        "objcopy": "/usr/bin/objcopy",
        "objdump": "/usr/bin/objdump",
        "strip": "/usr/bin/strip"
    },
    compile_flags = ["-U_FORTIFY_SOURCE",
    "-fstack-protector",
    "-Wall",
    "-Wunused-but-set-parameter",
    "-Wno-free-nonheap-object",
    "-fno-omit-frame-pointer"],
    opt_compile_flags = ["-g0",
    "-O2",
    "-D_FORTIFY_SOURCE=1",
    "-DNDEBUG",
    "-ffunction-sections",
    "-fdata-sections"],
    dbg_compile_flags = ["-g"],
    cxx_flags = ["-std=c++20"],
    link_flags = ["-fuse-ld=gold",
    "-Wl,-no-as-needed",
    "-Wl,-z,relro,-z,now",
    "-B/usr/bin",
    "-pass-exit-codes",
    "-lstdc++",
    "-lm"],
    link_libs = [],
    opt_link_flags = ["-Wl,--gc-sections"],
    unfiltered_compile_flags = ["-fno-canonical-system-headers",
    "-Wno-builtin-macro-redefined",
    "-D__DATE__=\"redacted\"",
    "-D__TIMESTAMP__=\"redacted\"",
    "-D__TIME__=\"redacted\""],
    coverage_compile_flags = ["--coverage"],
    coverage_link_flags = ["--coverage"],
    supports_start_end_lib = True,
)

Link the custom toolchain to bazel


  • Add in .bazelrc the following command:
build --crosstool_top=//toolchain:gcc10_toolchain_suite

Bazel will then choose the custom defined toolchain rule //toolchain:gcc10_toolchain_suite

Example


A working example can be found in https://github.com/rstancioiu/SR2TR.

Credits


Bazel toolchain configuration: https://docs.bazel.build/versions/master/tutorial/cc-toolchain-config.html.