|
1 | 1 | """Defines the repo rules and module extension for managing C++ toolchains across different platforms.""" |
2 | 2 |
|
3 | | -load("//impl:alpine.bzl", "extract_alpine") |
4 | | -load("//impl:config.bzl", "get_config_from_env_vars", "repro_dump") |
5 | | -load("//impl:ubuntu.bzl", "extract_ubuntu") |
6 | | - |
7 | | -def _lazy_download_bins_impl(rctx): |
8 | | - """Lazily downloads only the toolchain binaries for the configured platform.""" |
9 | | - config = get_config_from_env_vars(rctx) |
10 | | - repro_dump(rctx, config) |
11 | | - |
12 | | - # TODO: not a huge fan of vendor == "unknown" but it's how ubuntu distrubtions are packaged |
13 | | - if config["vendor"] == "unknown": |
14 | | - extract_ubuntu(rctx, config) |
15 | | - elif config["vendor"] == "alpine": |
16 | | - extract_alpine(rctx, config) |
17 | | - else: |
18 | | - fail("(toolchains_cc.bzl bug) Unknown vendor: %s" % config["vendor"]) |
19 | | - |
20 | | - rctx.download_and_extract( |
21 | | - url = "https://github.com/reutermj/toolchains_cc.bzl/releases/download/binaries/llvm-19.1.7-linux-x86_64.tar.xz", |
22 | | - ) |
23 | | - |
24 | | - rctx.file( |
25 | | - "BUILD", |
26 | | - """ |
27 | | -load("@toolchains_cc//impl:declare_tools.bzl", "declare_tools") |
28 | | -declare_tools( |
29 | | - name = "{original_name}", |
30 | | - all_files = glob(["**"]), |
31 | | - visibility = ["//visibility:public"], |
32 | | -) |
33 | | -""".format( |
34 | | - original_name = rctx.original_name, |
35 | | - ), |
36 | | - ) |
37 | | - |
38 | | -def _eager_declare_toolchain_impl(rctx): |
39 | | - """Eagerly declare the toolchain(...) to determine which registered toolchain is valid for the current platform.""" |
40 | | - config = get_config_from_env_vars(rctx) |
41 | | - |
42 | | - rctx.file( |
43 | | - "BUILD", |
44 | | - """ |
45 | | -load("@toolchains_cc//impl:declare_toolchain.bzl", "declare_toolchain") |
46 | | -load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain") |
47 | | -declare_toolchain( |
48 | | - name = "{original_name}", |
49 | | - cxx_std_lib = "{cxx_std_lib}", |
50 | | - vendor = "{vendor}", |
51 | | - target_triple = "{target_triple}", |
52 | | - sysroot = "@@{bins_repo_name}//:{original_name}_bins.sysroot", |
53 | | - all_tools = "@@{bins_repo_name}//:{original_name}_bins.all_tools", |
54 | | - visibility = ["//visibility:public"], |
55 | | -) |
56 | | -
|
57 | | -# TODO: currently cant declare this in the macro because this rule creates |
58 | | -# a target that doesnt following the naming rules of macros. |
59 | | -cc_toolchain( |
60 | | - name = "{original_name}_cc_toolchain", |
61 | | - args = [ |
62 | | - ":{original_name}-no-canonical-prefixes", |
63 | | - ":{original_name}_target_triple", |
64 | | - ":{original_name}-sysroot-arg", |
65 | | - ":{original_name}_use_llvm_linker", |
66 | | - ":{original_name}_cxx_std_lib", |
67 | | - ], |
68 | | - enabled_features = ["@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features"], |
69 | | - known_features = ["@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features"], |
70 | | - tool_map = "@@{bins_repo_name}//:{original_name}_bins.all_tools", |
71 | | - visibility = ["//visibility:public"], |
72 | | -) |
73 | | -""".format( |
74 | | - original_name = rctx.original_name, |
75 | | - cxx_std_lib = config["cxx_std_lib"], |
76 | | - vendor = config["vendor"], |
77 | | - target_triple = config["triple"], |
78 | | - bins_repo_name = rctx.name + "_bins", |
79 | | - ), |
80 | | - ) |
81 | | - |
82 | | -_lazy_download_bins = repository_rule( |
83 | | - implementation = _lazy_download_bins_impl, |
84 | | - attrs = { |
85 | | - "toolchain_name": attr.string( |
86 | | - mandatory = True, |
87 | | - doc = "The name of the toolchain, used for registration.", |
88 | | - ), |
89 | | - "_build_tpl": attr.label( |
90 | | - default = "@toolchains_cc//:bins.BUILD.tpl", |
91 | | - ), |
92 | | - }, |
93 | | -) |
94 | | - |
95 | | -_eager_declare_toolchain = repository_rule( |
96 | | - implementation = _eager_declare_toolchain_impl, |
97 | | - attrs = { |
98 | | - "toolchain_name": attr.string( |
99 | | - mandatory = True, |
100 | | - doc = "The name of the toolchain, used for registration.", |
101 | | - ), |
102 | | - "_build_tpl": attr.label( |
103 | | - default = "@toolchains_cc//:toolchain.BUILD.tpl", |
104 | | - ), |
105 | | - }, |
106 | | -) |
| 3 | +load("//impl:declare_toolchain.bzl", "eager_declare_toolchain") |
| 4 | +load("//impl:declare_tools.bzl", "lazy_download_bins") |
107 | 5 |
|
108 | 6 | def _cxx_toolchains(module_ctx): |
109 | 7 | for mod in module_ctx.modules: |
@@ -145,11 +43,11 @@ def _cxx_toolchains(module_ctx): |
145 | 43 | if declared_toolchain.name == "toolchains_cc_default_toolchain": |
146 | 44 | toolchain_name = "toolchains_cc" |
147 | 45 |
|
148 | | - _eager_declare_toolchain( |
| 46 | + eager_declare_toolchain( |
149 | 47 | name = declared_toolchain.name, |
150 | 48 | toolchain_name = toolchain_name, |
151 | 49 | ) |
152 | | - _lazy_download_bins( |
| 50 | + lazy_download_bins( |
153 | 51 | name = declared_toolchain.name + "_bins", |
154 | 52 | toolchain_name = toolchain_name, |
155 | 53 | ) |
|
0 commit comments