Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 1 addition & 65 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,72 +22,8 @@ alias(
visibility = ["//:__subpackages__"],
)

constraint_value(
name = "armv6hf",
constraint_setting = "@platforms//cpu",
)

# Raspberry Pi
config_setting(
name = "linux_armv6hf",
constraint_values = [
"@platforms//os:linux",
"//:armv6hf",
],
)

config_setting(
name = "darwin_x86_64",
constraint_values = [
"@platforms//os:macos",
"@platforms//cpu:x86_64",
],
)

config_setting(
name = "darwin_aarch64",
constraint_values = [
"@platforms//os:macos",
"@platforms//cpu:aarch64",
],
)

config_setting(
name = "linux_x86_64",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
)

config_setting(
name = "linux_aarch64",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
)

config_setting(
name = "windows_x86_64",
constraint_values = [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
)

alias(
name = "shellcheck",
actual = select(
{
":darwin_aarch64": "@shellcheck_darwin_aarch64//:shellcheck",
":darwin_x86_64": "@shellcheck_darwin_x86_64//:shellcheck",
":linux_aarch64": "@shellcheck_linux_aarch64//:shellcheck",
":linux_armv6hf": "@shellcheck_linux_armv6hf//:shellcheck",
":linux_x86_64": "@shellcheck_linux_x86_64//:shellcheck",
":windows_x86_64": "@shellcheck_windows_x86_64//:shellcheck",
},
no_match_error = "binaries for your platform could not be found",
),
actual = "//shellcheck:current_shellcheck_toolchain",
visibility = ["//visibility:public"],
)
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ use_repo(
"shellcheck_linux_aarch64",
"shellcheck_linux_armv6hf",
"shellcheck_linux_x86_64",
"shellcheck_toolchains",
"shellcheck_windows_x86_64",
)

register_toolchains(
"@shellcheck_toolchains//:all",
)

# Dev dependencies

bazel_dep(name = "rules_pkg", version = "0.9.1", dev_dependency = True)
Expand Down
1 change: 1 addition & 0 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- OSX 64-bit
"""

# buildifier: disable=bzl-visibility
load("//shellcheck/internal:extensions.bzl", _shellcheck_dependencies = "shellcheck_dependencies")

shellcheck_dependencies = _shellcheck_dependencies
1 change: 1 addition & 0 deletions shellcheck/.shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# https://github.com/koalaman/shellcheck/blob/v0.11.0/shellcheck.1.md#rc-files
26 changes: 26 additions & 0 deletions shellcheck/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
load("//shellcheck/internal:toolchain.bzl", "current_shellcheck_toolchain")

filegroup(
name = "distribution",
srcs = [
".shellcheckrc",
"BUILD.bazel",
"defs.bzl",
"shellcheck_aspect.bzl",
"shellcheck_test.bzl",
"shellcheck_toolchain.bzl",
"//shellcheck/internal:distribution",
"//shellcheck/settings:distribution",
],
visibility = ["//:__pkg__"],
)

toolchain_type(
name = "toolchain_type",
visibility = ["//visibility:public"],
)

current_shellcheck_toolchain(
name = "current_shellcheck_toolchain",
visibility = ["//visibility:public"],
)

alias(
name = "shellcheck",
actual = ":current_shellcheck_toolchain",
visibility = ["//visibility:public"],
)

label_flag(
name = "rc",
build_setting_default = ".shellcheckrc",
visibility = ["//visibility:public"],
)
147 changes: 136 additions & 11 deletions shellcheck/internal/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,76 @@

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@rules_shellcheck//shellcheck:shellcheck_toolchain.bzl", "shellcheck_toolchain")

_HUB_BUILD_CONTENT = """\
{toolchains}
"""

_CONSTRAINTS = {
"darwin_aarch64": [
"@platforms//os:macos",
"@platforms//cpu:aarch64",
],
"darwin_x86_64": [
"@platforms//os:macos",
"@platforms//cpu:x86_64",
],
"linux_aarch64": [
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
"linux_armv6hf": [
"@platforms//cpu:armv6-m",
"@platforms//os:linux",
],
"linux_x86_64": [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
"windows_x86_64": [
"@platforms//os:windows",
"@platforms//cpu:x86_64",
],
}

_TOOLCHAIN_ENTRY = """\
toolchain(
name = "shellcheck_toolchain_{arch}",
toolchain_type = "@rules_shellcheck//shellcheck:toolchain_type",
toolchain = "{toolchain}",
exec_compatible_with = {constraints},
visibility = ["//visibility:public"],
)
"""

def _shellcheck_toolchains_hub_impl(repository_ctx):
toolchains = []
for toolchain, arch in repository_ctx.attr.toolchains.items():
toolchains.append(_TOOLCHAIN_ENTRY.format(
arch = arch,
constraints = repr(_CONSTRAINTS[arch]),
toolchain = str(toolchain),
))

repository_ctx.file("BUILD.bazel", _HUB_BUILD_CONTENT.format(
toolchains = "\n".join(toolchains),
))

repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
repository_ctx.name,
))

shellcheck_toolchains_hub = repository_rule(
doc = "A repository rule for defining shellcheck toolchains",
implementation = _shellcheck_toolchains_hub_impl,
attrs = {
"toolchains": attr.label_keyed_string_dict(
doc = "A mapping of toolchain labels to platforms.",
mandatory = True,
),
},
)

def _urls(arch, version):
archive_template_name = {
Expand All @@ -27,6 +97,48 @@ def _urls(arch, version):
url,
]

def create_shellcheck_repository_targets(name, shellcheck):
"""A utility function for defining shellcheck repositories

Args:
name (str): The name of the repository.
shellcheck (str): THe path to the shellcheck binary.
"""
visibility = ["//visibility:public"]

native.exports_files(
[shellcheck],
visibility = visibility,
)

native.alias(
name = name,
actual = shellcheck,
visibility = visibility,
)

if shellcheck.endswith(".exe"):
native.alias(
name = shellcheck[:-4],
actual = shellcheck,
visibility = visibility,
)

shellcheck_toolchain(
name = "toolchain",
shellcheck = shellcheck,
visibility = visibility,
)

_SHELLCHECK_CONTENT = """\
load("@rules_shellcheck//shellcheck/internal:extensions.bzl", "create_shellcheck_repository_targets")

create_shellcheck_repository_targets(
name = "{name}",
shellcheck = "{shellcheck}",
)
"""

def shellcheck_dependencies():
"""Define shellcheck repositories"""
version = "v0.11.0"
Expand All @@ -36,27 +148,40 @@ def shellcheck_dependencies():
"linux_aarch64": "12b331c1d2db6b9eb13cfca64306b1b157a86eb69db83023e261eaa7e7c14588",
"linux_armv6hf": "8afc50b302d5feeac9381ea114d563f0150d061520042b254d6eb715797c8223",
"linux_x86_64": "8c3be12b05d5c177a04c29e3c78ce89ac86f1595681cab149b65b97c4e227198",
"windows_x86_64": "8a4e35ab0b331c85d73567b12f2a444df187f483e5079ceffa6bda1faa2e740e",
}

toolchains = {}

for arch, sha256 in sha256.items():
name = "shellcheck_{arch}".format(arch = arch)

strip_prefix = "shellcheck-{version}".format(version = version)
shellcheck_bin = "shellcheck"

# Special case, as it is a zip archive with no prefix to strip.
if "windows" in arch:
strip_prefix = None
shellcheck_bin = "shellcheck.exe"

maybe(
http_archive,
name = "shellcheck_{arch}".format(arch = arch),
strip_prefix = "shellcheck-{version}".format(version = version),
build_file_content = """exports_files(["shellcheck"])
""",
name = name,
strip_prefix = strip_prefix,
build_file_content = _SHELLCHECK_CONTENT.format(
name = name,
shellcheck = shellcheck_bin,
),
sha256 = sha256,
urls = _urls(arch = arch, version = version),
)

# Special case, as it is a zip archive with no prefix to strip.
toolchains["@{}//:toolchain".format(name)] = arch

maybe(
http_archive,
name = "shellcheck_windows_x86_64",
build_file_content = """exports_files(["shellcheck"])
""",
sha256 = "8a4e35ab0b331c85d73567b12f2a444df187f483e5079ceffa6bda1faa2e740e",
urls = _urls(arch = "windows_x86_64", version = version),
shellcheck_toolchains_hub,
name = "shellcheck_toolchains",
toolchains = toolchains,
)

def _impl(_):
Expand Down
Loading