Skip to content

Commit

Permalink
Implement local_config_platform in @platforms (#86)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Wyverald authored Mar 21, 2024
1 parent 6b04b81 commit 2af915c
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 45 deletions.
73 changes: 31 additions & 42 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,35 @@
---
#
# Bazel releases
#
lts: &lts
bazel: latest

rolling: &rolling
bazel: rolling


targets: &targets
build_targets:
- "//..."


tasks:
ubuntu_lts:
name: ubuntu_lts
ubuntu:
platform: ubuntu2004
<<: *lts
<<: *targets
ubuntu_rolling:
name: ubuntu_rolling
platform: ubuntu2004
<<: *rolling
<<: *targets
macos_lts:
name: macos_lts
build_targets:
- "//..."
test_targets:
- "//..."
environment:
EXPECTED_HOST_CONSTRAINTS: '["@platforms//cpu:x86_64", "@platforms//os:linux"]'
macos:
platform: macos
<<: *lts
<<: *targets
windows_lts:
name: windows_lts
build_targets:
- "//..."
test_targets:
- "//..."
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
<<: *lts
<<: *targets
check_bzlmod:
name: check_bzlmod
# No need to check bzlmod on every platform. This repository only holds
# constants and has no per-platform behavior.
platform: ubuntu2004
<<: *rolling
<<: *targets
build_flags:
- "--enable_bzlmod"
build_targets:
- "//..."
# We don't run this test on Windows, because amazingly, sh_test doesn't work on Windows for exactly
# the @platforms repo (see https://github.com/bazelbuild/platforms/pull/86#issuecomment-2011954684)
#test_targets:
#- "//..."
#environment:
# EXPECTED_HOST_CONSTRAINTS: '["@platforms//cpu:x86_64", "@platforms//os:windows"]'
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ filegroup(
"WORKSPACE",
"//cpu:srcs",
"//os:srcs",
"//host:srcs",
],
)

Expand Down
6 changes: 5 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module(
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,
)

bazel_dep(name = "rules_license", version = "0.0.7")

host_platform = use_extension("//host:extension.bzl", "host_platform")
use_repo(host_platform, "host_platform")

2 changes: 1 addition & 1 deletion distro/makerel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fi


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)

path="github.com/bazelbuild/platforms/releases/download/$version/platforms-$version.tar.gz"
Expand Down
18 changes: 18 additions & 0 deletions host/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions host/constraints.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
load("@host_platform//:constraints.bzl", _host_constraints = "HOST_CONSTRAINTS")

HOST_CONSTRAINTS = _host_constraints

67 changes: 67 additions & 0 deletions host/extension.bzl
Original file line number Diff line number Diff line change
@@ -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.""",
)

8 changes: 8 additions & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//:version.bzl", "version")
load("//host:constraints.bzl", "HOST_CONSTRAINTS")

package(default_visibility = ["//visibility:private"])

Expand All @@ -19,3 +20,10 @@ genrule(
"//: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 changes: 3 additions & 0 deletions tests/host_constraints_test.sh
Original file line number Diff line number Diff line change
@@ -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}"
2 changes: 1 addition & 1 deletion version.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.
"""The version of bazelbuild/platforms."""

version = "0.0.8"
version = "0.0.9"

0 comments on commit 2af915c

Please sign in to comment.