caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Creating an OPAM package that wraps c functions that can be used in a non-custom utop
@ 2014-02-21 10:46 mads379
  2014-02-21 11:11 ` Daniel Bünzli
  0 siblings, 1 reply; 5+ messages in thread
From: mads379 @ 2014-02-21 10:46 UTC (permalink / raw)
  To: caml-list

I have a small OCaml library that wraps a couple of C functions that I wrote.
I'm able to compile the project to a .cmx?a file just fine using ocamlbuild.

I have the following files

src
    geolocalisation_c.c
    geolocalisation.ml
    geolocalisation.mllib

I want to be able to play around with my code in a top-level, and I'm able to
do this just fine with the following series of commands.

    ocamlbuild src/geolocalisation_c.o
    ocamlbuild -pkgs ounit src/geolocalisation.cma
    ocamlmktop -custom -cclib -lGeoIp _build/src/geolocalisation_c.o _build/
src/geolocalisation.cma -o myutop

To verify that it works I run

    ./myutop -I _build/src/
    #load "geolocalisation.cmo";;
    Geolocalisation.create_context;;

and I get the correct type-signature printed in the top-level.

The next thing I want to do is create an OPAM package for it that my co-
workers and I can use. My question is, will I be able to create the package in
such a way that the library can be loaded into utop using topfind so we don't
have to build a custom top-level in the projects that use this library?
Basically I would like the users of the library to be unaware that it invokes
external c functions.

If it is possible, do you have any pointers to where I might find some
information about how to do it?

Cheers,
Mads

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

* Re: [Caml-list] Creating an OPAM package that wraps c functions that can be used in a non-custom utop
  2014-02-21 10:46 [Caml-list] Creating an OPAM package that wraps c functions that can be used in a non-custom utop mads379
@ 2014-02-21 11:11 ` Daniel Bünzli
  2014-02-26 21:49   ` Mads Hartmann Jensen
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Bünzli @ 2014-02-21 11:11 UTC (permalink / raw)
  To: mads379; +Cc: caml-list

Le vendredi, 21 février 2014 à 11:46, mads379@gmail.com a écrit :
> The next thing I want to do is create an OPAM package for it that my co-
> workers and I can use. My question is, will I be able to create the package in
> such a way that the library can be loaded into utop using topfind so we don't
> have to build a custom top-level in the projects that use this library?

Yes.

> If it is possible, do you have any pointers to where I might find some
> information about how to do it?

First for generating the correct artefacts with ocamlbuild you can refer to these instructions:

    https://github.com/ocamllabs/ocaml-ctypes/issues/51#issuecomment-30729675

It uses ctypes so the stub C files is empty but ignore that, it also uses pkg-config for getting the cflags but you should be able to read through and simplify the myocamlbuild.ml if you don't need that.  

For the opam package use or generate a .install file at the toplevel of your distribution that will more or less look like this [1].

Feel free to ask further questions if you need to.  

Best,

Daniel

[1]
lib: [
"_build/META" {"META"}
"_build/geolocalisation.mli" {"geolocalisation.mli"}
"?_build/geolocalisation.cmti" {"geolocalisation.cmti"}
"_build/geolocalisation.cmi" {"geolocalisation.cmi"}
"_build/geolocalisation.cmx" {"geolocalisation.cmx"}
"_build/geolocalisation.cma" {"geolocalisation.cma"}
"_build/geolocalisation.a" {"geolocalisation.a"}
"_build/geolocalisation.cmxa" {"geolocalisation.cmxa"}
"_build/geolocalisation.cmxs" {"geolocalisation.cmxs"}
]
stublibs: [
"_build/dllgeolocalisation.so" {"dllgeolocalisation.so"}
]


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

* Re: [Caml-list] Creating an OPAM package that wraps c functions that can be used in a non-custom utop
  2014-02-21 11:11 ` Daniel Bünzli
