|
16 | 16 | "@bazel_tools//tools/cpp:unix_cc_toolchain_config.bzl",
|
17 | 17 | unix_cc_toolchain_config = "cc_toolchain_config",
|
18 | 18 | )
|
| 19 | +load( |
| 20 | + "@bazel_tools//tools/cpp:windows_cc_toolchain_config.bzl", |
| 21 | + windows_cc_toolchain_config = "cc_toolchain_config", |
| 22 | +) |
19 | 23 | load(
|
20 | 24 | "//toolchain/internal:common.bzl",
|
21 | 25 | _check_os_arch_keys = "check_os_arch_keys",
|
@@ -43,6 +47,14 @@ def cc_toolchain_config(
|
43 | 47 | compiler_configuration,
|
44 | 48 | llvm_version,
|
45 | 49 | host_tools_info = {}):
|
| 50 | + |
| 51 | + # Windows even if cross compiling is different to the point of just using a |
| 52 | + # different configuration step |
| 53 | + if target_os == "windows": |
| 54 | + return windows_cc_toolchain(name, host_arch, host_os, target_arch, target_os, |
| 55 | + toolchain_path_prefix, tools_path_prefix, wrapper_bin_prefix, |
| 56 | + compiler_configuration, llvm_version, host_tools_info = {}) |
| 57 | + |
46 | 58 | host_os_arch_key = _os_arch_pair(host_os, host_arch)
|
47 | 59 | target_os_arch_key = _os_arch_pair(target_os, target_arch)
|
48 | 60 | _check_os_arch_keys([host_os_arch_key, target_os_arch_key])
|
@@ -169,6 +181,7 @@ def cc_toolchain_config(
|
169 | 181 | stdlib = compiler_configuration["stdlib"]
|
170 | 182 | if stdlib == "builtin-libc++" and is_xcompile:
|
171 | 183 | stdlib = "stdc++"
|
| 184 | + |
172 | 185 | if stdlib == "builtin-libc++":
|
173 | 186 | cxx_flags = [
|
174 | 187 | "-std=" + cxx_standard,
|
@@ -245,8 +258,10 @@ def cc_toolchain_config(
|
245 | 258 |
|
246 | 259 | sysroot_path = compiler_configuration["sysroot_path"]
|
247 | 260 | sysroot_prefix = ""
|
| 261 | + |
248 | 262 | if sysroot_path:
|
249 | 263 | sysroot_prefix = "%sysroot%"
|
| 264 | + |
250 | 265 | if target_os == "linux":
|
251 | 266 | cxx_builtin_include_directories.extend([
|
252 | 267 | sysroot_prefix + "/include",
|
@@ -285,8 +300,6 @@ def cc_toolchain_config(
|
285 | 300 | else:
|
286 | 301 | ar_binary = host_tools_info["libtool"]["path"]
|
287 | 302 |
|
288 |
| - # The tool names come from [here](https://github.com/bazelbuild/bazel/blob/c7e58e6ce0a78fdaff2d716b4864a5ace8917626/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java#L76-L90): |
289 |
| - # NOTE: Ensure these are listed in toolchain_tools in toolchain/internal/common.bzl. |
290 | 303 | tool_paths = {
|
291 | 304 | "ar": ar_binary,
|
292 | 305 | "cpp": tools_path_prefix + "clang-cpp",
|
@@ -358,3 +371,131 @@ def cc_toolchain_config(
|
358 | 371 | supports_start_end_lib = supports_start_end_lib,
|
359 | 372 | builtin_sysroot = sysroot_path,
|
360 | 373 | )
|
| 374 | + |
| 375 | +def windows_cc_toolchain( |
| 376 | + name, |
| 377 | + host_arch, |
| 378 | + host_os, |
| 379 | + target_arch, |
| 380 | + target_os, |
| 381 | + toolchain_path_prefix, |
| 382 | + tools_path_prefix, |
| 383 | + wrapper_bin_prefix, |
| 384 | + compiler_configuration, |
| 385 | + llvm_version, |
| 386 | + host_tools_info = {}): |
| 387 | + |
| 388 | + sysroot_path = compiler_configuration["sysroot_path"] |
| 389 | + |
| 390 | + # For the bring your own sysroot crowd, we assume xwin |
| 391 | + # These are the defaults ... |
| 392 | + msvc_env_tmp = "msvc_not_found" |
| 393 | + msvc_env_path = "msvc_not_found" |
| 394 | + msvc_env_include = "msvc_not_found" |
| 395 | + msvc_env_lib = "msvc_not_found" |
| 396 | + msvc_cl_path = tools_path_prefix + "clang-cl" |
| 397 | + msvc_ml_path = tools_path_prefix + "clang-cl" # llvm-ml" |
| 398 | + msvc_link_path = tools_path_prefix + "lld-link" |
| 399 | + msvc_lib_path = tools_path_prefix + "llvm-lib" |
| 400 | + |
| 401 | + cxx_builtin_include_directories = [] |
| 402 | + |
| 403 | + raw_llvm_repo_path = compiler_configuration['raw_llvm_repo_path'] |
| 404 | + if raw_llvm_repo_path: |
| 405 | + cxx_builtin_include_directories.extend([ |
| 406 | + raw_llvm_repo_path + "include/c++/v1", |
| 407 | + raw_llvm_repo_path + "include/windows/c++/v1", |
| 408 | + raw_llvm_repo_path + "lib/clang/{}/include".format(llvm_version), |
| 409 | + raw_llvm_repo_path + "lib64/clang/{}/include".format(llvm_version), |
| 410 | + ]) |
| 411 | + |
| 412 | + if host_os == "windows": |
| 413 | + script_lang = ".bat" |
| 414 | + # TODO: Make work on windows ;) |
| 415 | + # sysroot = figure_out_sysroot_vs_msvc_install() |
| 416 | + fail("Not currently working on windows") |
| 417 | + else: |
| 418 | + script_lang = ".sh" |
| 419 | + if not sysroot_path: |
| 420 | + fail("In cross compilation of windows artifacts an MSVCRT sysroot needs to be prepared") |
| 421 | + |
| 422 | + includes = [ |
| 423 | + sysroot_path + "/crt/include", |
| 424 | + sysroot_path + "/sdk/include/ucrt", |
| 425 | + sysroot_path + "/sdk/include/um", |
| 426 | + sysroot_path + "/sdk/include/shared", |
| 427 | + ] |
| 428 | + |
| 429 | + lib_paths = [ |
| 430 | + # TODO: Arch should be inferred |
| 431 | + sysroot_path + "/crt/lib/x86_64", |
| 432 | + sysroot_path + "/sdk/lib/um/x86_64", |
| 433 | + sysroot_path + "/sdk/lib/ucrt/x86_64", |
| 434 | + # TODO: How to get this in a sysroot |
| 435 | + sysroot_path + "/clang-rt-14.0.0", |
| 436 | + ] |
| 437 | + |
| 438 | + # HACK we combine paths with `;` which is not a unix idiom, since windows_cc_toolchain_config |
| 439 | + # HACK breaks this apart with split :/ |
| 440 | + msvc_env_include = ';'.join(includes) |
| 441 | + cxx_builtin_include_directories.extend(includes) |
| 442 | + |
| 443 | + msvc_env_lib = ';'.join(lib_paths) |
| 444 | + #msvc_link_path = ';'.join(lib_paths) |
| 445 | + |
| 446 | + # These are the defaults, how to configure? |
| 447 | + archiver_flags = [] |
| 448 | + default_link_flags = [] |
| 449 | + |
| 450 | + tool_paths = { |
| 451 | + "ar": tools_path_prefix + "clang-cl", |
| 452 | + "ml": tools_path_prefix + "clang-cl", # llvm-ml", |
| 453 | + "cpp": tools_path_prefix + "clang-cl", |
| 454 | + "gcc": tools_path_prefix + "clang-cl", |
| 455 | + "ld": tools_path_prefix + "lld-link", |
| 456 | + "gcov": wrapper_bin_prefix + "msvc_nop" + script_lang, |
| 457 | + "nm": wrapper_bin_prefix + "msvc_nop" + script_lang, |
| 458 | + "objcopy": wrapper_bin_prefix + "msvc_nop" + script_lang, |
| 459 | + "objdump": wrapper_bin_prefix + "msvc_nop" + script_lang, |
| 460 | + "strip": wrapper_bin_prefix + "msvc_nop" + script_lang, |
| 461 | + } |
| 462 | + |
| 463 | + tool_bin_path = "not_found" |
| 464 | + |
| 465 | + cxx_builtin_include_directories.extend(compiler_configuration["additional_include_dirs"]) |
| 466 | + |
| 467 | + dbg_mode_debug_flag = "/DEBUG" |
| 468 | + fastbuild_mode_debug_flag = "/DEBUG" |
| 469 | + |
| 470 | + windows_cc_toolchain_config( |
| 471 | + name = name, |
| 472 | + cpu = "x64_windows", |
| 473 | + # The compiler is hardcoded, we only support clang (suprise!) and its |
| 474 | + # required to satisify https://cs.opensource.google/bazel/bazel/+/master:tools/cpp/windows_cc_toolchain_config.bzl;l=82 |
| 475 | + compiler = "clang-cl", |
| 476 | + # TODO: To support Arm or x86_32 this needs to be configured |
| 477 | + toolchain_identifier = "clang_cl_x64", |
| 478 | + host_system_name = "linux", |
| 479 | + target_system_name = "windows", |
| 480 | + # TODO: On windows this should probably figure out where the VC install is |
| 481 | + # This toolchain only supports MSCRT as the ABI, I would suggest zig-cc-bazel for GNU ABI |
| 482 | + # These values are related to the provided sysroot |
| 483 | + target_libc = "msvcrt", |
| 484 | + abi_version = "local", |
| 485 | + abi_libc_version = "local", |
| 486 | + cxx_builtin_include_directories = cxx_builtin_include_directories, |
| 487 | + tool_paths = tool_paths, |
| 488 | + archiver_flags = ["/MACHINE:X64"], |
| 489 | + default_link_flags = ["/MACHINE:X64", "/DEFAULTLIB:clang_rt.builtins-x86_64.lib"], |
| 490 | + msvc_env_tmp = msvc_env_tmp, |
| 491 | + msvc_env_path = msvc_env_path, |
| 492 | + msvc_env_include = msvc_env_include, |
| 493 | + msvc_env_lib = msvc_env_lib, |
| 494 | + msvc_cl_path = msvc_cl_path, |
| 495 | + msvc_ml_path = msvc_ml_path, |
| 496 | + msvc_link_path = msvc_link_path, |
| 497 | + msvc_lib_path = msvc_lib_path, |
| 498 | + dbg_mode_debug_flag = dbg_mode_debug_flag, |
| 499 | + fastbuild_mode_debug_flag = fastbuild_mode_debug_flag, |
| 500 | + tool_bin_path = tool_bin_path, |
| 501 | + ) |
0 commit comments