Skip to content

Commit a982bde

Browse files
committed
Migrate ffi examples to bzlmod
1 parent 0e2fbbf commit a982bde

File tree

24 files changed

+200
-28
lines changed

24 files changed

+200
-28
lines changed

.bazelci/presubmit.yml

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,6 @@ tasks:
471471
- "//..."
472472
# TODO: This requires an updated `rules_foreign_cc`
473473
- "-//sys/..."
474-
# See https://github.com/bazelbuild/bazel/issues/9987
475-
- "-//ffi/rust_calling_c:matrix_dylib_test"
476474
shell_commands:
477475
- sed -i 's/^# load("@bazel_ci_rules/load("@bazel_ci_rules/' WORKSPACE.bazel
478476
- sed -i 's/^# rbe_preconfig/rbe_preconfig/' WORKSPACE.bazel
@@ -750,12 +748,39 @@ tasks:
750748
- "//:all"
751749
test_targets:
752750
- "//..."
753-
bzlmod_ffi:
754-
name: FFI with bzlmod
751+
example_ffi_linux:
755752
platform: ubuntu2004
756753
working_directory: examples/bzlmod/ffi
754+
name: FFI Example
757755
build_targets:
758756
- "//..."
757+
test_targets:
758+
- "//..."
759+
# TODO: https://github.com/bazelbuild/bazel/issues/9987
760+
# example_ffi_linux_rbe:
761+
# platform: rbe_ubuntu2004
762+
# working_directory: examples/bzlmod/ffi
763+
# name: FFI Example
764+
# build_targets:
765+
# - "//..."
766+
# test_targets:
767+
# - "//..."
768+
example_ffi_macos:
769+
platform: macos_arm64
770+
working_directory: examples/bzlmod/ffi
771+
name: FFI Example
772+
build_targets:
773+
- "//..."
774+
test_targets:
775+
- "//..."
776+
example_ffi_windows:
777+
platform: windows
778+
working_directory: examples/bzlmod/ffi
779+
name: FFI Example
780+
build_targets:
781+
- "//..."
782+
test_targets:
783+
- "//..."
759784
ubuntu2004_bzlmod_bcr:
760785
name: bzlmod BCR presubmit
761786
platform: ubuntu2004

examples/bzlmod/ffi/.bazelrc

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@
55
## https://bazel.build/docs/best-practices#bazelrc-file
66
###############################################################################
77

8-
9-
###############################################################################
10-
## Windows configuration
11-
###############################################################################
12-
13-
# https://bazel.build/reference/command-line-reference#flag--enable_platform_specific_config
14-
common --enable_platform_specific_config
15-
16-
# https://bazel.build/docs/windows#symlink
17-
startup --windows_enable_symlinks
18-
build:windows --enable_runfiles
19-
208
###############################################################################
219
## Build configuration
2210
###############################################################################
@@ -40,6 +28,9 @@ test --verbose_failures
4028
# Enable Bzlmod for every Bazel command
4129
common --enable_bzlmod
4230

31+
# Disable the bzlmod lockfile, so we don't accidentally commit MODULE.bazel.lock
32+
common --lockfile_mode=off
33+
4334
# Write build outputs in a platform-specific directory;
4435
# avoid outputs being wiped and rewritten when switching between platforms.
4536
common --experimental_platform_in_output_dir
@@ -55,6 +46,8 @@ common --noslim_profile
5546
common --experimental_profile_include_target_label
5647
common --experimental_profile_include_primary_output
5748

49+
common --incompatible_macos_set_install_name
50+
5851
###############################################################################
5952
## Rust configuration
6053
###############################################################################
@@ -67,6 +60,12 @@ build:rustfmt --output_groups=+rustfmt_checks
6760
build:clippy --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
6861
build:clippy --output_groups=+clippy_checks
6962

63+
###############################################################################
64+
## Java configuration
65+
###############################################################################
66+
67+
common --java_runtime_version=remotejdk_21
68+
7069
###############################################################################
7170
## Custom user flags
7271
##

examples/bzlmod/ffi/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/bazel-*
2-
.DS_Store
2+
user.bazelrc

examples/bzlmod/ffi/MODULE.bazel

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module(
2-
name = "ffi",
2+
name = "rules_rust_example_ffi",
33
version = "0.0.0",
44
)
55

@@ -13,6 +13,11 @@ local_path_override(
1313
path = "../../..",
1414
)
1515

