caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* ocamlbuild and C stubs
@ 2007-03-07 21:07 Shivkumar Chandrasekaran
  2007-03-07 22:03 ` [Caml-list] " William D. Neumann
  0 siblings, 1 reply; 3+ messages in thread
From: Shivkumar Chandrasekaran @ 2007-03-07 21:07 UTC (permalink / raw)
  To: caml-list

Hi,

Is there any (easy) way to use ocamlbuild to handle the automatic  
(re)-compilation of the C stub files to external libraries? Thanks,

--shiv--


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] ocamlbuild and C stubs
  2007-03-07 21:07 ocamlbuild and C stubs Shivkumar Chandrasekaran
@ 2007-03-07 22:03 ` William D. Neumann
  2007-03-08 12:22   ` Nicolas Pouillard
  0 siblings, 1 reply; 3+ messages in thread
From: William D. Neumann @ 2007-03-07 22:03 UTC (permalink / raw)
  To: Shivkumar Chandrasekaran; +Cc: caml-list

On Wed, 7 Mar 2007, Shivkumar Chandrasekaran wrote:

> Is there any (easy) way to use ocamlbuild to handle the automatic 
> (re)-compilation of the C stub files to external libraries? Thanks,

Well, I don't know if it's the best way (I wait for Nicolas to chime in), 
but while playing around with it I tried to get cryptokit to build with 
ocamlbuild.  What I ended up with that worked was a myocamlbuild.ml that 
looks like:

open Ocamlbuild_plugin
open Command
open Arch

let static = false

let zlib = "-DHAVE_ZLIB"
let zlib_lib="-lz"
let zlib_libdir="/usr/lib"
(* let zlib_libdir=/usr/lib64  (* for x86-64 Linux *) *)
let zlib_include="/usr/include"

let libname = "cryptokit"

let cc_map lst = List.fold_right (fun v b -> A"-ccopt"::(A v)::b) lst []
let l_ x = "-L"^x

let _a = "%.a"

let c_files =
   [ "rijndael-alg-fst";  "stubs-aes"
   ; "d3des"; "stubs-des"
   ; "arcfour"; "stubs-arcfour"
   ; "sha1"; "stubs-sha1"
   ; "sha2"; "stubs-sha2"
   ; "sha256"; "stubs-sha256"
   ; "ripemd160"; "stubs-ripemd160"
   ; "pool"; "stubs-pool"
   ; "stubs-md5" ; "stubs-zlib"; "stubs-misc"; "stubs-rng"
   ]

let c_objs = List.map (fun f -> f-.-"o") c_files

let _ = dispatch begin function
| Before_rules ->

     rule  "create C library rule"
         ~prod:_a
         ~deps:c_objs
         begin fun env build ->
           let a = env _a in
           let tags = tags_of_pathname a++"library"++"object"++"archive" in
           Cmd(S([ !Options.ocamlmklib; A"-o"; A libname ]
                 @ (List.map (fun o -> A o) c_objs) @
                 [  A(l_ zlib_libdir); A zlib_lib; T tags ]
                )
              )
         end;
     flag  [ "c"; "compile"; ] (S(A"-I"::P".."::(cc_map [("-I"^zlib_include); zlib])));
     flag  [ "library"; "ocaml" ] (S[A"-ccopt"; A(l_ zlib_libdir); A"-cclib"; A zlib_lib]);
     flag  [ "ocaml"; "program" ] (S[A"-ccopt"; A"-L."]);
     if static then flag  [ "byte"; "link" ] (A"-custom");
     flag  [ "byte"; "library"; "link" ]  (S[A"-dllib"; A"-lcryptokit"; A"-cclib"; A"-lcryptokit"]);
     flag  [ "native"; "library"; "link" ]  (S[A"-cclib"; A"-lcryptokit"]);
| _ -> ()
end

The main thing here being the rule to build the C library.  I then use an 
itarget file to build the library.  This contains

libcryptokit.a
cryptokit.cma
cryptokit.cmxa

Now, to build the library, I just issue the command
ocamlbuild libcryptokit.otarget

And then to build, for example, the test program I use something like
ocamlbuild test.native -libs nums,unix,cryptokit

I'm sure there's a bunch of cruft in there that could be better handled, 
but it seems to work well for now.

William D. Neumann

---

"There's just so many extra children, we could just feed the
children to these tigers.  We don't need them, we're not doing 
anything with them.

Tigers are noble and sleek; children are loud and messy."

         -- Neko Case

Life is unfair.  Kill yourself or get over it.
 	-- Black Box Recorder


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] ocamlbuild and C stubs
  2007-03-07 22:03 ` [Caml-list] " William D. Neumann
