Skip to content

Commit

Permalink
feat: extended native-image build options
Browse files Browse the repository at this point in the history
- feat: add experimental build setting which controls the `march`
  parameter

Not yet implemented:

Needs integration with `native_image_settings` and Bazel's
Platforms feature.

Signed-off-by: Sam Gammon <[email protected]>
  • Loading branch information
sgammon committed Sep 4, 2023
1 parent d08e249 commit 5bd8af2
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 0 deletions.
11 changes: 11 additions & 0 deletions graalvm/nativeimage/options.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"Defines targets and enumerations for various Native Image builder options."

load(
"//internal/native_image/options:arch.bzl",
_NativeImageArchitecture = "NativeImageArchitecture",
)

# Exports

# buildifier: disable=name-conventions
NativeImageArchitecture = _NativeImageArchitecture
14 changes: 14 additions & 0 deletions graalvm/nativeimage/options/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load(
"//internal/native_image/options:arch.bzl",
"arch",
"NativeImageArchitecture",
)

package(default_visibility = [
"//visibility:public",
])

arch(
name = "arch",
build_setting_default = NativeImageArchitecture.COMPATIBILITY,
)
16 changes: 16 additions & 0 deletions internal/native_image/builder.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
"Logic to assemble `native-image` options."

load(
"//internal/native_image/options:arch.bzl",
"NativeImageArchInfo",
)

def _configure_static_zlib_compile(ctx, args, direct_inputs):
"""Configure a static image compile against hermetic/static zlib.
Expand Down Expand Up @@ -115,6 +120,17 @@ def _configure_native_compiler(ctx, args, c_compiler_path, gvm_toolchain):
if gvm_toolchain != None:
args.add(c_compiler_path, format = "--native-compiler-path=%s")

# calculate the value to use for `march`, which should default to nothing
native_arch_advice = None
native_arch_info = ctx.attr.native_arch[NativeImageArchInfo]
if native_arch_info != None:
native_arch_advice = native_arch_info.target_arch
if native_arch_advice != None:
args.add(
native_arch_advice,
format = "-march=%s",
)

# add custom compiler options
args.add_all(
ctx.attr.c_compiler_option,
Expand Down
9 changes: 9 additions & 0 deletions internal/native_image/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ load(
"//internal/native_image:builder.bzl",
_assemble_native_build_options = "assemble_native_build_options",
)
load(
"//internal/native_image/options:arch.bzl",
"NativeImageArchInfo",
)

_RULES_REPO = "@rules_graalvm"
_DEFAULT_GVM_REPO = "@graalvm"
Expand All @@ -13,6 +17,7 @@ _BAZEL_CURRENT_CPP_TOOLCHAIN = "@bazel_tools//tools/cpp:current_cc_toolchain"
_LINUX_CONSTRAINT = "@platforms//os:linux"
_MACOS_CONSTRAINT = "@platforms//os:macos"
_WINDOWS_CONSTRAINT = "@platforms//os:windows"
_SETTING_TARGET_ARCH = "%s//graalvm/nativeimage/options:arch" % _RULES_REPO

# buildifier: disable=name-conventions
_NativeImageOptimization = struct(
Expand Down Expand Up @@ -109,6 +114,10 @@ _NATIVE_IMAGE_ATTRS = {
"executable_name": attr.string(
mandatory = True,
),
"native_arch": attr.label(
default = Label(_SETTING_TARGET_ARCH),
providers = [[NativeImageArchInfo]],
),
"_cc_toolchain": attr.label(
default = Label(_BAZEL_CURRENT_CPP_TOOLCHAIN),
),
Expand Down
10 changes: 10 additions & 0 deletions internal/native_image/options/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package(default_visibility = [
"//graalvm/nativeimage/options:__pkg__",
"//graalvm/nativeimage/options:__subpackages__",
"//internal:__pkg__",
"//internal:__subpackages__",
])

exports_files([
"arch.bzl",
])
46 changes: 46 additions & 0 deletions internal/native_image/options/arch.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"Defines provider info for various Native Image build options."

# buildifier: disable=name-conventions
_NativeImageArchitecture = struct(
COMPATIBILITY = "compatibility",
NATIVE = "native",
AMD_64 = "amd64",
ARM_64 = "amd64",
)

_NativeImageArchInfo = provider(
fields = ["target_arch"],
doc = "Provides target architecture selection support for GraalVM native image targets.",
)

supported_architectures = [
_NativeImageArchitecture.COMPATIBILITY,
_NativeImageArchitecture.NATIVE,
_NativeImageArchitecture.AMD_64,
_NativeImageArchitecture.ARM_64,
]

def _native_image_arch_impl(ctx):
target_arch = ctx.build_setting_value
if target_arch not in supported_architectures:
fail(str(ctx.label) + " build setting allowed to take values {"
+ ", ".join(supported_architectures) + "} but was set to unallowed value "
+ target_arch)

return _NativeImageArchInfo(
target_arch = target_arch,
)

# Exports.

# buildifier: disable=name-conventions
NativeImageArchitecture = _NativeImageArchitecture

NativeImageArchInfo = _NativeImageArchInfo

arch = rule(
implementation = _native_image_arch_impl,
build_setting = config.string(
flag = True,
)
)
9 changes: 9 additions & 0 deletions lib/nativeimage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ package(default_visibility = [
bzl_library(
name = "builder",
srcs = ["//internal/native_image:builder.bzl"],
deps = [
":options",
],
)

bzl_library(
Expand Down Expand Up @@ -42,6 +45,7 @@ bzl_library(
":internals",
":settings",
":toolchain",
":options",
"//lib:tooling",
"@bazel_skylib//lib:dicts",
],
Expand All @@ -67,3 +71,8 @@ bzl_library(
"@bazel_skylib//lib:dicts",
],
)

bzl_library(
name = "options",
srcs = ["//internal/native_image/options:arch.bzl"],
)

0 comments on commit 5bd8af2

Please sign in to comment.