Skip to content

Commit

Permalink
fix(builtin): add two missing locations where Mac M1 support needs to…
Browse files Browse the repository at this point in the history
… be declared

Fixes bazel-contrib#2733
  • Loading branch information
alexeagle authored and twheys committed Jan 13, 2022
1 parent e62936c commit c4c236e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
38 changes: 22 additions & 16 deletions internal/common/os_name.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
"""

OS_ARCH_NAMES = [
("darwin", "amd64"),
("windows", "amd64"),
("darwin", "amd64"),
("darwin", "arm64"),
("linux", "amd64"),
("linux", "arm64"),
("linux", "s390x"),
Expand All @@ -35,28 +36,33 @@ def os_name(rctx):
A string describing the os for a repository rule
"""
os_name = rctx.os.name.lower()
if os_name.startswith("mac os"):
if os_name.find("windows") != -1:
return OS_NAMES[0]
elif os_name.find("windows") != -1:
return OS_NAMES[1]

# This is not ideal, but bazel doesn't directly expose arch.
arch = rctx.execute(["uname", "-m"]).stdout.strip()
if os_name.startswith("mac os"):
if arch == "x86_64":
return OS_NAMES[1]
elif arch == "arm64":
return OS_NAMES[2]
elif os_name.startswith("linux"):
# This is not ideal, but bazel doesn't directly expose arch.
arch = rctx.execute(["uname", "-m"]).stdout.strip()
if arch == "aarch64":
if arch == "x86_64":
return OS_NAMES[3]
elif arch == "s390x":
elif arch == "aarch64":
return OS_NAMES[4]
else:
return OS_NAMES[2]
else:
fail("Unsupported operating system: " + os_name)
elif arch == "s390x":
return OS_NAMES[5]

def is_darwin_os(rctx):
return os_name(rctx) == OS_NAMES[0]
fail("Unsupported operating system {} architecture {}".format(os_name, arch))

def is_windows_os(rctx):
return os_name(rctx) == OS_NAMES[1]
return os_name(rctx) == OS_NAMES[0]

def is_darwin_os(rctx):
name = os_name(rctx)
return name == OS_NAMES[1] or name == OS_NAMES[2]

def is_linux_os(rctx):
name = os_name(rctx)
return name == OS_NAMES[2] or name == OS_NAMES[3] or name == OS_NAMES[4]
return name == OS_NAMES[3] or name == OS_NAMES[4] or name == OS_NAMES[5]
22 changes: 19 additions & 3 deletions internal/node/node_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ load("//internal/node:node_versions.bzl", "NODE_VERSIONS")
load("//third_party/github.com/bazelbuild/bazel-skylib:lib/paths.bzl", "paths")
load("//toolchains/node:node_toolchain_configure.bzl", "node_toolchain_configure")

_DEFAULT_NODE_VERSION = "12.13.0"

# @unsorted-dict-items
_YARN_VERSIONS = {
"1.3.2": ("yarn-v1.3.2.tar.gz", "yarn-v1.3.2", "6cfe82e530ef0837212f13e45c1565ba53f5199eec2527b85ecbcd88bf26821d"),
Expand Down Expand Up @@ -172,7 +174,7 @@ and `{filename}` with the matching entry from the `node_repositories` attribute.
""",
),
"node_version": attr.string(
default = "12.13.0",
default = _DEFAULT_NODE_VERSION,
doc = "the specific version of NodeJS to install or, if vendored_node is specified, the vendored version of node",
),
"package_json": attr.label_list(
Expand Down Expand Up @@ -235,6 +237,7 @@ If this list is empty, we won't download yarn at all.

BUILT_IN_NODE_PLATFORMS = [
"darwin_amd64",
"darwin_arm64",
"linux_amd64",
"linux_arm64",
"windows_amd64",
Expand All @@ -258,6 +261,15 @@ done
SCRIPT_DIR="$(cd -P "$( dirname "$SOURCE" )" >/dev/null && pwd)"
"""

def _node_exists_for_platform(node_version, os_name):
"Whether a node binary is available for this platform"
if not node_version:
node_version = _DEFAULT_NODE_VERSION
node_major_version = int(node_version.split(".")[0])

# There is no Apple Silicon native version of node before 16
return node_major_version >= 16 or os_name != "darwin_arm64"

def _download_node(repository_ctx):
"""Used to download a NodeJS runtime package.
Expand All @@ -276,6 +288,8 @@ def _download_node(repository_ctx):
host_os = repository_ctx.name.split("nodejs_", 1)[1]

node_version = repository_ctx.attr.node_version
if not _node_exists_for_platform(node_version, host_os):
return
node_repositories = repository_ctx.attr.node_repositories

# We insert our default value here, not on the attribute's default, so it isn't documented.
Expand Down Expand Up @@ -755,7 +769,8 @@ def node_repositories(**kwargs):
Also register bazel toolchains, and make other convenience repositories.
Note, the documentation is generated from the node_repositories_rule, not this macro.
Args:
**kwargs: the documentation is generated from the node_repositories_rule, not this macro.
"""

# 0.14.0: @bazel_tools//tools/bash/runfiles is required for nodejs
Expand All @@ -778,10 +793,11 @@ def node_repositories(**kwargs):
name = node_repository_name,
**kwargs
)
target_tool = "@%s//:node_bin" % node_repository_name if _node_exists_for_platform(kwargs.get("node_version"), os_name) else "node"
native.register_toolchains("@build_bazel_rules_nodejs//toolchains/node:node_%s_toolchain" % os_name)
node_toolchain_configure(
name = "%s_config" % node_repository_name,
target_tool = "@%s//:node_bin" % node_repository_name,
target_tool = target_tool,
)

# This "nodejs" repo is just for convinience so one does not have to target @nodejs_<os_name>//...
Expand Down

0 comments on commit c4c236e

Please sign in to comment.