diff options
author | Xùdōng Yáng <wyverald@gmail.com> | 2024-03-21 13:06:19 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-21 13:06:19 -0400 |
commit | 2af915c086d82ab0b233c8ff420ca72e128b8790 (patch) | |
tree | 44a2032f1dcff522d1fcab39750b5543fa0cb69b | |
parent | 6b04b816a0edc09e1a3ad4a094177e181a2a1803 (diff) | |
download | bazelbuild-platforms-upstream/0.0.9.tar.gz |
Implement local_config_platform in @platforms (#86)upstream/0.0.9
* 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
-rw-r--r-- | .bazelci/presubmit.yml | 73 | ||||
-rw-r--r-- | BUILD | 1 | ||||
-rw-r--r-- | MODULE.bazel | 6 | ||||
-rwxr-xr-x | distro/makerel.sh | 2 | ||||
-rw-r--r-- | host/BUILD.bazel | 18 | ||||
-rw-r--r-- | host/constraints.bzl | 4 | ||||
-rw-r--r-- | host/extension.bzl | 67 | ||||
-rw-r--r-- | tests/BUILD | 8 | ||||
-rwxr-xr-x | tests/host_constraints_test.sh | 3 | ||||
-rw-r--r-- | version.bzl | 2 |
10 files changed, 139 insertions, 45 deletions
diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index b24af0f..98f0619 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -1,46 +1,35 @@ ---- -# -# Bazel releases -# -lts: <s - 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"]' @@ -25,6 +25,7 @@ filegroup( "WORKSPACE", "//cpu:srcs", "//os:srcs", + "//host:srcs", ], ) diff --git a/MODULE.bazel b/MODULE.bazel index 617a84a..7082454 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -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") + diff --git a/distro/makerel.sh b/distro/makerel.sh index 20c2f0b..063226a 100755 --- a/distro/makerel.sh +++ b/distro/makerel.sh @@ -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" diff --git a/host/BUILD.bazel b/host/BUILD.bazel new file mode 100644 index 0000000..1989bba --- /dev/null +++ b/host/BUILD.bazel @@ -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, +) + diff --git a/host/constraints.bzl b/host/constraints.bzl new file mode 100644 index 0000000..776c1d9 --- /dev/null +++ b/host/constraints.bzl @@ -0,0 +1,4 @@ +load("@host_platform//:constraints.bzl", _host_constraints = "HOST_CONSTRAINTS") + +HOST_CONSTRAINTS = _host_constraints + diff --git a/host/extension.bzl b/host/extension.bzl new file mode 100644 index 0000000..8756c59 --- /dev/null +++ b/host/extension.bzl @@ -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.""", +) + diff --git a/tests/BUILD b/tests/BUILD index e0c7526..66a9d38 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -1,4 +1,5 @@ load("//:version.bzl", "version") +load("//host:constraints.bzl", "HOST_CONSTRAINTS") package(default_visibility = ["//visibility:private"]) @@ -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"], +) diff --git a/tests/host_constraints_test.sh b/tests/host_constraints_test.sh new file mode 100755 index 0000000..40535b8 --- /dev/null +++ b/tests/host_constraints_test.sh @@ -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}" diff --git a/version.bzl b/version.bzl index 2068e00..42ded0d 100644 --- a/version.bzl +++ b/version.bzl @@ -13,4 +13,4 @@ # limitations under the License. """The version of bazelbuild/platforms.""" -version = "0.0.8" +version = "0.0.9" |