-
Notifications
You must be signed in to change notification settings - Fork 454
/
Copy pathgraal.bzl
80 lines (73 loc) · 2.76 KB
/
graal.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
def _graal_native_binary_impl(ctx):
cc_toolchain_info = ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"].cc
if len(ctx.attr.java_binary[JavaInfo].java_outputs) == 0:
fail("no jars in java_binary rule output")
if len(ctx.attr.java_binary[JavaInfo].java_outputs) > 1:
fail("more than one output jar in java_binary rule output")
jar = ctx.attr.java_binary[JavaInfo].java_outputs[0].class_jar
graal_runtime = ctx.attr.graal_runtime[java_common.JavaRuntimeInfo]
args = [
"-cp",
jar.path,
"-o",
ctx.outputs.output_name.path,
"--native-compiler-path=" + cc_toolchain_info.compiler_executable,
# Add /usr/bin as prefix, so that `native-image` can find ld.
# The real solution would be to get `native-image` to work with the combination of lld and gcc.
# However, that has proved difficult so far.
"--native-compiler-options=-B/usr/bin",
"--silent",
] + ctx.attr.extra_args
ctx.actions.run(
outputs = [ctx.outputs.output_name],
inputs = depset(
[jar],
transitive = [
cc_toolchain_info.all_files,
],
),
executable = graal_runtime.java_home + "/bin/native-image",
arguments = args,
tools = [graal_runtime.files],
)
return [
DefaultInfo(
files = depset([ctx.outputs.output_name]),
executable = ctx.outputs.output_name,
),
]
# Caution to user, this rule was designed for a very specific single use case of building a native-image binary from a jar file.
# It is not intended for generalized native-image use.
graal_native_binary = rule(
implementation = _graal_native_binary_impl,
attrs = {
"extra_args": attr.string_list(),
"graal_runtime": attr.label(
providers = [java_common.JavaRuntimeInfo],
allow_files = True,
),
"java_binary": attr.label(
providers = [JavaInfo],
),
"output_name": attr.output(mandatory = True),
},
toolchains = [
"@bazel_tools//tools/cpp:toolchain_type",
],
executable = True,
)