diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index bc24f1f65c..1cce99ccfe 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -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 @@ -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 diff --git a/examples/bzlmod/ffi/.bazelrc b/examples/bzlmod/ffi/.bazelrc index 28c961e396..36f55180fc 100644 --- a/examples/bzlmod/ffi/.bazelrc +++ b/examples/bzlmod/ffi/.bazelrc @@ -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 ############################################################################### @@ -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 @@ -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 ############################################################################### @@ -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 ## diff --git a/examples/bzlmod/ffi/.gitignore b/examples/bzlmod/ffi/.gitignore index e28b710ff7..2a3bb2d902 100644 --- a/examples/bzlmod/ffi/.gitignore +++ b/examples/bzlmod/ffi/.gitignore @@ -1,2 +1,2 @@ /bazel-* -.DS_Store \ No newline at end of file +user.bazelrc diff --git a/examples/bzlmod/ffi/MODULE.bazel b/examples/bzlmod/ffi/MODULE.bazel index ccb9a2a939..f0087a35b8 100644 --- a/examples/bzlmod/ffi/MODULE.bazel +++ b/examples/bzlmod/ffi/MODULE.bazel @@ -1,5 +1,5 @@ module( - name = "ffi", + name = "rules_rust_example_ffi", version = "0.0.0", ) @@ -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 ############################################################################### @@ -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", + ], +) diff --git a/examples/ffi/c_calling_rust/BUILD.bazel b/examples/bzlmod/ffi/c_calling_rust/BUILD.bazel similarity index 100% rename from examples/ffi/c_calling_rust/BUILD.bazel rename to examples/bzlmod/ffi/c_calling_rust/BUILD.bazel diff --git a/examples/ffi/c_calling_rust/lib.rs b/examples/bzlmod/ffi/c_calling_rust/lib.rs similarity index 100% rename from examples/ffi/c_calling_rust/lib.rs rename to examples/bzlmod/ffi/c_calling_rust/lib.rs diff --git a/examples/ffi/c_calling_rust/main.c b/examples/bzlmod/ffi/c_calling_rust/main.c similarity index 100% rename from examples/ffi/c_calling_rust/main.c rename to examples/bzlmod/ffi/c_calling_rust/main.c diff --git a/examples/ffi/java_calling_rust/com/example/rustjni/BUILD.bazel b/examples/bzlmod/ffi/java_calling_rust/com/example/rustjni/BUILD.bazel similarity index 88% rename from examples/ffi/java_calling_rust/com/example/rustjni/BUILD.bazel rename to examples/bzlmod/ffi/java_calling_rust/com/example/rustjni/BUILD.bazel index 6f2305eea3..e3c50ae35a 100644 --- a/examples/ffi/java_calling_rust/com/example/rustjni/BUILD.bazel +++ b/examples/bzlmod/ffi/java_calling_rust/com/example/rustjni/BUILD.bazel @@ -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", diff --git a/examples/ffi/java_calling_rust/com/example/rustjni/RustJniTest.java b/examples/bzlmod/ffi/java_calling_rust/com/example/rustjni/RustJniTest.java similarity index 100% rename from examples/ffi/java_calling_rust/com/example/rustjni/RustJniTest.java rename to examples/bzlmod/ffi/java_calling_rust/com/example/rustjni/RustJniTest.java diff --git a/examples/ffi/java_calling_rust/com/example/rustjni/RustStringLength.java b/examples/bzlmod/ffi/java_calling_rust/com/example/rustjni/RustStringLength.java similarity index 87% rename from examples/ffi/java_calling_rust/com/example/rustjni/RustStringLength.java rename to examples/bzlmod/ffi/java_calling_rust/com/example/rustjni/RustStringLength.java index fa2e74caaa..bf74b088ed 100644 --- a/examples/ffi/java_calling_rust/com/example/rustjni/RustStringLength.java +++ b/examples/bzlmod/ffi/java_calling_rust/com/example/rustjni/RustStringLength.java @@ -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); } diff --git a/examples/ffi/java_calling_rust/rust-crate/BUILD.bazel b/examples/bzlmod/ffi/java_calling_rust/rust-crate/BUILD.bazel similarity index 100% rename from examples/ffi/java_calling_rust/rust-crate/BUILD.bazel rename to examples/bzlmod/ffi/java_calling_rust/rust-crate/BUILD.bazel diff --git a/examples/ffi/java_calling_rust/rust-crate/rust_string_length.rs b/examples/bzlmod/ffi/java_calling_rust/rust-crate/rust_string_length.rs similarity index 100% rename from examples/ffi/java_calling_rust/rust-crate/rust_string_length.rs rename to examples/bzlmod/ffi/java_calling_rust/rust-crate/rust_string_length.rs diff --git a/examples/bzlmod/ffi/maven_install.json b/examples/bzlmod/ffi/maven_install.json new file mode 100644 index 0000000000..ca3192bb6d --- /dev/null +++ b/examples/bzlmod/ffi/maven_install.json @@ -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" +} diff --git a/examples/bzlmod/ffi/ffi/BUILD.bazel b/examples/bzlmod/ffi/rust_calling_c/example_1/BUILD.bazel similarity index 100% rename from examples/bzlmod/ffi/ffi/BUILD.bazel rename to examples/bzlmod/ffi/rust_calling_c/example_1/BUILD.bazel diff --git a/examples/bzlmod/ffi/ffi/c/cc_library.cc b/examples/bzlmod/ffi/rust_calling_c/example_1/c/cc_library.cc similarity index 100% rename from examples/bzlmod/ffi/ffi/c/cc_library.cc rename to examples/bzlmod/ffi/rust_calling_c/example_1/c/cc_library.cc diff --git a/examples/bzlmod/ffi/ffi/src/rust_shared_lib_with_static_dep.rs b/examples/bzlmod/ffi/rust_calling_c/example_1/src/rust_shared_lib_with_static_dep.rs similarity index 100% rename from examples/bzlmod/ffi/ffi/src/rust_shared_lib_with_static_dep.rs rename to examples/bzlmod/ffi/rust_calling_c/example_1/src/rust_shared_lib_with_static_dep.rs diff --git a/examples/ffi/rust_calling_c/BUILD.bazel b/examples/bzlmod/ffi/rust_calling_c/example_2/BUILD.bazel similarity index 78% rename from examples/ffi/rust_calling_c/BUILD.bazel rename to examples/bzlmod/ffi/rust_calling_c/example_2/BUILD.bazel index 041d1da49b..8d2322e962 100644 --- a/examples/ffi/rust_calling_c/BUILD.bazel +++ b/examples/bzlmod/ffi/rust_calling_c/example_2/BUILD.bazel @@ -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", @@ -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", ], ) @@ -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", ], ) @@ -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": [], diff --git a/examples/ffi/rust_calling_c/c/BUILD.bazel b/examples/bzlmod/ffi/rust_calling_c/example_2/c/BUILD.bazel similarity index 90% rename from examples/ffi/rust_calling_c/c/BUILD.bazel rename to examples/bzlmod/ffi/rust_calling_c/example_2/c/BUILD.bazel index 2cab18bc5f..6e587f5cd1 100644 --- a/examples/ffi/rust_calling_c/c/BUILD.bazel +++ b/examples/bzlmod/ffi/rust_calling_c/example_2/c/BUILD.bazel @@ -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", diff --git a/examples/ffi/rust_calling_c/c/matrix.c b/examples/bzlmod/ffi/rust_calling_c/example_2/c/matrix.c similarity index 98% rename from examples/ffi/rust_calling_c/c/matrix.c rename to examples/bzlmod/ffi/rust_calling_c/example_2/c/matrix.c index bf0260ffc4..128d6675fa 100644 --- a/examples/ffi/rust_calling_c/c/matrix.c +++ b/examples/bzlmod/ffi/rust_calling_c/example_2/c/matrix.c @@ -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 #include diff --git a/examples/ffi/rust_calling_c/c/matrix.h b/examples/bzlmod/ffi/rust_calling_c/example_2/c/matrix.h similarity index 100% rename from examples/ffi/rust_calling_c/c/matrix.h rename to examples/bzlmod/ffi/rust_calling_c/example_2/c/matrix.h diff --git a/examples/ffi/rust_calling_c/c/matrix_test.c b/examples/bzlmod/ffi/rust_calling_c/example_2/c/matrix_test.c similarity index 98% rename from examples/ffi/rust_calling_c/c/matrix_test.c rename to examples/bzlmod/ffi/rust_calling_c/example_2/c/matrix_test.c index b73c5ebb00..486b13d450 100644 --- a/examples/ffi/rust_calling_c/c/matrix_test.c +++ b/examples/bzlmod/ffi/rust_calling_c/example_2/c/matrix_test.c @@ -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 #include diff --git a/examples/ffi/rust_calling_c/src/ffi.rs b/examples/bzlmod/ffi/rust_calling_c/example_2/src/ffi.rs similarity index 100% rename from examples/ffi/rust_calling_c/src/ffi.rs rename to examples/bzlmod/ffi/rust_calling_c/example_2/src/ffi.rs diff --git a/examples/ffi/rust_calling_c/src/matrix.rs b/examples/bzlmod/ffi/rust_calling_c/example_2/src/matrix.rs similarity index 100% rename from examples/ffi/rust_calling_c/src/matrix.rs rename to examples/bzlmod/ffi/rust_calling_c/example_2/src/matrix.rs diff --git a/examples/ffi/BUILD.bazel b/examples/ffi/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000