-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathrules.bzl
More file actions
131 lines (117 loc) · 4.11 KB
/
rules.bzl
File metadata and controls
131 lines (117 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
load("@rules_cc//cc:defs.bzl", "CcInfo", "cc_binary", "cc_library")
load("//misc/bazel:os.bzl", "os_select")
# TODO: make a shared library with the internal repos for transitions
# unfortunately github.com/fmeum/rules_meta doesn't work any more with latest bazel
def _transition_impl(settings, attr):
return {
"macos": {
"//command_line_option:copt": [
"-fno-rtti",
# we currently cannot built the swift extractor for ARM
"-arch",
"x86_64",
],
"//command_line_option:cxxopt": [
"-std=c++20",
# we currently cannot built the swift extractor for ARM
"-arch",
"x86_64",
],
"//command_line_option:linkopt": [
# we currently cannot built the swift extractor for ARM
"-arch",
"x86_64",
],
},
"linux": {
"//command_line_option:copt": [
"-fno-rtti",
],
"//command_line_option:cxxopt": [
"-std=c++20",
],
"//command_line_option:linkopt": [],
},
"windows": {
"//command_line_option:copt": [],
"//command_line_option:cxxopt": [
"/std:c++20",
"--cxxopt=/Zc:preprocessor",
],
"//command_line_option:linkopt": [],
},
}[attr.os]
_transition = transition(
implementation = _transition_impl,
inputs = [],
outputs = ["//command_line_option:copt", "//command_line_option:cxxopt", "//command_line_option:linkopt"],
)
def _cc_transition_impl(ctx):
src = ctx.attr.src[0]
default_info = src[DefaultInfo]
executable = default_info.files_to_run.executable
runfiles = default_info.default_runfiles
direct = []
if executable:
original_executable = executable
executable = ctx.actions.declare_file(original_executable.basename)
command = "cp %s %s" % (original_executable.path, executable.path)
ctx.actions.run_shell(
inputs = [original_executable],
outputs = [executable],
command = command,
)
# costly, but no other way to remove the internal exe from the runfiles
files = runfiles.files.to_list()
files.remove(original_executable)
files.append(executable)
runfiles = ctx.runfiles(files)
direct = [executable]
providers = [
DefaultInfo(
files = depset(direct),
runfiles = runfiles,
executable = executable,
),
]
for p in (OutputGroupInfo, CcInfo):
if p in src:
providers.append(src[p])
return providers
_cc_transition = rule(
implementation = _cc_transition_impl,
attrs = {
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
"src": attr.label(mandatory = True, cfg = _transition),
"os": attr.string(),
},
)
def _add_args(kwargs, kwarg, value):
kwargs[kwarg] = kwargs.get(kwarg, []) + value
def _wrap_cc(rule, kwargs):
name = kwargs.pop("name")
visibility = kwargs.pop("visibility", None)
_add_args(kwargs, "features", [
# temporary, before we do universal merging
"-universal_binaries",
])
if "target_compatible_with" not in kwargs:
# Restrict to Linux or macOS by default, but allow overriding
_add_args(kwargs, "target_compatible_with", select({
"@platforms//os:linux": [],
"@platforms//os:macos": [],
"//conditions:default": ["@platforms//:incompatible"],
}))
rule(name = "internal/" + name, visibility = ["//visibility:private"], **kwargs)
_cc_transition(
name = name,
visibility = visibility,
src = ":internal/" + name,
os = os_select(linux = "linux", macos = "macos", windows = "windows"),
)
def swift_cc_binary(**kwargs):
_wrap_cc(cc_binary, kwargs)
def swift_cc_library(**kwargs):
_wrap_cc(cc_library, kwargs)