Version française
Home     About     Download     Resources     Contact us    
Browse thread
dll from ocaml code
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Matthieu Dubuget <matthieu.dubuget@g...>
Subject: Re: [Caml-list] dll from ocaml code
Joel Reymont a écrit :
> Does anyone have a recipe for packaging OCaml code as a DLL or shared
> library?

yes

>
> I need to be able to expose a few entry points that correspond to
> OCaml functions.

Suppose that you add this line:

      flag ["link";"cmldll"] (S[A"-output-obj"]);

and this rule in the After_rules section of myocamlbuild.ml plugin:

    rule "Mixed C-Ocaml native DLL: cmldll & o* & cmx* -> native DLL
(.dll | .so)"
        ~dep:"%.cmldll"
        ~prod:("%.native" -.- ext_dll)
        begin
          fun env build ->
            let output = env ("%.native" -.- ext_dll)
            and input = env "%.cmldll" in
            let dir = Pathname.dirname input in

            let ext_o_files, moduls_files =
              string_list_of_file input |>
                  List.partition (fun fic -> Filename.check_suffix fic
".o") in
            let objs = ext_o_files |>
                List.map Filename.chop_extension |>
                    List.map (fun fic -> fic -.- ext_obj) in
            let cmxs = moduls_files |>
                List.map (fun modul ->
                            (uncap_module_path modul) -.- "cmx") in
            let deps = cmxs @ objs in

              List.iter ignore_good
                (build (List.map (fun x -> [dir/x]) deps));

              Cmd (S [!Options.ocamlopt;
                      A"-o"; Px output;
                      T (tags_of_pathname
output++"ocaml"++"native"++"cmldll"++"link");
                      atomize_paths deps
                     ])
        end;


where ext_dll is the dll file extension on your system,
ext_obj is tho obj files extension on your system,
let uncap_module_path p =
  (Pathname.dirname p) / (String.uncapitalize (Pathname.basename p)),

In order to build my.native.dll, you have to put in my.cmldll file:
- the ml Modules you want to link in your dll
- the associated obj files, obtained from C stubs.

And ocamlbuild my.native.dll will show you the working command lines…

Now, what to put in your C stub and Modules?

First, one module with your OCaml code.
Then, maybe a separated module that contains Callback.register calls, or
you have to had
them in to your module if you prefer.

Then the C part:

- first step is to init the caml part (see caml_startup in the manual),
either automatically upon
dll loading (in DllMain or _init function, depending on you system) or
in a function to be called
by the DLL client

- then write the C functions, that will calls your registered callbacks

I have no real example here around. But could find one somewhere on my
home's computer if
necessary.

Hoping this helps

Matt