caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Matthieu Dubuget <matthieu.dubuget@gmail.com>
Cc: O'Caml Mailing List <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] dll from ocaml code
Date: Mon, 16 Mar 2009 13:56:39 +0100	[thread overview]
Message-ID: <49BE4C87.9050808@gmail.com> (raw)
In-Reply-To: <38CFD1B3-7B91-4F7A-80E0-354A9FD29E56@gmail.com>

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




      reply	other threads:[~2009-03-16 12:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-16 11:21 Joel Reymont
2009-03-16 12:56 ` Matthieu Dubuget [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=49BE4C87.9050808@gmail.com \
    --to=matthieu.dubuget@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).