1# Copyright 2021 The Bazel Authors. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15# Proof-of-concept example showing how to write a custom C++ toolchain. 16# 17# Important documentation: 18# 19# - https://docs.bazel.build/versions/master/platforms-intro.html#c 20# - https://docs.bazel.build/versions/master/tutorial/cc-toolchain-config.html 21# - https://docs.bazel.build/versions/master/be/c-cpp.html#cc_toolchain 22# 23# There are two ways to select C++ toolchains: 24# 25# - NEW (USE IF POSSIBLE): with the --platforms flag 26# - LEGACY: with the --crosstool_top and --cpu flags 27# 28# See https://docs.bazel.build/versions/master/platforms-intro.html#c for details. 29# 30# This example demonstrates both approaches. 31 32load("@rules_cc//cc:defs.bzl", "cc_library", "cc_toolchain", "cc_toolchain_suite") 33 34# Load the Starlark logic defining the toolchain's behavior. For example: what 35# program runs to compile a source file and how its command line is 36# constructed. See toolchain_config.bzl for details. 37load(":toolchain_config.bzl", "cc_toolchain_config") 38 39# The library we want to build. Building this calls two C++ actions: compile (.cc -> 40# .o) and archive (.o -> .a). 41cc_library( 42 name = "buildme", 43 srcs = ["buildme.cc"], 44) 45 46# This example intentionally makes the cc_toolchain_config definition 47# simple. You could alternative add attributes to support multiple 48# cc_toolchain_config targets with finer customization. 49cc_toolchain_config( 50 name = "toolchain_semantics", 51) 52 53# Register the toolchain with Bazel. Most of these attribute just tell Bazel 54# where to find the files needed to run C++ commands. The toolchain_config 55# attribute registers the behavior specification declared above. 56cc_toolchain( 57 name = "my_custom_toolchain", 58 all_files = ":toolchain_files", 59 ar_files = ":toolchain_files", 60 compiler_files = ":toolchain_files", 61 dwp_files = ":toolchain_files", 62 linker_files = ":toolchain_files", 63 objcopy_files = ":toolchain_files", 64 strip_files = ":toolchain_files", 65 toolchain_config = ":toolchain_semantics", 66) 67 68filegroup( 69 name = "toolchain_files", 70 srcs = [ 71 "sample_compiler", 72 "sample_linker", 73 ], 74) 75 76# Implements legacy toolchain selection. 77# 78# Setting --crosstool_top here registers the set of available 79# toolchains. Setting --cpu to one of the toolchain attribute's keys selects a 80#toolchain. 81cc_toolchain_suite( 82 name = "legacy_selector", 83 toolchains = { 84 "x86": ":my_custom_toolchain", 85 }, 86) 87 88# Implements platform-based (recommended) toolchain selection. 89# 90# See https://docs.bazel.build/versions/master/platforms-intro.html. The main 91# differences are: 92# 93# 1. --cpu / --crosstool_top are replaced by a platform() definition with 94# much more customizable properties. For example, a platform can specify 95# OS, device type (server, phone, tablet) or custom hardware extensions. 96# 2. All languages can support platform-based toolchains. A single --platforms 97# value can choose C++, Python, Scala, and all other toolchains in your 98# build. This is especially useful for multi-language builds. 99# 3. Platforms support features like incompatible target skipping: 100# https://docs.bazel.build/versions/master/platforms.html#skipping-incompatible-targets. 101toolchain( 102 name = "platform_based_toolchain", 103 # Trigger this toolchain for x86-compatible platforms. 104 # See https://github.com/bazelbuild/platforms. 105 target_compatible_with = ["@platforms//cpu:x86_64"], 106 # Register this toolchain with platforms. 107 toolchain = ":my_custom_toolchain", 108 # The public interface for all C++ toolchains. Starlark rules that use C++ 109 # access the toolchain through this interface. 110 toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", 111) 112 113# Define a platform matching any x86-compatible toolchain. See 114# https://docs.bazel.build/versions/master/platforms.html. 115platform( 116 name = "x86_platform", 117 constraint_values = ["@platforms//cpu:x86_64"], 118) 119