Skip to content
Open
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
2,783 changes: 2,783 additions & 0 deletions MODULE.bazel

Large diffs are not rendered by default.

120 changes: 120 additions & 0 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sqlc/private/BUILD.sqlc.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sqlc_release(
root_file = "ROOT",
sqlc = "sqlc{exe}",
version = "{version}",
visibility = ["//visibility:public"],
)

declare_toolchains(
Expand Down
8 changes: 4 additions & 4 deletions sqlc/private/actions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def sqlc_configure(ctx, params, queries, schemas, out, config_path_depth):
))

if versions.is_at_least("1.5.0", toolchain_version):
config = struct(
config = json.encode(struct(
version = "1",
overrides = overrides,
packages = [struct(
Expand All @@ -80,9 +80,9 @@ def sqlc_configure(ctx, params, queries, schemas, out, config_path_depth):
queries = ["{}/{}".format(back_to_root, p) for p in queries],
schema = ["{}/{}".format(back_to_root, p) for p in schemas],
)],
).to_json()
))
else:
config = struct(
config = json.encode(struct(
version = "1",
overrides = overrides,
packages = [struct(
Expand All @@ -97,7 +97,7 @@ def sqlc_configure(ctx, params, queries, schemas, out, config_path_depth):
queries = ["{}/{}".format(back_to_root, p) for p in queries],
schema = ["{}/{}".format(back_to_root, p) for p in schemas],
)],
).to_json()
))

ctx.actions.write(out, config)

Expand Down
88 changes: 88 additions & 0 deletions sqlc/private/extension.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
load("//sqlc/private:release.bzl", "sqlc_download_release_bzlmod")
load(
"//sqlc/private/rules_go/lib:platforms.bzl",
"generate_toolchain_names",
)

_toolchain_tag = tag_class(
attrs = {
"goarch": attr.string(),
"goos": attr.string(),
"version": attr.string(),
"urls": attr.string_list(default = ["https://github.com/kyleconroy/sqlc/releases/download/v{}/{}"]),
},
)

def _sqlc_toolchain_hub_impl(ctx):
"""Implementation for the hub repository that aliases all registered toolchains."""
repo_names = ctx.attr.repo_names
toolchain_names = generate_toolchain_names()

build_content = []
for name in repo_names:
build_content.append('load("@{name}//:toolchains.bzl", {name}_declare_toolchains = "bzlmod_declare_toolchains")'.format(name = name))

for name in repo_names:
build_content.append("{name}_declare_toolchains()".format(name = name))

ctx.file("BUILD.bazel", "\n".join(build_content))

_sqlc_toolchain_hub = repository_rule(
implementation = _sqlc_toolchain_hub_impl,
attrs = {
"repo_names": attr.string_list(mandatory = True),
},
)

def _toolchain_repo_name(toolchain_tag):
return "sqlc_release_{}_{}".format(
toolchain_tag.goos or "host",
toolchain_tag.goarch or "host",
)

def _make_root_module_last(modules):
roots = []
other = []
for mod in modules:
if mod.is_root:
roots.append(mod)
else:
other.append(mod)

return other + roots

def _toolchain_impl(mctx):
toolchain_tags = {}

# We want to process the tags such that, in the case of conflicts, the tag definitions
# from the root module "win".
for mod in _make_root_module_last(mctx.modules):
for toolchain_tag in mod.tags.toolchain:
repo_name = _toolchain_repo_name(toolchain_tag)
toolchain_tags[repo_name] = toolchain_tag

for release_name, toolchain_tag in toolchain_tags.items():
sqlc_download_release_bzlmod(
name = release_name,
goarch = toolchain_tag.goarch,
goos = toolchain_tag.goos,
version = toolchain_tag.version,
)

_sqlc_toolchain_hub(
name = "sqlc_toolchains",
repo_names = list(toolchain_tags.keys()),
)

return mctx.extension_metadata(
reproducible = True,
root_module_direct_deps = ["sqlc_toolchains"],
root_module_direct_dev_deps = [],
)

sqlc = module_extension(
implementation = _toolchain_impl,
tag_classes = {
"toolchain": _toolchain_tag,
},
)
27 changes: 22 additions & 5 deletions sqlc/private/release.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load(
"//sqlc/private/rules_go/lib:platforms.bzl",
"generate_toolchain_names",
)
load(
"@com_plezentek_rules_sqlc//sqlc/private/skylib/lib:versions.bzl",
"versions",
Expand All @@ -26,6 +22,10 @@ load(
"MIN_SUPPORTED_VERSION",
"SQLC_VERSIONS",
)
load(
"//sqlc/private/rules_go/lib:platforms.bzl",
"generate_toolchain_names",
)

##### Download SQLC binary #####
def _detect_host_platform(ctx):
Expand All @@ -47,7 +47,7 @@ def _detect_host_platform(ctx):
uname = arch_result.stdout.strip()
if uname in ("aarch64", "arm64"):
goarch = "arm64"
if uname in ("armv6l", "armv7l"):
elif uname in ("armv6l", "armv7l"):
goarch = "arm"
elif uname in ("amd64", "x86_64"):
goarch = "amd64"
Expand Down Expand Up @@ -124,6 +124,20 @@ def _sqlc_download_release_impl(ctx):
},
)

# For bzlmod the toolchain registration is hardcoded in the MODULE.bazel file. Since we can't dynamically add more
# toolchains, usually this is done by using a "hub" repo where we `register_toolchains("@hub//:all")` and then
# generate the "hub" repo in a separate repository rule.
# The simplest way would be to set up a bunch of aliases, however https://github.com/bazelbuild/bazel/issues/16298
# tells us that doesn't work, so we set up this intermediate macro for the "hub" to call.
ctx.file("toolchains.bzl", """
load("@com_plezentek_rules_sqlc//sqlc:def.bzl", "declare_toolchains")
def bzlmod_declare_toolchains():
declare_toolchains(
host ="{goos}_{goarch}",
release = Label("//:sqlc_release")
)
""".format(goos = goos, goarch = goarch))

# Get the binary tool
ctx.report_progress("downloading")
ctx.download_and_extract(
Expand All @@ -141,6 +155,9 @@ _sqlc_download_release = repository_rule(
},
)

def sqlc_download_release_bzlmod(name, **kwargs):
_sqlc_download_release(name = name, **kwargs)

def sqlc_download_release(name, **kwargs):
_sqlc_download_release(name = name, **kwargs)
_register_toolchains(name)
Expand Down
4 changes: 2 additions & 2 deletions sqlc/private/rules/package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ def _sqlc_package_impl(ctx):
)

# TODO(V2) Investigate direct compilation by embedding a go_library rule
return struct(providers = [
return [
DefaultInfo(
files = depset(outputs),
runfiles = ctx.runfiles(outputs), # For tests
),
])
]

sqlc_package = rule(
_sqlc_package_impl,
Expand Down
2 changes: 1 addition & 1 deletion sqlc/private/sqlc_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ sqlc_toolchain = rule(
def declare_toolchains(host, release):
host_goos, _, host_goarch = host.partition("_")
for p in PLATFORMS:
toolchain_name = "sqlc_" + p.name
toolchain_name = "sqlc_" + p.name + "_" + host
impl_name = toolchain_name + "-impl"

cgo_constraints = (
Expand Down
Loading