Skip to content

Commit

Permalink
feat: add jq_host and yq_host repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan authored and kormide committed Apr 22, 2022
1 parent 207d219 commit 048c32a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 16 deletions.
42 changes: 37 additions & 5 deletions lib/private/jq_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"Setup jq toolchain repositories and rules"

load(":repo_utils.bzl", "repo_utils")

# Platform names follow the os_arch_name() convention in lib/private/repo_utils.bzl
JQ_PLATFORMS = {
"darwin_amd64": struct(
Expand Down Expand Up @@ -89,7 +91,7 @@ def _jq_toolchains_repo_impl(repository_ctx):
# Expose a concrete toolchain which is the result of Bazel resolving the toolchain
# for the execution or target platform.
# Workaround for https://github.com/bazelbuild/bazel/issues/14009
starlark_content = """# Generated by lib/private/toolchain.bzl
starlark_content = """# Generated by @aspect_bazel_lib//lib/private:jq_toolchain.bzl
# Forward all the providers
def _resolved_toolchain_impl(ctx):
Expand All @@ -111,7 +113,7 @@ resolved_toolchain = rule(
"""
repository_ctx.file("defs.bzl", starlark_content)

build_content = """# Generated by lib/private/toolchain.bzl
build_content = """# Generated by @aspect_bazel_lib//lib/private:jq_toolchain.bzl
#
# These can be registered in the workspace file or passed to --extra_toolchains flag.
# By default all these toolchains are registered by the jq_register_toolchains macro
Expand All @@ -129,12 +131,11 @@ toolchain(
name = "{platform}_toolchain",
exec_compatible_with = {compatible_with},
target_compatible_with = {compatible_with},
toolchain = "@{name}_{platform}//:jq_toolchain",
toolchain = "@{user_repository_name}_{platform}//:jq_toolchain",
toolchain_type = "@aspect_bazel_lib//lib:jq_toolchain_type",
)
""".format(
platform = platform,
name = repository_ctx.attr.name,
user_repository_name = repository_ctx.attr.user_repository_name,
compatible_with = meta.compatible_with,
)
Expand Down Expand Up @@ -168,7 +169,7 @@ def _jq_platform_repo_impl(repository_ctx):
executable = True,
integrity = JQ_VERSIONS[repository_ctx.attr.version][release_platform],
)
build_content = """#Generated by lib/repositories.bzl
build_content = """# Generated by @aspect_bazel_lib//lib/private:jq_toolchain.bzl
load("@aspect_bazel_lib//lib/private:jq_toolchain.bzl", "jq_toolchain")
exports_files(["{0}"])
jq_toolchain(name = "jq_toolchain", bin = "{0}", visibility = ["//visibility:public"])
Expand All @@ -185,3 +186,34 @@ jq_platform_repo = repository_rule(
"platform": attr.string(mandatory = True, values = JQ_PLATFORMS.keys()),
},
)

def _jq_host_alias_repo(repository_ctx):
# Base BUILD file for this repository
repository_ctx.file("BUILD.bazel", """# Generated by @aspect_bazel_lib//lib/private:jq_toolchain.bzl
package(default_visibility = ["//visibility:public"])
alias(name = "jq", actual = "@{name}_{platform}//:jq")
exports_files(["index.bzl"])
""".format(
name = repository_ctx.attr.user_repository_name,
platform = repo_utils.os_arch_name(repository_ctx),
))

# index.bzl file for this repository
repository_ctx.file("index.bzl", content = """# Generated by @aspect_bazel_lib//lib/private:jq_toolchain.bzl
host_platform="{host_platform}"
""".format(host_platform = repo_utils.os_arch_name(repository_ctx)))

jq_host_alias_repo = repository_rule(
_jq_host_alias_repo,
doc = """Creates a repository with a shorter name meant for the host platform, which contains
- A BUILD.bazel file declaring aliases to the host platform's binaries
- index.bzl containing some constants
""",
attrs = {
"user_repository_name": attr.string(
doc = "User-provided name from the workspace file, eg. jq",
mandatory = True,
),
},
)
4 changes: 2 additions & 2 deletions lib/private/repo_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

def _is_darwin_os(rctx):
"""Returns true if the host operating system is Darwin"""
return rctx.os.name.lower().os_name.startswith("mac os")
return rctx.os.name.lower().startswith("mac os")

def _is_linux_os(rctx):
"""Returns true if the host operating system is Linux"""
Expand Down Expand Up @@ -91,7 +91,7 @@ def _os_arch_name(rctx):
}
if arch in arch_map.keys():
arch = arch_map[arch]
return "%s_%s" % arch
return "%s_%s" % (os_name, arch)

