Version française
Home     About     Download     Resources     Contact us    
Browse thread
Producing a C wrapper with ocamlbuild or OMake
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nicolas Pouillard <nicolas.pouillard@g...>
Subject: Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
On 3/28/07, Joel Reymont <joelr1@gmail.com> wrote:
> Folks,
>
> I'm trying to produce a C library from my OCaml code. I found these
> instructions on the net:
>
> ocamlopt -output-obj -o fibcaml.o fib.ml
> ocamlopt -c fibwrap.c
> cp /usr/local/lib/ocaml/libasmrun.a libfib.a
> ar r libfib.a fibcaml.o fibwrap.o
>
> How would I accomplish the same with ocamlbuild, which I'm using now,
> or with OMake?
>

Just add some rules...

Here is a plugin example that I will add to the example directory.

open Ocamlbuild_plugin;;
open Command;;

let cc = A"cc";;
let ar = A"ar";;
let libasmrun = !*Ocamlbuild_pack.Ocaml_utils.stdlib_dir/"libasmrun.a";;

dispatch begin function
| After_rules ->
    rule "output C obj"
      ~dep:"%.ml"
      ~prod:"%caml.o"
      begin fun env _ ->
        let caml_o = env "%caml.o" and ml = env "%.ml" in
        Cmd(S[!Options.ocamlopt; A"-output-obj"; P ml; A"-o"; Px caml_o])
      end;
    rule "build C lib"
      ~deps:["%wrap.o"; "%caml.o"]
      ~prod:"lib%.a"
      begin fun env _ ->
        let wrap_o = env "%wrap.o" and caml_o = env "%caml.o"
        and lib_a = env "lib%.a" in
        Seq[cp libasmrun lib_a;
            Cmd(S[ar; A"r"; Px lib_a; P caml_o; P wrap_o])]
      end;
    rule "build main"
      ~deps:["libfib.a"; "main.o"]
      ~prod:"main"
      begin fun _ _ ->
        Cmd(S[cc; P"main.o"; P"libfib.a"; A"-o"; Px"main"])
      end;
| _ -> ()
end


-- 
Nicolas Pouillard