Previous week Up Next week


Here is the latest OCaml Weekly News, for the week of April 14 to 21, 2015.

  1. Suggested way to determine platform specific capabilities in build system?
  2. OCaml-openmaple
  3. Timeout
  4. Other OCaml News

Suggested way to determine platform specific capabilities in build system?


Malcolm Matalka asked and Daniel Bünzli replied:
> What is the current suggested way to determine what, roughly, autoconf
> would do for you? I have some platform specific functionality to be
> included (or excluded) depending on the OS.

I don't know if there's a suggested way but here are various ways to proceed.

If you are using ocamlbuild, you can use `Sys.win32` since 4.01.0 or
`Sys.os_type = "Win32"` to determine if you are on windows and otherwise get
the (stripped and lowercased) result of `uname -s`, see e.g (but it's missing
the win bit):

If you are using Makefiles you may want include
`$(ocamlc -where)/lib/ocaml/Makefile.config` and use the `$SYSTEM` variable.

If this is only needed for C stubs you can also try solve this at the CPP
level — but I guess this can be quite brittle — see e.g (here again missing
the win bit as it's undefined for now):

In any case make sure the value can be overridden from the command line for
cross compilation scenarios.
Thomas Gazagnaire then added:
See also the system detection functions in opam-depext:
Ivan Gotovchits also suggested:
oasis, look at and



Maxence Guesdon announced:
I started OCaml-openmaple, bindings to the Openmaple C library:

This is still work in progress but you can give it a try.
It uses the excellent OCaml-ctypes library.



Rodolphe Lepigre asked and Benjamin Greenman:
> I was wondering: is there a standard way to stop a computation after, say,
> a given number of milliseconds (or seconds) in OCaml?
> For instance I would like to have a function
> exception Timeout
> val exec : int -> ('a -> 'b) -> 'a -> 'b
> such that [exec t f x] computes [f x] but raises [Timeout] in case the
> computation did not end before [t] milliseconds (or seconds).
> My guess would be that I need to use some Unix signals magic. Has anyone
> come up with a clean solution to this problem?

Here's a small function I use, taken from the book "Developing Applications
with Objective Caml"

exception Timeout
let sigalrm_handler = Sys.Signal_handle (fun _ -> raise Timeout)
let timeout (time : int) (f : 'a -> 'b) (arg : 'a) =
   let old_behavior = Sys.signal Sys.sigalrm sigalrm_handler in
   let reset_sigalrm () = ignore (Unix.alarm 0); Sys.set_signal Sys.sigalrm old_behavior in
   ignore (Unix.alarm time) ;
   let res = f arg in reset_sigalrm () ; res
Rodolphe Lepigre then said:
Great, thank you!

I only see one problem: when [Timeout] is raised, the signal handler is not
reset. This can be fix by doing something like:

  try let res = f arg in reset_sigalrm (); res
  with e -> (reset_sigalrm (); raise e)

This will have the advantage of transmitting other exceptions to the caller
as well.

Also, I guess [Unix.alarm time] should also be in the [try ... with ...].

Other OCaml News

From the ocamlcore planet blog:
Thanks to Alp Mestan, we now include in the OCaml Weekly News the links to the
recent posts from the ocamlcore planet blog at

Mid/Senior Software Development Engineer at Lookingglass Cyber Solutions (Full-time):

Senior/Principal Software Development Engineer at Lookingglass Cyber Solutions (Full-time):

Old cwn

If you happen to miss a CWN, you can send me a message and I'll mail it to you, or go take a look at the archive or the RSS feed of the archives.

If you also wish to receive it every week by mail, you may subscribe online.

Alan Schmitt