Version française
Home     About     Download     Resources     Contact us    
Browse thread
getting the system architecture
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Julien Moutinho <julien.moutinho@g...>
Subject: Re: [Caml-list] getting the system architecture
On Thu, Aug 02, 2007 at 04:09:15PM -0700, Eric Merritt wrote:
>  Is there anyway to get the system architecture natively in ocaml? I
> found Sys.os_type but have yet to find anything comparable for the
> architecture.
Grepping at the Ocaml sources I see a few uses of <sys/utsname.h>,
but none of them deals with the ``machine'' field.
Apparently, they never need a runtime check of the arch.

> The alternative is to build out a function to get this
> info in C but I would rather not complicate my build
I don't know exactly how portable you want to be,
but you can also try to launch uname -m or this awful config/gnu/config.guess
either in a sub-process or in a ./configure coupled with a camlp4 definition.

> (or code in C ;)
% cat c_arch.c
#include <sys/utsname.h>

#include <caml/memory.h>
#include <caml/compatibility.h>

CAMLprim value arch_get(value unit)
{
  CAMLparam0(); /* ``unit'' is not used */
  struct utsname utsbuf;
  if (uname( &utsbuf ))
    failwith( "Arch.get" );
  CAMLreturn(copy_string( utsbuf.machine ));
}

% cat arch.ml
external get : unit -> string = "arch_get"

% cat test.ml
print_endline (Arch.get ())

% gcc -c c_arch.c
% ar rc libarch.a c_arch.o
% ocamlopt -c arch.ml
% ocamlopt -a -o arch.cmxa -cclib -larch arch.cmx
% ocamlopt -c test.ml
% ocamlopt -o test.opt -I . arch.cmxa test.cmx
% ./test.opt
i686

Hope this helps a bit.