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

Allow getting output shared libraries #535

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
11 changes: 6 additions & 5 deletions internal/native_image/builder.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ def _configure_native_test_flags(ctx, args):
def assemble_native_build_options(
ctx,
args,
binary,
binary_name,
outdir,
classpath_depset,
direct_inputs,
c_compiler_path,
Expand Down Expand Up @@ -204,13 +205,13 @@ def assemble_native_build_options(
if not ctx.attr.allow_fallback:
args.add("--no-fallback")

trimmed_basename = binary.basename
trimmed_binary_name = binary_name
if bin_postfix:
trimmed_basename = trimmed_basename[0:-(len(bin_postfix))]
trimmed_binary_name = trimmed_binary_name[0:-(len(bin_postfix))]

args.add(ctx.attr.main_class, format = "-H:Class=%s")
args.add(trimmed_basename, format = "-H:Name=%s")
args.add(binary.dirname, format = "-H:Path=%s")
args.add(trimmed_binary_name, format = "-H:Name=%s")
args.add(outdir, format = "-H:Path=%s")
args.add("-H:+ReportExceptionStackTraces")

if not ctx.attr.check_toolchains:
Expand Down
16 changes: 14 additions & 2 deletions internal/native_image/classic.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"Legacy ('classic') rules for building with GraalVM on Bazel."

load(
"//internal/native_image:builder.bzl",
_assemble_native_build_options = "assemble_native_build_options",
)
load(
"//internal/native_image:common.bzl",
_BAZEL_CPP_TOOLCHAIN_TYPE = "BAZEL_CPP_TOOLCHAIN_TYPE",
Expand All @@ -10,7 +14,7 @@ load(
_NATIVE_IMAGE_ATTRS = "NATIVE_IMAGE_ATTRS",
_OPTIMIZATION_MODE_CONDITION = "OPTIMIZATION_MODE_CONDITION",
_RULES_REPO = "RULES_REPO",
_prepare_native_image_rule_context = "prepare_native_image_rule_context",
_path_list_separator = "path_list_separator",
)
load(
"//internal/native_image:toolchain.bzl",
Expand Down Expand Up @@ -50,12 +54,20 @@ def _graal_binary_classic_implementation(ctx):
)

args = ctx.actions.args()
binary = _prepare_native_image_rule_context(

binary_name = ctx.attr.executable_name.replace("%target%", ctx.attr.name)
binary = ctx.actions.declare_file("output/" + binary_name)
path_list_separator = _path_list_separator(ctx)

_assemble_native_build_options(
ctx,
args,
binary_name,
binary.dirname,
classpath_depset,
direct_inputs,
native_toolchain.c_compiler_path,
path_list_separator,
)

if ctx.files.data:
Expand Down
41 changes: 7 additions & 34 deletions internal/native_image/common.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
"Defines common properties shared by modern and legacy Native Image rules."

load(
"//internal/native_image:builder.bzl",
_assemble_native_build_options = "assemble_native_build_options",
)

_RULES_REPO = "@rules_graalvm"
_DEFAULT_GVM_REPO = "@graalvm"
_GVM_TOOLCHAIN_TYPE = "%s//graalvm/toolchain" % _RULES_REPO
Expand Down Expand Up @@ -125,6 +120,9 @@ _NATIVE_IMAGE_ATTRS = {
mandatory = False,
allow_single_file = True,
),
"extra_output_files": attr.string_list(
mandatory = False,
),
"_cc_toolchain": attr.label(
default = Label(_BAZEL_CURRENT_CPP_TOOLCHAIN),
),
Expand Down Expand Up @@ -153,39 +151,14 @@ def _prepare_bin_name(
return "%s%s" % (name, bin_postfix)
return name

def _prepare_native_image_rule_context(
ctx,
args,
classpath_depset,
direct_inputs,
c_compiler_path,
gvm_toolchain = None,
bin_postfix = None):
"""Prepare a `native-image` build context."""

out_bin_name = ctx.attr.executable_name.replace("%target%", ctx.attr.name)
binary = ctx.actions.declare_file(_prepare_bin_name(out_bin_name, bin_postfix))

def _path_list_separator(ctx):
# TODO: This check really should be on the exec platform, not the target platform, but that
# requires going through a separate rule. Since GraalVM doesn't support cross-compilation, the
# distinction doesn't matter for now.
if ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]):
path_list_separator = ";"
return ";"
else:
path_list_separator = ":"

_assemble_native_build_options(
ctx,
args,
binary,
classpath_depset,
direct_inputs,
c_compiler_path,
path_list_separator,
gvm_toolchain,
bin_postfix,
)
return binary
return ":"

## Exports.

Expand All @@ -203,4 +176,4 @@ BAZEL_CURRENT_CPP_TOOLCHAIN = _BAZEL_CURRENT_CPP_TOOLCHAIN
MACOS_CONSTRAINT = _MACOS_CONSTRAINT
WINDOWS_CONSTRAINT = _WINDOWS_CONSTRAINT
NATIVE_IMAGE_ATTRS = _NATIVE_IMAGE_ATTRS
prepare_native_image_rule_context = _prepare_native_image_rule_context
path_list_separator = _path_list_separator
28 changes: 22 additions & 6 deletions internal/native_image/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ load(
_NATIVE_IMAGE_ATTRS = "NATIVE_IMAGE_ATTRS",
_OPTIMIZATION_MODE_CONDITION = "OPTIMIZATION_MODE_CONDITION",
_RULES_REPO = "RULES_REPO",
_prepare_native_image_rule_context = "prepare_native_image_rule_context",
_path_list_separator = "path_list_separator",
)
load(
"//internal/native_image:toolchain.bzl",
_resolve_cc_toolchain = "resolve_cc_toolchain",
)
load(
"//internal/native_image:builder.bzl",
_assemble_native_build_options = "assemble_native_build_options",
)

_BIN_POSTFIX_DYLIB = ".dylib"
_BIN_POSTFIX_EXE = ".exe"
Expand Down Expand Up @@ -92,15 +96,27 @@ def _graal_binary_implementation(ctx):
elif (not is_windows and not is_macos) and ctx.attr.shared_library:
bin_postfix = _BIN_POSTFIX_SO

args = ctx.actions.args().use_param_file("@%s", use_always = False)
binary = _prepare_native_image_rule_context(
args = ctx.actions.args().use_param_file("@%s", use_always=False)

binary_name = ctx.attr.executable_name.replace("%target%", ctx.attr.name)
binary = ctx.actions.declare_file("output/" + binary_name)
extra_output_files = []
for f in ctx.attr.extra_output_files:
extra_output_files.append(ctx.actions.declare_file("output/" + f))

path_list_separator = _path_list_separator(ctx)

_assemble_native_build_options(
ctx,
args,
binary_name,
binary.dirname,
classpath_depset,
direct_inputs,
native_toolchain.c_compiler_path,
path_list_separator,
gvm_toolchain,
bin_postfix = bin_postfix,
bin_postfix,
)

if ctx.files.data:
Expand All @@ -122,7 +138,7 @@ def _graal_binary_implementation(ctx):
transitive = transitive_inputs,
)
run_params = {
"outputs": [binary],
"outputs": [binary] + extra_output_files,
"executable": graal,
"inputs": inputs,
"mnemonic": "NativeImage",
Expand Down Expand Up @@ -168,7 +184,7 @@ def _graal_binary_implementation(ctx):

return [DefaultInfo(
executable = binary,
files = depset([binary]),
files = depset([binary] + extra_output_files),
runfiles = ctx.runfiles(
collect_data = True,
collect_default = True,
Expand Down