From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: ** X-Spam-Status: No, score=2.3 required=5.0 tests=AWL,DNS_FROM_RFC_POST, MISSING_HEADERS,SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 5EEC2BBC4 for ; Mon, 16 Mar 2009 13:56:42 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AukDAAPpvUnRVduumGdsb2JhbACMcYcxYD8BAQEBAQgJDAcRsBwHgQCNJAEDAQODfAaDN4FM X-IronPort-AV: E=Sophos;i="4.38,373,1233529200"; d="scan'208";a="24386163" Received: from mail-ew0-f174.google.com ([209.85.219.174]) by mail3-smtp-sop.national.inria.fr with ESMTP; 16 Mar 2009 13:56:41 +0100 Received: by ewy22 with SMTP id 22so4123797ewy.27 for ; Mon, 16 Mar 2009 05:56:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:reply-to :user-agent:mime-version:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=g96pQWOhFixsvSwOnTw7dF1pW93YcW5vATkWLRNpIlg=; b=S8l20puTEq13I1cFueFtZM1EW+6TLOCd2BejR0DEMYw8qReEftqXXIumdhFA6qcjPF z27dxdGL5IT5aC/rk5SE9ySAXlprAInOxkdgzuKDS8/3w2l4expZo5XV3oKZj5uLkgRn TT3d41+kPVgAimU2DuTizZXGj8nCRmX8K5JtI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:reply-to:user-agent:mime-version:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=ewKhnmlNidFROBebe6hjVTZe2oU4u0NoFHfZPGIggE0Dqp2/KUe8HLnQVg94R3Xpmq fhGsVsTfXpq7ovsO8KXzsJ0zGTuFEul9EdjKwkeYOs9s6dmJktC104+UEK33qmfguHXU z7+i5irQGEi3Fwjve+OxeZA1jD3H6GaB6S0QQ= Received: by 10.216.53.195 with SMTP id g45mr1793484wec.112.1237208201665; Mon, 16 Mar 2009 05:56:41 -0700 (PDT) Received: from ?192.168.0.102? (papillon.metalscan.fr [212.73.210.234]) by mx.google.com with ESMTPS id h6sm9621305nfh.15.2009.03.16.05.56.40 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 16 Mar 2009 05:56:40 -0700 (PDT) Message-ID: <49BE4C87.9050808@gmail.com> Date: Mon, 16 Mar 2009 13:56:39 +0100 From: Matthieu Dubuget Reply-To: matthieu.dubuget@gmail.com User-Agent: Thunderbird 2.0.0.19 (X11/20090105) MIME-Version: 1.0 Cc: O'Caml Mailing List Subject: Re: [Caml-list] dll from ocaml code References: <38CFD1B3-7B91-4F7A-80E0-354A9FD29E56@gmail.com> In-Reply-To: <38CFD1B3-7B91-4F7A-80E0-354A9FD29E56@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; matthieu:01 dubuget:01 matthieu:01 dubuget:01 ocaml:01 ocaml:01 -output-obj:01 cmx:01 pathname:01 dirname:01 objs:01 uncap:01 cmx:01 deps:01 objs:01 Joel Reymont a =C3=A9crit : > Does anyone have a recipe for packaging OCaml code as a DLL or shared > library? yes > > I need to be able to expose a few entry points that correspond to > OCaml functions. Suppose that you add this line: flag ["link";"cmldll"] (S[A"-output-obj"]); and this rule in the After_rules section of myocamlbuild.ml plugin: rule "Mixed C-Ocaml native DLL: cmldll & o* & cmx* -> native DLL (.dll | .so)" ~dep:"%.cmldll" ~prod:("%.native" -.- ext_dll) begin fun env build -> let output =3D env ("%.native" -.- ext_dll) and input =3D env "%.cmldll" in let dir =3D Pathname.dirname input in let ext_o_files, moduls_files =3D string_list_of_file input |> List.partition (fun fic -> Filename.check_suffix fic ".o") in let objs =3D ext_o_files |> List.map Filename.chop_extension |> List.map (fun fic -> fic -.- ext_obj) in let cmxs =3D moduls_files |> List.map (fun modul -> (uncap_module_path modul) -.- "cmx") in let deps =3D cmxs @ objs in List.iter ignore_good (build (List.map (fun x -> [dir/x]) deps)); Cmd (S [!Options.ocamlopt; A"-o"; Px output; T (tags_of_pathname output++"ocaml"++"native"++"cmldll"++"link"); atomize_paths deps ]) end; where ext_dll is the dll file extension on your system, ext_obj is tho obj files extension on your system, let uncap_module_path p =3D (Pathname.dirname p) / (String.uncapitalize (Pathname.basename p)), In order to build my.native.dll, you have to put in my.cmldll file: - the ml Modules you want to link in your dll - the associated obj files, obtained from C stubs. And ocamlbuild my.native.dll will show you the working command lines=E2=80= =A6 Now, what to put in your C stub and Modules? First, one module with your OCaml code. Then, maybe a separated module that contains Callback.register calls, or you have to had them in to your module if you prefer. Then the C part: - first step is to init the caml part (see caml_startup in the manual), either automatically upon dll loading (in DllMain or _init function, depending on you system) or in a function to be called by the DLL client - then write the C functions, that will calls your registered callbacks I have no real example here around. But could find one somewhere on my home's computer if necessary. Hoping this helps Matt