| 1 |
""" |
| 2 |
This file specifies a clang toolchain that can run on a Windows host. |
| 3 |
|
| 4 |
See download_windows_toolchain.bzl for more details on the creation of the toolchain. |
| 5 |
|
| 6 |
It uses the usr subfolder of the built toolchain as a sysroot |
| 7 |
|
| 8 |
It follows the example of: |
| 9 |
- linux_amd64_toolchain_config.bzl |
| 10 |
""" |
| 11 |
|
| 12 |
# https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/cc/action_names.bzl |
| 13 |
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") |
| 14 |
|
| 15 |
# https://github.com/bazelbuild/bazel/blob/master/tools/cpp/cc_toolchain_config_lib.bzl |
| 16 |
load( |
| 17 |
"@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", |
| 18 |
"action_config", |
| 19 |
"feature", |
| 20 |
"flag_group", |
| 21 |
"flag_set", |
| 22 |
"tool", |
| 23 |
"variable_with_value", |
| 24 |
) |
| 25 |
load("@rules_cc//cc/common:cc_common.bzl", "cc_common") |
| 26 |
|
| 27 |
# TODO(borenet): These variables were copied from the automatically-generated |
| 28 |
# @clang_windows_amd64//:vars.bzl file. They are available to be directly |
| 29 |
# used here, but in order to do so, Bazel needs access to both the Clang and |
| 30 |
# MSVC archives, even when we aren't going to use this toolchain. Due to the |
| 31 |
# time required to download and extract these archives, we've opted to hard-code |
| 32 |
# the versions and paths here. |
| 33 |
#load("@clang_windows_amd64//:vars.bzl", "MSVC_INCLUDE", "MSVC_LIB", "WIN_SDK_INCLUDE", "WIN_SDK_LIB") |
| 34 |
MSVC_VERSION = "14.39.33519" |
| 35 |
MSVC_INCLUDE = "VC/Tools/MSVC/" + MSVC_VERSION + "/include" |
| 36 |
MSVC_LIB = "VC/Tools/MSVC/" + MSVC_VERSION + "/lib" |
| 37 |
WIN_SDK_VERSION = "10.0.22621.0" |
| 38 |
WIN_SDK_INCLUDE = "win_sdk/Include/" + WIN_SDK_VERSION |
| 39 |
WIN_SDK_LIB = "win_sdk/Lib/" + WIN_SDK_VERSION |
| 40 |
|
| 41 |
# The location of the downloaded clang toolchain. |
| 42 |
CLANG_TOOLCHAIN = "external/clang_windows_amd64" |
| 43 |
|
| 44 |
# Paths inside the win_toolchain CIPD package. |
| 45 |
FULL_MSVC_INCLUDE = CLANG_TOOLCHAIN + "/" + MSVC_INCLUDE |
| 46 |
FULL_MSVC_LIB = CLANG_TOOLCHAIN + "/" + MSVC_LIB |
| 47 |
FULL_WIN_SDK_INCLUDE = CLANG_TOOLCHAIN + "/" + WIN_SDK_INCLUDE |
| 48 |
FULL_WIN_SDK_LIB = CLANG_TOOLCHAIN + "/" + WIN_SDK_LIB |
| 49 |
|
| 50 |
def _windows_amd64_toolchain_info(ctx): |
| 51 |
action_configs = _make_action_configs() |
| 52 |
features = [ |
| 53 |
feature( |
| 54 |
name = "archive_param_file", |
| 55 |
enabled = True, |
| 56 |
), |
| 57 |
] |
| 58 |
features += _make_default_flags() |
| 59 |
features += _make_diagnostic_flags() |
| 60 |
|
| 61 |
# https://bazel.build/rules/lib/cc_common#create_cc_toolchain_config_info |
| 62 |
# Note, this rule is defined in Java code, not Starlark |
| 63 |
# https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcModuleApi.java |
| 64 |
return cc_common.create_cc_toolchain_config_info( |
| 65 |
ctx = ctx, |
| 66 |
features = features, |
| 67 |
action_configs = action_configs, |
| 68 |
# These are required, but do nothing |
| 69 |
compiler = "", |
| 70 |
target_cpu = "", |
| 71 |
target_libc = "", |
| 72 |
target_system_name = "", |
| 73 |
toolchain_identifier = "", |
| 74 |
) |
| 75 |
|
| 76 |
provide_windows_amd64_toolchain_config = rule( |
| 77 |
attrs = {}, |
| 78 |
provides = [CcToolchainConfigInfo], |
| 79 |
implementation = _windows_amd64_toolchain_info, |
| 80 |
) |
| 81 |
|
| 82 |
def _make_action_configs(): |
| 83 |
""" |
| 84 |
This function sets up the tools needed to perform the various compile/link actions. |
| 85 |
|
| 86 |
Bazel normally restricts us to referring to (and therefore running) executables/scripts |
| 87 |
that are in this directory (That is EXEC_ROOT/toolchain). However, the executables we want |
| 88 |
to run are brought in via WORKSPACE.bazel and are located in EXEC_ROOT/external/clang.... |
| 89 |
Therefore, we make use of "trampoline scripts" that will call the binaries from the |
| 90 |
toolchain directory. |
| 91 |
|
| 92 |
These action_configs also let us dynamically specify arguments from the Bazel |
| 93 |
environment if necessary (see cpp_link_static_library_action). |
| 94 |
""" |
| 95 |
|
| 96 |
# https://cs.opensource.google/bazel/bazel/+/master:tools/cpp/cc_toolchain_config_lib.bzl;l=435;drc=3b9e6f201a9a3465720aad8712ab7bcdeaf2e5da |
| 97 |
clang_tool = tool(path = "windows_trampolines/clang_trampoline_windows.bat") |
| 98 |
ar_tool = tool(path = "windows_trampolines/ar_trampoline_windows.bat") |
| 99 |
|
| 100 |
# https://cs.opensource.google/bazel/bazel/+/master:tools/cpp/cc_toolchain_config_lib.bzl;l=488;drc=3b9e6f201a9a3465720aad8712ab7bcdeaf2e5da |
| 101 |
assemble_action = action_config( |
| 102 |
action_name = ACTION_NAMES.assemble, |
| 103 |
tools = [clang_tool], |
| 104 |
) |
| 105 |
c_compile_action = action_config( |
| 106 |
action_name = ACTION_NAMES.c_compile, |
| 107 |
tools = [clang_tool], |
| 108 |
) |
| 109 |
cpp_compile_action = action_config( |
| 110 |
action_name = ACTION_NAMES.cpp_compile, |
| 111 |
tools = [clang_tool], |
| 112 |
) |
| 113 |
linkstamp_compile_action = action_config( |
| 114 |
action_name = ACTION_NAMES.linkstamp_compile, |
| 115 |
tools = [clang_tool], |
| 116 |
) |
| 117 |
preprocess_assemble_action = action_config( |
| 118 |
action_name = ACTION_NAMES.preprocess_assemble, |
| 119 |
tools = [clang_tool], |
| 120 |
) |
| 121 |
|
| 122 |
cpp_link_dynamic_library_action = action_config( |
| 123 |
action_name = ACTION_NAMES.cpp_link_dynamic_library, |
| 124 |
tools = [clang_tool], |
| 125 |
) |
| 126 |
cpp_link_executable_action = action_config( |
| 127 |
action_name = ACTION_NAMES.cpp_link_executable, |
| 128 |
# Bazel assumes it is talking to clang when building an executable. There are |
| 129 |
# "-Wl" flags on the command: https://releases.llvm.org/6.0.1/tools/clang/docs/ClangCommandLineReference.html#cmdoption-clang-Wl |
| 130 |
tools = [clang_tool], |
| 131 |
) |
| 132 |
cpp_link_nodeps_dynamic_library_action = action_config( |
| 133 |
action_name = ACTION_NAMES.cpp_link_nodeps_dynamic_library, |
| 134 |
tools = [clang_tool], |
| 135 |
) |
| 136 |
|
| 137 |
# By default, there are no flags or libraries passed to the llvm-ar tool, so |
| 138 |
# we need to specify them. The variables mentioned by expand_if_available are defined |
| 139 |
# https://bazel.build/docs/cc-toolchain-config-reference#cctoolchainconfiginfo-build-variables |
| 140 |
cpp_link_static_library_action = action_config( |
| 141 |
action_name = ACTION_NAMES.cpp_link_static_library, |
| 142 |
flag_sets = [ |
| 143 |
flag_set( |
| 144 |
flag_groups = [ |
| 145 |
flag_group( |
| 146 |
# https://llvm.org/docs/CommandGuide/llvm-ar.html |
| 147 |
# replace existing files or insert them if they already exist, |
| 148 |
# create the file if it doesn't already exist |
| 149 |
# symbol table should be added |
| 150 |
# Deterministic timestamps should be used |
| 151 |
flags = ["rcsD", "%{output_execpath}"], |
| 152 |
# Despite the name, output_execpath just refers to linker output, |
| 153 |
# e.g. libFoo.a |
| 154 |
expand_if_available = "output_execpath", |
| 155 |
), |
| 156 |
], |
| 157 |
), |
| 158 |
flag_set( |
| 159 |
flag_groups = [ |
| 160 |
flag_group( |
| 161 |
iterate_over = "libraries_to_link", |
| 162 |
flag_groups = [ |
| 163 |
flag_group( |
| 164 |
flags = ["%{libraries_to_link.name}"], |
| 165 |
expand_if_equal = variable_with_value( |
| 166 |
name = "libraries_to_link.type", |
| 167 |
value = "object_file", |
| 168 |
), |
| 169 |
), |
| 170 |
flag_group( |
| 171 |
flags = ["%{libraries_to_link.object_files}"], |
| 172 |
iterate_over = "libraries_to_link.object_files", |
| 173 |
expand_if_equal = variable_with_value( |
| 174 |
name = "libraries_to_link.type", |
| 175 |
value = "object_file_group", |
| 176 |
), |
| 177 |
), |
| 178 |
], |
| 179 |
expand_if_available = "libraries_to_link", |
| 180 |
), |
| 181 |
], |
| 182 |
), |
| 183 |
flag_set( |
| 184 |
flag_groups = [ |
| 185 |
flag_group( |
| 186 |
flags = ["@%{archive_param_file}"], |
| 187 |
expand_if_available = "archive_param_file", |
| 188 |
), |
| 189 |
], |
| 190 |
), |
| 191 |
], |
| 192 |
tools = [ar_tool], |
| 193 |
) |
| 194 |
|
| 195 |
action_configs = [ |
| 196 |
assemble_action, |
| 197 |
c_compile_action, |
| 198 |
cpp_compile_action, |
| 199 |
cpp_link_dynamic_library_action, |
| 200 |
cpp_link_executable_action, |
| 201 |
cpp_link_nodeps_dynamic_library_action, |
| 202 |
cpp_link_static_library_action, |
| 203 |
linkstamp_compile_action, |
| 204 |
preprocess_assemble_action, |
| 205 |
] |
| 206 |
return action_configs |
| 207 |
|
| 208 |
def _make_default_flags(): |
| 209 |
"""Here we define the flags for certain actions that are always applied. |
| 210 |
|
| 211 |
For any flag that might be conditionally applied, it should be defined in //bazel/copts.bzl. |
| 212 |
|
| 213 |
Flags that are set here will be unconditionally applied to everything we compile with |
| 214 |
this toolchain, even third_party deps. |
| 215 |
""" |
| 216 |
|
| 217 |
# Note: These values must be kept in sync with those defined in cmake_exporter.go. |
| 218 |
cxx_compile_includes = flag_set( |
| 219 |
actions = [ |
| 220 |
ACTION_NAMES.c_compile, |
| 221 |
ACTION_NAMES.cpp_compile, |
| 222 |
], |
| 223 |
flag_groups = [ |
| 224 |
flag_group( |
| 225 |
flags = [ |
| 226 |
# THIS ORDER MATTERS GREATLY. If these are in the wrong order, the |
| 227 |
# #include_next directives will fail to find the files, causing a compilation |
| 228 |
# error (or, without -no-canonical-prefixes, a mysterious case where files |
| 229 |
# are included with an absolute path and fail the build). |
| 230 |
"-isystem", |
| 231 |
CLANG_TOOLCHAIN + "/include/clang", |
| 232 |
"-isystem", |
| 233 |
CLANG_TOOLCHAIN + "/include/clang-c", |
| 234 |
"-isystem", |
| 235 |
CLANG_TOOLCHAIN + "/include/clang-tidy", |
| 236 |
"-isystem", |
| 237 |
CLANG_TOOLCHAIN + "/include/lld", |
| 238 |
"-isystem", |
| 239 |
CLANG_TOOLCHAIN + "/include/lldb", |
| 240 |
"-isystem", |
| 241 |
CLANG_TOOLCHAIN + "/include/llvm", |
| 242 |
"-isystem", |
| 243 |
CLANG_TOOLCHAIN + "/include/llvm-c", |
| 244 |
"-isystem", |
| 245 |
CLANG_TOOLCHAIN + "/lib/clang/18/include", |
| 246 |
"-isystem", |
| 247 |
FULL_WIN_SDK_INCLUDE + "/shared", |
| 248 |
"-isystem", |
| 249 |
FULL_WIN_SDK_INCLUDE + "/ucrt", |
| 250 |
"-isystem", |
| 251 |
FULL_WIN_SDK_INCLUDE + "/um", |
| 252 |
"-isystem", |
| 253 |
FULL_WIN_SDK_INCLUDE + "/winrt", |
| 254 |
"-isystem", |
| 255 |
FULL_WIN_SDK_INCLUDE + "/cppwinrt", |
| 256 |
"-isystem", |
| 257 |
FULL_MSVC_INCLUDE, |
| 258 |
# We do not want clang to search in absolute paths for files. This makes |
| 259 |
# Bazel think we are using an outside resource and fail the compile. |
| 260 |
"-no-canonical-prefixes", |
| 261 |
], |
| 262 |
), |
| 263 |
], |
| 264 |
) |
| 265 |
|
| 266 |
cpp_compile_flags = flag_set( |
| 267 |
actions = [ |
| 268 |
ACTION_NAMES.cpp_compile, |
| 269 |
], |
| 270 |
flag_groups = [ |
| 271 |
flag_group( |
| 272 |
flags = [ |
| 273 |
"-std=c++20", |
| 274 |
], |
| 275 |
), |
| 276 |
], |
| 277 |
) |
| 278 |
|
| 279 |
link_exe_flags = flag_set( |
| 280 |
actions = [ |
| 281 |
ACTION_NAMES.cpp_link_executable, |
| 282 |
ACTION_NAMES.cpp_link_dynamic_library, |
| 283 |
ACTION_NAMES.cpp_link_nodeps_dynamic_library, |
| 284 |
], |
| 285 |
flag_groups = [ |
| 286 |
flag_group( |
| 287 |
flags = [ |
| 288 |
"-fuse-ld=lld", |
| 289 |
# We chose to use the llvm runtime, not the gcc one because it is already |
| 290 |
# included in the clang binary |
| 291 |
"--rtlib=compiler-rt", |
| 292 |
"-std=c++20", |
| 293 |
"-L" + FULL_MSVC_LIB + "/x64", |
| 294 |
"-L" + FULL_WIN_SDK_LIB + "/ucrt/x64", |
| 295 |
"-L" + FULL_WIN_SDK_LIB + "/um/x64", |
| 296 |
], |
| 297 |
), |
| 298 |
], |
| 299 |
) |
| 300 |
return [feature( |
| 301 |
"default_flags", |
| 302 |
enabled = True, |
| 303 |
flag_sets = [ |
| 304 |
cxx_compile_includes, |
| 305 |
cpp_compile_flags, |
| 306 |
link_exe_flags, |
| 307 |
], |
| 308 |
)] |
| 309 |
|
| 310 |
def _make_diagnostic_flags(): |
| 311 |
"""Here we define the flags that can be turned on via features to yield debug info.""" |
| 312 |
cxx_diagnostic = flag_set( |
| 313 |
actions = [ |
| 314 |
ACTION_NAMES.c_compile, |
| 315 |
ACTION_NAMES.cpp_compile, |
| 316 |
], |
| 317 |
flag_groups = [ |
| 318 |
flag_group( |
| 319 |
flags = [ |
| 320 |
"--trace-includes", |
| 321 |
"-v", |
| 322 |
], |
| 323 |
), |
| 324 |
], |
| 325 |
) |
| 326 |
|
| 327 |
link_diagnostic = flag_set( |
| 328 |
actions = [ACTION_NAMES.cpp_link_executable], |
| 329 |
flag_groups = [ |
| 330 |
flag_group( |
| 331 |
flags = [ |
| 332 |
"-Wl,-verbose", |
| 333 |
"-v", |
| 334 |
], |
| 335 |
), |
| 336 |
], |
| 337 |
) |
| 338 |
|
| 339 |
link_search_dirs = flag_set( |
| 340 |
actions = [ACTION_NAMES.cpp_link_executable], |
| 341 |
flag_groups = [ |
| 342 |
flag_group( |
| 343 |
flags = [ |
| 344 |
"--print-search-dirs", |
| 345 |
], |
| 346 |
), |
| 347 |
], |
| 348 |
) |
| 349 |
return [ |
| 350 |
# Running a Bazel command with --features diagnostic will cause the compilation and |
| 351 |
# link steps to be more verbose. |
| 352 |
feature( |
| 353 |
"diagnostic", |
| 354 |
enabled = False, |
| 355 |
flag_sets = [ |
| 356 |
cxx_diagnostic, |
| 357 |
link_diagnostic, |
| 358 |
], |
| 359 |
), |
| 360 |
feature( |
| 361 |
"diagnostic_link", |
| 362 |
enabled = False, |
| 363 |
flag_sets = [ |
| 364 |
link_diagnostic, |
| 365 |
], |
| 366 |
), |
| 367 |
# Running a Bazel command with --features print_search_dirs will cause the link to fail |
| 368 |
# but directories searched for libraries, etc will be displayed. |
| 369 |
feature( |
| 370 |
"print_search_dirs", |
| 371 |
enabled = False, |
| 372 |
flag_sets = [ |
| 373 |
link_search_dirs, |
| 374 |
], |
| 375 |
), |
| 376 |
] |