clang_cc_toolchain_config.bzl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. # Exceptions. See /LICENSE for license information.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. """A Starlark cc_toolchain configuration rule"""
  5. load("@rules_cc//cc:defs.bzl", "cc_toolchain")
  6. load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
  7. load(":cc_toolchain_carbon_project_features.bzl", "carbon_project_features")
  8. load(":cc_toolchain_cpp_features.bzl", "libcxx_feature")
  9. load(":cc_toolchain_features.bzl", "clang_cc_toolchain_features")
  10. load(
  11. ":cc_toolchain_tools.bzl",
  12. "llvm_action_configs",
  13. "llvm_tool_paths",
  14. )
  15. load(
  16. ":clang_detected_variables.bzl",
  17. "clang_bindir",
  18. "clang_include_dirs",
  19. "clang_resource_dir",
  20. "clang_version_for_cache",
  21. "llvm_bindir",
  22. "sysroot_dir",
  23. )
  24. def _impl(ctx):
  25. # Only use a sysroot if one was found when detecting Clang.
  26. sysroot = None
  27. if sysroot_dir != "None":
  28. sysroot = sysroot_dir
  29. identifier = "local-{0}-{1}".format(ctx.attr.target_cpu, ctx.attr.target_os)
  30. return cc_common.create_cc_toolchain_config_info(
  31. ctx = ctx,
  32. features = clang_cc_toolchain_features(
  33. target_os = ctx.attr.target_os,
  34. target_cpu = ctx.attr.target_cpu,
  35. project_features = carbon_project_features(clang_version_for_cache),
  36. extra_cpp_features = [libcxx_feature(llvm_bindir, clang_bindir)],
  37. ),
  38. action_configs = llvm_action_configs(llvm_bindir, clang_bindir),
  39. cxx_builtin_include_directories = clang_include_dirs + [
  40. # Add Clang's resource directory to the end of the builtin include
  41. # directories to cover the use of sanitizer resource files by the
  42. # driver.
  43. clang_resource_dir + "/share",
  44. ],
  45. builtin_sysroot = sysroot,
  46. # This configuration only supports local non-cross builds so derive
  47. # everything from the target CPU selected.
  48. toolchain_identifier = identifier,
  49. # This is used to expose a "flag" that `config_setting` rules can use to
  50. # determine if the compiler is Clang.
  51. compiler = "clang",
  52. # We do have to pass in our tool paths.
  53. tool_paths = llvm_tool_paths(llvm_bindir, clang_bindir),
  54. )
  55. cc_toolchain_config = rule(
  56. implementation = _impl,
  57. attrs = {
  58. "target_cpu": attr.string(mandatory = True),
  59. "target_os": attr.string(mandatory = True),
  60. },
  61. provides = [CcToolchainConfigInfo],
  62. )
  63. def cc_local_toolchain_suite(name, configs):
  64. """Create a toolchain suite that uses the local Clang/LLVM install.
  65. Args:
  66. name: The name of the toolchain suite to produce.
  67. configs: An array of (os, cpu) pairs to support in the toolchain.
  68. """
  69. # An empty filegroup to use when stubbing out the toolchains.
  70. native.filegroup(
  71. name = name + "_empty",
  72. srcs = [],
  73. )
  74. # Create the individual local toolchains for each CPU.
  75. for (os, cpu) in configs:
  76. config_name = "{0}_{1}_{2}".format(name, os, cpu)
  77. cc_toolchain_config(
  78. name = config_name + "_config",
  79. target_os = os,
  80. target_cpu = cpu,
  81. )
  82. cc_toolchain(
  83. name = config_name + "_tools",
  84. all_files = ":" + name + "_empty",
  85. ar_files = ":" + name + "_empty",
  86. as_files = ":" + name + "_empty",
  87. compiler_files = ":" + name + "_empty",
  88. dwp_files = ":" + name + "_empty",
  89. linker_files = ":" + name + "_empty",
  90. objcopy_files = ":" + name + "_empty",
  91. strip_files = ":" + name + "_empty",
  92. supports_param_files = 1,
  93. toolchain_config = ":" + config_name + "_config",
  94. toolchain_identifier = config_name,
  95. )
  96. compatible_with = ["@platforms//cpu:" + cpu, "@platforms//os:" + os]
  97. native.toolchain(
  98. name = config_name,
  99. exec_compatible_with = compatible_with,
  100. target_compatible_with = compatible_with + ["@carbon//bazel/cc_toolchains:stage0"],
  101. toolchain = config_name + "_tools",
  102. toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
  103. )