repo_utils = struct(
is_darwin_os = _is_darwin_os,
Expand Down
42 changes: 37 additions & 5 deletions lib/private/yq_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"Setup yq toolchain repositories and rules"

load(":repo_utils.bzl", "repo_utils")

# Platform names follow the os_arch_name() convention in lib/private/repo_utils.bzl
YQ_PLATFORMS = {
"darwin_amd64": struct(
Expand Down Expand Up @@ -122,7 +124,7 @@ def _yq_toolchains_repo_impl(repository_ctx):
# Expose a concrete toolchain which is the result of Bazel resolving the toolchain
# for the execution or target platform.
# Workaround for https://github.com/bazelbuild/bazel/issues/14009
starlark_content = """# Generated by @aspect_bazel_lib//lib/private/yq_toolchain.bzl
starlark_content = """# Generated by @aspect_bazel_lib//lib/private:yq_toolchain.bzl
# Forward all the providers
def _resolved_toolchain_impl(ctx):
Expand All @@ -144,7 +146,7 @@ resolved_toolchain = rule(
"""
repository_ctx.file("defs.bzl", starlark_content)

build_content = """# Generated by @aspect_bazel_lib//lib/private/yq_toolchain.bzl
build_content = """# Generated by @aspect_bazel_lib//lib/private:yq_toolchain.bzl
#
# These can be registered in the workspace file or passed to --extra_toolchains flag.
# By default all these toolchains are registered by the yq_register_toolchains macro
Expand All @@ -162,12 +164,11 @@ toolchain(
name = "{platform}_toolchain",
exec_compatible_with = {compatible_with},
target_compatible_with = {compatible_with},
toolchain = "@{name}_{platform}//:yq_toolchain",
toolchain = "@{user_repository_name}_{platform}//:yq_toolchain",
toolchain_type = "@aspect_bazel_lib//lib:yq_toolchain_type",
)
""".format(
platform = platform,
name = repository_ctx.attr.name,
user_repository_name = repository_ctx.attr.user_repository_name,
compatible_with = meta.compatible_with,
)
Expand Down Expand Up @@ -202,7 +203,7 @@ def _yq_platform_repo_impl(repository_ctx):
executable = True,
integrity = YQ_VERSIONS[repository_ctx.attr.version][release_platform],
)
build_content = """#Generated by @aspect_bazel_lib//lib/private/yq_toolchain.bzl
build_content = """# Generated by @aspect_bazel_lib//lib/private:yq_toolchain.bzl
load("@aspect_bazel_lib//lib/private:yq_toolchain.bzl", "yq_toolchain")
exports_files(["{0}"])
yq_toolchain(name = "yq_toolchain", bin = "{0}", visibility = ["//visibility:public"])
Expand All @@ -219,3 +220,34 @@ yq_platform_repo = repository_rule(
"platform": attr.string(mandatory = True, values = YQ_PLATFORMS.keys()),
},
)

def _yq_host_alias_repo(repository_ctx):
# Base BUILD file for this repository
repository_ctx.file("BUILD.bazel", """# Generated by @aspect_bazel_lib//lib/private:yq_toolchain.bzl
package(default_visibility = ["//visibility:public"])
alias(name = "yq", actual = "@{name}_{platform}//:yq")
exports_files(["index.bzl"])
""".format(
name = repository_ctx.attr.user_repository_name,
platform = repo_utils.os_arch_name(repository_ctx),
))

# index.bzl file for this repository
repository_ctx.file("index.bzl", content = """# Generated by lib/private/yq_toolchain.bzl
host_platform="{host_platform}"
""".format(host_platform = repo_utils.os_arch_name(repository_ctx)))

yq_host_alias_repo = repository_rule(
_yq_host_alias_repo,
doc = """Creates a repository with a shorter name meant for the host platform, which contains
- A BUILD.bazel file declaring aliases to the host platform's binaries
- index.bzl containing some constants
""",
attrs = {
"user_repository_name": attr.string(
doc = "User-provided name from the workspace file, eg. yq",
mandatory = True,
),
},
)
20 changes: 16 additions & 4 deletions lib/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("//lib/private:jq_toolchain.bzl", "JQ_PLATFORMS", "jq_platform_repo", "jq_toolchains_repo")
load("//lib/private:yq_toolchain.bzl", "YQ_PLATFORMS", "yq_platform_repo", "yq_toolchains_repo")
load("//lib/private:jq_toolchain.bzl", "JQ_PLATFORMS", "jq_host_alias_repo", "jq_platform_repo", "jq_toolchains_repo")
load("//lib/private:yq_toolchain.bzl", "YQ_PLATFORMS", "yq_host_alias_repo", "yq_platform_repo", "yq_toolchains_repo")

def aspect_bazel_lib_dependencies():
"Load dependencies required by aspect rules"
Expand All @@ -26,14 +26,20 @@ def register_jq_toolchains(version, name = "jq"):
"""
for [platform, meta] in JQ_PLATFORMS.items():
jq_platform_repo(
name = "%s_toolchains_%s" % (name, platform),
name = "%s_%s" % (name, platform),
platform = platform,
version = version,
)
native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform))

jq_host_alias_repo(
name = "%s_host" % name,
user_repository_name = name,
)

jq_toolchains_repo(
name = "%s_toolchains" % name,
user_repository_name = name,
)

def register_yq_toolchains(version, name = "yq"):
Expand All @@ -45,12 +51,18 @@ def register_yq_toolchains(version, name = "yq"):
"""
for [platform, meta] in YQ_PLATFORMS.items():
yq_platform_repo(
name = "%s_toolchains_%s" % (name, platform),
name = "%s_%s" % (name, platform),
platform = platform,
version = version,
)
native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform))

yq_host_alias_repo(
name = "%s_host" % name,
user_repository_name = name,
)

yq_toolchains_repo(
name = "%s_toolchains" % name,
user_repository_name = name,
)

0 comments on commit 048c32a

Please sign in to comment.