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

ctypes stanza #3905

Closed
wants to merge 78 commits into from
Closed
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
716d790
start working on ctypes stanza
mbacarella Oct 30, 2020
40463b5
Merge branch 'master' of github.com:mbacarella/dune into ctypes-stanza
mbacarella Oct 30, 2020
ec9e8c1
pseudocode direction
mbacarella Nov 6, 2020
bbb3f93
naive first pass translation of hand-written dune file rules. with er…
mbacarella Dec 6, 2020
6f68c0c
have ctypes provide list of generated files to Dir_contents
mbacarella Dec 7, 2020
5ea1a03
lowercase
mbacarella Dec 7, 2020
f372ccd
save progress
mbacarella Dec 24, 2020
5743185
dont use Build.exec
mbacarella Dec 25, 2020
c68c1b6
no need for this variant anymore
mbacarella Dec 25, 2020
83947d8
saving work, almost almost compiles a ctypes module
mbacarella Jan 26, 2021
69a0f55
include parsing_context for :include directives
mbacarella Jan 29, 2021
a4d7fc3
minimal example works
mbacarella Jan 31, 2021
76a50c9
declare module names
mbacarella Feb 2, 2021
760b3ea
move from library to buildable stanza, so exe can use ctypes too
mbacarella Feb 4, 2021
1a0875f
finish ctypes stanza for executables
mbacarella Feb 4, 2021
8b8ca1f
inch towards a finalized ctypes config (for ctypes 0.1)
mbacarella Feb 13, 2021
ef88772
headers include or preamble
mbacarella Feb 14, 2021
545bf2e
get rid of extra cflags file. support vendoring
mbacarella Feb 14, 2021
227f520
user_error instead of failwith
mbacarella Feb 16, 2021
fdbbb13
ctypes_library is its own top-level stanza now
mbacarella Feb 21, 2021
d993a1d
decided to not do a top-level stanza after all
mbacarella Feb 22, 2021
1bd95b1
trying an approach with no intermediate libraries
mbacarella Mar 8, 2021
9d4662d
Add a test showing that Dune always pulls unused ml files
jeremiedimino Mar 8, 2021
7bd71b9
Narrow dependencies on .ml files for executables
jeremiedimino Mar 8, 2021
09fe7fd
resolve merge conflict with ctypes-stanza
mbacarella Mar 8, 2021
f0810fa
Merge remote-tracking branch 'jeremiedimino/dont-pull-all-ml-for-tran…
mbacarella Mar 8, 2021
365505e
(executables ... (ctypes ...)) almost works
mbacarella Mar 9, 2021
8a9c921
dont need the ctypes.foreign library
mbacarella Mar 10, 2021
827ec06
pull ctypes stubs into Buildable.foreign_stubs
mbacarella Mar 11, 2021
a75d8d6
implement ctypes for libraries too
mbacarella Mar 11, 2021
b1c9e40
add ctypes libraries to buildable if user uses ctypes
mbacarella Mar 12, 2021
93f69cd
use ctypes link flags in libraries
mbacarella Mar 12, 2021
ac3ad60
remove unneeded code
mbacarella Mar 13, 2021
326c279
build some blackbox tests
mbacarella Mar 25, 2021
22d7771
simplify stanza a bit
mbacarella Apr 1, 2021
c2f2521
start documenting ctypes stanza
mbacarella Apr 1, 2021
4702cb1
capital percent S
mbacarella Apr 16, 2021
64a581b
better error message
mbacarella Apr 16, 2021
44988cc
support building docs for libraries that depend on foreign_librarys
mbacarella Apr 23, 2021
968e238
dont install modules that are for stub generation
mbacarella Apr 23, 2021
12d58bf
simplifiy non_installable_modules
mbacarella Apr 24, 2021
823151f
whitespace
mbacarella Apr 24, 2021
cac6945
revert recent changes to install rules
mbacarella Apr 24, 2021
00d9efc
exclude ctypes code-gen programs from being added to archives
mbacarella Apr 24, 2021
33b73de
ctypes doc fix
mbacarella Apr 24, 2021
efcb33a
support repeated function_description stanzas
mbacarella Apr 28, 2021
4f535b8
list ctypes in CHANGES.md
mbacarella Apr 29, 2021
99aca83
fix descriptions
mbacarella May 19, 2021
90dfeed
merge main into ctypes-stanza
mbacarella May 20, 2021
e25db44
fix accidentally calling build_all twice
mbacarella May 20, 2021
233efad
fix comment
mbacarella May 20, 2021
746744d
re-add PR request number to CHANGES.md entry
mbacarella May 27, 2021
8f3227b
extension appears in dune 3.0 lang
mbacarella May 27, 2021
550e56f
Small doc tweak
rgrinberg Jul 20, 2021
8ed91d0
Move Dune_file.Ctypes to own module
rgrinberg Jul 20, 2021
60d1303
did not actually need to add this sub_systems field here
mbacarella Jul 20, 2021
35f66b1
ctypes build tests dune-project 2.8 -> 3.0
mbacarella Jul 20, 2021
3a8d53d
quick description of each unit test
mbacarella Jul 20, 2021
84ab501
remove whitespace-only changes from PR
mbacarella Jul 20, 2021
2da56ab
make fmt
mbacarella Jul 20, 2021
1655d9b
more ctypes unit tests (pkg-config probed args)
mbacarella Jul 21, 2021
0be0ea6
more docs on using ctypes, with quick example ocaml code
mbacarella Jul 22, 2021
a9a88e6
more unit test coverage
mbacarella Jul 22, 2021
4f341ec
unneeded file
mbacarella Jul 22, 2021
b175c71
coverage for preamble headers
mbacarella Jul 22, 2021
9e165d6
one more test
mbacarella Jul 22, 2021
d3519d3
Merge branch 'main' into ctypes-stanza
mbacarella Aug 2, 2021
0c46b09
resolve merge conflict
mbacarella Oct 14, 2021
b658441
Merge branch 'main' into ctypes-stanza
mbacarella Oct 15, 2021
5f57410
resolve merge conflict markers in doc
mbacarella Oct 15, 2021
3e48615
Merge branch 'ctypes-stanza' of github.com:mbacarella/dune into ctype…
mbacarella Oct 15, 2021
52ed839
fix(ctypes): busted merge conflict
rgrinberg Oct 17, 2021
8fa4100
test(ctypes): require ctypes as test dependency
rgrinberg Oct 17, 2021
627967e
test(ctypes): remove binaries
rgrinberg Oct 17, 2021
605350f
Merge branch 'main' into ctypes-stanza
rgrinberg Oct 17, 2021
18ca12c
chore(nix): add ctypes to test dependencies
rgrinberg Oct 17, 2021
9781f16
chore: add ctypes to test deps in makefile
rgrinberg Oct 17, 2021
cda682e
chore: enable ctypes tests on linux only
rgrinberg Oct 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ Unreleased
- Do not pass include directories containing native objects when compiling
bytecode (#4200, @nojb)

- If an .ml file is not used by an executable, Dune no longer report
parsing error in this file (#4330, @jeremiedimino)

- Experimental ctypes support (#3905, fixes #135, @mbacarella)


2.8.2 (21/01/2021)
------------------

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cinaps \
coq-native \
"coq>=8.12.1" \
core_bench \
ctypes \
"csexp>=1.3.0" \
js_of_ocaml \
js_of_ocaml-compiler \
Expand Down
45 changes: 45 additions & 0 deletions doc/foreign-code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,51 @@ without the ``.h`` extension. When a library install header files,
these are made visible to users of the library via the include search
path.

Support for ctypes stub generation
----------------------------------

It is possible to use the ``ctypes`` stanza to generate bindings for C
libraries without writing any C code.

You need only provide two OCaml modules, named ``Type_description`` and
``Function_description`` which describe the types, values and functions you
want to access in the C library from OCaml. Additionally, you must list
any C headers and a method for resolving build and link flags.

If you're binding a library distributed by your OS, you can use the
``pkg-config`` utility to resolve any build and link flags. Alternatively,
if you're using a locally installed library or a vendored library, you can
provide the flags manually.

The ``Type_description`` module must define a functor named ``Types`` with
signature ``Ctypes.TYPE``. The ``Function_description`` module must define
a functor named ``Functions`` with signature ``Ctypes.FOREIGN``.

.. code:: scheme

(executable
(name foo)
(libraries core)
(flags (:standard -w -9-27))
(ctypes
(external_library_name libfoo)
(build_flags_resolver pkg_config)
(headers (include "foo.h"))
(type_description
(instance Types)
(functor Type_description))
(function_description
(concurrency unlocked)
(instance Functions)
(functor Function_description))
(generated_entry_point C)))

This stanza will introduce a module named ``C`` into your project, with the
sub-modules ``Types`` and ``Functions`` that will have your fully bound C
types, constants and functions.

A complete example of using the ctypes stanza is available here (TBD).

.. _foreign-sandboxing:

Foreign build sandboxing
Expand Down
2 changes: 2 additions & 0 deletions src/dune_engine/dune_project.ml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ let equal = ( == )

let hash = Hashtbl.hash

let parsing_context t = t.parsing_context

let packages t = t.packages

let version t = t.version
Expand Down
2 changes: 2 additions & 0 deletions src/dune_engine/dune_project.mli
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ val equal : t -> t -> bool

val hash : t -> int

val parsing_context : t -> Univ_map.t

(** Return the path of the project file. *)
val file : t -> Path.Source.t

Expand Down
21 changes: 16 additions & 5 deletions src/dune_rules/cm_files.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@ type t =
; modules : Module.t list
; top_sorted_modules : Module.t list Action_builder.t
; ext_obj : string
; excluded_modules : Module_name.Set.t
}

let make ~obj_dir ~modules ~top_sorted_modules ~ext_obj =
let filter_excluded_modules t modules =
List.filter modules ~f:(fun module_ ->
let name = Module.name module_ in
not (Module_name.Set.mem t.excluded_modules name))

let make ?(excluded_modules=[]) ~obj_dir ~modules ~top_sorted_modules ~ext_obj () =
let modules = Modules.impl_only modules in
{ obj_dir; modules; top_sorted_modules; ext_obj }
let excluded_modules = Module_name.Set.of_list excluded_modules in
{ obj_dir; modules; top_sorted_modules; ext_obj; excluded_modules }

let objects_and_cms t ~mode modules =
let kind = Mode.cm_kind mode in
let modules = filter_excluded_modules t modules in
let cm_files = Obj_dir.Module.L.cm_files t.obj_dir modules ~kind in
match mode with
| Byte -> cm_files
Expand All @@ -25,8 +33,11 @@ let unsorted_objects_and_cms t ~mode = objects_and_cms t ~mode t.modules

let top_sorted_cms t ~mode =
let kind = Mode.cm_kind mode in
Action_builder.map t.top_sorted_modules
~f:(Obj_dir.Module.L.cm_files t.obj_dir ~kind)
Action_builder.map t.top_sorted_modules ~f:(fun modules ->
let modules = filter_excluded_modules t modules in
Obj_dir.Module.L.cm_files t.obj_dir ~kind modules)

let top_sorted_objects_and_cms t ~mode =
Action_builder.map t.top_sorted_modules ~f:(objects_and_cms t ~mode)
Action_builder.map t.top_sorted_modules ~f:(fun modules ->
let modules = filter_excluded_modules t modules in
objects_and_cms t ~mode modules)
4 changes: 3 additions & 1 deletion src/dune_rules/cm_files.mli
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ open Stdune
type t

val make :
obj_dir:Path.Build.t Obj_dir.t
?excluded_modules:Module_name.t list
-> obj_dir:Path.Build.t Obj_dir.t
-> modules:Modules.t
-> top_sorted_modules:Module.t list Action_builder.t
-> ext_obj:string
-> unit
-> t

val unsorted_objects_and_cms : t -> mode:Mode.t -> Path.t list
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/compilation_context.mli
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type t

(** Sets whether [-opaque] is going to be used during compilation. This
constructs a different dependency graph for native executables. In
partricular, we can omit dependency on .cmx files. For mli only modules,
particular, we can omit dependency on .cmx files. For mli only modules,
this setting is ignored and is always set when it's available. As there are
no .cmx files for such modules anyway *)
type opaque =
Expand Down
Loading