@ 2014-02-26 21:49   ` Mads Hartmann Jensen
  2014-02-26 22:33     ` Daniel Bünzli
  2014-02-26 22:50     ` Daniel Bünzli
  0 siblings, 2 replies; 5+ messages in thread
From: Mads Hartmann Jensen @ 2014-02-26 21:49 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: caml-list

Hi Daniel and Christophe,

Thanks for your help so far. I think I've gotten further but I'm not 
quite there yet. I've followed the instructions that were outlined in 
the ocaml-ctypes issue and I've also created a META and .install file.

I think what I'm missing is the actual ocamlbuild and other tool 
invocations that I need to run in order to build and move the code into 
my opam repository. I'm able to build my .cma file but I'm not sure how 
all of these things (the .install and META file) play together.

So if we forget about creating an OPAM package for now and focus on 
installing the library locally so I can use it in an ocaml toplevel 
using #use topfind followed by #require "tgeoip" what commands would I 
need to invoke?

I tried running `ocamlfind install tgeoip META` to install the package 
but that just copied the meta file to 
/Users/hartmann/.opam/issuu/lib/tgeoip/META.

I've placed the code on Github for now 
https://github.com/mads379/ocaml-geoip [1] so it's easier to discuss.

Cheers,
Mads Hartmann

[1] At the moment the actual code is very specific to what we use it for 
here at ISSUU so it makes little sense to open-source it but the 
longterm goal is to provide a more general wrapper of the geoip library 
using ctypes - but for now we just want to move our existing code into a 
separate opam package :)

On 21 Feb 2014, at 12:11, Daniel Bünzli wrote:

> Le vendredi, 21 février 2014 à 11:46, mads379@gmail.com a écrit :
>> The next thing I want to do is create an OPAM package for it that my 
>> co-
>> workers and I can use. My question is, will I be able to create the 
>> package in
>> such a way that the library can be loaded into utop using topfind so 
>> we don't
>> have to build a custom top-level in the projects that use this 
>> library?
>
> Yes.
>
>> If it is possible, do you have any pointers to where I might find 
>> some
>> information about how to do it?
>
> First for generating the correct artefacts with ocamlbuild you can 
> refer to these instructions:
>
>  
> https://github.com/ocamllabs/ocaml-ctypes/issues/51#issuecomment-30729675
>
> It uses ctypes so the stub C files is empty but ignore that, it also 
> uses pkg-config for getting the cflags but you should be able to read 
> through and simplify the myocamlbuild.ml if you don't need that.
>
> For the opam package use or generate a .install file at the toplevel 
> of your distribution that will more or less look like this [1].
>
> Feel free to ask further questions if you need to.
>
> Best,
>
> Daniel
>
> [1]
> lib: [
> "_build/META" {"META"}
> "_build/geolocalisation.mli" {"geolocalisation.mli"}
> "?_build/geolocalisation.cmti" {"geolocalisation.cmti"}
> "_build/geolocalisation.cmi" {"geolocalisation.cmi"}
> "_build/geolocalisation.cmx" {"geolocalisation.cmx"}
> "_build/geolocalisation.cma" {"geolocalisation.cma"}
> "_build/geolocalisation.a" {"geolocalisation.a"}
> "_build/geolocalisation.cmxa" {"geolocalisation.cmxa"}
> "_build/geolocalisation.cmxs" {"geolocalisation.cmxs"}
> ]
> stublibs: [
> "_build/dllgeolocalisation.so" {"dllgeolocalisation.so"}
> ]

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

* Re: [Caml-list] Creating an OPAM package that wraps c functions that can be used in a non-custom utop
  2014-02-26 21:49   ` Mads Hartmann Jensen
@ 2014-02-26 22:33     ` Daniel Bünzli
  2014-02-26 22:50     ` Daniel Bünzli
  1 sibling, 0 replies; 5+ messages in thread
From: Daniel Bünzli @ 2014-02-26 22:33 UTC (permalink / raw)
  To: Mads Hartmann Jensen; +Cc: caml-list



Le mercredi, 26 février 2014 à 22:49, Mads Hartmann Jensen a écrit :

> Thanks for your help so far. I think I've gotten further but I'm not
> quite there yet. I've followed the instructions that were outlined in  
> the ocaml-ctypes issue and I've also created a META and .install file.

First if your opam package name is called ocaml-geoip that file must be called `ocaml-geoip.install` not `.install`. What you need to build is everything you want to install through the .install file. So just make a target `pkg` in your Makefile as follows:

    pkg:
        ocamlbuild -use-ocamlfind tgeoip.{mli,cmi,cmx,cma,a,cmxa,cmxs} dlltgeoip.so META

you may need to add `<src> : include` to your `_tags` file and I'm no longer sure if Makefile supports globbing as above. Verify that after a `make pkg` you have all the build artefacts mentioned in the .install file build at that place.

I don't see your `opam` file (add one at the toplevel of the project, this allows opam to use this opam file when you pin the package which can be useful when you want people to test for fixes and that your deps or build procedure has changed) but the build section should then read:

    build:  
    [
    [ "make" "pkg"]
    ]

That's all, no need to have a `remove` section, since you are using a .install file. After having executed this make invocation, opam will use the file ocaml-geoip.install to copy the files you built at the right places. See the opam developer manual [1] for more information about .install files.  
  
> I tried running `ocamlfind install tgeoip META` to install the package
> but that just copied the meta file to  
> /Users/hartmann/.opam/issuu/lib/tgeoip/META.

If you run ocamlfind by yourself you need to specify each file you want to install (basically those that are in the .install file) so that would be:

ocamlfind install tgeoip META _build/tgeoip.{mli,cmi,cmx,cma,a,cmxa,cmxs} _build/dlltgeoip.so  

See `man ocamlfind`,

Best,

Daniel

[1] https://github.com/OCamlPro/opam/raw/master/doc/dev-manual/dev-manual.pdf

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

* Re: [Caml-list] Creating an OPAM package that wraps c functions that can be used in a non-custom utop
  2014-02-26 21:49   ` Mads Hartmann Jensen
  2014-02-26 22:33     ` Daniel Bünzli
@ 2014-02-26 22:50     ` Daniel Bünzli
  1 sibling, 0 replies; 5+ messages in thread
From: Daniel Bünzli @ 2014-02-26 22:50 UTC (permalink / raw)
  To: Mads Hartmann Jensen; +Cc: caml-list

Also on this line

  https://github.com/mads379/ocaml-geoip/blob/master/myocamlbuild.ml#L40

the ~stublib argument should be "tgeoip" and this file:

  https://github.com/mads379/ocaml-geoip/blob/master/src/tgeoip.clib

should be renamed to libtgeoip.clib.

Best,

Daniel



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

end of thread, other threads:[~2014-02-26 22:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-21 10:46 [Caml-list] Creating an OPAM package that wraps c functions that can be used in a non-custom utop mads379
2014-02-21 11:11 ` Daniel Bünzli
2014-02-26 21:49   ` Mads Hartmann Jensen
2014-02-26 22:33     ` Daniel Bünzli
2014-02-26 22:50     ` Daniel Bünzli

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