Skip to content

Redesign the cmake dune_add_library function

Simon Praetorius requested to merge feature/cmake-dune-add-library into master

Summary

The cmake function dune_add_library is used to create new library targets that can be installed alongside the other targets and are collected in the same export-set. This MR redesigns the implementation of that function while preserving the old functionality.

There are three different signatures to that function, following the cmake function add_library:

Creating regular library targets

dune_add_library(<basename> [STATIC|SHARED|MODULE]
    [SOURCES <sources...>]
    [LINK_LIBRARIES <targets>...]
    [COMPILE_OPTIONS "<flags>;..."]
    [OUTPUT_NAME <libname>]
    [EXPORT_NAME <exportname>]
    [NO_EXPORT]
    [NO_MODULE_LIBRARY]
)

This creates a new library target with <basename> for the library name. On Unix this created lib<libname>.so or lib<libname>.a. The target properties are automatically filled with the given (optional) arguments.

A dune library is (by default) exported into the <export-set> given by the global name ${ProjectName}-targets if the parameter NO_EXPORT is not given. This <export-set> is automatically installed and exported in the dune_finalize_project() function.

The following options can be passed to this dune_add_library function:

  • SOURCES: The source files from which to build the library.
  • LINK_LIBRARIES: A list of dependency the libraries is explicitly linked against
  • COMPILE_OPTIONS: Any additional compile flags for building the library.
  • OUTPUT_NAME: Name of the library file, e.g. lib<libname>.so or lib<libname>.a.
  • EXPORT_NAME: Name of the exported target to be used when linking against the library
  • NO_EXPORT: If omitted the library is exported for usage in other modules.
  • NO_MODULE_LIBRARY: If omitted the library is added to the global property <module>_LIBRARIES

Note, there are old options that still exist but are not advised anymore. We follow in the options names the classical cmake target property names. All additional properties are set as PUBLIC properties.

Creating interface targets

dune_add_library(<basename> INTERFACE
    [LINK_LIBRARIES <targets>...]
    [COMPILE_OPTIONS "<flags>;..."]
    [EXPORT_NAME <exportname>]
    [NO_EXPORT]
    [NO_MODULE_LIBRARY]
)

This creates an interface library target with <basename> for the library name. An interface target does not contain any sources but my contain flags and dependencies.

  • LINK_LIBRARIES: A list of dependency the libraries is explicitly linked against.
  • COMPILE_OPTIONS: Any additional compile flags for building the library.
  • EXPORT_NAME: Name of the exported target to be used when linking against the library.
  • NO_EXPORT: If omitted the library is exported for usage in other modules.
  • NO_MODULE_LIBRARY: If omitted the library is added to the global property <module>_LIBRARIES.

Creating object libraries (deprecated)

dune_add_library(<basename> OBJECT
    [SOURCES <sources...>]
    [LINK_LIBRARIES <targets>...]
    [COMPILE_OPTIONS "<flags>;..."]
)

This creates a global placeholder variable _DUNE_TARGET_OBJECTS:<basename>_ to collect multiple sources to be added to a library target later. Note, this utility is deprecated. Create a regular library target in a parent scope and add the sources directly using target_sources(<target> PRIVATE <sources>...) instead, or use standard cmake object library targets.

  • SOURCES: The source files from which to build the library.
  • LINK_LIBRARIES: A list of dependency the libraries is explicitly linked against.
  • COMPILE_OPTIONS: Any additional compile flags for building the library.
Edited by Simon Praetorius

Merge request reports

Loading