Skip to content

[RFC] Clean up and extend existing support for C/C++ libraries #2644

@snowleopard

Description

@snowleopard

@diml, @aalekseyev and I would like to clean up and extend existing support for C/C++ libraries in Dune, which requires a way to describe the following information:

  • The C/C++ library's name, i.e. the name of the resulting .a file. This name can be used as a reference in the self_build_stubs_archive field which could probably be aliased as c_archive for simplicity. A possible further extension is to also support public_name as in OCaml libraries.
  • The set of all C/C++ files.
  • The list of command-line flags: both common, as well as C and C++ specific ones.
  • The set of include directories: again both common, as well as C and C++ specific ones.

We can describe the above using the following top-level c_library stanza:

(c_library

  (archive_name my_lib)

  (c_names file1 file2 ...)
  (cxx_names file1 file2 ...)

  (flags common_flag1 common_flag2 ...)
  (c_flags c_flag1 c_flag2 ...)
  (cxx_flags cxx_flag1 cxx_flag2 ...) 

  (include_dirs common_dir1 %{lib:another_lib} ...)
  (c_include_dirs c_dir1 c_dir2 ...)
  (cxx_include_dirs cxx_dir1 cxx_dir2 ...)

  (extra_deps extra_file1 extra_file2 ...)
)

Here is the semantics:

  • The C/C++ compiler is called on each file matching *_names in a sandbox (duplicate name matches are disallowed as in the current implementation). All flags will be passed to every invocation of the compiler; c_flags will be passed when compiling C files and cxx_flags will be passed when compiling C++ files. All the resulting objects are placed into archive_name.a file.
  • The header directories from *include_dirs will be available and tracked as dependencies. They will also appear in the command line in -I flags. Note that you can refer to public headers of other (OCaml) libraries using the %{lib:another_lib} syntax.
  • Additionally, extra_deps will be available and tracked to support ad hoc include dependencies.

One can also use the same stanza as a part of an OCaml library definition via c_stubs:

(library 

...

  (c_stubs (c_names ... ) ...)

...
)

Any comments/suggestions are welcome!


See a generalisation of this proposal in #2650.

Metadata

Metadata

Assignees

Labels

proposalRFC's that are awaiting discussion to be accepted or rejected

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions