diff --git a/internal/native_image/builder.bzl b/internal/native_image/builder.bzl index 098e939..257b6e3 100644 --- a/internal/native_image/builder.bzl +++ b/internal/native_image/builder.bzl @@ -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, @@ -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: diff --git a/internal/native_image/classic.bzl b/internal/native_image/classic.bzl index 6a42a47..bb0d936 100644 --- a/internal/native_image/classic.bzl +++ b/internal/native_image/classic.bzl @@ -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", @@ -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", @@ -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: diff --git a/internal/native_image/common.bzl b/internal/native_image/common.bzl index dc6e3f7..8eaa785 100644 --- a/internal/native_image/common.bzl +++ b/internal/native_image/common.bzl @@ -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 @@ -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), ), @@ -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. @@ -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 diff --git a/internal/native_image/rules.bzl b/internal/native_image/rules.bzl index fb3c772..4321e35 100644 --- a/internal/native_image/rules.bzl +++ b/internal/native_image/rules.bzl @@ -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" @@ -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: @@ -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", @@ -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,