Skip to content

Commit

Permalink
Migrate ffi examples to bzlmod (#3129)
Browse files Browse the repository at this point in the history
Relates to #1839
  • Loading branch information
UebelAndre authored Dec 29, 2024
1 parent 55ef2b8 commit 51ff049
Show file tree
Hide file tree
Showing 24 changed files with 200 additions and 28 deletions.
33 changes: 29 additions & 4 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,6 @@ tasks:
rbe_examples_targets: &rbe_examples_targets
- "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245
- "//..."
# See https://github.com/bazelbuild/bazel/issues/9987
- "-//ffi/rust_calling_c:matrix_dylib_test"
shell_commands:
- sed -i 's/^# load("@bazel_ci_rules/load("@bazel_ci_rules/' WORKSPACE.bazel
- sed -i 's/^# rbe_preconfig/rbe_preconfig/' WORKSPACE.bazel
Expand Down Expand Up @@ -748,12 +746,39 @@ tasks:
- "//:all"
test_targets:
- "//..."
bzlmod_ffi:
name: FFI with bzlmod
example_ffi_linux:
platform: ubuntu2004
working_directory: examples/bzlmod/ffi
name: FFI Example
build_targets:
- "//..."
test_targets:
- "//..."
# TODO: https://github.com/bazelbuild/bazel/issues/9987
# example_ffi_linux_rbe:
# platform: rbe_ubuntu2004
# working_directory: examples/bzlmod/ffi
# name: FFI Example
# build_targets:
# - "//..."
# test_targets:
# - "//..."
example_ffi_macos:
platform: macos_arm64
working_directory: examples/bzlmod/ffi
name: FFI Example
build_targets:
- "//..."
test_targets:
- "//..."
example_ffi_windows:
platform: windows
working_directory: examples/bzlmod/ffi
name: FFI Example
build_targets:
- "//..."
test_targets:
- "//..."
ubuntu2004_bzlmod_bcr:
name: bzlmod BCR presubmit
platform: ubuntu2004
Expand Down
23 changes: 11 additions & 12 deletions examples/bzlmod/ffi/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@
## https://bazel.build/docs/best-practices#bazelrc-file
###############################################################################


###############################################################################
## Windows configuration
###############################################################################

# https://bazel.build/reference/command-line-reference#flag--enable_platform_specific_config
common --enable_platform_specific_config

# https://bazel.build/docs/windows#symlink
startup --windows_enable_symlinks
build:windows --enable_runfiles

###############################################################################
## Build configuration
###############################################################################
Expand All @@ -40,6 +28,9 @@ test --verbose_failures
# Enable Bzlmod for every Bazel command
common --enable_bzlmod

# Disable the bzlmod lockfile, so we don't accidentally commit MODULE.bazel.lock
common --lockfile_mode=off

# Write build outputs in a platform-specific directory;
# avoid outputs being wiped and rewritten when switching between platforms.
common --experimental_platform_in_output_dir
Expand All @@ -55,6 +46,8 @@ common --noslim_profile
common --experimental_profile_include_target_label
common --experimental_profile_include_primary_output

common --incompatible_macos_set_install_name

###############################################################################
## Rust configuration
###############################################################################
Expand All @@ -67,6 +60,12 @@ build:rustfmt --output_groups=+rustfmt_checks
build:clippy --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
build:clippy --output_groups=+clippy_checks

###############################################################################
## Java configuration
###############################################################################

common --java_runtime_version=remotejdk_21

###############################################################################
## Custom user flags
##
Expand Down
2 changes: 1 addition & 1 deletion examples/bzlmod/ffi/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/bazel-*
.DS_Store
user.bazelrc
103 changes: 102 additions & 1 deletion examples/bzlmod/ffi/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module(
name = "ffi",
name = "rules_rust_example_ffi",
version = "0.0.0",
)

Expand All @@ -13,6 +13,11 @@ local_path_override(
path = "../../..",
)

bazel_dep(
name = "platforms",
version = "0.0.10",
)

###############################################################################
# T O O L C H A I N S
###############################################################################
Expand All @@ -30,3 +35,99 @@ rust.toolchain(
use_repo(rust, "rust_toolchains")

register_toolchains("@rust_toolchains//:all")

###############################################################################
# F F I T A R G E T D E P S
###############################################################################

bazel_dep(
name = "rules_java",
version = "8.6.3",
)
bazel_dep(
name = "rules_jvm_external",
version = "6.6",
)

java_toolchains = use_extension("@rules_java//java:extensions.bzl", "toolchains")
use_repo(java_toolchains, "remote_java_tools")
use_repo(java_toolchains, "remote_java_tools_linux")
use_repo(java_toolchains, "remote_java_tools_windows")
use_repo(java_toolchains, "remote_java_tools_darwin_x86_64")
use_repo(java_toolchains, "remote_java_tools_darwin_arm64")

JDKS = {
# Must match JDK repos defined in remote_jdk21_repos()
"21": [
"linux",
"linux_aarch64",
"linux_ppc64le",
"linux_s390x",
"macos",
"macos_aarch64",
"win",
"win_arm64",
],
}

REMOTE_JDK_REPOS = [
(("remote_jdk" if version == "8" else "remotejdk") + version + "_" + platform)
for version in JDKS
for platform in JDKS[version]
]

[
use_repo(
java_toolchains,
repo + "_toolchain_config_repo",
)
for repo in REMOTE_JDK_REPOS
]

[
register_toolchains("@" + name + "_toolchain_config_repo//:all")
for name in REMOTE_JDK_REPOS
]

maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
artifacts = [
"net.java.dev.jna:jna:5.14.0",
"org.hamcrest:hamcrest:2.2",
],
lock_file = "@//:maven_install.json",
repositories = [
"https://repo1.maven.org/maven2",
],
)
use_repo(maven, "maven")

# https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/repo/http.bzl
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "libc",
build_file_content = """\
load("@rules_rust//rust:defs.bzl", "rust_library")
rust_library(
name = "libc",
srcs = glob(["src/**/*.rs"]),
edition = "2015",
rustc_flags = [
# In most cases, warnings in 3rd party crates are not interesting as
# they're out of the control of consumers. The flag here silences
# warnings. For more details see:
# https://doc.rust-lang.org/rustc/lints/levels.html
"--cap-lints=allow",
],
visibility = ["//visibility:public"],
)
""",
sha256 = "1ac4c2ac6ed5a8fb9020c166bc63316205f1dc78d4b964ad31f4f21eb73f0c6d",
strip_prefix = "libc-0.2.20",
urls = [
"https://mirror.bazel.build/github.com/rust-lang/libc/archive/0.2.20.zip",
"https://github.com/rust-lang/libc/archive/0.2.20.zip",
],
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("@rules_java//java:defs.bzl", "java_library", "java_test")
java_library(
name = "rustjni",
srcs = ["RustStringLength.java"],
data = ["//ffi/java_calling_rust/rust-crate:rstrlen"],
data = ["//java_calling_rust/rust-crate:rstrlen"],
deps = [
"@bazel_tools//tools/java/runfiles",
"@maven//:net_java_dev_jna_jna",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static RustStringLength loadNativeLibrary() throws IOException {
extension = "dll";
}
Runfiles.Preloaded runfiles = Runfiles.preload();
String dylibPath = runfiles.withSourceRepository(AutoBazelRepository_RustStringLength.NAME).rlocation("examples/ffi/java_calling_rust/rust-crate/" + prefix + "rstrlen." + extension);
String dylibPath = runfiles.withSourceRepository(AutoBazelRepository_RustStringLength.NAME).rlocation("rules_rust_example_ffi/java_calling_rust/rust-crate/" + prefix + "rstrlen." + extension);

return Native.load(dylibPath, RustStringLength.class);
}
Expand Down
49 changes: 49 additions & 0 deletions examples/bzlmod/ffi/maven_install.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
"__INPUT_ARTIFACTS_HASH": -152875169,
"__RESOLVED_ARTIFACTS_HASH": 61617868,
"artifacts": {
"net.java.dev.jna:jna": {
"shasums": {
"jar": "34ed1e1f27fa896bca50dbc4e99cf3732967cec387a7a0d5e3486c09673fe8c6"
},
"version": "5.14.0"
},
"org.hamcrest:hamcrest": {
"shasums": {
"jar": "5e62846a89f05cd78cd9c1a553f340d002458380c320455dd1f8fc5497a8a1c1"
},
"version": "2.2"
}
},
"dependencies": {},
"packages": {
"net.java.dev.jna:jna": [
"com.sun.jna",
"com.sun.jna.internal",
"com.sun.jna.ptr",
"com.sun.jna.win32"
],
"org.hamcrest:hamcrest": [
"org.hamcrest",
"org.hamcrest.beans",
"org.hamcrest.collection",
"org.hamcrest.comparator",
"org.hamcrest.core",
"org.hamcrest.internal",
"org.hamcrest.io",
"org.hamcrest.number",
"org.hamcrest.object",
"org.hamcrest.text",
"org.hamcrest.xml"
]
},
"repositories": {
"https://repo1.maven.org/maven2/": [
"net.java.dev.jna:jna",
"org.hamcrest:hamcrest"
]
},
"services": {},
"version": "2"
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_rust//rust:defs.bzl", "rust_doc", "rust_library", "rust_test")

package(default_visibility = ["//ffi/rust_calling_c:__subpackages__"])
package(default_visibility = ["//rust_calling_c/example_2:__subpackages__"])

rust_library(
name = "matrix",
Expand All @@ -9,7 +9,7 @@ rust_library(
"src/matrix.rs",
],
deps = [
"//ffi/rust_calling_c/c:native_matrix",
"//rust_calling_c/example_2/c:native_matrix",
"@libc",
],
)
Expand Down Expand Up @@ -39,7 +39,7 @@ rust_library(
"//conditions:default": [],
}),
deps = [
"//ffi/rust_calling_c/c:native_matrix_so",
"//rust_calling_c/example_2/c:native_matrix_so",
"@libc",
],
)
Expand All @@ -48,8 +48,6 @@ rust_test(
name = "matrix_dylib_test",
crate = ":matrix_dynamically_linked",
target_compatible_with = select({
# TODO: This test requires --incompatible_macos_set_install_name and Bazel 4.2.0+
"@platforms//os:macos": ["@platforms//:incompatible"],
# TODO: Make this work on windows
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_import", "cc_library", "cc_test")

package(default_visibility = ["//ffi/rust_calling_c:__subpackages__"])
package(default_visibility = ["//rust_calling_c/example_2:__subpackages__"])

cc_library(
name = "native_matrix",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "ffi/rust_calling_c/c/matrix.h"
#include "rust_calling_c/example_2/c/matrix.h"

#include <stdio.h>
#include <string.h>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "ffi/rust_calling_c/c/matrix.h"
#include "rust_calling_c/example_2/c/matrix.h"

#include <assert.h>
#include <inttypes.h>
Expand Down
File renamed without changes.
File renamed without changes.
Empty file removed examples/ffi/BUILD.bazel
Empty file.

0 comments on commit 51ff049

Please sign in to comment.