Initial commit

This commit is contained in:
Fabien Freling 2019-05-10 13:45:32 +02:00
parent 6499d7d7ae
commit 18f29579e0
7 changed files with 78 additions and 0 deletions

29
Makefile Normal file
View file

@ -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

21
c/main.c Normal file
View file

@ -0,0 +1,21 @@
#include <caml/mlvalues.h>
#include <caml/callback.h>
#include <stdio.h>
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;
}

2
dune-project Normal file
View file

@ -0,0 +1,2 @@
(lang dune 1.9)
(name ocaml-native-lib)

16
ocaml-native-lib.opam Normal file
View file

@ -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"}
]

4
ocaml/bye.ml Normal file
View file

@ -0,0 +1,4 @@
let bye () = "bye"
let () =
Callback.register "Bye callback" bye;

2
ocaml/dune Normal file
View file

@ -0,0 +1,2 @@
(library
(name hello))

4
ocaml/hello.ml Normal file
View file

@ -0,0 +1,4 @@
let hello () = "hello"
let () =
Callback.register "Hello callback" hello;