File: DuneAddLibrary.cmake

package info (click to toggle)
dune-common 2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,112 kB
  • sloc: cpp: 44,714; python: 3,480; sh: 1,590; makefile: 17
file content (303 lines) | stat: -rw-r--r-- 10,855 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# SPDX-FileCopyrightInfo: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
# SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception

#[=======================================================================[.rst:
DuneAddLibrary
--------------

Add a library to a Dune module.

.. cmake:command:: dune_add_library

  Create a new library target. There are three different interfaces following
  the standard cmake signatures.

  .. code-block:: cmake

    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]
    )

    Create 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.

  ``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``.


  .. code-block:: cmake

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

    Create 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``.


  .. code-block:: cmake

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

    Create an object library target ``<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.

  ``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.

#]=======================================================================]
include_guard(GLOBAL)


# Public interface for creating a module library
function(dune_add_library _name)
  cmake_parse_arguments(ARG "OBJECT;INTERFACE" "" "" ${ARGN})

  if(ARG_OBJECT)
    dune_add_library_object(${_name} ${ARGN})
  elseif(ARG_INTERFACE)
    dune_add_library_interface(${_name} ${ARGN})
  else()
    dune_add_library_normal(${_name} ${ARGN})
  endif()
endfunction(dune_add_library)


# ------------------------------------------------------------------------
# Internal macros and functions
# ------------------------------------------------------------------------


