Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download GraalVM distributions lazily #66

Closed
fmeum opened this issue Aug 29, 2023 · 3 comments · Fixed by #82 or #80
Closed

Download GraalVM distributions lazily #66

fmeum opened this issue Aug 29, 2023 · 3 comments · Fixed by #82 or #80
Labels
bug Something isn't working
Milestone

Comments

@fmeum
Copy link
Collaborator

fmeum commented Aug 29, 2023

Bazel only fetches repositories when they are actually needed during the build. By moving the toolchain definitions into a separate repo from the actual files comprising the toolchains, this can be used to prevent fetches of large toolchains (such as GraalVM distributions) when not used for a particular build (because the toolchain is never selected).

It would be great if rules_graalvm could support this, potentially by splitting

# bazel 6+ has support for the `version` attribute on the `java_runtime` rule. earlier
# versions do not, so we omit it.
toolchain_template = _JDK_BUILD_TEMPLATE
if not versions.is_at_least("6", versions.get()):
toolchain_template = _JDK_BUILD_TEMPLATE_BAZEL5
toolchain_aliases_template = """
# Entry Aliases
alias(
name = "entry",
actual = ":{bin_java_path}",
)
alias(
name = "graalvm",
actual = ":entry",
)
alias(
name = "{name}",
actual = ":entry",
)
# Toolchains
alias(
name = "toolchain",
actual = "@{repo}//:toolchain",
visibility = ["//visibility:public"],
)
{bootstrap_toolchain_alias}
load(
"@rules_graalvm//internal:toolchain.bzl",
"graalvm_sdk",
"graalvm_engine",
)
graalvm_sdk(
name = "gvm",
native_image_bin = ":native-image",
gvm_files = ":files",
)
alias(
name = "sdk",
actual = "toolchain_gvm",
)
toolchain(
name = "toolchain_gvm",
exec_compatible_with = [
{gvm_toolchain_tags_exec}
],
target_compatible_with = [
{gvm_toolchain_tags_target}
],
toolchain = ":gvm",
toolchain_type = "@rules_graalvm//graalvm/toolchain",
)
# Tool Aliases
{rendered_bin_aliases}
""".format(
name = ctx_alias,
repo = ctx.attr.toolchain_config,
bootstrap_toolchain_alias = bootstrap_toolchain_alias,
rendered_bin_aliases = rendered_bin_aliases,
bin_java_path = rendered_bin_paths.java,
gvm_toolchain_tags_exec = "",
gvm_toolchain_tags_target = "",
)
ctx.file(
"BUILD.bazel",
"""
exports_files(glob(["**/*"]))
filegroup(
name = "files",
srcs = glob(["**/*"]),
)
# Tool Targets
{rendered_bin_targets}
# Toolchain
{toolchain}
# Aliases
{aliases}
""".format(
toolchain = toolchain_template.format(RUNTIME_VERSION = java_version),
aliases = ctx.attr.enable_toolchain and toolchain_aliases_template or "",
rendered_bin_targets = rendered_bin_targets,
),
)

into two separate repository rules that are instantiated by a macro.

Context: I am trying to add rules_graalvm to Bazel's remote java tools, but currently this would require anyone who builds Bazel (or really any target in the repo) to also download GraalVM.

@sgammon
Copy link
Owner

sgammon commented Aug 29, 2023

@fmeum With regard to Java toolchains, I believe two repos are already configured -- the <name>_toolchain_config_repo repo, and the main <name> repo configured by the user.

repo structure looks like:

flowchart TD
    A(graalvm) -->|Alias| B(graalvm_toolchain_config_repo)
    B-->C[Java toolchain]
    A-->|toolchain target|D[GVM toolchain]

Loading

graalvm_toolchain_config_repo looks like:

@graalvm_toolchain_config_repo//:BUILD.bazel (null)
@graalvm_toolchain_config_repo//:bootstrap_runtime_toolchain (9dc7412)
@graalvm_toolchain_config_repo//:gvm (9dc7412)
@graalvm_toolchain_config_repo//:prefix_version_setting (9dc7412)
@graalvm_toolchain_config_repo//:toolchain (9dc7412)
@graalvm_toolchain_config_repo//:version_or_prefix_version_setting (9af774c)
@graalvm_toolchain_config_repo//:version_or_prefix_version_setting (9dc7412)
@graalvm_toolchain_config_repo//:version_setting (9dc7412)

graalvm looks like:

# ...
@graalvm//:sdk (9af774c) → @graalvm//:toolchain_gvm
@graalvm//:toolchain (9af774c) → @graalvm_toolchain_config_repo//:toolchain
@graalvm//:toolchain_gvm (9dc7412) → (see below)
# ...
toolchain(
  name = "toolchain_gvm",
  toolchain_type = "//graalvm/toolchain:toolchain",
  exec_compatible_with = [],
  target_compatible_with = [],
  toolchain = "@_main~graalvm~graalvm//:gvm",
)
graalvm_sdk(
  name = "gvm",
  native_image_bin = "@_main~graalvm~graalvm//:native-image",
  gvm_files = "@_main~graalvm~graalvm//:files",
)