@ 2007-03-08 12:22   ` Nicolas Pouillard
  0 siblings, 0 replies; 3+ messages in thread
From: Nicolas Pouillard @ 2007-03-08 12:22 UTC (permalink / raw)
  To: William D. Neumann; +Cc: Shivkumar Chandrasekaran, caml-list

On 3/7/07, William D. Neumann <wneumann@cs.unm.edu> wrote:
> On Wed, 7 Mar 2007, Shivkumar Chandrasekaran wrote:
>
> > Is there any (easy) way to use ocamlbuild to handle the automatic
> > (re)-compilation of the C stub files to external libraries? Thanks,
>
> Well, I don't know if it's the best way (I wait for Nicolas to chime in),

Hi, thanks for your response :)

> but while playing around with it I tried to get cryptokit to build with
> ocamlbuild.  What I ended up with that worked was a myocamlbuild.ml that
> looks like:

Whooa, that's nice for a first try!

Let's me give some remarks and then propose a simpler solution.

> open Ocamlbuild_plugin
> open Command

> open Arch
Unused

>
> let static = false
>
> let zlib = "-DHAVE_ZLIB"
> let zlib_lib="-lz"
> let zlib_libdir="/usr/lib"
> (* let zlib_libdir=/usr/lib64  (* for x86-64 Linux *) *)
> let zlib_include="/usr/include"
>
> let libname = "cryptokit"
>
> let cc_map lst = List.fold_right (fun v b -> A"-ccopt"::(A v)::b) lst []
> let l_ x = "-L"^x
>
> let _a = "%.a"
>
> let c_files =
>    [ "rijndael-alg-fst";  "stubs-aes"
>    ; "d3des"; "stubs-des"
>    ; "arcfour"; "stubs-arcfour"
>    ; "sha1"; "stubs-sha1"
>    ; "sha2"; "stubs-sha2"
>    ; "sha256"; "stubs-sha256"
>    ; "ripemd160"; "stubs-ripemd160"
>    ; "pool"; "stubs-pool"
>    ; "stubs-md5" ; "stubs-zlib"; "stubs-misc"; "stubs-rng"
>    ]
>
> let c_objs = List.map (fun f -> f-.-"o") c_files
>
> let _ = dispatch begin function
> | Before_rules ->
>
>      rule  "create C library rule"
>          ~prod:_a
>          ~deps:c_objs
>          begin fun env build ->
>            let a = env _a in
>            let tags = tags_of_pathname a++"library"++"object"++"archive" in
>            Cmd(S([ !Options.ocamlmklib; A"-o"; A libname ]
>                  @ (List.map (fun o -> A o) c_objs) @
>                  [  A(l_ zlib_libdir); A zlib_lib; T tags ]
>                 )
>               )
>          end;

Here you define a custom rule, that's good but there is already a rule
to make C libraries.

For tags I'd have chosen "link" (a verb) "c" "library" (nouns).

>      flag  [ "c"; "compile"; ] (S(A"-I"::P".."::(cc_map [("-I"^zlib_include); zlib])));
>      flag  [ "library"; "ocaml" ] (S[A"-ccopt"; A(l_ zlib_libdir); A"-cclib"; A zlib_lib]);
>      flag  [ "ocaml"; "program" ] (S[A"-ccopt"; A"-L."]);
>      if static then flag  [ "byte"; "link" ] (A"-custom");
>      flag  [ "byte"; "library"; "link" ]  (S[A"-dllib"; A"-lcryptokit"; A"-cclib"; A"-lcryptokit"]);
>      flag  [ "native"; "library"; "link" ]  (S[A"-cclib"; A"-lcryptokit"]);

Good usage of `flag' declarations!

Having some `dep' declarations can help to sync flags and files that
need to be present.

> | _ -> ()
> end

Here is my solution:

$ cat myocamlbuild.ml
open Ocamlbuild_plugin
open Command

(* Configuration section *)
let have_zlib = "-DHAVE_ZLIB"
let zlib_lib = "-lz"
let zlib_libdir = "-L/usr/lib"
(* let zlib_libdir = "-L/usr/lib64"  (* for x86-64 Linux *) *)
let zlib_include = "-I/usr/include"

let static = true

(* List of headers *)
let headers =
  ["arcfour.h"; "d3des.h"; "rijndael-alg-fst.h";
   "ripemd160.h"; "sha1.h"; "sha256.h"]

;;

dispatch begin function
| After_rules ->

    (* When one make a C library that use the zlib with ocamlmklib,
       then issue these flags. *)
    flag ["ocamlmklib"; "c"; "use_zlib"]
         (S[A zlib_libdir; A zlib_lib]);

    (* When one compile C code using the zlib *)
    flag ["c"; "compile"; "include_zlib"]
         (S[A"-ccopt"; A zlib_include; A"-ccopt"; A have_zlib]);

    flag ["link"; "ocaml"; "library"; "use_zlib"]
         (S[A"-ccopt"; A zlib_libdir; A"-cclib"; A zlib_lib]);

    (* If `static' is true then every ocaml link in bytecode will add -custom *)
    if static then flag ["link"; "ocaml"; "byte"] (A"-custom");

    (* cryptokit is an ocaml library.
       This will declare use_cryptokit and include_cryptokit *)
    ocaml_lib "cryptokit";

    flag ["link"; "library"; "ocaml"; "byte"; "use_libcryptokit"]
         (S[A"-dllib"; A"-lcryptokit"; A"-cclib"; A"-lcryptokit"]);

    flag ["link"; "library"; "ocaml"; "native"; "use_libcryptokit"]
         (S[A"-cclib"; A"-lcryptokit"]);

    (* When ocaml link something that use the libcryptokit,
       then one need that file to be up to date. *)
    dep  ["link"; "ocaml"; "use_libcryptokit"] ["libcryptokit.a"];

    (* As an approximation all our C files use the headers.
       Note: This will import headers in the build directory. *)
    dep  ["compile"; "c"] headers;
| _ -> ()
end

$ cat _tags
<*.c>: include_zlib
"libcryptokit.a": use_zlib
<cryptokit.cm{x,}a>: use_zlib, use_libcryptokit
<{,speed}test.{byte,native}>: use_cryptokit, use_unix, use_nums

$ cat libcryptokit.clib
rijndael-alg-fst.o stubs-aes.o
d3des.o stubs-des.o
arcfour.o stubs-arcfour.o
sha1.o stubs-sha1.o
sha256.o stubs-sha256.o
ripemd160.o stubs-ripemd160.o
stubs-md5.o
stubs-zlib.o
stubs-misc.o
stubs-rng.o

$ cat cryptokit.mllib
Cryptokit

# Here it suffice to produce the test file.
$ ocamlbuild test.native
# [ ... some warnings ... ]
Finished, 26 targets (0 cached) in 00:00:04.

$ ocamlbuild test.native
Finished, 26 targets (26 cached) in 00:00:00.

$ ./test.native
# [ ... some output ... ]

$ cat all.itarget
cryptokit.cma
cryptokit.cmxa

$ cat tests.itarget
test.native
speedtest.native
test.byte
speedtest.byte

$ ocamlbuild tests.otarget
# [ ... some warnings ... ]
Finished, 35 targets (26 cached) in 00:00:01.

Hope this helps,

-- 
Nicolas Pouillard


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-03-08 12:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-07 21:07 ocamlbuild and C stubs Shivkumar Chandrasekaran
2007-03-07 22:03 ` [Caml-list] " William D. Neumann
2007-03-08 12:22   ` Nicolas Pouillard

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).