fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

UebelAndreAdded bzlmod support to all extension rules. (#3037)a7aba2d0

main
2.9 KiB103 linesraw
1"""Utilities for llvm targets"""
2
3load("@rules_cc//cc:defs.bzl", "CcInfo")
4
5def _cc_stdcc17_transition_impl(settings, attr):
6    # clang --cxxopt=-std=c++17 --host_cxxopt=-std=c++17
7    # msvc --cxxopt=/std:c++17 --host_cxxopt=/std:c++17
8    host_opt = settings["//command_line_option:host_cxxopt"]
9    tgt_opt = settings["//command_line_option:cxxopt"]
10
11    host_opt.extend(attr.cxxopts)
12    tgt_opt.extend(attr.cxxopts)
13
14    return {
15        "//command_line_option:cxxopt": tgt_opt,
16        "//command_line_option:host_cxxopt": host_opt,
17    }
18
19_cc_stdcc17_transition = transition(
20    implementation = _cc_stdcc17_transition_impl,
21    inputs = [
22        "//command_line_option:host_cxxopt",
23        "//command_line_option:cxxopt",
24    ],
25    outputs = [
26        "//command_line_option:host_cxxopt",
27        "//command_line_option:cxxopt",
28    ],
29)
30
31_CXX_OPTS = select({
32    "@platforms//os:windows": ["/std:c++17"],
33    "//conditions:default": ["-std=c++17"],
34})
35
36_COMMON_ATTRS = {
37    "cxxopts": attr.string_list(
38        doc = "Flags to inject into `--host_cxxopt` and `--cxxopt` command line flags.",
39        mandatory = True,
40    ),
41    "target": attr.label(
42        doc = "The target to transition.",
43        mandatory = True,
44    ),
45}
46
47def _cc_stdcc17_transitioned_target_impl(ctx):
48    providers = []
49
50    if CcInfo in ctx.attr.target:
51        providers.append(ctx.attr.target[CcInfo])
52
53    if DefaultInfo in ctx.attr.target:
54        info = ctx.attr.target[DefaultInfo]
55        kwargs = {
56            "files": info.files,
57            "runfiles": info.default_runfiles,
58        }
59
60        if info.files_to_run and info.files_to_run.executable:
61            exe = info.files_to_run.executable
62            kwargs["executable"] = ctx.actions.declare_file("{}.{}".format(ctx.label.name, exe.extension).rstrip("."))
63            ctx.actions.symlink(
64                output = kwargs["executable"],
65                target_file = exe,
66                is_executable = True,
67            )
68
69        providers.append(DefaultInfo(**kwargs))
70
71    if OutputGroupInfo in ctx.attr.target:
72        providers.append(ctx.attr.target[OutputGroupInfo])
73
74    return providers
75
76_cc_stdcc17_transitioned_library = rule(
77    doc = "A rule to transition a C++ library to build with stdc++17.",
78    implementation = _cc_stdcc17_transitioned_target_impl,
79    cfg = _cc_stdcc17_transition,
80    attrs = _COMMON_ATTRS,
81)
82
83_cc_stdcc17_transitioned_binary = rule(
84    doc = "A rule to transition a C++ binary to build with stdc++17.",
85    implementation = _cc_stdcc17_transitioned_target_impl,
86    cfg = _cc_stdcc17_transition,
87    attrs = _COMMON_ATTRS,
88    executable = True,
89)
90
91def cc_stdcc17_transitioned_library(name, **kwargs):
92    _cc_stdcc17_transitioned_library(
93        name = name,
94        cxxopts = _CXX_OPTS,
95        **kwargs
96    )
97
98def cc_stdcc17_transitioned_binary(name, **kwargs):
99    _cc_stdcc17_transitioned_binary(
100        name = name,
101        cxxopts = _CXX_OPTS,
102        **kwargs
103    )