Skip to content

Commit

Permalink
refactor: align toolchains
Browse files Browse the repository at this point in the history
  • Loading branch information
mgred committed Jan 18, 2024
1 parent 324fd1e commit 9072cb1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 29 deletions.
6 changes: 3 additions & 3 deletions s2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ bzl_library(
)

bzl_library(
name = "artifacts",
name = "assets",
srcs = [
"artifacts.bzl",
"assets.bzl",
],
visibility = ["//visibility:private"],
deps = [
Expand All @@ -33,7 +33,7 @@ bzl_library(
srcs = ["repositories.bzl"],
visibility = ["//visibility:public"],
deps = [
":artifacts",
":assets",
":utils",
"@bzlparty_tools//lib:github",
],
Expand Down
4 changes: 2 additions & 2 deletions s2/artifacts.bzl → s2/assets.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ https://github.com/klauspost/compress/releases/tag/v1.17.4

load("@bzlparty_tools//lib:platforms.bzl", _PLATFORMS = "PLATFORMS")

ARTIFACTS = {
ASSETS = {
"linux_amd64": ("s2-linux_amd64.tar.gz", "sha384-qjydEFa7DGnOkMlXH2xvfk9fnJwd/0nX9lVgg6RlakGFA1Of0y7VetWF0UkYxiZy"),
"linux_arm64": ("s2-linux_arm64.tar.gz", "sha384-swUKxhNuqswI0K5uca4t8abQo9qUscuKs7PnDcE/kCppCrzxxNbu0LGF8f/+Isq+"),
"freebsd_amd64": ("s2-freebsd_amd64.tar.gz", "sha384-PykoEFxXdaxpmSvIcYylfKyJuYJLSqaZAzLFV1i00mqUaNum2O+XoTir+FQru0C7"),
Expand All @@ -18,7 +18,7 @@ ARTIFACTS = {

PLATFORMS = {
p: _PLATFORMS[p]
for p in ARTIFACTS.keys()
for p in ASSETS.keys()
}

VERSION = "1.17.4"
28 changes: 11 additions & 17 deletions s2/repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"Repositories"

load("@bzlparty_tools//lib:github.bzl", "github")
load(":artifacts.bzl", "ARTIFACTS", "PLATFORMS", "VERSION")
load(":utils.bzl", "s2_toolchain_build_file", "s2_toolchains_build_file_snippet")
load("//utils:toolchain.bzl", "toolchain_build_file")
load(":assets.bzl", "ASSETS", "PLATFORMS", "VERSION")

def s2_platform_toolchains(name, platforms = PLATFORMS.keys(), version = VERSION):
"""Macro to prepare all toolchains
Expand All @@ -12,34 +12,26 @@ def s2_platform_toolchains(name, platforms = PLATFORMS.keys(), version = VERSION
platforms: platforms to use
version: s2 version
"""
toolchains_build_file = ""
for platform in platforms:
s2_toolchain_repo(
name = "%s-%s" % (name, platform),
platform = platform,
version = version,
)
toolchains_build_file += s2_toolchains_build_file_snippet(
name = "s2c",
prefix = name,
platform = platform,
exec_compatible_with = PLATFORMS[platform],
)

s2_toolchains_repo(
name = "%s_toolchains" % name,
build_file = toolchains_build_file,
)
s2_toolchains_repo(name = "%s_toolchains" % name)

def _s2_toolchain_repo_impl(ctx):
platform = ctx.attr.platform
version = "v%s" % ctx.attr.version
(asset, integrity) = ARTIFACTS[platform]
(asset, integrity) = ASSETS[platform]
gh = github(ctx, orga = "klauspost", project = "compress")

gh.download_archive(version, asset, integrity = integrity)

s2_toolchain_build_file(ctx, "s2c", ctx.attr._build_file)
toolchain_build_file(ctx, substitutions = {
"EXECUTABLE": "s2c",
})

s2_toolchain_repo = repository_rule(
_s2_toolchain_repo_impl,
Expand All @@ -51,11 +43,13 @@ s2_toolchain_repo = repository_rule(
)

def _s2_toolchains_repo(ctx):
ctx.file("BUILD.bazel", ctx.attr.build_file)
toolchain_build_file(ctx, substitutions = {
"EXECUTABLE": "s2c",
})

s2_toolchains_repo = repository_rule(
_s2_toolchains_repo,
attrs = {
"build_file": attr.string(mandatory = True),
"_build_file": attr.label(default = "//s2:toolchains.BUILD.bazel"),
},
)
6 changes: 3 additions & 3 deletions s2/toolchain.BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

load("@bzlparty_rules_compress//s2:toolchain.bzl", "s2_toolchain")

exports_files(["{executable}"], visibility = ["//visibility:public"])
exports_files(["EXECUTABLE"], visibility = ["//visibility:public"])

s2_toolchain(
name = "{name}_toolchain",
executable = "{executable}",
name = "s2c_toolchain",
executable = "EXECUTABLE",
visibility = ["//visibility:public"],
)
20 changes: 20 additions & 0 deletions s2/toolchains.BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"BUILD file template for a combined toolchain that hosts all platform toolchains"

load("@bzlparty_rules_compress//s2:assets.bzl", "PLATFORMS")
load("@bzlparty_tools//platforms:host.bzl", "HOST_PLATFORM")

[
toolchain(
name = "s2c-%s_toolchain" % platform,
toolchain = "@s2-%s//:s2c_toolchain" % platform,
exec_compatible_with = ecw,
toolchain_type = "@bzlparty_rules_compress//s2:s2c_toolchain_type",
visibility = ["//visibility:public"],
)
for (platform, ecw) in PLATFORMS.items()
]

alias(
name = "s2c",
actual = "@s2-%s//:EXECUTABLE" % HOST_PLATFORM,
)
Empty file added utils/BUILD.bazel
Empty file.
6 changes: 6 additions & 0 deletions utils/toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"Toolchain Utils"

def toolchain_build_file(ctx, build_file = None, **kwargs):
if not build_file and ctx.attr._build_file:
build_file = ctx.attr._build_file
ctx.template("BUILD.bazel", build_file, executable = False, **kwargs)
8 changes: 4 additions & 4 deletions zip/repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"Repositories"

load("@bzlparty_tools//lib:github.bzl", "github")
load("//utils:toolchain.bzl", "toolchain_build_file")
load(":assets.bzl", "ASSETS", "PLATFORMS", "VERSION")
load(":utils.bzl", "executable_target", "zip_toolchain_build_file")
load(":utils.bzl", "executable_target")

def zip_platform_toolchains(name, platforms = PLATFORMS.keys(), version = VERSION):
"""Macro to prepare all toolchains
Expand Down Expand Up @@ -40,7 +41,7 @@ def _zip_toolchain_repo_impl(ctx):
else:
gh.download_archive(version, asset, integrity = integrity)

zip_toolchain_build_file(ctx, ctx.attr._build_file, substitutions = {
toolchain_build_file(ctx, substitutions = {
"EXECUTABLE": executable,
})

Expand All @@ -64,9 +65,8 @@ zip_toolchain_repo = repository_rule(

def _zip_toolchains_repo(ctx):
platform = "%s_%s" % (ctx.os.name, ctx.os.arch)
zip_toolchain_build_file(
toolchain_build_file(
ctx,
ctx.attr._build_file,
substitutions = {
"EXECUTABLE": executable_target(ctx.attr.executable, platform),
"HOST_PLATFORM": platform,
Expand Down

0 comments on commit 9072cb1

Please sign in to comment.