16+
bazel_dep(
17+
name = "platforms",
18+
version = "0.0.10",
19+
)
20+
1621
###############################################################################
1722
# T O O L C H A I N S
1823
###############################################################################
@@ -30,3 +35,99 @@ rust.toolchain(
3035
use_repo(rust, "rust_toolchains")
3136

3237
register_toolchains("@rust_toolchains//:all")
38+
39+
###############################################################################
40+
# F F I T A R G E T D E P S
41+
###############################################################################
42+
43+
bazel_dep(
44+
name = "rules_java",
45+
version = "8.6.3",
46+
)
47+
bazel_dep(
48+
name = "rules_jvm_external",
49+
version = "6.6",
50+
)
51+
52+
java_toolchains = use_extension("@rules_java//java:extensions.bzl", "toolchains")
53+
use_repo(java_toolchains, "remote_java_tools")
54+
use_repo(java_toolchains, "remote_java_tools_linux")
55+
use_repo(java_toolchains, "remote_java_tools_windows")
56+
use_repo(java_toolchains, "remote_java_tools_darwin_x86_64")
57+
use_repo(java_toolchains, "remote_java_tools_darwin_arm64")
58+
59+
JDKS = {
60+
# Must match JDK repos defined in remote_jdk21_repos()
61+
"21": [
62+
"linux",
63+
"linux_aarch64",
64+
"linux_ppc64le",
65+
"linux_s390x",
66+
"macos",
67+
"macos_aarch64",
68+
"win",
69+
"win_arm64",
70+
],
71+
}
72+
73+
REMOTE_JDK_REPOS = [
74+
(("remote_jdk" if version == "8" else "remotejdk") + version + "_" + platform)
75+
for version in JDKS
76+
for platform in JDKS[version]
77+
]
78+
79+
[
80+
use_repo(
81+
java_toolchains,
82+
repo + "_toolchain_config_repo",
83+
)
84+
for repo in REMOTE_JDK_REPOS
85+
]
86+
87+
[
88+
register_toolchains("@" + name + "_toolchain_config_repo//:all")
89+
for name in REMOTE_JDK_REPOS
90+
]
91+
92+
maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
93+
maven.install(
94+
artifacts = [
95+
"net.java.dev.jna:jna:5.14.0",
96+
"org.hamcrest:hamcrest:2.2",
97+
],
98+
lock_file = "@//:maven_install.json",
99+
repositories = [
100+
"https://repo1.maven.org/maven2",
101+
],
102+
)
103+
use_repo(maven, "maven")
104+
105+
# https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/repo/http.bzl
106+
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
107+
108+
http_archive(
109+
name = "libc",
110+
build_file_content = """\
111+
load("@rules_rust//rust:defs.bzl", "rust_library")
112+
113+
rust_library(
114+
name = "libc",
115+
srcs = glob(["src/**/*.rs"]),
116+
edition = "2015",
117+
rustc_flags = [
118+
# In most cases, warnings in 3rd party crates are not interesting as
119+
# they're out of the control of consumers. The flag here silences
120+
# warnings. For more details see:
121+
# https://doc.rust-lang.org/rustc/lints/levels.html
122+
"--cap-lints=allow",
123+
],
124+
visibility = ["//visibility:public"],
125+
)
126+
""",
127+
sha256 = "1ac4c2ac6ed5a8fb9020c166bc63316205f1dc78d4b964ad31f4f21eb73f0c6d",
128+
strip_prefix = "libc-0.2.20",
129+
urls = [
130+
"https://mirror.bazel.build/github.com/rust-lang/libc/archive/0.2.20.zip",
131+
"https://github.com/rust-lang/libc/archive/0.2.20.zip",
132+
],
133+
)

examples/ffi/java_calling_rust/com/example/rustjni/BUILD.bazel renamed to examples/bzlmod/ffi/java_calling_rust/com/example/rustjni/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ load("@rules_java//java:defs.bzl", "java_library", "java_test")
33
java_library(
44
name = "rustjni",
55
srcs = ["RustStringLength.java"],
6-
data = ["//ffi/java_calling_rust/rust-crate:rstrlen"],
6+
data = ["//java_calling_rust/rust-crate:rstrlen"],
77
deps = [
88
"@bazel_tools//tools/java/runfiles",
99
"@maven//:net_java_dev_jna_jna",

examples/ffi/java_calling_rust/com/example/rustjni/RustStringLength.java renamed to examples/bzlmod/ffi/java_calling_rust/com/example/rustjni/RustStringLength.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static RustStringLength loadNativeLibrary() throws IOException {
2222
extension = "dll";
2323
}
2424
Runfiles.Preloaded runfiles = Runfiles.preload();
25-
String dylibPath = runfiles.withSourceRepository(AutoBazelRepository_RustStringLength.NAME).rlocation("examples/ffi/java_calling_rust/rust-crate/" + prefix + "rstrlen." + extension);
25+
String dylibPath = runfiles.withSourceRepository(AutoBazelRepository_RustStringLength.NAME).rlocation("rules_rust_example_ffi/java_calling_rust/rust-crate/" + prefix + "rstrlen." + extension);
2626

2727
return Native.load(dylibPath, RustStringLength.class);
2828
}

0 commit comments

Comments
 (0)