This is an automated email from the ASF dual-hosted git repository.
Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new 847cafaace fix(bindings/ocaml): link installed native and bytecode
consumers (#9062)
847cafaace is described below
commit 847cafaace94eb55690a2fb626e49b4a7edcfb37
Author: Xuanwo <[email protected]>
AuthorDate: Wed Nov 25 19:13:07 2026 +0800
fix(bindings/ocaml): link installed native and bytecode consumers (#9062)
Provide the Rust static archive for native OCaml and the shared stub for
bytecode through Dune foreign archives. Leave OCaml runtime startup to the
host, and explicitly install the OpenDAL facade defaults for static linking.
Use rustc to discover native link flags and run the existing tests and
example without a custom library path.
Signed-off-by: Xuanwo <[email protected]>
---
bindings/ocaml/Cargo.toml | 5 +++--
bindings/ocaml/examples/dune | 17 ++++-------------
bindings/ocaml/src/dune | 27 +++++++++++++++------------
bindings/ocaml/src/lib.rs | 2 ++
bindings/ocaml/test/dune | 20 +++-----------------
5 files changed, 27 insertions(+), 44 deletions(-)
diff --git a/bindings/ocaml/Cargo.toml b/bindings/ocaml/Cargo.toml
index 0968ee0dc8..b374a69adf 100644
--- a/bindings/ocaml/Cargo.toml
+++ b/bindings/ocaml/Cargo.toml
@@ -27,11 +27,12 @@ repository = "https://github.com/apache/opendal"
rust-version = "1.91"
[lib]
-crate-type = ["cdylib"]
+crate-type = ["staticlib", "cdylib"]
doc = false
[dependencies]
-ocaml = { version = "1.2.0" }
+# The OCaml executable owns and initializes the runtime.
+ocaml = { version = "1.2.0", features = ["no-caml-startup"] }
# this crate won't be published, we always use the local version
opendal = { version = ">=0", path = "../../core", features = [
"blocking",
diff --git a/bindings/ocaml/examples/dune b/bindings/ocaml/examples/dune
index e3661ee5ec..2e5c879025 100644
--- a/bindings/ocaml/examples/dune
+++ b/bindings/ocaml/examples/dune
@@ -15,20 +15,11 @@
; specific language governing permissions and limitations
; under the License.
-(executables
- (libraries opendal)
- (names getting_started)
- (modules getting_started))
+(executable
+ (name getting_started)
+ (libraries opendal))
(rule
(alias run_examples)
- (deps ../src/libopendal_core.so)
(action
- (progn
- (setenv
- LD_LIBRARY_PATH
- "../src:%{env:LD_LIBRARY_PATH=}"
- (setenv
- DYLD_LIBRARY_PATH
- "../src:%{env:DYLD_LIBRARY_PATH=}"
- (run ./getting_started.exe))))))
+ (run ./getting_started.exe)))
diff --git a/bindings/ocaml/src/dune b/bindings/ocaml/src/dune
index d3b4770e14..4c3432131f 100644
--- a/bindings/ocaml/src/dune
+++ b/bindings/ocaml/src/dune
@@ -16,26 +16,29 @@
; under the License.
(rule
- (targets libopendal_core.a dllopendal_core.so libopendal_core.so)
+ (targets libopendal_core.a dllopendal_core.so opendal_core_link_flags.sexp)
(deps
+ ../Cargo.toml
(glob_files *.rs))
(action
(progn
- (run sh -c "cd %{project_root}/../.. && cargo build --release")
(run sh -c
- "if [ -f %{project_root}/../../target/release/libopendal_ocaml.so ]; then
cp %{project_root}/../../target/release/libopendal_ocaml.so
./dllopendal_core.so; cp
%{project_root}/../../target/release/libopendal_ocaml.so ./libopendal_core.so;
else cp %{project_root}/../../target/release/libopendal_ocaml.dylib
./dllopendal_core.so; cp
%{project_root}/../../target/release/libopendal_ocaml.dylib
./libopendal_core.so; fi")
- (run sh -c
- "echo 'void __opendal_core_dummy(void) {}' | cc -c -x c - -o
opendal_core_dummy.o && ar crs libopendal_core.a opendal_core_dummy.o && rm
opendal_core_dummy.o"))))
+ "cd %{project_root}/../.. && cargo rustc --release --
--print=native-static-libs=target/release/opendal_core_link_flags")
+ (run cp %{project_root}/../../target/release/libopendal_ocaml.a
+ libopendal_core.a)
+ (run sh -ec
+ "if [ '%{ocaml-config:system}' = macosx ]; then ext=dylib; else ext=so;
fi\n cp %{project_root}/../../target/release/libopendal_ocaml.$ext
dllopendal_core.so")
+ (with-stdout-to
+ opendal_core_link_flags.sexp
+ (progn
+ (echo "(")
+ (run cat %{project_root}/../../target/release/opendal_core_link_flags)
+ (echo ")"))))))
(library
(name opendal_core)
(public_name opendal.opendal_core)
(foreign_archives opendal_core)
+ ; Static linking also needs Rust's system libraries and platform frameworks.
(c_library_flags
- (-lpthread -lc -lm)))
-
-(install
- (section lib)
- (package opendal)
- (files
- (libopendal_core.so as opendal_core/libopendal_core.so)))
+ (:include opendal_core_link_flags.sexp)))
diff --git a/bindings/ocaml/src/lib.rs b/bindings/ocaml/src/lib.rs
index 8f27c507e6..4ecd2ceb76 100644
--- a/bindings/ocaml/src/lib.rs
+++ b/bindings/ocaml/src/lib.rs
@@ -31,6 +31,8 @@ static RUNTIME: LazyLock<tokio::runtime::Runtime> =
LazyLock::new(|| {
});
pub fn new_operator(scheme: &str, map: HashMap<String, String>) ->
Result<od::Operator, od::Error> {
+ // Static linking can omit the facade's startup constructor.
+ od::install_default();
od::Operator::via_iter(scheme, map)
}
diff --git a/bindings/ocaml/test/dune b/bindings/ocaml/test/dune
index bba172c3f2..89b9ca7da0 100644
--- a/bindings/ocaml/test/dune
+++ b/bindings/ocaml/test/dune
@@ -15,20 +15,6 @@
; specific language governing permissions and limitations
; under the License.
-(executables
- (libraries opendal ounit2)
- (names test)
- (modules test))
-
-(rule
- (alias runtest)
- (deps ../src/libopendal_core.so)
- (action
- (progn
- (setenv
- LD_LIBRARY_PATH
- "../src:%{env:LD_LIBRARY_PATH=}"
- (setenv
- DYLD_LIBRARY_PATH
- "../src:%{env:DYLD_LIBRARY_PATH=}"
- (run ./test.exe))))))
+(test
+ (name test)
+ (libraries opendal ounit2))