Implement local_config_platform in @platforms (#86)

* POC: implement local_config_platform in @platforms

* missing colons

* move stuff around

* the repo rule need not be public

* attempt at a test setup...

* whoops

* newlines

* comments

* more comments
This commit is contained in:
Xùdōng Yáng 2024-03-21 13:06:19 -04:00 committed by GitHub
parent 6b04b816a0
commit 2af915c086
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 139 additions and 45 deletions

View File

@ -1,46 +1,35 @@
--- tasks:
# ubuntu:
# Bazel releases platform: ubuntu2004
#
lts: &lts
bazel: latest
rolling: &rolling
bazel: rolling
targets: &targets
build_targets: build_targets:
- "//..." - "//..."
test_targets:
- "//..."
tasks: environment:
ubuntu_lts: EXPECTED_HOST_CONSTRAINTS: '["@platforms//cpu:x86_64", "@platforms//os:linux"]'
name: ubuntu_lts macos:
platform: ubuntu2004
<<: *lts
<<: *targets
ubuntu_rolling:
name: ubuntu_rolling
platform: ubuntu2004
<<: *rolling
<<: *targets
macos_lts:
name: macos_lts
platform: macos platform: macos
<<: *lts build_targets:
<<: *targets - "//..."
windows_lts: test_targets:
name: windows_lts - "//..."
environment:
EXPECTED_HOST_CONSTRAINTS: '["@platforms//cpu:x86_64", "@platforms//os:osx"]'
macos_arm64:
platform: macos_arm64
build_targets:
- "//..."
test_targets:
- "//..."
environment:
EXPECTED_HOST_CONSTRAINTS: '["@platforms//cpu:aarch64", "@platforms//os:osx"]'
windows:
platform: windows platform: windows
<<: *lts build_targets:
<<: *targets - "//..."
check_bzlmod: # We don't run this test on Windows, because amazingly, sh_test doesn't work on Windows for exactly
name: check_bzlmod # the @platforms repo (see https://github.com/bazelbuild/platforms/pull/86#issuecomment-2011954684)
# No need to check bzlmod on every platform. This repository only holds #test_targets:
# constants and has no per-platform behavior. #- "//..."
platform: ubuntu2004 #environment:
<<: *rolling # EXPECTED_HOST_CONSTRAINTS: '["@platforms//cpu:x86_64", "@platforms//os:windows"]'
<<: *targets
build_flags:
- "--enable_bzlmod"

1
BUILD
View File

@ -25,6 +25,7 @@ filegroup(
"WORKSPACE", "WORKSPACE",
"//cpu:srcs", "//cpu:srcs",
"//os:srcs", "//os:srcs",
"//host:srcs",
], ],
) )

View File

@ -1,7 +1,11 @@
module( module(
name = "platforms", name = "platforms",
version = "0.0.8", # keep in sync with version.bzl version = "0.0.9", # keep in sync with version.bzl
compatibility_level = 1, compatibility_level = 1,
) )
bazel_dep(name = "rules_license", version = "0.0.7") bazel_dep(name = "rules_license", version = "0.0.7")
host_platform = use_extension("//host:extension.bzl", "host_platform")
use_repo(host_platform, "host_platform")

View File

@ -19,7 +19,7 @@ fi
dist_file="/tmp/platforms-${version}.tar.gz" dist_file="/tmp/platforms-${version}.tar.gz"
tar czf "$dist_file" BUILD LICENSE MODULE.bazel WORKSPACE WORKSPACE.bzlmod version.bzl cpu os tar czf "$dist_file" BUILD LICENSE MODULE.bazel WORKSPACE WORKSPACE.bzlmod version.bzl cpu os host
sha256=$(shasum -a256 "$dist_file" | cut -d' ' -f1) sha256=$(shasum -a256 "$dist_file" | cut -d' ' -f1)
path="github.com/bazelbuild/platforms/releases/download/$version/platforms-$version.tar.gz" path="github.com/bazelbuild/platforms/releases/download/$version/platforms-$version.tar.gz"

18
host/BUILD.bazel Normal file
View File

