Skip to content

Commit 8e26f64

Browse files
committed
Bazel: fix non-swift macOS builds
This is meant to be cleaned up in a later PR with respect to the TODOs.
1 parent 61593ae commit 8e26f64

File tree

6 files changed

+154
-38
lines changed

6 files changed

+154
-38
lines changed

.bazelrc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ common --override_module=semmle_code=%workspace%/misc/bazel/semmle_code_stub
1010

1111
build --repo_env=CC=clang --repo_env=CXX=clang++
1212

13-
build:linux --cxxopt=-std=c++20 --host_cxxopt=-std=c++20
14-
# we currently cannot built the swift extractor for ARM
15-
build:macos --cxxopt=-std=c++20 --host_cxxopt=-std=c++20 --copt=-arch --copt=x86_64 --linkopt=-arch --linkopt=x86_64
16-
build:windows --cxxopt=/std:c++20 --cxxopt=/Zc:preprocessor --host_cxxopt=/std:c++20 --host_cxxopt=/Zc:preprocessor
17-
1813
# this requires developer mode, but is required to have pack installer functioning
1914
startup --windows_enable_symlinks
2015
common --enable_runfiles

.github/workflows/go-tests-other-os.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ on:
77
- .github/workflows/go-tests-other-os.yml
88
- .github/actions/**
99
- codeql-workspace.yml
10+
- MODULE.bazel
11+
- .bazelrc
12+
- misc/bazel/**
1013

1114
permissions:
1215
contents: read

.github/workflows/go-tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ on:
1515
- .github/workflows/go-tests.yml
1616
- .github/actions/**
1717
- codeql-workspace.yml
18+
- MODULE.bazel
19+
- .bazelrc
20+
- misc/bazel/**
1821

1922
permissions:
2023
contents: read

misc/bazel/os.bzl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
""" Os detection facilities. """
2+
3+
def os_select(
4+
ctx = None,
5+
*,
6+
linux = None,
7+
windows = None,
8+
macos = None,
9+
default = None):
10+
"""
11+
This can work both in a macro and a rule context to choose something based on the current OS.
12+
If used in a rule implementation, you need to pass `ctx` and add `OS_DETECTION_ATTRS` to the
13+
rule attributes.
14+
"""
15+
choices = {
16+
"linux": linux or default,
17+
"windows": windows or default,
18+
"macos": macos or default,
19+
}
20+
if not ctx:
21+
return select({
22+
"@platforms//os:%s" % os: v
23+
for os, v in choices.items()
24+
if v != None
25+
})
26+
27+
for os, v in choices.items():
28+
if ctx.target_platform_has_constraint(getattr(ctx.attr, "_%s_constraint" % os)[platform_common.ConstraintValueInfo]):
29+
if v == None:
30+
fail("%s not supported by %s" % (os, ctx.label))
31+
return v
32+
fail("Unknown OS detected")
33+
34+
OS_DETECTION_ATTRS = {
35+
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
36+
"_macos_constraint": attr.label(default = "@platforms//os:macos"),
37+
"_linux_constraint": attr.label(default = "@platforms//os:linux"),
38+
}

misc/bazel/pkg.bzl

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_fil
77
load("@rules_pkg//pkg:pkg.bzl", "pkg_zip")
88
load("@rules_pkg//pkg:providers.bzl", "PackageFilegroupInfo", "PackageFilesInfo")
99
load("@rules_python//python:defs.bzl", "py_binary")
10+
load("//misc/bazel:os.bzl", "OS_DETECTION_ATTRS", "os_select")
1011

1112
def _make_internal(name):
1213
def internal(suffix = "internal", *args):
@@ -15,11 +16,6 @@ def _make_internal(name):
1516

1617
return internal
1718

18-
_PLAT_DETECTION_ATTRS = {
19-
"_windows": attr.label(default = "@platforms//os:windows"),
20-
"_macos": attr.label(default = "@platforms//os:macos"),
21-
}
22-
2319
_PLAT_PLACEHOLDER = "{CODEQL_PLATFORM}"
2420

2521
def _expand_path(path, platform):
@@ -28,28 +24,8 @@ def _expand_path(path, platform):
2824
return ("arch", path)
2925
return ("generic", path)
3026

31-
def _platform_select(
32-
ctx = None,
33-
*,
34-
linux,
35-
windows,
36-
macos):
37-
if ctx:
38-
if ctx.target_platform_has_constraint(ctx.attr._windows[platform_common.ConstraintValueInfo]):
39-
return windows
40-
elif ctx.target_platform_has_constraint(ctx.attr._macos[platform_common.ConstraintValueInfo]):
41-
return macos
42-
else:
43-
return linux
44-
else:
45-
return select({
46-
"@platforms//os:linux": linux,
47-
"@platforms//os:macos": macos,
48-
"@platforms//os:windows": windows,
49-
})
50-
5127
def _detect_platform(ctx = None):
52-
return _platform_select(ctx, linux = "linux64", macos = "osx64", windows = "win64")
28+
return os_select(ctx, linux = "linux64", macos = "osx64", windows = "win64")
5329

5430
def codeql_pkg_files(
5531
*,
@@ -131,7 +107,7 @@ _extract_pkg_filegroup = rule(
131107
attrs = {
132108
"src": attr.label(providers = [PackageFilegroupInfo, DefaultInfo]),
133109
"kind": attr.string(doc = "What part to extract", values = ["generic", "arch"]),
134-
} | _PLAT_DETECTION_ATTRS,
110+
} | OS_DETECTION_ATTRS,
135111
)
136112

137113
_ZipInfo = provider(fields = {"zips_to_prefixes": "mapping of zip files to prefixes"})
@@ -181,7 +157,7 @@ _zip_info_filter = rule(
181157
attrs = {
182158
"srcs": attr.label_list(doc = "_ZipInfos to transform", providers = [_ZipInfo]),
183159
"kind": attr.string(doc = "Which zip kind to consider", values = ["generic", "arch"]),
184-
} | _PLAT_DETECTION_ATTRS,
160+
} | OS_DETECTION_ATTRS,
185161
)
186162

187163
def _imported_zips_manifest_impl(ctx):

swift/rules.bzl

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,106 @@
1+
load("//misc/bazel:os.bzl", "os_select")
2+
3+
# TODO: make a shared library with the internal repos for transitions
4+
# unfortunately github.com/fmeum/rules_meta doesn't work any more with latest bazel
5+
6+
def _transition_impl(settings, attr):
7+
return {
8+
"macos": {
9+
"//command_line_option:copt": [
10+
"-fno-rtti",
11+
# we currently cannot built the swift extractor for ARM
12+
"-arch",
13+
"x86_64",
14+
],
15+
"//command_line_option:cxxopt": [
16+
"-std=c++20",
17+
# we currently cannot built the swift extractor for ARM
18+
"-arch",
19+
"x86_64",
20+
],
21+
"//command_line_option:linkopt": [
22+
# we currently cannot built the swift extractor for ARM
23+
"-arch",
24+
"x86_64",
25+
],
26+
},
27+
"linux": {
28+
"//command_line_option:copt": [
29+
"-fno-rtti",
30+
],
31+
"//command_line_option:cxxopt": [
32+
"-std=c++20",
33+
],
34+
},
35+
"windows": {
36+
"//command_line_option:cxxopt": [
37+
"/std:c++20",
38+
"--cxxopt=/Zc:preprocessor",
39+
],
40+
},
41+
}[attr.os]
42+
43+
_transition = transition(
44+
implementation = _transition_impl,
45+
inputs = [],
46+
outputs = ["//command_line_option:copt", "//command_line_option:cxxopt", "//command_line_option:linkopt"],
47+
)
48+
49+
def _cc_transition_impl(ctx):
50+
src = ctx.attr.src[0]
51+
default_info = src[DefaultInfo]
52+
executable = default_info.files_to_run.executable
53+
runfiles = default_info.default_runfiles
54+
direct = []
55+
if executable:
56+
original_executable = executable
57+
executable = ctx.actions.declare_file(original_executable.basename)
58+
command = "cp %s %s" % (original_executable.path, executable.path)
59+
60+
ctx.actions.run_shell(
61+
inputs = [original_executable],
62+
outputs = [executable],
63+
command = command,
64+
)
65+
66+
# costly, but no other way to remove the internal exe from the runfiles
67+
files = runfiles.files.to_list()
68+
files.remove(original_executable)
69+
files.append(executable)
70+
runfiles = ctx.runfiles(files)
71+
72+
direct = [executable]
73+
74+
providers = [
75+
DefaultInfo(
76+
files = depset(direct),
77+
runfiles = runfiles,
78+
executable = executable,
79+
),
80+
]
81+
for p in (OutputGroupInfo, CcInfo):
82+
if p in src:
83+
providers.append(src[p])
84+
85+
return providers
86+
87+
_cc_transition = rule(
88+
implementation = _cc_transition_impl,
89+
attrs = {
90+
"_allowlist_function_transition": attr.label(
91+
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
92+
),
93+
"src": attr.label(mandatory = True, cfg = _transition),
94+
"os": attr.string(),
95+
},
96+
)
97+
198
def _add_args(kwargs, kwarg, value):
299
kwargs[kwarg] = kwargs.get(kwarg, []) + value
3100

4101
def _wrap_cc(rule, kwargs):
5-
_add_args(kwargs, "copts", [
6-
# Required by LLVM/Swift
7-
"-fno-rtti",
8-
])
102+
name = kwargs.pop("name")
103+
visibility = kwargs.pop("visibility", None)
9104
_add_args(kwargs, "features", [
10105
# temporary, before we do universal merging
11106
"-universal_binaries",
@@ -17,7 +112,13 @@ def _wrap_cc(rule, kwargs):
17112
"@platforms//os:macos": [],
18113
"//conditions:default": ["@platforms//:incompatible"],
19114
}))
20-
rule(**kwargs)
115+
rule(name = "internal/" + name, visibility = ["//visibility:private"], **kwargs)
116+
_cc_transition(
117+
name = name,
118+
visibility = visibility,
119+
src = ":internal/" + name,
120+
os = os_select(linux = "linux", macos = "macos", windows = "windows"),
121+
)
21122

22123
def swift_cc_binary(**kwargs):
23124
_wrap_cc(native.cc_binary, kwargs)

0 commit comments

Comments
 (0)