1# Copyright 2024 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://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, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14 15load("@bazel_skylib//rules:native_binary.bzl", "native_binary") 16load("@rules_cc//cc/toolchains:args.bzl", "cc_args") 17load("@rules_cc//cc/toolchains:args_list.bzl", "cc_args_list") 18load("@rules_cc//cc/toolchains:tool.bzl", "cc_tool") 19load("@rules_cc//cc/toolchains:tool_map.bzl", "cc_tool_map") 20load("@bazel_skylib//rules/directory:directory.bzl", "directory") 21load("@bazel_skylib//rules/directory:subdirectory.bzl", "subdirectory") 22load("@pigweed//pw_build/constraints/cortex_m:lists.bzl", "ALL_CORTEX_M_CPUS") 23load("@pigweed//pw_build:glob_dirs.bzl", "match_dir") 24load("@bazel_skylib//lib:selects.bzl", "selects") 25 26package(default_visibility = ["//visibility:public"]) 27 28licenses(["notice"]) 29 30# This build file defines a complete set of tools for a LLVM-based toolchain. 31 32exports_files(glob(["**"])) 33 34filegroup( 35 name = "all", 36 srcs = glob(["**"]), 37 visibility = ["//visibility:public"], 38) 39 40# Export other tools in `bin` so they can be directly referenced if necessary. 41exports_files(glob(["bin/**"])) 42 43##################### 44# Tools # 45##################### 46 47alias( 48 name = "all_tools", 49 actual = select({ 50 "@platforms//os:macos": ":macos_tools", 51 "//conditions:default": ":default_tools", 52 }), 53) 54 55COMMON_TOOLS = { 56 "@rules_cc//cc/toolchains/actions:assembly_actions": ":asm", 57 "@rules_cc//cc/toolchains/actions:c_compile_actions": ":clang", 58 "@rules_cc//cc/toolchains/actions:cpp_compile_actions": ":clang++", 59 "@rules_cc//cc/toolchains/actions:link_actions": ":lld", 60 "@rules_cc//cc/toolchains/actions:objcopy_embed_data": ":llvm-objcopy", 61 "@pigweed//pw_toolchain/action:objdump": ":llvm-objdump", 62 "@rules_cc//cc/toolchains/actions:strip": ":llvm-strip", 63} 64 65cc_tool_map( 66 name = "default_tools", 67 tools = COMMON_TOOLS | { 68 "@rules_cc//cc/toolchains/actions:ar_actions": ":llvm-ar" 69 }, 70 visibility = ["//visibility:private"], 71) 72 73cc_tool_map( 74 name = "macos_tools", 75 tools = COMMON_TOOLS | { 76 "@rules_cc//cc/toolchains/actions:ar_actions": ":llvm-libtool-darwin" 77 }, 78 visibility = ["//visibility:private"], 79) 80 81# TODO: https://github.com/bazelbuild/rules_cc/issues/235 - Workaround until 82# Bazel has a more robust way to implement `cc_tool_map`. 83alias( 84 name = "asm", 85 actual = ":clang", 86) 87 88cc_tool( 89 name = "clang", 90 src = select({ 91 "@platforms//os:windows": "//:bin/clang.exe", 92 "//conditions:default": "//:bin/clang", 93 }), 94 data = glob([ 95 "bin/llvm", 96 "include/**", 97 "lib/clang/**/include/**", 98 ]), 99 allowlist_include_directories = [ 100 ":lib-clang-include", 101 ], 102) 103 104cc_tool( 105 name = "clang++", 106 src = select({ 107 "@platforms//os:windows": "//:bin/clang++.exe", 108 "//conditions:default": "//:bin/clang++", 109 }), 110 data = glob([ 111 "bin/llvm", 112 "include/**", 113 "lib/clang/**/include/**", 114 ]), 115 allowlist_include_directories = [ 116 # This linux-specific entry can be moved to a cc_args rule if there's 117 # ever a desire for more strict management of this include path. 118 ":include-x86_64-unknown-linux-gnu-c++-v1", 119 ":include-c++-v1", 120 ":lib-clang-include", 121 ], 122) 123 124cc_tool( 125 name = "lld", 126 src = select({ 127 "@platforms//os:windows": "//:bin/clang++.exe", 128 "//conditions:default": "//:bin/clang++", 129 }), 130 data = glob([ 131 "bin/llvm", 132 "bin/lld*", 133 "bin/ld*", 134 "lib/**/*.a", 135 "lib/**/*.so*", 136 "lib/**/*.o", 137 ]), 138) 139 140cc_tool( 141 name = "llvm-ar", 142 src = select({ 143 "@platforms//os:windows": "//:bin/llvm-ar.exe", 144 "//conditions:default": "//:bin/llvm-ar", 145 }), 146 data = glob(["bin/llvm"]), 147) 148 149cc_tool( 150 name = "llvm-libtool-darwin", 151 src = select({ 152 "@platforms//os:windows": "//:bin/llvm-libtool-darwin.exe", 153 "//conditions:default": "//:bin/llvm-libtool-darwin", 154 }), 155 data = glob(["bin/llvm"]), 156) 157 158cc_tool( 159 name = "llvm-objcopy", 160 src = select({ 161 "@platforms//os:windows": "//:bin/llvm-objcopy.exe", 162 "//conditions:default": "//:bin/llvm-objcopy", 163 }), 164 data = glob(["bin/llvm"]), 165) 166 167cc_tool( 168 name = "llvm-objdump", 169 src = select({ 170 "@platforms//os:windows": "//:bin/llvm-objdump.exe", 171 "//conditions:default": "//:bin/llvm-objdump", 172 }), 173 data = glob(["bin/llvm"]), 174) 175 176cc_tool( 177 name = "llvm-cov", 178 src = select({ 179 "@platforms//os:windows": "//:bin/llvm-cov.exe", 180 "//conditions:default": "//:bin/llvm-cov", 181 }), 182 data = glob(["bin/llvm"]), 183) 184 185cc_tool( 186 name = "llvm-strip", 187 src = select({ 188 "@platforms//os:windows": "//:bin/llvm-strip.exe", 189 "//conditions:default": "//:bin/llvm-strip", 190 }), 191 data = glob(["bin/llvm"]), 192) 193 194# TODO(amontanez): Add sysroot for macos to the `data` field selection once 195# Pigweed migrates to use rules_cc toolchains. 196native_binary( 197 name = "clang-tidy", 198 src = select({ 199 "@platforms//os:windows": "//:bin/clang-tidy.exe", 200 "//conditions:default": "//:bin/clang-tidy", 201 }), 202 data = glob([ 203 "include/**", 204 "lib/clang/**/include/**", 205 ]) + select({ 206 "@platforms//os:linux": ["@linux_sysroot//:sysroot"], 207 "//conditions:default": [], 208 }), 209 out = select({ 210 "@platforms//os:windows": "clang-tidy.exe", 211 "//conditions:default": "clang-tidy", 212 }), 213 visibility = ["//visibility:public"], 214) 215 216##################### 217# Arguments # 218##################### 219 220# None... 221 222##################### 223# Features # 224##################### 225 226# None... 227 228##################### 229# Builtins # 230##################### 231 232subdirectory( 233 name = "include-c++-v1", 234 parent = ":toolchain_root", 235 path = "include/c++/v1", 236) 237 238subdirectory( 239 name = "lib-clang-include", 240 parent = ":toolchain_root", 241 path = match_dir(["lib/clang/*/include"], allow_empty=False), 242) 243 244subdirectory( 245 name = "include-x86_64-unknown-linux-gnu-c++-v1", 246 parent = ":toolchain_root", 247 path = "include/x86_64-unknown-linux-gnu/c++/v1", 248) 249 250directory( 251 name = "toolchain_root", 252 srcs = glob([ 253 "lib/**", 254 "include/**", 255 ]), 256) 257 258filegroup( 259 name = "llvm-libc_files", 260 srcs = selects.with_or({ 261 ALL_CORTEX_M_CPUS: [ 262 ":llvm-libc_arm-none-eabi_compile_files", 263 ":llvm-libc_arm-none-eabi_link_files", 264 ], 265 "//conditions:default": [], 266 }), 267) 268 269config_setting( 270 name = "x86_64-unknown-linux-gnu", 271 constraint_values = [ 272 "@platforms//os:linux", 273 "@platforms//cpu:x86_64", 274 ], 275 visibility = ["//visibility:private"], 276) 277 278##################### 279# llvm-libc # 280##################### 281 282filegroup( 283 name = "llvm-libc_arm-none-eabi_compile_files", 284 srcs = glob([ 285 "include/armv*-unknown-none-eabi/**", 286 ]), 287 visibility = ["//visibility:public"], 288) 289 290filegroup( 291 name = "llvm-libc_arm-none-eabi_link_files", 292 srcs = glob([ 293 "lib/armv*-unknown-none-eabi/**", 294 "lib/clang/*/lib/armv*-unknown-none-eabi/**", 295 ]), 296 visibility = ["//visibility:public"], 297) 298 299cc_args( 300 name = "llvm-libc_link_args", 301 actions = ["@rules_cc//cc/toolchains/actions:link_actions"], 302 args = selects.with_or({ 303 ALL_CORTEX_M_CPUS: [ 304 "-nostdlib++", 305 "-nostartfiles", 306 "-Wl,-lc++", 307 ], 308 "//conditions:default": [], 309 }), 310 data = [":llvm-libc_arm-none-eabi_link_files"], 311 visibility = ["//visibility:private"], 312) 313 314cc_args( 315 name = "llvm-libc_compile_args", 316 actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], 317 args = selects.with_or({ 318 ALL_CORTEX_M_CPUS: [], 319 "//conditions:default": [], 320 }), 321 data = [":llvm-libc_arm-none-eabi_compile_files"], 322 visibility = ["//visibility:private"], 323) 324 325cc_args_list( 326 name = "llvm-libc_args", 327 args = [ 328 ":llvm-libc_compile_args", 329 ":llvm-libc_link_args", 330 ] 331) 332