aboutsummaryrefslogtreecommitdiff
path: root/examples/custom_toolchain/BUILD
blob: 371fdfd224abcd14f21add86358969d6379486f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright 2021 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Proof-of-concept example showing how to write a custom C++ toolchain.
#
# Important documentation:
#
# - https://docs.bazel.build/versions/master/platforms-intro.html#c
# - https://docs.bazel.build/versions/master/tutorial/cc-toolchain-config.html
# - https://docs.bazel.build/versions/master/be/c-cpp.html#cc_toolchain
#
# There are two ways to select C++ toolchains:
#
#  - NEW (USE IF POSSIBLE): with the --platforms flag
#  - LEGACY: with the --crosstool_top and --cpu flags
#
# See https://docs.bazel.build/versions/master/platforms-intro.html#c for details.
#
# This example demonstrates both approaches.

load("@rules_cc//cc:defs.bzl", "cc_library", "cc_toolchain", "cc_toolchain_suite")

# Load the Starlark logic defining the toolchain's behavior. For example: what
# program runs to compile a source file and how its command line is
# constructed. See toolchain_config.bzl for details.
load(":toolchain_config.bzl", "cc_toolchain_config")

# The library we want to build. Building this calls two C++ actions: compile (.cc ->
# .o) and archive (.o -> .a).
cc_library(
    name = "buildme",
    srcs = ["buildme.cc"],
)

# This example intentionally makes the cc_toolchain_config definition
# simple. You could alternative add attributes to support multiple
# cc_toolchain_config targets with finer customization.
cc_toolchain_config(
    name = "toolchain_semantics",
)

# Register the toolchain with Bazel. Most of these attribute just tell Bazel
# where to find the files needed to run C++ commands. The toolchain_config
# attribute registers the behavior specification declared above.
cc_toolchain(
    name = "my_custom_toolchain",
    all_files = ":toolchain_files",
    ar_files = ":toolchain_files",
    compiler_files = ":toolchain_files",
    dwp_files = ":toolchain_files",
    linker_files = ":toolchain_files",
    objcopy_files = ":toolchain_files",
    strip_files = ":toolchain_files",
    toolchain_config = ":toolchain_semantics",
)

filegroup(
    name = "toolchain_files",
    srcs = [
        "sample_compiler",
        "sample_linker",
    ],
)

# Implements legacy toolchain selection.
#
# Setting --crosstool_top here registers the set of available
# toolchains. Setting --cpu to one of the toolchain attribute's keys selects a
#toolchain.
cc_toolchain_suite(
    name = "legacy_selector",
    toolchains = {
        "x86": ":my_custom_toolchain",
    },
)

# Implements platform-based (recommended) toolchain selection.
#
# See https://docs.bazel.build/versions/master/platforms-intro.html. The main
# differences are:
#
#  1. --cpu / --crosstool_top are replaced by a platform() definition with
#       much more customizable properties. For example, a platform can specify
#       OS, device type (server, phone, tablet) or custom hardware extensions.
#  2. All languages can support platform-based toolchains. A single --platforms
#       value can choose C++, Python, Scala, and all other toolchains in your
#       build. This is especially useful for multi-language builds.
#  3. Platforms  support features like incompatible target skipping:
#       https://docs.bazel.build/versions/master/platforms.html#skipping-incompatible-targets.
toolchain(
    name = "platform_based_toolchain",
    # Trigger this toolchain for x86-compatible platforms.
    # See https://github.com/bazelbuild/platforms.
    target_compatible_with = ["@platforms//cpu:x86_64"],
    # Register this toolchain with platforms.
    toolchain = ":my_custom_toolchain",
    # The public interface for all C++ toolchains. Starlark rules that use C++
    # access the toolchain through this interface.
    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

# Define a platform matching any x86-compatible toolchain. See
# https://docs.bazel.build/versions/master/platforms.html.
platform(
    name = "x86_platform",
    constraint_values = ["@platforms//cpu:x86_64"],
)