@ -0,0 +1,18 @@
# Host platform detection
load("@host_platform//:constraints.bzl", "HOST_CONSTRAINTS")
package(default_visibility = ["//visibility:public"])
exports_files(["constraints.bzl", "extension.bzl"])
filegroup(
name = "srcs",
srcs = glob(["**"]),
)
platform(
name = "host",
constraint_values = HOST_CONSTRAINTS,
)

4
host/constraints.bzl Normal file
View File

@ -0,0 +1,4 @@
load("@host_platform//:constraints.bzl", _host_constraints = "HOST_CONSTRAINTS")
HOST_CONSTRAINTS = _host_constraints

67
host/extension.bzl Normal file
View File

@ -0,0 +1,67 @@
def _translate_cpu(arch):
if arch in ["i386", "i486", "i586", "i686", "i786", "x86"]:
return "x86_32"
if arch in ["amd64", "x86_64", "x64"]:
return "x86_64"
if arch in ["ppc", "ppc64", "ppc64le"]:
return "ppc"
if arch in ["arm", "armv7l"]:
return "arm"
if arch in ["aarch64"]:
return "aarch64"
if arch in ["s390x", "s390"]:
return "s390x"
if arch in ["mips64el", "mips64"]:
return "mips64"
if arch in ["riscv64"]:
return "riscv64"
return None
def _translate_os(os):
if os.startswith("mac os"):
return "osx"
if os.startswith("freebsd"):
return "freebsd"
if os.startswith("openbsd"):
return "openbsd"
if os.startswith("linux"):
return "linux"
if os.startswith("windows"):
return "windows"
return None
def _host_platform_repo_impl(rctx):
cpu = _translate_cpu(rctx.os.arch)
os = _translate_os(rctx.os.name)
cpu = "" if cpu == None else " '@platforms//cpu:%s',\n" % cpu
os = "" if os == None else " '@platforms//os:%s',\n" % os
rctx.file("BUILD.bazel", """
# DO NOT EDIT: automatically generated BUILD file
exports_files(["constraints.bzl"])
""")
rctx.file("constraints.bzl", """
# DO NOT EDIT: automatically generated constraints list
HOST_CONSTRAINTS = [
%s%s]
""" % (cpu, os))
host_platform_repo = repository_rule(
implementation = _host_platform_repo_impl,
doc = """Generates constraints for the host platform. The constraints.bzl
file contains a single <code>HOST_CONSTRAINTS</code> variable, which is a
list of strings, each of which is a label to a <code>constraint_value</code>
for the host platform.""",
)
def _host_platform_impl(_mctx):
host_platform_repo(name = "host_platform")
host_platform = module_extension(
implementation = _host_platform_impl,
doc = """Generates a <code>host_platform_repo</code> repo named
<code>host_platform</code>, containing constraints for the host platform.""",
)

View File

@ -1,4 +1,5 @@
load("//:version.bzl", "version") load("//:version.bzl", "version")
load("//host:constraints.bzl", "HOST_CONSTRAINTS")
package(default_visibility = ["//visibility:private"]) package(default_visibility = ["//visibility:private"])
@ -19,3 +20,10 @@ genrule(
"//:MODULE.bazel", "//:MODULE.bazel",
], ],
) )
sh_test(
name = "host_constraints_test",
srcs = ["host_constraints_test.sh"],
env = {"ACTUAL_HOST_CONSTRAINTS": repr(HOST_CONSTRAINTS)},
env_inherit = ["EXPECTED_HOST_CONSTRAINTS"],
)

3
tests/host_constraints_test.sh Executable file
View File

@ -0,0 +1,3 @@
echo actual host constraints: ${ACTUAL_HOST_CONSTRAINTS}
echo expected host constraints: ${EXPECTED_HOST_CONSTRAINTS}
test "${ACTUAL_HOST_CONSTRAINTS}" == "${EXPECTED_HOST_CONSTRAINTS}"

View File

@ -13,4 +13,4 @@
# limitations under the License. # limitations under the License.
"""The version of bazelbuild/platforms.""" """The version of bazelbuild/platforms."""
version = "0.0.8" version = "0.0.9"