Skip to content

Commit 59d74d5

Browse files
ruifangChenraulchen
authored andcommitted
[Java] Build Java code with Bazel (#4284)
1 parent 4b8b703 commit 59d74d5

File tree

24 files changed

+751
-226
lines changed

24 files changed

+751
-226
lines changed

.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# build config
2+
build --compilation_mode=opt

.travis.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ matrix:
2525
env:
2626
- JDK='Oracle JDK 8'
2727
- PYTHON=3.5 PYTHONWARNINGS=ignore
28-
- RAY_USE_CMAKE=1
2928
- RAY_INSTALL_JAVA=1
3029
install:
3130
- eval `python $TRAVIS_BUILD_DIR/ci/travis/determine_tests_to_run.py`
3231
- if [ $RAY_CI_JAVA_AFFECTED != "1" ]; then exit; fi
33-
34-
- ./ci/travis/install-dependencies.sh
32+
- ./ci/suppress_output ./ci/travis/install-bazel.sh
33+
- ./ci/suppress_output ./ci/travis/install-dependencies.sh
3534
- export PATH="$HOME/miniconda/bin:$PATH"
36-
- ./ci/travis/install-ray.sh
35+
- ./ci/suppress_output ./ci/travis/install-ray.sh
3736
script:
3837
- if [ $RAY_CI_JAVA_AFFECTED != "1" ]; then exit; fi
39-
4038
- ./java/test.sh
4139

4240
- os: linux
@@ -167,7 +165,7 @@ install:
167165
- ./ci/suppress_output bash src/ray/test/run_gcs_tests.sh
168166
# Raylet tests.
169167
- ./ci/suppress_output bash src/ray/test/run_object_manager_tests.sh
170-
- ./ci/suppress_output bazel test --build_tests_only --test_lang_filters=cc ... -c opt
168+
- ./ci/suppress_output bazel test --build_tests_only --test_lang_filters=cc //:all
171169

172170
script:
173171
- export PATH="$HOME/miniconda/bin:$PATH"

BUILD.bazel

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ cc_binary(
1111
name = "raylet",
1212
srcs = ["src/ray/raylet/main.cc"],
1313
copts = COPTS,
14+
visibility = ["//java:__subpackages__"],
1415
deps = [
1516
":ray_util",
1617
":raylet_lib",
@@ -100,6 +101,7 @@ cc_test(
100101
copts = COPTS,
101102
deps = [
102103
":ray_util",
104+
"@boost//:asio",
103105
"@com_google_googletest//:gtest_main",
104106
],
105107
)
@@ -372,7 +374,7 @@ pyx_library(
372374
)
373375

374376
cc_binary(
375-
name = "raylet_library_java.so",
377+
name = "libraylet_library_java.so",
376378
srcs = [
377379
"src/ray/raylet/lib/java/org_ray_runtime_raylet_RayletClientImpl.h",
378380
"src/ray/raylet/lib/java/org_ray_runtime_raylet_RayletClientImpl.cc",
@@ -394,7 +396,7 @@ cc_binary(
394396
"//conditions:default": ["external/bazel_tools/tools/jdk/include/linux"],
395397
}),
396398
linkshared = 1,
397-
linkstatic = 0,
399+
linkstatic = 1,
398400
deps = [
399401
"//:raylet_lib",
400402
"@plasma//:plasma_client",
@@ -403,25 +405,31 @@ cc_binary(
403405

404406
genrule(
405407
name = "raylet-jni-darwin-compat",
406-
srcs = [":raylet_library_java.so"],
407-
outs = ["raylet_library_java.dylib"],
408+
srcs = [":libraylet_library_java.so"],
409+
outs = ["libraylet_library_java.dylib"],
408410
cmd = "cp $< $@",
409411
output_to_bindir = 1,
410412
)
411413

412414
filegroup(
413415
name = "raylet_library_java",
414416
srcs = select({
415-
"@bazel_tools//src/conditions:darwin": [":raylet_library_java.dylib"],
416-
"//conditions:default": [":raylet_library_java.so"],
417+
"@bazel_tools//src/conditions:darwin": [":libraylet_library_java.dylib"],
418+
"//conditions:default": [":libraylet_library_java.so"],
417419
}),
418-
visibility = ["//visibility:public"],
420+
visibility = ["//java:__subpackages__"],
421+
)
422+
423+
filegroup(
424+
name = "gcs_fbs_file",
425+
srcs = ["src/ray/gcs/format/gcs.fbs"],
426+
visibility = ["//java:__subpackages__"],
419427
)
420428

421429
flatbuffer_py_library(
422430
name = "python_gcs_fbs",
423431
srcs = [
424-
"src/ray/gcs/format/gcs.fbs",
432+
":gcs_fbs_file",
425433
],
426434
outs = [
427435
"ActorCheckpointIdData.py",
@@ -526,10 +534,11 @@ genrule(
526534
mv ./src/redis-cli $(location redis-cli) &&
527535
chmod +x $(location redis-cli)
528536
""",
537+
visibility = ["//java:__subpackages__"],
529538
)
530539

531540
cc_binary(
532-
name = "ray_redis_module.so",
541+
name = "libray_redis_module.so",
533542
srcs = [
534543
"src/ray/gcs/redis_module/ray_redis_module.cc",
535544
"src/ray/gcs/redis_module/redis_string.h",
@@ -538,6 +547,7 @@ cc_binary(
538547
copts = COPTS,
539548
linkshared = 1,
540549
linkstatic = 1,
550+
visibility = ["//java:__subpackages__"],
541551
deps = [
542552
":ray_common",
543553
],
@@ -552,29 +562,30 @@ genrule(
552562
"//:python_node_manager_fbs",
553563
"//:redis-server",
554564
"//:redis-cli",
555-
"//:ray_redis_module.so",
565+
"//:libray_redis_module.so",
556566
"//:raylet",
557567
"//:raylet_monitor",
558568
"@plasma//:plasma_store_server",
559569
],
560-
outs = ["ray_pkg"],
570+
outs = ["ray_pkg.out"],
561571
cmd = """
562572
set -x &&
563-
mkdir -p python/ray &&
564-
cp $(location python/ray/_raylet.so) python/ray &&
565-
mkdir -p python/ray/core/src/ray/thirdparty/redis/src/ &&
566-
cp $(location //:redis-server) python/ray/core/src/ray/thirdparty/redis/src/ &&
567-
cp $(location //:redis-cli) python/ray/core/src/ray/thirdparty/redis/src/ &&
568-
mkdir -p python/ray/core/src/ray/gcs/redis_module/ &&
569-
cp $(locations //:ray_redis_module.so) python/ray/core/src/ray/gcs/redis_module/libray_redis_module.so &&
570-
mkdir -p python/ray/core/src/ray/raylet/ &&
571-
cp $(location //:raylet_monitor) python/ray/core/src/ray/raylet/ &&
572-
mkdir -p python/ray/core/src/plasma &&
573-
cp $(location @plasma//:plasma_store_server) python/ray/core/src/plasma/ &&
574-
cp $(location //:raylet) python/ray/core/src/ray/raylet/ &&
575-
for f in $(locations //:python_gcs_fbs); do cp $$f python/ray/core/generated/; done &&
576-
mkdir -p python/ray/core/generated/ray/protocol/ &&
577-
for f in $(locations //:python_node_manager_fbs); do cp $$f python/ray/core/generated/ray/protocol/; done &&
578-
mv python $(location ray_pkg)
579-
""",
573+
WORK_DIR=$$(pwd) &&
574+
cp -f $(location python/ray/_raylet.so) $$WORK_DIR/python/ray &&
575+
mkdir -p $$WORK_DIR/python/ray/core/src/ray/thirdparty/redis/src/ &&
576+
cp -f $(location //:redis-server) $$WORK_DIR/python/ray/core/src/ray/thirdparty/redis/src/ &&
577+
cp -f $(location //:redis-cli) $$WORK_DIR/python/ray/core/src/ray/thirdparty/redis/src/ &&
578+
mkdir -p $$WORK_DIR/python/ray/core/src/ray/gcs/redis_module/ &&
579+
cp -f $(locations //:libray_redis_module.so) $$WORK_DIR/python/ray/core/src/ray/gcs/redis_module/ &&
580+
cp -f $(location //:raylet_monitor) $$WORK_DIR/python/ray/core/src/ray/raylet/ &&
581+
cp -f $(location @plasma//:plasma_store_server) $$WORK_DIR/python/ray/core/src/plasma/ &&
582+
cp -f $(location //:raylet) $$WORK_DIR/python/ray/core/src/ray/raylet/ &&
583+
for f in $(locations //:python_gcs_fbs); do cp -f $$f $$WORK_DIR/python/ray/core/generated/; done &&
584+
mkdir -p $$WORK_DIR/python/ray/core/generated/ray/protocol/ &&
585+
for f in $(locations //:python_node_manager_fbs); do
586+
cp -f $$f $$WORK_DIR/python/ray/core/generated/ray/protocol/;
587+
done &&
588+
echo $$WORK_DIR > $@
589+
""",
590+
local = 1,
580591
)

WORKSPACE

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1+
workspace(name = "com_github_ray_project_ray")
2+
13
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository", "new_git_repository")
4+
load("//java:repo.bzl", "java_repositories")
5+
6+
java_repositories()
7+
8+
git_repository(
9+
name = "com_github_checkstyle_java",
10+
commit = "85f37871ca03b9d3fee63c69c8107f167e24e77b",
11+
remote = "https://github.com/ruifangChen/checkstyle_java",
12+
)
213

314
git_repository(
415
name = "com_github_nelhage_rules_boost",
@@ -38,8 +49,8 @@ new_git_repository(
3849
new_git_repository(
3950
name = "plasma",
4051
build_file = "@//bazel:BUILD.plasma",
41-
commit = "6a27c660ea700febf6fd73b2e851ab96e9315134",
42-
remote = "https://github.com/ray-project/arrow",
52+
commit = "9c33e1aee98f96a82c6a0b6bb214df172a143fb1",
53+
remote = "https://github.com/apache/arrow",
4354
)
4455

4556
new_git_repository(

bazel/BUILD.plasma

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_cc_library")
22

3+
java_library(
4+
name = "org_apache_arrow_arrow_plasma",
5+
srcs = glob(["java/plasma/src/main/java/**/*.java"]),
6+
data = [":plasma_client_java"],
7+
visibility = ["//visibility:public"],
8+
deps = [
9+
"@org_slf4j_slf4j_api//jar",
10+
],
11+
)
12+
13+
java_binary(
14+
name = "org_apache_arrow_arrow_plasma_test",
15+
srcs = ["java/plasma/src/test/java/org/apache/arrow/plasma/PlasmaClientTest.java"],
16+
main_class = "org.apache.arrow.plasma.PlasmaClientTest",
17+
visibility = ["//visibility:public"],
18+
deps = [
19+
":org_apache_arrow_arrow_plasma",
20+
"@junit_junit//jar",
21+
],
22+
)
23+
324
cc_library(
425
name = "arrow",
526
srcs = [
@@ -8,6 +29,7 @@ cc_library(
829
"cpp/src/arrow/status.cc",
930
"cpp/src/arrow/util/io-util.cc",
1031
"cpp/src/arrow/util/logging.cc",
32+
"cpp/src/arrow/util/memory.cc",
1133
"cpp/src/arrow/util/thread-pool.cc",
1234
],
1335
hdrs = [
@@ -19,6 +41,7 @@ cc_library(
1941
"cpp/src/arrow/util/io-util.h",
2042
"cpp/src/arrow/util/logging.h",
2143
"cpp/src/arrow/util/macros.h",
44+
"cpp/src/arrow/util/memory.h",
2245
"cpp/src/arrow/util/string_builder.h",
2346
"cpp/src/arrow/util/string_view.h",
2447
"cpp/src/arrow/util/thread-pool.h",
@@ -48,6 +71,7 @@ cc_library(
4871
"cpp/src/plasma/common.h",
4972
"cpp/src/plasma/common_generated.h",
5073
"cpp/src/plasma/compat.h",
74+
"cpp/src/plasma/external_store.h",
5175
"cpp/src/plasma/fling.h",
5276
"cpp/src/plasma/io.h",
5377
"cpp/src/plasma/malloc.h",
@@ -65,7 +89,7 @@ cc_library(
6589
)
6690

6791
cc_binary(
68-
name = "plasma_client_java.so",
92+
name = "libplasma_java.so",
6993
srcs = [
7094
"cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.cc",
7195
"cpp/src/plasma/lib/java/org_apache_arrow_plasma_PlasmaClientJNI.h",
@@ -77,7 +101,7 @@ cc_binary(
77101
"cpp/src",
78102
],
79103
linkshared = 1,
80-
linkstatic = 0,
104+
linkstatic = 1,
81105
deps = [":plasma_client"],
82106
)
83107

@@ -101,17 +125,17 @@ genrule(
101125

102126
genrule(
103127
name = "plasma-jni-darwin-compat",
104-
srcs = [":plasma_client_java.so"],
105-
outs = ["plasma_client_java.dylib"],
128+
srcs = [":libplasma_java.so"],
129+
outs = ["libplasma_java.dylib"],
106130
cmd = "cp $< $@",
107131
output_to_bindir = 1,
108132
)
109133

110134
filegroup(
111135
name = "plasma_client_java",
112136
srcs = select({
113-
"@bazel_tools//src/conditions:darwin": [":plasma_client_java.dylib"],
114-
"//conditions:default": [":plasma_client_java.so"],
137+
"@bazel_tools//src/conditions:darwin": [":libplasma_java.dylib"],
138+
"//conditions:default": [":libplasma_java.so"],
115139
}),
116140
visibility = ["//visibility:public"],
117141
)
@@ -122,10 +146,14 @@ cc_library(
122146
"cpp/src/plasma/dlmalloc.cc",
123147
"cpp/src/plasma/events.cc",
124148
"cpp/src/plasma/eviction_policy.cc",
149+
"cpp/src/plasma/external_store.cc",
150+
"cpp/src/plasma/plasma_allocator.cc",
125151
],
126152
hdrs = [
127153
"cpp/src/plasma/events.h",
128154
"cpp/src/plasma/eviction_policy.h",
155+
"cpp/src/plasma/external_store.h",
156+
"cpp/src/plasma/plasma_allocator.h",
129157
"cpp/src/plasma/store.h",
130158
"cpp/src/plasma/thirdparty/ae/ae.h",
131159
"cpp/src/plasma/thirdparty/ae/ae_epoll.c",

bazel/ray.bzl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_library_public")
2+
load("@com_github_checkstyle_java//checkstyle:checkstyle.bzl", "checkstyle_test")
23

34
def flatbuffer_py_library(name, srcs, outs, out_prefix, includes = [], include_paths = []):
45
flatbuffer_library_public(
@@ -10,3 +11,44 @@ def flatbuffer_py_library(name, srcs, outs, out_prefix, includes = [], include_p
1011
include_paths = include_paths,
1112
includes = includes,
1213
)
14+
15+
def flatbuffer_java_library(name, srcs, outs, out_prefix, includes = [], include_paths = []):
16+
flatbuffer_library_public(
17+
name = name,
18+
srcs = srcs,
19+
outs = outs,
20+
language_flag = "-j",
21+
out_prefix = out_prefix,
22+
include_paths = include_paths,
23+
includes = includes,
24+
)
25+
26+
def define_java_module(name, additional_srcs = [], additional_resources = [], define_test_lib = False, test_deps = [], **kwargs):
27+
native.java_library(
28+
name = "org_ray_ray_" + name,
29+
srcs = additional_srcs + native.glob([name + "/src/main/java/**/*.java"]),
30+
resources = native.glob([name + "/src/main/resources/**"]) + additional_resources,
31+
**kwargs
32+
)
33+
checkstyle_test(
34+
name = "org_ray_ray_" + name + "-checkstyle",
35+
target = "//java:org_ray_ray_" + name,
36+
config = "//java:checkstyle.xml",
37+
suppressions = "//java:checkstyle-suppressions.xml",
38+
size = "small",
39+
tags = ["checkstyle"],
40+
)
41+
if define_test_lib:
42+
native.java_library(
43+
name = "org_ray_ray_" + name + "_test",
44+
srcs = native.glob([name + "/src/test/java/**/*.java"]),
45+
deps = test_deps,
46+
)
47+
checkstyle_test(
48+
name = "org_ray_ray_" + name + "_test-checkstyle",
49+
target = "//java:org_ray_ray_" + name + "_test",
50+
config = "//java:checkstyle.xml",
51+
suppressions = "//java:checkstyle-suppressions.xml",
52+
size = "small",
53+
tags = ["checkstyle"],
54+
)

build.sh

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,15 @@ else
121121
$PYTHON_EXECUTABLE -m pip install \
122122
--target=$ROOT_DIR/python/ray/pyarrow_files pyarrow==0.12.0.RAY \
123123
--find-links https://s3-us-west-2.amazonaws.com/arrow-wheels/9357dc130789ee42f8181d8724bee1d5d1509060/index.html
124-
bazel build //:ray_pkg -c opt --verbose_failures --action_env=PYTHON_BIN_PATH=$PYTHON_EXECUTABLE
125-
# Copy files and keep them writeable. This is a workaround, as Bazel
126-
# marks all generated files non-writeable. If we would just copy them
127-
# over without adding write permission, the copy would fail the next time.
128-
# TODO(pcm): It would be great to have a solution here that does not
129-
# require us to copy the files.
130-
find $ROOT_DIR/bazel-genfiles/ray_pkg/ -exec chmod +w {} \;
131-
cp -r $ROOT_DIR/bazel-genfiles/ray_pkg/ray $ROOT_DIR/python || true
124+
125+
if [ "$RAY_BUILD_JAVA" == "YES" ]; then
126+
bazel run //java:bazel_deps -- generate -r $ROOT_DIR -s java/third_party/workspace.bzl -d java/dependencies.yaml
127+
bazel build //java:all --verbose_failures --action_env=PATH
128+
fi
129+
130+
if [ "$RAY_BUILD_PYTHON" == "YES" ]; then
131+
bazel build //:ray_pkg --verbose_failures --action_env=PYTHON_BIN_PATH=$PYTHON_EXECUTABLE
132+
fi
132133
fi
133134

134135
popd

0 commit comments

Comments
 (0)