Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPR#6918: Using ocamlbuild to generate dynamic libraries (*.so/*.dll) with a C-API and an OCaml implementation? #184

Closed
damiendoligez opened this issue Mar 1, 2017 · 1 comment

Comments

@damiendoligez
Copy link
Member

PR transferred from https://caml.inria.fr/mantis/view.php?id=6918
[original reporter: @martin-neuhaeusser]

Could we enable ocamlbuild to build dynamic libraries (.so/.dll)
that are accessible via a C-API which internally uses OCaml code?

We have such a setup, where we need to provide a C-API to the outside
and use OCaml to implement the functionality.

The request is somehow the reverse of the C-stubs build:
We are not looking for a means to build stubs to C-libraries,
but to wrap OCaml code into a C-API.


The custom myocamlbuild.ml (admittedly hacked and ugly) that does the job for our setup in Windows is as follows:

open Ocamlbuild_plugin
open Command

(** This function implements the action for building the native C library *)
let library_build env (_build : builder) =
  let lib_main = env "%.cmxa" in
  let stub_obj = env "%_api.obj" in
  let final_link_cmd = Cmd (S [ S[A "ocamlfind"; A "ocamlopt"; 
                  S[A "-package"; A "ocamlgraph"];
                  S[A "-package"; A "batteries"];
                                  S[A "-cclib"; A "-implib"];
                  S[A "-cclib"; A "-maindll"];
                  A "-output-obj"; A "-linkpkg"; A "-linkall"]; S [A "-o"; A "ourlibrary.dll"]; P stub_obj; P lib_main]) in
  let pn_lib_main = Pathname.mk lib_main in
  let pn_stub_main = Pathname.mk stub_obj in
  let _ = _build [[pn_lib_main]; [pn_stub_main]] in 
  final_link_cmd

let () =
  dispatch (function
    | After_rules ->
       rule "Building C-DLL from OCaml with a C-API"
        ~prod:"%.clibrary"
        ~deps:["%.ml"; "%_api.c"; "%.h"]
            library_build;
       flag ["clibrary"] (A "-linkpkg");
       tag_file "<*/*.so>" ["clibrary"]
    | _ -> ())
@whitequark
Copy link
Member

This is what the new -output-complete-obj flag does. It's not quite functional yet, but this is essentially a duplicate of #148.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants