caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Producing a C wrapper with ocamlbuild or OMake
@ 2007-03-27 23:10 Joel Reymont
  2007-03-28  8:53 ` [Caml-list] " Nicolas Pouillard
  0 siblings, 1 reply; 14+ messages in thread
From: Joel Reymont @ 2007-03-27 23:10 UTC (permalink / raw)
  To: Caml List

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?

	Thanks in advance, Joel

--
http://wagerlabs.com/




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

* Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
  2007-03-27 23:10 Producing a C wrapper with ocamlbuild or OMake Joel Reymont
@ 2007-03-28  8:53 ` Nicolas Pouillard
       [not found]   ` <cd67f63a0703280158g559eff38pb6cca758682cad4a@mail.gmail.com>
  0 siblings, 1 reply; 14+ messages in thread
From: Nicolas Pouillard @ 2007-03-28  8:53 UTC (permalink / raw)
  To: Joel Reymont; +Cc: Caml List

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


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

* Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
       [not found]   ` <cd67f63a0703280158g559eff38pb6cca758682cad4a@mail.gmail.com>
@ 2007-03-28  9:04     ` Joel Reymont
  2007-03-28 12:38       ` Nicolas Pouillard
  0 siblings, 1 reply; 14+ messages in thread
From: Joel Reymont @ 2007-03-28  9:04 UTC (permalink / raw)
  To: Nicolas Pouillard; +Cc: Caml List


On Mar 28, 2007, at 9:58 AM, Nicolas Pouillard wrote:

> In fact you better should add the libasmrun definition inside the
> After_rules to not force something too soon...

This is still foreign science to me. How would I do this and also why?

Last but not least, how do I trigger the build?

Do I just run ocamlbuild without a target?

	Thanks, Joel

--
http://wagerlabs.com/






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

* Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
  2007-03-28  9:04     ` Joel Reymont
@ 2007-03-28 12:38       ` Nicolas Pouillard
  2007-03-28 13:25         ` Joel Reymont
  0 siblings, 1 reply; 14+ messages in thread
From: Nicolas Pouillard @ 2007-03-28 12:38 UTC (permalink / raw)
  To: Joel Reymont; +Cc: Caml List

On 3/28/07, Joel Reymont <joelr1@gmail.com> wrote:
>
> On Mar 28, 2007, at 9:58 AM, Nicolas Pouillard wrote:
>
> > In fact you better should add the libasmrun definition inside the
> > After_rules to not force something too soon...
>
> This is still foreign science to me. How would I do this and also why?

Yep that's my bad to use that intern variable to the standard library,
it will better safer one day.

> Last but not least, how do I trigger the build?
>
> Do I just run ocamlbuild without a target?

Nop there is no concept of default target

In this example you can run `ocamlbuild main' assuming you have all
sources files (fib.ml  fibwrap.c  main.c  myocamlbuild.ml).

PS: this example is in the CVS (in release310 branch but not yet
propagated to the pubilc server).

-- 
Nicolas Pouillard


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

* Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
  2007-03-28 12:38       ` Nicolas Pouillard
@ 2007-03-28 13:25         ` Joel Reymont
  2007-03-28 13:57           ` Nicolas Pouillard
  0 siblings, 1 reply; 14+ messages in thread
From: Joel Reymont @ 2007-03-28 13:25 UTC (permalink / raw)
  To: Nicolas Pouillard; +Cc: Caml List

Nicolas,

I think I'm missing one more bit:

ocamlbuild main

+ ocamlfind ocamlopt -package ounit -output-obj morpher.ml -o  
morphercaml.o
No implementations provided for the following modules:
   Symtab referenced from morpher.cmx
   Ninja referenced from morpher.cmx
   Easy_lexer referenced from morpher.cmx
   Easy_parser referenced from morpher.cmx
   Ppninja referenced from morpher.cmx
   Pretty referenced from morpher.cmx
   Parser_util referenced from morpher.cmx
   Ninja_morph referenced from morpher.cmx
Command exited with code 2.
Compilation unsuccessful after building 2 targets (1 cached) in  
00:00:00.

My complete plugin file is below. I can build my test program just  
fine using

ocamlbuild test.byte

but trying to produce the library gives me the above error.

What am I missing? How can I have morpher.ml automatically pull in  
the other modules?

I do open modules at the top of morpher.ml like this

open Ninja_morph
open Parser_util
module PP = Ppninja
module N = Ninja


	Thanks, Joel

---

open Ocamlbuild_plugin;;
open Command;;

let cc = A"cc";;
let ar = A"ar";;
let packages = "ounit" (* "pkg1,pkg2,..." *);;

let ocamlfind cmd =
S[A"ocamlfind"; A cmd; A"-package"; A packages];;

flag ["ocaml"; "link"] (A"-linkpkg");;

dispatch begin function
| After_options ->
      Options.ocamlc := ocamlfind "ocamlc";
      Options.ocamlopt := ocamlfind "ocamlopt";
| 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:["%_stubs.o"; "%caml.o"]
        ~prod:"lib%.a"
        begin fun env _ ->
          let wrap_o = env "%wrap.o"
          and caml_o = env "%caml.o"
          and libasmrun = ! 
*Ocamlbuild_pack.Ocaml_utils.stdlib_dir/"libasmrun.a"
          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 morpher"
        ~deps:["libmorpher.a"; "main.o"]
        ~prod:"main"
        begin fun _ _ ->
          Cmd(S[cc; P"morpher.o"; P"libmorpher.a"; A"-o"; Px"main"])
        end;
| _ -> ()
end

--
http://wagerlabs.com/






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

* Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
  2007-03-28 13:25         ` Joel Reymont
@ 2007-03-28 13:57           ` Nicolas Pouillard
  2007-03-28 14:10             ` Joel Reymont
  2007-03-28 14:44             ` Joel Reymont
  0 siblings, 2 replies; 14+ messages in thread
From: Nicolas Pouillard @ 2007-03-28 13:57 UTC (permalink / raw)
  To: Joel Reymont; +Cc: Caml List

Hum, -output-obj is more a link operation....

Using this rule:

...
    flag ["ocaml"; "link"; "output_obj"] (A"-output-obj");
    (* I will make this one part of the standard set of flags *)

    rule "output C obj"
      ~deps:["%.cmx"; "%.o"]
      ~prod:"%caml.o"
      (Ocamlbuild_pack.Ocaml_compiler.native_link "%.cmx" "%caml.o");
...

Plus having this in your _tags

<*caml.o>: output_obj

Will works fine.

On 3/28/07, Joel Reymont <joelr1@gmail.com> wrote:
> Nicolas,
>
> I think I'm missing one more bit:
>
> ocamlbuild main
>
> + ocamlfind ocamlopt -package ounit -output-obj morpher.ml -o
> morphercaml.o
> No implementations provided for the following modules:
>    Symtab referenced from morpher.cmx
>    Ninja referenced from morpher.cmx
>    Easy_lexer referenced from morpher.cmx
>    Easy_parser referenced from morpher.cmx
>    Ppninja referenced from morpher.cmx
>    Pretty referenced from morpher.cmx
>    Parser_util referenced from morpher.cmx
>    Ninja_morph referenced from morpher.cmx
> Command exited with code 2.
> Compilation unsuccessful after building 2 targets (1 cached) in
> 00:00:00.
>
> My complete plugin file is below. I can build my test program just
> fine using
>
> ocamlbuild test.byte
>
> but trying to produce the library gives me the above error.
>
> What am I missing? How can I have morpher.ml automatically pull in
> the other modules?
>
> I do open modules at the top of morpher.ml like this
>
> open Ninja_morph
> open Parser_util
> module PP = Ppninja
> module N = Ninja
>
>
>         Thanks, Joel
>
> ---
>
> open Ocamlbuild_plugin;;
> open Command;;
>
> let cc = A"cc";;
> let ar = A"ar";;
> let packages = "ounit" (* "pkg1,pkg2,..." *);;
>
> let ocamlfind cmd =
> S[A"ocamlfind"; A cmd; A"-package"; A packages];;
>
> flag ["ocaml"; "link"] (A"-linkpkg");;
>
> dispatch begin function
> | After_options ->
>       Options.ocamlc := ocamlfind "ocamlc";
>       Options.ocamlopt := ocamlfind "ocamlopt";
> | 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:["%_stubs.o"; "%caml.o"]
>         ~prod:"lib%.a"
>         begin fun env _ ->
>           let wrap_o = env "%wrap.o"
>           and caml_o = env "%caml.o"
>           and libasmrun = !
> *Ocamlbuild_pack.Ocaml_utils.stdlib_dir/"libasmrun.a"
>           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 morpher"
>         ~deps:["libmorpher.a"; "main.o"]
>         ~prod:"main"
>         begin fun _ _ ->
>           Cmd(S[cc; P"morpher.o"; P"libmorpher.a"; A"-o"; Px"main"])
>         end;
> | _ -> ()
> end
>
> --
> http://wagerlabs.com/
>
>
>
>
>
>


-- 
Nicolas Pouillard


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

* Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
  2007-03-28 13:57           ` Nicolas Pouillard
@ 2007-03-28 14:10             ` Joel Reymont
  2007-03-28 15:46               ` Nicolas Pouillard
  2007-03-28 14:44             ` Joel Reymont
  1 sibling, 1 reply; 14+ messages in thread
From: Joel Reymont @ 2007-03-28 14:10 UTC (permalink / raw)
  To: Nicolas Pouillard; +Cc: Caml List


On Mar 28, 2007, at 2:57 PM, Nicolas Pouillard wrote:

>    flag ["ocaml"; "link"; "output_obj"] (A"-output-obj");
>    (* I will make this one part of the standard set of flags *)

Wouldn't this set of flags conflict with my previous

flag ["ocaml"; "link"] (A"-linkpkg");;

I replaced the flags and now get

ocamlbuild test.byte --

+ ocamlfind ocamlc -package ounit easy_code.cmo ninja.cmo easy.cmo  
ninja_morph.cmo parser_util.cmo symtab.cmo easy_parser.cmo  
easy_lexer.cmo parser_test.cmo pretty.cmo ppninja.cmo  
ninja_morph_test.cmo test.cmo -o test.byte
Error while linking parser_test.cmo: Reference to undefined global  
`OUnit'
Command exited with code 2.

What's the flag syntax to add -linkpkg to -output-obj?

	Thanks, Joel

--
http://wagerlabs.com/






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

* Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
  2007-03-28 13:57           ` Nicolas Pouillard
  2007-03-28 14:10             ` Joel Reymont
@ 2007-03-28 14:44             ` Joel Reymont
  2007-03-28 15:49               ` Nicolas Pouillard
  1 sibling, 1 reply; 14+ messages in thread
From: Joel Reymont @ 2007-03-28 14:44 UTC (permalink / raw)
  To: Nicolas Pouillard; +Cc: Caml List

One last question... Two, actually.

How would I add another C file to the pipeline to produce the final  
executable?

Then, I tried using the library I built but I get an error. I have no  
iea what's going on, does anyone? I'm on Mac OSX if it matters.

gcc foo.c -L_build -lmorpher -o foo
/usr/bin/ld: Undefined symbols:
_caml_atom_table
_caml_code_area_end
_caml_code_area_start
_caml_static_data_end
_caml_static_data_start
collect2: ld returned 1 exit status

nm _build/libmorpher.a |grep caml_atom_table
00000400 C _caml_atom_table
          U _caml_atom_table

	Thanks, Joel

--
http://wagerlabs.com/






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

* Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
  2007-03-28 14:10             ` Joel Reymont
@ 2007-03-28 15:46               ` Nicolas Pouillard
  2007-03-28 18:58                 ` Joel Reymont
  0 siblings, 1 reply; 14+ messages in thread
From: Nicolas Pouillard @ 2007-03-28 15:46 UTC (permalink / raw)
  To: Joel Reymont; +Cc: Caml List

On 3/28/07, Joel Reymont <joelr1@gmail.com> wrote:
>
> On Mar 28, 2007, at 2:57 PM, Nicolas Pouillard wrote:
>
> >    flag ["ocaml"; "link"; "output_obj"] (A"-output-obj");
> >    (* I will make this one part of the standard set of flags *)
>
> Wouldn't this set of flags conflict with my previous

I don't know why it would. What is your _tags contents?

> I replaced the flags and now get

replaced ?


-- 
Nicolas Pouillard


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

* Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
  2007-03-28 14:44             ` Joel Reymont
@ 2007-03-28 15:49               ` Nicolas Pouillard
  2007-03-28 22:46                 ` Joel Reymont
  0 siblings, 1 reply; 14+ messages in thread
From: Nicolas Pouillard @ 2007-03-28 15:49 UTC (permalink / raw)
  To: Joel Reymont; +Cc: Caml List

On 3/28/07, Joel Reymont <joelr1@gmail.com> wrote:
> One last question... Two, actually.
>
> How would I add another C file to the pipeline to produce the final
> executable?

Add it like a link flag.

flag ["ocaml"; "link"; "output_obj"(* I think that's where you want it
*)] (A"your_file.o");

Then add it to dependencies to force it's creation.

dep ["ocaml"; "link"; "output_obj"(* I think that's where you want it
*)] ["your_file.o"];

> Then, I tried using the library I built but I get an error. I have no
> iea what's going on, does anyone? I'm on Mac OSX if it matters.
>
> gcc foo.c -L_build -lmorpher -o foo
> /usr/bin/ld: Undefined symbols:
> _caml_atom_table
> _caml_code_area_end
> _caml_code_area_start
> _caml_static_data_end
> _caml_static_data_start
> collect2: ld returned 1 exit status
>
> nm _build/libmorpher.a |grep caml_atom_table
> 00000400 C _caml_atom_table
>           U _caml_atom_table
>

