Skip to content

Commit 8e20268

Browse files
committed
[Bazel] Replace the use of @bazel_tools//src/conditions:host_windows
This has been deprecated and will be removed in Bazel 10. This implementation of the platform information retrieval is similar to what is done in Skylib (see https://github.com/bazelbuild/bazel-skylib/blob/56a2abbaf131332835ab2721a258ea3c763a7178/rules/private/copy_file_private.bzl#L117) however this does not use the experimental platform API. - We define a provider that gives the script extension for a given platform and a rule to instantiate it. - We define a target with the `platform_info` rule that selects the correct settings based on constraints from `@platform`. - During toolchain config creation we inject that target **using the `exec` config**. - We can then retrieve the platform information in the toolchain config (namely the script extension here). Note that the platform information could have given a simple `is_windows` flag, but I chose this implementation because it matches the previous one more closely. Note also that it was not possible to keep the `script_extension` attribute as string, because the `cfg` field is not supported on string attributes.
1 parent d6b88f4 commit 8e20268

4 files changed

Lines changed: 43 additions & 9 deletions

File tree

bazel/emscripten_toolchain/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@ load("@rules_cc//cc:defs.bzl", "cc_library")
22
load("@rules_python//python:py_binary.bzl", "py_binary")
33
load("//:emscripten_deps.bzl", "emscripten_repo_name")
44
load("//:remote_emscripten_repository.bzl", "create_toolchains", "emscripten_toolchain_name")
5+
load(":platform_info.bzl", "platform_info")
56

67
package(default_visibility = ["//visibility:public"])
78

89
# dlmalloc.bc is implicitly added by the emscripten toolchain
910
cc_library(name = "malloc")
1011

12+
platform_info(
13+
name = "platform_info",
14+
script_extension = select({
15+
"@platforms//os:windows": "bat",
16+
"//conditions:default": "sh",
17+
}),
18+
visibility = [":__subpackages__"],
19+
)
20+
1121
create_toolchains(
1222
name = emscripten_toolchain_name("linux"),
1323
exec_compatible_with = [
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Defines a provider and rule for platform information.
2+
"""
3+
4+
PlatformInfo = provider(
5+
doc = "Provides some info about a platform",
6+
fields = {
7+
"script_extension": "The script extension for the platform, e.g. 'sh' or 'bat'",
8+
},
9+
)
10+
11+
platform_info = rule(
12+
implementation = lambda ctx: PlatformInfo(
13+
script_extension = ctx.attr.script_extension,
14+
),
15+
attrs = {
16+
"script_extension": attr.string(
17+
mandatory = True,
18+
values = ["sh", "bat"],
19+
doc = "The script extension for the platform, e.g. 'sh' or 'bat'",
20+
),
21+
},
22+
)

bazel/emscripten_toolchain/toolchain.bzl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ load(
1616
_flag_set = "flag_set",
1717
)
1818
load("@rules_cc//cc:defs.bzl", "CcToolchainConfigInfo", "cc_common")
19+
load(":platform_info.bzl", "PlatformInfo", "platform_info")
1920

2021
def flag_set(flags = None, features = None, not_features = None, **kwargs):
2122
"""Extension to flag_set which allows for a "simple" form.
@@ -56,6 +57,8 @@ CROSSTOOL_DEFAULT_WARNINGS = [
5657
]
5758

5859
def _impl(ctx):
60+
platform_info = ctx.attr._exec_platform_info[PlatformInfo]
61+
5962
target_cpu = ctx.attr.cpu
6063
toolchain_identifier = "emscripten-" + target_cpu
6164
target_system_name = target_cpu + "-unknown-emscripten"
@@ -82,9 +85,9 @@ def _impl(ctx):
8285

8386
builtin_sysroot = emscripten_dir + "/emscripten/cache/sysroot"
8487

85-
emcc_script = "emcc.%s" % ctx.attr.script_extension
86-
emcc_link_script = "emcc_link.%s" % ctx.attr.script_extension
87-
emar_script = "emar.%s" % ctx.attr.script_extension
88+
emcc_script = "emcc.%s" % platform_info.script_extension
89+
emcc_link_script = "emcc_link.%s" % platform_info.script_extension
90+
emar_script = "emar.%s" % platform_info.script_extension
8891

8992
################################################################
9093
# Tools
@@ -1162,7 +1165,11 @@ emscripten_cc_toolchain_config_rule = rule(
11621165
"em_config": attr.label(mandatory = True, allow_single_file = True),
11631166
"emscripten_binaries": attr.label(mandatory = True, cfg = "exec"),
11641167
"nodejs_bin": attr.label(mandatory = True, allow_single_file = True),
1165-
"script_extension": attr.string(mandatory = True, values = ["sh", "bat"]),
1168+
"_exec_platform_info": attr.label(
1169+
providers = [PlatformInfo],
1170+
default = Label(":platform_info"),
1171+
cfg = "exec",
1172+
),
11661173
},
11671174
provides = [CcToolchainConfigInfo],
11681175
toolchains = [
@@ -1178,7 +1185,6 @@ def _python_interpreter_files_impl(ctx):
11781185

11791186
return DefaultInfo(files = python_exec_runtime.files)
11801187

1181-
11821188
emscripten_python_interpreter_files = rule(
11831189
implementation = _python_interpreter_files_impl,
11841190
toolchains = [

bazel/remote_emscripten_repository.bzl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ def create_toolchains(name, repo_name, exec_compatible_with):
116116
em_config = "@emscripten_cache//:emscripten_config",
117117
emscripten_binaries = repo_compiler_files_target,
118118
nodejs_bin = "@nodejs//:node",
119-
script_extension = select({
120-
"@bazel_tools//src/conditions:host_windows": "bat",
121-
"//conditions:default": "sh",
122-
}),
123119
)
124120

125121
cc_toolchain(

0 commit comments

Comments
 (0)