-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
10 changed files
with
139 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ filegroup( | |
"WORKSPACE", | ||
"//cpu:srcs", | ||
"//os:srcs", | ||
"//host:srcs", | ||
], | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.""", | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ | |
# limitations under the License. | ||
"""The version of bazelbuild/platforms.""" | ||
|
||
version = "0.0.8" | ||
version = "0.0.9" |