So, is the above correct with regard to the Java toolchain, or are you referring to the GVM toolchain used to resolve native-image? And if the latter, would moving it to match the Java toolchain structure shown fix the issue? Thanks for your advice here, it is very helpful.

@sgammon sgammon added the bug Something isn't working label Aug 29, 2023
@sgammon sgammon added this to the 1.0.0 milestone Aug 29, 2023
@fmeum
Copy link
Collaborator Author

fmeum commented Aug 30, 2023

Yes, I was specifically referring to the toolchain for native_image, which I believe is in a single repo and would benefit from this optimization.

@sgammon
Copy link
Owner

sgammon commented Aug 31, 2023

@fmeum understood, sorry, I was only clarifying because I was wondering if this pattern I saw with Java Toolchains was one and the same with what you're talking about. I should be able to have this change in quickly.

sgammon added a commit that referenced this issue Sep 1, 2023
- fix: add `gvm` toolchain to toolchain config repo
- fix: add aliases from `graalvm` → toolchain targets
- fix: adjust instructions for registering toolchains
- fix: adjust workspace toolchain registration logic

Breaking change:
When registering toolchains in a Bzlmod installation of these rules,
the target `@graalvm//:all` must be changed to two toolchain
registrations, based on the desired functionality:

  Register the Java toolchain:
  register_toolchains("@graalvm//:jvm")

  Register the GVM toolchain:
  register_toolchains("@graalvm//:sdk")

Fixes and closes #66.

Relates-To: #66
Signed-off-by: Sam Gammon <[email protected]>
This was linked to pull requests Sep 1, 2023
sgammon added a commit that referenced this issue Sep 1, 2023
- fix: add `gvm` toolchain to toolchain config repo
- fix: add aliases from `graalvm` → toolchain targets
- fix: adjust instructions for registering toolchains
- fix: adjust workspace toolchain registration logic

Breaking change:
When registering toolchains in a Bzlmod installation of these rules,
the target `@graalvm//:all` must be changed to two toolchain
registrations, based on the desired functionality:

  Register the Java toolchain:
  register_toolchains("@graalvm//:jvm")

  Register the GVM toolchain:
  register_toolchains("@graalvm//:sdk")

Fixes and closes #66.

Relates-To: #66
Signed-off-by: Sam Gammon <[email protected]>
@sgammon sgammon mentioned this issue Sep 1, 2023
sgammon added a commit that referenced this issue Sep 2, 2023
- fix: add `gvm` toolchain to toolchain config repo
- fix: add aliases from `graalvm` → toolchain targets
- fix: adjust instructions for registering toolchains
- fix: adjust workspace toolchain registration logic

Breaking change:
When registering toolchains in a Bzlmod installation of these rules,
the target `@graalvm//:all` must be changed to two toolchain
registrations, based on the desired functionality:

  Register the Java toolchain:
  register_toolchains("@graalvm//:jvm")

  Register the GVM toolchain:
  register_toolchains("@graalvm//:sdk")

Fixes and closes #66.

Relates-To: #66
Signed-off-by: Sam Gammon <[email protected]>
sgammon added a commit that referenced this issue Sep 4, 2023
- fix: add `gvm` toolchain to toolchain config repo
- fix: add aliases from `graalvm` → toolchain targets
- fix: adjust instructions for registering toolchains
- fix: adjust workspace toolchain registration logic

Breaking change:
When registering toolchains in a Bzlmod installation of these rules,
the target `@graalvm//:all` must be changed to two toolchain
registrations, based on the desired functionality:

  Register the Java toolchain:
  register_toolchains("@graalvm//:jvm")

  Register the GVM toolchain:
  register_toolchains("@graalvm//:sdk")

Fixes and closes #66.

Relates-To: #66
Signed-off-by: Sam Gammon <[email protected]>
sgammon added a commit that referenced this issue Sep 8, 2023
- fix: add `gvm` toolchain to toolchain config repo
- fix: add aliases from `graalvm` → toolchain targets
- fix: adjust instructions for registering toolchains
- fix: adjust workspace toolchain registration logic

Breaking change:
When registering toolchains in a Bzlmod installation of these rules,
the target `@graalvm//:all` must be changed to two toolchain
registrations, based on the desired functionality:

  Register the Java toolchain:
  register_toolchains("@graalvm//:jvm")

  Register the GVM toolchain:
  register_toolchains("@graalvm//:sdk")

Fixes and closes #66.

Relates-To: #66
Signed-off-by: Sam Gammon <[email protected]>
sgammon added a commit that referenced this issue Sep 8, 2023
- fix: add `gvm` toolchain to toolchain config repo
- fix: add aliases from `graalvm` → toolchain targets
- fix: adjust instructions for registering toolchains
- fix: adjust workspace toolchain registration logic

Breaking change:
When registering toolchains in a Bzlmod installation of these rules,
the target `@graalvm//:all` must be changed to two toolchain
registrations, based on the desired functionality:

  Register the Java toolchain:
  register_toolchains("@graalvm//:jvm")

  Register the GVM toolchain:
  register_toolchains("@graalvm//:sdk")

Fixes and closes #66.

Relates-To: #66
Signed-off-by: Sam Gammon <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
2 participants