No idea.

-- 
Nicolas Pouillard


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

* Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
  2007-03-28 15:46               ` Nicolas Pouillard
@ 2007-03-28 18:58                 ` Joel Reymont
  2007-03-28 21:13                   ` Nicolas Pouillard
  0 siblings, 1 reply; 14+ messages in thread
From: Joel Reymont @ 2007-03-28 18:58 UTC (permalink / raw)
  To: Nicolas Pouillard; +Cc: Caml List


On Mar 28, 2007, at 4:46 PM, Nicolas Pouillard wrote:

> I don't know why it would. What is your _tags contents?

<*caml.o>: output_obj

>> I replaced the flags and now get
>
> replaced ?

Well, yes, I have this now:

(*
flag ["ocaml"; "link"] (A"-linkpkg");;
*)

flag ["ocaml"; "link"; "output_obj"] (A"-output-obj");

Can I use flag multiple times, i.e. uncomment the first flag and have  
two of them?

	Thanks, Joel

--
http://wagerlabs.com/






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

* Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
  2007-03-28 18:58                 ` Joel Reymont
@ 2007-03-28 21:13                   ` Nicolas Pouillard
  0 siblings, 0 replies; 14+ messages in thread
From: Nicolas Pouillard @ 2007-03-28 21:13 UTC (permalink / raw)
  To: Joel Reymont; +Cc: Caml List

On 3/28/07, Joel Reymont <joelr1@gmail.com> wrote:
>
> On Mar 28, 2007, at 4:46 PM, Nicolas Pouillard wrote:
>
> > I don't know why it would. What is your _tags contents?
>
> <*caml.o>: output_obj
>
> >> I replaced the flags and now get
> >
> > replaced ?
>
> Well, yes, I have this now:
>
> (*
> flag ["ocaml"; "link"] (A"-linkpkg");;
> *)
>
> flag ["ocaml"; "link"; "output_obj"] (A"-output-obj");
>
> Can I use flag multiple times, i.e. uncomment the first flag and have
> two of them?
>

Yes you need both of them.

-- 
Nicolas Pouillard


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

* Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
  2007-03-28 15:49               ` Nicolas Pouillard
@ 2007-03-28 22:46                 ` Joel Reymont
  2007-03-29 11:20                   ` Nicolas Pouillard
  0 siblings, 1 reply; 14+ messages in thread
From: Joel Reymont @ 2007-03-28 22:46 UTC (permalink / raw)
  To: Nicolas Pouillard; +Cc: Caml List

Nicolas,

How do you supply arguments to the C compiler when it's invoked  
implicitly, to build the C wrapper, for example?

I would like to add '-g', for example, to be able to use the debugger.

	Thanks, Joel

--
http://wagerlabs.com/






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

* Re: [Caml-list] Producing a C wrapper with ocamlbuild or OMake
  2007-03-28 22:46                 ` Joel Reymont
@ 2007-03-29 11:20                   ` Nicolas Pouillard
  0 siblings, 0 replies; 14+ messages in thread
From: Nicolas Pouillard @ 2007-03-29 11:20 UTC (permalink / raw)
  To: Joel Reymont; +Cc: Caml List

On 3/29/07, Joel Reymont <joelr1@gmail.com> wrote:
> Nicolas,
>
> How do you supply arguments to the C compiler when it's invoked
> implicitly, to build the C wrapper, for example?

flag ["c"; "compile"] (A"-g");

-- 
Nicolas Pouillard


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

end of thread, other threads:[~2007-03-29 11:20 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-27 23:10 Producing a C wrapper with ocamlbuild or OMake Joel Reymont
2007-03-28  8:53 ` [Caml-list] " Nicolas Pouillard
     [not found]   ` <cd67f63a0703280158g559eff38pb6cca758682cad4a@mail.gmail.com>
2007-03-28  9:04     ` Joel Reymont
2007-03-28 12:38       ` Nicolas Pouillard
2007-03-28 13:25         ` Joel Reymont
2007-03-28 13:57           ` Nicolas Pouillard
2007-03-28 14:10             ` Joel Reymont
2007-03-28 15:46               ` Nicolas Pouillard
2007-03-28 18:58                 ` Joel Reymont
2007-03-28 21:13                   ` Nicolas Pouillard
2007-03-28 14:44             ` Joel Reymont
2007-03-28 15:49               ` Nicolas Pouillard
2007-03-28 22:46                 ` Joel Reymont
2007-03-29 11:20                   ` 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).