| load("@bazel_skylib//rules:copy_file.bzl", "copy_file") | |
| load("@bazel_skylib//rules:write_file.bzl", "write_file") | |
| load("@rules_cc//cc:cc_binary.bzl", "cc_binary") | |
| load("@rules_cc//cc:cc_library.bzl", "cc_library") | |
| load("@rules_rust//rust:defs.bzl", "rust_shared_library") | |
| load(":rust_ffi.bzl", "RUST_FFI_DEPS", "RUST_TEST_FFI_DEPS") | |
| copy_file( | |
| name = "copy_link_jni_md_header", | |
| src = select({ | |
| "//src/conditions:darwin": "@rules_java//toolchains:jni_md_header-darwin", | |
| "//src/conditions:freebsd": "@rules_java//toolchains:jni_md_header-freebsd", | |
| "//src/conditions:openbsd": "@rules_java//toolchains:jni_md_header-openbsd", | |
| "//src/conditions:windows": "@rules_java//toolchains:jni_md_header-windows", | |
| "//conditions:default": "@rules_java//toolchains:jni_md_header-linux", | |
| }), | |
| out = "jni_md.h", | |
| allow_symlink = True, | |
| visibility = ["//src/main/native:__subpackages__"], | |
| ) | |
| copy_file( | |
| name = "copy_link_jni_header", | |
| src = "@rules_java//toolchains:jni_header", | |
| out = "jni.h", | |
| allow_symlink = True, | |
| visibility = ["//src/main/native:__subpackages__"], | |
| ) | |
| filegroup( | |
| name = "jni_os", | |
| srcs = select({ | |
| "//src/conditions:darwin": [ | |
| "darwin/file_jni.cc", | |
| "darwin/fsevents.cc", | |
| "darwin/system_cpu_speed_monitor_jni.cc", | |
| "darwin/system_disk_space_monitor_jni.cc", | |
| "darwin/system_load_advisory_monitor_jni.cc", | |
| "darwin/system_memory_pressure_jni.cc", | |
| "darwin/system_network_stats.cc", | |
| "darwin/system_suspension_monitor_jni.cc", | |
| "darwin/system_thermal_monitor_jni.cc", | |
| "darwin/util.cc", | |
| "darwin/util.h", | |
| ], | |
| "//src/conditions:freebsd": ["unix_jni_bsd.cc"], | |
| "//src/conditions:openbsd": ["unix_jni_bsd.cc"], | |
| "//conditions:default": ["unix_jni_linux.cc"], | |
| }), | |
| ) | |
| cc_library( | |
| name = "latin1_jni_path", | |
| srcs = [ | |
| "latin1_jni_path.cc", | |
| ":jni.h", | |
| ":jni_md.h", | |
| ], | |
| hdrs = ["latin1_jni_path.h"], | |
| includes = ["."], # For jni headers. | |
| deps = [ | |
| "//src/main/cpp/util:logging", | |
| ], | |
| ) | |
| cc_library( | |
| name = "export", | |
| hdrs = ["export.h"], | |
| visibility = ["//src/main/native:__subpackages__"], | |
| ) | |
| cc_library( | |
| name = "blake3_jni", | |
| srcs = ["blake3_jni.cc"], | |
| visibility = ["//src/main/native:__subpackages__"], | |
| deps = [ | |
| ":export", | |
| "@blake3", | |
| ], | |
| alwayslink = 1, | |
| ) | |
| write_file( | |
| name = "rust_ffi_rs", | |
| out = "rust_ffi.rs", | |
| content = [ | |
| "extern crate {} as _;".format(dep.rsplit(":", 1)[1].replace("-", "_")) | |
| for dep in RUST_FFI_DEPS | |
| ] + [""], | |
| ) | |
| # Aggregates server Rust FFI crates into a standalone cdylib so rustc drives | |
| # the Rust link step (avoiding rules_rust's strong default allocator_library | |
| # colliding with a custom #[global_allocator] on stable Rust) and automatically | |
| # exports all `#[unsafe(no_mangle)] pub extern "C"` symbols across platforms. | |
| rust_shared_library( | |
| name = "rust_ffi", | |
| srcs = [":rust_ffi_rs"], | |
| cc_runtime_linkage = "static", | |
| rustc_flags = select({ | |
| # System libraries omitted from rules_rs v0.2.4's msvc stdlib_linkflags. | |
| "//src/conditions:windows_msvc_like": [ | |
| "-lkernel32", | |
| "-lntdll", | |
| "-lsynchronization", | |
| ], | |
| "//conditions:default": [], | |
| }), | |
| visibility = ["//src/main/java/com/google/devtools/build/lib/jni:__pkg__"], | |
| deps = RUST_FFI_DEPS, | |
| ) | |
| write_file( | |
| name = "rust_test_ffi_rs", | |
| testonly = True, | |
| out = "rust_test_ffi.rs", | |
| content = [ | |
| "extern crate {} as _;".format(dep.rsplit(":", 1)[1].replace("-", "_")) | |
| for dep in RUST_FFI_DEPS + RUST_TEST_FFI_DEPS | |
| ] + [""], | |
| ) | |
| # Test-only superset of :rust_ffi that links RUST_TEST_FFI_DEPS into the same | |
| # cdylib so test helpers share the process-wide TrackingAllocator and Rust | |
| # statics with production crates without shipping test code in :rust_ffi. | |
| rust_shared_library( | |
| name = "rust_test_ffi", | |
| testonly = True, | |
| srcs = [":rust_test_ffi_rs"], | |
| cc_runtime_linkage = "static", | |
| rustc_flags = select({ | |
| # System libraries omitted from rules_rs v0.2.4's msvc stdlib_linkflags. | |
| "//src/conditions:windows_msvc_like": [ | |
| "-lkernel32", | |
| "-lntdll", | |
| "-lsynchronization", | |
| ], | |
| "//conditions:default": [], | |
| }), | |
| visibility = ["//src/main/java/com/google/devtools/build/lib/jni:__pkg__"], | |
| deps = RUST_FFI_DEPS + RUST_TEST_FFI_DEPS, | |
| ) | |
| cc_binary( | |
| name = "libunix_jni.so", | |
| srcs = [ | |
| "unix_jni.cc", | |
| "unix_jni.h", | |
| ":jni.h", | |
| ":jni_md.h", | |
| ":jni_os", | |
| ], | |
| copts = [ | |
| "-fPIC", | |
| ], | |
| includes = ["."], # For jni headers. | |
| linkopts = select({ | |
| "//src/conditions:darwin": [ | |
| "-Wl,-framework,CoreServices", | |
| "-Wl,-framework,IOKit", | |
| ], | |
| "//src/conditions:linux": [ | |
| # Do not export the symbols of a statically linked C++ runtime (the | |
| # hermetic LLVM toolchains link libc++ statically). Java only needs | |
| # the JNI functions and the BAZEL_EXPORT functions. Without this, | |
| # the linker keeps most of the runtime in the library. | |
| "-Wl,--exclude-libs,libc++.a:libc++abi.a:libunwind.a", | |
| ], | |
| "//conditions:default": [], | |
| }), | |
| linkshared = 1, | |
| visibility = ["//src/main/java/com/google/devtools/build/lib/jni:__pkg__"], | |
| deps = [ | |
| ":blake3_jni", | |
| ":latin1_jni_path", | |
| "//src/main/cpp/util:logging", | |
| ], | |
| ) | |
| # TODO(bazel-team): Come up with a way to support platform-specific dynamic | |
| # library extensions. This is issue #914. | |
| copy_file( | |
| name = "mac-compat", | |
| src = "libunix_jni.so", | |
| out = "libunix_jni.dylib", | |
| allow_symlink = True, | |
| visibility = ["//src/main/java/com/google/devtools/build/lib/jni:__pkg__"], | |
| ) | |
| filegroup( | |
| name = "srcs", | |
| srcs = glob(["**"]) + [ | |
| "//src/main/native/windows:srcs", | |
| ], | |
| visibility = ["//src:__pkg__"], | |
| ) | |
| filegroup( | |
| name = "embedded_tools", | |
| srcs = [ | |
| "BUILD", | |
| "//src/main/native/windows:embedded_tools", | |
| ], | |
| visibility = ["//visibility:public"], | |
| ) |