# Create a regular library target
function(dune_add_library_normal _name)
  cmake_parse_arguments(ARG
    "NO_EXPORT;NO_MODULE_LIBRARY;STATIC;SHARED;MODULE"
    "COMPILE_FLAGS;COMPILE_OPTIONS;OUTPUT_NAME;EXPORT_NAME"
    "ADD_LIBS;LINK_LIBRARIES;SOURCES;SOVERSION" ${ARGN})
  list(APPEND ARG_SOURCES ${ARG_UNPARSED_ARGUMENTS})
  dune_expand_object_libraries(ARG_SOURCES ARG_ADD_LIBS ARG_COMPILE_FLAGS)
  list(APPEND ARG_LINK_LIBRARIES ${ARG_ADD_LIBS})
  list(APPEND ARG_COMPILE_OPTIONS ${ARG_COMPILE_FLAGS})

  set(_type)
  if(ARG_STATIC)
    set(_type "STATIC")
  elseif(ARG_SHARED)
    set(_type "SHARED")
  elseif(ARG_MODULE)
    set(_type "MODULE")
  endif()

  if(NOT ARG_SOVERSION)
    # If no explicit version is given, we assume the ABI is not stable and
    # changes with every release.
    set(ARG_SOVERSION ${ProjectVersion})
  endif()

  # Create the library target
  add_library(${_name} ${_type} ${ARG_SOURCES})

  # Link with specified libraries from parameter ADD_LIBS
  target_link_libraries(${_name} PUBLIC "${ARG_LINK_LIBRARIES}")

  # Set target options from COMPILE_FLAGS
  target_compile_options(${_name} PUBLIC "${ARG_COMPILE_OPTIONS}")

  # Build library in ${PROJECT_BINARY_DIR}/lib
  set_target_properties(${_name} PROPERTIES
    SOVERSION "${ARG_SOVERSION}"
    LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib"
    ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")

  # Set an output name for the created library file
  if(ARG_OUTPUT_NAME)
    set_target_properties(${_name} PROPERTIES OUTPUT_NAME ${ARG_OUTPUT_NAME})
  endif()

  # Prepare the export of the library
  if(NOT ARG_NO_EXPORT)
    if(NOT ARG_EXPORT_NAME)
      set(ARG_EXPORT_NAME ${_name})
    endif()

    set_target_properties(${_name} PROPERTIES EXPORT_NAME ${ARG_EXPORT_NAME})

    # Register library in global property <module>LIBRARIES
    if(NOT ARG_NO_MODULE_LIBRARY)
      set_property(GLOBAL APPEND PROPERTY ${ProjectName}_LIBRARIES ${ARG_EXPORT_NAME})
    endif()

    # Install targets to use the libraries in other modules.
    install(TARGETS ${_name}
      EXPORT ${ProjectName}-targets DESTINATION ${CMAKE_INSTALL_LIBDIR})

    set(${ProjectName}_EXPORT_SET ${ProjectName}-targets CACHE INTERNAL "")
  elseif(NOT ARG_NO_MODULE_LIBRARY)
    # Register library in global property <module>_LIBRARIES
    set_property(GLOBAL APPEND PROPERTY ${ProjectName}_LIBRARIES ${_name})
  endif()
endfunction(dune_add_library_normal)


# Create an interface target that does not compile any sources
function(dune_add_library_interface _name)
  cmake_parse_arguments(ARG
    "NO_EXPORT;NO_MODULE_LIBRARY;INTERFACE"
    "COMPILE_FLAGS;COMPILE_OPTIONS;EXPORT_NAME"
    "ADD_LIBS;LINK_LIBRARIES" ${ARGN})
  list(APPEND ARG_LINK_LIBRARIES ${ARG_ADD_LIBS})
  list(APPEND ARG_COMPILE_OPTIONS ${ARG_COMPILE_FLAGS})

  # Create the library target
  add_library(${_name} INTERFACE)

  # Link with specified libraries from parameter LINK_LIBRARIES
  target_link_libraries(${_name} INTERFACE "${ARG_LINK_LIBRARIES}")

  # Set target options from COMPILE_FLAGS
  target_compile_options(${_name} INTERFACE "${ARG_COMPILE_OPTIONS}")

  # Prepare the export of the library
  if(NOT ARG_NO_EXPORT)
    if(NOT ARG_EXPORT_NAME)
      set(ARG_EXPORT_NAME ${_name})
    endif()

    set_target_properties(${_name} PROPERTIES EXPORT_NAME ${ARG_EXPORT_NAME})

    # Register library in global property <module>_LIBRARIES
    if(NOT ARG_NO_MODULE_LIBRARY)
      set_property(GLOBAL APPEND PROPERTY ${ProjectName}_LIBRARIES ${ARG_EXPORT_NAME})
    endif()

    # Install targets to use the libraries in other modules.
    install(TARGETS ${_name}
      EXPORT ${ProjectName}-targets DESTINATION ${CMAKE_INSTALL_LIBDIR})

    set(${ProjectName}_EXPORT_SET ${ProjectName}-targets CACHE INTERNAL "")
  elseif(NOT ARG_NO_MODULE_LIBRARY)
    # Register library in global property <module>_LIBRARIES
    set_property(GLOBAL APPEND PROPERTY ${ProjectName}_LIBRARIES ${_name})
  endif()
endfunction(dune_add_library_interface)


# Create an object library that can be used to transport a set of sources
# \deprecated
function(dune_add_library_object _name)
  message(DEPRECATION "The function dune_add_library(<obj> OBJECT ...) is deprecated. "
    "Create a regular target in a parent scope, e.g., by dune_add_library(<target>), "
    "and fill it with sources using target_sources(<target> PRIVATE <sources>...).")

  cmake_parse_arguments(ARG
    "OBJECT"
    "COMPILE_FLAGS;COMPILE_OPTIONS"
    "ADD_LIBS;LINK_LIBRARIES;SOURCES" ${ARGN})
  list(APPEND ARG_SOURCES ${ARG_UNPARSED_ARGUMENTS})
  list(TRANSFORM ARG_SOURCES PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/")
  list(APPEND ARG_LINK_LIBRARIES ${ARG_ADD_LIBS})
  list(APPEND ARG_COMPILE_OPTIONS ${ARG_COMPILE_FLAGS})

  # Register sources, libs and flags for building the library later
  define_property(GLOBAL PROPERTY DUNE_LIB_${_name}_SOURCES
    BRIEF_DOCS "Convenience property with sources for library ${_name}. DO NOT EDIT!"
    FULL_DOCS "Convenience property with sources for library ${_name}. DO NOT EDIT!")
  set_property(GLOBAL PROPERTY DUNE_LIB_${_name}_SOURCES
    "${ARG_SOURCES}")
  define_property(GLOBAL PROPERTY DUNE_LIB_${_name}_ADD_LIBS
    BRIEF_DOCS "Convenience property with libraries for library ${_name}. DO NOT EDIT!"
    FULL_DOCS "Convenience property with libraries for library ${_name}. DO NOT EDIT!")
  set_property(GLOBAL PROPERTY DUNE_LIB_${_name}_ADD_LIBS
    "${ARG_LINK_LIBRARIES}")
  define_property(GLOBAL PROPERTY DUNE_LIB_${_name}_COMPILE_FLAGS
    BRIEF_DOCS "Convenience property with compile flags for library ${_name}. DO NOT EDIT!"
    FULL_DOCS "Convenience property with compile flags for library ${_name}. DO NOT EDIT!")
  set_property(GLOBAL PROPERTY DUNE_LIB_${_name}_COMPILE_FLAGS
    "${ARG_COMPILE_OPTIONS}")
endfunction(dune_add_library_object)


function(dune_expand_object_libraries _SOURCES_var _ADD_LIBS_var _COMPILE_FLAGS_var)
  set(_new_SOURCES "")
  set(_new_ADD_LIBS "${${_ADD_LIBS_var}}")
  set(_new_COMPILE_FLAGS "${${_COMPILE_FLAGS_var}}")
  set(_regex "_DUNE_TARGET_OBJECTS:([a-zA-Z0-9_-]+)_")
  foreach(_source ${${_SOURCES_var}})
    string(REGEX MATCH ${_regex} _matched "${_source}")
    if(_matched)
      string(REGEX REPLACE "${_regex}" "\\1" _basename  "${_source}")
      foreach(var _SOURCES _ADD_LIBS _COMPILE_FLAGS)
        get_property(_prop GLOBAL PROPERTY DUNE_LIB_${_basename}${var})
        list(APPEND _new${var} "${_prop}")
      endforeach()
    else()
      list(APPEND _new_SOURCES "${_source}")
    endif()
  endforeach()

  foreach(var _SOURCES _ADD_LIBS _COMPILE_FLAGS)
    set(${${var}_var} "${_new${var}}" PARENT_SCOPE)
  endforeach()
endfunction(dune_expand_object_libraries)