From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id CB9487F02D for ; Mon, 20 Oct 2014 11:44:44 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of yallop@gmail.com) identity=pra; client-ip=74.125.82.49; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yallop@gmail.com"; x-sender="yallop@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of yallop@gmail.com designates 74.125.82.49 as permitted sender) identity=mailfrom; client-ip=74.125.82.49; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yallop@gmail.com"; x-sender="yallop@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wg0-f49.google.com) identity=helo; client-ip=74.125.82.49; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yallop@gmail.com"; x-sender="postmaster@mail-wg0-f49.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ak0BACvZRFRKfVIxm2dsb2JhbABcg2FND4MCyRcMh0sCgQkHFgERAQEBAQEGCwsJFC6EAwEBBAECDxEdARsPAwsBAwwGBQsNAgImAgIhAQEFDAEFARwGEwgTB4gIAQMRDaE7boswgXKDEIgIChknDWeFSgEBAQEBAQQBAQEBGAEBBA6BHoxxHYE/WAeCd4FUBZZGhA9zghGBMINGilKEZRgphSw8L4EGgUUBAQE X-IPAS-Result: Ak0BACvZRFRKfVIxm2dsb2JhbABcg2FND4MCyRcMh0sCgQkHFgERAQEBAQEGCwsJFC6EAwEBBAECDxEdARsPAwsBAwwGBQsNAgImAgIhAQEFDAEFARwGEwgTB4gIAQMRDaE7boswgXKDEIgIChknDWeFSgEBAQEBAQQBAQEBGAEBBA6BHoxxHYE/WAeCd4FUBZZGhA9zghGBMINGilKEZRgphSw8L4EGgUUBAQE X-IronPort-AV: E=Sophos;i="5.04,755,1406584800"; d="scan'208";a="102000405" Received: from mail-wg0-f49.google.com ([74.125.82.49]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 20 Oct 2014 11:44:44 +0200 Received: by mail-wg0-f49.google.com with SMTP id x12so4978480wgg.20 for ; Mon, 20 Oct 2014 02:44:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=cJm8gv5TwCrlc9W177FM5deFY2lWVrLYe1l8tTxSEXE=; b=Jwdl4jdBqjlgetMqGe8u9Z22wJqeEbGsmRSmN3StG71EJL6DOO0655fc5Q5LgNn1pY N8F+2eSC9GODz+oEZg3AhiDwgv5ybwmCrlkbnA8J2C1syj3JPbkUn84eVfCfWPC30KZW SfKs111DSuUhLp4e8drOzZAzcRtN1nNJljDGVmnCzFuilOiKooxR5qYXs8eQq5J5aHcc ZPanbzQ0CSrqJEjjbRO7cfl5Kcv2cNdIW9oI6sJYZiqtjmrEJkdieRybJoheiEBYu8He Vl7wau45eO2VmLy4qNf6anafPxfJZmWjrddptR1bZMgdcyY13++dlwmsX85O+tCyrmTx FLdg== MIME-Version: 1.0 X-Received: by 10.194.90.15 with SMTP id bs15mr30964575wjb.47.1413798284086; Mon, 20 Oct 2014 02:44:44 -0700 (PDT) Received: by 10.217.115.5 with HTTP; Mon, 20 Oct 2014 02:44:44 -0700 (PDT) In-Reply-To: References: Date: Mon, 20 Oct 2014 10:44:44 +0100 Message-ID: From: Jeremy Yallop To: Kenneth Adam Miller Cc: caml-list@inria.fr, ctypes@lists.ocaml.org Content-Type: text/plain; charset=UTF-8 Subject: Re: [Caml-list] Library recompilation with OCamljava [+cc ctypes@lists.ocaml.org] On Sat, Oct 18, 2014 at 5:09 AM, forum@x9c.fr wrote: >> I think a neat way to use a C library from an ocamljava-compiled >> program would be to have a Java "backend" for Jeremy Yallop's >> ctypes (https://github.com/ocamllabs/ocaml-ctypes). I never had >> the time to implement that, but toyed with this idea and think the >> best way to implement it would be to go through JNA >> (https://github.com/twall/jna) rather than JNI. JNA includes a >> "dlopen"-like mechanism, and automatically maps simple types from >> Java to C. My knowledge of ctypes is quite limited, but I see no >> showstopper. On 18/10/2014, Kenneth Adam Miller wrote: > Precisely! ocaml-ctypes is exactly what's being used by the library > that I'm porting to call into C sub libraries. It would be really > sweet if the ocamljava compiler could detect the ocaml-ctypes and > generate these mappings automatically. This would eliminate a lot of > error prone code, since C code tends to interpret data raw at some > point... How might I got about writing this to boot? I'm moderately > new to ocaml, lacking deep expertise in it, but I'm an aggressive > learner. Please explain the best path forward, I want to create a > robust and reusable solution. OCaml-Java support is on the wish list for ctypes https://github.com/ocamllabs/ocaml-ctypes/issues/13 but we don't have the resources to implement it at present. Adding support would mostly likely only involve changing ctypes itself, not OCaml-Java, and is likely to involve writing OCaml-Java implementations of the following components: (1) memory access, i.e. functions for allocating blocks, for reading and writing scalar values to arbitrary addresses, and for viewing C objects as bigarrays: https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes/memory_stubs.ml https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes/bigarray_stubs.ml (2) functions for printing primitive (scalar) values: https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes/value_printing_stubs.ml (3) implementations of signed and unsigned integer types of various sizes: https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes/signed.ml https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes/unsigned.ml (4) functions for converting between OCaml and C string representations https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes/std_view_stubs.ml plus one of the following approaches for calling functions (a) a "dynamic" approach, which resolves symbols and constructs call frames at runtime, like the ctypes Foreign module. This involves two components: a dynamic loading interface along the following lines https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes-foreign-base/dl.mli and primitives for dynamically constructing and making calls: https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/ctypes-foreign-base/ffi_stubs.ml (b) a "static" approach, which generates code to be compiled by the standard toolchain: https://github.com/ocamllabs/ocaml-ctypes/blob/master/src/cstubs/cstubs.mli Note that most of the links above are to internal Ctypes modules, not to the interface, which I'd expect to remain largely unchanged. As Xavier suggests, JNA may be a good starting point for some or all of the above. If someone would like to look at adding OCaml-Java support, please feel free to ask questions, either on the GitHub issue tracker, or on the ctypes mailing list. Jeremy.