From 18f29579e0891d32c427f24b2b3c37548c35cfa4 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Fri, 10 May 2019 13:45:32 +0200 Subject: [PATCH] Initial commit --- Makefile | 29 +++++++++++++++++++++++++++++ c/main.c | 21 +++++++++++++++++++++ dune-project | 2 ++ ocaml-native-lib.opam | 16 ++++++++++++++++ ocaml/bye.ml | 4 ++++ ocaml/dune | 2 ++ ocaml/hello.ml | 4 ++++ 7 files changed, 78 insertions(+) create mode 100644 Makefile create mode 100644 c/main.c create mode 100644 dune-project create mode 100644 ocaml-native-lib.opam create mode 100644 ocaml/bye.ml create mode 100644 ocaml/dune create mode 100644 ocaml/hello.ml diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..de6777b --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +BUILD:=_build +OCAML_ROOT:=$(shell ocamlopt -where) +OCAMLOPT_FLAGS:=-output-complete-obj + +.PHONY: ocaml +ocaml: + dune build ocaml/hello.a + cp _build/default/ocaml/hello.a $(BUILD)/libfoo.a + +ocaml-manual: + mkdir -p $(BUILD) + ocamlopt $(OCAMLOPT_FLAGS) ocaml/hello.ml -o $(BUILD)/hello_obj.o + ocamlopt $(OCAMLOPT_FLAGS) ocaml/bye.ml -o $(BUILD)/bye_obj.o + ar -rs $(BUILD)/libfoo.a $(BUILD)/hello_obj.o $(BUILD)/bye_obj.o + +.PHONY: c +c: + gcc -c c/main.c -I$(OCAML_ROOT) -o $(BUILD)/main.o + gcc -o $(BUILD)/test.exe $(BUILD)/main.o -L$(OCAML_ROOT) -L$(BUILD) -lfoo -lm -ldl -lasmrun + +run: + $(BUILD)/test.exe + +clean: + dune clean + rm -rf $(BUILD) + +bootstrap: + opam install . --deps-only diff --git a/c/main.c b/c/main.c new file mode 100644 index 0000000..f02594f --- /dev/null +++ b/c/main.c @@ -0,0 +1,21 @@ +#include +#include +#include + +void print_closure(const char *closure_name) { + value * closure_f = caml_named_value(closure_name); + if (closure_f == NULL) { + printf("ERROR: Unreachable closure %s\n", closure_name); + return; + } + + const char *str = String_val(caml_callback(*closure_f, Val_unit)); + printf("%s: %s\n", closure_name, str); +} + +int main(int argc, char **argv) { + caml_main(argv); + print_closure("Hello callback"); + print_closure("Bye callback"); + return 0; +} diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..6c90140 --- /dev/null +++ b/dune-project @@ -0,0 +1,2 @@ +(lang dune 1.9) +(name ocaml-native-lib) diff --git a/ocaml-native-lib.opam b/ocaml-native-lib.opam new file mode 100644 index 0000000..91eb814 --- /dev/null +++ b/ocaml-native-lib.opam @@ -0,0 +1,16 @@ +opam-version: "2.0" +maintainer: "public@ffreling.com" +authors: "Fabien Freling" +bug-reports: "https://gitlab.com/ffreling/ocaml-native-lib/issues" +homepage: "https://gitlab.com/ffreling/ocaml-native-lib" +dev-repo: "git+https://gitlab.com:ffreling/ocaml-native-lib.git" +synopsis: "Sample project to link OCaml to C programs" +description: """ +""" + +build: [["dune" "build" "-p" name "-j" jobs]] + +depends: [ + "ocaml" {>= "4.07.0"} + "dune" {build & >= "1.9"} +] diff --git a/ocaml/bye.ml b/ocaml/bye.ml new file mode 100644 index 0000000..29fa422 --- /dev/null +++ b/ocaml/bye.ml @@ -0,0 +1,4 @@ +let bye () = "bye" + +let () = + Callback.register "Bye callback" bye; diff --git a/ocaml/dune b/ocaml/dune new file mode 100644 index 0000000..9cf3196 --- /dev/null +++ b/ocaml/dune @@ -0,0 +1,2 @@ +(library + (name hello)) diff --git a/ocaml/hello.ml b/ocaml/hello.ml new file mode 100644 index 0000000..5e53ef7 --- /dev/null +++ b/ocaml/hello.ml @@ -0,0 +1,4 @@ +let hello () = "hello" + +let () = + Callback.register "Hello callback" hello;