Skip to content

Commit

Permalink
feat: implement support for output groups
Browse files Browse the repository at this point in the history
- add common enum for output group names
- add named output group for shared library headers
- use default output group for binary or shared library

Signed-off-by: Sam Gammon <[email protected]>
  • Loading branch information
sgammon committed Jan 14, 2024
1 parent 19e679d commit bb3ae0a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
3 changes: 3 additions & 0 deletions graalvm/artifacts/maven.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ _MavenTools = struct(
artifact = _graalvm_maven_artifact,
)

# buildifier: disable=name-conventions
MavenTools = _MavenTools

# Exports.

# buildifier: disable=name-conventions
Expand Down
15 changes: 15 additions & 0 deletions graalvm/nativeimage/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ load(
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
"use_cpp_toolchain",
)
load(
"//graalvm/artifacts:maven.bzl",
_MavenArtifacts = "MavenArtifacts",
_graalvm_maven_artifact = "graalvm_maven_artifact",
)
load(
"//internal/native_image:rules.bzl",
_DEBUG = "DEBUG_CONDITION",
_GVM_TOOLCHAIN_TYPE = "GVM_TOOLCHAIN_TYPE",
_NATIVE_IMAGE_ATTRS = "NATIVE_IMAGE_ATTRS",
_NATIVE_IMAGE_SHARED_LIB_ATTRS = "NATIVE_IMAGE_SHARED_LIB_ATTRS",
_OUTPUT_GROUPS = "OUTPUT_GROUPS",
_OPTIMIZATION_MODE = "OPTIMIZATION_MODE_CONDITION",
_graal_binary_implementation = "graal_binary_implementation",
_graal_shared_binary_implementation = "graal_shared_binary_implementation",
Expand Down Expand Up @@ -81,6 +87,12 @@ _native_image_shared_library = rule(
],
)

_NATIVE_IMAGE_UTILS = struct(
output_groups = _OUTPUT_GROUPS,
catalog = _MavenArtifacts,
artifact = _graalvm_maven_artifact,
)

# Exports.
def native_image(
name,
Expand Down Expand Up @@ -264,3 +276,6 @@ def native_image_shared_library(
default_outputs = default_outputs,
**kwargs
)

# Struct alias.
graalvm = _NATIVE_IMAGE_UTILS
10 changes: 10 additions & 0 deletions internal/native_image/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ _OPTIMIZATION_MODE_CONDITION = select({
"//conditions:default": _NativeImageOptimization.DEFAULT, # becomes `-O2` via GraalVM defaults
})

_OUTPUT_GROUPS = struct(
DEFAULT = "default",
SYMBOLS = "symbols",
HEADERS = "headers",
OBJECTS = "objects",
PROFILES = "profiles",
RESOURCES = "resources",
)

_NATIVE_IMAGE_BASE_ATTRS = {
"deps": attr.label_list(
providers = [[JavaInfo]],
Expand Down Expand Up @@ -246,6 +255,7 @@ def _prepare_native_image_rule_context(
NativeImageOptimization = _NativeImageOptimization

RULES_REPO = _RULES_REPO
OUTPUT_GROUPS = _OUTPUT_GROUPS
DEFAULT_GVM_REPO = _DEFAULT_GVM_REPO
DEBUG_CONDITION = _DEBUG_CONDITION
COVERAGE_CONDITION = _COVERAGE_CONDITION
Expand Down
38 changes: 28 additions & 10 deletions internal/native_image/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ load(
_NATIVE_IMAGE_ATTRS = "NATIVE_IMAGE_ATTRS",
_NATIVE_IMAGE_SHARED_LIB_ATTRS = "NATIVE_IMAGE_SHARED_LIB_ATTRS",
_OPTIMIZATION_MODE_CONDITION = "OPTIMIZATION_MODE_CONDITION",
_OUTPUT_GROUPS = "OUTPUT_GROUPS",
_RULES_REPO = "RULES_REPO",
_prepare_native_image_rule_context = "prepare_native_image_rule_context",
)
Expand Down Expand Up @@ -54,6 +55,7 @@ def _prepare_execution_env(ctx, base_env = {}):
return effective

def _graal_binary_implementation(ctx):
output_groups = {}
graal_attr = ctx.attr.native_image_tool
extra_tool_deps = []
gvm_toolchain = None
Expand Down Expand Up @@ -188,7 +190,7 @@ def _graal_binary_implementation(ctx):
)

# if we are building a shared library, prepare `CcSharedLibraryInfo` for it
cc_info = []
output_infos = []
runfiles = None
if ctx.attr.shared_library:
cc_toolchain = find_cpp_toolchain(ctx)
Expand All @@ -206,10 +208,10 @@ def _graal_binary_implementation(ctx):
dynamic_library = binary,
),
]
cc_info.extend([
OutputGroupInfo(
main_shared_library_output = depset([binary]),
),

## extend with additional cc info for shared library use
output_groups["main_shared_library_output"] = depset([binary])
output_infos.extend([
CcSharedLibraryInfo(
linker_input = cc_common.create_linker_input(
owner = ctx.label,
Expand All @@ -218,6 +220,16 @@ def _graal_binary_implementation(ctx):
),
])

# add output group for shared headers
# output_groups[_OUTPUT_GROUPS.HEADERS] = depset([binary])

# in shared library mode, the shared library is the default output
output_groups[_OUTPUT_GROUPS.DEFAULT] = depset([binary])

else:
# if it's not a shared library, our default output is the binary produced
output_groups[_OUTPUT_GROUPS.DEFAULT] = depset([binary])

# prepare all runfiles
all_runfiles = ctx.runfiles(
collect_data = True,
Expand All @@ -227,11 +239,16 @@ def _graal_binary_implementation(ctx):
if runfiles != None:
all_runfiles = all_runfiles.merge(runfiles)

return cc_info + [DefaultInfo(
executable = binary,
files = depset(all_outputs),
runfiles = all_runfiles,
)]
return output_infos + [
DefaultInfo(
executable = binary,
files = depset(all_outputs),
runfiles = all_runfiles,
),
OutputGroupInfo(
**output_groups
)
]

def _wrap_actions_for_graal(actions):
"""Wraps the given ctx.actions struct so that env variables are correctly passed to Graal."""
Expand Down Expand Up @@ -265,6 +282,7 @@ BAZEL_CURRENT_CPP_TOOLCHAIN = _BAZEL_CURRENT_CPP_TOOLCHAIN
NATIVE_IMAGE_ATTRS = _NATIVE_IMAGE_ATTRS
NATIVE_IMAGE_SHARED_LIB_ATTRS = _NATIVE_IMAGE_SHARED_LIB_ATTRS
GVM_TOOLCHAIN_TYPE = _GVM_TOOLCHAIN_TYPE
OUTPUT_GROUPS = _OUTPUT_GROUPS
DEBUG_CONDITION = _DEBUG_CONDITION
OPTIMIZATION_MODE_CONDITION = _OPTIMIZATION_MODE_CONDITION
graal_binary_implementation = _graal_binary_implementation
Expand Down

0 comments on commit bb3ae0a

Please sign in to comment.