-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefs.bzl
196 lines (172 loc) · 5.75 KB
/
defs.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Copyright 2020 The Cross-Media Measurement 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.
"""Build definitions for SWIG Java."""
def _create_src_jar(ctx, java_runtime_info, input_dir, output_jar):
jar_args = ctx.actions.args()
jar_args.add("cf", output_jar)
jar_args.add_all([input_dir])
ctx.actions.run(
outputs = [output_jar],
inputs = [input_dir],
executable = "%s/bin/jar" % java_runtime_info.java_home,
tools = java_runtime_info.files,
arguments = [jar_args],
mnemonic = "SwigJar",
)
def _java_wrap_cc_impl(ctx):
name = ctx.attr.name
src = ctx.file.src
outfile = ctx.outputs.outfile
header_sets = [] # depsets of Files
include_path_sets = [] # depsets of strings
# Include headers from deps.
for target in ctx.attr.deps:
cc_context = target[CcInfo].compilation_context
header_sets.append(cc_context.headers)
include_path_sets.append(cc_context.includes)
# Include workspace root in include path for when target is defined in an
# external workspace.
if target.label.workspace_root:
include_path_sets.append(depset([target.label.workspace_root]))
java_files_dir = ctx.actions.declare_directory("java_files")
swig_args = ctx.actions.args()
swig_args.add_all([java_files_dir], expand_directories = False)
swig_args.add("swig")
swig_args.add("-c++")
swig_args.add("-java")
swig_args.add("-package", ctx.attr.package)
swig_args.add_all("-outdir", [java_files_dir], expand_directories = False)
swig_args.add("-o", outfile)
if ctx.attr.module:
swig_args.add("-module", ctx.attr.module)
for include_path in depset(transitive = include_path_sets).to_list():
swig_args.add("-I" + include_path)
for f in ctx.files.swig_includes:
swig_args.add("-l" + f.path)
swig_args.add(src.path)
file_inputs = [src] + ctx.files.swig_includes
ctx.actions.run(
outputs = [outfile, java_files_dir],
inputs = depset(file_inputs, transitive = header_sets),
executable = ctx.executable._mkdir_wrapper,
arguments = [swig_args],
mnemonic = "SwigCompile",
)
java_runtime = ctx.attr._jdk[java_common.JavaRuntimeInfo]
_create_src_jar(ctx, java_runtime, java_files_dir, ctx.outputs.srcjar)
_java_wrap_cc = rule(
doc = """
Wraps C++ in Java using Swig.
It's expected that the `swig` binary exists in the host's path.
""",
implementation = _java_wrap_cc_impl,
attrs = {
"src": attr.label(
doc = "Single swig source file.",
allow_single_file = True,
mandatory = True,
),
"deps": attr.label_list(
doc = "C++ dependencies.",
providers = [CcInfo],
),
"swig_includes": attr.label_list(
doc = "List of files that are included by the swig file",
allow_files = True,
),
"package": attr.string(
doc = "Package for generated Java.",
mandatory = True,
),
"module": attr.string(doc = "Optional Swig module name."),
"outfile": attr.output(
doc = "Generated C++ output file.",
mandatory = True,
),
"srcjar": attr.output(
doc = "Generated Java source jar.",
mandatory = True,
),
"_jdk": attr.label(
default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
providers = [java_common.JavaRuntimeInfo],
),
"_mkdir_wrapper": attr.label(
default = Label("//tools:mkdir_wrapper"),
executable = True,
cfg = "exec",
),
},
)
def java_wrap_cc(
name,
src,
package,
deps = [],
swig_includes = [],
module = None,
visibility = None,
**kwargs):
"""Wraps C++ in Java using Swig.
It's expected that the `swig` binary exists in the host's path.
Args:
name: target name.
src: single .swig source file.
package: package of generated Java files.
deps: C++ deps.
swig_includes: files used by the src
module: optional name of Swig module.
Generated targets:
{name}: java_library
lib{name}.so: cc_binary
"""
wrapper_name = "_" + name + "_wrapper"
cc_name = "_" + name + "_cc"
outfile = name + ".cc"
srcjar = name + ".srcjar"
so_name = "lib%s.so" % name
_java_wrap_cc(
name = wrapper_name,
src = src,
package = package,
outfile = outfile,
srcjar = srcjar,
deps = deps,
module = module,
visibility = ["//visibility:private"],
swig_includes = swig_includes,
**kwargs
)
native.cc_library(
name = cc_name,
srcs = [outfile],
deps = deps + ["@bazel_tools//tools/jdk:jni"],
alwayslink = True,
visibility = ["//visibility:private"],
**kwargs
)
native.cc_binary(
name = so_name,
deps = [cc_name],
linkshared = True,
visibility = visibility,
**kwargs
)
native.java_library(
name = name,
srcs = [srcjar],
runtime_deps = [so_name],
visibility = visibility,
**kwargs
)