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